Ejemplo n.º 1
0
        internal void InsertOrUpdateTag(TagLocal tag, bool repairDeleted)
        {
            if (tag == null)
            {
                return;
            }

            if (tag.Id <= 0)
            {
                var deletedTagId = GetRemovedTagId(tag.AddressBook.Id, tag.Key);
                if (deletedTagId > 0)
                {
                    tag.Id        = deletedTagId;
                    tag.IsDeleted = !repairDeleted;
                }
            }

            using (new EnsuredResourceCriticalOperation(_sqlConnection))
                using (var command = _sqlConnection.CreateCommand())
                {
                    if (tag.Id > 0)
                    {
                        command.CommandText = "update tags set [name] = @name, color = @color, is_deleted = @is_deleted where id = @id";

                        command.Parameters.Add(new SQLiteParameter("@id", tag.Id));
                        command.Parameters.Add(new SQLiteParameter("@is_deleted", tag.IsDeleted ? 1 : 0));
                    }
                    else
                    {
                        tag.IncrementVersion();

                        command.CommandText = "insert into tags([name], color, addressbook_id, [key], version_tag) values (@name, @color, @addressbook_id, @key, @version_tag)";

                        command.Parameters.Add(new SQLiteParameter("@addressbook_id", tag.AddressBook.Id));
                        command.Parameters.Add(new SQLiteParameter("@key", tag.Key));
                        command.Parameters.Add(new SQLiteParameter("@version_tag", tag.VersionKey));
                    }

                    command.Parameters.Add(new SQLiteParameter("@name", tag.Name));
                    command.Parameters.Add(new SQLiteParameter("@color", tag.Color));

                    command.ExecuteNonQuery();

                    if (tag.Id <= 0)
                    {
                        tag.Id = GetLastInsertRowId();

                        if (!tag.IsDeleted)
                        {
                            _tags.Add(tag.Id, tag);
                        }
                    }
                }
        }
Ejemplo n.º 2
0
        private bool ProcessTags()
        {
            CurrentStateString = "Downloading tags...";
            Logger.LogNotice(String.Format("Downloading tags entities for '{0}'.", _addressBook.Name));
            var tags = _addressBook.AddressBook.GetTags().ToArray();

            Logger.LogNotice(String.Format("Download tags for '{0}' finished. Total downloaded: {1}", _addressBook.Name, tags.Length));

            CurrentStateString = "Processing tags...";

            var updatedCount = 0;
            var skippedCount = 0;
            var createdCount = 0;
            var count        = tags.Length;

            for (int i = 0; i < count; i++)
            {
                var item = tags[i];
                CurrentStateString = string.Format("Processing tag {0}/{1}", i, count);

                var tagLocal = _contactsManager.TagsDictionary.Values.FirstOrDefault(x => x.Key == item.Key && x.AddressBook.Id == _addressBook.Id);
                var newTag   = tagLocal == null;
                if (tagLocal == null)
                {
                    tagLocal = new TagLocal(_addressBook, _contactsManager);
                    createdCount++;
                }
                else if (tagLocal.VersionGenerator.CompareVersions(tagLocal, item) != VersionsCompareResult.Lower)
                {
                    skippedCount++;
                    continue;
                }

                if (tagLocal.Id > 0)
                {
                    updatedCount++;
                }

                tagLocal.UpdateFrom(item);
                _contactsManager.InsertOrUpdateTag(tagLocal, false);

                if (newTag && tagLocal.IsDeleted)
                {
                    createdCount--;
                }

                tagLocal.RaiseChanged();
            }

            Logger.LogNotice(string.Format("Finished tags update for '{0}'. Created: {1}, updated: {2}, skipped: {3} items.", _addressBook.Name, createdCount, updatedCount, skippedCount));
            return(updatedCount > 0 || createdCount > 0);
        }
Ejemplo n.º 3
0
        internal void RemoveTag(TagLocal tag)
        {
            if (tag.Id <= 0)
            {
                return;
            }

            using (new EnsuredResourceCriticalOperation(_sqlConnection))
                using (var command = _sqlConnection.CreateCommand())
                {
                    command.CommandText = @"
delete from tags_links where tag_id = @id;
update tags set is_deleted = 1 where id = @id";
                    command.Parameters.Add(new SQLiteParameter("@id", tag.Id));

                    command.ExecuteNonQuery();
                }

            if (_contactsTags.ContainsKey(tag.Id))
            {
                foreach (var contact in _contactsTags[tag.Id])
                {
                    foreach (var contactInfo in contact.ContactInfos)
                    {
                        var target = contactInfo.Tags.FirstOrDefault(x => x.Id == tag.Id);

                        if (target != null)
                        {
                            contactInfo.Tags.Remove(target);
                            contactInfo.Submit();
                        }
                    }
                }

                _contactsTags.Remove(tag.Id);
            }
        }
Ejemplo n.º 4
0
        public ContactsManager(ICore core, ISynchronizeInvoke syncInvoke)
        {
            Core = core;

            _contactsWrapper = new ObservableDictionaryValuesMapperConverter <Contact, IContact>(_contacts);
            _tagsWrapper     = new ObservableDictionaryValuesMapperConverter <TagLocal, IContactTagLocal>(_tags);

            var dbPath = string.Format("{0}{1}contactpoint{1}{2}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Path.DirectorySeparatorChar, "address_book.s3db");

#if CONTACTS_DEBUG
            if (File.Exists(dbPath))
            {
                File.Delete(dbPath);
            }
#endif

            if (!File.Exists(dbPath))
            {
                CreateDatabase(dbPath);
            }
            else
            {
                if (!InitializeConnection(dbPath))
                {
                    Logger.LogWarn("Error loading ContactsManager - continue without Contacts API");
                    return;
                }
            }

            var contacts = new Dictionary <long, Contact>();
            using (new EnsuredResourceCriticalOperation(_sqlConnection))
            {
                DatabaseSchema.Upgrade(_sqlConnection);

                #region Load address books

                Logger.LogNotice("Loading Address Books");
                using (var command = _sqlConnection.CreateCommand())
                {
                    command.CommandText = @"select id, name, lastupdate, key from addressbooks";

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            _addressBooks.Add(
                                new AddressBookLocal(
                                    this,
                                    reader.GetInt32(0),
                                    Guid.Parse(reader.GetString(3)),
                                    reader.GetString(1)
                                    )
                            {
                                LastUpdate =
                                    reader.IsDBNull(2)
                                                ? DateTime.Now - TimeSpan.FromDays(365)
                                                : reader.GetDateTime(2)
                            });
                        }
                    }
                }

                #endregion

                #region Load tags

                Logger.LogNotice("Loading Tags");
                using (var command = _sqlConnection.CreateCommand())
                {
                    command.CommandText =
                        @"select id, name, key, color, version_tag, addressbook_id from tags where is_deleted = 0";

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var addressBookId = reader.GetInt64(5);
                            var addressBook   = AddressBooks.FirstOrDefault(x => x.Id == addressBookId);
                            if (addressBook == null)
                            {
                                continue;
                            }

                            var tag = new TagLocal(reader.GetInt64(0), addressBook, this)
                            {
                                Name       = reader.GetString(1),
                                Key        = reader.GetStringSafe(2),
                                Color      = reader.GetStringSafe(3),
                                VersionKey = reader.GetStringSafe(4),
                                IsDeleted  = false
                            };

                            tag.ResetIsChanged();

                            _tags.Add(tag.Id, tag);
                        }
                    }
                }

                #endregion

                #region Load contacts

                Logger.LogNotice("Loading Contacts");
                using (var command = _sqlConnection.CreateCommand())
                {
                    command.CommandText = @"select id, first_name, last_name, middle_name, company from contacts";

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var id      = reader.GetInt32(0);
                            var contact = new Contact(this, id)
                            {
                                FirstName  = reader.GetStringSafe(1),
                                LastName   = reader.GetStringSafe(2),
                                MiddleName = reader.GetStringSafe(3),
                                Company    = reader.GetStringSafe(4)
                            };

                            contacts.Add(id, contact);
                        }
                    }
                }

                #endregion

                #region Load contact infos links for contacts

                Logger.LogNotice("Loading Contact Links");
                var contactInfoIdsContacts = new Dictionary <long, Contact>();
                using (var command = _sqlConnection.CreateCommand())
                {
                    command.CommandText = @"select contact_info_id, contact_id from contacts_links";

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var contactInfoId = reader.GetInt64(0);
                            if (contactInfoIdsContacts.ContainsKey(contactInfoId))
                            {
                                continue;
                            }

                            var contactId = reader.GetInt64(1);

                            if (!contacts.ContainsKey(contactId))
                            {
                                continue;
                            }

                            contactInfoIdsContacts.Add(contactInfoId, contacts[contactId]);
                        }
                    }
                }

                #endregion

                #region Load contact infos

                Logger.LogNotice("Loading Contact details");
                var contactInfos = new Dictionary <long, ContactInfoLocal>();
                using (var command = _sqlConnection.CreateCommand())
                {
                    command.CommandText =
                        @"select id, first_name, last_name, middle_name, company, job_title, addressbook_id, key, note, version_tag from contact_infos where is_deleted = 0";

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var id            = reader.GetInt64(0);
                            var addressBookId = reader.GetInt64(6);
                            var addressBook   = _addressBooks.FirstOrDefault(x => x.Id == addressBookId);

                            if (addressBook == null)
                            {
                                continue;
                            }
                            if (!contactInfoIdsContacts.ContainsKey(id))
                            {
                                continue;
                            }

                            var contactInfo = new ContactInfoLocal(id, addressBook, this)
                            {
                                FirstName  = reader.GetStringSafe(1),
                                LastName   = reader.GetStringSafe(2),
                                MiddleName = reader.GetStringSafe(3),
                                Company    = reader.GetStringSafe(4),
                                JobTitle   = reader.GetStringSafe(5),
                                Key        = reader.GetString(7),
                                Note       = reader.GetStringSafe(8),
                                VersionKey = reader.GetStringSafe(9),
                                Contact    = contactInfoIdsContacts[id],
                                IsDeleted  = false
                            };

                            contactInfos.Add(id, contactInfo);
                            contactInfo.ResetIsChanged();

                            contactInfoIdsContacts[id].ContactInfos.Add(contactInfo);
                        }
                    }
                }

                #endregion

                #region Load phone numbers

                Logger.LogNotice("Loading Phone Numbers");
                using (var command = _sqlConnection.CreateCommand())
                {
                    command.CommandText =
                        @"select id, number, comment, key, version_tag, contact_info_id from contact_info_phones";

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var contactInfoId = reader.GetInt64(5);
                            if (!contactInfos.ContainsKey(contactInfoId))
                            {
                                continue;
                            }

                            var number = new ContactPhoneLocal(reader.GetInt64(0),
                                                               contactInfos[contactInfoId].AddressBook)
                            {
                                Number     = reader.GetString(1),
                                Comment    = reader.GetStringSafe(2),
                                Key        = reader.GetStringSafe(3),
                                VersionKey = reader.GetStringSafe(4)
                            };

                            number.ResetIsChanged();

                            contactInfos[contactInfoId].PhoneNumbers.Add(number);
                            contactInfos[contactInfoId].ResetIsChanged();
                        }
                    }
                }

                #endregion

                #region Load emails

                Logger.LogNotice("Loading Emails");
                using (var command = _sqlConnection.CreateCommand())
                {
                    command.CommandText =
                        @"select id, email, comment, key, version_tag, contact_info_id from contact_info_emails";

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var contactInfoId = reader.GetInt64(5);
                            if (!contactInfos.ContainsKey(contactInfoId))
                            {
                                continue;
                            }

                            var email = new ContactEmailLocal(reader.GetInt64(0),
                                                              contactInfos[contactInfoId].AddressBook)
                            {
                                Email      = reader.GetString(1),
                                Comment    = reader.GetStringSafe(2),
                                Key        = reader.GetStringSafe(3),
                                VersionKey = reader.GetStringSafe(4)
                            };

                            email.ResetIsChanged();

                            contactInfos[contactInfoId].Emails.Add(email);
                            contactInfos[contactInfoId].ResetIsChanged();
                        }
                    }
                }

                #endregion

                #region Fill tags links on contacts infos

                Logger.LogNotice("Loading Tag links");
                using (var command = _sqlConnection.CreateCommand())
                {
                    command.CommandText = @"select tag_id, contact_info_id from tags_links";

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var tagId         = reader.GetInt32(0);
                            var contactInfoId = reader.GetInt32(1);

                            if (!_tags.ContainsKey(tagId))
                            {
                                continue;
                            }
                            if (!contactInfos.ContainsKey(contactInfoId))
                            {
                                continue;
                            }

                            contactInfos[contactInfoId].Tags.Add(_tags[tagId]);
                            contactInfos[contactInfoId].ResetIsChanged();

                            if (!_contactsTags.ContainsKey(tagId))
                            {
                                _contactsTags.Add(tagId, new List <IContact>());
                            }

                            _contactsTags[tagId].Add(contactInfoIdsContacts[contactInfoId]);
                        }
                    }
                }

                #endregion
            }

            // Fill contacts into main collection
            foreach (var item in contacts)
            {
                _contacts.Add(item.Key, item.Value);
            }

            Logger.LogNotice("Starting update watcher");
            _updateWatcher = new UpdateWatcher(this, syncInvoke);
        }