~and471/+junk/do-with-docky

« back to all changes in this revision

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

  • Committer: rugby471 at gmail
  • Date: 2010-10-15 16:08:38 UTC
  • Revision ID: rugby471@gmail.com-20101015160838-z9m3utbf7bxzb5ty
reverted to before docky removal

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.Wink;
 
33
using Do.Interface.CairoUtils;
 
34
 
 
35
using Docky.Interface.Menus;
 
36
using Docky.Utilities;
 
37
 
 
38
using Wnck;
 
39
 
 
40
namespace Docky.Interface
 
41
{
 
42
        
 
43
        
 
44
        public class ItemDockItem : WnckDockItem, IRightClickable
 
45
        {
 
46
                Item item;
 
47
                int window_count;
 
48
                uint handle_timer;
 
49
                List<Wnck.Window> windows;
 
50
                
 
51
                public event EventHandler RemoveClicked;
 
52
                
 
53
                protected override string Icon { 
 
54
                        get { return item.Icon; } 
 
55
                }
 
56
                
 
57
                string Name {
 
58
                        get {
 
59
                                if (NeedsAttention && AttentionWindows.Any ())
 
60
                                        return AttentionWindows.First ().Name;
 
61
                                
 
62
                                if (VisibleWindows.Any () && WindowCount == 1)
 
63
                                        return VisibleWindows.First ().Name;
 
64
                                
 
65
                                return Item.Name;
 
66
                        }
 
67
                }
 
68
                
 
69
                public override Item Item { 
 
70
                        get { return item; } 
 
71
                }
 
72
                
 
73
                public override IEnumerable<Wnck.Window> Windows { 
 
74
                        get { return windows; } 
 
75
                }
 
76
                
 
77
                IEnumerable<Wnck.Window> AttentionWindows {
 
78
                        get { return VisibleWindows.Where (w => w.NeedsAttention ()); }
 
79
                }
 
80
                
 
81
                public IEnumerable<int> Pids { 
 
82
                        get { return windows.Select (win => win.Pid).ToArray (); } 
 
83
                }
 
84
                
 
85
                public override int WindowCount {
 
86
                        get { return window_count; }
 
87
                }
 
88
                
 
89
                public ItemDockItem (Item item) : base ()
 
90
                {
 
91
                        Position = -1;
 
92
                        this.item = item;
 
93
                        windows = new List<Wnck.Window> ();
 
94
 
 
95
                        UpdateApplication ();
 
96
                        NeedsAttention = DetermineUrgencyStatus ();
 
97
                        
 
98
                        SetText (Name);
 
99
                }
 
100
                
 
101
                public void UpdateApplication ()
 
102
                {
 
103
                        UnregisterWindowEvents ();
 
104
                        
 
105
                        if (item is IApplicationItem) {
 
106
                                windows = WindowUtils.WindowListForCmd ((item as IApplicationItem).Exec);
 
107
                                window_count = windows.Where (w => !w.IsSkipTasklist).Count ();
 
108
                        }
 
109
                        
 
110
                        RegisterWindowEvents ();
 
111
                        SetText (Name);
 
112
                        SetIconRegionFromCache ();
 
113
                }
 
114
                
 
115
                void RegisterWindowEvents ()
 
116
                {
 
117
                        foreach (Wnck.Window w in VisibleWindows) {
 
118
                                w.StateChanged += HandleStateChanged;
 
119
                                w.NameChanged += HandleNameChanged;
 
120
                        }
 
121
                }
 
122
                
 
123
                void UnregisterWindowEvents ()
 
124
                {
 
125
                        foreach (Wnck.Window w in Windows) {
 
126
                                try {
 
127
                                        w.StateChanged -= HandleStateChanged;
 
128
                                        w.NameChanged -= HandleNameChanged;
 
129
                                } catch {}
 
130
                        }
 
131
                }
 
132
                
 
133
                void HandleStateChanged (object o, StateChangedArgs args)
 
134
                {
 
135
                        if (handle_timer > 0) return;
 
136
                        // we do this delayed so that we dont get a flood of these events.  Certain windows behave badly.
 
137
                        handle_timer = GLib.Timeout.Add (100, HandleUpdate);
 
138
                        window_count = VisibleWindows.Count ();
 
139
                        SetIconRegionFromCache ();
 
140
                }
 
141
                
 
142
                void HandleNameChanged(object sender, EventArgs e)
 
143
                {
 
144
                        SetText (Name);
 
145
                }
 
146
                
 
147
                bool HandleUpdate ()
 
148
                {
 
149
                        bool needed_attention = NeedsAttention;
 
150
                        NeedsAttention = DetermineUrgencyStatus ();
 
151
                        
 
152
                        if (NeedsAttention != needed_attention) {
 
153
                                UpdateRequestType req;
 
154
                                if (NeedsAttention) 
 
155
                                        req = UpdateRequestType.NeedsAttentionSet;
 
156
                                else
 
157
                                        req = UpdateRequestType.NeedsAttentionUnset;
 
158
                                OnUpdateNeeded (new UpdateRequestArgs (this, req));
 
159
                        }
 
160
                        
 
161
                        SetText (Name);
 
162
                        handle_timer = 0;
 
163
                        return false;
 
164
                }
 
165
                
 
166
                public override void HotSeatRequested ()
 
167
                {
 
168
                        if (WindowCount == 0) return;
 
169
                        
 
170
                        List<AbstractDockItem> dockitems = new List<AbstractDockItem> ();
 
171
                                        
 
172
                        foreach (Act act in ActionsForItem (item)) {
 
173
                                dockitems.Add (new ActionDockItem (act, item));
 
174
                        }
 
175
                        
 
176
                        Docky.Core.DockServices.ItemsService.HotSeatItem (this, dockitems);
 
177
                        base.HotSeatRequested ();
 
178
                }
 
179
                
 
180
                public override void Clicked (uint button, Gdk.ModifierType state, PointD position)
 
181
                {
 
182
                        SetIconRegionFromCache ();
 
183
                        base.Clicked (button, state, position);
 
184
                }
 
185
 
 
186
                
 
187
                protected override void Launch ()
 
188
                {
 
189
                        if (Item is IFileItem)
 
190
                                Services.Core.PerformDefaultAction (Item as Item, new [] { typeof (OpenAction), });
 
191
                        else
 
192
                                Services.Core.PerformDefaultAction (Item as Item, Type.EmptyTypes);
 
193
                }
 
194
                
 
195
                public override bool Equals (AbstractDockItem other)
 
196
                {
 
197
                        if (other == null) return false;
 
198
                        
 
199
                        ItemDockItem di = other as ItemDockItem;
 
200
                        return di != null && di.Item != null && Item != null && di.Item.UniqueId == Item.UniqueId;
 
201
                }
 
202
 
 
203
                #region IDisposable implementation 
 
204
                
 
205
                public override void Dispose ()
 
206
                {
 
207
                        UnregisterWindowEvents ();
 
208
                        item = null;
 
209
                        windows.Clear ();
 
210
                        
 
211
                        base.Dispose ();
 
212
                }
 
213
                
 
214
                #endregion
 
215
                
 
216
                #region IRightClickable implementation 
 
217
                
 
218
                public IEnumerable<AbstractMenuArgs> GetMenuItems ()
 
219
                {
 
220
                        bool hasApps = HasVisibleApps;
 
221
                        
 
222
                        yield return new SeparatorMenuButtonArgs ();
 
223
                        
 
224
                        if (hasApps) {
 
225
                                foreach (Act act in ActionsForItem (item))
 
226
                                        yield return new LaunchMenuButtonArgs (act, item, act.Name, act.Icon).AsDark ();
 
227
                        } else {
 
228
                                foreach (Act act in ActionsForItem (item))
 
229
                                        yield return new LaunchMenuButtonArgs (act, item, act.Name, act.Icon);
 
230
                        }
 
231
                        
 
232
                        if (hasApps) {
 
233
                                foreach (Wnck.Window window in VisibleWindows) {
 
234
                                        yield return new SeparatorMenuButtonArgs ();
 
235
                                        yield return new WindowMenuButtonArgs (window, window.Name, Icon);
 
236
                                }
 
237
                        }
 
238
 
 
239
                }
 
240
                #endregion
 
241
        }
 
242
}