Changed the retry function in PithosClient to use the TPL
[pithos-ms-client] / trunk / Pithos.Client.WPF / TaskbarViewModel.cs
1 // -----------------------------------------------------------------------
2 // <copyright file="StatusViewModel.cs" company="Microsoft">
3 // TODO: Update copyright text.
4 // </copyright>
5 // -----------------------------------------------------------------------
6
7 using System.ComponentModel.Composition;
8 using System.Diagnostics;
9 using System.IO;
10 using System.Windows;
11 using Caliburn.Micro;
12 using Pithos.Client.WPF.Properties;
13 using Pithos.Core;
14 using Pithos.Interfaces;
15
16 namespace Pithos.Client.WPF
17 {
18     using System;
19     using System.Collections.Generic;
20     using System.Linq;
21     using System.Text;
22
23     /// <summary>
24     /// TODO: Update summary.
25     /// </summary>
26     [Export]
27     public class TaskbarViewModel:ViewAware
28     {
29         private IStatusChecker _statusChecker;
30
31         public PithosMonitor Monitor { get; private set; }
32
33         public IPithosSettings Settings { get; private set; }
34
35         public IScreen Parent { get; set; }
36
37         [ImportingConstructor]
38         public TaskbarViewModel(IStatusChecker statusChecker,PithosMonitor monitor,IPithosSettings settings)
39         {
40             OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder);
41             _statusChecker = statusChecker;
42             Settings = settings;
43             Monitor = monitor;
44
45             var account=settings.Accounts.FirstOrDefault(act => act.IsActive);
46             if (account != null)
47             {
48                 Monitor.UserName = account.AccountName;
49                 Monitor.ApiKey = account.ApiKey;
50                 Monitor.UsePithos = account.UsePithos;
51                 var appSettings = Properties.Settings.Default;
52                 Monitor.AuthenticationUrl = account.UsePithos
53                                                 ? appSettings.PithosAuthenticationUrl
54                                                 : appSettings.CloudfilesAuthenticationUrl;
55                 Monitor.RootPath = Path.Combine(Settings.PithosPath, account.RootPath??"");
56             }
57
58         }
59         #region Status Properties
60
61         private string _statusMessage;
62         public string StatusMessage
63         {
64             get { return _statusMessage; }
65             set
66             {
67                 _statusMessage = value;
68                 NotifyOfPropertyChange(() => StatusMessage);
69             }
70         }
71
72         private string _usageMessage;
73         public string UsageMessage
74         {
75             get { return _usageMessage; }
76             set
77             {
78                 _usageMessage = value;
79                 NotifyOfPropertyChange(() => UsageMessage);
80             }
81         }
82
83
84         private string _pauseSyncCaption="Pause Syncing";
85         public string PauseSyncCaption
86         {
87             get { return _pauseSyncCaption; }
88             set
89             {
90                 _pauseSyncCaption = value;
91                 NotifyOfPropertyChange(() => PauseSyncCaption);
92             }
93         }
94
95         private readonly IObservableCollection<FileEntry> _recentFiles = new BindableCollection<FileEntry>();
96         public IObservableCollection<FileEntry> RecentFiles
97         {
98             get { return _recentFiles; }
99         }
100
101
102         private string _statusIcon;
103         public string StatusIcon
104         {
105             get { return _statusIcon; }
106             set
107             {
108                 _statusIcon = value;
109                 NotifyOfPropertyChange(() => StatusIcon);
110             }
111         }
112
113         #endregion
114
115         #region Commands
116
117         public void ShowPreferences()
118         {
119             Settings.Reload();
120         }
121
122
123         public PithosCommand OpenPithosFolderCommand { get; private set; }
124
125         public void OpenPithosFolder()
126         {
127             Process.Start(Settings.PithosPath);
128         }
129
130         public void GoToSite()
131         {
132             Process.Start(Properties.Settings.Default.PithosSite);
133         }
134
135
136         public void ToggleSynching()
137         {
138             Monitor.Pause = !Monitor.Pause;
139             PauseSyncCaption = Monitor.Pause ? "Resume syncing" : "Pause syncing";
140             var iconKey = Monitor.Pause ? "TraySyncPaused" : "TrayInSynch";
141             StatusIcon = String.Format(@"Images/{0}.ico", iconKey);
142         }
143
144         public void ExitPithos()
145         {
146             Monitor.Stop();
147             Parent.TryClose();
148         }
149         #endregion
150
151
152         private Dictionary<PithosStatus, StatusInfo> iconNames = new List<StatusInfo>
153             {
154                 new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"),
155                 new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching"),
156                 new StatusInfo(PithosStatus.SyncPaused, "Sync Paused", "TraySyncPaused")
157             }.ToDictionary(s => s.Status);
158
159         
160         public void UpdateStatus()
161         {
162             var pithosStatus = _statusChecker.GetPithosStatus();
163
164             if (iconNames.ContainsKey(pithosStatus))
165             {
166                 var info = iconNames[pithosStatus];
167                 StatusIcon = String.Format(@"Images/{0}.ico", info.IconName);
168                 StatusMessage = String.Format("Pithos 1.0\r\n{0}", info.StatusText);
169             }
170             if (!String.IsNullOrWhiteSpace(Monitor.UserName) &&
171                 !String.IsNullOrWhiteSpace(Monitor.ApiKey))
172                 try
173                 {
174                     Monitor.Start();
175                 }
176                 catch (Exception exc)
177                 {
178                     MessageBox.Show("An exception occured. Can't start monitoring");
179                 }
180         }
181     }
182 }