~ubuntu-branches/ubuntu/precise/gnome-do/precise-proposed

« back to all changes in this revision

Viewing changes to Do/src/Do.UI/PluginNodeView.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-09-14 10:09:40 UTC
  • mto: (0.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20080914100940-kyghudg7py14bu2z
Tags: upstream-0.6.0.0
ImportĀ upstreamĀ versionĀ 0.6.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* PluginNodeView.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this
 
5
 * source distribution.
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
using System;
 
22
using System.Threading;
 
23
using System.Collections.Generic;
 
24
 
 
25
using Gtk;
 
26
using Mono.Addins;
 
27
using Mono.Addins.Setup;
 
28
 
 
29
using Do.Core;
 
30
using Do.Universe;
 
31
 
 
32
namespace Do.UI
 
33
{
 
34
        public class PluginNodeView : NodeView
 
35
        {
 
36
                enum Column {
 
37
                        Enabled = 0,
 
38
                        Description,
 
39
                        Id,
 
40
                        NumColumns,
 
41
                }
 
42
 
 
43
                const int IconSize = 26;
 
44
                const int WrapWidth = 305;
 
45
                const string DescriptionFormat =
 
46
                        "<b>{0}</b> <small>v{2}</small>\n<small>{1}</small>";
 
47
 
 
48
                protected string filter;
 
49
                protected string repository;
 
50
                protected Dictionary<string, string> addins;
 
51
 
 
52
                public string Filter {
 
53
                        get { return filter; }
 
54
                        set {
 
55
                                filter = value ?? "";
 
56
                                Refresh (false);
 
57
                        }
 
58
                }
 
59
 
 
60
                public string ShowRepository {
 
61
                        get { return repository; }
 
62
                        set {
 
63
                                repository = value;
 
64
                                Refresh (false);
 
65
                        }
 
66
                }
 
67
 
 
68
                public PluginNodeView () :
 
69
                        base ()
 
70
                {
 
71
                        ListStore store;
 
72
                        CellRenderer cell;
 
73
 
 
74
                        filter = "";
 
75
                        repository = PluginManager.AllPluginsRepository;
 
76
                        addins = new Dictionary<string,string> ();
 
77
 
 
78
                        RulesHint = true;
 
79
                        HeadersVisible = false;
 
80
                        Model = store = new ListStore (
 
81
                                typeof (bool),
 
82
                                typeof (string),
 
83
                                typeof (string));
 
84
 
 
85
                        cell = new CellRendererToggle ();
 
86
                        (cell as CellRendererToggle).Activatable = true;
 
87
                        (cell as CellRendererToggle).Toggled += OnPluginToggle;
 
88
                        AppendColumn ("Enable", cell, "active", Column.Enabled);
 
89
 
 
90
                        cell = new CellRendererPixbuf ();                               
 
91
                        cell.SetFixedSize (IconSize + 8, IconSize + 8);
 
92
                        AppendColumn ("Icon", cell, new TreeCellDataFunc (IconDataFunc));
 
93
 
 
94
                        cell = new Gtk.CellRendererText ();
 
95
                        (cell as CellRendererText).WrapWidth = WrapWidth;
 
96
                        (cell as CellRendererText).WrapMode = Pango.WrapMode.Word;
 
97
                        AppendColumn ("Plugin", cell, "markup", Column.Description);
 
98
 
 
99
                        store.SetSortFunc ((int) Column.Id,
 
100
                                new TreeIterCompareFunc (DefaultTreeIterCompareFunc));
 
101
                        store.SetSortColumnId ((int) Column.Id, SortType.Descending);
 
102
 
 
103
                        Selection.Changed += OnSelectionChanged;
 
104
 
 
105
                        Refresh ();
 
106
                }
 
107
 
 
108
                public int DefaultTreeIterCompareFunc(TreeModel model, TreeIter a, 
 
109
                                TreeIter b)
 
110
                {
 
111
                        string repA, repB;
 
112
                        int scoreA, scoreB;
 
113
                        ListStore store = Model as ListStore;
 
114
 
 
115
                        repA = store.GetValue (a, (int)Column.Description) as string;
 
116
                        repB = store.GetValue (b, (int)Column.Description) as string;
 
117
 
 
118
                        if (string.IsNullOrEmpty (repA) || string.IsNullOrEmpty (repB))
 
119
                                return 0;
 
120
 
 
121
                        if (filter == "") {
 
122
                                return string.Compare (repB, repA,
 
123
                                                StringComparison.CurrentCultureIgnoreCase);
 
124
                        }
 
125
 
 
126
                        scoreA = repA.IndexOf (filter,
 
127
                                        StringComparison.CurrentCultureIgnoreCase);
 
128
                        scoreB = repB.IndexOf (filter,
 
129
                                        StringComparison.CurrentCultureIgnoreCase);
 
130
 
 
131
                        return scoreB - scoreA;
 
132
                }
 
133
 
 
134
                protected virtual void IconDataFunc (TreeViewColumn column,
 
135
                                CellRenderer cell,
 
136
                                TreeModel model,
 
137
                                TreeIter iter)
 
138
                {
 
139
                        string id, icon;
 
140
                        CellRendererPixbuf renderer;
 
141
 
 
142
                        renderer = cell as CellRendererPixbuf;
 
143
                        id = (Model as ListStore).GetValue (iter, (int)Column.Id) as string;
 
144
                        icon = PluginManager.IconForAddin (id);
 
145
                        renderer.Pixbuf = IconProvider.PixbufFromIconName (icon, IconSize);
 
146
                }
 
147
 
 
148
                bool AddinShouldShow (Addin a)
 
149
                {
 
150
                        return a.Name.ToLower ().Contains (filter.ToLower ()) &&
 
151
                                PluginManager.AddinIsFromRepository (a, ShowRepository);
 
152
                }
 
153
 
 
154
                bool AddinShouldShow (AddinRepositoryEntry e)
 
155
                {
 
156
                        return e.Addin.Name.ToLower ().Contains (filter.ToLower ()) &&
 
157
                                PluginManager.AddinIsFromRepository (e, ShowRepository);
 
158
                }
 
159
 
 
160
                public virtual void Refresh () {
 
161
                        Refresh (true);
 
162
                }
 
163
 
 
164
                public virtual void Refresh (bool goOnline) {
 
165
                        ListStore store;
 
166
 
 
167
                        store = Model as ListStore;
 
168
                        store.Clear ();
 
169
                        addins.Clear ();
 
170
                        // Add other (non-online) addins.
 
171
                        foreach (Addin a in AddinManager.Registry.GetAddins ()) {
 
172
                                if (!AddinShouldShow (a)) continue;
 
173
                                addins [Addin.GetIdName (a.Id)] = a.Id;
 
174
                                store.AppendValues (a.Enabled, Description (a), a.Id);
 
175
                        }
 
176
                        ScrollFirst (false);
 
177
                        // Add online plugins asynchronously so UI doesn't block.
 
178
                        RefreshOnlinePluginsAsync (goOnline);
 
179
                }
 
180
 
 
181
                protected void ScrollFirst (bool select)
 
182
                {
 
183
                        if (addins.Count > 0) {
 
184
                                ScrollToCell (TreePath.NewFirst (), Columns [0], true, 0, 0);
 
185
                                if (select) Selection.SelectPath (TreePath.NewFirst ());
 
186
                        }
 
187
                }
 
188
 
 
189
                protected void RefreshOnlinePluginsAsync (bool goOnline)
 
190
                {
 
191
                        ListStore store;
 
192
                        SetupService setup;
 
193
 
 
194
                        store = Model as ListStore;
 
195
                        setup = new SetupService (AddinManager.Registry);
 
196
 
 
197
                        Thread th = new Thread ((ThreadStart) delegate {
 
198
                                if (goOnline)
 
199
                                        setup.Repositories.UpdateAllRepositories (new ConsoleProgressStatus (true));
 
200
                                // Add addins from online repositories.
 
201
                                Application.Invoke (delegate {
 
202
                                        try {
 
203
                                                foreach (AddinRepositoryEntry e in
 
204
                                                        setup.Repositories.GetAvailableAddins ()) {
 
205
                                                        if (!AddinShouldShow (e)) continue;
 
206
                                                        // If addin already made its way into the store,
 
207
                                                        // skip.
 
208
                                                        string id = e.Addin.Id;
 
209
                                                        if (addins.ContainsKey (Addin.GetIdName (id)))
 
210
                                                                continue;
 
211
                                                        addins [Addin.GetIdName (id)] = id;
 
212
                                                        store.AppendValues (
 
213
                                                                AddinManager.Registry.IsAddinEnabled (id),
 
214
                                                                Description (e),
 
215
                                                                id);
 
216
                                                }
 
217
                                                ScrollFirst (false);
 
218
                                        } catch {
 
219
                                                // A crash may result if window is closed before this
 
220
                                                // event occurs.
 
221
                                        }
 
222
                                });
 
223
                        });
 
224
                        
 
225
                        th.IsBackground = true;
 
226
                        th.Start ();
 
227
                }
 
228
 
 
229
                protected string Description (string name, string desc, string version)
 
230
                {
 
231
                        return string.Format (DescriptionFormat, name, desc, version);
 
232
                }
 
233
 
 
234
                protected string Description (Addin a)
 
235
                {
 
236
                        return Description (a.Name, a.Description.Description, a.Version);
 
237
                }
 
238
 
 
239
                protected string Description (AddinRepositoryEntry a)
 
240
                {
 
241
                        return Description (a.Addin);
 
242
                }
 
243
 
 
244
                protected string Description (AddinHeader a)
 
245
                {
 
246
                        return Description (a.Name, a.Description, a.Version);
 
247
                }
 
248
 
 
249
                public string[] GetSelectedAddins () {
 
250
                        string id;
 
251
                        TreeIter iter;
 
252
                        ListStore store;
 
253
 
 
254
                        if (Selection.CountSelectedRows () == 0)
 
255
                                return new string [0];
 
256
 
 
257
                        store = Model as ListStore;
 
258
                        Selection.GetSelected (out iter);
 
259
                        id = store.GetValue (iter, (int)Column.Id) as string;
 
260
                        return new string[] { id };
 
261
                }
 
262
 
 
263
                protected void OnPluginToggle (object sender, ToggledArgs args)
 
264
                {
 
265
                        string addinId;
 
266
                        bool enabled;
 
267
                        TreeIter iter;
 
268
                        ListStore store;
 
269
 
 
270
                        store = Model as ListStore;
 
271
                        if (!store.GetIter (out iter, new TreePath (args.Path)))
 
272
                                return;
 
273
 
 
274
                        addinId = (string) store.GetValue (iter, (int)Column.Id);
 
275
                        enabled = (bool) store.GetValue (iter, (int)Column.Enabled);
 
276
                        store.SetValue (iter, (int)Column.Enabled, !enabled);
 
277
                        
 
278
                        if (null != PluginToggled) {
 
279
                                PluginToggled (addinId, !enabled);
 
280
                        }
 
281
                        store.SetValue (iter, (int)Column.Enabled,
 
282
                                        AddinManager.Registry.IsAddinEnabled (addinId));
 
283
                }
 
284
 
 
285
                protected void OnSelectionChanged (object sender, EventArgs args)
 
286
                {
 
287
                        if (null != PluginSelected) {
 
288
                                PluginSelected (this,
 
289
                                                new PluginSelectionEventArgs (GetSelectedAddins ()));
 
290
                        }
 
291
                }
 
292
 
 
293
                public event PluginToggledDelegate PluginToggled;
 
294
                public event PluginSelectedDelegate PluginSelected;
 
295
 
 
296
                public delegate void PluginToggledDelegate (string id, bool enabled);
 
297
                public delegate void PluginSelectedDelegate (object sender, PluginSelectionEventArgs args);
 
298
        }
 
299
}