2015-01-20 21:44:52 +01:00
|
|
|
// Copyright (C) 2015, The Duplicati Team
|
|
|
|
|
// http://www.duplicati.com, info@duplicati.com
|
2012-03-29 18:02:07 +00:00
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2011-11-28 11:05:42 +00:00
|
|
|
using System;
|
|
|
|
|
using Duplicati.Server.Serialization;
|
2013-11-23 14:26:36 +01:00
|
|
|
using Duplicati.Server.Serialization.Interface;
|
2012-03-29 18:02:07 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Duplicati.GUI.TrayIcon
|
2011-11-28 11:05:42 +00:00
|
|
|
{
|
|
|
|
|
public enum MenuIcons
|
|
|
|
|
{
|
|
|
|
|
None,
|
|
|
|
|
Status,
|
|
|
|
|
Quit,
|
|
|
|
|
Pause,
|
|
|
|
|
Resume,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum TrayIcons
|
|
|
|
|
{
|
|
|
|
|
Idle,
|
|
|
|
|
Paused,
|
|
|
|
|
Running,
|
2019-10-07 21:14:24 -07:00
|
|
|
IdleWarning,
|
2011-11-28 11:05:42 +00:00
|
|
|
IdleError,
|
|
|
|
|
PausedError,
|
|
|
|
|
RunningError
|
|
|
|
|
}
|
2012-03-29 18:02:07 +00:00
|
|
|
|
|
|
|
|
public enum WindowIcons
|
|
|
|
|
{
|
|
|
|
|
Regular,
|
|
|
|
|
LogWindow
|
|
|
|
|
}
|
2014-08-04 22:16:15 +02:00
|
|
|
|
|
|
|
|
public enum NotificationType
|
|
|
|
|
{
|
|
|
|
|
Information,
|
|
|
|
|
Warning,
|
|
|
|
|
Error
|
|
|
|
|
}
|
2011-11-28 11:05:42 +00:00
|
|
|
|
|
|
|
|
public interface IMenuItem
|
|
|
|
|
{
|
2018-12-08 11:03:38 -08:00
|
|
|
void SetDefault(bool isDefault);
|
2018-12-08 12:28:06 -08:00
|
|
|
void SetIcon(MenuIcons icon);
|
2018-12-08 10:49:22 -08:00
|
|
|
void SetText(string text);
|
2011-11-28 11:05:42 +00:00
|
|
|
}
|
2012-03-29 18:02:07 +00:00
|
|
|
|
|
|
|
|
public abstract class TrayIconBase : IDisposable
|
2011-11-28 11:05:42 +00:00
|
|
|
{
|
|
|
|
|
protected IMenuItem m_pauseMenu;
|
|
|
|
|
protected bool m_stateIsPaused;
|
2012-07-02 18:07:36 +00:00
|
|
|
protected Action m_onSingleClick;
|
|
|
|
|
protected Action m_onDoubleClick;
|
2019-04-06 09:29:10 -04:00
|
|
|
protected Action m_onNotificationClick;
|
2011-11-28 11:05:42 +00:00
|
|
|
|
|
|
|
|
public virtual void Init(string[] args)
|
|
|
|
|
{
|
|
|
|
|
SetMenu(BuildMenu());
|
|
|
|
|
RegisterStatusUpdateCallback();
|
2014-08-04 22:16:15 +02:00
|
|
|
RegisterNotificationCallback();
|
2011-11-28 11:05:42 +00:00
|
|
|
OnStatusUpdated(Program.Connection.Status);
|
2012-07-02 18:07:36 +00:00
|
|
|
m_onDoubleClick = ShowStatusWindow;
|
2019-04-06 08:47:05 -04:00
|
|
|
m_onNotificationClick = ShowStatusWindow;
|
2011-11-28 11:05:42 +00:00
|
|
|
Run(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void Run(string[] args);
|
2014-07-02 12:05:09 +02:00
|
|
|
|
|
|
|
|
public void InvokeExit()
|
|
|
|
|
{
|
|
|
|
|
UpdateUIState(() => { this.Exit(); });
|
|
|
|
|
}
|
2014-04-09 15:59:08 +02:00
|
|
|
|
|
|
|
|
protected virtual void UpdateUIState(Action action)
|
|
|
|
|
{
|
|
|
|
|
action();
|
|
|
|
|
}
|
2011-11-28 11:05:42 +00:00
|
|
|
|
|
|
|
|
protected abstract IMenuItem CreateMenuItem(string text, MenuIcons icon, Action callback, IList<IMenuItem> subitems);
|
|
|
|
|
|
|
|
|
|
protected abstract void Exit();
|
2018-12-08 10:27:07 -08:00
|
|
|
|
|
|
|
|
protected abstract void SetIcon(TrayIcons icon);
|
2011-11-28 11:05:42 +00:00
|
|
|
|
|
|
|
|
protected abstract void SetMenu(IEnumerable<IMenuItem> items);
|
2014-08-04 22:16:15 +02:00
|
|
|
|
|
|
|
|
protected abstract void NotifyUser(string title, string message, NotificationType type);
|
|
|
|
|
|
2011-11-28 11:05:42 +00:00
|
|
|
protected virtual void RegisterStatusUpdateCallback()
|
|
|
|
|
{
|
2014-08-04 20:38:43 +02:00
|
|
|
Program.Connection.OnStatusUpdated += OnStatusUpdated;
|
2011-11-28 11:05:42 +00:00
|
|
|
}
|
2012-03-29 18:02:07 +00:00
|
|
|
|
2014-08-04 22:16:15 +02:00
|
|
|
protected virtual void RegisterNotificationCallback()
|
|
|
|
|
{
|
|
|
|
|
Program.Connection.OnNotification += OnNotification;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void OnNotification(INotification notification)
|
|
|
|
|
{
|
|
|
|
|
var type = NotificationType.Information;
|
|
|
|
|
switch (notification.Type)
|
|
|
|
|
{
|
|
|
|
|
case Server.Serialization.NotificationType.Information:
|
|
|
|
|
type = NotificationType.Information;
|
|
|
|
|
break;
|
|
|
|
|
case Server.Serialization.NotificationType.Warning:
|
|
|
|
|
type = NotificationType.Warning;
|
|
|
|
|
break;
|
|
|
|
|
case Server.Serialization.NotificationType.Error:
|
|
|
|
|
type = NotificationType.Error;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateUIState(() => {
|
|
|
|
|
NotifyUser(notification.Title, notification.Message, type);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-29 18:02:07 +00:00
|
|
|
public virtual IBrowserWindow ShowUrlInWindow(string url)
|
|
|
|
|
{
|
|
|
|
|
//Fallback is to just show the window in a browser
|
2018-05-11 19:09:14 +02:00
|
|
|
Duplicati.Library.Utility.UrlUtility.OpenURL(url, Program.BrowserCommand);
|
2012-03-29 18:02:07 +00:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-28 11:05:42 +00:00
|
|
|
protected IEnumerable<IMenuItem> BuildMenu()
|
|
|
|
|
{
|
2012-07-02 18:07:36 +00:00
|
|
|
var tmp = CreateMenuItem("Open", MenuIcons.Status, OnStatusClicked, null);
|
2018-12-08 11:03:38 -08:00
|
|
|
tmp.SetDefault(true);
|
2011-11-28 11:05:42 +00:00
|
|
|
return new IMenuItem[] {
|
2012-07-02 18:07:36 +00:00
|
|
|
tmp,
|
|
|
|
|
m_pauseMenu = CreateMenuItem("Pause", MenuIcons.Pause, OnPauseClicked, null ),
|
2011-11-28 11:05:42 +00:00
|
|
|
CreateMenuItem("Quit", MenuIcons.Quit, OnQuitClicked, null),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-29 18:02:07 +00:00
|
|
|
protected void ShowStatusWindow()
|
|
|
|
|
{
|
|
|
|
|
var window = ShowUrlInWindow(Program.Connection.StatusWindowURL);
|
|
|
|
|
if (window != null)
|
|
|
|
|
{
|
2018-12-08 20:35:07 -08:00
|
|
|
window.SetIcon(WindowIcons.Regular);
|
2018-12-08 20:33:55 -08:00
|
|
|
window.SetTitle("Duplicati status");
|
2012-03-29 18:02:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-28 11:05:42 +00:00
|
|
|
protected void OnStatusClicked()
|
|
|
|
|
{
|
2012-03-29 18:02:07 +00:00
|
|
|
ShowStatusWindow();
|
2011-11-28 11:05:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void OnQuitClicked()
|
|
|
|
|
{
|
|
|
|
|
Exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void OnPauseClicked()
|
|
|
|
|
{
|
|
|
|
|
if (m_stateIsPaused)
|
|
|
|
|
Program.Connection.Resume();
|
|
|
|
|
else
|
|
|
|
|
Program.Connection.Pause();
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-05 20:13:33 +00:00
|
|
|
protected void OnStatusUpdated(IServerStatus status)
|
2011-11-28 11:05:42 +00:00
|
|
|
{
|
2014-04-09 15:59:08 +02:00
|
|
|
this.UpdateUIState(() => {
|
|
|
|
|
switch(status.SuggestedStatusIcon)
|
|
|
|
|
{
|
|
|
|
|
case SuggestedStatusIcon.Active:
|
2018-12-08 10:27:07 -08:00
|
|
|
this.SetIcon(TrayIcons.Running);
|
2014-04-09 15:59:08 +02:00
|
|
|
break;
|
|
|
|
|
case SuggestedStatusIcon.ActivePaused:
|
2018-12-08 10:27:07 -08:00
|
|
|
this.SetIcon(TrayIcons.Paused);
|
2014-04-09 15:59:08 +02:00
|
|
|
break;
|
|
|
|
|
case SuggestedStatusIcon.ReadyError:
|
2018-12-08 10:27:07 -08:00
|
|
|
this.SetIcon(TrayIcons.IdleError);
|
2014-04-09 15:59:08 +02:00
|
|
|
break;
|
|
|
|
|
case SuggestedStatusIcon.ReadyWarning:
|
2019-10-07 21:14:24 -07:00
|
|
|
this.SetIcon(TrayIcons.IdleWarning);
|
2014-04-09 15:59:08 +02:00
|
|
|
break;
|
|
|
|
|
case SuggestedStatusIcon.Paused:
|
2018-12-08 10:27:07 -08:00
|
|
|
this.SetIcon(TrayIcons.Paused);
|
2014-04-09 15:59:08 +02:00
|
|
|
break;
|
|
|
|
|
case SuggestedStatusIcon.Ready:
|
|
|
|
|
default:
|
2018-12-08 10:27:07 -08:00
|
|
|
this.SetIcon(TrayIcons.Idle);
|
2014-04-09 15:59:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status.ProgramState == LiveControlState.Running)
|
|
|
|
|
{
|
2018-12-08 12:28:06 -08:00
|
|
|
m_pauseMenu.SetIcon(MenuIcons.Pause);
|
2018-12-08 10:49:22 -08:00
|
|
|
m_pauseMenu.SetText("Pause");
|
2014-04-09 15:59:08 +02:00
|
|
|
m_stateIsPaused = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-08 12:28:06 -08:00
|
|
|
m_pauseMenu.SetIcon(MenuIcons.Resume);
|
2018-12-08 10:49:22 -08:00
|
|
|
m_pauseMenu.SetText("Resume");
|
2014-04-09 15:59:08 +02:00
|
|
|
m_stateIsPaused = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2011-11-28 11:05:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IDisposable implementation
|
|
|
|
|
public abstract void Dispose();
|
|
|
|
|
#endregion
|
2012-03-29 18:02:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|