public AddRemoteViewModel(AddRemoteWindow view, RemoteGroupModel group, string name, string uri, string extra_headers) { View = view; Group = group; RemoteName = name ?? ""; RemoteURI = uri ?? ""; ExtraHeaders = extra_headers ?? ""; }
public MainViewModel(MainWindow view, string config) { View = view; #region Load Config if (File.Exists(config)) { var json = JSON.ToObject <Dictionary <string, dynamic> >(File.ReadAllText(config)); if (json["version"] == 1) { MessageBox.Show("Old winform configuration is not supported, please migrate the config manually or delete config.json", "Opps", MessageBoxButton.OK, MessageBoxImage.Error); Environment.Exit(0); } if (json["version"] > CONFIG_VERSION && MessageBox.Show(GetLocalized("Message_NewConfigVersion"), "Opps", MessageBoxButton.OKCancel, MessageBoxImage.Warning) != MessageBoxResult.OK) { Environment.Exit(0); } SetLanguage(json.ContainsKey("language") ? json["language"] : null, false); AllowAddListener = !json.ContainsKey("allow_add_listener") || json["allow_add_listener"]; AllowWindowResize = !json.ContainsKey("allow_window_resize") || json["allow_window_resize"]; ScanLeftover = !json.ContainsKey("check_leftover") || json["check_leftover"]; if (json.ContainsKey("width")) { View.Width = json["width"]; } if (json.ContainsKey("height")) { View.Height = json["height"]; } if (json.ContainsKey("remotes")) { foreach (KeyValuePair <string, object> g in json["remotes"]) { try { var group = new RemoteGroupModel(g.Key, this); foreach (KeyValuePair <string, dynamic> r in (Dictionary <string, dynamic>)g.Value) { try { group.Add(new RemoteModel(r.Key) { Remote = new UriBuilder(r.Value["remote"]), ExtraHeaders = r.Value.ContainsKey("extra_headers") ? RemoteModel.ParseExtraHeaders(r.Value["extra_headers"]) : null }); } catch (Exception e) { if (MessageBox.Show(string.Format(GetLocalized("Message_RemoteParseError"), g.Key, e.ToString()), "Error", MessageBoxButton.YesNo, MessageBoxImage.Error) == MessageBoxResult.Yes) { Environment.Exit(0); } } } Remotes.Add(group); } catch (Exception e) { if (MessageBox.Show(string.Format(GetLocalized("Message_GroupParseError"), g.Key, e.ToString()), "Error", MessageBoxButton.YesNo, MessageBoxImage.Error) == MessageBoxResult.Yes) { Environment.Exit(0); } } } } if (json.ContainsKey("listeners")) { foreach (var l in json["listeners"]) { var listener = new ListenerModel(this, l["listen"]); if (l["remote"] is Dictionary <string, dynamic> ) { var name = l["remote"]["name"]; var group = l["remote"]["group"]; foreach (var g in Remotes) { if (g.Name == group) { foreach (var r in g) { if (r.Name == name) { listener.Remote = r; break; } } break; } } } if (l.ContainsKey("enable") && l["enable"]) { listener.Enabled = true; } Listeners.Add(listener); } } if (json.ContainsKey("subscriptions")) { var sub = (Dictionary <string, dynamic>)json["subscriptions"]; foreach (KeyValuePair <string, dynamic> kv in sub["data"]) { var s = (Dictionary <string, dynamic>)kv.Value; Subscriptions.Add(new SubscriptionModel(this, kv.Key, s["url"], s["enable"], DateTime.Parse(s["last_update"]))); } SubscriptionLastUpdate = DateTime.Parse(sub["last_update"]); SubscriptionUpdateInterval = (int)sub["update_interval"]; } Log("Config loaded."); } #endregion if (ScanLeftover) { Log("Scanning for leftover processes..."); SearchLeftoverProcesses(); } if (Remotes.Count == 0) { Remotes.Add(new RemoteGroupModel("Default", this)); } ConfigPath = config; Remotes.CollectionChanged += (s, e) => ReloadTrayMenu(); Listeners.CollectionChanged += (s, e) => ReloadTrayMenu(); Listeners.Add(new FakeListener(this)); Subscriptions.Add(new FakeSubscription()); SwitchTab(0); ReloadTrayMenu(); mainTicker = new Thread(new ThreadStart(() => { while (true) { Tick++; try { foreach (var l in Listeners) { if (l.IsReal) { l.Real.Tick(); } } } catch (ThreadAbortException) { break; } catch { } Thread.Sleep(200); } })) { IsBackground = true }; mainTicker.Start(); subscriptionTimer = new Timer(s => UpdateSubscription(), null, 0, 60000); Microsoft.Win32.SystemEvents.SessionEnding += (s, e) => { ConfigPath = null; mainTicker.Abort(); foreach (var l in Listeners) { if (l.IsReal) { l.Real.Enabled = false; l.Real.Stop(); } } }; }