Example #1
0
        private static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Set an error handler (just a message box) for unexpected exceptions in threads
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            // Parse command line arguments
            CommandlineArguments arguments = new CommandlineArguments(args);

            // Is a database path set per command line?
            if (!string.IsNullOrEmpty(arguments["database"]))
            {
                DbManager.DatabasePath = arguments["database"];
            }

            // Initialise database
            try
            {
                DbManager.CreateOrUpgradeDatabase();

                WebRequest.DefaultWebProxy = DbManager.Proxy;
                if (Settings.GetValue("AuthorGuid") == null)
                {
                    Settings.SetValue("AuthorGuid", Guid.NewGuid().ToString("B"));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not create or load the database file: " + ex.Message);
                return;
            }

            // Initialisation of protocols.
            WebRequest.RegisterPrefix("sf", new ScpWebRequestCreator());
            WebRequest.RegisterPrefix("httpx", new HttpxRequestCreator());

            // Do not try using SSL3 by default since some websites make SSL3 requests fail.
            ServicePointManager.SecurityProtocol = Updater.DefaultHttpProtocols;

            // Either run silently on command line...
            if (arguments["silent"] != null)
            {
                Kernel32.ManagedAttachConsole(Kernel32.ATTACH_PARENT_PROCESS);

                ApplicationJob[] jobs = DbManager.GetJobs();

                // Filter by application name and category.
                string categoryFilter = arguments["category"];
                if (!string.IsNullOrEmpty(categoryFilter))
                {
                    jobs = jobs.Where(x => string.Equals(x.Category, categoryFilter, StringComparison.OrdinalIgnoreCase)).ToArray();
                }

                string appFilter = arguments["appname"];
                if (!string.IsNullOrEmpty(appFilter))
                {
                    jobs = jobs.Where(x => LikeOperator.LikeString(x.Name, appFilter, Microsoft.VisualBasic.CompareMethod.Text)).ToArray();
                }

                Updater updater = new Updater();
                updater.StatusChanged   += updater_StatusChanged;
                updater.ProgressChanged += updater_ProgressChanged;
                updater.BeginUpdate(jobs, false, false);

                if (arguments["notify"] != null)
                {
                    m_Icon = new NotifyIcon
                    {
                        Icon    = System.Drawing.Icon.FromHandle(Properties.Resources.Restart.GetHicon()),
                        Text    = "Ketarin is working...",
                        Visible = true
                    };
                }

                while (updater.IsBusy)
                {
                    Thread.Sleep(1000);
                }

                m_Icon?.Dispose();

                Kernel32.FreeConsole();
            }
            // ...perform database operations...
            else if (arguments["update"] != null && arguments["appguid"] != null)
            {
                // Update properties of an application in the database
                ApplicationJob job = DbManager.GetJob(new Guid(arguments["appguid"]));
                if (job == null)
                {
                    return;
                }

                if (arguments["PreviousLocation"] != null)
                {
                    job.PreviousLocation = arguments["PreviousLocation"];
                }

                job.Save();
            }
            else if (arguments["export"] != null)
            {
                ApplicationJob[] jobs        = DbManager.GetJobs();
                string           exportedXml = ApplicationJob.GetXml(jobs, false, System.Text.Encoding.UTF8);
                try
                {
                    File.WriteAllText(arguments["export"], exportedXml, System.Text.Encoding.UTF8);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not export to the specified location: " + ex.Message);
                }
            }
            else if (arguments["import"] != null)
            {
                string sourceFile = arguments["import"];
                try
                {
                    if (arguments["clear"] != null)
                    {
                        ApplicationJob.DeleteAll();
                    }

                    ApplicationJob.ImportFromXml(sourceFile);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not import from the specified location: " + ex.Message);
                }
            }
            else if (arguments["install"] != null)
            {
                try
                {
                    // Install all applications in the given XML
                    ApplicationJob[] appsToInstall;
                    string           path = arguments["install"];
                    if (Uri.IsWellFormedUriString(path, UriKind.Absolute))
                    {
                        using (WebClient client = new WebClient())
                        {
                            appsToInstall = ApplicationJob.ImportFromXmlString(client.DownloadString(path), false);
                        }
                    }
                    else
                    {
                        appsToInstall = ApplicationJob.ImportFromXml(path);
                    }

                    InstallingApplicationsDialog dialog = new InstallingApplicationsDialog
                    {
                        Applications  = appsToInstall,
                        ShowIcon      = true,
                        ShowInTaskbar = true,
                        AutoClose     = (arguments["exit"] != null)
                    };
                    Application.Run(dialog);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Setup cannot be started: " + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            // ...or launch the GUI.
            else
            {
                Application.Run(new MainForm());
            }

            string logFile = arguments["log"];

            if (!string.IsNullOrEmpty(logFile))
            {
                try
                {
                    logFile = UrlVariable.GlobalVariables.ReplaceAllInString(logFile);
                    LogDialog.SaveLogToFile(logFile);
                }
                catch (Exception)
                {
                    // ignore errors
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Set an error handler (just a message box) for unexpected exceptions in threads
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            // Parse command line arguments
            CommandlineArguments arguments = new CommandlineArguments(args);

            // Is a database path set per command line?
            if (!string.IsNullOrEmpty(arguments["database"]))
            {
                DbManager.DatabasePath = arguments["database"];
            }

            // Initialise database
            try
            {
                DbManager.CreateOrUpgradeDatabase();

                WebRequest.DefaultWebProxy = DbManager.Proxy;
                if (Settings.GetValue("AuthorGuid") == null)
                {
                    Settings.SetValue("AuthorGuid", Guid.NewGuid().ToString("B"));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not create or load the database file: " + ex.Message);
                return;
            }

            // Either run silently on command line...
            if (arguments["silent"] != null)
            {
                Kernel32.ManagedAttachConsole(Kernel32.ATTACH_PARENT_PROCESS);

                ApplicationJob[] jobs    = DbManager.GetJobs();
                Updater          updater = new Updater();
                updater.StatusChanged   += new EventHandler <Updater.JobStatusChangedEventArgs>(updater_StatusChanged);
                updater.ProgressChanged += new EventHandler <Updater.JobProgressChangedEventArgs>(updater_ProgressChanged);
                updater.BeginUpdate(jobs, false, false);

                if (arguments["notify"] != null)
                {
                    m_Icon         = new NotifyIcon();
                    m_Icon.Icon    = System.Drawing.Icon.FromHandle(Properties.Resources.Restart.GetHicon());
                    m_Icon.Text    = "Ketarin is working...";
                    m_Icon.Visible = true;
                }

                while (updater.IsBusy)
                {
                    Thread.Sleep(1000);
                }

                if (m_Icon != null)
                {
                    m_Icon.Dispose();
                }

                Kernel32.FreeConsole();
            }
            // ...perform database operations...
            else if (arguments["update"] != null && arguments["appguid"] != null)
            {
                // Update properties of an application in the database
                ApplicationJob job = DbManager.GetJob(new Guid(arguments["appguid"]));
                if (job == null)
                {
                    return;
                }

                if (arguments["PreviousLocation"] != null)
                {
                    job.PreviousLocation = arguments["PreviousLocation"];
                }

                job.Save();
            }
            else if (arguments["export"] != null)
            {
                ApplicationJob[] jobs        = DbManager.GetJobs();
                string           exportedXml = ApplicationJob.GetXml(jobs, false, System.Text.Encoding.UTF8);
                try
                {
                    File.WriteAllText(arguments["export"] as string, exportedXml, System.Text.Encoding.UTF8);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could export to the specified location: " + ex.Message);
                }
            }
            else if (arguments["install"] != null)
            {
                try
                {
                    // Install all applications in the given XML
                    ApplicationJob[] appsToInstall = null;
                    string           path          = arguments["install"] as string;
                    if (Uri.IsWellFormedUriString(path, UriKind.Absolute))
                    {
                        using (WebClient client = new WebClient())
                        {
                            appsToInstall = ApplicationJob.ImportFromXmlString(client.DownloadString(path), false);
                        }
                    }
                    else
                    {
                        appsToInstall = ApplicationJob.ImportFromXml(path);
                    }

                    InstallingApplicationsDialog dialog = new InstallingApplicationsDialog();
                    dialog.Applications  = appsToInstall;
                    dialog.ShowIcon      = true;
                    dialog.ShowInTaskbar = true;
                    dialog.AutoClose     = (arguments["exit"] != null);
                    Application.Run(dialog);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Setup cannot be started: " + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            // ...or launch the GUI.
            else
            {
                Application.Run(new MainForm());
            }

            string logFile = arguments["log"];

            if (!string.IsNullOrEmpty(logFile))
            {
                try
                {
                    logFile = UrlVariable.GlobalVariables.ReplaceAllInString(logFile);
                    LogDialog.SaveLogToFile(logFile);
                }
                catch (Exception)
                {
                    // ignore errors
                }
            }
        }