Mercurial > silverbladetech
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Chronosv2/source/ViewModel/ShellViewModel.cs Tue Feb 21 17:25:44 2012 +0700 @@ -0,0 +1,534 @@ +/* +The MIT License + +Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/ + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Windows; +using System.Windows.Input; +using System.Windows.Shell; +using System.Windows.Threading; +using Chronos.Authentication; +using Chronos.Modules.Navigation; +using Chronos.Presentation.Core.Navigation; +using Chronos.Presentation.Core.ViewModel; +using Chronos.Presentation.Core.VirtualDesktops; +using Chronos.Presentation.ViewModel; +using Chronos.Presentation.Windows; +using Chronos.Presentation.Windows.Navigation; +using Chronos.WidgetLibrary; +using NLog; +using nRoute.Components; +using nRoute.Components.Messaging; +using nRoute.Navigation; + +namespace Chronos.ViewModel +{ + /// <summary> + /// Shell Window ViewModel + /// </summary> + public sealed class ShellViewModel + : ViewModelBase + { + #region · Logger · + + private static Logger Logger = LogManager.GetCurrentClassLogger(); + + #endregion + + #region · Fields · + + private WindowState windowState; + private string userName; + + #region · Commands · + + private ICommand maximizeCommand; + private ICommand minimizeCommand; + private ICommand showWidgetLibraryCommand; + private ICommand shutdownCommand; + private ICommand closeSessionCommand; + private ICommand switchDesktopCommand; + private ICommand showDesktopCommand; + private ICommand saveCurrentDesktopCommand; + private ICommand saveAllDesktopsCommand; + private ICommand showAboutBoxCommand; + + #endregion + + #region · Observers · + + private ChannelObserver<AuthenticationInfo> authenticationObserver; + private ChannelObserver<ActiveDesktopChangedInfo> activeDesktopObserver; + private ChannelObserver<NavigatedInfo> navigatedObserver; + + #endregion + + #endregion + + #region · Commands · + + /// <summary> + /// Gets the maximize command. + /// </summary> + /// <value>The maximize command.</value> + public ICommand MaximizeCommand + { + get + { + if (this.maximizeCommand == null) + { + this.maximizeCommand = new ActionCommand(() => OnMaximizeWindow()); + } + + return this.maximizeCommand; + } + } + + /// <summary> + /// Gets the minimize command. + /// </summary> + /// <value>The minimize command.</value> + public ICommand MinimizeCommand + { + get + { + if (this.minimizeCommand == null) + { + this.minimizeCommand = new ActionCommand(() => OnMinimizeWindow()); + } + + return this.minimizeCommand; + } + } + + /// <summary> + /// Gets the switch desktop command + /// </summary> + public ICommand SwitchDesktopCommand + { + get + { + if (this.switchDesktopCommand == null) + { + this.switchDesktopCommand = new ActionCommand(() => OnSwitchDesktop()); + } + + return this.switchDesktopCommand; + } + } + + /// <summary> + /// Gets the show desktop command + /// </summary> + public ICommand ShowDesktopCommand + { + get + { + if (this.showDesktopCommand == null) + { + this.showDesktopCommand = new ActionCommand(() => OnShowDesktop()); + } + + return this.showDesktopCommand; + } + } + + /// <summary> + /// Gets the save current desktop command. + /// </summary> + /// <value>The save desktop command.</value> + public ICommand SaveCurrentDesktopCommand + { + get + { + if (this.saveCurrentDesktopCommand == null) + { + this.saveCurrentDesktopCommand = new ActionCommand(() => OnSaveCurrentDesktop()); + } + + return this.saveCurrentDesktopCommand; + } + } + + /// <summary> + /// Gets the save all desktops command. + /// </summary> + /// <value>The save desktop command.</value> + public ICommand SaveAllDesktopsCommand + { + get + { + if (this.saveAllDesktopsCommand == null) + { + this.saveAllDesktopsCommand = new ActionCommand(() => OnSaveAllDesktops()); + } + + return this.saveAllDesktopsCommand; + } + } + + /// <summary> + /// Gets the show widget library command. + /// </summary> + /// <value>The show widget library command.</value> + public ICommand ShowWidgetLibraryCommand + { + get + { + if (this.showWidgetLibraryCommand == null) + { + this.showWidgetLibraryCommand = new ActionCommand(() => OnShowWidgetLibrary()); + } + + return this.showWidgetLibraryCommand; + } + } + + /// <summary> + /// Gets the about box command. + /// </summary> + /// <value>The about box command.</value> + public ICommand ShowAboutBoxCommand + { + get + { + if (this.showAboutBoxCommand == null) + { + this.showAboutBoxCommand = new ActionCommand(() => OnShowAboutBoxCommand()); + } + + return this.showAboutBoxCommand; + } + } + + /// <summary> + /// Gets the shutdown command + /// </summary> + public ICommand ShutdownCommand + { + get + { + if (this.shutdownCommand == null) + { + this.shutdownCommand = new ActionCommand(() => OnShutdown()); + } + + return this.shutdownCommand; + } + } + + /// <summary> + /// Gets the log off command + /// </summary> + public ICommand CloseSessionCommand + { + get + { + if (this.closeSessionCommand == null) + { + this.closeSessionCommand = new ActionCommand(() => OnCloseSession()); + } + + return this.closeSessionCommand; + } + } + + #endregion + + #region · Properties · + + /// <summary> + /// Gets or sets the state of the window. + /// </summary> + /// <value>The state of the window.</value> + public WindowState WindowState + { + get { return this.windowState; } + set + { + if (this.windowState != value) + { + this.windowState = value; + this.NotifyPropertyChanged(() => WindowState); + } + } + } + + /// <summary> + /// Gets the active windows. + /// </summary> + /// <value>The active windows.</value> + public IList<INavigationViewModel> ActiveWindows + { + get + { + if (this.GetService<IVirtualDesktopManager>().HasDesktopActive) + { + return this.GetService<IVirtualDesktopManager>().ActiveDesktopWindows; + } + + return null; + } + } + + /// <summary> + /// Gets the logged in user name + /// </summary> + public string UserName + { + get { return (!String.IsNullOrEmpty(this.userName) ? this.userName : "Sesión no iniciada"); } + private set + { + this.userName = value; + this.NotifyPropertyChanged(() => UserName); + } + } + + #endregion + + #region · Constructors · + + /// <summary> + /// Initializes a new instance of the <see cref="ShellViewModel"/> class. + /// </summary> + public ShellViewModel() + : base() + { + this.InitializeObservers(); + } + + #endregion + + #region · Command Actions · + + /// <summary> + /// Handles the show widget library command action + /// </summary> + private void OnShowWidgetLibrary() + { + this.GetService<IVirtualDesktopManager>().Show<WidgetLibraryView>(); + } + + /// <summary> + /// Handles the switch desktop command action + /// </summary> + private void OnSwitchDesktop() + { + this.GetService<IVirtualDesktopManager>().SwitchDesktop(); + } + + /// <summary> + /// Handles the show desktop command + /// </summary> + private void OnShowDesktop() + { + this.GetService<IVirtualDesktopManager>().ShowDesktop(); + } + + /// <summary> + /// Handles the save current desktop command action + /// </summary> + private void OnSaveCurrentDesktop() + { + this.GetService<IVirtualDesktopManager>().SaveCurrentDesktop(); + } + + /// <summary> + /// Handles the save all desktops command action + /// </summary> + private void OnSaveAllDesktops() + { + this.GetService<IVirtualDesktopManager>().SaveAllDesktops(); + } + + /// <summary> + /// Handles the shutdown command action + /// </summary> + private void OnShutdown() + { + Logger.Debug("Finalizando la sesión"); + Application.Current.Shutdown(); + } + + /// <summary> + /// Closes the session + /// </summary> + private void OnCloseSession() + { + Channel<AuthenticationInfo>.Public.OnNext( + new AuthenticationInfo { Action = AuthenticationAction.LogOut }, true); + } + + /// <summary> + /// Maximizes the window. + /// </summary> + private void OnMaximizeWindow() + { + if (this.WindowState == WindowState.Maximized) + { + this.WindowState = WindowState.Normal; + } + else + { + this.WindowState = WindowState.Maximized; + } + + Logger.Debug("Cambiado el estado de la ventana principal de la aplicación ({0})", this.WindowState); + } + + /// <summary> + /// Handles the minimize window command action + /// </summary> + private void OnMinimizeWindow() + { + this.WindowState = WindowState.Minimized; + } + + private void OnShowAboutBoxCommand() + { + this.GetService<INavigationService>() + .Navigate(NavigateMode.Modal, NavigationRoutes.About); + } + + #endregion + + #region · Observer Initialization · + + private void InitializeObservers() + { + Logger.Debug("Inicializando observers de nroute"); + + // Authentication Observer + this.authenticationObserver = new ChannelObserver<AuthenticationInfo>( + (l) => OnAuthenticationAction(l)); + + // Subscribe on the UI Thread + this.authenticationObserver.Subscribe(ThreadOption.BackgroundThread); + + // Active desktop changed observer + this.activeDesktopObserver = new ChannelObserver<ActiveDesktopChangedInfo>( + (l) => OnActiveDesktopChanged(l)); + + // Subscribe on the UI Thread + this.activeDesktopObserver.Subscribe(ThreadOption.UIThread); + + // Navigation observers + + // Navigated observer + this.navigatedObserver = new ChannelObserver<NavigatedInfo>( + (l) => OnNavigated(l)); + + this.navigatedObserver.Subscribe(ThreadOption.BackgroundThread); + } + + #endregion + + #region · Observer Actions · + + private void OnAuthenticationAction(AuthenticationInfo info) + { + switch (info.Action) + { + case AuthenticationAction.LogOn: + break; + + case AuthenticationAction.LoggedIn: + this.Invoke(() => { this.UserName = info.UserId; }); + break; + + case AuthenticationAction.LogOut: + this.Invoke(() => { this.UserName = info.UserId; }); + break; + } + } + + private void OnActiveDesktopChanged(ActiveDesktopChangedInfo info) + { + Logger.Debug("Cambiando el escritorio activo"); + + this.NotifyPropertyChanged(() => ActiveWindows); + } + + private void OnNavigated(NavigatedInfo info) + { + Logger.Debug("Navegación finalizada correctamente ({0})", info.Request.RequestUrl); + + this.CreateRecentNavigationEntry(info); + } + + #endregion + + #region · Windows 7 Taskbar · + + private void CreateRecentNavigationEntry(NavigatedInfo value) + { + if (value.Request.NavigationMode == NavigateMode.New) + { + this.Dispatcher.BeginInvoke( + (Action)delegate + { + if (App.RunningOnWin7) + { + JumpList jl = JumpList.GetJumpList(Application.Current); + + if (jl != null) + { + if (jl.JumpItems.Count >= 10) + { + jl.JumpItems.Clear(); + } + + var q = jl.JumpItems.OfType<JumpTask>().Where(t => t.Arguments.Equals(value.Request.RequestUrl)); + + if (q.Count() == 0) + { + jl.JumpItems.Add + ( + new JumpTask + { + CustomCategory = "Recent", + Title = value.Title, + Arguments = value.Request.RequestUrl, + IconResourcePath = null, + IconResourceIndex = -1, + Description = null, + WorkingDirectory = + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + } + ); + + jl.Apply(); + } + } + } + }, DispatcherPriority.Background); + } + } + + #endregion + } +} \ No newline at end of file