Ejemplo n.º 1
0
        /// <summary>
        /// Method called when the import button clicked. Prompts
        /// the user if they want to import the contacts from a
        /// remote location or local one and then calls the
        /// relevant methods to complete the request.
        /// </summary>
        /// <param name="obj">object</param>
        /// <returns>void</returns>
        private void ExecuteImportCommand(object obj)
        {
            if (ServiceManager.Instance.LinphoneService.VCardSupported && !ImportInProgress)
            {
                ImportInProgress = true;
                string           msg        = "To download your contacts from the Provider, please select Yes. Select No to import from local file system.";
                string           caption    = "Import Contacts";
                MessageBoxButton button     = MessageBoxButton.YesNoCancel;
                MessageBoxResult result     = MessageBox.Show(msg, caption, button, MessageBoxImage.Question);
                Dispatcher       Dispatcher = Dispatcher.CurrentDispatcher;

                switch (result)
                {
                case MessageBoxResult.Yes:
                    string uri = App.CurrentAccount.ContactsURI;
                    if (uri == string.Empty)
                    {
                        msg     = "Valid URI required to import contacts. Please go to the Settings, Account menu to input a valid URI.";
                        caption = "Import Contacts";
                        button  = MessageBoxButton.OK;
                        MessageBox.Show(msg, caption, button, MessageBoxImage.Error);
                        ImportInProgress = false;
                        break;
                    }

                    importTask = Task.Run(async() =>
                    {
                        try
                        {
                            await Dispatcher.BeginInvoke((Action)(() => Mouse.OverrideCursor = Cursors.AppStarting));
                            XmlDocument xDoc    = new XmlDocument();
                            xDoc                = await JsonWebRequest.MakeXmlWebRequestAsync <XmlDocument>(uri);
                            var recordsImported = ServiceManager.Instance.ContactService.ImportVcardFromXdoc(xDoc);
                        }
                        catch (Exception ex)
                        {
                            try
                            {
                                System.Net.HttpWebResponse response = ((ex as System.Net.WebException).Response) as System.Net.HttpWebResponse;
                                if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                                {
                                    await Dispatcher.BeginInvoke((Action)(() => Mouse.OverrideCursor = Cursors.AppStarting));
                                    VATRPCredential creds = App.CurrentAccount.configuration.FindCredential("contacts", null);
                                    if (creds == null)
                                    {
                                        await Dispatcher.BeginInvoke((Action) delegate()
                                        {
                                            creds = GetAuthentication();
                                        });
                                    }
                                    XmlDocument xDoc    = new XmlDocument();
                                    xDoc                = await JsonWebRequest.MakeXmlWebRequestAuthenticatedAsync <XmlDocument>(uri, creds);
                                    var recordsImported = ServiceManager.Instance.ContactService.ImportVcardFromXdoc(xDoc);
                                }
                                else
                                {
                                    throw;
                                }
                            }
                            catch
                            {
                                await Dispatcher.BeginInvoke((Action) delegate()
                                {
                                    msg     = ex.Message;
                                    caption = "Download failed";
                                    button  = MessageBoxButton.OK;
                                    MessageBox.Show(msg, caption, button, MessageBoxImage.Error);
                                });
                            }
                        }
                        finally
                        {
                            await Dispatcher.BeginInvoke((Action)(() => Mouse.OverrideCursor = null));
                            ImportInProgress = false;
                        }
                    });
                    break;

                case MessageBoxResult.No:
                    importTask = Task.Run((Action) delegate()
                    {
                        try
                        {
                            Dispatcher.BeginInvoke((Action)(() => Mouse.OverrideCursor = Cursors.AppStarting));
                            ExecuteLocalImport();
                        }
                        catch (Exception ex)
                        {
                            Dispatcher.BeginInvoke((Action) delegate()
                            {
                                msg     = "Contacts failed to load.";
                                caption = "Import Error";
                                button  = MessageBoxButton.OK;
                                MessageBox.Show(msg, caption, button, MessageBoxImage.Error);
                            });
                        }
                        finally
                        {
                            Dispatcher.BeginInvoke((Action)(() => Mouse.OverrideCursor = null));
                            ImportInProgress = false;
                        }
                    });
                    break;

                case MessageBoxResult.Cancel:
                    ImportInProgress = false;
                    break;
                }
            }
        }