Пример #1
0
        // Checks for existing settings file, if none exists generate a new one using the defaults.
        private void DoGetSettings()
        {
            bool isEmpty = !File.Exists(ConfigClass.settingsFile); // Check if file exists

            // If file doesn't exist, generate one from the DefaultSettings class.
            if (isEmpty)
            {
                UserSettings tempSettings = new UserSettings();
                tempSettings = DefaultSettings.set();
                MainWindow.currentSettings = tempSettings;
                JSON.SerializeSettings(tempSettings);
            }

            // If file does exist, deserialise the file and load the object up into the "currentSettings" object - If the object is unusable, prompts the user and creates settings from default.
            else
            {
                try
                {
                    UserSettings tempSettings = JSON.DeserializeSettings();
                    MainWindow.currentSettings = tempSettings;
                }
                catch
                {
                    MusickError errorWin = new MusickError();
                    errorWin.Owner            = this;
                    errorWin.lblError.Content = "Settings file corrupted - Using default settings";
                    if (errorWin.ShowDialog() == true)
                    {
                        UserSettings tempSettings = new UserSettings();
                        tempSettings = DefaultSettings.set();
                        MainWindow.currentSettings = tempSettings;
                        JSON.SerializeSettings(tempSettings);
                    }
                }
            }
        }
Пример #2
0
        // If a local library (or libraries) exists, this will deserialise the file(s) into the songList object for the Library window to use.
        private async Task <string> DoLoadLibrary()
        {
            await Task.Run(() =>
            {
                foreach (var file in Directory.GetFiles(ConfigClass.appLibraryFolder))
                {
                    ObservableCollection <Song> tempLibrary = JSON.DeserializeLibrary(file);
                    LibraryFile tempLibFile = GenerateLibrary.CreateLibraryEntry(tempLibrary, file);
                    foreach (var song in tempLibrary.ToList())
                    {
                        if (!File.Exists(song.FileLocation))
                        {
                            tempLibrary.Remove(song);
                        }
                    }
                    foreach (var musicFile in Directory.GetFiles(tempLibFile.LibrarySource, "*", SearchOption.AllDirectories))
                    {
                        if (!tempLibrary.Any(p => p.FileLocation == musicFile))
                        {
                            if (file.Contains(".mp3") || file.Contains(".wma") || file.Contains(".wav"))
                            {
                                tempLibrary.Add(GenerateLibrary.GenerateSong(musicFile));
                            }
                        }
                    }
                    JSON.SerializeLibrary(file, tempLibrary);
                    try
                    {
                        foreach (var tempSong in tempLibrary)
                        {
                            tempSongList.Add(tempSong);
                        }

                        MusickSettings.libList.Add(tempLibFile); // Creates an entry for the local library file to be displayed and interracted with.
                    }


                    catch // If the object returned is unusable, it'll throw this error.
                    {
                        Dispatcher.Invoke(() =>
                        {
                            MusickError errorWin      = new MusickError();
                            errorWin.Owner            = this;
                            errorWin.lblError.Content = "One or more libraries are corrupt/missing - Restart to generate a new library";
                            if (errorWin.ShowDialog() == true)
                            {
                                foreach (var libFile in Directory.GetFiles(ConfigClass.appLibraryFolder))
                                {
                                    File.Delete(libFile);
                                }
                                this.Close();
                            }
                        }
                                          );
                    }
                }
            }
                           );

            MusickLibrary.SongList = tempSongList;
            lblStatus.Content      = "Library loaded...";
            await Task.Delay(729);

            return("");
        }