Exemple #1
0
        private async Task ProcessImportQueue()
        {
            while (true)
            {
                DictionaryViewModel dictionaryViewModel = Dictionaries.FirstOrDefault(dict => dict.Status == DictionaryStatus.Queued);

                if (dictionaryViewModel == null)
                {
                    break;
                }

                dictionaryViewModel.Status = DictionaryStatus.Installing;

                try
                {
                    await DatabaseManager.Instance.ImportWordlist(dictionaryViewModel, cancellationTokenSource.Token);

                    await DatabaseManager.Instance.OptimizeTable(dictionaryViewModel.Dictionary);
                }
                catch (OperationCanceledException)
                {
                    Dictionaries.Remove(dictionaryViewModel);
                    cancellationTokenSource = new CancellationTokenSource();
                    continue;
                }
                catch
                {
                    ContentDialog contentDialog = new ContentDialog()
                    {
                        Title           = resourceLoader.GetString("Import_Error_Title"),
                        Content         = string.Format(resourceLoader.GetString("Import_Error_Body"), dictionaryViewModel.OriginLanguage, dictionaryViewModel.DestinationLanguage),
                        CloseButtonText = "OK",
                        XamlRoot        = MainWindow.Instance.Content.XamlRoot
                    };

                    await contentDialog.ShowAsync();

                    Dictionaries.Remove(dictionaryViewModel);
                    continue;
                }
                finally
                {
                    dictionaryViewModel.WordlistReader.Dispose();
                    dictionaryViewModel.WordlistReader = null;
                }

                dictionaryViewModel.Status = DictionaryStatus.Installed;

                DictionariesChanged?.Invoke(this, EventArgs.Empty);
            }
        }
        public TaskPropertyDictionary OpenDictionary(String TaskName)
        {
            var output = Dictionaries.FirstOrDefault(x => x.TaskName == TaskName);

            if (output == null)
            {
                output = new TaskPropertyDictionary()
                {
                    TaskName = TaskName
                };
                Dictionaries.Add(output);
            }

            return(output);
        }
Exemple #3
0
        private async Task <bool> CheckForConflictingDictionary(WordlistReader wordlistReader)
        {
            DictionaryViewModel conflictingDictionary = Dictionaries.FirstOrDefault(dict =>
                                                                                    (dict.OriginLanguageCode == wordlistReader.OriginLanguageCode && dict.DestinationLanguageCode == wordlistReader.DestinationLanguageCode) ||
                                                                                    (dict.OriginLanguageCode == wordlistReader.DestinationLanguageCode && dict.DestinationLanguageCode == wordlistReader.OriginLanguageCode));

            if (conflictingDictionary == null)
            {
                return(true);
            }

            DateTimeFormatter dateTimeFormatter = new DateTimeFormatter("shortdate shorttime");

            string content = resourceLoader.GetString("Import_Conflict_Body1");

            content += "\r\n\r\n";
            content += string.Format(resourceLoader.GetString("Import_Conflict_Body2"),
                                     conflictingDictionary.OriginLanguage, conflictingDictionary.DestinationLanguage, dateTimeFormatter.Format(conflictingDictionary.CreationDate));
            content += "\r\n";
            content += string.Format(resourceLoader.GetString("Import_Conflict_Body3"),
                                     conflictingDictionary.OriginLanguage, conflictingDictionary.DestinationLanguage, dateTimeFormatter.Format(wordlistReader.CreationDate));

            ContentDialog contentDialog = new ContentDialog()
            {
                Title             = resourceLoader.GetString("Import_Conflict_Title"),
                Content           = content,
                PrimaryButtonText = resourceLoader.GetString("Import_Conflict_Replace"),
                CloseButtonText   = resourceLoader.GetString("Import_Conflict_Skip"),
                DefaultButton     = ContentDialogButton.Primary,
                XamlRoot          = MainWindow.Instance.Content.XamlRoot
            };

            bool replace = await contentDialog.ShowAsync() == ContentDialogResult.Primary;

            if (replace)
            {
                bool removedSuccessfully = await RemoveDictionary(conflictingDictionary);

                // as long as another import is still in process, we cannot replace a dictionary
                if (!removedSuccessfully)
                {
                    return(false);
                }
            }

            return(replace);
        }
        /// <summary>
        /// Imports a dictionary, the imported dictionary is added to the dictionaries collection.
        /// </summary>
        private async Task AddDictionaryAsync()
        {
            BusyStateManager.EnterBusy();
            BusyStateManager.SetMessage(SeverityType.Info, "Opening OpenFileDialog.");

            await Task.Run(() =>
            {
                OpenFileDialog dlg   = new OpenFileDialog();
                dlg.DefaultExt       = ".txt";                                        // Default file extension
                dlg.Filter           = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; // Filter files by extension
                dlg.InitialDirectory = _configurationProvider.DictionariesDir;
                dlg.Title            = "Import dictionaries";
                dlg.Multiselect      = true;

                bool dialogResult = dlg.ShowDialog() ?? false;
                if (dialogResult)
                {
                    foreach (string FileName in dlg.FileNames)
                    {
                        App.Current.Dispatcher.BeginInvoke(new System.Action(() =>
                        {
                            Dictionaries.Add(new Dictionary(FileName, Encoding.UTF8));
                        }));
                    }

                    /* Set dialog initial directory to last visited directory. */
                    string file = dlg.FileNames.FirstOrDefault();
                    if (file != default(string))
                    {
                        _configurationProvider.DictionariesDir = Path.GetDirectoryName(file);
                    }
                }
            });

            /* Select first dictionary in list. */
            SelectedDictionary = Dictionaries.FirstOrDefault();
            BusyStateManager.SetMessage(SeverityType.None);
            BusyStateManager.ExitBusy();
        }