using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.Composition; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.IO.IsolatedStorage; using System.Linq; using System.Reflection; using System.Runtime.Serialization; using System.Text; using System.Windows.Forms; using Pithos.Client.Properties; using Pithos.Core; using Pithos.Interfaces; using Pithos.ShellExtensions; namespace Pithos.Client { public partial class Preferences : Form { [Import] public IStatusChecker StatusChecker; [Import] public IPithosSettings Settings; bool _allowVisible; // ContextMenu's Show command used bool _allowClose; // ContextMenu's Exit command used bool _loadFired; // Form was shown once [Import] public PithosMonitor Monitor; public Preferences() { InitializeComponent(); } protected override void SetVisibleCore(bool value) { if (!_allowVisible) value = false; base.SetVisibleCore(value); } protected override void OnFormClosing(FormClosingEventArgs e) { if (!_allowClose) { this.Hide(); e.Cancel = true; } base.OnFormClosing(e); } private void openPithosFolderToolStripMenuItem_Click(object sender, EventArgs e) { OpenPithosPath(); } private void notifyIcon_DoubleClick(object sender, EventArgs e) { OpenPithosPath(); } private void OpenPithosPath() { Process.Start(Settings.PithosPath); } private Dictionary iconNames =new List { new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"), new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching") }.ToDictionary(s => s.Status); public void UpdateStatus() { var pithosStatus=StatusChecker.GetPithosStatus(); if (iconNames.ContainsKey(pithosStatus)) { var info= iconNames[pithosStatus]; notifyIcon.Icon = (Icon) Resources.ResourceManager.GetObject(info.IconName); notifyIcon.Text = String.Format("Pithos 1.0\r\n{0}", info.StatusText); notifyIcon.BalloonTipText = info.StatusText; statusToolStripMenuItem.Text = info.StatusText; } if (!String.IsNullOrWhiteSpace(Settings.UserName )&& !String.IsNullOrWhiteSpace(Settings.ApiKey)) Monitor.Start(); } private void btnBrowsePithosPath_Click(object sender, EventArgs e) { pithosFolderBrowser.SelectedPath = txtPithosPath.Text; var result = pithosFolderBrowser.ShowDialog(); if (result == DialogResult.OK) { var newPath = pithosFolderBrowser.SelectedPath; txtPithosPath.Text = newPath; Settings.PithosPath = newPath; Settings.Save(); } } private void preferencesToolStripMenuItem_Click(object sender, EventArgs e) { _allowVisible = true; _loadFired = true; Show(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { _allowClose = _allowVisible = true; Monitor.Stop(); if (!_loadFired) Show(); Close(); } private void btnCancel_Click(object sender, EventArgs e) { Settings.Reload(); Hide(); } private void btnOK_Click(object sender, EventArgs e) { DoSave(); Hide(); } private void btnApply_Click(object sender, EventArgs e) { DoSave(); } private void DoSave() { Settings.Save(); Monitor.Start(); } private void pauseSynchingToolStripMenuItem_Click(object sender, EventArgs e) { Monitor.Pause = !Monitor.Pause; pauseSynchingToolStripMenuItem.Text = Monitor.Pause?"Resume Syncing":"Pause Syncing"; } private void tabAccount_Click(object sender, EventArgs e) { } private void checkActivateExtensions_CheckedChanged(object sender, EventArgs e) { var box = (CheckBox) sender; if (box.Checked) RegisterExtensions(); else UnregisterExtensions(); } private void UnregisterExtensions() { using (var installer = new ProjectInstaller()) { IDictionary state = LoadState(); installer.Uninstall(state); } } private void RegisterExtensions() { using (var installer = new ProjectInstaller()) { IDictionary state = new Dictionary(); installer.Install(state); SaveState(state); } } private static void SaveState(IDictionary state) { using(var store= IsolatedStorageFile.GetUserStoreForApplication()) using(var file=store.CreateFile("PithosManualInstallFile")) { var serializer = new NetDataContractSerializer(); serializer.Serialize(file,state); } } private static IDictionary LoadState() { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { if (!store.FileExists("PithosManualInstallFile")) return new Dictionary(); using (var file = store.OpenFile("PithosManualInstallFile", FileMode.Open)) { var serializer = new NetDataContractSerializer(); var state = serializer.Deserialize(file); return (IDictionary) state; } } } } }