FileWatcher/code/src/Preferences.cs

315 lines
9.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace FileWatcher
{
public sealed partial class Preferences : Form
{
#region Singleton
private static readonly Preferences instance = new Preferences();
static Preferences()
{
}
public static Preferences Instance
{
get
{
return instance;
}
}
#endregion
private class ProgramData
{
public string Path;
public bool Save;
public ProgramData(string path, bool save = false)
{
Path = path;
Save = save;
}
}
private Dictionary<string, ProgramData> programs = new Dictionary<string, ProgramData>();
private BindingList<string> UnboundBindingList = null;
private Preferences()
{
InitializeComponent();
// TODO: load preferences
UnboundBindingList = new BindingList<string>(new List<string>(programs.Keys));
listBox1.DataSource = UnboundBindingList;
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
SetRunAtStartup(registryKey.GetValue(Form1.ApplicationName) != null);
LoadEditorPreferences(new List<string>(), new List<Tuple<string,string>>());
}
public void LoadEditorPreferences(List<string> defaultPrograms, List<Tuple<string,string>> userDefinedPrograms)
{
programs.Clear();
#region Get System Default Text Editor
//http://stackoverflow.com/questions/11365984/c-sharp-open-file-with-default-application-and-parameters
string textEditorDefault = "";
string InstallPath = (string)Registry.GetValue(@"HKEY_CLASSES_ROOT\.txt", "", null);
if (InstallPath != null)
{
string commandPath = (string)Registry.GetValue(@"HKEY_CLASSES_ROOT\\" + InstallPath + "\\Shell\\Open\\Command", "", null);
string ext = ".exe";
int indexOf = commandPath.ToLower().LastIndexOf(ext);
if (indexOf > 0)
{
// strip off arguments (%1,%2,etc)
textEditorDefault = commandPath.Substring(0, indexOf + ext.Length);
}
}
#endregion
string pfx64 = Utils.ProgramFilesx64();
string pfx86 = Utils.ProgramFilesx86();
string sys = Utils.SystemDirectory();
// Add system default options first so the user options can overwrite these
if (File.Exists(textEditorDefault))
{
programs.Add("System Default", new ProgramData(textEditorDefault));
}
if (File.Exists(pfx64 + "\\Sublime Text 3\\sublime_text.exe"))
{
programs.Add("Sublime Text 3", new ProgramData(pfx64 + "\\Sublime Text 3\\sublime_text.exe"));
}
if (File.Exists(pfx64 + "\\Sublime Text 2\\sublime_text.exe"))
{
programs.Add("Sublime Text 2", new ProgramData(pfx64 + "\\Sublime Text 2\\sublime_text.exe"));
}
if (File.Exists(pfx64 + "\\Sublime Text\\sublime_text.exe"))
{
programs.Add("Sublime Text", new ProgramData(pfx64 + "\\Sublime Text\\sublime_text.exe"));
}
if (File.Exists(pfx86 + "\\Notepad++\\notepad++.exe"))
{
programs.Add("Notepad++", new ProgramData(pfx86 + "\\Notepad++\\notepad++.exe"));
}
if (File.Exists(sys + "\\notepad.exe"))
{
programs.Add("NotePad", new ProgramData(sys + "\\notepad.exe"));
}
foreach (Tuple<string, string> t in userDefinedPrograms)
{
programs.Add(t.Item1, new ProgramData(t.Item2,true));
if (File.Exists(t.Item2) == false)
{
Logger.WriteLine("A program set as one of the default text editors does not exist on your system: " + t.Item2);
}
}
defaultPrograms.RemoveAll(str => !programs.ContainsKey(str) || !File.Exists(programs[str].Path));
UnboundBindingList = new BindingList<string>(defaultPrograms);
foreach (string str in programs.Keys)
{
if (defaultPrograms.Contains(str) == false)
{
defaultPrograms.Add(str);
}
}
listBox1.DataSource = UnboundBindingList;
}
public string GetDefaultEditor()
{
foreach (string programName in UnboundBindingList)
{
string path = programs[programName].Path;
if (File.Exists(path))
{
return path;
}
}
return "";
}
public List<string> GetDefaultPrograms()
{
return new List<string>(UnboundBindingList);
}
// Gets user defined paths
public List<Tuple<string,string>> GetEditorProgramPaths()
{
List<Tuple<string, string>> programNames = new List<Tuple<string, string>>();
foreach (KeyValuePair<string,ProgramData> t in programs)
{
if (t.Value.Save)
{
programNames.Add(new Tuple<string, string>(t.Key, t.Value.Path));
}
}
return programNames;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void saveButton_Click(object sender, EventArgs e)
{
Form1.Instance.Save();
}
private void cancelButton_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (listBox1.SelectedValue != null)
{
string selectedValue = (string)listBox1.SelectedValue;
if (programs.ContainsKey(selectedValue))
{
string path = programs[(string)listBox1.SelectedValue].Path;
textBox1.Text = path;
}
}
}
private void Preferences_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
int selectedIndex = listBox1.SelectedIndex;
if (selectedIndex >= listBox1.Items.Count - 1)
{
return;
}
listBox1.DataSource = null;
listBox1.SelectionMode = SelectionMode.None;
string obj1 = UnboundBindingList[selectedIndex];
UnboundBindingList[selectedIndex] = UnboundBindingList[selectedIndex + 1];
UnboundBindingList[selectedIndex + 1] = obj1;
listBox1.DataSource = UnboundBindingList;
listBox1.SelectionMode = SelectionMode.One;
listBox1.SelectedIndex = selectedIndex + 1;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
int selectedIndex = listBox1.SelectedIndex;
if (selectedIndex == 0)
{
return;
}
listBox1.DataSource = null;
listBox1.SelectionMode = SelectionMode.None;
string obj1 = UnboundBindingList[selectedIndex];
UnboundBindingList[selectedIndex] = UnboundBindingList[selectedIndex - 1];
UnboundBindingList[selectedIndex - 1] = obj1;
listBox1.DataSource = UnboundBindingList;
listBox1.SelectionMode = SelectionMode.One;
listBox1.SelectedIndex = selectedIndex - 1;
}
public void SetRunAtStartup(bool yes)
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (registryKey == null)
{
return;
}
if (yes)
{
registryKey.SetValue(Form1.ApplicationName, Application.ExecutablePath + " --minimize");
}
else
{
if (registryKey.GetValue(Form1.ApplicationName) != null)
{
registryKey.DeleteValue(Form1.ApplicationName);
}
}
runAtStartupCB.Checked = yes;
}
private void runAtStartupCB_CheckedChanged(object sender, EventArgs e)
{
SetRunAtStartup(runAtStartupCB.Checked);
}
private void Preferences_Load(object sender, EventArgs e)
{
}
}
}