~ubuntu-branches/debian/sid/docky/sid

« back to all changes in this revision

Viewing changes to Docky.DBus/Docky.DBus/DockManagerDBusItem.cs

  • Committer: Package Import Robot
  • Author(s): Rico Tzschichholz
  • Date: 2012-01-19 19:03:38 UTC
  • mfrom: (1.1.14) (10.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20120119190338-n44q7tmqsrkudvk7
Tags: 2.1.3-2
* Upload to unstable
* debian/watch:
  + Look for xz tarballs from now on

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2010 Robert Dyer, Chris Szikszoy
 
3
// 
 
4
//  This program is free software: you can redistribute it and/or modify
 
5
//  it under the terms of the GNU General Public License as published by
 
6
//  the Free Software Foundation, either version 3 of the License, or
 
7
//  (at your option) any later version.
 
8
// 
 
9
//  This program is distributed in the hope that it will be useful,
 
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
//  GNU General Public License for more details.
 
13
// 
 
14
//  You should have received a copy of the GNU General Public License
 
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
// 
 
17
 
 
18
using System;
 
19
using System.Collections.Generic;
 
20
using System.Linq;
 
21
 
 
22
using GLib;
 
23
 
 
24
using org.freedesktop.DBus;
 
25
using DBus;
 
26
 
 
27
using Docky.Items;
 
28
using Docky.Menus;
 
29
 
 
30
namespace Docky.DBus
 
31
{
 
32
        public struct ItemTuple
 
33
        {
 
34
                public string Name;
 
35
                public string Icon;
 
36
                public string Title;
 
37
                
 
38
                public ItemTuple (string name, string icon, string title)
 
39
                {
 
40
                        Name = name;
 
41
                        Icon = icon;
 
42
                        Title = title;
 
43
                }
 
44
        }
 
45
        
 
46
        public class DockManagerDBusItem : IDockManagerDBusItem, IDisposable
 
47
        {               
 
48
                uint timer;
 
49
                Dictionary<uint, RemoteMenuEntry> items = new Dictionary<uint, RemoteMenuEntry> ();
 
50
                Dictionary<uint, DateTime> update_time = new Dictionary<uint, DateTime> ();
 
51
 
 
52
                AbstractDockItem owner;
 
53
                
 
54
                string DesktopFile {
 
55
                        get {
 
56
                                if (owner is ApplicationDockItem)
 
57
                                        return (owner as ApplicationDockItem).OwnedItem.Path;
 
58
                                return "";
 
59
                        }
 
60
                }
 
61
                
 
62
                string Uri {
 
63
                        get {
 
64
                                if (owner is FileDockItem)
 
65
                                        return (owner as FileDockItem).Uri;
 
66
                                return "";
 
67
                        }
 
68
                }
 
69
                
 
70
                int[] PIDs {
 
71
                        get {
 
72
                                if (owner is WnckDockItem) {
 
73
                                        return (owner as WnckDockItem).ManagedWindows.Select (w => w.Pid).DefaultIfEmpty (-1).ToArray ();
 
74
                                }
 
75
                                return new int[] { -1 };
 
76
                        }
 
77
                }
 
78
                
 
79
                public DockManagerDBusItem (AbstractDockItem item)
 
80
                {
 
81
                        owner = item;
 
82
                        
 
83
                        timer = GLib.Timeout.Add (4 * 60 * 1000, delegate {
 
84
                                TriggerConfirmation ();
 
85
                                return true;
 
86
                        });
 
87
                }
 
88
                
 
89
                public void TriggerConfirmation ()
 
90
                {
 
91
                        if (MenuItemConfirmationNeeded != null)
 
92
                                MenuItemConfirmationNeeded ();
 
93
                        
 
94
                        GLib.Timeout.Add (30 * 1000, delegate {
 
95
                                foreach (uint i in update_time
 
96
                                        .Where (kvp => (DateTime.UtcNow - kvp.Value).TotalMinutes > 1)
 
97
                                        .Select (kvp => kvp.Key))
 
98
                                        RemoveMenuItem (i);
 
99
                                
 
100
                                return false;
 
101
                        });
 
102
                }
 
103
                
 
104
                public event Action MenuItemConfirmationNeeded;
 
105
                
 
106
                public void ConfirmMenuItem (uint item)
 
107
                {
 
108
                        update_time [item] = DateTime.UtcNow;
 
109
                }
 
110
                
 
111
                #region IDockManagerDBusItem implementation
 
112
                
 
113
                public object Get (string iface, string property)
 
114
                {
 
115
                        if (iface != DBusManager.DockManagerItemBusName)
 
116
                                return null;
 
117
                        
 
118
                        switch (property) {
 
119
                        case "DesktopFile":
 
120
                                return DesktopFile;
 
121
                        case "Uri":
 
122
                                return Uri;
 
123
                        case "PIDs":
 
124
                                return PIDs;
 
125
                        }
 
126
 
 
127
                        return null;
 
128
                }
 
129
                
 
130
                public void Set (string iface, string property, object val)
 
131
                {
 
132
                }
 
133
                
 
134
                public IDictionary<string, object> GetAll (string iface)
 
135
                {
 
136
                        if (iface != DBusManager.DockManagerItemBusName)
 
137
                                return null;
 
138
                        
 
139
                        Dictionary<string, object> items = new Dictionary<string, object> ();
 
140
                        
 
141
                        items ["DesktopFile"] = DesktopFile;
 
142
                        items ["Uri"] = Uri;
 
143
                        items ["PIDs"] = PIDs;
 
144
                        
 
145
                        return items;
 
146
                }
 
147
                
 
148
                #endregion
 
149
                
 
150
                #region IDockManagerDBusItem implementation
 
151
                
 
152
                public event MenuItemActivatedHandler MenuItemActivated;
 
153
                
 
154
                public uint AddMenuItem (IDictionary<string, object> dict)
 
155
                {
 
156
                        string uri = "";
 
157
                        if (dict.ContainsKey ("uri"))
 
158
                                uri = (string) dict ["uri"];
 
159
                        
 
160
                        string title = "";
 
161
                        if (dict.ContainsKey ("container-title"))
 
162
                                title = (string) dict ["container-title"];
 
163
                        
 
164
                        RemoteMenuEntry rem;
 
165
                        
 
166
                        if (uri.Length > 0) {
 
167
                                 rem = new RemoteFileMenuEntry (FileFactory.NewForUri (uri), title);
 
168
                                
 
169
                                AddToList (rem);
 
170
                        } else {
 
171
                                string label = "";
 
172
                                if (dict.ContainsKey ("label"))
 
173
                                        label = (string) dict ["label"];
 
174
                                
 
175
                                string iconName = "";
 
176
                                if (dict.ContainsKey ("icon-name"))
 
177
                                        iconName = (string) dict ["icon-name"];
 
178
                                
 
179
                                string iconFile = "";
 
180
                                if (dict.ContainsKey ("icon-file"))
 
181
                                        iconFile = (string) dict ["icon-file"];
 
182
 
 
183
                                if (iconFile.Length > 0)
 
184
                                        rem = new RemoteMenuEntry (label, iconFile, title);
 
185
                                else
 
186
                                        rem = new RemoteMenuEntry (label, iconName, title);
 
187
                                rem.Clicked += HandleActivated;
 
188
                                
 
189
                                AddToList (rem);
 
190
                        }
 
191
                        
 
192
                        return rem.ID;
 
193
                }
 
194
                
 
195
                public void RemoveMenuItem (uint item)
 
196
                {
 
197
                        if (items.ContainsKey (item)) {
 
198
                                RemoteMenuEntry entry = items [item];
 
199
                                entry.Clicked -= HandleActivated;
 
200
                                
 
201
                                items.Remove (item);
 
202
                                
 
203
                                owner.RemoteMenuItems.Remove (entry);
 
204
                        }
 
205
                }
 
206
                
 
207
                public void UpdateDockItem (IDictionary<string, object> dict)
 
208
                {
 
209
                        foreach (string key in dict.Keys)
 
210
                        {
 
211
                                if (key == "tooltip") {
 
212
                                        owner.SetRemoteText ((string) dict [key]);
 
213
                                } else if (key == "badge") {
 
214
                                        owner.SetRemoteBadgeText ((string) dict [key]);
 
215
                                } else if (key == "progress") {
 
216
                                        owner.Progress = (double) dict [key];
 
217
                                } else if (key == "message") {
 
218
                                        owner.SetMessage ((string) dict [key]);
 
219
                                } else if (key == "icon-file") {
 
220
                                        if (owner is IconDockItem)
 
221
                                                (owner as IconDockItem).SetRemoteIcon ((string) dict [key]);
 
222
                                } else if (key == "attention") {
 
223
                                        if ((bool) dict [key])
 
224
                                                owner.State |= ItemState.Urgent;
 
225
                                        else
 
226
                                                owner.State &= ~ItemState.Urgent;
 
227
                                } else if (key == "waiting") {
 
228
                                        if ((bool) dict [key])
 
229
                                                owner.State |= ItemState.Wait;
 
230
                                        else
 
231
                                                owner.State &= ~ItemState.Wait;
 
232
                                }
 
233
                        }
 
234
                }
 
235
                
 
236
                #endregion
 
237
                
 
238
                private void AddToList (RemoteMenuEntry entry)
 
239
                {
 
240
                        items [entry.ID] = entry;
 
241
                        update_time [entry.ID] = DateTime.UtcNow;
 
242
                                                
 
243
                        //TODO Insert items into list... this is stupid but whatever fix later
 
244
                        foreach (MenuItem item in items.Values)
 
245
                                owner.RemoteMenuItems.Remove (item);
 
246
                        
 
247
                        MenuListContainer _container = MenuListContainer.Footer + 1;
 
248
                        var groupedItems = items.Values
 
249
                                .GroupBy (rmi => rmi.Title)
 
250
                                .OrderBy (g => g.Key);
 
251
                        
 
252
                        foreach (var itemGroup in groupedItems) {
 
253
                                MenuListContainer container;
 
254
                                
 
255
                                switch (itemGroup.Key.ToLower ()) {
 
256
                                case "actions":
 
257
                                        container = MenuListContainer.Actions;
 
258
                                        break;
 
259
                                case "relateditems":
 
260
                                        container = MenuListContainer.RelatedItems;
 
261
                                        break;
 
262
                                case "windows":
 
263
                                        container = MenuListContainer.Windows;
 
264
                                        break;
 
265
                                case "header":
 
266
                                        container = MenuListContainer.Header;
 
267
                                        break;
 
268
                                case "footer":
 
269
                                        container = MenuListContainer.Footer;
 
270
                                        break;
 
271
                                default:
 
272
                                        container = _container;
 
273
                                        owner.RemoteMenuItems.SetContainerTitle (container, itemGroup.Key);
 
274
                                        break;
 
275
                                }
 
276
                                
 
277
                                foreach (RemoteMenuEntry item in itemGroup.OrderBy (i => i.ID)) {
 
278
                                        owner.RemoteMenuItems [container].Add (item);
 
279
                                }
 
280
                                _container++;
 
281
                        }
 
282
                }
 
283
                
 
284
                public ItemTuple GetItem (uint item)
 
285
                {
 
286
                        if (!items.ContainsKey (item))
 
287
                                return new ItemTuple ("", "", "");
 
288
                        
 
289
                        RemoteMenuEntry entry = items [item];
 
290
                        return new ItemTuple (entry.Text, entry.Icon, entry.Title);
 
291
                }
 
292
                        
 
293
                void HandleActivated (object sender, EventArgs args)
 
294
                {
 
295
                        if (!(sender is RemoteMenuEntry))
 
296
                                return;
 
297
                        
 
298
                        if (MenuItemActivated != null)
 
299
                                MenuItemActivated ((sender as RemoteMenuEntry).ID);
 
300
                }
 
301
                
 
302
                #region IDisposable implementation
 
303
                
 
304
                public void Dispose ()
 
305
                {
 
306
                        if (timer > 0)
 
307
                                GLib.Source.Remove (timer);
 
308
                        
 
309
                        update_time.Clear ();
 
310
                        
 
311
                        foreach (RemoteMenuEntry m in items.Values) {
 
312
                                m.Clicked -= HandleActivated;
 
313
                                m.Dispose ();
 
314
                        }
 
315
                        items.Clear ();
 
316
                        
 
317
                        owner = null;
 
318
                }
 
319
                
 
320
                #endregion
 
321
        }
 
322
}