Modifications to allow synchronization of shared files:
[pithos-ms-client] / trunk / Pithos.Client / Preferences.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.ComponentModel.Composition;
6 using System.Data;
7 using System.Diagnostics;
8 using System.Drawing;
9 using System.IO;
10 using System.IO.IsolatedStorage;
11 using System.Linq;
12 using System.Reflection;
13 using System.Runtime.Serialization;
14 using System.Text;
15 using System.Windows.Forms;
16 using Pithos.Client.Properties;
17 using Pithos.Core;
18 using Pithos.Interfaces;
19 using Pithos.ShellExtensions;
20
21 namespace Pithos.Client
22 {
23     public partial class Preferences : Form
24     {
25         [Import]
26         public IStatusChecker StatusChecker;
27
28         [Import] 
29         public IPithosSettings Settings;
30
31
32         bool _allowVisible;     // ContextMenu's Show command used
33         bool _allowClose;       // ContextMenu's Exit command used
34         bool _loadFired;        // Form was shown once
35
36
37         [Import]
38         public PithosMonitor Monitor;
39
40         public Preferences()
41         {
42             InitializeComponent();            
43         }
44
45         protected override void SetVisibleCore(bool value)
46         {
47             if (!_allowVisible) 
48                 value = false;
49             base.SetVisibleCore(value);
50         }
51
52         protected override void OnFormClosing(FormClosingEventArgs e)
53         {
54             if (!_allowClose)
55             {
56                 this.Hide();
57                 e.Cancel = true;
58             }
59             base.OnFormClosing(e);
60         }
61         private void openPithosFolderToolStripMenuItem_Click(object sender, EventArgs e)
62         {
63             OpenPithosPath();
64         }
65
66         private void notifyIcon_DoubleClick(object sender, EventArgs e)
67         {
68             OpenPithosPath();
69         }
70
71         private void OpenPithosPath()
72         {
73             Process.Start(Settings.PithosPath);
74         }
75
76
77         private Dictionary<PithosStatus, StatusInfo> iconNames =new List<StatusInfo>
78             {
79                 new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"),
80                 new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching")
81             }.ToDictionary(s => s.Status);
82
83         public void UpdateStatus()
84         {
85             var pithosStatus=StatusChecker.GetPithosStatus();
86
87             if (iconNames.ContainsKey(pithosStatus))
88             {
89                 var info= iconNames[pithosStatus];
90
91
92                 notifyIcon.Icon = (Icon) Resources.ResourceManager.GetObject(info.IconName);
93                 notifyIcon.Text = String.Format("Pithos 1.0\r\n{0}", info.StatusText);
94                 notifyIcon.BalloonTipText = info.StatusText;
95                 statusToolStripMenuItem.Text = info.StatusText;
96             }
97             if (!String.IsNullOrWhiteSpace(Settings.UserName )&&
98                 !String.IsNullOrWhiteSpace(Settings.ApiKey))
99                 Monitor.Start();
100             
101         }
102
103         private void btnBrowsePithosPath_Click(object sender, EventArgs e)
104         {
105             pithosFolderBrowser.SelectedPath = txtPithosPath.Text;
106             var result = pithosFolderBrowser.ShowDialog();
107             if (result == DialogResult.OK)
108             {
109                 var newPath = pithosFolderBrowser.SelectedPath;
110                 txtPithosPath.Text = newPath;
111                 Settings.PithosPath = newPath;
112                 Settings.Save();
113             }
114         }
115
116         private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
117         {
118             _allowVisible = true;
119             _loadFired = true;
120             Show();
121         }
122
123         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
124         {
125             _allowClose = _allowVisible = true;
126             Monitor.Stop();
127             if (!_loadFired) 
128                 Show();
129             Close();
130         }
131
132         private void btnCancel_Click(object sender, EventArgs e)
133         {
134             Settings.Reload();
135             Hide();
136         }
137
138         private void btnOK_Click(object sender, EventArgs e)
139         {
140             DoSave();
141             Hide();
142         }
143
144         private void btnApply_Click(object sender, EventArgs e)
145         {
146             DoSave();
147         }
148
149         private void DoSave()
150         {
151             Settings.Save();
152             Monitor.Start();
153         }
154
155         private void pauseSynchingToolStripMenuItem_Click(object sender, EventArgs e)
156         {
157             Monitor.Pause = !Monitor.Pause;
158             pauseSynchingToolStripMenuItem.Text = Monitor.Pause?"Resume Syncing":"Pause Syncing";
159         }
160
161         private void tabAccount_Click(object sender, EventArgs e)
162         {
163
164         }
165
166         private void checkActivateExtensions_CheckedChanged(object sender, EventArgs e)
167         {
168             var box = (CheckBox) sender;
169             if (box.Checked)
170                 RegisterExtensions();
171             else
172                 UnregisterExtensions();
173         }
174
175         private void UnregisterExtensions()
176         {
177             using (var installer = new ProjectInstaller())
178             {
179                 IDictionary state = LoadState();
180                 installer.Uninstall(state);
181             }
182         }
183
184         private void RegisterExtensions()
185         {
186             using (var installer = new ProjectInstaller())
187             {
188                 IDictionary state = new Dictionary<object, object>();                
189                 installer.Install(state);
190                 SaveState(state);
191                 
192             }
193         }
194
195         private static void SaveState(IDictionary state)
196         {
197             using(var store= IsolatedStorageFile.GetUserStoreForApplication())
198             using(var file=store.CreateFile("PithosManualInstallFile"))
199             {
200                 var serializer = new NetDataContractSerializer();
201                 serializer.Serialize(file,state);
202             }
203         }
204
205         private static IDictionary LoadState()
206         {
207             using (var store = IsolatedStorageFile.GetUserStoreForApplication())
208             {
209                 if (!store.FileExists("PithosManualInstallFile"))
210                     return new Dictionary<object, object>();
211
212                 using (var file = store.OpenFile("PithosManualInstallFile", FileMode.Open))
213                 {
214                     var serializer = new NetDataContractSerializer();
215                     var state = serializer.Deserialize(file);
216                     return (IDictionary) state;
217                 }
218             }
219         }
220     }
221 }