private void EditEntry()
        {
            var row = GetFirstPrintServerSelectedRow();

            if (row != null)
            {
                var server = row.DataBoundItem as FrameworkServer;

                FrameworkServerProxy proxy = new FrameworkServerProxy(server);

                // The user will now edit the print properties of this server.
                using (FrameworkServerEditForm form = new FrameworkServerEditForm(_controller, proxy))
                {
                    if (form.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                    else
                    {
                        // Copy the working copy (proxy) data back to the server object, and then
                        // save these changes to the database.
                        proxy.CopyTo(server);
                        SaveChanges();
                    }
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FrameworkServerEditForm"/> class.
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="server">The server.</param>
        public FrameworkServerEditForm(FrameworkServerController controller, FrameworkServerProxy server)
        {
            _controller = controller;
            _proxy      = server;

            InitializeComponent();

            hostname_TextBox.ReadOnly         = true;
            serverTypes_ListBox.DisplayMember = "Name";
        }
        private DialogResult EditEntry(FrameworkServerProxy proxy)
        {
            DialogResult result = DialogResult.None;

            using (FrameworkServerEditForm form = new FrameworkServerEditForm(_controller, proxy))
            {
                result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    _unsavedChanges = true;
                }
            }

            return(result);
        }
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            var sourceProxy = e.Argument as FrameworkServerProxy;

            // Use a copy of the proxy class that is not databound to UI controls.
            // If not done this way, the _proxy will fire property changed events
            // as it gets updated and will cause the UI updates to fail as this is
            // running on a background thread.
            FrameworkServerProxy proxy = new FrameworkServerProxy();

            proxy.CopyFrom(sourceProxy);
            string error  = string.Empty;
            bool   result = _controller.QueryServer(proxy, out error);

            e.Result = new object[] { result, proxy, error };
        }
        private void server_RadGridView_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            var row = GetFirstSelectedRow();

            if (row != null)
            {
                var server = row.DataBoundItem as FrameworkServer;

                FrameworkServerProxy proxy = new FrameworkServerProxy(server);

                if (EditEntry(proxy) == DialogResult.OK)
                {
                    proxy.CopyTo(server);
                }
            }
        }
        private void edit_Button_Click(object sender, EventArgs e)
        {
            var row = GetFirstSelectedRow();

            if (row != null)
            {
                var server = row.DataBoundItem as FrameworkServer;

                FrameworkServerProxy proxy = new FrameworkServerProxy(server);

                if (EditEntry(proxy) == DialogResult.OK)
                {
                    proxy.CopyTo(server);
                }
            }
        }
        /// <summary>
        /// Adds a new Framework Server with Print properties and retrieves it's print queues.
        /// </summary>
        private bool AddNewServer(string serverName, Guid serverId)
        {
            // The server doesn't exist in configuration, so try to query it and load
            // that information so it can be reviewed and edited by the user.
            Cursor = Cursors.WaitCursor;
            string error = string.Empty;
            FrameworkServerProxy proxy = new FrameworkServerProxy(serverName);
            bool success = _controller.QueryServer(proxy, out error);

            Cursor = Cursors.Default;

            // If there is a failure reading server and/or the user doesn't want to continue, then return
            if (!success && !ContinueEditing(proxy, error))
            {
                return(false);
            }

            FrameworkServer server = new FrameworkServer()
            {
                FrameworkServerId = serverId
            };

            // Add Print as a server type for this server before editing
            FrameworkServerType serverType = _controller.GetServerType(ServerType.Print);

            proxy.ServerTypes.Add(serverType);

            using (FrameworkServerEditForm form = new FrameworkServerEditForm(_controller, proxy))
            {
                if (form.ShowDialog() != DialogResult.OK)
                {
                    return(false);
                }

                proxy.CopyTo(server);
                //Save the new server to the database at this point.  If there is an error getting
                //print queues, the server data will be preserved.
                _controller.AddNewServer(server);
                _printServers.Add(server);
                SaveChanges();
            }

            // Scan the server for queues.
            AddServerQueues(server);

            return(true);
        }
        private void add_Button_Click(object sender, System.EventArgs e)
        {
            FrameworkServerProxy proxy = new FrameworkServerProxy();

            // Prompt for the hostname
            string inputText = InputDialog.Show("Server Hostname: ", "Add Server");

            if (string.IsNullOrWhiteSpace(inputText))
            {
                // User cancelled
                return;
            }

            // Determine if this hostname already exists
            if (_controller.HostNameExists(inputText))
            {
                MessageBox.Show("This hostname already exists", "Hostname Exists", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            proxy.HostName = inputText;
            string error = string.Empty;

            Cursor = Cursors.WaitCursor;
            bool foundHost = _controller.QueryServer(proxy, out error);

            Cursor = Cursors.Default;

            // If there is a failure reading server and the user doesn't want to continue, then return
            if (!foundHost && !ContinueEditing(proxy, error))
            {
                return;
            }

            if (EditEntry(proxy) == DialogResult.OK)
            {
                FrameworkServer server = new FrameworkServer()
                {
                    FrameworkServerId = SequentialGuid.NewGuid()
                };
                proxy.CopyTo(server);
                _controller.ServersList.Add(server);
            }

            server_RadGridView.Refresh();
        }
        private static bool ContinueEditing(FrameworkServerProxy proxy, string error)
        {
            var result = MessageBox.Show
                         (
                "Unable to connect to server.  {0} {1}{2}Enter server properties manually?".FormatWith(error, Environment.NewLine, Environment.NewLine),
                "Connection Error",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Error
                         );

            if (result == System.Windows.Forms.DialogResult.No)
            {
                return(false);
            }

            proxy.Architecture = WindowsSystemInfo.ArchitectureX64;
            proxy.Cores        = 1;
            proxy.Memory       = 2000;

            return(true);
        }
        private static bool ContinueEditing(FrameworkServerProxy server, string error)
        {
            DialogResult result = MessageBox.Show
                                  (
                $"Unable to connect to {server.HostName}.  {error} {Environment.NewLine}{Environment.NewLine}Enter server properties manually?",
                "Connection Error",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Error
                                  );

            if (result == DialogResult.No)
            {
                return(false);
            }

            server.Architecture = WindowsSystemInfo.ArchitectureX64;
            server.Cores        = 1;
            server.Memory       = 2000;

            return(true);
        }
        /// <summary>
        /// Updates the server properties of an existing FrameworkServer to a Print Server.
        /// Scans the server for Print Queues and adds them to the database.
        /// </summary>
        private bool UpdateServerProperties(FrameworkServer server)
        {
            // Before displaying the server properties, add the Print Property to this server entry if it's not already there.
            if (!server.ServerTypes.Any(x => x.Name.Equals(ServerType.Print.ToString(), StringComparison.OrdinalIgnoreCase)))
            {
                server.ServerTypes.Add(_controller.GetServerType(ServerType.Print));
                SaveChanges();
            }

            // Instantiate the proxy class against the server, which will be used to edit the properties
            FrameworkServerProxy proxy = new FrameworkServerProxy(server);

            using (FrameworkServerEditForm form = new FrameworkServerEditForm(_controller, proxy))
            {
                if (form.ShowDialog() != DialogResult.OK)
                {
                    return(false);
                }

                // Copy the working copy (proxy) data back to the server object
                proxy.CopyTo(server);
                //At this point we want to save in case the refreshing of the queues fails for some reason
                SaveChanges();
            }

            // If the grid isn't already showing the FrameworkServer, then add it.
            int index = GetDataBoundItemIndex(server.FrameworkServerId);

            if (index == -1)
            {
                _printServers.Add(server);
            }

            // The server was not a print server before, so we will need to refresh the print queues.
            AddServerQueues(server);

            return(true);
        }