예제 #1
0
 /// <summary>
 /// Looks for other culture files with the same prefix in the same directory and asks the
 /// user whether other files shall also be loaded.
 /// </summary>
 /// <param name="filesToLoad">The files to load. This method only does anything if the list
 /// contains a single .xml file. If the user accepts to load other files as well, they're
 /// added to this list.</param>
 /// <returns>false if the user cancelled the operation; otherwise, true.</returns>
 public static bool FindOtherCultures(List <string> filesToLoad)
 {
     if (filesToLoad.Count == 1 && Path.GetExtension(filesToLoad[0]).ToLowerInvariant() == ".xml")
     {
         // Scan for similar files and ask if not all of them are selected
         string        myCulture     = FileNameHelper.GetCulture(filesToLoad[0]);
         List <string> otherFiles    = new List <string>();
         List <string> otherCultures = new List <string>();
         foreach (string otherFile in Directory.GetFiles(Path.GetDirectoryName(filesToLoad[0]), FileNameHelper.GetPrefix(filesToLoad[0]) + ".*.xml"))
         {
             string otherCulture = FileNameHelper.GetCulture(otherFile);
             if (otherCulture != null && otherCulture != myCulture)
             {
                 otherFiles.Add(otherFile);
                 otherCultures.Add(otherCulture);
             }
         }
         if (otherCultures.Count > 0)
         {
             otherCultures.Sort();
             if (App.SplashScreen != null)
             {
                 App.SplashScreen.Close(TimeSpan.Zero);
             }
             var result = TaskDialog.Show(
                 owner: MainWindow.Instance,
                 title: "TxEditor",
                 mainInstruction: Tx.T("msg.load file.other cultures for prefix"),
                 content: Tx.T("msg.load file.other cultures for prefix.desc", "list", string.Join(", ", otherCultures)),
                 customButtons: new string[] { Tx.T("task dialog.button.load all"), Tx.T("task dialog.button.load one"), Tx.T("task dialog.button.cancel") },
                 allowDialogCancellation: true);
             if (result.CustomButtonResult == 0)
             {
                 // Load all, add other files
                 filesToLoad.AddRange(otherFiles);
             }
             else if (result.CustomButtonResult == 1)
             {
                 // Load one, do nothing
             }
             else
             {
                 // Cancel or unset
                 return(false);
             }
         }
     }
     return(true);
 }
예제 #2
0
        protected override void OnStartup(StartupEventArgs args)
        {
            base.OnStartup(args);

            // Fix WPF's built-in themes
            if (OSInfo.IsWindows8OrNewer)
            {
                ReAddResourceDictionary("/Resources/RealWindows8.xaml");
            }

            // Initialise and show the main window

            CommandLineHelper cmdLine = new CommandLineHelper();
            var scanOption            = cmdLine.RegisterOption("scan", 1).Alias("s");

            try
            {
                cmdLine.Parse();
            }
            catch (Exception ex)
            {
                App.ErrorMessage("Command line error.", ex, "Parsing command line");
                Application.Current.Shutdown();
            }

            List <string> filesToLoad = new List <string>();
            bool          error       = false;

            foreach (string fileNameArg in cmdLine.FreeArguments)
            {
                if (!string.IsNullOrWhiteSpace(fileNameArg))
                {
                    // File name
                    if (File.Exists(fileNameArg))
                    {
                        // File exists, open it
                        string fileName = fileNameArg;
                        if (!Path.IsPathRooted(fileName))
                        {
                            fileName = Path.GetFullPath(fileName);
                        }
                        filesToLoad.Add(fileName);
                    }
                    else if (Directory.Exists(fileNameArg))
                    {
                        // Directory specified, collect all files
                        foreach (string fileName in Directory.GetFiles(fileNameArg, "*.txd"))
                        {
                            filesToLoad.Add(fileName);
                        }
                        if (filesToLoad.Count == 0)
                        {
                            // Nothing found, try older XML file names
                            foreach (string fileName in Directory.GetFiles(fileNameArg, "*.xml"))
                            {
                                if (FileNameHelper.GetCulture(fileName) != null)
                                {
                                    filesToLoad.Add(fileName);
                                }
                            }
                        }
                    }
                    else
                    {
                        FL.Error("File/directory not found", fileNameArg);
                        error = true;
                    }
                }
            }
            if (error)
            {
                App.ErrorMessage("At least one of the files or directories specified at the command line could not be found.");
            }

            // Scan for other files near the selected files
            // (Currently only active if a single file is specified)
            //if (filesToLoad.Count == 1)
            //{
            //    foreach (string fileName in filesToLoad.Distinct().ToArray())
            //    {
            //        if (fileName.ToLowerInvariant().EndsWith(".txd") && File.Exists(fileName))
            //        {
            //            // Existing .txd file
            //            // Scan same directory for other .txd files
            //            string[] otherFiles = Directory.GetFiles(Path.GetDirectoryName(fileName), "*.txd");
            //            // otherFiles should contain fileName and may contain additional files
            //            if (otherFiles.Length > 1)
            //            {
            //                if (App.YesNoQuestion("Other Tx dictionary files are located in the same directory as the selected file. Should they also be loaded?"))
            //                {
            //                    // Duplicates will be removed later
            //                    filesToLoad.AddRange(otherFiles);
            //                }
            //            }
            //        }
            //    }
            //}
            // NOTE: Loading multiple txd files is not supported. (Only scan for more cultures of version 1 files. - Done)

            if (!FileNameHelper.FindOtherCultures(filesToLoad))
            {
                Application.Current.Shutdown();
            }

            // Create main window and view model
            var view      = new MainWindow();
            var viewModel = new MainViewModel();

            if (filesToLoad.Count == 0 && scanOption.IsSet)
            {
                viewModel.ScanDirectory = scanOption.Value;
            }

            view.DataContext = viewModel;

            // Load selected files
            if (filesToLoad.Count > 0)
            {
                viewModel.LoadFiles(filesToLoad);
            }

            // Show the main window
            view.Show();
        }