root / trunk / Pithos.Client.WPF / Preferences / PreferencesViewModel.cs @ 78ebfd2d
History | View | Annotate | Download (15.5 kB)
1 | 9bae55d1 | Panagiotis Kanavos | // ----------------------------------------------------------------------- |
---|---|---|---|
2 | f3d080df | Panagiotis Kanavos | // <copyright file="PreferencesViewModel.cs" company="GRNet"> |
3 | f3d080df | Panagiotis Kanavos | // Copyright 2011 GRNET S.A. All rights reserved. |
4 | f3d080df | Panagiotis Kanavos | // |
5 | f3d080df | Panagiotis Kanavos | // Redistribution and use in source and binary forms, with or |
6 | f3d080df | Panagiotis Kanavos | // without modification, are permitted provided that the following |
7 | f3d080df | Panagiotis Kanavos | // conditions are met: |
8 | f3d080df | Panagiotis Kanavos | // |
9 | f3d080df | Panagiotis Kanavos | // 1. Redistributions of source code must retain the above |
10 | f3d080df | Panagiotis Kanavos | // copyright notice, this list of conditions and the following |
11 | f3d080df | Panagiotis Kanavos | // disclaimer. |
12 | f3d080df | Panagiotis Kanavos | // |
13 | f3d080df | Panagiotis Kanavos | // 2. Redistributions in binary form must reproduce the above |
14 | f3d080df | Panagiotis Kanavos | // copyright notice, this list of conditions and the following |
15 | f3d080df | Panagiotis Kanavos | // disclaimer in the documentation and/or other materials |
16 | f3d080df | Panagiotis Kanavos | // provided with the distribution. |
17 | f3d080df | Panagiotis Kanavos | // |
18 | f3d080df | Panagiotis Kanavos | // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
19 | f3d080df | Panagiotis Kanavos | // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | f3d080df | Panagiotis Kanavos | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 | f3d080df | Panagiotis Kanavos | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
22 | f3d080df | Panagiotis Kanavos | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | f3d080df | Panagiotis Kanavos | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | f3d080df | Panagiotis Kanavos | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
25 | f3d080df | Panagiotis Kanavos | // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
26 | f3d080df | Panagiotis Kanavos | // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | f3d080df | Panagiotis Kanavos | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
28 | f3d080df | Panagiotis Kanavos | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | f3d080df | Panagiotis Kanavos | // POSSIBILITY OF SUCH DAMAGE. |
30 | f3d080df | Panagiotis Kanavos | // |
31 | f3d080df | Panagiotis Kanavos | // The views and conclusions contained in the software and |
32 | f3d080df | Panagiotis Kanavos | // documentation are those of the authors and should not be |
33 | f3d080df | Panagiotis Kanavos | // interpreted as representing official policies, either expressed |
34 | f3d080df | Panagiotis Kanavos | // or implied, of GRNET S.A. |
35 | f3d080df | Panagiotis Kanavos | |
36 | 9bae55d1 | Panagiotis Kanavos | // </copyright> |
37 | 9bae55d1 | Panagiotis Kanavos | // ----------------------------------------------------------------------- |
38 | 9bae55d1 | Panagiotis Kanavos | |
39 | 7e26c075 | Panagiotis Kanavos | using System.Collections.Concurrent; |
40 | 9bae55d1 | Panagiotis Kanavos | using System.ComponentModel.Composition; |
41 | 9bae55d1 | Panagiotis Kanavos | using System.IO; |
42 | 0eea575a | Panagiotis Kanavos | using System.Reflection; |
43 | 9bae55d1 | Panagiotis Kanavos | using System.Windows; |
44 | 9bae55d1 | Panagiotis Kanavos | using System.Windows.Forms; |
45 | 9bae55d1 | Panagiotis Kanavos | using Caliburn.Micro; |
46 | 7e26c075 | Panagiotis Kanavos | using IWshRuntimeLibrary; |
47 | 9bae55d1 | Panagiotis Kanavos | using Pithos.Client.WPF.Configuration; |
48 | 77f3c897 | Panagiotis Kanavos | using Pithos.Client.WPF.Preferences; |
49 | d3a13891 | Panagiotis Kanavos | using Pithos.Client.WPF.SelectiveSynch; |
50 | 9bae55d1 | Panagiotis Kanavos | using Pithos.Core; |
51 | 9bae55d1 | Panagiotis Kanavos | using Pithos.Interfaces; |
52 | 7e26c075 | Panagiotis Kanavos | using File = System.IO.File; |
53 | 9bae55d1 | Panagiotis Kanavos | using Screen = Caliburn.Micro.Screen; |
54 | 9bae55d1 | Panagiotis Kanavos | |
55 | 9bae55d1 | Panagiotis Kanavos | namespace Pithos.Client.WPF |
56 | 9bae55d1 | Panagiotis Kanavos | { |
57 | 9bae55d1 | Panagiotis Kanavos | using System; |
58 | 9bae55d1 | Panagiotis Kanavos | using System.Linq; |
59 | 0c02aa65 | Panagiotis Kanavos | using System.Threading.Tasks; |
60 | 9bae55d1 | Panagiotis Kanavos | |
61 | 9bae55d1 | Panagiotis Kanavos | /// <summary> |
62 | 9bae55d1 | Panagiotis Kanavos | /// TODO: Update summary. |
63 | 9bae55d1 | Panagiotis Kanavos | /// </summary> |
64 | c53aa229 | Panagiotis Kanavos | [Export] |
65 | c53aa229 | Panagiotis Kanavos | public class PreferencesViewModel : Screen |
66 | 9bae55d1 | Panagiotis Kanavos | { |
67 | 0c02aa65 | Panagiotis Kanavos | private IEventAggregator _events; |
68 | 9bae55d1 | Panagiotis Kanavos | |
69 | 9bae55d1 | Panagiotis Kanavos | |
70 | 5120f3cb | Panagiotis Kanavos | private PithosSettings _settings; |
71 | 5120f3cb | Panagiotis Kanavos | public PithosSettings Settings |
72 | 5120f3cb | Panagiotis Kanavos | { |
73 | 5120f3cb | Panagiotis Kanavos | get { return _settings; } |
74 | 5120f3cb | Panagiotis Kanavos | set |
75 | 5120f3cb | Panagiotis Kanavos | { |
76 | 5120f3cb | Panagiotis Kanavos | _settings = value; |
77 | 5120f3cb | Panagiotis Kanavos | NotifyOfPropertyChange(()=>Settings); |
78 | 5120f3cb | Panagiotis Kanavos | } |
79 | 5120f3cb | Panagiotis Kanavos | } |
80 | 7e26c075 | Panagiotis Kanavos | |
81 | 7e26c075 | Panagiotis Kanavos | private ObservableConcurrentCollection<AccountSettings> _accounts; |
82 | 7e26c075 | Panagiotis Kanavos | public ObservableConcurrentCollection<AccountSettings> Accounts |
83 | 7e26c075 | Panagiotis Kanavos | { |
84 | 7e26c075 | Panagiotis Kanavos | get { return _accounts; } |
85 | 7e26c075 | Panagiotis Kanavos | set |
86 | 7e26c075 | Panagiotis Kanavos | { |
87 | 7e26c075 | Panagiotis Kanavos | _accounts = value; |
88 | 7e26c075 | Panagiotis Kanavos | NotifyOfPropertyChange(()=>Accounts); |
89 | 7e26c075 | Panagiotis Kanavos | } |
90 | 7e26c075 | Panagiotis Kanavos | } |
91 | c53aa229 | Panagiotis Kanavos | |
92 | 7e26c075 | Panagiotis Kanavos | public bool StartOnSystemStartup { get; set; } |
93 | 7e26c075 | Panagiotis Kanavos | |
94 | 7e26c075 | Panagiotis Kanavos | private static void CreateShortcut(string shortcutPath) |
95 | 7e26c075 | Panagiotis Kanavos | { |
96 | 78ebfd2d | Panagiotis Kanavos | //TODO: ###THROWS PERMISSIONS ERROR### |
97 | 7e26c075 | Panagiotis Kanavos | var wshShell = new WshShellClass(); |
98 | 7e26c075 | Panagiotis Kanavos | var shortcut = (IWshRuntimeLibrary.IWshShortcut) wshShell.CreateShortcut( |
99 | 7e26c075 | Panagiotis Kanavos | shortcutPath); |
100 | 7e26c075 | Panagiotis Kanavos | |
101 | 7e26c075 | Panagiotis Kanavos | var exePath = Assembly.GetExecutingAssembly().Location; |
102 | 7e26c075 | Panagiotis Kanavos | shortcut.TargetPath = exePath; |
103 | 7e26c075 | Panagiotis Kanavos | shortcut.WorkingDirectory = Path.GetDirectoryName(exePath); |
104 | 7e26c075 | Panagiotis Kanavos | shortcut.Description = "Pithos"; |
105 | 7e26c075 | Panagiotis Kanavos | shortcut.Save(); |
106 | 7e26c075 | Panagiotis Kanavos | } |
107 | 7e26c075 | Panagiotis Kanavos | |
108 | c53aa229 | Panagiotis Kanavos | public ShellViewModel Shell { get; set; } |
109 | 0c02aa65 | Panagiotis Kanavos | //ShellExtensionController _extensionController=new ShellExtensionController(); |
110 | d3a13891 | Panagiotis Kanavos | |
111 | d3a13891 | Panagiotis Kanavos | public PreferencesViewModel(IWindowManager windowManager, IEventAggregator events, ShellViewModel shell, PithosSettings settings) |
112 | 9bae55d1 | Panagiotis Kanavos | { |
113 | d3a13891 | Panagiotis Kanavos | _windowManager = windowManager; |
114 | 0c02aa65 | Panagiotis Kanavos | _events = events; |
115 | 0c02aa65 | Panagiotis Kanavos | |
116 | 9bae55d1 | Panagiotis Kanavos | DisplayName = "Pithos Preferences"; |
117 | c53aa229 | Panagiotis Kanavos | Shell = shell; |
118 | 5bcf6d70 | Panagiotis Kanavos | |
119 | 7e26c075 | Panagiotis Kanavos | Settings=settings; |
120 | 7e26c075 | Panagiotis Kanavos | Accounts = new ObservableConcurrentCollection<AccountSettings>(); |
121 | f734ab5b | Panagiotis Kanavos | if (settings.Accounts == null) |
122 | f734ab5b | Panagiotis Kanavos | { |
123 | f734ab5b | Panagiotis Kanavos | settings.Accounts=new AccountsCollection(); |
124 | f734ab5b | Panagiotis Kanavos | settings.Save(); |
125 | f734ab5b | Panagiotis Kanavos | } |
126 | 7e26c075 | Panagiotis Kanavos | Accounts.AddFromEnumerable(settings.Accounts); |
127 | 7e26c075 | Panagiotis Kanavos | |
128 | 7e26c075 | Panagiotis Kanavos | var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); |
129 | 7e26c075 | Panagiotis Kanavos | _shortcutPath = Path.Combine(startupPath, "Pithos.lnk"); |
130 | 9bae55d1 | Panagiotis Kanavos | |
131 | 9bae55d1 | Panagiotis Kanavos | |
132 | 7e26c075 | Panagiotis Kanavos | StartOnSystemStartup = File.Exists(_shortcutPath); |
133 | 7e26c075 | Panagiotis Kanavos | |
134 | 9bae55d1 | Panagiotis Kanavos | } |
135 | 0eea575a | Panagiotis Kanavos | |
136 | 9bae55d1 | Panagiotis Kanavos | |
137 | 5bcf6d70 | Panagiotis Kanavos | #region Preferences Properties |
138 | 9bae55d1 | Panagiotis Kanavos | |
139 | 5bcf6d70 | Panagiotis Kanavos | private bool _noProxy; |
140 | 5bcf6d70 | Panagiotis Kanavos | public bool NoProxy |
141 | 9bae55d1 | Panagiotis Kanavos | { |
142 | 5bcf6d70 | Panagiotis Kanavos | get { return _noProxy; } |
143 | 9bae55d1 | Panagiotis Kanavos | set |
144 | 9bae55d1 | Panagiotis Kanavos | { |
145 | 5bcf6d70 | Panagiotis Kanavos | _noProxy = value; |
146 | 5bcf6d70 | Panagiotis Kanavos | NotifyOfPropertyChange(()=>NoProxy); |
147 | 9bae55d1 | Panagiotis Kanavos | } |
148 | 9bae55d1 | Panagiotis Kanavos | } |
149 | 9bae55d1 | Panagiotis Kanavos | |
150 | 5bcf6d70 | Panagiotis Kanavos | |
151 | 5bcf6d70 | Panagiotis Kanavos | private bool _defaultProxy; |
152 | 5bcf6d70 | Panagiotis Kanavos | |
153 | 5bcf6d70 | Panagiotis Kanavos | public bool DefaultProxy |
154 | 9bae55d1 | Panagiotis Kanavos | { |
155 | 5bcf6d70 | Panagiotis Kanavos | get { return _defaultProxy; } |
156 | 9bae55d1 | Panagiotis Kanavos | set |
157 | 9bae55d1 | Panagiotis Kanavos | { |
158 | 5bcf6d70 | Panagiotis Kanavos | _defaultProxy = value; |
159 | 5bcf6d70 | Panagiotis Kanavos | NotifyOfPropertyChange(() => DefaultProxy); |
160 | 9bae55d1 | Panagiotis Kanavos | } |
161 | 9bae55d1 | Panagiotis Kanavos | } |
162 | 9bae55d1 | Panagiotis Kanavos | |
163 | 9bae55d1 | Panagiotis Kanavos | |
164 | 5bcf6d70 | Panagiotis Kanavos | private bool _manualProxy; |
165 | 9bae55d1 | Panagiotis Kanavos | |
166 | 5bcf6d70 | Panagiotis Kanavos | public bool ManualProxy |
167 | 9bae55d1 | Panagiotis Kanavos | { |
168 | 5bcf6d70 | Panagiotis Kanavos | get { return _manualProxy; } |
169 | 9bae55d1 | Panagiotis Kanavos | set |
170 | 9bae55d1 | Panagiotis Kanavos | { |
171 | 5bcf6d70 | Panagiotis Kanavos | _manualProxy = value; |
172 | 5bcf6d70 | Panagiotis Kanavos | NotifyOfPropertyChange(() => ManualProxy); |
173 | 9bae55d1 | Panagiotis Kanavos | } |
174 | 9bae55d1 | Panagiotis Kanavos | } |
175 | 9bae55d1 | Panagiotis Kanavos | #endregion |
176 | 9bae55d1 | Panagiotis Kanavos | |
177 | 5bcf6d70 | Panagiotis Kanavos | |
178 | 9bae55d1 | Panagiotis Kanavos | #region Commands |
179 | 9bae55d1 | Panagiotis Kanavos | |
180 | aba9e6d9 | Panagiotis Kanavos | public bool CanSelectiveSyncFolders |
181 | aba9e6d9 | Panagiotis Kanavos | { |
182 | aba9e6d9 | Panagiotis Kanavos | get { return CurrentAccount != null; } |
183 | aba9e6d9 | Panagiotis Kanavos | } |
184 | aba9e6d9 | Panagiotis Kanavos | |
185 | d3a13891 | Panagiotis Kanavos | public void SelectiveSyncFolders() |
186 | d3a13891 | Panagiotis Kanavos | { |
187 | 7e26c075 | Panagiotis Kanavos | var monitor = Shell.Monitors[CurrentAccount.AccountName]; |
188 | 7e26c075 | Panagiotis Kanavos | var folders=monitor.GetRootFolders(); |
189 | 7e26c075 | Panagiotis Kanavos | |
190 | 7e26c075 | Panagiotis Kanavos | var model = new SelectiveSynchViewModel(folders,_events,CurrentAccount); |
191 | d3a13891 | Panagiotis Kanavos | if (_windowManager.ShowDialog(model) == true) |
192 | d3a13891 | Panagiotis Kanavos | { |
193 | d3a13891 | Panagiotis Kanavos | |
194 | d3a13891 | Panagiotis Kanavos | } |
195 | d3a13891 | Panagiotis Kanavos | } |
196 | 0eea575a | Panagiotis Kanavos | |
197 | 9bae55d1 | Panagiotis Kanavos | public void SaveChanges() |
198 | 9bae55d1 | Panagiotis Kanavos | { |
199 | 9bae55d1 | Panagiotis Kanavos | DoSave(); |
200 | c53aa229 | Panagiotis Kanavos | TryClose(true); |
201 | 9bae55d1 | Panagiotis Kanavos | } |
202 | 9bae55d1 | Panagiotis Kanavos | |
203 | 9bae55d1 | Panagiotis Kanavos | public void RejectChanges() |
204 | 9bae55d1 | Panagiotis Kanavos | { |
205 | 9bae55d1 | Panagiotis Kanavos | Settings.Reload(); |
206 | c53aa229 | Panagiotis Kanavos | TryClose(false); |
207 | 9bae55d1 | Panagiotis Kanavos | } |
208 | 9bae55d1 | Panagiotis Kanavos | |
209 | 9bae55d1 | Panagiotis Kanavos | public void ApplyChanges() |
210 | 9bae55d1 | Panagiotis Kanavos | { |
211 | 9bae55d1 | Panagiotis Kanavos | DoSave(); |
212 | 9bae55d1 | Panagiotis Kanavos | } |
213 | 9bae55d1 | Panagiotis Kanavos | |
214 | 9bae55d1 | Panagiotis Kanavos | private void DoSave() |
215 | 9bae55d1 | Panagiotis Kanavos | { |
216 | 9bae55d1 | Panagiotis Kanavos | Settings.Save(); |
217 | 7e26c075 | Panagiotis Kanavos | SetStartupMode(); |
218 | 7e26c075 | Panagiotis Kanavos | |
219 | 7e26c075 | Panagiotis Kanavos | |
220 | c53aa229 | Panagiotis Kanavos | foreach (var account in Settings.Accounts) |
221 | 7e26c075 | Panagiotis Kanavos | { |
222 | c53aa229 | Panagiotis Kanavos | Shell.MonitorAccount(account); |
223 | c53aa229 | Panagiotis Kanavos | } |
224 | 153975d0 | Panagiotis Kanavos | |
225 | c53aa229 | Panagiotis Kanavos | NotifyOfPropertyChange(()=>Settings); |
226 | 9bae55d1 | Panagiotis Kanavos | } |
227 | 9bae55d1 | Panagiotis Kanavos | |
228 | 7e26c075 | Panagiotis Kanavos | private void SetStartupMode() |
229 | 7e26c075 | Panagiotis Kanavos | { |
230 | 7e26c075 | Panagiotis Kanavos | if (StartOnSystemStartup && !File.Exists(_shortcutPath)) |
231 | 7e26c075 | Panagiotis Kanavos | { |
232 | 7e26c075 | Panagiotis Kanavos | CreateShortcut(_shortcutPath); |
233 | 7e26c075 | Panagiotis Kanavos | } |
234 | 7e26c075 | Panagiotis Kanavos | else if (!StartOnSystemStartup && File.Exists(_shortcutPath)) |
235 | 7e26c075 | Panagiotis Kanavos | { |
236 | 7e26c075 | Panagiotis Kanavos | if (File.Exists(_shortcutPath)) |
237 | 7e26c075 | Panagiotis Kanavos | File.Delete(_shortcutPath); |
238 | 7e26c075 | Panagiotis Kanavos | } |
239 | 7e26c075 | Panagiotis Kanavos | } |
240 | 7e26c075 | Panagiotis Kanavos | |
241 | 7e26c075 | Panagiotis Kanavos | /* public void ChangePithosFolder() |
242 | 9bae55d1 | Panagiotis Kanavos | { |
243 | 9bae55d1 | Panagiotis Kanavos | var browser = new FolderBrowserDialog(); |
244 | 9bae55d1 | Panagiotis Kanavos | browser.SelectedPath = Settings.PithosPath; |
245 | 9bae55d1 | Panagiotis Kanavos | var result = browser.ShowDialog((IWin32Window)GetView()); |
246 | 9bae55d1 | Panagiotis Kanavos | if (result == DialogResult.OK) |
247 | 9bae55d1 | Panagiotis Kanavos | { |
248 | 9bae55d1 | Panagiotis Kanavos | var newPath = browser.SelectedPath; |
249 | 7e26c075 | Panagiotis Kanavos | var accountName = CurrentAccount.AccountName; |
250 | 7e26c075 | Panagiotis Kanavos | var monitor = Shell.Monitors[accountName]; |
251 | 7e26c075 | Panagiotis Kanavos | monitor.Stop(); |
252 | 7e26c075 | Panagiotis Kanavos | |
253 | 7e26c075 | Panagiotis Kanavos | Shell.Monitors.Remove(accountName); |
254 | 7e26c075 | Panagiotis Kanavos | |
255 | 9bae55d1 | Panagiotis Kanavos | Directory.Move(Settings.PithosPath, newPath); |
256 | 9bae55d1 | Panagiotis Kanavos | Settings.PithosPath = newPath; |
257 | 9bae55d1 | Panagiotis Kanavos | Settings.Save(); |
258 | 0eea575a | Panagiotis Kanavos | |
259 | 7e26c075 | Panagiotis Kanavos | Shell.MonitorAccount(CurrentAccount); |
260 | 7e26c075 | Panagiotis Kanavos | |
261 | 7e26c075 | Panagiotis Kanavos | NotifyOfPropertyChange(() => Settings); |
262 | 9bae55d1 | Panagiotis Kanavos | } |
263 | 9bae55d1 | Panagiotis Kanavos | } |
264 | 7e26c075 | Panagiotis Kanavos | */ |
265 | 0eea575a | Panagiotis Kanavos | public void AddAccount() |
266 | 77f3c897 | Panagiotis Kanavos | { |
267 | 77f3c897 | Panagiotis Kanavos | var wizard = new AddAccountViewModel(); |
268 | 77f3c897 | Panagiotis Kanavos | if (_windowManager.ShowDialog(wizard) == true) |
269 | 77f3c897 | Panagiotis Kanavos | { |
270 | 77f3c897 | Panagiotis Kanavos | var newAccount = new AccountSettings |
271 | 77f3c897 | Panagiotis Kanavos | { |
272 | 77f3c897 | Panagiotis Kanavos | AccountName = wizard.AccountName, |
273 | 74d78c90 | Panagiotis Kanavos | ServerUrl=wizard.CurrentServer, |
274 | 77f3c897 | Panagiotis Kanavos | ApiKey=wizard.Token, |
275 | 77f3c897 | Panagiotis Kanavos | RootPath=wizard.AccountPath, |
276 | 74d78c90 | Panagiotis Kanavos | IsActive=wizard.IsAccountActive |
277 | 77f3c897 | Panagiotis Kanavos | }; |
278 | 77f3c897 | Panagiotis Kanavos | Settings.Accounts.Add(newAccount); |
279 | 77f3c897 | Panagiotis Kanavos | (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(newAccount); |
280 | 77f3c897 | Panagiotis Kanavos | CurrentAccount = newAccount; |
281 | 77f3c897 | Panagiotis Kanavos | NotifyOfPropertyChange(() => Accounts); |
282 | 77f3c897 | Panagiotis Kanavos | NotifyOfPropertyChange(() => Settings); |
283 | 77f3c897 | Panagiotis Kanavos | } |
284 | 77f3c897 | Panagiotis Kanavos | |
285 | 77f3c897 | Panagiotis Kanavos | |
286 | 77f3c897 | Panagiotis Kanavos | |
287 | 77f3c897 | Panagiotis Kanavos | } |
288 | 9bae55d1 | Panagiotis Kanavos | |
289 | 437abfca | Panagiotis Kanavos | public async void AddPithosAccount() |
290 | 0c02aa65 | Panagiotis Kanavos | { |
291 | 03ee454f | Panagiotis Kanavos | var credentials=await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl); |
292 | 437abfca | Panagiotis Kanavos | var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName); |
293 | 437abfca | Panagiotis Kanavos | if (account == null) |
294 | 437abfca | Panagiotis Kanavos | { |
295 | 437abfca | Panagiotis Kanavos | account=new AccountSettings{ |
296 | 437abfca | Panagiotis Kanavos | AccountName=credentials.UserName, |
297 | 74d78c90 | Panagiotis Kanavos | ApiKey=credentials.Password |
298 | 437abfca | Panagiotis Kanavos | }; |
299 | 437abfca | Panagiotis Kanavos | Settings.Accounts.Add(account); |
300 | 437abfca | Panagiotis Kanavos | (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(account); |
301 | 437abfca | Panagiotis Kanavos | } |
302 | 437abfca | Panagiotis Kanavos | else |
303 | 437abfca | Panagiotis Kanavos | { |
304 | 437abfca | Panagiotis Kanavos | account.ApiKey=credentials.Password; |
305 | 437abfca | Panagiotis Kanavos | } |
306 | 437abfca | Panagiotis Kanavos | //SelectedAccountIndex= Settings.Accounts.IndexOf(account); |
307 | 437abfca | Panagiotis Kanavos | CurrentAccount = account; |
308 | 437abfca | Panagiotis Kanavos | NotifyOfPropertyChange(() => Accounts); |
309 | 437abfca | Panagiotis Kanavos | NotifyOfPropertyChange(()=>Settings); |
310 | 0c02aa65 | Panagiotis Kanavos | } |
311 | 0c02aa65 | Panagiotis Kanavos | |
312 | 5bcf6d70 | Panagiotis Kanavos | public void RemoveAccount() |
313 | 5bcf6d70 | Panagiotis Kanavos | { |
314 | c53aa229 | Panagiotis Kanavos | var accountName = CurrentAccount.AccountName; |
315 | 7e26c075 | Panagiotis Kanavos | Settings.Accounts.Remove(CurrentAccount); |
316 | 7e26c075 | Panagiotis Kanavos | |
317 | 7e26c075 | Panagiotis Kanavos | Accounts.TryRemove(CurrentAccount); |
318 | 7e26c075 | Panagiotis Kanavos | |
319 | 7e26c075 | Panagiotis Kanavos | |
320 | 7e26c075 | Panagiotis Kanavos | CurrentAccount = null; |
321 | 7e26c075 | Panagiotis Kanavos | //Accounts = Settings.Accounts; |
322 | 7e26c075 | Panagiotis Kanavos | //Settings.Save(); |
323 | c53aa229 | Panagiotis Kanavos | Shell.RemoveMonitor(accountName); |
324 | 6aa29f4f | Panagiotis Kanavos | NotifyOfPropertyChange(() => Accounts); |
325 | 6aa29f4f | Panagiotis Kanavos | NotifyOfPropertyChange(() => Settings); |
326 | 7e26c075 | Panagiotis Kanavos | |
327 | 0eea575a | Panagiotis Kanavos | //NotifyOfPropertyChange("Settings.Accounts"); |
328 | 5bcf6d70 | Panagiotis Kanavos | } |
329 | 9bae55d1 | Panagiotis Kanavos | |
330 | 5bcf6d70 | Panagiotis Kanavos | public bool CanRemoveAccount |
331 | 9bae55d1 | Panagiotis Kanavos | { |
332 | 5bcf6d70 | Panagiotis Kanavos | get { return (CurrentAccount != null); } |
333 | 5bcf6d70 | Panagiotis Kanavos | } |
334 | 9bae55d1 | Panagiotis Kanavos | |
335 | 9bae55d1 | Panagiotis Kanavos | |
336 | 9bae55d1 | Panagiotis Kanavos | |
337 | 9bae55d1 | Panagiotis Kanavos | public bool ExtensionsActivated |
338 | 9bae55d1 | Panagiotis Kanavos | { |
339 | 9bae55d1 | Panagiotis Kanavos | get { return Settings.ExtensionsActivated; } |
340 | 9bae55d1 | Panagiotis Kanavos | set |
341 | 9bae55d1 | Panagiotis Kanavos | { |
342 | 9bae55d1 | Panagiotis Kanavos | if (Settings.ExtensionsActivated == value) |
343 | 5bcf6d70 | Panagiotis Kanavos | return; |
344 | 5bcf6d70 | Panagiotis Kanavos | |
345 | 9bae55d1 | Panagiotis Kanavos | Settings.ExtensionsActivated = value; |
346 | 9bae55d1 | Panagiotis Kanavos | |
347 | aba9e6d9 | Panagiotis Kanavos | /* |
348 | aba9e6d9 | Panagiotis Kanavos | if (value) |
349 | aba9e6d9 | Panagiotis Kanavos | _extensionController.RegisterExtensions(); |
350 | aba9e6d9 | Panagiotis Kanavos | else |
351 | aba9e6d9 | Panagiotis Kanavos | { |
352 | aba9e6d9 | Panagiotis Kanavos | _extensionController.UnregisterExtensions(); |
353 | aba9e6d9 | Panagiotis Kanavos | } |
354 | aba9e6d9 | Panagiotis Kanavos | */ |
355 | 5bcf6d70 | Panagiotis Kanavos | NotifyOfPropertyChange(() => ExtensionsActivated); |
356 | 9bae55d1 | Panagiotis Kanavos | } |
357 | 9bae55d1 | Panagiotis Kanavos | } |
358 | 9bae55d1 | Panagiotis Kanavos | |
359 | c53aa229 | Panagiotis Kanavos | |
360 | 5bcf6d70 | Panagiotis Kanavos | #endregion |
361 | 5bcf6d70 | Panagiotis Kanavos | |
362 | 7e26c075 | Panagiotis Kanavos | /* private int _selectedAccountIndex; |
363 | 0eea575a | Panagiotis Kanavos | public int SelectedAccountIndex |
364 | 5bcf6d70 | Panagiotis Kanavos | { |
365 | 0eea575a | Panagiotis Kanavos | get { return _selectedAccountIndex; } |
366 | 5bcf6d70 | Panagiotis Kanavos | set |
367 | 5bcf6d70 | Panagiotis Kanavos | { |
368 | 153975d0 | Panagiotis Kanavos | //var accountCount=Settings.Accounts.Count; |
369 | 153975d0 | Panagiotis Kanavos | //if (accountCount == 0) |
370 | 153975d0 | Panagiotis Kanavos | // return; |
371 | 153975d0 | Panagiotis Kanavos | //if (0 <= value && value < accountCount) |
372 | 153975d0 | Panagiotis Kanavos | // _selectedAccountIndex = value; |
373 | 153975d0 | Panagiotis Kanavos | //else |
374 | 153975d0 | Panagiotis Kanavos | // _selectedAccountIndex = 0; |
375 | 0c02aa65 | Panagiotis Kanavos | _selectedAccountIndex = value; |
376 | 0eea575a | Panagiotis Kanavos | NotifyOfPropertyChange(() => CurrentAccount); |
377 | 0eea575a | Panagiotis Kanavos | NotifyOfPropertyChange(() => CanRemoveAccount); |
378 | 0eea575a | Panagiotis Kanavos | NotifyOfPropertyChange(()=>SelectedAccountIndex); |
379 | 5bcf6d70 | Panagiotis Kanavos | } |
380 | 7e26c075 | Panagiotis Kanavos | }*/ |
381 | 5bcf6d70 | Panagiotis Kanavos | |
382 | 0eea575a | Panagiotis Kanavos | private AccountSettings _currentAccount; |
383 | d3a13891 | Panagiotis Kanavos | private IWindowManager _windowManager; |
384 | 7e26c075 | Panagiotis Kanavos | private string _shortcutPath; |
385 | 7e26c075 | Panagiotis Kanavos | |
386 | d3a13891 | Panagiotis Kanavos | |
387 | 7e26c075 | Panagiotis Kanavos | |
388 | 7e26c075 | Panagiotis Kanavos | public AccountSettings CurrentAccount |
389 | 7e26c075 | Panagiotis Kanavos | { |
390 | 7e26c075 | Panagiotis Kanavos | get { return _currentAccount; } |
391 | 7e26c075 | Panagiotis Kanavos | set |
392 | 7e26c075 | Panagiotis Kanavos | { |
393 | 7e26c075 | Panagiotis Kanavos | _currentAccount = value; |
394 | 7e26c075 | Panagiotis Kanavos | NotifyOfPropertyChange(()=>CurrentAccount); |
395 | 7e26c075 | Panagiotis Kanavos | NotifyOfPropertyChange(() => CanRemoveAccount); |
396 | aba9e6d9 | Panagiotis Kanavos | NotifyOfPropertyChange(() => CanSelectiveSyncFolders); |
397 | aba9e6d9 | Panagiotis Kanavos | NotifyOfPropertyChange(() => CanMoveAccountFolder); |
398 | 7e26c075 | Panagiotis Kanavos | } |
399 | 7e26c075 | Panagiotis Kanavos | } |
400 | 7e26c075 | Panagiotis Kanavos | |
401 | 7e26c075 | Panagiotis Kanavos | /* |
402 | 0eea575a | Panagiotis Kanavos | public AccountSettings CurrentAccount |
403 | 9bae55d1 | Panagiotis Kanavos | { |
404 | 153975d0 | Panagiotis Kanavos | get { |
405 | 153975d0 | Panagiotis Kanavos | if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count) |
406 | 153975d0 | Panagiotis Kanavos | return Settings.Accounts[SelectedAccountIndex]; |
407 | 153975d0 | Panagiotis Kanavos | return null; |
408 | 153975d0 | Panagiotis Kanavos | } |
409 | 5120f3cb | Panagiotis Kanavos | |
410 | 9bae55d1 | Panagiotis Kanavos | } |
411 | 7e26c075 | Panagiotis Kanavos | */ |
412 | 9bae55d1 | Panagiotis Kanavos | |
413 | 9bae55d1 | Panagiotis Kanavos | |
414 | aba9e6d9 | Panagiotis Kanavos | public bool CanMoveAccountFolder |
415 | aba9e6d9 | Panagiotis Kanavos | { |
416 | aba9e6d9 | Panagiotis Kanavos | get { return CurrentAccount != null; } |
417 | aba9e6d9 | Panagiotis Kanavos | } |
418 | 5120f3cb | Panagiotis Kanavos | |
419 | 5120f3cb | Panagiotis Kanavos | public void MoveAccountFolder() |
420 | 5120f3cb | Panagiotis Kanavos | { |
421 | 5120f3cb | Panagiotis Kanavos | |
422 | 5120f3cb | Panagiotis Kanavos | using (var dlg = new FolderBrowserDialog()) |
423 | 5120f3cb | Panagiotis Kanavos | { |
424 | 5120f3cb | Panagiotis Kanavos | var currentFolder = CurrentAccount.RootPath; |
425 | 5120f3cb | Panagiotis Kanavos | dlg.SelectedPath = currentFolder; |
426 | 5120f3cb | Panagiotis Kanavos | //Ask the user to select a folder |
427 | 5120f3cb | Panagiotis Kanavos | //Note: We need a parent window here, which we retrieve with GetView |
428 | 5120f3cb | Panagiotis Kanavos | var view = (Window)GetView(); |
429 | 5120f3cb | Panagiotis Kanavos | if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view))) |
430 | 5120f3cb | Panagiotis Kanavos | return; |
431 | 5120f3cb | Panagiotis Kanavos | |
432 | 5120f3cb | Panagiotis Kanavos | var newPath= dlg.SelectedPath; |
433 | 5120f3cb | Panagiotis Kanavos | //Find the account's monitor and stop it |
434 | 7e26c075 | Panagiotis Kanavos | PithosMonitor monitor; |
435 | 7e26c075 | Panagiotis Kanavos | if (Shell.Monitors.TryGetValue(CurrentAccount.AccountName, out monitor)) |
436 | 5120f3cb | Panagiotis Kanavos | { |
437 | 7e26c075 | Panagiotis Kanavos | monitor.Stop(); |
438 | 7e26c075 | Panagiotis Kanavos | |
439 | 7e26c075 | Panagiotis Kanavos | |
440 | 7e26c075 | Panagiotis Kanavos | var oldPath = monitor.RootPath; |
441 | 7e26c075 | Panagiotis Kanavos | //The old directory may not exist eg. if we create an account for the first time |
442 | 7e26c075 | Panagiotis Kanavos | if (Directory.Exists(oldPath)) |
443 | 7e26c075 | Panagiotis Kanavos | { |
444 | 7e26c075 | Panagiotis Kanavos | //If it does, do the move |
445 | 5a99309c | Panagiotis Kanavos | |
446 | 5a99309c | Panagiotis Kanavos | //Now Create all of the directories |
447 | 5a99309c | Panagiotis Kanavos | foreach (string dirPath in Directory.EnumerateDirectories(oldPath, "*", |
448 | 5a99309c | Panagiotis Kanavos | SearchOption.AllDirectories)) |
449 | 5a99309c | Panagiotis Kanavos | Directory.CreateDirectory(dirPath.Replace(oldPath, newPath)); |
450 | 5a99309c | Panagiotis Kanavos | |
451 | 5a99309c | Panagiotis Kanavos | //Copy all the files |
452 | 5a99309c | Panagiotis Kanavos | foreach (string newFilePath in Directory.EnumerateFiles(oldPath, "*.*", |
453 | 5a99309c | Panagiotis Kanavos | SearchOption.AllDirectories)) |
454 | 5a99309c | Panagiotis Kanavos | File.Copy(newFilePath, newFilePath.Replace(oldPath, newPath)); |
455 | 5a99309c | Panagiotis Kanavos | |
456 | 5a99309c | Panagiotis Kanavos | Directory.Delete(oldPath, true); |
457 | 5a99309c | Panagiotis Kanavos | |
458 | 7e26c075 | Panagiotis Kanavos | //We also need to change the path of the existing file states |
459 | 7e26c075 | Panagiotis Kanavos | if (monitor != null) |
460 | 7e26c075 | Panagiotis Kanavos | monitor.MoveFileStates(oldPath, newPath); |
461 | 7e26c075 | Panagiotis Kanavos | } |
462 | 5120f3cb | Panagiotis Kanavos | } |
463 | 5120f3cb | Panagiotis Kanavos | //Replace the old rootpath with the new |
464 | 5120f3cb | Panagiotis Kanavos | CurrentAccount.RootPath = newPath; |
465 | 5120f3cb | Panagiotis Kanavos | //TODO: This will save all settings changes. Too coarse grained, need to fix at a later date |
466 | 5120f3cb | Panagiotis Kanavos | Settings.Save(); |
467 | 5120f3cb | Panagiotis Kanavos | //And start the monitor on the new RootPath |
468 | 7e26c075 | Panagiotis Kanavos | if (monitor != null) |
469 | 7e26c075 | Panagiotis Kanavos | { |
470 | 7e26c075 | Panagiotis Kanavos | monitor.RootPath = newPath; |
471 | 7e26c075 | Panagiotis Kanavos | if (CurrentAccount.IsActive) |
472 | 7e26c075 | Panagiotis Kanavos | monitor.Start(); |
473 | 7e26c075 | Panagiotis Kanavos | } |
474 | 7e26c075 | Panagiotis Kanavos | else |
475 | 7e26c075 | Panagiotis Kanavos | Shell.MonitorAccount(CurrentAccount); |
476 | 5120f3cb | Panagiotis Kanavos | //Finally, notify that the Settings, CurrentAccount have changed |
477 | 5120f3cb | Panagiotis Kanavos | NotifyOfPropertyChange(() => CurrentAccount); |
478 | 5120f3cb | Panagiotis Kanavos | NotifyOfPropertyChange(() => Settings); |
479 | 5120f3cb | Panagiotis Kanavos | |
480 | 5120f3cb | Panagiotis Kanavos | } |
481 | 5120f3cb | Panagiotis Kanavos | } |
482 | 9bae55d1 | Panagiotis Kanavos | } |
483 | 9bae55d1 | Panagiotis Kanavos | } |