private void toolStripButtonUploadConfig_Click(object sender, EventArgs e)
        {
            // Null proxy connections is not valid, however, a count of zero might be
            if ((object)m_proxyConnections == null)
            {
                MessageBox.Show("Cannot upload configuration, no proxy connections are currently defined.", Tag.ToNonNullString(Text), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (m_proxyConnections.Count == 0)
            {
                if ((object)m_serviceConnection != null && MessageBox.Show("WARNING: You are about to upload an empty configuration as the running service configuration.\r\n\r\nAre you sure you want to upload an empty configuration and clear the running service configuration?", Tag.ToNonNullString(Text), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    m_serviceConnection.SendCommand("UploadConfig", ProxyConnectionCollection.SerializeConfiguration(m_proxyConnections));
                }
            }
            else if (SaveConfiguration(true, "Current configuration must be saved before you upload it. Do you want to save changes to the current configuration?") == SaveState.Saved)
            {
                if ((object)m_serviceConnection != null && MessageBox.Show("Are you sure you want to upload the current local configuration and make it the running service configuration?", Tag.ToNonNullString(Text), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    m_serviceConnection.SendCommand("UploadConfig", ProxyConnectionCollection.SerializeConfiguration(m_proxyConnections));
                }
            }
            else
            {
                MessageBox.Show("Configuration upload canceled.", Tag.ToNonNullString(Text), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Loads proxy connections from a previously saved configuration file.
        /// </summary>
        /// <param name="fileName">Configuration file to load.</param>
        /// <returns>New <see cref="ProxyConnectionCollection"/> instance from specified <paramref name="fileName"/>.</returns>
        public static ProxyConnectionCollection LoadConfiguration(string fileName)
        {
            ProxyConnectionCollection proxyConnections = null;

            if ((object)fileName == null)
            {
                fileName = string.Empty;
            }

            if (File.Exists(fileName))
            {
                using (FileStream settingsFile = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    proxyConnections = DeserializeConfiguration(settingsFile);
                }
            }

            // Create an empty proxy connection list if none exists
            if ((object)proxyConnections == null)
            {
                proxyConnections = new ProxyConnectionCollection();
            }

            return(proxyConnections);
        }
        private SaveState SaveConfiguration(bool verify = false, string verificationMessage = null)
        {
            if (m_configurationSaved)
            {
                return(SaveState.Saved);
            }

            if (string.IsNullOrWhiteSpace(verificationMessage))
            {
                verificationMessage = "Do you want to save the local configuration?";
            }

            // Make sure configuration needs to be saved - user can choose to skip the save (i.e., discard changes)
            if (verify && MessageBox.Show(string.Format(verificationMessage), Tag.ToNonNullString(Text), MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
            {
                return(SaveState.Skipped);
            }

            // Make sure a file name is defined for the configuration
            if (string.IsNullOrEmpty(m_configurationFileName))
            {
                saveFileDialog.FileName = null;

                if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    m_configurationFileName = saveFileDialog.FileName;
                }
                else
                {
                    return(SaveState.Canceled);
                }
            }

            try
            {
                Cursor = Cursors.WaitCursor;

                lock (m_proxyConnections)
                {
                    ProxyConnectionCollection.SaveConfiguration(m_proxyConnections, m_configurationFileName);
                }

                ConfigurationSaved = true;

                // Change form title to include working file name
                UpdateFormTitle();

                return(SaveState.Saved);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to save configuration file: " + ex.Message, Tag.ToNonNullString(Text), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                Cursor = Cursors.Default;
            }

            return(SaveState.Canceled);
        }
Beispiel #4
0
 /// <summary>
 /// Saves proxy connections to a configuration file.
 /// </summary>
 /// <param name="proxyConnections">Existing <see cref="ProxyConnectionCollection"/> instance to save.</param>
 /// <param name="fileName">Configuration file name to use when saving.</param>
 public static void SaveConfiguration(ProxyConnectionCollection proxyConnections, string fileName)
 {
     using (FileStream settingsFile = File.Create(fileName))
     {
         SerializeConfiguration(proxyConnections, settingsFile);
     }
 }
Beispiel #5
0
 /// <summary>
 /// Serializes proxy connections to a byte-array.
 /// </summary>
 /// <param name="proxyConnections">Existing <see cref="ProxyConnectionCollection"/> instance to save.</param>
 /// <returns>Buffer of serializes proxy connections.</returns>
 public static byte[] SerializeConfiguration(ProxyConnectionCollection proxyConnections)
 {
     using (MemoryStream destinationStream = new MemoryStream())
     {
         SerializeConfiguration(proxyConnections, destinationStream);
         return(destinationStream.ToArray());
     }
 }
Beispiel #6
0
        // Static Methods

        /// <summary>
        /// Serializes proxy connections to a stream.
        /// </summary>
        /// <param name="proxyConnections">Existing <see cref="ProxyConnectionCollection"/> instance to save.</param>
        /// <param name="destinationStream">Destination stream.</param>
        public static void SerializeConfiguration(ProxyConnectionCollection proxyConnections, Stream destinationStream)
        {
            SoapFormatter formatter = new SoapFormatter
            {
                AssemblyFormat = FormatterAssemblyStyle.Simple,
                TypeFormat     = FormatterTypeStyle.TypesWhenNeeded
            };

            formatter.Serialize(destinationStream, proxyConnections);
        }
        // Create a new configuration
        private void NewConfiguration()
        {
            // Make sure to save any existing configuration before creating another
            if (SaveConfiguration(true) == SaveState.Canceled)
            {
                return;
            }

            m_configurationFileName = null;
            ProxyConnections        = ProxyConnectionCollection.LoadConfiguration(null);
            ConfigurationSaved      = true;

            // Change form title to include working file name
            UpdateFormTitle();

            ShowToolTipHelpForEmptyConfiguration();
        }
        private void m_serviceConnection_ServiceResponse(object sender, EventArgs <ServiceResponse, string, bool> e)
        {
            if ((object)e == null)
            {
                return;
            }

            // Handle service responses
            ServiceResponse response        = e.Argument1;
            string          sourceCommand   = e.Argument2.ToNonNullString().Trim();
            bool            responseSuccess = e.Argument3;

            if ((object)response == null || string.IsNullOrWhiteSpace(sourceCommand))
            {
                return;
            }

            // If command has attachments, they will be first followed by an attachment with the original command arguments
            List <object> attachments      = response.Attachments;
            bool          attachmentsExist = ((object)attachments != null && attachments.Count > 1);

            // Handle command responses
            if (string.Compare(sourceCommand, "GetStreamProxyStatus", true) == 0)
            {
                if (responseSuccess && attachmentsExist)
                {
                    ThreadPool.QueueUserWorkItem(ApplyStreamProxyStatusUpdates, attachments[0] as StreamProxyStatus[]);
                }
            }
            else if (string.Compare(sourceCommand, "DownloadConfig", true) == 0)
            {
                if (responseSuccess && attachmentsExist)
                {
                    ThreadPool.QueueUserWorkItem(ApplyDownloadedProxyConnections, ProxyConnectionCollection.DeserializeConfiguration(attachments[0] as byte[]));
                }
            }
        }
        // Load existing configuration
        private void LoadConfiguration(string configurationFileName = null)
        {
            // Make to save any existing configuration before loading another
            if (SaveConfiguration(true) == SaveState.Canceled)
            {
                return;
            }

            // Make sure a file name is defined for the configuration
            if (string.IsNullOrEmpty(configurationFileName))
            {
                openFileDialog.FileName = null;

                if (openFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    m_configurationFileName = openFileDialog.FileName;
                }
                else
                {
                    return;
                }
            }
            else
            {
                m_configurationFileName = configurationFileName;
            }

            try
            {
                Cursor           = Cursors.WaitCursor;
                ProxyConnections = ProxyConnectionCollection.LoadConfiguration(m_configurationFileName);

                // Establish an editing user control for each proxy connection
                foreach (ProxyConnection connection in m_proxyConnections)
                {
                    if (!m_loaded)
                    {
                        SplashScreen.SetStatus("Loading " + connection.Name + "...", true);
                    }

                    // Editing-control creation happens after proxy connection exists and is in the binding list,
                    // so we depend on the PropertyChanged event to attach to needed control events
                    AddProxyConnectionEditorControl(connection);
                }

                ConfigurationSaved = true;

                if (m_loaded)
                {
                    ThreadPool.QueueUserWorkItem(PostProxyConnectionsLoad);
                }

                // Change form title to include working file name
                UpdateFormTitle();

                ShowToolTipHelpForEmptyConfiguration();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load configuration file: " + ex.Message, Tag.ToNonNullString(Text), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }