Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TrayIconBase.cs « Duplicati.GUI.TrayIcon « GUI « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b4788e81310feba11b3ad27109e357498418e09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//  Copyright (C) 2015, The Duplicati Team
//  http://www.duplicati.com, info@duplicati.com
//  
//  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
using System;
using Duplicati.Server.Serialization;
using Duplicati.Server.Serialization.Interface;
using System.Collections.Generic;

namespace Duplicati.GUI.TrayIcon
{
    public enum MenuIcons
    {
        None,
        Status,
        Quit,
        Pause,
        Resume,
    }
    
    public enum TrayIcons
    {
        Idle,
        Paused,
        Running,
        IdleError,
        PausedError,
        RunningError
    }

    public enum WindowIcons
    {
        Regular,
        LogWindow
    }

    public enum NotificationType
    {
        Information,
        Warning,
        Error
    }
    
    public interface IMenuItem
    {
        string Text { set; }
        MenuIcons Icon { set; }
        bool Enabled { set; }
        bool Default { set; }
    }
    
    public abstract class TrayIconBase : IDisposable
    {           
        protected IMenuItem m_pauseMenu;
        protected bool m_stateIsPaused;
        protected Action m_onSingleClick;
        protected Action m_onDoubleClick;
        
        public virtual void Init(string[] args)
        {
            SetMenu(BuildMenu());
            RegisterStatusUpdateCallback();
            RegisterNotificationCallback();
            OnStatusUpdated(Program.Connection.Status);
            m_onDoubleClick = ShowStatusWindow;
            Run(args);
        }
        
        protected abstract void Run(string[] args);

        public void InvokeExit()
        {
            UpdateUIState(() => { this.Exit(); });
        }
        
        protected virtual void UpdateUIState(Action action)
        {
            action();
        }
        
        protected abstract TrayIcons Icon { set; }
        
        protected abstract IMenuItem CreateMenuItem(string text, MenuIcons icon, Action callback, IList<IMenuItem> subitems);
        
        protected abstract void Exit();
        
        protected abstract void SetMenu(IEnumerable<IMenuItem> items);

        protected abstract void NotifyUser(string title, string message, NotificationType type);

        protected virtual void RegisterStatusUpdateCallback()
        {
            Program.Connection.OnStatusUpdated += OnStatusUpdated;
        }

        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);
            });
        }

        public virtual IBrowserWindow ShowUrlInWindow(string url)
        {
            //Fallback is to just show the window in a browser
            Duplicati.Library.Utility.UrlUtillity.OpenURL(url, Program.BrowserCommand);

            return null;
        }

        protected IEnumerable<IMenuItem> BuildMenu() 
        {
            var tmp = CreateMenuItem("Open", MenuIcons.Status, OnStatusClicked, null);
            tmp.Default = true;
            return new IMenuItem[] {
                tmp,
                m_pauseMenu = CreateMenuItem("Pause", MenuIcons.Pause, OnPauseClicked, null ),
                CreateMenuItem("Quit", MenuIcons.Quit, OnQuitClicked, null),
            };
        }
        
        protected void ShowStatusWindow()
        {
            var window = ShowUrlInWindow(Program.Connection.StatusWindowURL);
            if (window != null)
            {
                window.Icon = WindowIcons.Regular;
                window.Title = "Duplicati status";
            }
        }

        protected void OnStatusClicked()
        {
            ShowStatusWindow();
        }

        protected void OnWizardClicked()
        {
        }
        
        protected void OnOptionsClicked()
        {
        }
        
        protected void OnStopClicked()
        {
        }

        protected void OnQuitClicked()
        {
            Exit();
        }

        protected void OnThrottleClicked()
        {
        }

        protected void OnPauseClicked()
        {
            if (m_stateIsPaused)
                Program.Connection.Resume();
            else
                Program.Connection.Pause();
        }
        
        protected void OnStatusUpdated(IServerStatus status)
        {
            this.UpdateUIState(() => {
                switch(status.SuggestedStatusIcon)
                {
                    case SuggestedStatusIcon.Active:
                        Icon = TrayIcons.Running;
                        break;
                    case SuggestedStatusIcon.ActivePaused:
                        Icon = TrayIcons.Paused;
                        break;
                    case SuggestedStatusIcon.ReadyError:
                        Icon = TrayIcons.IdleError;
                        break;
                    case SuggestedStatusIcon.ReadyWarning:
                        Icon = TrayIcons.IdleError;
                        break;
                    case SuggestedStatusIcon.Paused:
                        Icon = TrayIcons.Paused;
                        break;
                    case SuggestedStatusIcon.Ready:
                    default:    
                        Icon = TrayIcons.Idle;
                        break;
                    
                }
    
                if (status.ProgramState == LiveControlState.Running)
                {
                    m_pauseMenu.Icon = MenuIcons.Pause;
                    m_pauseMenu.Text = "Pause";
                    m_stateIsPaused = false;
                }
                else
                {
                    m_pauseMenu.Icon = MenuIcons.Resume;
                    m_pauseMenu.Text = "Resume";
                    m_stateIsPaused = true;
                }
            });
        }

        #region IDisposable implementation
        public abstract void Dispose();
        #endregion
    }
}