Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / PreferencesViewModel.cs @ 5ce54458

History | View | Annotate | Download (10.4 kB)

1
// -----------------------------------------------------------------------
2
// <copyright file="PreferencesViewModel.cs" company="Microsoft">
3
// TODO: Update copyright text.
4
// </copyright>
5
// -----------------------------------------------------------------------
6

    
7
using System.Collections;
8
using System.ComponentModel.Composition;
9
using System.Diagnostics;
10
using System.IO;
11
using System.IO.IsolatedStorage;
12
using System.Linq.Expressions;
13
using System.Net;
14
using System.Reflection;
15
using System.Runtime.InteropServices;
16
using System.Runtime.Serialization;
17
using System.Windows;
18
using System.Windows.Forms;
19
using Caliburn.Micro;
20
using Hardcodet.Wpf.TaskbarNotification;
21
using Pithos.Client.WPF.Configuration;
22
using Pithos.Core;
23
using Pithos.Interfaces;
24

    
25
using Screen = Caliburn.Micro.Screen;
26

    
27
namespace Pithos.Client.WPF
28
{
29
    using System;
30
    using System.Collections.Generic;
31
    using System.Linq;
32
    using System.Text;
33
    using System.Threading.Tasks;
34

    
35
    /// <summary>
36
    /// TODO: Update summary.
37
    /// </summary>
38
    [Export(typeof(IShell))]
39
    public class PreferencesViewModel : Screen, IShell, IHandle<Notification>
40
    {
41
        private IEventAggregator _events;
42

    
43

    
44
        public PithosSettings Settings { get; set; }
45

    
46

    
47
        public PithosMonitor Monitor { get; private set; }
48

    
49
        private TaskbarViewModel _taskbar;
50
        public TaskbarViewModel Taskbar
51
        {
52
            get { return _taskbar; }
53
            set
54
            {
55
                _taskbar = value;
56
            }
57
        }
58

    
59
        //ShellExtensionController _extensionController=new ShellExtensionController();
60

    
61
        [ImportingConstructor]
62
        public PreferencesViewModel(IEventAggregator events, TaskbarViewModel taskbar, PithosSettings settings, PithosMonitor monitor)
63
        {
64
            _events = events;
65
            _events.Subscribe(this);
66

    
67
            DisplayName = "Pithos Preferences";
68

    
69
            Taskbar=taskbar;
70
            Taskbar.Parent = this;
71
            
72
            Settings=settings;
73
            Monitor=monitor;
74

    
75

    
76
            Taskbar.UsageMessage = "Using 15% of 50 GB";
77
            Taskbar.RecentFiles.AddRange(new[]
78
                                     {
79
                                      new FileEntry{FileName="Moo",FullPath=@"e:\Pithos\moo"}   ,
80
                                      new FileEntry{FileName="Mee",FullPath=@"e:\Pithos\mee"}   
81
                                     });
82
            Taskbar.StatusMessage = "In Synch";            
83
        }
84

    
85
        protected override void OnViewAttached(object view, object context)
86
        {
87
            var window = (Window)view;
88

    
89
            base.OnViewAttached(view, context);
90
        }
91

    
92

    
93
        protected override void OnViewLoaded(object view)
94
        {
95
            var window = (Window)view;
96
            window.Hide();
97
            Taskbar.UpdateStatus();
98
            base.OnViewLoaded(view);
99
        }
100

    
101
        #region Preferences Properties
102

    
103
        private bool _noProxy;
104
        public bool NoProxy
105
        {
106
            get { return _noProxy; }
107
            set
108
            {
109
                _noProxy = value;
110
                NotifyOfPropertyChange(()=>NoProxy);
111
            }
112
        }
113

    
114

    
115
        private bool _defaultProxy;
116

    
117
        public bool DefaultProxy
118
        {
119
            get { return _defaultProxy; }
120
            set
121
            {
122
                _defaultProxy = value;
123
                NotifyOfPropertyChange(() => DefaultProxy);
124
            }
125
        }
126

    
127

    
128
        private bool _manualProxy;
129

    
130
        public bool ManualProxy
131
        {
132
            get { return _manualProxy; }
133
            set
134
            {
135
                _manualProxy = value;
136
                NotifyOfPropertyChange(() => ManualProxy);
137
            }
138
        }
139
        #endregion
140

    
141
       
142
        #region Commands
143
        
144

    
145
    
146
        public void SaveChanges()
147
        {
148
            DoSave();
149
           
150
        }
151

    
152
        public void RejectChanges()
153
        {
154
            Settings.Reload();
155
           
156
        }
157

    
158
        public void ApplyChanges()
159
        {
160
            DoSave();
161
        }
162

    
163
        private void DoSave()
164
        {
165
            Settings.Save();
166
            NotifyOfPropertyChange(()=>Settings);
167

    
168
            var activeAccount = Settings.Accounts.FirstOrDefault(account => account.IsActive);
169
            if (activeAccount == null)
170
                return;
171
            if (String.IsNullOrWhiteSpace(activeAccount.AccountName))
172
                return;
173

    
174
            Monitor.ApiKey = activeAccount.ApiKey;
175
            Monitor.UserName = activeAccount.AccountName;
176
            Monitor.UsePithos = activeAccount.UsePithos;
177

    
178
            Monitor.Start();
179
        }
180

    
181
        public void ChangePithosFolder()
182
        {
183
            var browser = new FolderBrowserDialog();
184
            browser.SelectedPath = Settings.PithosPath;
185
            var result = browser.ShowDialog((IWin32Window)GetView());
186
            if (result == DialogResult.OK)
187
            {
188
                var newPath = browser.SelectedPath;
189
                Directory.Move(Settings.PithosPath, newPath);
190
                Settings.PithosPath = newPath;
191
                Settings.Save();
192
                NotifyOfPropertyChange(() => Settings);
193

    
194
            }
195
        }
196

    
197
       public void AddAccount()
198
        {
199
            var newAccount = new AccountSettings();
200
            Settings.Accounts.Add(newAccount);
201
            SelectedAccountIndex= Settings.Accounts.Count-1;
202
            NotifyOfPropertyChange(()=>Settings);
203
        }
204

    
205
        public void AddPithosAccount()
206
       {
207
           var task=PithosAccount.RetrieveCredentialsAsync(Settings.PithosSite)
208
               .ContinueWith(t=>
209
                   {                       
210
                       var credentials=t.Result;
211
                       var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName);
212
                       if (account == null)
213
                       {
214
                           account=new AccountSettings{
215
                               AccountName=credentials.UserName,
216
                               ApiKey=credentials.Password,
217
                               UsePithos=true
218
                           };
219
                           Settings.Accounts.Add(account);
220
                       }
221
                       else
222
                       {
223
                           account.ApiKey=credentials.Password;
224
                       }
225
                       SelectedAccountIndex= Settings.Accounts.IndexOf(account);
226
                       NotifyOfPropertyChange(()=>Settings);
227
                   });
228
            ((Task)task).WaitWithPumping();
229
       }
230

    
231
        public void RemoveAccount()
232
        {
233
            Settings.Accounts.RemoveAll(account => account.AccountName == CurrentAccount.AccountName);
234
            
235
            NotifyOfPropertyChange(()=>CurrentAccount);
236
            NotifyOfPropertyChange(()=>Settings);
237
            //NotifyOfPropertyChange("Settings.Accounts");
238
        }
239

    
240
        public bool CanRemoveAccount
241
        {
242
            get { return (CurrentAccount != null); }
243
        }
244

    
245

    
246

    
247
        public bool ExtensionsActivated
248
        {
249
            get { return Settings.ExtensionsActivated; }
250
            set
251
            {
252
                if (Settings.ExtensionsActivated == value)
253
                    return;
254

    
255
                Settings.ExtensionsActivated = value;
256

    
257
                //if (value)
258
                //    _extensionController.RegisterExtensions();
259
                //else
260
                //{
261
                //    _extensionController.UnregisterExtensions();
262
                //}
263
                NotifyOfPropertyChange(() => ExtensionsActivated);
264
            }
265
        }
266

    
267
        public void RefreshOverlays()
268
        {
269
            string path=Settings.PithosPath;
270
            if (String.IsNullOrWhiteSpace(path))
271
                throw new ArgumentNullException("path", "The path parameter must not be emtpy");
272

    
273
            if (!Directory.Exists(path) && !File.Exists(path))
274
                throw new FileNotFoundException("The specified file or path does not exist", path);
275

    
276

    
277
            IntPtr pathPointer = Marshal.StringToCoTaskMemAuto(path);
278

    
279
            try
280
            {
281
                NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM,
282
                                             HChangeNotifyFlags.SHCNF_PATHW | HChangeNotifyFlags.SHCNF_FLUSHNOWAIT,
283
                                             pathPointer, IntPtr.Zero);
284
            }
285
            finally
286
            {
287
                Marshal.FreeHGlobal(pathPointer);
288
            }
289

    
290
        }
291
        #endregion
292

    
293
        private int _selectedAccountIndex;
294
        public int SelectedAccountIndex
295
        {
296
            get { return _selectedAccountIndex; }
297
            set
298
            {
299
                //var accountCount=Settings.Accounts.Count;
300
                //if (accountCount == 0)
301
                //    return;
302
                //if (0 <= value && value < accountCount)
303
                //    _selectedAccountIndex = value;
304
                //else
305
                //    _selectedAccountIndex = 0;
306
                _selectedAccountIndex = value;
307
                NotifyOfPropertyChange(() => CurrentAccount);
308
                NotifyOfPropertyChange(() => CanRemoveAccount);
309
                NotifyOfPropertyChange(()=>SelectedAccountIndex);
310
            }
311
        }
312

    
313
        private AccountSettings _currentAccount;
314
        public AccountSettings CurrentAccount
315
        {
316
            get {
317
                if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count)                    
318
                    return Settings.Accounts[SelectedAccountIndex];
319
                return null;
320
            }
321
/*
322
            set
323
            {
324
                _currentAccount = value;
325
            }
326
*/
327
        }
328

    
329

    
330
        public void Handle(Notification notification)
331
        {
332
            if (!Settings.ShowDesktopNotifications)
333
                return;
334
            BalloonIcon icon = BalloonIcon.None;
335
            switch (notification.Level)
336
            {
337
                case TraceLevel.Error:
338
                    icon = BalloonIcon.Error;
339
                    break;
340
                case TraceLevel.Info:
341
                case TraceLevel.Verbose:
342
                    icon = BalloonIcon.Info;
343
                    break;
344
                case TraceLevel.Warning:
345
                    icon = BalloonIcon.Warning;
346
                    break;
347
                default:
348
                    icon = BalloonIcon.None;
349
                    break;
350
            }
351

    
352
            var tv = (PreferencesView)this.GetView();
353
            tv.TaskbarView.ShowBalloonTip(notification.Title, notification.Message, icon);
354
        }
355

    
356
    }
357
}