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

CocoaRunner.cs « Duplicati.GUI.TrayIcon « GUI « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be668b8e83973c66c6f74a77d8880e3d54b83cb9 (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
245
246
247
248
249
250
251
//  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 MonoMac.Foundation;
using MonoMac.AppKit;
using MonoMac.ObjCRuntime;
using System.Collections.Generic;
using MonoMac.CoreGraphics;
using System.IO;

namespace Duplicati.GUI.TrayIcon
{
    public class CocoaRunner : Duplicati.GUI.TrayIcon.TrayIconBase
    {        
        private class MenuItemWrapper : Duplicati.GUI.TrayIcon.IMenuItem
        {
            private readonly NSMenuItem m_item;
            private readonly Action m_callback;
            
            public NSMenuItem MenuItem { get { return m_item; } }
            
            public MenuItemWrapper(string text, Action callback, IList<Duplicati.GUI.TrayIcon.IMenuItem> subitems)
            {
                if (text == "-")
                    m_item = NSMenuItem.SeparatorItem;
                else
                {
                    m_item = new NSMenuItem(text, ClickHandler);
                    m_callback = callback;
                    
                    if (subitems != null && subitems.Count > 0)
                    {
                        m_item.Submenu = new NSMenu();
                        foreach(var itm in subitems)
                            m_item.Submenu.AddItem(((MenuItemWrapper)itm).MenuItem);
                    }
                }
            }
            
            private void ClickHandler(object sender, EventArgs args)
            {
                if (m_callback != null)
                    m_callback();
            }
            
            #region IMenuItem implementation
            public void SetText(string text)
            {
                m_item.Title = text;
            }

            public void SetIcon(MenuIcons icons)
            {
                // Do nothing.  Implementation needed for IMenuItem interface.
            }

            public void SetDefault(bool isDefault)
            {
                // Do nothing.  Implementation needed for IMenuItem interface.
            }
            #endregion
        }
        
        private static readonly System.Reflection.Assembly ASSEMBLY = System.Reflection.Assembly.GetExecutingAssembly();
        private static readonly string ICON_PATH = ASSEMBLY.GetName().Name + ".OSX_Icons.";
        
        private static readonly string ICON_NORMAL = ICON_PATH + "normal.png";
        private static readonly string ICON_PAUSED = ICON_PATH + "normal-pause.png";
        private static readonly string ICON_RUNNING = ICON_PATH + "normal-running.png";
        private static readonly string ICON_WARNING = ICON_PATH + "normal-error.png"; // TODO: create a normal-warning.png, for now use normal-error.png
        private static readonly string ICON_ERROR = ICON_PATH + "normal-error.png";
        
        private NSStatusItem m_statusItem;
        private readonly Dictionary<Duplicati.GUI.TrayIcon.TrayIcons, NSImage> m_images = new Dictionary<Duplicati.GUI.TrayIcon.TrayIcons, NSImage>();
        private NSApplication m_app;

        // We need to keep the items around, otherwise the GC will destroy them and crash the app
        private readonly List<Duplicati.GUI.TrayIcon.IMenuItem> m_keeper = new List<Duplicati.GUI.TrayIcon.IMenuItem>();

        public override void Init(string[] args)
        {
            NSApplication.Init();
            m_app = NSApplication.SharedApplication;
            m_app.ActivateIgnoringOtherApps(true);

            m_statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem(32);
            m_statusItem.HighlightMode = true;

            base.Init(args);
        }

        private NSImage LoadStream(System.IO.Stream s)
        {
            using(var ms = new MemoryStream())
            {
                s.CopyTo(ms);
                ms.Flush();
                ms.Close();

                var b = ms.ToArray();

                var dp = new CGDataProvider(b, 0, b.Length);
                var img2 = CGImage.FromPNG(dp, null, false, CGColorRenderingIntent.Default);

                return new NSImage(img2, new System.Drawing.SizeF(18, 18));
            }
        }

        private NSImage GetIcon(Duplicati.GUI.TrayIcon.TrayIcons icon)
        {
            if (!m_images.ContainsKey(icon))
            {
                switch(icon)
                {
                    case Duplicati.GUI.TrayIcon.TrayIcons.IdleError:
                        m_images[icon] = LoadStream(ASSEMBLY.GetManifestResourceStream(ICON_ERROR));
                        break;
                    case Duplicati.GUI.TrayIcon.TrayIcons.IdleWarning:
                        m_images[icon] = LoadStream(ASSEMBLY.GetManifestResourceStream(ICON_WARNING));
                        break;
                    case Duplicati.GUI.TrayIcon.TrayIcons.Paused:
                        m_images[icon] = LoadStream(ASSEMBLY.GetManifestResourceStream(ICON_PAUSED));
                        break;
                    case Duplicati.GUI.TrayIcon.TrayIcons.PausedError:
                        m_images[icon] = LoadStream(ASSEMBLY.GetManifestResourceStream(ICON_PAUSED));
                        break;
                    case Duplicati.GUI.TrayIcon.TrayIcons.Running:
                        m_images[icon] = LoadStream(ASSEMBLY.GetManifestResourceStream(ICON_RUNNING));
                        break;
                    case Duplicati.GUI.TrayIcon.TrayIcons.RunningError:
                        m_images[icon] = LoadStream(ASSEMBLY.GetManifestResourceStream(ICON_RUNNING));
                        break;
                    case Duplicati.GUI.TrayIcon.TrayIcons.Idle:
                    default:
                        m_images[icon] = LoadStream(ASSEMBLY.GetManifestResourceStream(ICON_NORMAL));
                        break;
                }
            }
            
            return m_images[icon];
        }
        
        #region implemented abstract members of Duplicati.GUI.TrayIcon.TrayIconBase
        protected override void Run(string[] args)
        {
            try
            {
                m_app.Run();
            }
            finally
            {
                if (m_statusItem != null)
                {
                    NSStatusBar.SystemStatusBar.RemoveStatusItem(m_statusItem);
                    m_statusItem = null;
                    m_keeper.Clear();
                    m_images.Clear();
                }
                m_app = null;
            }
        }
        
        protected override void UpdateUIState(Action action)
        {
            if (m_app != null)
                m_app.BeginInvokeOnMainThread(() => { 
                    action();
                });
            else
                action();
        }

        protected override Duplicati.GUI.TrayIcon.IMenuItem CreateMenuItem (string text, Duplicati.GUI.TrayIcon.MenuIcons icon, Action callback, System.Collections.Generic.IList<Duplicati.GUI.TrayIcon.IMenuItem> subitems)
        {
            return new MenuItemWrapper(text, callback, subitems);
        }

        protected override void Exit()
        {
            if (m_app != null)
            {
                // Set the flag
                m_app.Stop(m_app);
                // Post an event to trigger the exit
                m_app.PostEvent(NSEvent.OtherEvent(NSEventType.ApplicationDefined, 
                    new System.Drawing.PointF(0,0),
                    0, 0, 0, null, 0, 0, 0), true);
            }
        }

        protected override void SetIcon(TrayIcons icon)
        {
            m_statusItem.Image = GetIcon(icon);
        }

        protected override void SetMenu(System.Collections.Generic.IEnumerable<Duplicati.GUI.TrayIcon.IMenuItem> items)
        {
            m_statusItem.Menu = new NSMenu();
            m_keeper.AddRange(items);
            foreach(var itm in items)
                m_statusItem.Menu.AddItem(((MenuItemWrapper)itm).MenuItem);
        }

        protected override void NotifyUser(string title, string message, NotificationType type)
        {
            var notification = new NSUserNotification();
            notification.Title = title;
            notification.InformativeText = message;
            notification.DeliveryDate = DateTime.Now;
            notification.SoundName = NSUserNotification.NSUserNotificationDefaultSoundName;
 
            // We get the Default notification Center
            var center = NSUserNotificationCenter.DefaultUserNotificationCenter;

            // TODO: Figure out why this does not work
            if (center == null)
                return;

            //center.DidDeliverNotification += (s, e) => { };

            center.DidActivateNotification += (s, e) => { };

            // If we return true here, Notification will show up even if your app is TopMost.
            center.ShouldPresentNotification = (c, n) =>
            {
                return true;
            };

            center.ScheduleNotification(notification);
        }

        public override void Dispose ()
        {
        }
        #endregion
    }
}