Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / PreferencesViewModel.cs @ 9bae55d1

History | View | Annotate | Download (7.7 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.Runtime.Serialization;
13
using System.Windows;
14
using System.Windows.Forms;
15
using Caliburn.Micro;
16
using Pithos.Client.WPF.Configuration;
17
using Pithos.Core;
18
using Pithos.Interfaces;
19
using Pithos.ShellExtensions;
20
using Screen = Caliburn.Micro.Screen;
21

    
22
namespace Pithos.Client.WPF
23
{
24
    using System;
25
    using System.Collections.Generic;
26
    using System.Linq;
27
    using System.Text;
28

    
29
    /// <summary>
30
    /// TODO: Update summary.
31
    /// </summary>
32
    [Export(typeof(IShell))]
33
    public class PreferencesViewModel : Screen, IShell
34
    {
35
        
36
        private IStatusChecker _statusChecker;
37

    
38

    
39
        public IPithosSettings Settings { get; set; }
40

    
41

    
42
        public PithosMonitor Monitor { get; private set; }
43

    
44

    
45
        ShellExtensionController _extensionController=new ShellExtensionController();
46

    
47
        [ImportingConstructor]
48
        public PreferencesViewModel(IStatusChecker statusChecker, IPithosSettings settings, PithosMonitor monitor)
49
        {
50
            DisplayName = "Pithos Preferences";
51
            _statusChecker = statusChecker;
52
            Settings=settings;
53
            Monitor=monitor;
54

    
55
            OpenPithosFolderCommand=new PithosCommand(OpenPithosFolder);
56

    
57
            
58

    
59
            UsageMessage = "Using 15% of 50 GB";
60
            RecentFiles.AddRange(new[]
61
                                     {
62
                                      new FileEntry{FileName="Moo",FullPath=@"e:\Pithos\moo"}   ,
63
                                      new FileEntry{FileName="Mee",FullPath=@"e:\Pithos\mee"}   
64
                                     });
65
            StatusMessage = "In Synch";
66
            UpdateStatus();
67
        }
68
        protected override void OnViewAttached(object view, object context)
69
        {
70
            var window = (Window)view;
71
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
72
            window.ShowInTaskbar = false;
73

    
74
            base.OnViewAttached(view, context);
75
        }
76

    
77

    
78
        protected override void OnViewLoaded(object view)
79
        {
80
            var window = (Window)view;
81
            window.Hide();
82

    
83
            base.OnViewLoaded(view);
84
        }
85

    
86

    
87
        #region Status Properties
88

    
89
        private string _statusMessage;
90
        public string StatusMessage
91
        {
92
            get { return _statusMessage; }
93
            set
94
            {
95
                _statusMessage = value;
96
                NotifyOfPropertyChange(() => StatusMessage);
97
            }
98
        }
99

    
100
        private string _usageMessage;
101
        public string UsageMessage
102
        {
103
            get { return _usageMessage; }
104
            set
105
            {
106
                _usageMessage = value;
107
                NotifyOfPropertyChange(() => UsageMessage);
108
            }
109
        }
110

    
111
        private readonly IObservableCollection<FileEntry> _recentFiles = new BindableCollection<FileEntry>();
112
        public IObservableCollection<FileEntry> RecentFiles
113
        {
114
            get { return _recentFiles; }
115
        }
116

    
117

    
118
        private string _statusIcon;
119
        public string StatusIcon
120
        {
121
            get { return _statusIcon; }
122
            set
123
            {
124
                _statusIcon = value;
125
                NotifyOfPropertyChange(()=>StatusIcon);
126
            }
127
        }
128

    
129
        #endregion
130

    
131
        //public PithosSettings Settings { get; set; }
132

    
133
        #region Commands
134
        
135

    
136
        public void ShowPreferences()
137
        {
138
            Settings.Reload();
139

    
140
            var window = (Window)this.GetView();
141
            window.Show();
142
        }
143

    
144

    
145
        public PithosCommand OpenPithosFolderCommand { get; private set; }
146

    
147
        public void OpenPithosFolder()
148
        {
149
            Process.Start(Settings.PithosPath);
150
        }
151

    
152
        public void GoToSite()
153
        {
154

    
155
        }
156

    
157
        public void ToggleSynching()
158
        {
159

    
160
        }
161

    
162
        public void SaveChanges()
163
        {
164
            DoSave();
165
            /*var window = (Window)GetView();
166
            window.Hide();*/
167
        }
168

    
169
        public void RejectChanges()
170
        {
171
            Settings.Reload();
172
           /* var window = (Window)GetView();
173
            window.Hide();*/
174
        }
175

    
176
        public void ApplyChanges()
177
        {
178
            DoSave();
179
        }
180

    
181
        private void DoSave()
182
        {
183
            Settings.Save();
184
            Monitor.Start();
185
        }
186

    
187
        public void ChangePithosFolder()
188
        {
189
            var browser = new FolderBrowserDialog();
190
            browser.SelectedPath = Settings.PithosPath;
191
            var result = browser.ShowDialog((IWin32Window)GetView());
192
            if (result == DialogResult.OK)
193
            {
194
                var newPath = browser.SelectedPath;
195
                Directory.Move(Settings.PithosPath, newPath);
196
                Settings.PithosPath = newPath;
197
                Settings.Save();
198
                NotifyOfPropertyChange(() => Settings);
199
            }
200
        }
201

    
202
        public void ExitPithos()
203
        {
204
            Monitor.Stop();
205
            this.TryClose();            
206
        }
207

    
208
        #endregion
209

    
210
        private AccountSettings _currentAccount;
211
        public AccountSettings CurrentAccount
212
        {
213
            get { return _currentAccount; }
214
            set
215
            {
216
                _currentAccount = value;
217
                NotifyOfPropertyChange(()=>CurrentAccount);
218
            }
219
        }
220

    
221

    
222
        private Dictionary<PithosStatus, StatusInfo> iconNames = new List<StatusInfo>
223
            {
224
                new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"),
225
                new StatusInfo(PithosStatus.Synching, "Synching Files", "TraySynching")
226
            }.ToDictionary(s => s.Status);
227

    
228
        public void UpdateStatus()
229
        {
230
            var pithosStatus = _statusChecker.GetPithosStatus();
231

    
232

    
233
            if (iconNames.ContainsKey(pithosStatus))
234
            {
235
                var info = iconNames[pithosStatus];
236
                StatusIcon = String.Format(@"Images/{0}.ico",info.IconName);
237
                StatusMessage= String.Format("Pithos 1.0\r\n{0}", info.StatusText);                
238
            }
239
            if (!String.IsNullOrWhiteSpace(Settings.UserName) &&
240
                !String.IsNullOrWhiteSpace(Settings.ApiKey))
241
                Monitor.Start();
242
        }
243

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

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

    
265
        /* private void UnregisterExtensions()
266
        {
267
            using (var installer = new ProjectInstaller())
268
            {
269
                IDictionary state = ShellExtensionController.LoadState();
270
                installer.Uninstall(state);
271
            }
272
        }
273

    
274
        private void RegisterExtensions()
275
        {
276
            using (var installer = new ProjectInstaller())
277
            {
278
                IDictionary state = new Dictionary<object, object>();
279
                installer.Install(state);
280
                ShellExtensionController.SaveState(state);
281

    
282
            }
283
        }*/
284
    }
285
}