summaryrefslogtreecommitdiff
path: root/SparkleShare/Windows/UserInterface/StatusIcon.cs
blob: fb02f598cadcbfa8016f8932e27c7a47f8b42c15 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons <hi@planetpeanut.uk>
//
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   This program 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 General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Controls;

using Drawing = System.Drawing;

namespace SparkleShare {

    public class StatusIcon : Control {

        public StatusIconController Controller = new StatusIconController();

        private readonly Drawing.Bitmap syncing_idle_image = UserInterfaceHelpers.GetBitmap("process-syncing-idle");
        private readonly Drawing.Bitmap syncing_up_image = UserInterfaceHelpers.GetBitmap("process-syncing-up");
        private readonly Drawing.Bitmap syncing_down_image = UserInterfaceHelpers.GetBitmap("process-syncing-down");
        private readonly Drawing.Bitmap syncing_image = UserInterfaceHelpers.GetBitmap("process-syncing");
        private readonly Drawing.Bitmap syncing_error_image = UserInterfaceHelpers.GetBitmap("process-syncing-error");

        private ContextMenu context_menu;

        private SparkleMenuItem log_item;
        private SparkleMenuItem state_item;
        private SparkleMenuItem exit_item;
        private SparkleMenuItem[] state_menu_items;

        private readonly NotifyIcon notify_icon = new NotifyIcon();


        public StatusIcon() {
            this.notify_icon.HeaderText = "SparkleShare";
            this.notify_icon.Icon = this.syncing_idle_image;

            CreateMenu();

            Controller.UpdateIconEvent += delegate(IconState state) {
                Dispatcher.BeginInvoke(
                    (Action) delegate {
                    switch(state) {
                        case IconState.Idle: {
                                this.notify_icon.Icon = this.syncing_idle_image;
                                break;
                            }
                        case IconState.SyncingUp: {
                                this.notify_icon.Icon = this.syncing_up_image;
                                break;
                            }
                        case IconState.SyncingDown: {
                                this.notify_icon.Icon = this.syncing_down_image;
                                break;
                            }
                        case IconState.Syncing: {
                                this.notify_icon.Icon = this.syncing_image;
                                break;
                            }
                        case IconState.Error: {
                                this.notify_icon.Icon = this.syncing_error_image;
                                break;
                            }
                    }
                });
            };

            Controller.UpdateStatusItemEvent += delegate(string state_text) {
                Dispatcher.BeginInvoke(
                    (Action) delegate {
                    this.state_item.Header = state_text;
                    this.state_item.UpdateLayout();

                    if(Controller.Projects.Length == state_menu_items.Length) {
                        for(int i = 0; i < Controller.Projects.Length; i++)
                            state_menu_items[i].Header = Controller.Projects[i].StatusMessage;
                    }

                    this.notify_icon.HeaderText = "SparkleShare\n" + state_text;
                });
            };

            Controller.UpdateMenuEvent += delegate {
                Dispatcher.BeginInvoke((Action) CreateMenu);
            };

            Controller.UpdateQuitItemEvent += delegate(bool item_enabled) {
                Dispatcher.BeginInvoke((Action) delegate {
                    this.exit_item.IsEnabled = item_enabled;
                    this.exit_item.UpdateLayout();
                });
            };
        }


        public void CreateMenu() {
            this.context_menu = new ContextMenu();

            this.state_item = new SparkleMenuItem {
                Header = Controller.StateText,
                IsEnabled = false
            };

            Image folder_image = new Image {
                Source = UserInterfaceHelpers.GetImageSource("sparkleshare-folder"),
                Width = 16,
                Height = 16
            };

            SparkleMenuItem folder_item = new SparkleMenuItem {
                Header = "SparkleShare",
                Icon = folder_image
            };

            SparkleMenuItem add_item = new SparkleMenuItem {
                Header = "Add hosted project…"
            };

            this.log_item = new SparkleMenuItem {
                Header = "Recent changes…",
                IsEnabled = Controller.RecentEventsItemEnabled
            };

            SparkleMenuItem link_code_item = new SparkleMenuItem {
                Header = "Client ID"
            };

            if(Controller.LinkCodeItemEnabled) {
                SparkleMenuItem code_item = new SparkleMenuItem {
                    Header = SparkleShare.Controller.UserAuthenticationInfo.PublicKey.Substring(0, 20) + "..."
                };

                SparkleMenuItem copy_item = new SparkleMenuItem {
                    Header = "Copy to Clipboard"
                };
                copy_item.Click += delegate {
                    Controller.CopyToClipboardClicked();
                };

                link_code_item.Items.Add(code_item);
                link_code_item.Items.Add(new Separator());
                link_code_item.Items.Add(copy_item);
            }

            CheckBox notify_check_box = new CheckBox {
                Margin = new Thickness(6, 0, 0, 0),
                IsChecked = SparkleShare.Controller.NotificationsEnabled
            };

            SparkleMenuItem notify_item = new SparkleMenuItem {
                Header = "Notifications",
                Icon = notify_check_box
            };

            SparkleMenuItem about_item = new SparkleMenuItem {
                Header = "About SparkleShare"
            };
            this.exit_item = new SparkleMenuItem {
                Header = "Exit"
            };


            add_item.Click += delegate {
                Controller.AddHostedProjectClicked();
            };
            this.log_item.Click += delegate {
                Controller.RecentEventsClicked();
            };
            about_item.Click += delegate {
                Controller.AboutClicked();
            };

            notify_check_box.Click += delegate {
                this.context_menu.IsOpen = false;
                SparkleShare.Controller.ToggleNotifications();
                notify_check_box.IsChecked = SparkleShare.Controller.NotificationsEnabled;
            };

            notify_item.Click += delegate {
                SparkleShare.Controller.ToggleNotifications();
                notify_check_box.IsChecked = SparkleShare.Controller.NotificationsEnabled;
            };

            this.exit_item.Click += delegate {
                this.notify_icon.Dispose();
                Controller.QuitClicked();
            };


            this.context_menu.Items.Add(this.state_item);
            this.context_menu.Items.Add(new Separator());
            this.context_menu.Items.Add(folder_item);

            state_menu_items = new SparkleMenuItem[Controller.Projects.Length];

            if(Controller.Projects.Length > 0) {
                int i = 0;
                foreach(ProjectInfo project in Controller.Projects) {

                    SparkleMenuItem subfolder_item = new SparkleMenuItem {
                        Header = project.Name.Replace("_", "__"),
                        Icon = new Image {
                            Source = UserInterfaceHelpers.GetImageSource("folder"),
                            Width = 16,
                            Height = 16
                        }
                    };

                    state_menu_items[i] = new SparkleMenuItem {
                        Header = project.StatusMessage,
                        IsEnabled = false
                    };

                    subfolder_item.Items.Add(state_menu_items[i]);
                    subfolder_item.Items.Add(new Separator());

                    SparkleMenuItem open_item = new SparkleMenuItem {
                        Header = "Open folder",
                        Icon = new Image
                        {
                            Source = UserInterfaceHelpers.GetImageSource("folder"),
                            Width = 16,
                            Height = 16
                        }
                    };

                    open_item.Click += new RoutedEventHandler(Controller.OpenFolderDelegate(project.Name));
                    subfolder_item.Items.Add(open_item);
                    subfolder_item.Items.Add(new Separator());

                    if(project.IsPaused) {
                        SparkleMenuItem resume_item;

                        if(project.UnsyncedChangesInfo.Count > 0) {
                            foreach(KeyValuePair<string, string> pair in project.UnsyncedChangesInfo)
                                subfolder_item.Items.Add(new SparkleMenuItem {
                                    Header = pair.Key,
                                    // TODO image
                                    IsEnabled = false
                                });

                            if(!string.IsNullOrEmpty(project.MoreUnsyncedChanges)) {
                                subfolder_item.Items.Add(new SparkleMenuItem {
                                    Header = project.MoreUnsyncedChanges,
                                    IsEnabled = false
                                });
                            }

                            subfolder_item.Items.Add(new Separator());
                            resume_item = new SparkleMenuItem {
                                Header = "Sync and Resume…"
                            };

                        } else {
                            resume_item = new SparkleMenuItem {
                                Header = "Resume"
                            };
                        }

                        resume_item.Click += (sender, e) => Controller.ResumeDelegate(project.Name)(sender, e);
                        subfolder_item.Items.Add(resume_item);

                    } else {
                        if(Controller.Projects[i].HasError) {
                            subfolder_item.Icon = new Image {
                                Source = Imaging.CreateBitmapSourceFromHIcon(
                                    Drawing.SystemIcons.Exclamation.Handle, Int32Rect.Empty,
                                    BitmapSizeOptions.FromWidthAndHeight(16, 16))
                            };

                            SparkleMenuItem try_again_item = new SparkleMenuItem {
                                Header = "Retry Sync"
                            };
                            try_again_item.Click += (sender, e) => Controller.TryAgainDelegate(project.Name)(sender, e);
                            subfolder_item.Items.Add(try_again_item);

                        } else {
                            SparkleMenuItem pause_item = new SparkleMenuItem {
                                Header = "Pause"
                            };
                            pause_item.Click +=
                                (sender, e) => Controller.PauseDelegate(project.Name)(sender, e);
                            subfolder_item.Items.Add(pause_item);
                        }
                    }

                    this.context_menu.Items.Add(subfolder_item);
                    i++;
                };
            }

            folder_item.Items.Add(this.log_item);
            folder_item.Items.Add(add_item);
            folder_item.Items.Add(new Separator());
            folder_item.Items.Add(notify_item);
            folder_item.Items.Add(new Separator());
            folder_item.Items.Add(link_code_item);
            folder_item.Items.Add(new Separator());
            folder_item.Items.Add(about_item);

            this.context_menu.Items.Add(new Separator());
            this.context_menu.Items.Add(this.exit_item);

            this.notify_icon.ContextMenu = this.context_menu;
        }


        public void ShowBalloon(string title, string subtext, string image_path) {
            this.notify_icon.ShowBalloonTip(title, subtext, image_path);
        }


        public void Dispose() {
            this.notify_icon.Dispose();
        }
    }

    public class SparkleMenuItem : MenuItem {

        public SparkleMenuItem() {
            Padding = new Thickness(6, 3, 4, 0);
        }
    }
}