// -----------------------------------------------------------------------
//
// TODO: Update copyright text.
//
// -----------------------------------------------------------------------
using System.Collections;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Forms;
using Caliburn.Micro;
using Hardcodet.Wpf.TaskbarNotification;
using Pithos.Client.WPF.Configuration;
using Pithos.Core;
using Pithos.Interfaces;
using Screen = Caliburn.Micro.Screen;
namespace Pithos.Client.WPF
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
///
/// TODO: Update summary.
///
[Export(typeof(IShell))]
public class PreferencesViewModel : Screen, IShell, IHandle
{
private IEventAggregator _events;
public PithosSettings Settings { get; set; }
public PithosMonitor Monitor { get; private set; }
private TaskbarViewModel _taskbar;
public TaskbarViewModel Taskbar
{
get { return _taskbar; }
set
{
_taskbar = value;
}
}
//ShellExtensionController _extensionController=new ShellExtensionController();
[ImportingConstructor]
public PreferencesViewModel(IEventAggregator events, TaskbarViewModel taskbar, PithosSettings settings, PithosMonitor monitor)
{
_events = events;
_events.Subscribe(this);
DisplayName = "Pithos Preferences";
Taskbar=taskbar;
Taskbar.Parent = this;
Settings=settings;
Monitor=monitor;
Taskbar.UsageMessage = "Using 15% of 50 GB";
Taskbar.StatusMessage = "In Synch";
}
protected override void OnViewAttached(object view, object context)
{
var window = (Window)view;
base.OnViewAttached(view, context);
}
protected override void OnViewLoaded(object view)
{
var window = (Window)view;
window.Hide();
Taskbar.UpdateStatus();
base.OnViewLoaded(view);
}
#region Preferences Properties
private bool _noProxy;
public bool NoProxy
{
get { return _noProxy; }
set
{
_noProxy = value;
NotifyOfPropertyChange(()=>NoProxy);
}
}
private bool _defaultProxy;
public bool DefaultProxy
{
get { return _defaultProxy; }
set
{
_defaultProxy = value;
NotifyOfPropertyChange(() => DefaultProxy);
}
}
private bool _manualProxy;
public bool ManualProxy
{
get { return _manualProxy; }
set
{
_manualProxy = value;
NotifyOfPropertyChange(() => ManualProxy);
}
}
#endregion
#region Commands
public void SaveChanges()
{
DoSave();
}
public void RejectChanges()
{
Settings.Reload();
}
public void ApplyChanges()
{
DoSave();
}
private void DoSave()
{
Settings.Save();
NotifyOfPropertyChange(()=>Settings);
var activeAccount = Settings.Accounts.FirstOrDefault(account => account.IsActive);
if (activeAccount == null)
return;
if (String.IsNullOrWhiteSpace(activeAccount.AccountName))
return;
Monitor.ApiKey = activeAccount.ApiKey;
Monitor.UserName = activeAccount.AccountName;
Monitor.UsePithos = activeAccount.UsePithos;
Monitor.Start();
}
public void ChangePithosFolder()
{
var browser = new FolderBrowserDialog();
browser.SelectedPath = Settings.PithosPath;
var result = browser.ShowDialog((IWin32Window)GetView());
if (result == DialogResult.OK)
{
var newPath = browser.SelectedPath;
Directory.Move(Settings.PithosPath, newPath);
Settings.PithosPath = newPath;
Settings.Save();
NotifyOfPropertyChange(() => Settings);
}
}
public void AddAccount()
{
var newAccount = new AccountSettings();
Settings.Accounts.Add(newAccount);
SelectedAccountIndex= Settings.Accounts.Count-1;
NotifyOfPropertyChange(()=>Settings);
}
public void AddPithosAccount()
{
var task=PithosAccount.RetrieveCredentialsAsync(Settings.PithosSite)
.ContinueWith(t=>
{
var credentials=t.Result;
var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName);
if (account == null)
{
account=new AccountSettings{
AccountName=credentials.UserName,
ApiKey=credentials.Password,
UsePithos=true
};
Settings.Accounts.Add(account);
}
else
{
account.ApiKey=credentials.Password;
}
SelectedAccountIndex= Settings.Accounts.IndexOf(account);
NotifyOfPropertyChange(()=>Settings);
});
((Task)task).WaitWithPumping();
}
public void RemoveAccount()
{
Settings.Accounts.RemoveAll(account => account.AccountName == CurrentAccount.AccountName);
NotifyOfPropertyChange(()=>CurrentAccount);
NotifyOfPropertyChange(()=>Settings);
//NotifyOfPropertyChange("Settings.Accounts");
}
public bool CanRemoveAccount
{
get { return (CurrentAccount != null); }
}
public bool ExtensionsActivated
{
get { return Settings.ExtensionsActivated; }
set
{
if (Settings.ExtensionsActivated == value)
return;
Settings.ExtensionsActivated = value;
//if (value)
// _extensionController.RegisterExtensions();
//else
//{
// _extensionController.UnregisterExtensions();
//}
NotifyOfPropertyChange(() => ExtensionsActivated);
}
}
public void RefreshOverlays()
{
string path=Settings.PithosPath;
if (String.IsNullOrWhiteSpace(path))
throw new ArgumentNullException("path", "The path parameter must not be emtpy");
if (!Directory.Exists(path) && !File.Exists(path))
throw new FileNotFoundException("The specified file or path does not exist", path);
IntPtr pathPointer = Marshal.StringToCoTaskMemAuto(path);
try
{
NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM,
HChangeNotifyFlags.SHCNF_PATHW | HChangeNotifyFlags.SHCNF_FLUSHNOWAIT,
pathPointer, IntPtr.Zero);
}
finally
{
Marshal.FreeHGlobal(pathPointer);
}
}
#endregion
private int _selectedAccountIndex;
public int SelectedAccountIndex
{
get { return _selectedAccountIndex; }
set
{
//var accountCount=Settings.Accounts.Count;
//if (accountCount == 0)
// return;
//if (0 <= value && value < accountCount)
// _selectedAccountIndex = value;
//else
// _selectedAccountIndex = 0;
_selectedAccountIndex = value;
NotifyOfPropertyChange(() => CurrentAccount);
NotifyOfPropertyChange(() => CanRemoveAccount);
NotifyOfPropertyChange(()=>SelectedAccountIndex);
}
}
private AccountSettings _currentAccount;
public AccountSettings CurrentAccount
{
get {
if (0 <= SelectedAccountIndex && SelectedAccountIndex < Settings.Accounts.Count)
return Settings.Accounts[SelectedAccountIndex];
return null;
}
/*
set
{
_currentAccount = value;
}
*/
}
public void Handle(Notification notification)
{
if (!Settings.ShowDesktopNotifications)
return;
BalloonIcon icon = BalloonIcon.None;
switch (notification.Level)
{
case TraceLevel.Error:
icon = BalloonIcon.Error;
break;
case TraceLevel.Info:
case TraceLevel.Verbose:
icon = BalloonIcon.Info;
break;
case TraceLevel.Warning:
icon = BalloonIcon.Warning;
break;
default:
icon = BalloonIcon.None;
break;
}
var tv = (PreferencesView)this.GetView();
tv.TaskbarView.ShowBalloonTip(notification.Title, notification.Message, icon);
}
}
}