Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / TaskbarViewModel.cs @ 0c02aa65

History | View | Annotate | Download (6.4 kB)

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.Threading.Tasks;
11
using System.Windows;
12
using Caliburn.Micro;
13
using Pithos.Client.WPF.Properties;
14
using Pithos.Core;
15
using Pithos.Interfaces;
16

    
17
namespace Pithos.Client.WPF
18
{
19
    using System;
20
    using System.Collections.Generic;
21
    using System.Linq;
22
    using System.Text;
23

    
24
    /// <summary>
25
    /// TODO: Update summary.
26
    /// </summary>
27
    [Export]
28
    public class TaskbarViewModel:ViewAware,IStatusNotification
29
    {
30
        private IStatusChecker _statusChecker;
31
        private IEventAggregator _events;
32

    
33
        public PithosMonitor Monitor { get; private set; }
34

    
35
        public IPithosSettings Settings { get; private set; }
36

    
37
        public IScreen Parent { get; set; }
38

    
39
        [ImportingConstructor]
40
        public TaskbarViewModel(IEventAggregator events, IStatusChecker statusChecker,PithosMonitor monitor,IPithosSettings settings)
41
        {
42
            OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder);
43
            _statusChecker = statusChecker;
44
            _events = events;            
45
            Settings = settings;
46
            Monitor = monitor;
47
            Monitor.StatusNotification = this;
48

    
49
            
50

    
51
            var account=settings.Accounts.FirstOrDefault(act => act.IsActive);
52
            if (account != null)
53
            {
54
                Monitor.UserName = account.AccountName;
55
                Monitor.ApiKey = account.ApiKey;
56
                Monitor.UsePithos = account.UsePithos;
57
                var appSettings = Properties.Settings.Default;
58
                Monitor.AuthenticationUrl = account.UsePithos
59
                                                ? appSettings.PithosAuthenticationUrl
60
                                                : appSettings.CloudfilesAuthenticationUrl;
61
                Monitor.RootPath = Path.Combine(Settings.PithosPath, account.RootPath??"");
62
            }
63

    
64
        }
65
        #region Status Properties
66

    
67
        private string _statusMessage;
68
        public string StatusMessage
69
        {
70
            get { return _statusMessage; }
71
            set
72
            {
73
                _statusMessage = value;
74
                NotifyOfPropertyChange(() => StatusMessage);
75
            }
76
        }
77

    
78
        private string _usageMessage;
79
        public string UsageMessage
80
        {
81
            get { return _usageMessage; }
82
            set
83
            {
84
                _usageMessage = value;
85
                NotifyOfPropertyChange(() => UsageMessage);
86
            }
87
        }
88

    
89

    
90
        private string _pauseSyncCaption="Pause Syncing";
91
        public string PauseSyncCaption
92
        {
93
            get { return _pauseSyncCaption; }
94
            set
95
            {
96
                _pauseSyncCaption = value;
97
                NotifyOfPropertyChange(() => PauseSyncCaption);
98
            }
99
        }
100

    
101
        private readonly IObservableCollection<FileEntry> _recentFiles = new BindableCollection<FileEntry>();
102
        public IObservableCollection<FileEntry> RecentFiles
103
        {
104
            get { return _recentFiles; }
105
        }
106

    
107

    
108
        private string _statusIcon;
109
        public string StatusIcon
110
        {
111
            get { return _statusIcon; }
112
            set
113
            {
114
                _statusIcon = value;
115
                NotifyOfPropertyChange(() => StatusIcon);
116
            }
117
        }
118

    
119
        #endregion
120

    
121
        #region Commands
122

    
123
        public void ShowPreferences()
124
        {
125
            Settings.Reload();
126
        }
127

    
128

    
129
        public PithosCommand OpenPithosFolderCommand { get; private set; }
130

    
131
        public void OpenPithosFolder()
132
        {
133
            Process.Start(Settings.PithosPath);
134
        }
135

    
136
        public void GoToSite()
137
        {
138
            Process.Start(Properties.Settings.Default.PithosSite);
139
        }
140

    
141

    
142
        public void ToggleSynching()
143
        {
144
            Monitor.Pause = !Monitor.Pause;
145
            PauseSyncCaption = Monitor.Pause ? "Resume syncing" : "Pause syncing";
146
            var iconKey = Monitor.Pause ? "TraySyncPaused" : "TrayInSynch";
147
            StatusIcon = String.Format(@"Images/{0}.ico", iconKey);
148
        }
149

    
150
        public void ExitPithos()
151
        {
152
            Monitor.Stop();
153
            Parent.TryClose();
154
        }
155
        #endregion
156

    
157

    
158
        private Dictionary<PithosStatus, StatusInfo> iconNames = new List<StatusInfo>
159
            {
160
                new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"),
161
                new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching"),
162
                new StatusInfo(PithosStatus.SyncPaused, "Sync Paused", "TraySyncPaused")
163
            }.ToDictionary(s => s.Status);
164

    
165
        
166
        public void UpdateStatus()
167
        {
168
            var pithosStatus = _statusChecker.GetPithosStatus();
169

    
170
            if (iconNames.ContainsKey(pithosStatus))
171
            {
172
                var info = iconNames[pithosStatus];
173
                StatusIcon = String.Format(@"Images/{0}.ico", info.IconName);
174
                StatusMessage = String.Format("Pithos 1.0\r\n{0}", info.StatusText);
175
            }
176

    
177
            var tv=this.GetView();
178
            _events.Publish(new Notification { Title = "Start", Message = "Start Monitoring", Level = TraceLevel.Info});
179
            if (!String.IsNullOrWhiteSpace(Monitor.UserName) &&
180
                !String.IsNullOrWhiteSpace(Monitor.ApiKey))
181
                Task.Factory.StartNew(() => 
182
                    Monitor.Start())
183
                    .ContinueWith(t =>{
184
                        if (t.IsFaulted)
185
                        {
186
                            
187
                            var message= String.Format("An exception occured. Can't start monitoring\n{0}",t.Exception);
188
                            _events.Publish(new Notification { Title = "Error",Message=message,Level= TraceLevel.Error });
189
                            MessageBox.Show(message);
190
                        }
191
                    });                
192
        }
193

    
194
       
195

    
196
        public void NotifyChange(string status, TraceLevel level=TraceLevel.Info)
197
        {
198
            this.StatusMessage = status;
199
            
200
            _events.Publish(new Notification { Title = "Pithos", Message = status, Level = level });
201
        }
202
    }
203
}