~ubuntu-branches/ubuntu/natty/gnome-do/natty

« back to all changes in this revision

Viewing changes to Do.Interface.Linux.Docky/src/Docky.Interface/Docky.Interface.Items/ItemDockItem.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2009-03-22 22:44:39 UTC
  • mto: (1.1.7 upstream) (0.1.4 squeeze)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20090322224439-ztmk4tbq19s3safs
ImportĀ upstreamĀ versionĀ 0.8.1.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ItemDockItem.cs
 
2
// 
 
3
// Copyright (C) 2008 GNOME Do
 
4
//
 
5
// This program is free software: you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation, either version 3 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
//
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.IO;
 
22
using System.Linq;
 
23
 
 
24
using Gdk;
 
25
using Cairo;
 
26
using Mono.Unix;
 
27
 
 
28
using Do.Platform;
 
29
using Do.Universe;
 
30
using Do.Universe.Common;
 
31
using Do.Interface;
 
32
using Do.Interface.CairoUtils;
 
33
 
 
34
using Docky.Interface.Menus;
 
35
using Docky.Utilities;
 
36
 
 
37
using Wnck;
 
38
 
 
39
namespace Docky.Interface
 
40
{
 
41
        
 
42
        
 
43
        public class ItemDockItem : WnckDockItem, IRightClickable
 
44
        {
 
45
                const string ErrorMessage = "Docky could not move the file to the requested Directory.  " + 
 
46
                        "Please check file name and permissions and try again";
 
47
                
 
48
                Item element;
 
49
                int window_count;
 
50
                uint handle_timer;
 
51
                bool accepting_drops;
 
52
                Gdk.Pixbuf drag_pixbuf;
 
53
                Gdk.Rectangle icon_region;
 
54
                List<Wnck.Application> apps;
 
55
                
 
56
                public event EventHandler RemoveClicked;
 
57
                
 
58
                public override bool IsAcceptingDrops { 
 
59
                        get { return accepting_drops; } 
 
60
                }
 
61
                
 
62
                string Icon { 
 
63
                        get { return element.Icon; } 
 
64
                }
 
65
                
 
66
                public Item Element { 
 
67
                        get { return element; } 
 
68
                }
 
69
                
 
70
                protected override IEnumerable<Wnck.Application> Applications { 
 
71
                        get { return apps; } 
 
72
                }
 
73
                
 
74
                public IEnumerable<int> Pids { 
 
75
                        get { return apps.Select (win => win.Pid).ToArray (); } 
 
76
                }
 
77
                
 
78
                public override int WindowCount {
 
79
                        get { return window_count; }
 
80
                }
 
81
                
 
82
                public ItemDockItem (Item element) : base ()
 
83
                {
 
84
                        Position = -1;
 
85
                        this.element = element;
 
86
                        apps = new List<Wnck.Application> ();
 
87
 
 
88
                        SetText (element.Name);
 
89
 
 
90
                        UpdateApplication ();
 
91
                        NeedsAttention = DetermineUrgencyStatus ();
 
92
                        
 
93
                        if (element is IFileItem && Directory.Exists ((element as IFileItem).Path))
 
94
                                accepting_drops = true;
 
95
                        else
 
96
                                accepting_drops = false;
 
97
                }
 
98
                
 
99
                public override bool ReceiveItem (string item)
 
100
                {
 
101
                        bool result = false;
 
102
                        if (!IsAcceptingDrops)
 
103
                                return result;
 
104
                        
 
105
                        if (item.StartsWith ("file://"))
 
106
                                item = item.Substring ("file://".Length);
 
107
                        
 
108
                        if (File.Exists (item)) {
 
109
                                try {
 
110
                                        File.Move (item, System.IO.Path.Combine ((Element as IFileItem).Path, System.IO.Path.GetFileName (item)));
 
111
                                        result = true;
 
112
                                } catch { 
 
113
                                        Services.Notifications.Notify ("Docky Error", ErrorMessage);
 
114
                                }
 
115
                        } else if (Directory.Exists (item)) {
 
116
                                try {
 
117
                                        Directory.Move (item, System.IO.Path.Combine ((Element as IFileItem).Path, System.IO.Path.GetFileName (item)));
 
118
                                        result = true;
 
119
                                } catch { 
 
120
                                        Services.Notifications.Notify ("Docky Error", ErrorMessage);
 
121
                                }
 
122
                        }
 
123
                        return result;
 
124
                }
 
125
                
 
126
                public void UpdateApplication ()
 
127
                {
 
128
                        UnregisterStateChangeEvents ();
 
129
                        
 
130
                        if (element is IApplicationItem) {
 
131
                                apps = WindowUtils.GetApplicationList ((element as IApplicationItem).Exec);
 
132
                                window_count = Applications.SelectMany (a => a.Windows).Where (w => !w.IsSkipTasklist).Count ();
 
133
                        }
 
134
                        
 
135
                        RegisterStateChangeEvents ();
 
136
                }
 
137
                
 
138
                void RegisterStateChangeEvents ()
 
139
                {
 
140
                        foreach (Application app in Applications) {
 
141
                                foreach (Wnck.Window w in app.Windows) {
 
142
                                        if (!w.IsSkipTasklist)
 
143
                                                w.StateChanged += OnWindowStateChanged;
 
144
                                }
 
145
                        }
 
146
                }
 
147
                
 
148
                void UnregisterStateChangeEvents ()
 
149
                {
 
150
                        foreach (Application app in Applications) {
 
151
                                foreach (Wnck.Window w in app.Windows) {
 
152
                                        try {
 
153
                                                w.StateChanged -= OnWindowStateChanged;
 
154
                                        } catch {}
 
155
                                }
 
156
                        }
 
157
                }
 
158
                
 
159
                void OnWindowStateChanged (object o, StateChangedArgs args)
 
160
                {
 
161
                        if (handle_timer > 0) return;
 
162
                        // we do this delayed so that we dont get a flood of these events.  Certain windows behave badly.
 
163
                        handle_timer = GLib.Timeout.Add (100, HandleUpdate);
 
164
                        window_count = Applications.SelectMany (a => a.Windows).Where (w => !w.IsSkipTasklist).Count ();
 
165
                        SetIconRegionFromCache ();
 
166
                }
 
167
                
 
168
                bool HandleUpdate ()
 
169
                {
 
170
                        bool needed_attention = NeedsAttention;
 
171
                        NeedsAttention = DetermineUrgencyStatus ();
 
172
                        
 
173
                        if (NeedsAttention != needed_attention) {
 
174
                                UpdateRequestType req;
 
175
                                if (NeedsAttention) 
 
176
                                        req = UpdateRequestType.NeedsAttentionSet;
 
177
                                else
 
178
                                        req = UpdateRequestType.NeedsAttentionUnset;
 
179
                                OnUpdateNeeded (new UpdateRequestArgs (this, req));
 
180
                        }
 
181
                        
 
182
                        handle_timer = 0;
 
183
                        return false;
 
184
                }
 
185
                
 
186
                protected override Gdk.Pixbuf GetSurfacePixbuf (int size)
 
187
                {
 
188
                        Gdk.Pixbuf pbuf = IconProvider.PixbufFromIconName (Icon, size);
 
189
                        if (pbuf.Height != size && pbuf.Width != size) {
 
190
                                double scale = (double)DockPreferences.FullIconSize / Math.Max (pbuf.Width, pbuf.Height);
 
191
                                Gdk.Pixbuf temp = pbuf.ScaleSimple ((int) (pbuf.Width * scale), (int) (pbuf.Height * scale), InterpType.Bilinear);
 
192
                                pbuf.Dispose ();
 
193
                                pbuf = temp;
 
194
                        }
 
195
                        
 
196
                        return pbuf;
 
197
                }
 
198
                
 
199
                public override Pixbuf GetDragPixbuf ()
 
200
                {
 
201
                        if (drag_pixbuf == null)
 
202
                                drag_pixbuf = IconProvider.PixbufFromIconName (Icon, DockPreferences.FullIconSize);
 
203
                        return drag_pixbuf;
 
204
                }
 
205
                
 
206
                public override void HotSeatRequested ()
 
207
                {
 
208
                        if (WindowCount == 0) return;
 
209
                        
 
210
                        List<AbstractDockItem> dockitems = new List<AbstractDockItem> ();
 
211
                                        
 
212
                        foreach (Act act in ActionsForItem (element)) {
 
213
                                dockitems.Add (new ActionDockItem (act, element));
 
214
                        }
 
215
                        
 
216
                        Docky.Core.DockServices.ItemsService.HotSeatItem (this, dockitems);
 
217
                        base.HotSeatRequested ();
 
218
                }
 
219
                
 
220
                protected override void Launch ()
 
221
                {
 
222
                        if (Element is IFileItem)
 
223
                                Services.Core.PerformDefaultAction (Element as Item, new [] { typeof (OpenAction), });
 
224
                        else
 
225
                                Services.Core.PerformDefaultAction (Element as Item, Type.EmptyTypes);
 
226
                }
 
227
                
 
228
                public override void SetIconRegion (Gdk.Rectangle region)
 
229
                {
 
230
                        if (icon_region == region) return;
 
231
                        icon_region = region;
 
232
                        SetIconRegionFromCache ();
 
233
                }
 
234
                
 
235
                void SetIconRegionFromCache ()
 
236
                {
 
237
                        VisibleWindows.ForEach (w => w.SetIconGeometry (icon_region.X, icon_region.Y, icon_region.Width, icon_region.Height));
 
238
                }
 
239
                
 
240
                public override bool Equals (AbstractDockItem other)
 
241
                {
 
242
                        if (other == null) return false;
 
243
                        
 
244
                        ItemDockItem di = other as ItemDockItem;
 
245
                        return di != null && di.Element != null && Element != null && di.Element.UniqueId == Element.UniqueId;
 
246
                }
 
247
 
 
248
                #region IDisposable implementation 
 
249
                
 
250
                public override void Dispose ()
 
251
                {
 
252
                        UnregisterStateChangeEvents ();
 
253
                        element = null;
 
254
                        apps.Clear ();
 
255
                        
 
256
                        if (drag_pixbuf != null)
 
257
                                drag_pixbuf.Dispose ();
 
258
                        
 
259
                        base.Dispose ();
 
260
                }
 
261
                
 
262
                #endregion
 
263
                
 
264
                #region IRightClickable implementation 
 
265
                
 
266
                public IEnumerable<AbstractMenuArgs> GetMenuItems ()
 
267
                {
 
268
                        bool hasApps = HasVisibleApps;
 
269
                        
 
270
                        yield return new SeparatorMenuButtonArgs ();
 
271
                        
 
272
                        if (hasApps) {
 
273
                                foreach (Act act in ActionsForItem (element))
 
274
                                        yield return new LaunchMenuButtonArgs (act, element, act.Name, act.Icon).AsDark ();
 
275
                        } else {
 
276
                                foreach (Act act in ActionsForItem (element))
 
277
                                        yield return new LaunchMenuButtonArgs (act, element, act.Name, act.Icon);
 
278
                        }
 
279
                        
 
280
                        if (hasApps) {
 
281
                                foreach (Wnck.Window window in VisibleWindows) {
 
282
                                        yield return new SeparatorMenuButtonArgs ();
 
283
                                        yield return new WindowMenuButtonArgs (window, window.Name, Icon);
 
284
                                }
 
285
                        }
 
286
 
 
287
                }
 
288
                #endregion
 
289
        }
 
290
}