示例#1
0
        public static void Load()
        {
            var file = Config.Instance.DataDir + "Database.xml";

            if (!File.Exists(file))
            {
                return;
            }
            try
            {
                _instance = XmlManager <VocabularyDatabase> .Load(file);
            }
            catch (Exception)
            {
                //failed loading deckstats
                var corruptedFile = Helper.GetValidFilePath(Config.Instance.DataDir, "Database_corrupted", "xml");
                try
                {
                    File.Move(file, corruptedFile);
                }
                catch (Exception)
                {
                    throw new Exception("Can not load or move Database.xml file. Please manually delete the file in \"%appdata\\VocabularyTrainer\".");
                }

                //get latest backup file
                var backup =
                    new DirectoryInfo(Config.Instance.DataDir).GetFiles("Database_backup*")
                    .OrderByDescending(x => x.CreationTime)
                    .FirstOrDefault();
                if (backup != null)
                {
                    try
                    {
                        File.Copy(backup.FullName, file);
                        _instance = XmlManager <VocabularyDatabase> .Load(file);
                    }
                    catch (Exception)
                    {
                        throw new Exception("Error restoring Database backup. Please manually rename \"Database_backup.xml\" to \"Database.xml\" in \"%appdata\\VocabularyTrainer\".");
                    }
                }
                else
                {
                    //can't call ShowMessageAsync on MainWindow at this point. todo: Add something like a message queue.
                    //MessageBox.Show("Your Database file got corrupted and there was no backup to restore from.", "Error restoring Database backup");
                    Logger.WriteLine("Your Database file got corrupted and there was no backup to restore from.", "Error restoring Database backup");
                }
            }
        }
示例#2
0
        public MainWindow()
        {
            InitializeComponent();
            Helper.MainWindow = this;
            Config.Load();

            Config.Instance.SaveDataInAppData = false;
            Config.Save();

            VocabularyDatabase.Load();

            // Add categories and lections
            foreach (Vocabulary v in VocabularyDatabase.Instance.vocs)
            {
                if (!Config.Instance.categories.Contains(v.cat))
                {
                    Config.Instance.categories.Add(v.cat);
                }
                if (!Config.Instance.lections.Contains(v.lection))
                {
                    Config.Instance.lections.Add(v.lection);
                }
            }

            Config.Save();

            foreach (string s in Config.Instance.categories)
            {
                AddVocabulary.comboCategory.Items.Add(s);
                Options.comboCategory.Items.Add(s);
                System.Windows.Controls.CheckBox cc = new System.Windows.Controls.CheckBox();
                cc.Checked   += (o, e) => { view.Refresh(); };
                cc.Unchecked += (o, e) => { view.Refresh(); };
                cc.Content    = s;
                menuKategorie.Items.Add(cc);
            }

            foreach (string s in Config.Instance.lections)
            {
                AddVocabulary.comboLection.Items.Add(s);
                Options.comboLection.Items.Add(s);
                System.Windows.Controls.CheckBox cc = new System.Windows.Controls.CheckBox();
                cc.Checked   += (o, e) => { view.Refresh(); };
                cc.Unchecked += (o, e) => { view.Refresh(); };
                cc.Content    = s;
                menuLektionen.Items.Add(cc);
            }

            ObservableCollection <Vocabulary> custdata = VocabularyDatabase.Instance.vocs;

            var itemSourceView = new CollectionViewSource()
            {
                Source = VocabularyDatabase.Instance.vocs
            };

            view = itemSourceView.View;

            view.Filter = new Predicate <object>(
                item =>
            {
                Vocabulary voc = (Vocabulary)item;
                if (!this.searchText.Text.Equals(""))
                {
                    if (voc.german.Contains(this.searchText.Text) || voc.japanese.Contains(this.searchText.Text) || voc.romaji.Contains(this.searchText.Text))
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }

                Predicate <object> stdPred = new Predicate <object>(x => { return(true); });

                menuLektionen.Items.Filter = new Predicate <object>(x => { return(((System.Windows.Controls.CheckBox)x).IsChecked.Value); });
                menuKategorie.Items.Filter = new Predicate <object>(x => { return(((System.Windows.Controls.CheckBox)x).IsChecked.Value); });

                List <string> cLektionen = new List <string>();
                foreach (System.Windows.Controls.CheckBox c in menuLektionen.Items)
                {
                    cLektionen.Add(c.Content.ToString());
                }
                List <string> cKategorien = new List <string>();
                foreach (System.Windows.Controls.CheckBox c in menuKategorie.Items)
                {
                    cKategorien.Add(c.Content.ToString());
                }

                menuLektionen.Items.Filter = stdPred;
                menuKategorie.Items.Filter = stdPred;

                //Filter sind aktiv
                if ((cLektionen.Count == 0 || cLektionen.Contains(voc.lection, null)) &&
                    (cKategorien.Count == 0 || cKategorien.Contains(voc.cat)))
                {
                    return(true);
                }

                //Fällt in keine Kategorie oder Lektion
                return(false);
            }
                );
            dataGrid.ItemsSource = view;
        }
示例#3
0
 private void MetroWindow_Closed(object sender, EventArgs e)
 {
     Config.Save();
     VocabularyDatabase.Save();
 }