/// <summary>
        /// Attempts to load the config file into memory
        /// </summary>
        public void Load()
        {
            try {
                using (var stream = File.OpenRead(_configFile)) {
                    _config = (Config)_serializer.ReadObject(stream);
                }
            }
            catch (Exception) { // file is either corrupt or doesn't exist (or some other error)
                try {           // try and back the file up
                    File.Move(_configFile, _configFile + ".errored");
                }
                catch (Exception) {
                    // ignored
                }

                _config = Config.DefaultConfig;

                Save();
            }

            if (_config.Sync)
            {
                _fileWatcher.Start();
            }

            _configUpdated();
        }
Beispiel #2
0
        public MainWindow()
        {
            InitializeComponent();

            _configManager = new ConfigManager("config.json", OnConfigUpdated);

            var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                        "Google Play Music Desktop Player", "json_store", "playback.json");

            var fileWatcher = new JsonWatcher <Playback, PlaybackUpdateEvent>(fileName, 150)
            {
                CheckUpdate = (c, p) => new PlaybackUpdateEvent(c, p),
                OnUpdate    = OnPlaybackUpdate,
                OnLoad      = p => {
                    if (p.IsNull())
                    {
                        return;
                    }

                    Dispatcher.Invoke(new JsonWatcher <Playback, PlaybackUpdateEvent> .DoOnUpdate(OnPlaybackUpdate),
                                      new PlaybackUpdateEvent(p, new Playback()));
                }
            };

            Loaded += (s, e) => {
                fileWatcher.Start();
                Visibility = Visibility.Hidden;
                _configManager.Load();
            };
            Deactivated += WindowDeactivated;
        }