sipsorcery-mono /sipsorcery-silverlight/SIPSorcery.GUI/UserInterface/AssetManagers/SIPAccountManager.xaml.cs

Language C# Lines 514
MD5 Hash 197f9ae0cd1430fd28543373f5330cac
Repository https://github.com/thecc4re/sipsorcery-mono.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SIPSorcery.SIP.App;
using SIPSorcery.Persistence;
using SIPSorcery.Silverlight.Messaging;
using SIPSorcery.UIControls;
using SIPSorcery.SIPSorceryProvisioningClient;

namespace SIPSorcery
{   
    public delegate void ManagerHeightChanged(double newHeight);

    public partial class SIPAccountManager : UserControl
	{
        private const int SIPACCOUNTS_DISPLAY_COUNT = 25;
        private const int SIPBINDINGS_DISPLAY_COUNT = 25;

        private ActivityMessageDelegate LogActivityMessage_External;
        private ActivityProgressDelegate ShowActivityProgress_External;
        private GetDialPlanNamesDelegate GetDialPlanNames_External;
        private SIPSorceryPersistor m_persistor;

        private ObservableCollection<SIPAccount> m_sipAccounts;
        private Dictionary<string, List<string>> m_ownerDomains = new Dictionary<string, List<string>>();           // [owner, List<domain names>]. 

        private SIPAccount m_selectedSIPAccount;
        private SIPAccountDetailsControl m_addControl;
        private SIPAccountDetailsControl m_editControl;
        private int m_sipAccountsPanelOffset;
        private int m_sipAccountsPanelCount;
        private int m_sipBindingsPanelOffset;
        private int m_sipBindingsPanelCount;

        private string m_owner;
        private string m_sipDomainsWhere;       // Used when filtering is enabled.
        private string m_sipAccountsWhere;      // Used when filtering is enabled.
        private string m_sipBindingsWhere;      // Used when filtering is enabled.

        public bool Initialised;
        private bool m_initialLoadComplete;
        private bool m_sipDomainsLoaded;
        private bool m_sipAccountsLoaded;
        private bool m_sipBindingsLoaded;
        private bool m_sipAccountLoadInProgress;
        private bool m_sipAccountsPanelRefreshInProgress;
        private bool m_sipBindingsPanelRefreshInProgress;

        public SIPAccountManager()
        {
            InitializeComponent();
        }

        public SIPAccountManager(
            ActivityMessageDelegate logActivityMessage,
            ActivityProgressDelegate showActivityProgress,
            SIPSorceryPersistor persistor,
            GetDialPlanNamesDelegate getDialPlanNames,
            string owner)
		{
			InitializeComponent();

            LogActivityMessage_External = logActivityMessage;
            ShowActivityProgress_External = showActivityProgress;
            GetDialPlanNames_External = getDialPlanNames;
            m_persistor = persistor;
            m_owner = owner;

            m_sipAccountsPanel.SetTitle("SIP Accounts");
            m_sipAccountsPanel.DisplayCount = SIPACCOUNTS_DISPLAY_COUNT;
            m_sipAccountsPanel.MenuEnableFilter(false);
            m_sipAccountsPanel.MenuEnableDelete(false);
            m_sipAccountsPanel.Add += new MenuButtonClickedDelegate(SIPAccountsAssetViewer_Add);
            m_sipAccountsPanel.GetAssetList = GetSIPAccounts;

            m_sipBindingsPanel.SetTitle("SIP Bindings");
            m_sipBindingsPanel.DisplayCount = SIPBINDINGS_DISPLAY_COUNT;
            m_sipBindingsPanel.MenuEnableAdd(false);
            m_sipBindingsPanel.MenuEnableFilter(false);
            m_sipBindingsPanel.MenuEnableDelete(false);
            m_sipBindingsPanel.GetAssetList = GetSIPBindings;
		}

        public void Initialise()
        {
            Initialised = true;

            m_persistor.GetSIPDomainsComplete += GetSIPDomainsComplete;
            m_persistor.GetSIPAccountsCountComplete += GetSIPAccountsCountComplete;
            m_persistor.GetSIPAccountsComplete += GetSIPAccountsComplete;
            m_persistor.UpdateSIPAccountComplete += UpdateSIPAccountComplete;
            m_persistor.DeleteSIPAccountComplete += DeleteSIPAccountComplete;
            m_persistor.AddSIPAccountComplete += AddSIPAccountComplete;
            m_persistor.GetRegistrarBindingsCountComplete += GetRegistrarBindingsCountComplete;
            m_persistor.GetRegistrarBindingsComplete += GetRegistrarBindingsComplete;

            Load();
        }

        private void Load()
        {
            try
            {
                if (!m_sipDomainsLoaded)
                {
                    ShowActivityProgress_External(25);
                    LogActivityMessage_External(MessageLevelsEnum.Info, "Loading SIP Domains...");
                    m_persistor.GetSIPDomainsAsync(m_sipDomainsWhere, 0, Int32.MaxValue);
                }
                else if (!m_sipAccountsLoaded)
                {
                    ShowActivityProgress_External(50);
                    LogActivityMessage_External(MessageLevelsEnum.Info, "Loading SIP Accounts...");
                    m_sipAccountsPanel.RefreshAsync();
                }
                else if (!m_sipBindingsLoaded)
                {
                    ShowActivityProgress_External(75);
                    LogActivityMessage_External(MessageLevelsEnum.Info, "Loading SIP Bindings...");
                    m_sipBindingsPanel.RefreshAsync();
                }
                else
                {
                    ShowActivityProgress_External(null);
                    m_initialLoadComplete = true;
                }
            }
            catch (Exception excp)
            {
                LogActivityMessage_External(MessageLevelsEnum.Error, "Exception thrown loading the SIP Account Manager. " + excp.Message);
            }
        }

        public void Close()
        {
            m_persistor.GetSIPAccountsCountComplete -= GetSIPAccountsCountComplete;
            m_persistor.GetSIPAccountsComplete -= GetSIPAccountsComplete;
            m_persistor.GetSIPDomainsComplete -= GetSIPDomainsComplete;
            m_persistor.UpdateSIPAccountComplete -= UpdateSIPAccountComplete;
            m_persistor.DeleteSIPAccountComplete -= DeleteSIPAccountComplete;
            m_persistor.AddSIPAccountComplete -= AddSIPAccountComplete;
            m_persistor.GetRegistrarBindingsCountComplete -= GetRegistrarBindingsCountComplete;
        }

        public void SIPMonitorMachineEventHandler(SIPMonitorMachineEvent machineEvent)
        {
            // Update the bindings display.
            if (m_initialLoadComplete && !m_sipAccountsPanelRefreshInProgress)
            {
                m_sipBindingsPanel.RefreshAsync();
            }
        }

        private void GetSIPDomainsComplete(GetSIPDomainsCompletedEventArgs e)
        {
            try
            {
                ObservableCollection<SIPDomain> domains = e.Result;

                if (domains != null)
                {
                    LogActivityMessage_External(MessageLevelsEnum.Info, domains.Count + " successfully loaded.");

                    foreach (SIPDomain domain in domains)
                    {
                        if (m_ownerDomains.ContainsKey(m_owner))
                        {
                            List<string> ownerDomains = m_ownerDomains[m_owner];
                            if (!ownerDomains.Contains(domain.Domain))
                            {
                                ownerDomains.Add(domain.Domain);
                            }
                        }
                        else
                        {
                            m_ownerDomains.Add(m_owner, new List<string>() { domain.Domain });
                        }
                    }
                }
                else
                {
                    LogActivityMessage_External(MessageLevelsEnum.Warn, "No domains could be loaded.");
                }

                m_sipDomainsLoaded = true;
                Load();
            }
            catch (Exception excp)
            {
                string excpMessage = (excp.InnerException != null) ? excp.InnerException.Message : excp.Message;
                LogActivityMessage_External(MessageLevelsEnum.Error, "There was an error retrieving the SIP Domains. " + excpMessage);
            }
        }

        #region SIP Account functions.

        private void GetSIPAccounts(int offset, int count)
        {
            if (!m_sipAccountsPanelRefreshInProgress)
            {
                m_sipAccountsPanelRefreshInProgress = true;
                m_sipAccountLoadInProgress = true;

                m_sipAccountsPanelOffset = offset;
                m_sipAccountsPanelCount = count;
                m_persistor.GetSIPAccountsCountAsync(m_sipAccountsWhere);
            }
            else
            {
                LogActivityMessage_External(MessageLevelsEnum.Warn, "A SIP accounts refresh is already in progress.");
            }
        }

        private void GetSIPAccountsCountComplete(GetSIPAccountsCountCompletedEventArgs e)
        {
            try
            {
                m_sipAccountsPanel.AssetListTotal = e.Result;
                m_persistor.GetSIPAccountsAsync(m_sipAccountsWhere, m_sipAccountsPanelOffset, m_sipAccountsPanelCount);
            }
            catch (Exception excp)
            {
                string excpMessage = (excp.InnerException != null) ? excp.InnerException.Message : excp.Message;
                LogActivityMessage_External(MessageLevelsEnum.Error, "There was an error retrieving the number of SIP accounts. " + excpMessage);

                m_sipAccountLoadInProgress = false;
                m_sipAccountsPanelRefreshInProgress = false;
            }
        }

        private void GetSIPAccountsComplete(GetSIPAccountsCompletedEventArgs e)
        {
            m_sipAccountsLoaded = true;

            try
            {
                m_sipAccounts = e.Result;
                m_sipAccountsPanel.SetAssetListSource(m_sipAccounts);

                if (!m_initialLoadComplete)
                {
                    Load();
                }

                LogActivityMessage_External(MessageLevelsEnum.Info, "SIP Accounts successfully loaded " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + ".");
            }
            catch (Exception excp)
            {
                string excpMessage = (excp.InnerException != null) ? excp.InnerException.Message : excp.Message;
                LogActivityMessage_External(MessageLevelsEnum.Error, "There was an error retrieving SIP accounts. " + excpMessage);
            }
            finally
            {
                m_sipAccountLoadInProgress = false;
                m_sipAccountsPanelRefreshInProgress = false;
            }
        }

        private void SIPAccountAdd(SIPAccount sipAccount)
        {
            m_persistor.AddSIPAccountAsync(sipAccount);
        }

        private void AddSIPAccountComplete(AddSIPAccountCompletedEventArgs e)
        {
            try
            {
                SIPAccount sipAccount = e.Result;

                if (m_addControl != null)
                {
                    m_addControl.WriteStatusMessage(MessageLevelsEnum.Info, "SIP Account was successfully created for " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + ".");
                }

                if (m_sipAccounts == null)
                {
                    m_sipAccountsPanel.RefreshAsync();
                }
                else
                {
                    m_sipAccounts.Add(sipAccount);
                    m_sipAccountsPanel.AssetAdded();
                }
            }
            catch (Exception excp)
            {
                string excpMessage = (excp.InnerException != null) ? excp.InnerException.Message : excp.Message;
                if (m_addControl != null)
                {
                    m_addControl.WriteStatusMessage(MessageLevelsEnum.Error, excpMessage);
                }
                else
                {
                    LogActivityMessage_External(MessageLevelsEnum.Error, "There was an error adding a SIP Account. " + excpMessage);
                }
            }
        }

        private void SIPAccountUpdate(SIPAccount sipAccount)
        {
            //LogActivityMessage_External(MessageLevelsEnum.Info, "Attempting to update " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + ".");
            m_persistor.UpdateSIPAccount(sipAccount);
        }

        private void UpdateSIPAccountComplete(UpdateSIPAccountCompletedEventArgs e)
        {
            try
            {
                // The SIP account returned by the web service call is the account after the update operation was carried out.
                SIPAccount sipAccount = e.Result;

                if (m_editControl != null)
                {
                    m_editControl.WriteStatusMessage(MessageLevelsEnum.Info, "Update completed successfully for " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + ".");
                }
            }
            catch (Exception excp)
            {
                string excpMessage = (excp.InnerException != null) ? excp.InnerException.Message : excp.Message;
                if (m_editControl != null)
                {
                    m_editControl.WriteStatusMessage(MessageLevelsEnum.Error, excpMessage);
                }
                else
                {
                    LogActivityMessage_External(MessageLevelsEnum.Error, "There was an error performing a SIP Account update." + excpMessage);
                }
            }
        }

        private void SIPAccountsAssetViewer_Add()
        {
            m_selectedSIPAccount = null;
            m_addControl = new SIPAccountDetailsControl(DetailsControlModesEnum.Add, null, m_owner, SIPAccountAdd, null, DetailsControlClosed, GetDialPlanNames_External, GetSIPDomains);
            m_sipAccountsPanel.SetDetailsElement(m_addControl);
        }

        private void SIPAccountsDataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (m_initialLoadComplete && !m_sipAccountLoadInProgress && m_sipAccounts.Count > 0)
                {
                    DataGrid dataGrid = (DataGrid)sender;
                    if (dataGrid.CurrentColumn.Header as string != "Delete")
                    {
                        SIPAccount sipAccount = (SIPAccount)m_sipAccountsDataGrid.SelectedItem;

                        if (m_selectedSIPAccount == null || m_selectedSIPAccount != sipAccount)
                        {
                            m_selectedSIPAccount = sipAccount;
                            m_editControl = new SIPAccountDetailsControl(DetailsControlModesEnum.Edit, sipAccount, m_owner, null, SIPAccountUpdate, DetailsControlClosed, GetDialPlanNames_External, GetSIPDomains);
                            m_sipAccountsPanel.SetDetailsElement(m_editControl);
                        }
                    }
                }
            }
            catch (Exception excp)
            {
                LogActivityMessage_External(MessageLevelsEnum.Error, "Exception showing SIP account details. " + excp.Message);
                m_selectedSIPAccount = null;
            }
        }

        private void DeleteSIPAccount(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            SIPAccount sipAccount = m_sipAccountsDataGrid.SelectedItem as SIPAccount;
            MessageBoxResult confirmDelete = MessageBox.Show("Press Ok to delete " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + ".", "Confirm Delete", MessageBoxButton.OKCancel);
            if (confirmDelete == MessageBoxResult.OK)
            {
                LogActivityMessage_External(MessageLevelsEnum.Info, "Sending delete request for SIP Account " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + ".");
                m_persistor.DeleteSIPAccount(sipAccount);
            }
        }

        private void DeleteSIPAccountComplete(DeleteSIPAccountCompletedEventArgs e)
        {
            try
            {
                SIPAccount sipAccount = e.Result;

                LogActivityMessage_External(MessageLevelsEnum.Info, "Delete completed successfully for " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + ".");

                for (int index = 0; index < m_sipAccounts.Count; index++)
                {
                    if (m_sipAccounts[index].Id == sipAccount.Id)
                    {
                        m_sipAccounts.RemoveAt(index);
                        break;
                    }
                }

                m_sipAccountsPanel.AssetDeleted();
            }
            catch (Exception excp)
            {
                string excpMessage = (excp.InnerException != null) ? excp.InnerException.Message : excp.Message;
                LogActivityMessage_External(MessageLevelsEnum.Error, "There was an error deleting the SIP Account. " + excpMessage);
            }
        }

        private void ShowPassword(Object sender, MouseEventArgs e)
        {
            StackPanel panel = (StackPanel)sender;
            panel.Children[0].Visibility = Visibility.Collapsed;
            panel.Children[1].Visibility = Visibility.Visible;
        }

        private void HidePassword(Object sender, MouseEventArgs e)
        {
            StackPanel panel = (StackPanel)sender;
            panel.Children[0].Visibility = Visibility.Visible;
            panel.Children[1].Visibility = Visibility.Collapsed;
        }

        private List<string> GetSIPDomains(string owner)
        {
            if (m_ownerDomains.ContainsKey(owner))
            {
                return m_ownerDomains[owner];
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region SIP Registrar Bindings Functions.

        private void GetSIPBindings(int offset, int count)
        {
            if (!m_sipBindingsPanelRefreshInProgress)
            {
                m_sipBindingsPanelRefreshInProgress = true;

                m_sipBindingsPanelOffset = offset;
                m_sipBindingsPanelCount = count;
                m_persistor.GetRegistrarBindingsCountAsync(m_sipBindingsWhere);
            }
            else
            {
                LogActivityMessage_External(MessageLevelsEnum.Warn, "A SIP bindings refresh is already in progress.");
            }
        }

        private void GetRegistrarBindingsCountComplete(GetSIPRegistrarBindingsCountCompletedEventArgs e)
        {
            try
            {
                m_sipBindingsPanel.AssetListTotal = e.Result;
                LogActivityMessage_External(MessageLevelsEnum.Info, e.Result + " SIP Bindings are registered.");
                m_persistor.GetRegistrarBindingsAsync(m_sipBindingsWhere, m_sipBindingsPanelOffset, m_sipBindingsPanelCount);
            }
            catch (Exception excp)
            {
                string excpMessage = (excp.InnerException != null) ? excp.InnerException.Message : excp.Message;
                LogActivityMessage_External(MessageLevelsEnum.Error, "There was an error retrieving the number of SIP bindings. " + excpMessage);

                m_sipBindingsPanelRefreshInProgress = false;
            }
        }

        private void GetRegistrarBindingsComplete(GetSIPRegistrarBindingsCompletedEventArgs e)
        {
            m_sipBindingsLoaded = true;

            try
            {
                m_sipBindingsPanel.SetAssetListSource(e.Result);

                if (!m_initialLoadComplete)
                {
                    Load();
                }

                LogActivityMessage_External(MessageLevelsEnum.Info, "Bindings successfully loaded.");
            }
            catch (Exception excp)
            {
                string excpMessage = (excp.InnerException != null) ? excp.InnerException.Message : excp.Message;
                LogActivityMessage_External(MessageLevelsEnum.Error, "There was an error retrieving SIP bindings. " + excpMessage);
            }
            finally
            {
                m_sipBindingsPanelRefreshInProgress = false;
            }
        }

        #endregion

        private void DetailsControlClosed()
        {
            m_selectedSIPAccount = null;
            m_sipAccountsPanel.CloseDetailsPane();
        }
	}
}
Back to Top