2020-10-25 19:52:14 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using RGBController2.Commands;
|
|
|
|
|
using RGBController2.ViewModels.Tabs;
|
2020-10-27 12:06:59 +00:00
|
|
|
|
using RGBController2.ViewModels.Dialogs;
|
2020-10-27 20:07:26 +00:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Xml;
|
2020-10-29 20:47:52 +00:00
|
|
|
|
using System.IO;
|
2020-10-30 20:30:41 +00:00
|
|
|
|
using System.Linq;
|
2020-10-25 19:52:14 +00:00
|
|
|
|
|
|
|
|
|
namespace RGBController2.ViewModels
|
|
|
|
|
{
|
|
|
|
|
class MainViewModel
|
|
|
|
|
{
|
2020-11-03 14:24:46 +00:00
|
|
|
|
|
2020-10-30 20:30:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is the directory of the exe
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string BaseDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
|
|
|
|
|
2020-10-27 20:07:26 +00:00
|
|
|
|
private readonly ObservableCollection<Tab> _tabs;
|
2020-10-25 19:52:14 +00:00
|
|
|
|
public ICommand NewTabCommand { get; }
|
2020-10-27 20:07:26 +00:00
|
|
|
|
public static ICollection<Tab> Tabs { get; set; }
|
2020-10-25 19:52:14 +00:00
|
|
|
|
|
2020-10-29 20:47:52 +00:00
|
|
|
|
private string configLocation = "config.xml";
|
2020-10-30 20:30:41 +00:00
|
|
|
|
|
|
|
|
|
private bool _runOnStartUp;
|
|
|
|
|
|
|
|
|
|
public bool RunOnStartUp
|
|
|
|
|
{
|
|
|
|
|
get { return _runOnStartUp; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value != _runOnStartUp)
|
|
|
|
|
{
|
|
|
|
|
_runOnStartUp = value;
|
|
|
|
|
RunAppOnStartup(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 19:52:14 +00:00
|
|
|
|
public MainViewModel()
|
|
|
|
|
{
|
2020-11-03 14:24:46 +00:00
|
|
|
|
configLocation = Path.Combine(BaseDir, "config.xml");
|
2020-10-25 19:52:14 +00:00
|
|
|
|
NewTabCommand = new ActionCommand(p => NewTab());
|
|
|
|
|
|
2020-10-27 20:07:26 +00:00
|
|
|
|
_tabs = new ObservableCollection<Tab>();
|
2020-10-25 19:52:14 +00:00
|
|
|
|
_tabs.CollectionChanged += Tabs_CollectionChanged;
|
|
|
|
|
|
|
|
|
|
Tabs = _tabs;
|
2020-10-27 20:07:26 +00:00
|
|
|
|
|
2020-10-30 20:30:41 +00:00
|
|
|
|
_runOnStartUp = IsRunAppOnStartup();
|
|
|
|
|
|
2020-10-27 20:07:26 +00:00
|
|
|
|
// Check here that the config file exists
|
2020-10-29 20:47:52 +00:00
|
|
|
|
if (File.Exists(configLocation))
|
2020-10-27 20:07:26 +00:00
|
|
|
|
{
|
2020-10-29 20:47:52 +00:00
|
|
|
|
// Load the config file
|
2020-11-03 14:24:46 +00:00
|
|
|
|
using (XmlReader reader = XmlReader.Create(configLocation))
|
2020-10-27 20:07:26 +00:00
|
|
|
|
{
|
2020-10-29 20:47:52 +00:00
|
|
|
|
string name = "";
|
|
|
|
|
string type = "";
|
2020-10-30 20:30:41 +00:00
|
|
|
|
string device = "";
|
|
|
|
|
|
2020-10-29 20:47:52 +00:00
|
|
|
|
while (reader.Read())
|
2020-10-27 20:07:26 +00:00
|
|
|
|
{
|
2020-10-29 20:47:52 +00:00
|
|
|
|
if (reader.IsStartElement())
|
2020-10-27 20:07:26 +00:00
|
|
|
|
{
|
2020-10-29 20:47:52 +00:00
|
|
|
|
//return only when you have START tag
|
|
|
|
|
switch (reader.Name.ToString())
|
|
|
|
|
{
|
|
|
|
|
case "name":
|
|
|
|
|
name = reader.ReadString();
|
|
|
|
|
break;
|
|
|
|
|
case "type":
|
|
|
|
|
type = reader.ReadString();
|
|
|
|
|
break;
|
2020-10-30 20:30:41 +00:00
|
|
|
|
case "device":
|
|
|
|
|
device = reader.ReadString();
|
|
|
|
|
break;
|
2020-10-29 20:47:52 +00:00
|
|
|
|
}
|
2020-10-27 20:07:26 +00:00
|
|
|
|
}
|
2020-10-29 20:47:52 +00:00
|
|
|
|
// Create the tab here
|
2020-10-30 20:30:41 +00:00
|
|
|
|
if (name != "" && type != "" && device !="")
|
2020-10-27 20:07:26 +00:00
|
|
|
|
{
|
2020-10-29 20:47:52 +00:00
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case "arduino":
|
2020-10-30 20:30:41 +00:00
|
|
|
|
{
|
|
|
|
|
var tab = new ArduinoTab(name);
|
|
|
|
|
tab.SelectedPort = device;
|
|
|
|
|
_tabs.Add(tab);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-10-29 20:47:52 +00:00
|
|
|
|
case "cue":
|
2020-10-30 20:30:41 +00:00
|
|
|
|
{
|
|
|
|
|
var tab = new CueDeviceTab(name);
|
|
|
|
|
switch (device)
|
|
|
|
|
{
|
|
|
|
|
case "Headset":
|
|
|
|
|
tab.SelectedDevice = CUE.NET.Devices.Generic.Enums.CorsairDeviceType.Headset;
|
|
|
|
|
break;
|
|
|
|
|
case "HeadsetStand":
|
|
|
|
|
tab.SelectedDevice = CUE.NET.Devices.Generic.Enums.CorsairDeviceType.HeadsetStand;
|
|
|
|
|
break;
|
|
|
|
|
case "Keyboard":
|
|
|
|
|
tab.SelectedDevice = CUE.NET.Devices.Generic.Enums.CorsairDeviceType.Keyboard;
|
|
|
|
|
break;
|
|
|
|
|
case "Mouse":
|
|
|
|
|
tab.SelectedDevice = CUE.NET.Devices.Generic.Enums.CorsairDeviceType.Mouse;
|
|
|
|
|
break;
|
|
|
|
|
case "Mousemat":
|
|
|
|
|
tab.SelectedDevice = CUE.NET.Devices.Generic.Enums.CorsairDeviceType.Mousemat;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_tabs.Add(tab);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-10-29 20:47:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name = "";
|
|
|
|
|
type = "";
|
2020-10-30 20:30:41 +00:00
|
|
|
|
device = "";
|
2020-10-27 20:07:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-25 19:52:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Tabs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ITab tab;
|
|
|
|
|
|
|
|
|
|
switch (e.Action)
|
|
|
|
|
{
|
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
|
|
|
|
tab = (ITab) e.NewItems[0];
|
|
|
|
|
tab.CloseRequested += OnTabCloseRequested;
|
|
|
|
|
break;
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
|
|
|
|
tab = (ITab) e.OldItems[0];
|
|
|
|
|
tab.CloseRequested -= OnTabCloseRequested;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTabCloseRequested(object sender, EventArgs e)
|
|
|
|
|
{
|
2020-10-27 20:07:26 +00:00
|
|
|
|
Tabs.Remove((Tab) sender);
|
2020-10-25 19:52:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void NewTab()
|
|
|
|
|
{
|
2020-10-27 12:06:59 +00:00
|
|
|
|
// Open the add tab dialog
|
|
|
|
|
var dialog = new NewTabDialogViewModel();
|
|
|
|
|
dialog.ShowDialogWindow();
|
|
|
|
|
|
|
|
|
|
// Use the results from the dialog when creating the new tab
|
|
|
|
|
if (dialog.Success)
|
|
|
|
|
{
|
2020-10-27 20:07:26 +00:00
|
|
|
|
switch(dialog.SelectedDeviceType)
|
|
|
|
|
{
|
|
|
|
|
case 0: // Arduino
|
|
|
|
|
Tabs.Add(new ArduinoTab(dialog.DeviceName));
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
Tabs.Add(new CueDeviceTab(dialog.DeviceName));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-10-27 12:06:59 +00:00
|
|
|
|
}
|
2020-10-25 19:52:14 +00:00
|
|
|
|
}
|
2020-10-27 12:06:59 +00:00
|
|
|
|
|
2020-10-27 20:07:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This function is called when the application is closed.
|
|
|
|
|
/// This allows the configuration to be saved to disk.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
public void OnWindowClosing(object sender, CancelEventArgs e)
|
|
|
|
|
{
|
2020-10-29 20:47:52 +00:00
|
|
|
|
// Check if the config file currently exists
|
|
|
|
|
// if it does then we will need to delete it beofre we can save a new config
|
|
|
|
|
if (File.Exists(configLocation))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(configLocation);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 20:07:26 +00:00
|
|
|
|
// This may need to be saved somwhere better, for now we will just save it
|
|
|
|
|
// with the executable
|
2020-10-29 20:47:52 +00:00
|
|
|
|
XmlWriter xmlWriter = XmlWriter.Create(configLocation);
|
2020-10-27 20:07:26 +00:00
|
|
|
|
xmlWriter.WriteStartDocument();
|
|
|
|
|
|
|
|
|
|
xmlWriter.WriteStartElement("tabs");
|
|
|
|
|
xmlWriter.WriteAttributeString("count", Tabs.Count.ToString());
|
|
|
|
|
foreach (var tab in Tabs)
|
|
|
|
|
{
|
|
|
|
|
xmlWriter.WriteStartElement("tab");
|
|
|
|
|
xmlWriter.WriteStartElement("name");
|
|
|
|
|
xmlWriter.WriteString(tab.Name);
|
|
|
|
|
xmlWriter.WriteEndElement();
|
|
|
|
|
|
|
|
|
|
switch (tab.TabType)
|
|
|
|
|
{
|
|
|
|
|
case Tab.tabType.Arduino:
|
|
|
|
|
xmlWriter.WriteStartElement("type");
|
|
|
|
|
xmlWriter.WriteString("arduino");
|
|
|
|
|
xmlWriter.WriteEndElement();
|
2020-10-30 20:30:41 +00:00
|
|
|
|
xmlWriter.WriteStartElement("device");
|
|
|
|
|
xmlWriter.WriteString(((ArduinoTab)tab).SelectedPort);
|
|
|
|
|
xmlWriter.WriteEndElement();
|
2020-10-27 20:07:26 +00:00
|
|
|
|
break;
|
|
|
|
|
case Tab.tabType.CUE:
|
|
|
|
|
xmlWriter.WriteStartElement("type");
|
|
|
|
|
xmlWriter.WriteString("cue");
|
|
|
|
|
xmlWriter.WriteEndElement();
|
2020-10-30 20:30:41 +00:00
|
|
|
|
xmlWriter.WriteStartElement("device");
|
|
|
|
|
xmlWriter.WriteString(((CueDeviceTab)tab).SelectedDevice.ToString());
|
|
|
|
|
xmlWriter.WriteEndElement();
|
2020-10-27 20:07:26 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
xmlWriter.WriteEndElement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xmlWriter.WriteEndDocument();
|
|
|
|
|
xmlWriter.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 20:30:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if run on startup is enabled in the registry for the current user.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Truw if run on startup is enabled, false if not</returns>
|
|
|
|
|
private bool IsRunAppOnStartup()
|
|
|
|
|
{
|
|
|
|
|
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
|
|
|
|
|
return key.GetValueNames().Contains("RGB Application");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper function for modifying the registry to enable/disable run on startup.
|
|
|
|
|
/// This only enables run on startup for the current user, not the whole machine.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="enabled">True if should be enabled, false if should be disabled.</param>
|
|
|
|
|
private void RunAppOnStartup(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
if (enabled)
|
|
|
|
|
{
|
|
|
|
|
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
|
|
|
|
|
key.SetValue("RGB Application", Path.Combine(BaseDir, System.AppDomain.CurrentDomain.FriendlyName) + ".exe");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
|
|
|
|
|
key.DeleteValue("RGB Application", false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-25 19:52:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|