예제 #1
0
        private static void Main()
        {
            try
            {
                var nonetwork = false;
                var nosplash  = false;
                var args      = Environment.GetCommandLineArgs();

                PNStatic.CultureInvariant = (CultureInfo)CultureInfo.InvariantCulture.Clone();
                PNStatic.CultureInvariant.NumberFormat.NumberDecimalSeparator = ".";

                var currentDomain = AppDomain.CurrentDomain;
                currentDomain.UnhandledException += currentDomain_UnhandledException;
                checkPreRun();

                if (args.Length > 1)
                {
                    switch (args[1])
                    {
                    case "-x":
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WPM_CLOSE_PROG);
                        return;

                    case "-xs":
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WPM_CLOSE_SILENT_SAVE);
                        return;

                    case "-xn":
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WPM_CLOSE_SILENT_WO_SAVE);
                        return;

                    case "-c":
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WPM_NEW_NOTE);
                        return;

                    case "-cr":
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WPM_NEW_NOTE_FROM_CB);
                        return;

                    case "-cd":
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WPM_NEW_DIARY);
                        return;

                    case "-cn":
                        if (args.Length > 2)
                        {
                            _newNoteString = args[2];
                        }
                        if (args.Length > 3)
                        {
                            _newNoteName = args[3];
                        }
                        if (args.Length > 4)
                        {
                            _newNoteTags = args[4];
                        }
                        _copyDataType = CopyDataType.NewNote;
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WM_COPYDATA);
                        return;

                    case "-l":
                        if (args.Length > 2)
                        {
                            _FilesToLoad.AddRange(args.Skip(2));
                        }
                        _copyDataType = CopyDataType.LoadNotes;
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WM_COPYDATA);
                        return;

                    case "-i":
                        if (args.Length > 2)
                        {
                            _idToShow = args[2];
                        }
                        _copyDataType = CopyDataType.ShowNoteById;
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WM_COPYDATA);
                        if (_mainWindowExists)
                        {
                            //return if main window exists and program is already running
                            return;
                        }
                        //continue to start the program and add note's id to be proceeded later
                        PNSingleton.Instance.NoteFromShortcut = _idToShow;
                        break;

                    case "-r":
                        PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WPM_RELOAD_NOTES);
                        return;

                    case "-b":
                        if (args.Length >= 3)
                        {
                            var backFile = Path.Combine(args[2], DateTime.Now.ToString("yyyyMMddHHmmss") + PNStrings.FULL_BACK_EXTENSION);
                            PNStatic.CreateFullBackup(backFile);
                            return;
                        }
                        if (PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WPM_BACKUP))
                        {
                            // if no previos instance of PNotes is running - create backup at default directory and exit
                            var backFile = Path.Combine(PNPaths.Instance.BackupDir, DateTime.Now.ToString("yyyyMMddHHmmss") + PNStrings.FULL_BACK_EXTENSION);
                            PNStatic.CreateFullBackup(backFile);
                        }
                        return;

                    case "-nosplash":
                        if (isPrevInstance())
                        {
                            return;
                        }
                        nosplash = true;
                        break;

                    case "-nonetwork":
                        if (isPrevInstance())
                        {
                            return;
                        }
                        nonetwork = true;
                        if (args[args.Length - 1] == "nosplash")
                        {
                            nosplash = true;
                        }
                        break;

                    case "-updater":
                        if (args.Length == 3)
                        {
                            var updPath = args[2];
                            if (!string.IsNullOrEmpty(updPath))
                            {
                                var name = Path.GetFileName(updPath);
                                if (!string.IsNullOrEmpty(name))
                                {
                                    var localPath = Path.Combine(System.Windows.Forms.Application.StartupPath, name);
                                    if (File.Exists(updPath))
                                    {
                                        File.Copy(updPath, localPath, true);
                                        var dir = Path.GetDirectoryName(updPath);
                                        if (!string.IsNullOrEmpty(dir))
                                        {
                                            Directory.Delete(dir, true);
                                        }
                                    }
                                }
                            }
                        }
                        break;

                    case "-conf":
                    case "-config":
                    case "-confnonetwork":
                    case "-confignonetwork":
                        if (isPrevInstance())
                        {
                            return;
                        }
                        if (args.Length >= 3 && args[2].Trim().Length > 0)
                        {
                            PNPaths.Instance.SettingsDir = args[2];
                        }
                        if (args.Length >= 4 && args[3].Trim().Length > 0)
                        {
                            PNPaths.Instance.DataDir = args[3];
                        }
                        if (args.Length >= 5 && args[4].Trim().Length > 0)
                        {
                            PNPaths.Instance.SkinsDir = args[4];
                        }
                        if (args.Length >= 6 && args[5].Trim().Length > 0)
                        {
                            PNPaths.Instance.BackupDir = args[5];
                        }
                        if (args.Length >= 7 && args[6].Trim().Length > 0)
                        {
                            PNPaths.Instance.LangDir = args[6];
                        }
                        if (args.Length >= 8 && args[7].Trim().Length > 0)
                        {
                            PNPaths.Instance.SoundsDir = args[7];
                        }
                        if (args.Length >= 9 && args[8].Trim().Length > 0)
                        {
                            PNPaths.Instance.FontsDir = args[8];
                        }
                        if (args.Length >= 10 && args[9].Trim().Length > 0)
                        {
                            PNPaths.Instance.DictDir = args[9];
                        }
                        if (args.Length >= 11 && args[10].Trim().Length > 0)
                        {
                            PNPaths.Instance.PluginsDir = args[10];
                        }
                        if (args.Length >= 12 && args[11].Trim().Length > 0)
                        {
                            PNPaths.Instance.ThemesDir = args[11];
                        }
                        if (args[1] == "-confnonetwork" || args[1] == "-confignonetwork")
                        {
                            nonetwork = true;
                        }
                        if (args[args.Length - 1] == "nosplash")
                        {
                            nosplash = true;
                        }
                        break;

                    default:
                        if (args[1].StartsWith("/u"))
                        {
                            string[] pars = args[1].Split('=');
                            if (pars.Length == 2)
                            {
                                string guid = pars[1];
                                string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86),
                                                           "msiexec.exe");
                                var psi = new ProcessStartInfo(path, "/x " + guid);
                                Process.Start(psi);
                                return;
                            }
                        }
                        break;
                    }
                }

                if (!isPrevInstance())
                {
                    installFonts();
                    if (!nosplash && File.Exists(Path.Combine(PNPaths.Instance.DataDir, PNStrings.NOSPLASH)))
                    {
                        nosplash = true;
                    }
                    if (!nosplash)
                    {
                        PNStatic.SplashThread = new Thread(PNStatic.ShowSplash);
                        PNStatic.SplashThread.SetApartmentState(ApartmentState.STA);
                        PNStatic.SplashThread.Start();
                    }
                    PNStatic.StartProgram(nonetwork);

                    PNStatic.FormMain = new WndMain();
                    var app = new Application();

                    var res1 = new ResourceDictionary
                    {
                        Source = new Uri(@"/styles/Resources.xaml", UriKind.Relative)
                    };
                    var res2 = new ResourceDictionary
                    {
                        Source = new Uri(@"/styles/Images.xaml", UriKind.Relative)
                    };
                    var name        = res1["ThemeName"] as string;
                    var sampleImage = res2["sample"] as BitmapImage;
                    PNStatic.Themes.Add(name,
                                        Tuple.Create(new Uri(@"/styles/Resources.xaml", UriKind.Relative),
                                                     new Uri(@"/styles/Images.xaml", UriKind.Relative), sampleImage, "", new Version()));

                    loadThemes();

                    app.Resources.MergedDictionaries.Add(res1);
                    app.Resources.MergedDictionaries.Add(res2);
                    app.Resources.MergedDictionaries.Add(new ResourceDictionary
                    {
                        Source = new Uri(@"/styles/Styles.xaml", UriKind.Relative)
                    });
                    app.Resources.MergedDictionaries.Add(new ResourceDictionary
                    {
                        Source = new Uri(@"/Themes/generic.xaml", UriKind.Relative)
                    });

                    PNStatic.ApplyTheme(PNStatic.Settings.Behavior.Theme);

                    app.Run(PNStatic.FormMain);

                    if (PNStatic.Settings != null)
                    {
                        PNStatic.Settings.Dispose();
                    }
                    if (PNSingleton.Instance.Restart)
                    {
                        System.Windows.Forms.Application.Restart();
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(PNSingleton.Instance.UpdaterCommandLine))
                        {
                            var psi = new ProcessStartInfo
                            {
                                FileName        = Path.Combine(System.Windows.Forms.Application.StartupPath, "PNUpdater.exe"),
                                Arguments       = PNSingleton.Instance.UpdaterCommandLine,
                                UseShellExecute = true
                            };
                            var prc = new Process {
                                StartInfo = psi
                            };
                            prc.Start();
                        }
                    }
                }
                else
                {
                    PNInterop.EnumWindows(enumAllWindowsProc, PNInterop.WPM_START_FROM_ANOTHER_INSTANCE);
                }
            }
            catch (Exception ex)
            {
                PNStatic.LogException(ex);
            }
        }