Ejemplo n.º 1
0
 /// <summary>
 /// Dispose the trayicon and create a new one.
 /// </summary>
 public static void RestartTrayicon()
 {
     Program.DisposeTrayicon();
     trayicon = new TrayIcon(formmanager);
 }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            /*
             * a suggestion to "protect" against insecure Dynamic Library Loading vulnerability in windows
             * it does not fix it, it makes it harder to exploit insecure dll loading.
             * NoteFly uses APPDATA, TEMP and SystemRoot variables.
             * A subfolder in %APPDATA% is where NoteFly stores it program settings, skins settings, log etc.
             * %TEMP% is needs for logging if appdatafolder is not found.
             * %SystemRoot% is required by the LinkLabel control to work properly.
             * %SystemDrive% is required by NET framework.
             * Plugin developers should not rely on environment variables.
             */
            Program.LoadPlatformOs();
            if (Program.CurrentOS == OS.WINDOWS)
            {
                System.Collections.IDictionary environmentVariables = Environment.GetEnvironmentVariables();
                foreach (System.Collections.DictionaryEntry de in environmentVariables)
                {
                    string currentvariable = de.Key.ToString();
                    if (!currentvariable.Equals("APPDATA", StringComparison.OrdinalIgnoreCase) &&
                        !currentvariable.Equals("SystemRoot", StringComparison.OrdinalIgnoreCase) &&
                        !currentvariable.Equals("SystemDrive", StringComparison.OrdinalIgnoreCase) &&
                        !currentvariable.Equals("TEMP", StringComparison.OrdinalIgnoreCase))
                    {
                        Environment.SetEnvironmentVariable(de.Key.ToString(), null);
                    }
                }

                SetDllDirectory(string.Empty); // removes current working directory as dll search path, but requires kernel32.dll by itself to be looked up.
            }

#if DEBUG
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
#endif

            System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler(UnhanledThreadExceptionHandler);
            System.Windows.Forms.Application.SetUnhandledExceptionMode(System.Windows.Forms.UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
            System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(true);
            if (!xmlUtil.LoadSettings())
            {
                // settings.xml does not exist create default settings.xml file
                xmlUtil.WriteDefaultSettings();
                xmlUtil.LoadSettings();
            }

            Program.SetCulture(Settings.ProgramLanguage);
#if DEBUG
            stopwatch.Stop();
            Log.Write(LogType.info, "Settings load time: " + stopwatch.ElapsedMilliseconds + " ms");
#endif
            bool   visualstyle;
            bool   resetpositions;
            bool   newnote = false;
            string newnotetitle;
            string newnotecontent;
            ParserArguments(args, out visualstyle, out resetpositions, out newnote, out newnotetitle, out newnotecontent);
            if (Program.CurrentOS == OS.WINDOWS)
            {
                if (!Settings.ProgramSuspressWarnAdmin)
                {
                    // Security measure, show warning if runned with dangerous administrator rights.
                    System.Security.Principal.WindowsIdentity  identity  = System.Security.Principal.WindowsIdentity.GetCurrent();
                    System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
                    if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
                    {
                        string program_runasadministrator           = Strings.T("You are now running {0} as elevated Administrator.\nWhich is not recommended for security.\nPress OK if your understand the risks of running as administrator and want to hide this message in the future.", Program.AssemblyTitle);
                        string program_runasadministratortitle      = Strings.T("Elevated administrator");
                        System.Windows.Forms.DialogResult dlganswer = System.Windows.Forms.MessageBox.Show(program_runasadministrator, program_runasadministratortitle, System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Warning);
                        if (dlganswer == System.Windows.Forms.DialogResult.OK)
                        {
                            Settings.ProgramSuspressWarnAdmin = true;
                            if (!System.IO.Directory.Exists(Program.AppDataFolder))
                            {
                                Directory.CreateDirectory(Program.AppDataFolder);
                            }

                            xmlUtil.WriteSettings();
                        }
                    }
                }
            }

            if (visualstyle)
            {
                if (Program.CurrentOS == OS.WINDOWS)
                {
                    System.Windows.Forms.Application.EnableVisualStyles();
                }
            }

            bool pluginsupdated = false;
            if (Settings.ProgramPluginsAllEnabled)
            {
                pluginsupdated = PluginsManager.UpdatePluginReplaceFiles();
            }

            if (!pluginsupdated)
            {
                if (Program.CheckInstancesRunning() > 1)
                {
                    string program_alreadyrunning            = Strings.T("{0} is already running.\nLoad another instance? (not recommended)", Program.AssemblyTitle);
                    string program_alreadyrunningtitle       = Strings.T("already running");
                    System.Windows.Forms.DialogResult dlgres = System.Windows.Forms.MessageBox.Show(program_alreadyrunning, program_alreadyrunningtitle, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Exclamation);
                    if (dlgres == System.Windows.Forms.DialogResult.No)
                    {
                        // shutdown by don't continue this Main method
                        return;
                    }
                }
            }

            SyntaxHighlight.InitHighlighter();
            Program.notes = new Notes(resetpositions);
            if (Settings.ProgramPluginsAllEnabled)
            {
                PluginsManager.LoadPlugins();
            }

            Program.notes.ShowNotesVisible();
            formmanager = new FormManager(notes);
            trayicon    = new TrayIcon(formmanager);

            if (!Settings.ProgramFirstrunned)
            {
                // disable the firstrun the next time.
                Settings.ProgramFirstrunned = true;
                Settings.UpdatecheckUseGPG  = false;
                GPGVerifyWrapper gpgverif = new GPGVerifyWrapper();
                if (!string.IsNullOrEmpty(gpgverif.GetGPGPath()) && gpgverif != null)
                {
                    Settings.UpdatecheckGPGPath = gpgverif.GetGPGPath();
                    Settings.UpdatecheckUseGPG  = true;
                }

                gpgverif = null;
                Log.Write(LogType.info, "firstrun occur");
                xmlUtil.WriteSettings();
            }

            if (!Settings.ProgramLastrunVersion.Equals(Program.AssemblyVersionAsString, StringComparison.Ordinal))
            {
                Settings.ProgramLastrunVersion = Program.AssemblyVersionAsString;
                xmlUtil.WriteSettings();
                Log.Write(LogType.info, "Updated ProgramLastrunVersion setting.");

                if (PluginsManager.EnabledPlugins != null)
                {
                    for (int p = 0; p < PluginsManager.EnabledPlugins.Count; p++)
                    {
                        PluginsManager.EnabledPlugins[p].ProgramUpgraded();
                    }
                }
            }

            if (Settings.UpdatecheckEverydays > 0)
            {
                DateTime lastupdate = DateTime.Parse(Settings.UpdatecheckLastDate);
                if (lastupdate.AddDays(Settings.UpdatecheckEverydays) <= DateTime.Now)
                {
                    Settings.UpdatecheckLastDate = UpdateGetLatestVersion();
                    xmlUtil.WriteSettings();
                }
            }

            if (newnote)
            {
                formmanager.OpenNewNote(newnotetitle, newnotecontent);
            }
            else
            {
                newnotetitle   = null;
                newnotecontent = null;
            }

            SyntaxHighlight.DeinitHighlighter();
            System.Windows.Forms.Application.Run();
        }