Dialer.xaml.cs 9.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
Dialer.xaml.cs
Copyright (C) 2015  Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
15 16 17 18 19 20
*/

using Windows.UI.Xaml.Controls;
using Linphone.Model;
using Windows.UI.Xaml.Navigation;
using System;
Erwan Croze's avatar
Erwan Croze committed
21
using Linphone;
22
using Windows.UI.Xaml;
23
using Windows.UI.Xaml.Input;
24
using System.ComponentModel;
25
using System.Collections.Generic;
26

Erwan Croze's avatar
Erwan Croze committed
27
namespace Linphone.Views {
28

Erwan Croze's avatar
Erwan Croze committed
29
    public sealed partial class Dialer : Page, INotifyPropertyChanged {
30

Erwan Croze's avatar
Erwan Croze committed
31
        public Dialer() {
32
            this.InitializeComponent();
33
            DataContext = this;
34
            ContactsManager contactsManager = ContactsManager.Instance;
35 36 37 38 39
            addressBox.KeyDown += (sender, args) => {
                if (args.Key == Windows.System.VirtualKey.Enter) {
                    call_Click(null, null);
                }
            };
40 41
        }

42
        private int unreadMessageCount;
Erwan Croze's avatar
Erwan Croze committed
43 44
        public int UnreadMessageCount {
            get {
45 46 47
                return unreadMessageCount;
            }

Erwan Croze's avatar
Erwan Croze committed
48
            set {
49
                unreadMessageCount = value;
Erwan Croze's avatar
Erwan Croze committed
50 51 52
                if (unreadMessageCount > 0) {
                    unreadMessageText.Visibility = Visibility.Visible;
                } else {
53
                    unreadMessageText.Visibility = Visibility.Collapsed;
54 55
                }

Erwan Croze's avatar
Erwan Croze committed
56
                if (PropertyChanged != null) {
57 58 59 60 61 62
                    PropertyChanged(this, new PropertyChangedEventArgs("UnreadMessageCount"));
                }
            }
        }

        private int missedCallCount;
Erwan Croze's avatar
Erwan Croze committed
63 64
        public int MissedCallCount {
            get {
65 66 67
                return missedCallCount;
            }

Erwan Croze's avatar
Erwan Croze committed
68
            set {
69
                missedCallCount = value;
Erwan Croze's avatar
Erwan Croze committed
70
                if (missedCallCount > 0) {
71
                    MissedCallText.Visibility = Visibility.Visible;
Erwan Croze's avatar
Erwan Croze committed
72
                } else {
73 74 75
                    MissedCallText.Visibility = Visibility.Collapsed;
                }

76
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MissedCallCount"));
77 78 79 80 81
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

Erwan Croze's avatar
Erwan Croze committed
82 83 84 85 86 87 88 89 90
        private void LogUploadProgressIndication(int offset, int total) {
            /* base.UIDispatcher.BeginInvoke(() =>
             {
                 BugReportUploadProgressBar.Maximum = total;
                 if (offset <= total)
                 {
                     BugReportUploadProgressBar.Value = offset;
                 }
             });*/
91 92
        }

Erwan Croze's avatar
Erwan Croze committed
93
        private void RegistrationChanged(ProxyConfig config, RegistrationState state, string message) {
94 95 96
            status.RefreshStatus();
        }

Erwan Croze's avatar
Erwan Croze committed
97
        private void MessageReceived(ChatRoom room, ChatMessage message) {
Margaux Clerc's avatar
Margaux Clerc committed
98

99 100 101
            UnreadMessageCount = LinphoneManager.Instance.GetUnreadMessageCount();
        }

Erwan Croze's avatar
Erwan Croze committed
102
        protected override void OnNavigatedTo(NavigationEventArgs e) {
103 104 105 106
            base.OnNavigatedTo(e);

            LinphoneManager.Instance.CoreDispatcher = Dispatcher;
            LinphoneManager.Instance.RegistrationChanged += RegistrationChanged;
107
            LinphoneManager.Instance.MessageReceived += MessageReceived;
108
            LinphoneManager.Instance.CallStateChangedEvent += CallStateChanged;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
            status.RefreshStatus();
            /*    if (e.NavigationMode == NavigationMode.New)
                {
                    if (BugCollector.HasExceptionToReport())
                    {
                        // Allow to report exceptions before the creation of the core in case the problem is in there
                        CustomMessageBox reportIssueDialog = new CustomMessageBox()
                        {
                            Caption = AppResources.ReportCrashDialogCaption,
                            Message = AppResources.ReportCrashDialogMessage,
                            LeftButtonContent = AppResources.ReportCrash,
                            RightButtonContent = AppResources.Close
                        };

                        reportIssueDialog.Dismissed += (s, ev) =>
                        {
                            switch (ev.Result)
                            {
                                case CustomMessageBoxResult.LeftButton:
                                    BugReportUploadProgressBar.Minimum = 0;
                                    BugReportUploadProgressBar.Maximum = 100;
                                    BugReportUploadPopup.Visibility = Visibility.Visible;
                                    LinphoneManager.Instance.LogUploadProgressIndicationEH += LogUploadProgressIndication;
                                    LinphoneManager.Instance.LinphoneCore.UploadLogCollection();
                                    break;
                                case CustomMessageBoxResult.RightButton:
                                    BugCollector.DeleteFile();
                                    break;
                            }
                        };

                        reportIssueDialog.Show();
                    }
                    else
                    {
                        BugReportUploadPopup.Visibility = Visibility.Collapsed;
                    }
                }*/
Erwan Croze's avatar
Erwan Croze committed
147 148

            if (LinphoneManager.Instance.Core.CallsNb > 0) {
149
                Call call = LinphoneManager.Instance.Core.CurrentCall;
150 151 152
                List<String> parameters = new List<String>();
                parameters.Add(call.RemoteAddress.AsStringUriOnly());
                Frame.Navigate(typeof(Views.InCall), parameters);
153
            }
154

Erwan Croze's avatar
Erwan Croze committed
155
            if (LinphoneManager.Instance.GetUnreadMessageCount() > 0) {
156 157
                UnreadMessageCount = LinphoneManager.Instance.GetUnreadMessageCount();
            }
158

Erwan Croze's avatar
Erwan Croze committed
159
            if (LinphoneManager.Instance.Core.MissedCallsCount > 0) {
160 161 162
                MissedCallCount = LinphoneManager.Instance.Core.MissedCallsCount;
            }

Erwan Croze's avatar
Erwan Croze committed
163
            if (e.Parameter is String && (e.Parameter as String)?.Length > 0 && e.NavigationMode != NavigationMode.Back) {
164
                String arguments = e.Parameter as String;
165
                addressBox.Text = arguments;
Erwan Croze's avatar
Erwan Croze committed
166
                try {
Erwan Croze's avatar
Erwan Croze committed
167
                    Address address = LinphoneManager.Instance.Core.InterpretUrl(e.Parameter as String);
168 169
                    String sipAddressToCall = address.AsStringUriOnly();
                    addressBox.Text = sipAddressToCall;
Erwan Croze's avatar
Erwan Croze committed
170
                } catch (Exception exception) {
171 172
                }
            }
173 174
        }

Erwan Croze's avatar
Erwan Croze committed
175
        private void CallStateChanged(Call call, CallState state) {
176 177 178
            MissedCallCount = LinphoneManager.Instance.Core.MissedCallsCount;
        }

Erwan Croze's avatar
Erwan Croze committed
179
        protected override void OnNavigatedFrom(NavigationEventArgs nee) {
180
            base.OnNavigatedFrom(nee);
Erwan Croze's avatar
Erwan Croze committed
181 182
            // LinphoneManager.Instance.LogUploadProgressIndicationEH -= LogUploadProgressIndication;
            // BugReportUploadPopup.Visibility = Visibility.Collapsed;
183 184
        }

Erwan Croze's avatar
Erwan Croze committed
185 186
        private void call_Click(object sender, RoutedEventArgs e) {
            if (addressBox.Text.Length > 0) {
187
                LinphoneManager.Instance.NewOutgoingCall(addressBox.Text);
Erwan Croze's avatar
Erwan Croze committed
188
            } else {
189 190 191 192 193
                string lastDialedNumber = LinphoneManager.Instance.GetLastCalledNumber();
                addressBox.Text = lastDialedNumber == null ? "" : lastDialedNumber;
            }
        }

Erwan Croze's avatar
Erwan Croze committed
194
        private void numpad_Click(object sender, RoutedEventArgs e) {
195 196
            Button button = sender as Button;
            String tag = button.Tag as String;
Erwan Croze's avatar
Erwan Croze committed
197
            LinphoneManager.Instance.Core.PlayDtmf(Convert.ToSByte(tag.ToCharArray()[0]), 1000);
198

199
            addressBox.Text += tag;
200 201
        }

Erwan Croze's avatar
Erwan Croze committed
202
        private void VoicemailClick(object sender, RoutedEventArgs e) {
203 204 205

        }

Erwan Croze's avatar
Erwan Croze committed
206
        private void zero_Hold(object sender, RoutedEventArgs e) {
207
            if (addressBox.Text.Length > 0)
208
                addressBox.Text = addressBox.Text.Substring(0, addressBox.Text.Length - 1);
209 210 211
            addressBox.Text += "+";
        }

Erwan Croze's avatar
Erwan Croze committed
212
        private void chat_Click(object sender, RoutedEventArgs e) {
213 214 215
            Frame.Navigate(typeof(Views.Chats), null);
        }

Erwan Croze's avatar
Erwan Croze committed
216
        private void history_Click(object sender, RoutedEventArgs e) {
217 218 219
            Frame.Navigate(typeof(Views.History), null);
        }

Erwan Croze's avatar
Erwan Croze committed
220
        private void contacts_Click(object sender, RoutedEventArgs e) {
221 222 223
            Frame.Navigate(typeof(Views.ContactList), null);
        }

Erwan Croze's avatar
Erwan Croze committed
224
        private void settings_Click(object sender, RoutedEventArgs e) {
225 226 227
            Frame.Navigate(typeof(Views.Settings), null);
        }

Erwan Croze's avatar
Erwan Croze committed
228
        private void about_Click(object sender, RoutedEventArgs e) {
229 230 231
            Frame.Navigate(typeof(Views.About), null);
        }

Erwan Croze's avatar
Erwan Croze committed
232
        private void disconnect_Click(object sender, RoutedEventArgs e) {
233 234 235
            EnableRegister(false);
        }

Erwan Croze's avatar
Erwan Croze committed
236
        private void connect_Click(object sender, EventArgs e) {
237 238 239
            EnableRegister(true);
        }

Erwan Croze's avatar
Erwan Croze committed
240
        private void EnableRegister(bool enable) {
241 242
            Core lc = LinphoneManager.Instance.Core;
            ProxyConfig cfg = lc.DefaultProxyConfig;
Erwan Croze's avatar
Erwan Croze committed
243
            if (cfg != null) {
244
                cfg.Edit();
Erwan Croze's avatar
Erwan Croze committed
245
                cfg.RegisterEnabled = enable;
246 247 248 249
                cfg.Done();
            }
        }

Erwan Croze's avatar
Erwan Croze committed
250
        private void status_Tapped(object sender, TappedRoutedEventArgs e) {
251
            LinphoneManager.Instance.Core.RefreshRegisters();
252 253 254
        }
    }
}