Esempio n. 1
0
        public void Edit(string key, ClientSettings settings)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            // Edit is only called after a client setup dialog
            // has returned.  At this point the client name
            // has already been validated.  Just make sure we
            // have a value here.
            Debug.Assert(!String.IsNullOrEmpty(settings.Name));

            IClient client;
            ClientEditedEventArgs e;

            _syncLock.EnterWriteLock();
            try
            {
                bool keyChanged = key != settings.Name;
                if (keyChanged && _clientDictionary.ContainsKey(settings.Name))
                {
                    // would like to eventually use the exact same
                    // exception message that would be used by
                    // the inner dictionary object.
                    throw new ArgumentException("An element with the same key already exists.");
                }

                client = _clientDictionary[key];
                string existingName = client.Settings.Name;
                string existingPath = client.Settings.DataPath();

                client.SlotsChanged      -= OnInvalidate;
                client.RetrievalFinished -= OnInvalidate;
                // update the settings
                client.Settings = settings;
                // if the key changed the client object needs removed and readded with the correct key
                if (keyChanged)
                {
                    _clientDictionary.Remove(key);
                    _clientDictionary.Add(settings.Name, client);
                }
                client.SlotsChanged      += OnInvalidate;
                client.RetrievalFinished += OnInvalidate;

                if (settings.IsFahClient() || settings.IsLegacy())
                {
                    e = new ClientEditedEventArgs(existingName, settings.Name, existingPath, settings.DataPath());
                }
                else
                {
                    // no External support yet
                    throw new InvalidOperationException("Client type is not supported.");
                }
            }
            finally
            {
                _syncLock.ExitWriteLock();
            }

            IsDirty = true;
            OnClientEdited(e);
            OnDictionaryChanged(new ConfigurationChangedEventArgs(ConfigurationChangedType.Edit, client));
        }