public EditPhoneNumbers(Contact c)
 {
     InitializeComponent();
     this.contact = c;
     phonesBindingBindingSource.DataSource = c.PhoneNumbers;
     phonesBindingBindingSource.AllowNew = true;
 }
 public PhoneNumber(string phoneNumber, Contact contact)
 {
     this.Number = phoneNumber;
     this.Contact = contact;
 }
        public void SyncGoogleContacts() {

            using (ISession session = SessionFactory.OpenSession()) {

                if (bool.Parse(GetSetting(session, "google_sync", "false"))) {

                    using (ITransaction trx = session.BeginTransaction()) {

                        try {

                            Google.Contacts.ContactsRequest cr = new Google.Contacts.ContactsRequest(
                                new RequestSettings("AsteriskCid", new GDataCredentials(
                                                                    GetSetting(session, "google_login", ""),
                                                                    GetSetting(session, "google_password", ""))));

                            Feed<Google.Contacts.Contact> f = cr.GetContacts();
                            f.AutoPaging = true;
                            foreach (Google.Contacts.Contact entry in f.Entries) {

                                if (entry.Name != null && entry.Name.FullName != null) {

                                    Contact acidContact = new Contact(entry.Name.FullName);

                                    if (entry.PhotoEtag != null) {
                                        acidContact.SetPhoto(cr.Service.Query(entry.PhotoUri));
                                    }

                                    foreach (Google.GData.Extensions.PhoneNumber number in entry.Phonenumbers) {

                                        if (session.Get<PhoneNumber>(number.Value) != null) {
                                            log.WarnFormat("Phone number {0} already exist, skipping this number", number.Value);
                                            continue;
                                        }

                                        PhoneNumber phoneNumber = new PhoneNumber(number.Value, acidContact);
                                        acidContact.PhoneNumbers.Add(phoneNumber);
                                        session.Save(phoneNumber);
                                    }

                                    if (acidContact.PhoneNumbers.Count > 0)
                                        session.Save(acidContact);
                                }
                            }

                            trx.Commit();

                        } catch (Exception e) {
                            log.Error("Failed to synchronize google contacts", e);
                            trx.Rollback();
                            throw;
                        }
                    }
                }
            }
        }