private void btnSettingsClick(Office.CommandBarButton ctrl, ref bool cancel)
 {
     Settings settings = new Settings();
     settings.Show();
 }
        private void SyncContacts()
        {
            if (String.IsNullOrEmpty(Properties.Settings.Default.APIKey))
            {
                Settings settings = new Settings();
                settings.Show();
                return;
            }
            Banckle.Auth auth = new Banckle.Auth();
            auth.APIKey = Properties.Settings.Default.APIKey;
            if (!auth.authenticate(Properties.Settings.Default.Id, Properties.Settings.Default.Password))
            {
                Settings settings = new Settings();
                settings.Show();
                return;
            }

            Outlook.MAPIFolder folderContacts = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
            Outlook.Items searchFolder = folderContacts.Items;

            Sync sync = new Sync();

            try
            {

                //sync.tbSync.Text += "Starting the sync\n";

                //Outlook.AddressLists folders = this.Application.ActiveExplorer().Session.AddressLists;

                foreach (Outlook.ContactItem foundContact in searchFolder)
                {
                    sync.tbSync.Text += "Copying " + foundContact.FirstName + " " + foundContact.LastName + "\n";
                    //System.Windows.Forms.MessageBox.Show(contacts.CreatePerson(OutlookToBanckleContact(foundContact)),"Contact Status");
                    contacts.CreatePerson(OutlookToBanckleContact(foundContact));
                    //System.Windows.Forms.MessageBox.Show("Adding the person: " + contacts.CreatePerson(OutlookToBanckleContact(foundContact)) , "Contact Status");
                    //OutlookToBanckleContact(foundContact);
                }
            }catch(Exception ex){
                //sync.tbSync.Text += "Error 1 :" + ex.Message + "\n" + ex.StackTrace + "\n";
            }
            try{
                //contacts = new Banckle.Contacts();
                List<ContactDetails> contactDetails = contacts.GetContacts();
                //bool foundEmail = false;
                foreach (ContactDetails foundBCRMContact in contactDetails)
                {
                    //sync.tbSync.Text += "Copying " + foundBCRMContact.name + "\n";

                    if (IsContactExist(foundBCRMContact.name))
                    {
                        continue;
                    }

                    //for (int i = 0; i < foundBCRMContact.emails.Length; i++)
                    //{
                    //   if (String.IsNullOrEmpty(foundBCRMContact.emails[i].address))
                    //      continue;
                    //   if (IsEmailExist(foundBCRMContact.emails[i].address))
                    //   {
                    //      foundEmail = true;
                    //      //break;
                    //   }
                    //}
                    //if (foundEmail) {
                    //   continue;
                    //}
                    AddOutlookContact(foundBCRMContact);
                }
            }
            catch (Exception ex)
            {
                sync.tbSync.Text += "Error 2 :" + ex.Message + "\n" + ex.StackTrace + "\n";
            }
            sync.Show();
            //sync.Dispose();
            //sync.Close();
        }
        private void SyncContacts()
        {
            Banckle.Auth auth = new Banckle.Auth();
            auth.APIKey = Properties.Settings.Default.APIKey;
            if (!auth.authenticate())
            {
                Settings settings = new Settings();
                settings.Show();
            }

            Outlook.MAPIFolder folderContacts = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
            Outlook.Items searchFolder = folderContacts.Items;

            Sync sync = new Sync();

            try
            {

                sync.Show();
                sync.lblSync.Text = "Starting the sync";

                //Outlook.AddressLists folders = this.Application.ActiveExplorer().Session.AddressLists;

                foreach (Outlook.ContactItem foundContact in searchFolder)
                {
                    sync.lblSync.Text = "Copying " + foundContact.FirstName + " " + foundContact.LastName;
                    //System.Windows.Forms.MessageBox.Show(contacts.CreatePerson(OutlookToBanckleContact(foundContact)),"Contact Status");
                    contacts.CreatePerson(OutlookToBanckleContact(foundContact));
                    //System.Windows.Forms.MessageBox.Show("Adding the person: " + contacts.CreatePerson(OutlookToBanckleContact(foundContact)) , "Contact Status");
                    //OutlookToBanckleContact(foundContact);
                }
                List<ContactDetails> contactDetails = contacts.GetContacts();
                bool foundEmail = false;
                foreach (ContactDetails foundBCRMContact in contactDetails)
                {
                    sync.lblSync.Text = "Copying " + foundBCRMContact.name;

                    for (int i = 0; i < foundBCRMContact.emails.Length;i++ )
                    {
                        if (IsEmailExist(foundBCRMContact.emails[i].address))
                        {
                            //System.Windows.Forms.MessageBox.Show("Email Found:"+foundBCRMContact.emails[i].address);
                            foundEmail = true;
                        }
                    }
                    if (foundEmail) { continue; }
                    AddOutlookContact(foundBCRMContact);
                }
            }catch(Exception ex){
                sync.lblSync.Text = "Error:" + ex.StackTrace;
            }
            sync.Dispose();
            sync.Close();
        }