root / trunk / Pithos.Client.WPF / Shell / ShellViewModel.cs @ f734ab5b
History | View | Annotate | Download (26.8 kB)
1 | c53aa229 | Panagiotis Kanavos | using System.Collections.Concurrent; |
---|---|---|---|
2 | 42800be8 | Panagiotis Kanavos | using System.ComponentModel; |
3 | c53aa229 | Panagiotis Kanavos | using System.ComponentModel.Composition; |
4 | c53aa229 | Panagiotis Kanavos | using System.Diagnostics; |
5 | c28a075a | Panagiotis Kanavos | using System.Diagnostics.Contracts; |
6 | c53aa229 | Panagiotis Kanavos | using System.IO; |
7 | c28a075a | Panagiotis Kanavos | using System.Net; |
8 | d17258c2 | Panagiotis Kanavos | using System.Reflection; |
9 | c53aa229 | Panagiotis Kanavos | using System.Runtime.InteropServices; |
10 | c53aa229 | Panagiotis Kanavos | using System.ServiceModel; |
11 | c53aa229 | Panagiotis Kanavos | using System.ServiceModel.Description; |
12 | c53aa229 | Panagiotis Kanavos | using System.Threading.Tasks; |
13 | c53aa229 | Panagiotis Kanavos | using System.Windows; |
14 | 9bae55d1 | Panagiotis Kanavos | using Caliburn.Micro; |
15 | c53aa229 | Panagiotis Kanavos | using Hardcodet.Wpf.TaskbarNotification; |
16 | c53aa229 | Panagiotis Kanavos | using Pithos.Client.WPF.Configuration; |
17 | 42800be8 | Panagiotis Kanavos | using Pithos.Client.WPF.FileProperties; |
18 | c53aa229 | Panagiotis Kanavos | using Pithos.Client.WPF.Properties; |
19 | d3a13891 | Panagiotis Kanavos | using Pithos.Client.WPF.SelectiveSynch; |
20 | 42800be8 | Panagiotis Kanavos | using Pithos.Client.WPF.Services; |
21 | 5cb9d74f | Panagiotis Kanavos | using Pithos.Client.WPF.Shell; |
22 | c53aa229 | Panagiotis Kanavos | using Pithos.Core; |
23 | c53aa229 | Panagiotis Kanavos | using Pithos.Interfaces; |
24 | c53aa229 | Panagiotis Kanavos | using System; |
25 | c53aa229 | Panagiotis Kanavos | using System.Collections.Generic; |
26 | c53aa229 | Panagiotis Kanavos | using System.Linq; |
27 | c53aa229 | Panagiotis Kanavos | using System.Text; |
28 | 0bd56b7c | Panagiotis Kanavos | using Pithos.Network; |
29 | c53aa229 | Panagiotis Kanavos | using StatusService = Pithos.Client.WPF.Services.StatusService; |
30 | 9bae55d1 | Panagiotis Kanavos | |
31 | 9bae55d1 | Panagiotis Kanavos | namespace Pithos.Client.WPF { |
32 | 9bae55d1 | Panagiotis Kanavos | using System.ComponentModel.Composition; |
33 | 9bae55d1 | Panagiotis Kanavos | |
34 | 3ddbb7b8 | Panagiotis Kanavos | |
35 | cf761c0d | Panagiotis Kanavos | ///<summary> |
36 | cf761c0d | Panagiotis Kanavos | /// The "shell" of the Pithos application displays the taskbar icon, menu and notifications. |
37 | cf761c0d | Panagiotis Kanavos | /// The shell also hosts the status service called by shell extensions to retrieve file info |
38 | cf761c0d | Panagiotis Kanavos | ///</summary> |
39 | cf761c0d | Panagiotis Kanavos | ///<remarks> |
40 | cf761c0d | Panagiotis Kanavos | /// It is a strange "shell" as its main visible element is an icon instead of a window |
41 | cf761c0d | Panagiotis Kanavos | /// The shell subscribes to the following events: |
42 | cf761c0d | Panagiotis Kanavos | /// * Notification: Raised by components that want to notify the user. Usually displayed in a balloon |
43 | cf761c0d | Panagiotis Kanavos | /// * SelectiveSynchChanges: Notifies that the user made changes to the selective synch folders for an account. Raised by the Selective Synch dialog. Located here because the monitors are here |
44 | cf761c0d | Panagiotis Kanavos | /// * ShowFilePropertiesEvent: Raised when a shell command requests the display of the file/container properties dialog |
45 | cf761c0d | Panagiotis Kanavos | ///</remarks> |
46 | cf761c0d | Panagiotis Kanavos | //TODO: CODE SMELL Why does the shell handle the SelectiveSynchChanges? |
47 | 3ddbb7b8 | Panagiotis Kanavos | [Export(typeof(IShell))] |
48 | 42800be8 | Panagiotis Kanavos | public class ShellViewModel : Screen, IStatusNotification, IShell, |
49 | 42800be8 | Panagiotis Kanavos | IHandle<Notification>, IHandle<SelectiveSynchChanges>, IHandle<ShowFilePropertiesEvent> |
50 | 9bae55d1 | Panagiotis Kanavos | { |
51 | cf761c0d | Panagiotis Kanavos | //The Status Checker provides the current synch state |
52 | cf761c0d | Panagiotis Kanavos | //TODO: Could we remove the status checker and use events in its place? |
53 | c53aa229 | Panagiotis Kanavos | private IStatusChecker _statusChecker; |
54 | c53aa229 | Panagiotis Kanavos | private IEventAggregator _events; |
55 | c53aa229 | Panagiotis Kanavos | |
56 | b5ddf130 | Panagiotis Kanavos | public PithosSettings Settings { get; private set; } |
57 | c53aa229 | Panagiotis Kanavos | |
58 | cf761c0d | Panagiotis Kanavos | |
59 | c53aa229 | Panagiotis Kanavos | private Dictionary<string, PithosMonitor> _monitors = new Dictionary<string, PithosMonitor>(); |
60 | cf761c0d | Panagiotis Kanavos | ///<summary> |
61 | cf761c0d | Panagiotis Kanavos | /// Dictionary of account monitors, keyed by account |
62 | cf761c0d | Panagiotis Kanavos | ///</summary> |
63 | cf761c0d | Panagiotis Kanavos | ///<remarks> |
64 | cf761c0d | Panagiotis Kanavos | /// One monitor class is created for each account. The Shell needs access to the monitors to execute start/stop/pause commands, |
65 | cf761c0d | Panagiotis Kanavos | /// retrieve account and boject info |
66 | cf761c0d | Panagiotis Kanavos | ///</remarks> |
67 | cf761c0d | Panagiotis Kanavos | // TODO: Does the Shell REALLY need access to the monitors? Could we achieve the same results with a better design? |
68 | cf761c0d | Panagiotis Kanavos | // TODO: The monitors should be internal to Pithos.Core, even though exposing them makes coding of the Object and Container windows easier |
69 | c53aa229 | Panagiotis Kanavos | public Dictionary<string, PithosMonitor> Monitors |
70 | 9bae55d1 | Panagiotis Kanavos | { |
71 | c53aa229 | Panagiotis Kanavos | get { return _monitors; } |
72 | c53aa229 | Panagiotis Kanavos | } |
73 | c53aa229 | Panagiotis Kanavos | |
74 | cf761c0d | Panagiotis Kanavos | |
75 | cf761c0d | Panagiotis Kanavos | ///<summary> |
76 | cf761c0d | Panagiotis Kanavos | /// The status service is used by Shell extensions to retrieve file status information |
77 | cf761c0d | Panagiotis Kanavos | ///</summary> |
78 | cf761c0d | Panagiotis Kanavos | //TODO: CODE SMELL! This is the shell! While hosting in the shell makes executing start/stop commands easier, it is still a smell |
79 | c53aa229 | Panagiotis Kanavos | private ServiceHost _statusService { get; set; } |
80 | c53aa229 | Panagiotis Kanavos | |
81 | cf761c0d | Panagiotis Kanavos | //Logging in the Pithos client is provided by log4net |
82 | c53aa229 | Panagiotis Kanavos | private static readonly log4net.ILog Log = log4net.LogManager.GetLogger("Pithos"); |
83 | c53aa229 | Panagiotis Kanavos | |
84 | cf761c0d | Panagiotis Kanavos | ///<summary> |
85 | cf761c0d | Panagiotis Kanavos | /// The Shell depends on MEF to provide implementations for windowManager, events, the status checker service and the settings |
86 | cf761c0d | Panagiotis Kanavos | ///</summary> |
87 | cf761c0d | Panagiotis Kanavos | ///<remarks> |
88 | cf761c0d | Panagiotis Kanavos | /// The PithosSettings class encapsulates the app's settings to abstract their storage mechanism (App settings, a database or registry) |
89 | cf761c0d | Panagiotis Kanavos | ///</remarks> |
90 | cf761c0d | Panagiotis Kanavos | [ImportingConstructor] |
91 | c53aa229 | Panagiotis Kanavos | public ShellViewModel(IWindowManager windowManager, IEventAggregator events, IStatusChecker statusChecker, PithosSettings settings) |
92 | c53aa229 | Panagiotis Kanavos | { |
93 | 7e26c075 | Panagiotis Kanavos | try |
94 | 7e26c075 | Panagiotis Kanavos | { |
95 | d3a13891 | Panagiotis Kanavos | |
96 | 7e26c075 | Panagiotis Kanavos | _windowManager = windowManager; |
97 | cf761c0d | Panagiotis Kanavos | //CHECK: Caliburn doesn't need explicit command construction |
98 | cf761c0d | Panagiotis Kanavos | //OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder); |
99 | 7e26c075 | Panagiotis Kanavos | _statusChecker = statusChecker; |
100 | cf761c0d | Panagiotis Kanavos | //The event subst |
101 | 7e26c075 | Panagiotis Kanavos | _events = events; |
102 | 7e26c075 | Panagiotis Kanavos | _events.Subscribe(this); |
103 | c53aa229 | Panagiotis Kanavos | |
104 | 7e26c075 | Panagiotis Kanavos | Settings = settings; |
105 | c53aa229 | Panagiotis Kanavos | |
106 | 7e26c075 | Panagiotis Kanavos | StatusMessage = "In Synch"; |
107 | 7e26c075 | Panagiotis Kanavos | |
108 | 6aa29f4f | Panagiotis Kanavos | _accounts.CollectionChanged += (sender, e) => |
109 | 6aa29f4f | Panagiotis Kanavos | { |
110 | 6aa29f4f | Panagiotis Kanavos | NotifyOfPropertyChange(() => OpenFolderCaption); |
111 | 6aa29f4f | Panagiotis Kanavos | NotifyOfPropertyChange(() => HasAccounts); |
112 | 6aa29f4f | Panagiotis Kanavos | }; |
113 | 6aa29f4f | Panagiotis Kanavos | |
114 | 7e26c075 | Panagiotis Kanavos | } |
115 | 7e26c075 | Panagiotis Kanavos | catch (Exception exc) |
116 | 7e26c075 | Panagiotis Kanavos | { |
117 | 7e26c075 | Panagiotis Kanavos | Log.Error("Error while starting the ShellViewModel",exc); |
118 | 7e26c075 | Panagiotis Kanavos | throw; |
119 | 7e26c075 | Panagiotis Kanavos | } |
120 | c53aa229 | Panagiotis Kanavos | } |
121 | c53aa229 | Panagiotis Kanavos | |
122 | 6aa29f4f | Panagiotis Kanavos | |
123 | 7b0a5fec | Panagiotis Kanavos | protected override void OnActivate() |
124 | 7b0a5fec | Panagiotis Kanavos | { |
125 | 7b0a5fec | Panagiotis Kanavos | base.OnActivate(); |
126 | 42800be8 | Panagiotis Kanavos | |
127 | ff26eb71 | Panagiotis Kanavos | StartMonitoring(); |
128 | ff26eb71 | Panagiotis Kanavos | } |
129 | 7b0a5fec | Panagiotis Kanavos | |
130 | 42800be8 | Panagiotis Kanavos | |
131 | ff26eb71 | Panagiotis Kanavos | private async Task StartMonitoring() |
132 | ff26eb71 | Panagiotis Kanavos | { |
133 | ff26eb71 | Panagiotis Kanavos | try |
134 | f734ab5b | Panagiotis Kanavos | { |
135 | f734ab5b | Panagiotis Kanavos | if (Settings.Accounts == null) |
136 | f734ab5b | Panagiotis Kanavos | { |
137 | f734ab5b | Panagiotis Kanavos | Settings.Accounts=new AccountsCollection(); |
138 | f734ab5b | Panagiotis Kanavos | Settings.Save(); |
139 | f734ab5b | Panagiotis Kanavos | return; |
140 | f734ab5b | Panagiotis Kanavos | } |
141 | f734ab5b | Panagiotis Kanavos | |
142 | ff26eb71 | Panagiotis Kanavos | foreach (var account in Settings.Accounts) |
143 | ff26eb71 | Panagiotis Kanavos | { |
144 | ff26eb71 | Panagiotis Kanavos | await MonitorAccount(account); |
145 | ff26eb71 | Panagiotis Kanavos | } |
146 | ff26eb71 | Panagiotis Kanavos | _statusService = StatusService.Start(); |
147 | 42800be8 | Panagiotis Kanavos | } |
148 | 42800be8 | Panagiotis Kanavos | catch (AggregateException exc) |
149 | 42800be8 | Panagiotis Kanavos | { |
150 | ff26eb71 | Panagiotis Kanavos | exc.Handle(e => |
151 | ff26eb71 | Panagiotis Kanavos | { |
152 | 42800be8 | Panagiotis Kanavos | Log.Error("Error while starting monitoring", e); |
153 | 42800be8 | Panagiotis Kanavos | return true; |
154 | 42800be8 | Panagiotis Kanavos | }); |
155 | 42800be8 | Panagiotis Kanavos | throw; |
156 | 7b0a5fec | Panagiotis Kanavos | } |
157 | 42800be8 | Panagiotis Kanavos | } |
158 | 7b0a5fec | Panagiotis Kanavos | |
159 | 42800be8 | Panagiotis Kanavos | protected override void OnDeactivate(bool close) |
160 | 42800be8 | Panagiotis Kanavos | { |
161 | 42800be8 | Panagiotis Kanavos | base.OnDeactivate(close); |
162 | 42800be8 | Panagiotis Kanavos | if (close) |
163 | 42800be8 | Panagiotis Kanavos | { |
164 | 42800be8 | Panagiotis Kanavos | StatusService.Stop(_statusService); |
165 | 42800be8 | Panagiotis Kanavos | _statusService = null; |
166 | 42800be8 | Panagiotis Kanavos | } |
167 | 7b0a5fec | Panagiotis Kanavos | } |
168 | 7b0a5fec | Panagiotis Kanavos | |
169 | 42800be8 | Panagiotis Kanavos | public Task MonitorAccount(AccountSettings account) |
170 | c53aa229 | Panagiotis Kanavos | { |
171 | 42800be8 | Panagiotis Kanavos | return Task.Factory.StartNew(() => |
172 | b5ddf130 | Panagiotis Kanavos | { |
173 | 7e26c075 | Panagiotis Kanavos | PithosMonitor monitor = null; |
174 | 7e26c075 | Panagiotis Kanavos | var accountName = account.AccountName; |
175 | 7e26c075 | Panagiotis Kanavos | |
176 | 7e26c075 | Panagiotis Kanavos | if (_monitors.TryGetValue(accountName, out monitor)) |
177 | c53aa229 | Panagiotis Kanavos | { |
178 | 7e26c075 | Panagiotis Kanavos | //If the account is active |
179 | 7e26c075 | Panagiotis Kanavos | if (account.IsActive) |
180 | 7e26c075 | Panagiotis Kanavos | //Start the monitor. It's OK to start an already started monitor, |
181 | 0338939c | Panagiotis Kanavos | //it will just ignore the call |
182 | 0338939c | Panagiotis Kanavos | StartMonitor(monitor).Wait(); |
183 | 7e26c075 | Panagiotis Kanavos | else |
184 | 7e26c075 | Panagiotis Kanavos | { |
185 | 7e26c075 | Panagiotis Kanavos | //If the account is inactive |
186 | 7e26c075 | Panagiotis Kanavos | //Stop and remove the monitor |
187 | 7e26c075 | Panagiotis Kanavos | RemoveMonitor(accountName); |
188 | 7e26c075 | Panagiotis Kanavos | } |
189 | 7e26c075 | Panagiotis Kanavos | return; |
190 | c53aa229 | Panagiotis Kanavos | } |
191 | c53aa229 | Panagiotis Kanavos | |
192 | 7b0a5fec | Panagiotis Kanavos | //Create a new monitor/ Can't use MEF here, it would return a single instance for all monitors |
193 | 7e26c075 | Panagiotis Kanavos | monitor = new PithosMonitor |
194 | 7b0a5fec | Panagiotis Kanavos | { |
195 | 7b0a5fec | Panagiotis Kanavos | UserName = accountName, |
196 | 7b0a5fec | Panagiotis Kanavos | ApiKey = account.ApiKey, |
197 | 7b0a5fec | Panagiotis Kanavos | UsePithos = account.UsePithos, |
198 | 7b0a5fec | Panagiotis Kanavos | StatusNotification = this, |
199 | 7b0a5fec | Panagiotis Kanavos | RootPath = account.RootPath |
200 | 7b0a5fec | Panagiotis Kanavos | }; |
201 | 7b0a5fec | Panagiotis Kanavos | //PithosMonitor uses MEF so we need to resolve it |
202 | 7e26c075 | Panagiotis Kanavos | IoC.BuildUp(monitor); |
203 | 7e26c075 | Panagiotis Kanavos | |
204 | 7e26c075 | Panagiotis Kanavos | var appSettings = Properties.Settings.Default; |
205 | 7e26c075 | Panagiotis Kanavos | monitor.AuthenticationUrl = account.UsePithos |
206 | f734ab5b | Panagiotis Kanavos | ? account.ServerUrl.ToString() |
207 | 7e26c075 | Panagiotis Kanavos | : appSettings.CloudfilesAuthenticationUrl; |
208 | 7e26c075 | Panagiotis Kanavos | |
209 | 7e26c075 | Panagiotis Kanavos | _monitors[accountName] = monitor; |
210 | 7e26c075 | Panagiotis Kanavos | |
211 | 7e26c075 | Panagiotis Kanavos | if (account.IsActive) |
212 | 7e26c075 | Panagiotis Kanavos | { |
213 | 7e26c075 | Panagiotis Kanavos | //Don't start a monitor if it doesn't have an account and ApiKey |
214 | 7e26c075 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(monitor.UserName) || |
215 | 7e26c075 | Panagiotis Kanavos | String.IsNullOrWhiteSpace(monitor.ApiKey)) |
216 | 7e26c075 | Panagiotis Kanavos | return; |
217 | 7e26c075 | Panagiotis Kanavos | StartMonitor(monitor); |
218 | 7e26c075 | Panagiotis Kanavos | } |
219 | 7e26c075 | Panagiotis Kanavos | }); |
220 | 9bae55d1 | Panagiotis Kanavos | } |
221 | 9bae55d1 | Panagiotis Kanavos | |
222 | c53aa229 | Panagiotis Kanavos | |
223 | 9bae55d1 | Panagiotis Kanavos | protected override void OnViewLoaded(object view) |
224 | 9bae55d1 | Panagiotis Kanavos | { |
225 | c53aa229 | Panagiotis Kanavos | UpdateStatus(); |
226 | 6aa29f4f | Panagiotis Kanavos | var window = (Window)view; |
227 | 6aa29f4f | Panagiotis Kanavos | TaskEx.Delay(1000).ContinueWith(t => Execute.OnUIThread(window.Hide)); |
228 | 9bae55d1 | Panagiotis Kanavos | base.OnViewLoaded(view); |
229 | 9bae55d1 | Panagiotis Kanavos | } |
230 | 9bae55d1 | Panagiotis Kanavos | |
231 | 9bae55d1 | Panagiotis Kanavos | |
232 | 9bae55d1 | Panagiotis Kanavos | #region Status Properties |
233 | c53aa229 | Panagiotis Kanavos | |
234 | 9bae55d1 | Panagiotis Kanavos | private string _statusMessage; |
235 | 9bae55d1 | Panagiotis Kanavos | public string StatusMessage |
236 | 9bae55d1 | Panagiotis Kanavos | { |
237 | 9bae55d1 | Panagiotis Kanavos | get { return _statusMessage; } |
238 | 9bae55d1 | Panagiotis Kanavos | set |
239 | 9bae55d1 | Panagiotis Kanavos | { |
240 | 9bae55d1 | Panagiotis Kanavos | _statusMessage = value; |
241 | c53aa229 | Panagiotis Kanavos | NotifyOfPropertyChange(() => StatusMessage); |
242 | 9bae55d1 | Panagiotis Kanavos | } |
243 | 9bae55d1 | Panagiotis Kanavos | } |
244 | 9bae55d1 | Panagiotis Kanavos | |
245 | 6aa29f4f | Panagiotis Kanavos | private readonly ObservableConcurrentCollection<AccountInfo> _accounts = new ObservableConcurrentCollection<AccountInfo>(); |
246 | 0bd56b7c | Panagiotis Kanavos | public ObservableConcurrentCollection<AccountInfo> Accounts |
247 | 9bae55d1 | Panagiotis Kanavos | { |
248 | 0bd56b7c | Panagiotis Kanavos | get { return _accounts; } |
249 | 9bae55d1 | Panagiotis Kanavos | } |
250 | 9bae55d1 | Panagiotis Kanavos | |
251 | 6aa29f4f | Panagiotis Kanavos | public bool HasAccounts |
252 | 6aa29f4f | Panagiotis Kanavos | { |
253 | 6aa29f4f | Panagiotis Kanavos | get { return _accounts.Count > 0; } |
254 | 6aa29f4f | Panagiotis Kanavos | } |
255 | 6aa29f4f | Panagiotis Kanavos | |
256 | c53aa229 | Panagiotis Kanavos | |
257 | 6aa29f4f | Panagiotis Kanavos | public string OpenFolderCaption |
258 | 6aa29f4f | Panagiotis Kanavos | { |
259 | 6aa29f4f | Panagiotis Kanavos | get |
260 | 6aa29f4f | Panagiotis Kanavos | { |
261 | 6aa29f4f | Panagiotis Kanavos | return (_accounts.Count == 0) |
262 | 6aa29f4f | Panagiotis Kanavos | ? "No Accounts Defined" |
263 | 6aa29f4f | Panagiotis Kanavos | : "Open Pithos Folder"; |
264 | 6aa29f4f | Panagiotis Kanavos | } |
265 | 6aa29f4f | Panagiotis Kanavos | } |
266 | 6aa29f4f | Panagiotis Kanavos | |
267 | 6aa29f4f | Panagiotis Kanavos | private string _pauseSyncCaption="Pause Synching"; |
268 | c53aa229 | Panagiotis Kanavos | public string PauseSyncCaption |
269 | c53aa229 | Panagiotis Kanavos | { |
270 | c53aa229 | Panagiotis Kanavos | get { return _pauseSyncCaption; } |
271 | c53aa229 | Panagiotis Kanavos | set |
272 | c53aa229 | Panagiotis Kanavos | { |
273 | c53aa229 | Panagiotis Kanavos | _pauseSyncCaption = value; |
274 | c53aa229 | Panagiotis Kanavos | NotifyOfPropertyChange(() => PauseSyncCaption); |
275 | c53aa229 | Panagiotis Kanavos | } |
276 | c53aa229 | Panagiotis Kanavos | } |
277 | c53aa229 | Panagiotis Kanavos | |
278 | c53aa229 | Panagiotis Kanavos | private readonly ObservableConcurrentCollection<FileEntry> _recentFiles = new ObservableConcurrentCollection<FileEntry>(); |
279 | c53aa229 | Panagiotis Kanavos | public ObservableConcurrentCollection<FileEntry> RecentFiles |
280 | c53aa229 | Panagiotis Kanavos | { |
281 | c53aa229 | Panagiotis Kanavos | get { return _recentFiles; } |
282 | c53aa229 | Panagiotis Kanavos | } |
283 | c53aa229 | Panagiotis Kanavos | |
284 | c53aa229 | Panagiotis Kanavos | |
285 | 6aa29f4f | Panagiotis Kanavos | private string _statusIcon="../Images/Pithos.ico"; |
286 | c53aa229 | Panagiotis Kanavos | public string StatusIcon |
287 | 9bae55d1 | Panagiotis Kanavos | { |
288 | c53aa229 | Panagiotis Kanavos | get { return _statusIcon; } |
289 | c53aa229 | Panagiotis Kanavos | set |
290 | c53aa229 | Panagiotis Kanavos | { |
291 | 6aa29f4f | Panagiotis Kanavos | //_statusIcon = value; |
292 | c53aa229 | Panagiotis Kanavos | NotifyOfPropertyChange(() => StatusIcon); |
293 | c53aa229 | Panagiotis Kanavos | } |
294 | 9bae55d1 | Panagiotis Kanavos | } |
295 | 9bae55d1 | Panagiotis Kanavos | |
296 | 9bae55d1 | Panagiotis Kanavos | #endregion |
297 | 9bae55d1 | Panagiotis Kanavos | |
298 | c53aa229 | Panagiotis Kanavos | #region Commands |
299 | 9bae55d1 | Panagiotis Kanavos | |
300 | 9bae55d1 | Panagiotis Kanavos | public void ShowPreferences() |
301 | 9bae55d1 | Panagiotis Kanavos | { |
302 | c53aa229 | Panagiotis Kanavos | Settings.Reload(); |
303 | d3a13891 | Panagiotis Kanavos | var preferences = new PreferencesViewModel(_windowManager,_events, this,Settings); |
304 | c53aa229 | Panagiotis Kanavos | _windowManager.ShowDialog(preferences); |
305 | d3a13891 | Panagiotis Kanavos | |
306 | 9bae55d1 | Panagiotis Kanavos | } |
307 | 9bae55d1 | Panagiotis Kanavos | |
308 | 5cb9d74f | Panagiotis Kanavos | public void AboutPithos() |
309 | 5cb9d74f | Panagiotis Kanavos | { |
310 | 4b0a2fc0 | Panagiotis Kanavos | var about = new AboutViewModel(); |
311 | 5cb9d74f | Panagiotis Kanavos | _windowManager.ShowWindow(about); |
312 | 5cb9d74f | Panagiotis Kanavos | } |
313 | 5cb9d74f | Panagiotis Kanavos | |
314 | 5cb9d74f | Panagiotis Kanavos | public void SendFeedback() |
315 | 5cb9d74f | Panagiotis Kanavos | { |
316 | 2c27cdd0 | Panagiotis Kanavos | var feedBack = IoC.Get<FeedbackViewModel>(); |
317 | 5cb9d74f | Panagiotis Kanavos | _windowManager.ShowWindow(feedBack); |
318 | 5cb9d74f | Panagiotis Kanavos | } |
319 | c53aa229 | Panagiotis Kanavos | |
320 | cf761c0d | Panagiotis Kanavos | //public PithosCommand OpenPithosFolderCommand { get; private set; } |
321 | c53aa229 | Panagiotis Kanavos | |
322 | 9bae55d1 | Panagiotis Kanavos | public void OpenPithosFolder() |
323 | 9bae55d1 | Panagiotis Kanavos | { |
324 | 5a99309c | Panagiotis Kanavos | var account = Settings.Accounts.FirstOrDefault(acc => acc.IsActive); |
325 | 5a99309c | Panagiotis Kanavos | if (account == null) |
326 | 5a99309c | Panagiotis Kanavos | return; |
327 | 5a99309c | Panagiotis Kanavos | Process.Start(account.RootPath); |
328 | 5a99309c | Panagiotis Kanavos | } |
329 | 5a99309c | Panagiotis Kanavos | |
330 | 5a99309c | Panagiotis Kanavos | public void OpenPithosFolder(AccountInfo account) |
331 | 5a99309c | Panagiotis Kanavos | { |
332 | 5a99309c | Panagiotis Kanavos | Process.Start(account.AccountPath); |
333 | 9bae55d1 | Panagiotis Kanavos | } |
334 | 9bae55d1 | Panagiotis Kanavos | |
335 | 7b0a5fec | Panagiotis Kanavos | |
336 | f734ab5b | Panagiotis Kanavos | /* |
337 | 6aa29f4f | Panagiotis Kanavos | public void GoToSite() |
338 | 6aa29f4f | Panagiotis Kanavos | { |
339 | 6aa29f4f | Panagiotis Kanavos | var site = Properties.Settings.Default.PithosSite; |
340 | 6aa29f4f | Panagiotis Kanavos | Process.Start(site); |
341 | 6aa29f4f | Panagiotis Kanavos | } |
342 | f734ab5b | Panagiotis Kanavos | */ |
343 | 6aa29f4f | Panagiotis Kanavos | |
344 | 0bd56b7c | Panagiotis Kanavos | public void GoToSite(AccountInfo account) |
345 | 0bd56b7c | Panagiotis Kanavos | { |
346 | d3a13891 | Panagiotis Kanavos | var site = String.Format("{0}/ui/?token={1}&user={2}", |
347 | f734ab5b | Panagiotis Kanavos | account.SiteUri,account.Token, |
348 | 0bd56b7c | Panagiotis Kanavos | account.UserName); |
349 | c53aa229 | Panagiotis Kanavos | Process.Start(site); |
350 | 9bae55d1 | Panagiotis Kanavos | } |
351 | 9bae55d1 | Panagiotis Kanavos | |
352 | 7b0a5fec | Panagiotis Kanavos | public void ShowFileProperties() |
353 | 7b0a5fec | Panagiotis Kanavos | { |
354 | 7b0a5fec | Panagiotis Kanavos | var account = Settings.Accounts.First(acc => acc.IsActive); |
355 | 7b0a5fec | Panagiotis Kanavos | var dir = new DirectoryInfo(account.RootPath + @"\pithos"); |
356 | 7b0a5fec | Panagiotis Kanavos | var files=dir.GetFiles(); |
357 | 7b0a5fec | Panagiotis Kanavos | var r=new Random(); |
358 | 7b0a5fec | Panagiotis Kanavos | var idx=r.Next(0, files.Length); |
359 | 7b0a5fec | Panagiotis Kanavos | ShowFileProperties(files[idx].FullName); |
360 | 7b0a5fec | Panagiotis Kanavos | } |
361 | 7b0a5fec | Panagiotis Kanavos | |
362 | 7b0a5fec | Panagiotis Kanavos | public void ShowFileProperties(string filePath) |
363 | 7b0a5fec | Panagiotis Kanavos | { |
364 | 7b0a5fec | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(filePath)) |
365 | 7b0a5fec | Panagiotis Kanavos | throw new ArgumentNullException("filePath"); |
366 | 7b0a5fec | Panagiotis Kanavos | if (!File.Exists(filePath)) |
367 | 7b0a5fec | Panagiotis Kanavos | throw new ArgumentException(String.Format("Non existent file {0}",filePath),"filePath"); |
368 | 7b0a5fec | Panagiotis Kanavos | Contract.EndContractBlock(); |
369 | 7b0a5fec | Panagiotis Kanavos | |
370 | 7b0a5fec | Panagiotis Kanavos | var pair=(from monitor in Monitors |
371 | 7b0a5fec | Panagiotis Kanavos | where filePath.StartsWith(monitor.Value.RootPath, StringComparison.InvariantCultureIgnoreCase) |
372 | 7b0a5fec | Panagiotis Kanavos | select monitor).FirstOrDefault(); |
373 | 7b0a5fec | Panagiotis Kanavos | var account = pair.Key; |
374 | 7b0a5fec | Panagiotis Kanavos | var accountMonitor = pair.Value; |
375 | 7b0a5fec | Panagiotis Kanavos | |
376 | aba9e6d9 | Panagiotis Kanavos | if (accountMonitor == null) |
377 | aba9e6d9 | Panagiotis Kanavos | return; |
378 | aba9e6d9 | Panagiotis Kanavos | |
379 | c92e02f3 | Panagiotis Kanavos | var infoTask=Task.Factory.StartNew(()=>accountMonitor.GetObjectInfo(filePath)); |
380 | 7b0a5fec | Panagiotis Kanavos | |
381 | 7b0a5fec | Panagiotis Kanavos | |
382 | 7b0a5fec | Panagiotis Kanavos | |
383 | c92e02f3 | Panagiotis Kanavos | var fileProperties = new FilePropertiesViewModel(this, infoTask,filePath); |
384 | 7b0a5fec | Panagiotis Kanavos | _windowManager.ShowWindow(fileProperties); |
385 | 42800be8 | Panagiotis Kanavos | } |
386 | 42800be8 | Panagiotis Kanavos | |
387 | 42800be8 | Panagiotis Kanavos | public void ShowContainerProperties() |
388 | 42800be8 | Panagiotis Kanavos | { |
389 | 42800be8 | Panagiotis Kanavos | var account = Settings.Accounts.First(acc => acc.IsActive); |
390 | 42800be8 | Panagiotis Kanavos | var dir = new DirectoryInfo(account.RootPath); |
391 | 42800be8 | Panagiotis Kanavos | var fullName = (from folder in dir.EnumerateDirectories() |
392 | 42800be8 | Panagiotis Kanavos | where (folder.Attributes & FileAttributes.Hidden) == 0 |
393 | 42800be8 | Panagiotis Kanavos | select folder.FullName).First(); |
394 | 42800be8 | Panagiotis Kanavos | ShowContainerProperties(fullName); |
395 | 42800be8 | Panagiotis Kanavos | } |
396 | 42800be8 | Panagiotis Kanavos | |
397 | 42800be8 | Panagiotis Kanavos | public void ShowContainerProperties(string filePath) |
398 | 42800be8 | Panagiotis Kanavos | { |
399 | 42800be8 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(filePath)) |
400 | 42800be8 | Panagiotis Kanavos | throw new ArgumentNullException("filePath"); |
401 | 42800be8 | Panagiotis Kanavos | if (!Directory.Exists(filePath)) |
402 | 42800be8 | Panagiotis Kanavos | throw new ArgumentException(String.Format("Non existent file {0}",filePath),"filePath"); |
403 | 42800be8 | Panagiotis Kanavos | Contract.EndContractBlock(); |
404 | 42800be8 | Panagiotis Kanavos | |
405 | 42800be8 | Panagiotis Kanavos | var pair=(from monitor in Monitors |
406 | 42800be8 | Panagiotis Kanavos | where filePath.StartsWith(monitor.Value.RootPath, StringComparison.InvariantCultureIgnoreCase) |
407 | 42800be8 | Panagiotis Kanavos | select monitor).FirstOrDefault(); |
408 | 42800be8 | Panagiotis Kanavos | var account = pair.Key; |
409 | 42800be8 | Panagiotis Kanavos | var accountMonitor = pair.Value; |
410 | c92e02f3 | Panagiotis Kanavos | var info = accountMonitor.GetContainerInfo(filePath); |
411 | 42800be8 | Panagiotis Kanavos | |
412 | 42800be8 | Panagiotis Kanavos | |
413 | 42800be8 | Panagiotis Kanavos | |
414 | 42800be8 | Panagiotis Kanavos | var containerProperties = new ContainerPropertiesViewModel(this, info,filePath); |
415 | 42800be8 | Panagiotis Kanavos | _windowManager.ShowWindow(containerProperties); |
416 | 7b0a5fec | Panagiotis Kanavos | } |
417 | 7b0a5fec | Panagiotis Kanavos | |
418 | 7b0a5fec | Panagiotis Kanavos | public ObjectInfo RefreshObjectInfo(ObjectInfo currentInfo) |
419 | 7b0a5fec | Panagiotis Kanavos | { |
420 | 7b0a5fec | Panagiotis Kanavos | if (currentInfo==null) |
421 | 7b0a5fec | Panagiotis Kanavos | throw new ArgumentNullException("currentInfo"); |
422 | 7b0a5fec | Panagiotis Kanavos | Contract.EndContractBlock(); |
423 | 7b0a5fec | Panagiotis Kanavos | |
424 | 7b0a5fec | Panagiotis Kanavos | var monitor = Monitors[currentInfo.Account]; |
425 | 7b0a5fec | Panagiotis Kanavos | var newInfo=monitor.CloudClient.GetObjectInfo(currentInfo.Account, currentInfo.Container, currentInfo.Name); |
426 | 7b0a5fec | Panagiotis Kanavos | return newInfo; |
427 | 7b0a5fec | Panagiotis Kanavos | } |
428 | c53aa229 | Panagiotis Kanavos | |
429 | 42800be8 | Panagiotis Kanavos | public ContainerInfo RefreshContainerInfo(ContainerInfo container) |
430 | 42800be8 | Panagiotis Kanavos | { |
431 | 42800be8 | Panagiotis Kanavos | if (container == null) |
432 | 42800be8 | Panagiotis Kanavos | throw new ArgumentNullException("container"); |
433 | 42800be8 | Panagiotis Kanavos | Contract.EndContractBlock(); |
434 | 42800be8 | Panagiotis Kanavos | |
435 | 42800be8 | Panagiotis Kanavos | var monitor = Monitors[container.Account]; |
436 | 42800be8 | Panagiotis Kanavos | var newInfo = monitor.CloudClient.GetContainerInfo(container.Account, container.Name); |
437 | 42800be8 | Panagiotis Kanavos | return newInfo; |
438 | 42800be8 | Panagiotis Kanavos | } |
439 | 42800be8 | Panagiotis Kanavos | |
440 | 42800be8 | Panagiotis Kanavos | |
441 | 9bae55d1 | Panagiotis Kanavos | public void ToggleSynching() |
442 | 9bae55d1 | Panagiotis Kanavos | { |
443 | c53aa229 | Panagiotis Kanavos | bool isPaused=false; |
444 | c53aa229 | Panagiotis Kanavos | foreach (var pair in Monitors) |
445 | c53aa229 | Panagiotis Kanavos | { |
446 | c53aa229 | Panagiotis Kanavos | var monitor = pair.Value; |
447 | c53aa229 | Panagiotis Kanavos | monitor.Pause = !monitor.Pause; |
448 | c53aa229 | Panagiotis Kanavos | isPaused = monitor.Pause; |
449 | c53aa229 | Panagiotis Kanavos | } |
450 | c53aa229 | Panagiotis Kanavos | |
451 | c53aa229 | Panagiotis Kanavos | PauseSyncCaption = isPaused ? "Resume syncing" : "Pause syncing"; |
452 | c53aa229 | Panagiotis Kanavos | var iconKey = isPaused? "TraySyncPaused" : "TrayInSynch"; |
453 | 2f41824c | Panagiotis Kanavos | StatusIcon = String.Format(@"../Images/{0}.ico", iconKey); |
454 | c53aa229 | Panagiotis Kanavos | } |
455 | c53aa229 | Panagiotis Kanavos | |
456 | c53aa229 | Panagiotis Kanavos | public void ExitPithos() |
457 | c53aa229 | Panagiotis Kanavos | { |
458 | c53aa229 | Panagiotis Kanavos | foreach (var pair in Monitors) |
459 | c53aa229 | Panagiotis Kanavos | { |
460 | c53aa229 | Panagiotis Kanavos | var monitor = pair.Value; |
461 | c53aa229 | Panagiotis Kanavos | monitor.Stop(); |
462 | c53aa229 | Panagiotis Kanavos | } |
463 | c53aa229 | Panagiotis Kanavos | |
464 | c53aa229 | Panagiotis Kanavos | ((Window)GetView()).Close(); |
465 | c53aa229 | Panagiotis Kanavos | } |
466 | c53aa229 | Panagiotis Kanavos | #endregion |
467 | c53aa229 | Panagiotis Kanavos | |
468 | c53aa229 | Panagiotis Kanavos | |
469 | c53aa229 | Panagiotis Kanavos | private Dictionary<PithosStatus, StatusInfo> iconNames = new List<StatusInfo> |
470 | c53aa229 | Panagiotis Kanavos | { |
471 | c53aa229 | Panagiotis Kanavos | new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"), |
472 | c53aa229 | Panagiotis Kanavos | new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching"), |
473 | c53aa229 | Panagiotis Kanavos | new StatusInfo(PithosStatus.SyncPaused, "Sync Paused", "TraySyncPaused") |
474 | c53aa229 | Panagiotis Kanavos | }.ToDictionary(s => s.Status); |
475 | c53aa229 | Panagiotis Kanavos | |
476 | c53aa229 | Panagiotis Kanavos | readonly IWindowManager _windowManager; |
477 | c53aa229 | Panagiotis Kanavos | |
478 | c53aa229 | Panagiotis Kanavos | |
479 | cf761c0d | Panagiotis Kanavos | ///<summary> |
480 | cf761c0d | Panagiotis Kanavos | /// Updates the visual status indicators of the application depending on status changes, e.g. icon, stat |
481 | cf761c0d | Panagiotis Kanavos | ///</summary> |
482 | c53aa229 | Panagiotis Kanavos | public void UpdateStatus() |
483 | c53aa229 | Panagiotis Kanavos | { |
484 | c53aa229 | Panagiotis Kanavos | var pithosStatus = _statusChecker.GetPithosStatus(); |
485 | c53aa229 | Panagiotis Kanavos | |
486 | c53aa229 | Panagiotis Kanavos | if (iconNames.ContainsKey(pithosStatus)) |
487 | c53aa229 | Panagiotis Kanavos | { |
488 | c53aa229 | Panagiotis Kanavos | var info = iconNames[pithosStatus]; |
489 | 2f41824c | Panagiotis Kanavos | StatusIcon = String.Format(@"../Images/{0}.ico", info.IconName); |
490 | d17258c2 | Panagiotis Kanavos | |
491 | d17258c2 | Panagiotis Kanavos | Assembly assembly = Assembly.GetExecutingAssembly(); |
492 | d17258c2 | Panagiotis Kanavos | var fileVersion = FileVersionInfo.GetVersionInfo(assembly.Location); |
493 | d17258c2 | Panagiotis Kanavos | |
494 | d17258c2 | Panagiotis Kanavos | |
495 | d17258c2 | Panagiotis Kanavos | StatusMessage = String.Format("Pithos {0}\r\n{1}", fileVersion.FileVersion,info.StatusText); |
496 | c53aa229 | Panagiotis Kanavos | } |
497 | 79653ffc | Panagiotis Kanavos | |
498 | c53aa229 | Panagiotis Kanavos | _events.Publish(new Notification { Title = "Start", Message = "Start Monitoring", Level = TraceLevel.Info}); |
499 | c53aa229 | Panagiotis Kanavos | } |
500 | c53aa229 | Panagiotis Kanavos | |
501 | c53aa229 | Panagiotis Kanavos | |
502 | c53aa229 | Panagiotis Kanavos | |
503 | c28a075a | Panagiotis Kanavos | private Task StartMonitor(PithosMonitor monitor,int retries=0) |
504 | c53aa229 | Panagiotis Kanavos | { |
505 | c53aa229 | Panagiotis Kanavos | return Task.Factory.StartNew(() => |
506 | c53aa229 | Panagiotis Kanavos | { |
507 | c53aa229 | Panagiotis Kanavos | using (log4net.ThreadContext.Stacks["Monitor"].Push("Start")) |
508 | c53aa229 | Panagiotis Kanavos | { |
509 | c53aa229 | Panagiotis Kanavos | try |
510 | c53aa229 | Panagiotis Kanavos | { |
511 | c53aa229 | Panagiotis Kanavos | Log.InfoFormat("Start Monitoring {0}", monitor.UserName); |
512 | c28a075a | Panagiotis Kanavos | |
513 | c53aa229 | Panagiotis Kanavos | monitor.Start(); |
514 | c53aa229 | Panagiotis Kanavos | } |
515 | c28a075a | Panagiotis Kanavos | catch (WebException exc) |
516 | c28a075a | Panagiotis Kanavos | { |
517 | c28a075a | Panagiotis Kanavos | if (AbandonRetry(monitor, retries)) |
518 | c28a075a | Panagiotis Kanavos | return; |
519 | c28a075a | Panagiotis Kanavos | |
520 | c28a075a | Panagiotis Kanavos | if (IsUnauthorized(exc)) |
521 | c28a075a | Panagiotis Kanavos | { |
522 | c28a075a | Panagiotis Kanavos | var message = String.Format("API Key Expired for {0}. Starting Renewal",monitor.UserName); |
523 | c28a075a | Panagiotis Kanavos | Log.Error(message,exc); |
524 | 437abfca | Panagiotis Kanavos | TryAuthorize(monitor,retries).Wait(); |
525 | c28a075a | Panagiotis Kanavos | } |
526 | c28a075a | Panagiotis Kanavos | else |
527 | c28a075a | Panagiotis Kanavos | { |
528 | c28a075a | Panagiotis Kanavos | TryLater(monitor, exc,retries); |
529 | c28a075a | Panagiotis Kanavos | } |
530 | c28a075a | Panagiotis Kanavos | } |
531 | c53aa229 | Panagiotis Kanavos | catch (Exception exc) |
532 | c53aa229 | Panagiotis Kanavos | { |
533 | c28a075a | Panagiotis Kanavos | if (AbandonRetry(monitor, retries)) |
534 | c28a075a | Panagiotis Kanavos | return; |
535 | c28a075a | Panagiotis Kanavos | |
536 | c28a075a | Panagiotis Kanavos | TryLater(monitor,exc,retries); |
537 | c53aa229 | Panagiotis Kanavos | } |
538 | c53aa229 | Panagiotis Kanavos | } |
539 | c53aa229 | Panagiotis Kanavos | }); |
540 | c53aa229 | Panagiotis Kanavos | } |
541 | c53aa229 | Panagiotis Kanavos | |
542 | c28a075a | Panagiotis Kanavos | private bool AbandonRetry(PithosMonitor monitor, int retries) |
543 | c28a075a | Panagiotis Kanavos | { |
544 | c28a075a | Panagiotis Kanavos | if (retries > 1) |
545 | c28a075a | Panagiotis Kanavos | { |
546 | c28a075a | Panagiotis Kanavos | var message = String.Format("Monitoring of account {0} has failed too many times. Will not retry", |
547 | c28a075a | Panagiotis Kanavos | monitor.UserName); |
548 | c28a075a | Panagiotis Kanavos | _events.Publish(new Notification |
549 | c28a075a | Panagiotis Kanavos | {Title = "Account monitoring failed", Message = message, Level = TraceLevel.Error}); |
550 | c28a075a | Panagiotis Kanavos | return true; |
551 | c28a075a | Panagiotis Kanavos | } |
552 | c28a075a | Panagiotis Kanavos | return false; |
553 | c28a075a | Panagiotis Kanavos | } |
554 | c28a075a | Panagiotis Kanavos | |
555 | c28a075a | Panagiotis Kanavos | |
556 | b5ddf130 | Panagiotis Kanavos | private async Task TryAuthorize(PithosMonitor monitor,int retries) |
557 | c28a075a | Panagiotis Kanavos | { |
558 | c28a075a | Panagiotis Kanavos | _events.Publish(new Notification { Title = "Authorization failed", Message = "Your API Key has probably expired. You will be directed to a page where you can renew it", Level = TraceLevel.Error }); |
559 | c28a075a | Panagiotis Kanavos | |
560 | b5ddf130 | Panagiotis Kanavos | try |
561 | c28a075a | Panagiotis Kanavos | { |
562 | b5ddf130 | Panagiotis Kanavos | |
563 | 03ee454f | Panagiotis Kanavos | var credentials = await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl); |
564 | b5ddf130 | Panagiotis Kanavos | |
565 | b5ddf130 | Panagiotis Kanavos | var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName); |
566 | c28a075a | Panagiotis Kanavos | account.ApiKey = credentials.Password; |
567 | c28a075a | Panagiotis Kanavos | monitor.ApiKey = credentials.Password; |
568 | d3a13891 | Panagiotis Kanavos | Settings.Save(); |
569 | b5ddf130 | Panagiotis Kanavos | await TaskEx.Delay(10000); |
570 | b5ddf130 | Panagiotis Kanavos | StartMonitor(monitor, retries + 1); |
571 | c92e02f3 | Panagiotis Kanavos | NotifyOfPropertyChange(()=>Accounts); |
572 | b5ddf130 | Panagiotis Kanavos | } |
573 | b5ddf130 | Panagiotis Kanavos | catch (AggregateException exc) |
574 | b5ddf130 | Panagiotis Kanavos | { |
575 | b5ddf130 | Panagiotis Kanavos | string message = String.Format("API Key retrieval for {0} failed", monitor.UserName); |
576 | b5ddf130 | Panagiotis Kanavos | Log.Error(message, exc.InnerException); |
577 | b5ddf130 | Panagiotis Kanavos | _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error }); |
578 | b5ddf130 | Panagiotis Kanavos | return; |
579 | b5ddf130 | Panagiotis Kanavos | } |
580 | b5ddf130 | Panagiotis Kanavos | catch (Exception exc) |
581 | b5ddf130 | Panagiotis Kanavos | { |
582 | b5ddf130 | Panagiotis Kanavos | string message = String.Format("API Key retrieval for {0} failed", monitor.UserName); |
583 | b5ddf130 | Panagiotis Kanavos | Log.Error(message, exc); |
584 | b5ddf130 | Panagiotis Kanavos | _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error }); |
585 | b5ddf130 | Panagiotis Kanavos | return; |
586 | b5ddf130 | Panagiotis Kanavos | |
587 | b5ddf130 | Panagiotis Kanavos | } |
588 | b5ddf130 | Panagiotis Kanavos | |
589 | c28a075a | Panagiotis Kanavos | } |
590 | c28a075a | Panagiotis Kanavos | |
591 | c28a075a | Panagiotis Kanavos | private static bool IsUnauthorized(WebException exc) |
592 | c28a075a | Panagiotis Kanavos | { |
593 | c28a075a | Panagiotis Kanavos | if (exc==null) |
594 | c28a075a | Panagiotis Kanavos | throw new ArgumentNullException("exc"); |
595 | c28a075a | Panagiotis Kanavos | Contract.EndContractBlock(); |
596 | c28a075a | Panagiotis Kanavos | |
597 | c28a075a | Panagiotis Kanavos | var response = exc.Response as HttpWebResponse; |
598 | c28a075a | Panagiotis Kanavos | if (response == null) |
599 | c28a075a | Panagiotis Kanavos | return false; |
600 | c28a075a | Panagiotis Kanavos | return (response.StatusCode == HttpStatusCode.Unauthorized); |
601 | c28a075a | Panagiotis Kanavos | } |
602 | c28a075a | Panagiotis Kanavos | |
603 | c28a075a | Panagiotis Kanavos | private void TryLater(PithosMonitor monitor, Exception exc,int retries) |
604 | c28a075a | Panagiotis Kanavos | { |
605 | c28a075a | Panagiotis Kanavos | var message = String.Format("An exception occured. Can't start monitoring\nWill retry in 10 seconds"); |
606 | c28a075a | Panagiotis Kanavos | Task.Factory.StartNewDelayed(10000, () => StartMonitor(monitor,retries+1)); |
607 | c28a075a | Panagiotis Kanavos | _events.Publish(new Notification |
608 | c28a075a | Panagiotis Kanavos | {Title = "Error", Message = message, Level = TraceLevel.Error}); |
609 | c28a075a | Panagiotis Kanavos | Log.Error(message, exc); |
610 | c28a075a | Panagiotis Kanavos | } |
611 | c28a075a | Panagiotis Kanavos | |
612 | c53aa229 | Panagiotis Kanavos | |
613 | c53aa229 | Panagiotis Kanavos | public void NotifyChange(string status, TraceLevel level=TraceLevel.Info) |
614 | c53aa229 | Panagiotis Kanavos | { |
615 | c53aa229 | Panagiotis Kanavos | this.StatusMessage = status; |
616 | 9bae55d1 | Panagiotis Kanavos | |
617 | c53aa229 | Panagiotis Kanavos | _events.Publish(new Notification { Title = "Pithos", Message = status, Level = level }); |
618 | 9bae55d1 | Panagiotis Kanavos | } |
619 | 9bae55d1 | Panagiotis Kanavos | |
620 | c53aa229 | Panagiotis Kanavos | public void NotifyChangedFile(string filePath) |
621 | 9bae55d1 | Panagiotis Kanavos | { |
622 | c53aa229 | Panagiotis Kanavos | var entry = new FileEntry {FullPath=filePath}; |
623 | c53aa229 | Panagiotis Kanavos | IProducerConsumerCollection<FileEntry> files=this.RecentFiles; |
624 | c53aa229 | Panagiotis Kanavos | FileEntry popped; |
625 | c53aa229 | Panagiotis Kanavos | while (files.Count > 5) |
626 | c53aa229 | Panagiotis Kanavos | files.TryTake(out popped); |
627 | c53aa229 | Panagiotis Kanavos | files.TryAdd(entry); |
628 | 9bae55d1 | Panagiotis Kanavos | } |
629 | 9bae55d1 | Panagiotis Kanavos | |
630 | 0bd56b7c | Panagiotis Kanavos | public void NotifyAccount(AccountInfo account) |
631 | 0bd56b7c | Panagiotis Kanavos | { |
632 | 0bd56b7c | Panagiotis Kanavos | if (account== null) |
633 | 0bd56b7c | Panagiotis Kanavos | return; |
634 | 0bd56b7c | Panagiotis Kanavos | |
635 | 0bd56b7c | Panagiotis Kanavos | account.SiteUri= String.Format("{0}/ui/?token={1}&user={2}", |
636 | f734ab5b | Panagiotis Kanavos | account.SiteUri, account.Token, |
637 | 0bd56b7c | Panagiotis Kanavos | account.UserName); |
638 | 0bd56b7c | Panagiotis Kanavos | |
639 | 0bd56b7c | Panagiotis Kanavos | IProducerConsumerCollection<AccountInfo> accounts = Accounts; |
640 | 0bd56b7c | Panagiotis Kanavos | for (var i = 0; i < _accounts.Count; i++) |
641 | 0bd56b7c | Panagiotis Kanavos | { |
642 | 0bd56b7c | Panagiotis Kanavos | AccountInfo item; |
643 | 0bd56b7c | Panagiotis Kanavos | if (accounts.TryTake(out item)) |
644 | 0bd56b7c | Panagiotis Kanavos | { |
645 | 0bd56b7c | Panagiotis Kanavos | if (item.UserName!=account.UserName) |
646 | 0bd56b7c | Panagiotis Kanavos | { |
647 | 0bd56b7c | Panagiotis Kanavos | accounts.TryAdd(item); |
648 | 0bd56b7c | Panagiotis Kanavos | } |
649 | 0bd56b7c | Panagiotis Kanavos | } |
650 | 0bd56b7c | Panagiotis Kanavos | } |
651 | 0bd56b7c | Panagiotis Kanavos | |
652 | 0bd56b7c | Panagiotis Kanavos | accounts.TryAdd(account); |
653 | 0bd56b7c | Panagiotis Kanavos | } |
654 | 0bd56b7c | Panagiotis Kanavos | |
655 | c53aa229 | Panagiotis Kanavos | |
656 | c53aa229 | Panagiotis Kanavos | public void RemoveMonitor(string accountName) |
657 | c53aa229 | Panagiotis Kanavos | { |
658 | c53aa229 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(accountName)) |
659 | c53aa229 | Panagiotis Kanavos | return; |
660 | c53aa229 | Panagiotis Kanavos | |
661 | 6aa29f4f | Panagiotis Kanavos | var accountInfo=_accounts.FirstOrDefault(account => account.UserName == accountName); |
662 | 6aa29f4f | Panagiotis Kanavos | _accounts.TryRemove(accountInfo); |
663 | 6aa29f4f | Panagiotis Kanavos | |
664 | c53aa229 | Panagiotis Kanavos | PithosMonitor monitor; |
665 | c53aa229 | Panagiotis Kanavos | if (Monitors.TryGetValue(accountName, out monitor)) |
666 | c53aa229 | Panagiotis Kanavos | { |
667 | c53aa229 | Panagiotis Kanavos | Monitors.Remove(accountName); |
668 | c53aa229 | Panagiotis Kanavos | monitor.Stop(); |
669 | c53aa229 | Panagiotis Kanavos | } |
670 | c53aa229 | Panagiotis Kanavos | } |
671 | c53aa229 | Panagiotis Kanavos | |
672 | c53aa229 | Panagiotis Kanavos | public void RefreshOverlays() |
673 | c53aa229 | Panagiotis Kanavos | { |
674 | c53aa229 | Panagiotis Kanavos | foreach (var pair in Monitors) |
675 | c53aa229 | Panagiotis Kanavos | { |
676 | c53aa229 | Panagiotis Kanavos | var monitor = pair.Value; |
677 | c53aa229 | Panagiotis Kanavos | |
678 | c53aa229 | Panagiotis Kanavos | var path = monitor.RootPath; |
679 | c53aa229 | Panagiotis Kanavos | |
680 | c53aa229 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(path)) |
681 | c53aa229 | Panagiotis Kanavos | continue; |
682 | c53aa229 | Panagiotis Kanavos | |
683 | c53aa229 | Panagiotis Kanavos | if (!Directory.Exists(path) && !File.Exists(path)) |
684 | c53aa229 | Panagiotis Kanavos | continue; |
685 | c53aa229 | Panagiotis Kanavos | |
686 | c53aa229 | Panagiotis Kanavos | IntPtr pathPointer = Marshal.StringToCoTaskMemAuto(path); |
687 | c53aa229 | Panagiotis Kanavos | |
688 | c53aa229 | Panagiotis Kanavos | try |
689 | c53aa229 | Panagiotis Kanavos | { |
690 | c53aa229 | Panagiotis Kanavos | NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM, |
691 | c53aa229 | Panagiotis Kanavos | HChangeNotifyFlags.SHCNF_PATHW | HChangeNotifyFlags.SHCNF_FLUSHNOWAIT, |
692 | c53aa229 | Panagiotis Kanavos | pathPointer, IntPtr.Zero); |
693 | c53aa229 | Panagiotis Kanavos | } |
694 | c53aa229 | Panagiotis Kanavos | finally |
695 | c53aa229 | Panagiotis Kanavos | { |
696 | c53aa229 | Panagiotis Kanavos | Marshal.FreeHGlobal(pathPointer); |
697 | c53aa229 | Panagiotis Kanavos | } |
698 | c53aa229 | Panagiotis Kanavos | } |
699 | c53aa229 | Panagiotis Kanavos | } |
700 | c53aa229 | Panagiotis Kanavos | |
701 | d3a13891 | Panagiotis Kanavos | #region Event Handlers |
702 | d3a13891 | Panagiotis Kanavos | |
703 | d3a13891 | Panagiotis Kanavos | public void Handle(SelectiveSynchChanges message) |
704 | d3a13891 | Panagiotis Kanavos | { |
705 | d3a13891 | Panagiotis Kanavos | var accountName = message.Account.AccountName; |
706 | d3a13891 | Panagiotis Kanavos | PithosMonitor monitor; |
707 | d3a13891 | Panagiotis Kanavos | if (_monitors.TryGetValue(accountName, out monitor)) |
708 | d3a13891 | Panagiotis Kanavos | { |
709 | d3a13891 | Panagiotis Kanavos | monitor.AddSelectivePaths(message.Added); |
710 | d3a13891 | Panagiotis Kanavos | monitor.RemoveSelectivePaths(message.Removed); |
711 | d3a13891 | Panagiotis Kanavos | |
712 | d3a13891 | Panagiotis Kanavos | } |
713 | d3a13891 | Panagiotis Kanavos | |
714 | d3a13891 | Panagiotis Kanavos | } |
715 | d3a13891 | Panagiotis Kanavos | |
716 | d3a13891 | Panagiotis Kanavos | |
717 | d3a13891 | Panagiotis Kanavos | public void Handle(Notification notification) |
718 | d3a13891 | Panagiotis Kanavos | { |
719 | d3a13891 | Panagiotis Kanavos | if (!Settings.ShowDesktopNotifications) |
720 | d3a13891 | Panagiotis Kanavos | return; |
721 | d3a13891 | Panagiotis Kanavos | BalloonIcon icon = BalloonIcon.None; |
722 | d3a13891 | Panagiotis Kanavos | switch (notification.Level) |
723 | d3a13891 | Panagiotis Kanavos | { |
724 | d3a13891 | Panagiotis Kanavos | case TraceLevel.Error: |
725 | d3a13891 | Panagiotis Kanavos | icon = BalloonIcon.Error; |
726 | d3a13891 | Panagiotis Kanavos | break; |
727 | d3a13891 | Panagiotis Kanavos | case TraceLevel.Info: |
728 | d3a13891 | Panagiotis Kanavos | case TraceLevel.Verbose: |
729 | d3a13891 | Panagiotis Kanavos | icon = BalloonIcon.Info; |
730 | d3a13891 | Panagiotis Kanavos | break; |
731 | d3a13891 | Panagiotis Kanavos | case TraceLevel.Warning: |
732 | d3a13891 | Panagiotis Kanavos | icon = BalloonIcon.Warning; |
733 | d3a13891 | Panagiotis Kanavos | break; |
734 | d3a13891 | Panagiotis Kanavos | default: |
735 | d3a13891 | Panagiotis Kanavos | icon = BalloonIcon.None; |
736 | d3a13891 | Panagiotis Kanavos | break; |
737 | d3a13891 | Panagiotis Kanavos | } |
738 | d3a13891 | Panagiotis Kanavos | |
739 | 79653ffc | Panagiotis Kanavos | if (Settings.ShowDesktopNotifications) |
740 | 79653ffc | Panagiotis Kanavos | { |
741 | 79653ffc | Panagiotis Kanavos | var tv = (ShellView) this.GetView(); |
742 | 79653ffc | Panagiotis Kanavos | tv.TaskbarView.ShowBalloonTip(notification.Title, notification.Message, icon); |
743 | 79653ffc | Panagiotis Kanavos | } |
744 | d3a13891 | Panagiotis Kanavos | } |
745 | d3a13891 | Panagiotis Kanavos | #endregion |
746 | 42800be8 | Panagiotis Kanavos | |
747 | 42800be8 | Panagiotis Kanavos | public void Handle(ShowFilePropertiesEvent message) |
748 | 42800be8 | Panagiotis Kanavos | { |
749 | 42800be8 | Panagiotis Kanavos | if (message == null) |
750 | 42800be8 | Panagiotis Kanavos | throw new ArgumentNullException("message"); |
751 | 42800be8 | Panagiotis Kanavos | if (String.IsNullOrWhiteSpace(message.FileName) ) |
752 | 42800be8 | Panagiotis Kanavos | throw new ArgumentException("message"); |
753 | 42800be8 | Panagiotis Kanavos | Contract.EndContractBlock(); |
754 | 42800be8 | Panagiotis Kanavos | |
755 | 42800be8 | Panagiotis Kanavos | var fileName = message.FileName; |
756 | 42800be8 | Panagiotis Kanavos | |
757 | 42800be8 | Panagiotis Kanavos | if (File.Exists(fileName)) |
758 | 42800be8 | Panagiotis Kanavos | this.ShowFileProperties(fileName); |
759 | 42800be8 | Panagiotis Kanavos | else if (Directory.Exists(fileName)) |
760 | 42800be8 | Panagiotis Kanavos | this.ShowContainerProperties(fileName); |
761 | 42800be8 | Panagiotis Kanavos | } |
762 | 9bae55d1 | Panagiotis Kanavos | } |
763 | 9bae55d1 | Panagiotis Kanavos | } |