Exemplo n.º 1
0
        private void BtnSave_Click(object sender, EventArgs e)
        {
            string path;

            using (var save = new SaveFileDialog
            {
                Title = Words.SaveVocabularyBook,
                FileName = TbMotherTongue.Text + " - " + TbForeignLang.Text,
                InitialDirectory = Settings.Default.VhfPath,
                Filter = Words.VocupVocabularyBookFile + " (*.vhf)|*.vhf"
            })
            {
                if (save.ShowDialog() == DialogResult.OK)
                {
                    path = save.FileName;
                }
                else
                {
                    DialogResult = DialogResult.Cancel;
                    return;
                }
            }

            Cursor.Current = Cursors.WaitCursor;

            var result = new VocabularyBook
            {
                MotherTongue = TbMotherTongue.Text,
                ForeignLang  = TbForeignLang.Text,
                FilePath     = path
            };

            foreach (var book in books)
            {
                foreach (var word in book.Words)
                {
                    CopyWord(word, result);
                }
            }

            result.GenerateVhrCode();

            if (!VocabularyFile.WriteVhfFile(path, result) ||
                !VocabularyFile.WriteVhrFile(result))
            {
                MessageBox.Show(Messages.VocupFileWriteError, Messages.VocupFileWriteErrorT, MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                DialogResult = DialogResult.Abort;
            }
            else
            {
                DialogResult = DialogResult.OK;
            }

            Cursor.Current = Cursors.Default;
        }
Exemplo n.º 2
0
        public bool Read(VocabularyBook book)
        {
            if (string.IsNullOrWhiteSpace(book.VhrCode))
            {
                return(false);
            }
            FileInfo vhrInfo = new FileInfo(Path.Combine(Settings.Default.VhrPath, book.VhrCode + ".vhr"));

            if (!vhrInfo.Exists)
            {
                return(false);
            }

            string plaintext;

            try
            {
                plaintext = ReadFile(vhrInfo.FullName);
            }
            catch (FormatException)
            {
                DeleteInvalidFile(vhrInfo);
                return(false);
            }
            catch (System.Security.Cryptography.CryptographicException)
            {
                DeleteInvalidFile(vhrInfo);
                return(false);
            }

            using (StringReader reader = new StringReader(plaintext))
            {
                string path = reader.ReadLine();
                string mode = reader.ReadLine();

                if (string.IsNullOrWhiteSpace(path) ||
                    string.IsNullOrWhiteSpace(mode) || !int.TryParse(mode, out int imode) || !((PracticeMode)imode).IsValid())
                {
                    DeleteInvalidFile(vhrInfo);
                    return(false);
                }

                List <(int stateNumber, DateTime date)> results = new List <(int stateNumber, DateTime date)>();

                while (true)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    string[] columns = line.Split('#');
                    if (columns.Length != 2 || !int.TryParse(columns[0], out int state) || !PracticeStateHelper.Parse(state).IsValid())
                    {
                        DeleteInvalidFile(vhrInfo);
                        return(false);
                    }
                    DateTime time = DateTime.MinValue;
                    if (!string.IsNullOrWhiteSpace(columns[1]) && !DateTime.TryParseExact(columns[1], "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
                    {
                        DeleteInvalidFile(vhrInfo);
                        return(false);
                    }
                    results.Add((state, time));
                }

                bool countMatch = book.Words.Count == results.Count;

                FileInfo vhfInfo  = new FileInfo(book.FilePath);
                FileInfo pathInfo = new FileInfo(path);

                if (vhfInfo.FullName.Equals(pathInfo.FullName, StringComparison.OrdinalIgnoreCase))
                {
                    if (!countMatch)
                    {
                        MessageBox.Show(Messages.VhrInvalidRowCount, Messages.VhrCorruptFileT, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        try { vhrInfo.Delete(); } catch { }
                        return(false);
                    }
                }
                else
                {
                    if (!countMatch)
                    {
                        MessageBox.Show(Messages.VhrInvalidRowCountAndOtherFile, Messages.VhrCorruptFileT, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return(false);
                    }

                    if (pathInfo.Exists)
                    {
                        book.GenerateVhrCode(); // Save new results file if the old one is in use by another file
                    }
                    book.UnsavedChanges = true;
                }

                book.PracticeMode = (PracticeMode)imode;

                for (int i = 0; i < book.Words.Count; i++)
                {
                    VocabularyWord word = book.Words[i];
                    (word.PracticeStateNumber, word.PracticeDate) = results[i];
                }
            }

            return(true);
        }