comparison Chronosv2/source/ViewModel/ShellViewModel.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children 741981715d94
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 /*
2 The MIT License
3
4 Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24
25 using System;
26 using System.Collections.Generic;
27 using System.IO;
28 using System.Linq;
29 using System.Reflection;
30 using System.Windows;
31 using System.Windows.Input;
32 using System.Windows.Shell;
33 using System.Windows.Threading;
34 using Chronos.Authentication;
35 using Chronos.Modules.Navigation;
36 using Chronos.Presentation.Core.Navigation;
37 using Chronos.Presentation.Core.ViewModel;
38 using Chronos.Presentation.Core.VirtualDesktops;
39 using Chronos.Presentation.ViewModel;
40 using Chronos.Presentation.Windows;
41 using Chronos.Presentation.Windows.Navigation;
42 using Chronos.WidgetLibrary;
43 using NLog;
44 using nRoute.Components;
45 using nRoute.Components.Messaging;
46 using nRoute.Navigation;
47
48 namespace Chronos.ViewModel
49 {
50 /// <summary>
51 /// Shell Window ViewModel
52 /// </summary>
53 public sealed class ShellViewModel
54 : ViewModelBase
55 {
56 #region · Logger ·
57
58 private static Logger Logger = LogManager.GetCurrentClassLogger();
59
60 #endregion
61
62 #region · Fields ·
63
64 private WindowState windowState;
65 private string userName;
66
67 #region · Commands ·
68
69 private ICommand maximizeCommand;
70 private ICommand minimizeCommand;
71 private ICommand showWidgetLibraryCommand;
72 private ICommand shutdownCommand;
73 private ICommand closeSessionCommand;
74 private ICommand switchDesktopCommand;
75 private ICommand showDesktopCommand;
76 private ICommand saveCurrentDesktopCommand;
77 private ICommand saveAllDesktopsCommand;
78 private ICommand showAboutBoxCommand;
79
80 #endregion
81
82 #region · Observers ·
83
84 private ChannelObserver<AuthenticationInfo> authenticationObserver;
85 private ChannelObserver<ActiveDesktopChangedInfo> activeDesktopObserver;
86 private ChannelObserver<NavigatedInfo> navigatedObserver;
87
88 #endregion
89
90 #endregion
91
92 #region · Commands ·
93
94 /// <summary>
95 /// Gets the maximize command.
96 /// </summary>
97 /// <value>The maximize command.</value>
98 public ICommand MaximizeCommand
99 {
100 get
101 {
102 if (this.maximizeCommand == null)
103 {
104 this.maximizeCommand = new ActionCommand(() => OnMaximizeWindow());
105 }
106
107 return this.maximizeCommand;
108 }
109 }
110
111 /// <summary>
112 /// Gets the minimize command.
113 /// </summary>
114 /// <value>The minimize command.</value>
115 public ICommand MinimizeCommand
116 {
117 get
118 {
119 if (this.minimizeCommand == null)
120 {
121 this.minimizeCommand = new ActionCommand(() => OnMinimizeWindow());
122 }
123
124 return this.minimizeCommand;
125 }
126 }
127
128 /// <summary>
129 /// Gets the switch desktop command
130 /// </summary>
131 public ICommand SwitchDesktopCommand
132 {
133 get
134 {
135 if (this.switchDesktopCommand == null)
136 {
137 this.switchDesktopCommand = new ActionCommand(() => OnSwitchDesktop());
138 }
139
140 return this.switchDesktopCommand;
141 }
142 }
143
144 /// <summary>
145 /// Gets the show desktop command
146 /// </summary>
147 public ICommand ShowDesktopCommand
148 {
149 get
150 {
151 if (this.showDesktopCommand == null)
152 {
153 this.showDesktopCommand = new ActionCommand(() => OnShowDesktop());
154 }
155
156 return this.showDesktopCommand;
157 }
158 }
159
160 /// <summary>
161 /// Gets the save current desktop command.
162 /// </summary>
163 /// <value>The save desktop command.</value>
164 public ICommand SaveCurrentDesktopCommand
165 {
166 get
167 {
168 if (this.saveCurrentDesktopCommand == null)
169 {
170 this.saveCurrentDesktopCommand = new ActionCommand(() => OnSaveCurrentDesktop());
171 }
172
173 return this.saveCurrentDesktopCommand;
174 }
175 }
176
177 /// <summary>
178 /// Gets the save all desktops command.
179 /// </summary>
180 /// <value>The save desktop command.</value>
181 public ICommand SaveAllDesktopsCommand
182 {
183 get
184 {
185 if (this.saveAllDesktopsCommand == null)
186 {
187 this.saveAllDesktopsCommand = new ActionCommand(() => OnSaveAllDesktops());
188 }
189
190 return this.saveAllDesktopsCommand;
191 }
192 }
193
194 /// <summary>
195 /// Gets the show widget library command.
196 /// </summary>
197 /// <value>The show widget library command.</value>
198 public ICommand ShowWidgetLibraryCommand
199 {
200 get
201 {
202 if (this.showWidgetLibraryCommand == null)
203 {
204 this.showWidgetLibraryCommand = new ActionCommand(() => OnShowWidgetLibrary());
205 }
206
207 return this.showWidgetLibraryCommand;
208 }
209 }
210
211 /// <summary>
212 /// Gets the about box command.
213 /// </summary>
214 /// <value>The about box command.</value>
215 public ICommand ShowAboutBoxCommand
216 {
217 get
218 {
219 if (this.showAboutBoxCommand == null)
220 {
221 this.showAboutBoxCommand = new ActionCommand(() => OnShowAboutBoxCommand());
222 }
223
224 return this.showAboutBoxCommand;
225 }
226 }
227
228 /// <summary>
229 /// Gets the shutdown command
230 /// </summary>
231 public ICommand ShutdownCommand
232 {
233 get
234 {
235 if (this.shutdownCommand == null)
236 {
237 this.shutdownCommand = new ActionCommand(() => OnShutdown());
238 }
239
240 return this.shutdownCommand;
241 }
242 }
243
244 /// <summary>
245 /// Gets the log off command
246 /// </summary>
247 public ICommand CloseSessionCommand
248 {
249 get
250 {
251 if (this.closeSessionCommand == null)
252 {
253 this.closeSessionCommand = new ActionCommand(() => OnCloseSession());
254 }
255
256 return this.closeSessionCommand;
257 }
258 }
259
260 #endregion
261
262 #region · Properties ·
263
264 /// <summary>
265 /// Gets or sets the state of the window.
266 /// </summary>
267 /// <value>The state of the window.</value>
268 public WindowState WindowState
269 {
270 get { return this.windowState; }
271 set
272 {
273 if (this.windowState != value)
274 {
275 this.windowState = value;
276 this.NotifyPropertyChanged(() => WindowState);
277 }
278 }
279 }
280
281 /// <summary>
282 /// Gets the active windows.
283 /// </summary>
284 /// <value>The active windows.</value>
285 public IList<INavigationViewModel> ActiveWindows
286 {
287 get
288 {
289 if (this.GetService<IVirtualDesktopManager>().HasDesktopActive)
290 {
291 return this.GetService<IVirtualDesktopManager>().ActiveDesktopWindows;
292 }
293
294 return null;
295 }
296 }
297
298 /// <summary>
299 /// Gets the logged in user name
300 /// </summary>
301 public string UserName
302 {
303 get { return (!String.IsNullOrEmpty(this.userName) ? this.userName : "Sesión no iniciada"); }
304 private set
305 {
306 this.userName = value;
307 this.NotifyPropertyChanged(() => UserName);
308 }
309 }
310
311 #endregion
312
313 #region · Constructors ·
314
315 /// <summary>
316 /// Initializes a new instance of the <see cref="ShellViewModel"/> class.
317 /// </summary>
318 public ShellViewModel()
319 : base()
320 {
321 this.InitializeObservers();
322 }
323
324 #endregion
325
326 #region · Command Actions ·
327
328 /// <summary>
329 /// Handles the show widget library command action
330 /// </summary>
331 private void OnShowWidgetLibrary()
332 {
333 this.GetService<IVirtualDesktopManager>().Show<WidgetLibraryView>();
334 }
335
336 /// <summary>
337 /// Handles the switch desktop command action
338 /// </summary>
339 private void OnSwitchDesktop()
340 {
341 this.GetService<IVirtualDesktopManager>().SwitchDesktop();
342 }
343
344 /// <summary>
345 /// Handles the show desktop command
346 /// </summary>
347 private void OnShowDesktop()
348 {
349 this.GetService<IVirtualDesktopManager>().ShowDesktop();
350 }
351
352 /// <summary>
353 /// Handles the save current desktop command action
354 /// </summary>
355 private void OnSaveCurrentDesktop()
356 {
357 this.GetService<IVirtualDesktopManager>().SaveCurrentDesktop();
358 }
359
360 /// <summary>
361 /// Handles the save all desktops command action
362 /// </summary>
363 private void OnSaveAllDesktops()
364 {
365 this.GetService<IVirtualDesktopManager>().SaveAllDesktops();
366 }
367
368 /// <summary>
369 /// Handles the shutdown command action
370 /// </summary>
371 private void OnShutdown()
372 {
373 Logger.Debug("Finalizando la sesión");
374 Application.Current.Shutdown();
375 }
376
377 /// <summary>
378 /// Closes the session
379 /// </summary>
380 private void OnCloseSession()
381 {
382 Channel<AuthenticationInfo>.Public.OnNext(
383 new AuthenticationInfo { Action = AuthenticationAction.LogOut }, true);
384 }
385
386 /// <summary>
387 /// Maximizes the window.
388 /// </summary>
389 private void OnMaximizeWindow()
390 {
391 if (this.WindowState == WindowState.Maximized)
392 {
393 this.WindowState = WindowState.Normal;
394 }
395 else
396 {
397 this.WindowState = WindowState.Maximized;
398 }
399
400 Logger.Debug("Cambiado el estado de la ventana principal de la aplicación ({0})", this.WindowState);
401 }
402
403 /// <summary>
404 /// Handles the minimize window command action
405 /// </summary>
406 private void OnMinimizeWindow()
407 {
408 this.WindowState = WindowState.Minimized;
409 }
410
411 private void OnShowAboutBoxCommand()
412 {
413 this.GetService<INavigationService>()
414 .Navigate(NavigateMode.Modal, NavigationRoutes.About);
415 }
416
417 #endregion
418
419 #region · Observer Initialization ·
420
421 private void InitializeObservers()
422 {
423 Logger.Debug("Inicializando observers de nroute");
424
425 // Authentication Observer
426 this.authenticationObserver = new ChannelObserver<AuthenticationInfo>(
427 (l) => OnAuthenticationAction(l));
428
429 // Subscribe on the UI Thread
430 this.authenticationObserver.Subscribe(ThreadOption.BackgroundThread);
431
432 // Active desktop changed observer
433 this.activeDesktopObserver = new ChannelObserver<ActiveDesktopChangedInfo>(
434 (l) => OnActiveDesktopChanged(l));
435
436 // Subscribe on the UI Thread
437 this.activeDesktopObserver.Subscribe(ThreadOption.UIThread);
438
439 // Navigation observers
440
441 // Navigated observer
442 this.navigatedObserver = new ChannelObserver<NavigatedInfo>(
443 (l) => OnNavigated(l));
444
445 this.navigatedObserver.Subscribe(ThreadOption.BackgroundThread);
446 }
447
448 #endregion
449
450 #region · Observer Actions ·
451
452 private void OnAuthenticationAction(AuthenticationInfo info)
453 {
454 switch (info.Action)
455 {
456 case AuthenticationAction.LogOn:
457 break;
458
459 case AuthenticationAction.LoggedIn:
460 this.Invoke(() => { this.UserName = info.UserId; });
461 break;
462
463 case AuthenticationAction.LogOut:
464 this.Invoke(() => { this.UserName = info.UserId; });
465 break;
466 }
467 }
468
469 private void OnActiveDesktopChanged(ActiveDesktopChangedInfo info)
470 {
471 Logger.Debug("Cambiando el escritorio activo");
472
473 this.NotifyPropertyChanged(() => ActiveWindows);
474 }
475
476 private void OnNavigated(NavigatedInfo info)
477 {
478 Logger.Debug("Navegación finalizada correctamente ({0})", info.Request.RequestUrl);
479
480 this.CreateRecentNavigationEntry(info);
481 }
482
483 #endregion
484
485 #region · Windows 7 Taskbar ·
486
487 private void CreateRecentNavigationEntry(NavigatedInfo value)
488 {
489 if (value.Request.NavigationMode == NavigateMode.New)
490 {
491 this.Dispatcher.BeginInvoke(
492 (Action)delegate
493 {
494 if (App.RunningOnWin7)
495 {
496 JumpList jl = JumpList.GetJumpList(Application.Current);
497
498 if (jl != null)
499 {
500 if (jl.JumpItems.Count >= 10)
501 {
502 jl.JumpItems.Clear();
503 }
504
505 var q = jl.JumpItems.OfType<JumpTask>().Where(t => t.Arguments.Equals(value.Request.RequestUrl));
506
507 if (q.Count() == 0)
508 {
509 jl.JumpItems.Add
510 (
511 new JumpTask
512 {
513 CustomCategory = "Recent",
514 Title = value.Title,
515 Arguments = value.Request.RequestUrl,
516 IconResourcePath = null,
517 IconResourceIndex = -1,
518 Description = null,
519 WorkingDirectory =
520 Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
521 }
522 );
523
524 jl.Apply();
525 }
526 }
527 }
528 }, DispatcherPriority.Background);
529 }
530 }
531
532 #endregion
533 }
534 }