/// <summary>
        /// Create an ContactCollectionChangedEventArgs object.
        /// </summary>
        /// <param name="change">The type of change being signaled by the event (must be Added or Removed).</param>
        /// <param name="contactId">The id of the contact being added or removed.</param>
        /// <exception cref="System.ArgumentException">
        /// An invalid ContactCollectionChangeType was provided.
        /// This constructor can only be used with the Added and Removed types.
        /// </exception>
        public ContactCollectionChangedEventArgs(ContactCollectionChangeType change, string contactId)
        {
            if (change != ContactCollectionChangeType.Added
                && change != ContactCollectionChangeType.Removed)
            {
                throw new ArgumentException("This constructor can only be used for the ChangeTypes 'Added' and 'Removed'.");
            }
            Verify.IsNeitherNullNorEmpty(contactId, "contactId");

            _oldId = null;
            _newId = null;
            switch (change)
            {
                case ContactCollectionChangeType.Added:
                    _newId = contactId;
                    break;
                case ContactCollectionChangeType.Removed:
                    _oldId = contactId;
                    break;
            }
            _change = change;
        }
예제 #2
0
            private void _TryGetContactInfo(string path, string key, out ContactInfo newInfo, ref ContactCollectionChangeType action)
            {
                newInfo = new ContactInfo();

                Assert.IsTrue(_dispatcher.CheckAccess());

                bool failedMissingFile = false;
                bool failedBadData = false;
                bool failedManagerIsGone = false;
                try
                {
                    // We just have a file with this extension, it's not necessarily valid.
                    // If we can't get a contact from it then don't raise a notification.
                    using (Contact contact = _loader.GetByFilePath(path))
                    {
                        newInfo.Id = contact.Id;
                        newInfo.LastWrite = File.GetLastWriteTime(path);
                    }

                    _knownContacts[key] = newInfo;
                }
                // Couldn't load the file.  Whatever...
                catch (FileNotFoundException) { failedMissingFile = true; }
                catch (DirectoryNotFoundException) { failedMissingFile = true; }
                catch (InvalidDataException) { failedBadData = true; }
                catch (ObjectDisposedException) { failedManagerIsGone = true; }
                // Let these propagate to the caller.
                catch (IOException)
                {
                    throw;
                }
                catch (UnauthorizedAccessException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    // Not expecting other exceptions here, but if we hit them then throw away the cached object.
                    Assert.Fail(e.ToString());
                    failedBadData = true;
                }

                // Only care about failures if we're still processing.
                if (_stopProcessing)
                {
                    action = ContactCollectionChangeType.NoChange;
                    return;
                }

                if (failedBadData)
                {
                    // If we couldn't load the file and this was going to be treated as an add, then just do nothing.
                    action = (ContactCollectionChangeType.Added == action)
                        ? ContactCollectionChangeType.NoChange
                        : ContactCollectionChangeType.Removed;
                    _knownContacts.Remove(key);
                }
                else if (failedMissingFile)
                {
                    // If we couldn't load the file and this was going to be treated as an add, then just do nothing.
                    switch (action)
                    {
                        case ContactCollectionChangeType.Added:
                            action = ContactCollectionChangeType.NoChange;
                            break;
                        case ContactCollectionChangeType.Updated:
                            _SetPendingFile(path);
                            _timer.Start();
                            action = ContactCollectionChangeType.NoChange;
                            break;
                        default:
                            action = ContactCollectionChangeType.Removed;
                            _knownContacts.Remove(key);
                            break;
                    }
                }
                else if (failedManagerIsGone)
                {
                    // We should stop processing these...
                    action = ContactCollectionChangeType.NoChange;
                }
            }
        /// <summary>
        /// Create an ContactCollectionChangedEventArgs object.
        /// </summary>
        /// <param name="change">The type of change being signaled by the event (must be Moved or Updated).</param>
        /// <param name="oldContactId">The original id of the affected contact before the change.</param>
        /// <param name="newContactId">The id of the contact after the change.  This may be the same as the old id.</param>
        /// <exception cref="System.ArgumentException">
        /// An invalid ContactCollectionChangeType was provided.
        /// This constructor can only be used with the Moved and Updated types.
        /// </exception>
        public ContactCollectionChangedEventArgs(ContactCollectionChangeType change, string oldContactId, string newContactId)
        {
            if (change != ContactCollectionChangeType.Moved
                && change != ContactCollectionChangeType.Updated)
            {
                throw new ArgumentException("This constructor can only be used for the ChangeType 'Moved' and 'Updated'.");
            }
            Verify.IsNeitherNullNorEmpty(newContactId, "newContactId");
            Verify.IsNeitherNullNorEmpty(oldContactId, "oldContactId");

            _oldId = oldContactId;
            _newId = newContactId;
            _change = change;
        }