2
// Copyright (C) 2010 Robert Dyer, Chris Szikszoy
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.
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.
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/>.
19
using System.Collections.Generic;
24
using org.freedesktop.DBus;
32
public struct ItemTuple
38
public ItemTuple (string name, string icon, string title)
46
public class DockManagerDBusItem : IDockManagerDBusItem, IDisposable
49
Dictionary<uint, RemoteMenuEntry> items = new Dictionary<uint, RemoteMenuEntry> ();
50
Dictionary<uint, DateTime> update_time = new Dictionary<uint, DateTime> ();
52
AbstractDockItem owner;
56
if (owner is ApplicationDockItem)
57
return (owner as ApplicationDockItem).OwnedItem.Path;
64
if (owner is FileDockItem)
65
return (owner as FileDockItem).Uri;
72
if (owner is WnckDockItem) {
73
return (owner as WnckDockItem).ManagedWindows.Select (w => w.Pid).DefaultIfEmpty (-1).ToArray ();
75
return new int[] { -1 };
79
public DockManagerDBusItem (AbstractDockItem item)
83
timer = GLib.Timeout.Add (4 * 60 * 1000, delegate {
84
TriggerConfirmation ();
89
public void TriggerConfirmation ()
91
if (MenuItemConfirmationNeeded != null)
92
MenuItemConfirmationNeeded ();
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))
104
public event Action MenuItemConfirmationNeeded;
106
public void ConfirmMenuItem (uint item)
108
update_time [item] = DateTime.UtcNow;
111
#region IDockManagerDBusItem implementation
113
public object Get (string iface, string property)
115
if (iface != DBusManager.DockManagerItemBusName)
130
public void Set (string iface, string property, object val)
134
public IDictionary<string, object> GetAll (string iface)
136
if (iface != DBusManager.DockManagerItemBusName)
139
Dictionary<string, object> items = new Dictionary<string, object> ();
141
items ["DesktopFile"] = DesktopFile;
143
items ["PIDs"] = PIDs;
150
#region IDockManagerDBusItem implementation
152
public event MenuItemActivatedHandler MenuItemActivated;
154
public uint AddMenuItem (IDictionary<string, object> dict)
157
if (dict.ContainsKey ("uri"))
158
uri = (string) dict ["uri"];
161
if (dict.ContainsKey ("container-title"))
162
title = (string) dict ["container-title"];
166
if (uri.Length > 0) {
167
rem = new RemoteFileMenuEntry (FileFactory.NewForUri (uri), title);
172
if (dict.ContainsKey ("label"))
173
label = (string) dict ["label"];
175
string iconName = "";
176
if (dict.ContainsKey ("icon-name"))
177
iconName = (string) dict ["icon-name"];
179
string iconFile = "";
180
if (dict.ContainsKey ("icon-file"))
181
iconFile = (string) dict ["icon-file"];
183
if (iconFile.Length > 0)
184
rem = new RemoteMenuEntry (label, iconFile, title);
186
rem = new RemoteMenuEntry (label, iconName, title);
187
rem.Clicked += HandleActivated;
195
public void RemoveMenuItem (uint item)
197
if (items.ContainsKey (item)) {
198
RemoteMenuEntry entry = items [item];
199
entry.Clicked -= HandleActivated;
203
owner.RemoteMenuItems.Remove (entry);
207
public void UpdateDockItem (IDictionary<string, object> dict)
209
foreach (string key in dict.Keys)
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;
226
owner.State &= ~ItemState.Urgent;
227
} else if (key == "waiting") {
228
if ((bool) dict [key])
229
owner.State |= ItemState.Wait;
231
owner.State &= ~ItemState.Wait;
238
private void AddToList (RemoteMenuEntry entry)
240
items [entry.ID] = entry;
241
update_time [entry.ID] = DateTime.UtcNow;
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);
247
MenuListContainer _container = MenuListContainer.Footer + 1;
248
var groupedItems = items.Values
249
.GroupBy (rmi => rmi.Title)
250
.OrderBy (g => g.Key);
252
foreach (var itemGroup in groupedItems) {
253
MenuListContainer container;
255
switch (itemGroup.Key.ToLower ()) {
257
container = MenuListContainer.Actions;
260
container = MenuListContainer.RelatedItems;
263
container = MenuListContainer.Windows;
266
container = MenuListContainer.Header;
269
container = MenuListContainer.Footer;
272
container = _container;
273
owner.RemoteMenuItems.SetContainerTitle (container, itemGroup.Key);
277
foreach (RemoteMenuEntry item in itemGroup.OrderBy (i => i.ID)) {
278
owner.RemoteMenuItems [container].Add (item);
284
public ItemTuple GetItem (uint item)
286
if (!items.ContainsKey (item))
287
return new ItemTuple ("", "", "");
289
RemoteMenuEntry entry = items [item];
290
return new ItemTuple (entry.Text, entry.Icon, entry.Title);
293
void HandleActivated (object sender, EventArgs args)
295
if (!(sender is RemoteMenuEntry))
298
if (MenuItemActivated != null)
299
MenuItemActivated ((sender as RemoteMenuEntry).ID);
302
#region IDisposable implementation
304
public void Dispose ()
307
GLib.Source.Remove (timer);
309
update_time.Clear ();
311
foreach (RemoteMenuEntry m in items.Values) {
312
m.Clicked -= HandleActivated;