3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this
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.
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.
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/>.
22
using System.Threading;
23
using System.Collections.Generic;
27
using Mono.Addins.Setup;
34
public class PluginNodeView : NodeView
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>";
48
protected string filter;
49
protected string repository;
50
protected Dictionary<string, string> addins;
52
public string Filter {
53
get { return filter; }
60
public string ShowRepository {
61
get { return repository; }
68
public PluginNodeView () :
75
repository = PluginManager.AllPluginsRepository;
76
addins = new Dictionary<string,string> ();
79
HeadersVisible = false;
80
Model = store = new ListStore (
85
cell = new CellRendererToggle ();
86
(cell as CellRendererToggle).Activatable = true;
87
(cell as CellRendererToggle).Toggled += OnPluginToggle;
88
AppendColumn ("Enable", cell, "active", Column.Enabled);
90
cell = new CellRendererPixbuf ();
91
cell.SetFixedSize (IconSize + 8, IconSize + 8);
92
AppendColumn ("Icon", cell, new TreeCellDataFunc (IconDataFunc));
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);
99
store.SetSortFunc ((int) Column.Id,
100
new TreeIterCompareFunc (DefaultTreeIterCompareFunc));
101
store.SetSortColumnId ((int) Column.Id, SortType.Descending);
103
Selection.Changed += OnSelectionChanged;
108
public int DefaultTreeIterCompareFunc(TreeModel model, TreeIter a,
113
ListStore store = Model as ListStore;
115
repA = store.GetValue (a, (int)Column.Description) as string;
116
repB = store.GetValue (b, (int)Column.Description) as string;
118
if (string.IsNullOrEmpty (repA) || string.IsNullOrEmpty (repB))
122
return string.Compare (repB, repA,
123
StringComparison.CurrentCultureIgnoreCase);
126
scoreA = repA.IndexOf (filter,
127
StringComparison.CurrentCultureIgnoreCase);
128
scoreB = repB.IndexOf (filter,
129
StringComparison.CurrentCultureIgnoreCase);
131
return scoreB - scoreA;
134
protected virtual void IconDataFunc (TreeViewColumn column,
140
CellRendererPixbuf renderer;
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);
148
bool AddinShouldShow (Addin a)
150
return a.Name.ToLower ().Contains (filter.ToLower ()) &&
151
PluginManager.AddinIsFromRepository (a, ShowRepository);
154
bool AddinShouldShow (AddinRepositoryEntry e)
156
return e.Addin.Name.ToLower ().Contains (filter.ToLower ()) &&
157
PluginManager.AddinIsFromRepository (e, ShowRepository);
160
public virtual void Refresh () {
164
public virtual void Refresh (bool goOnline) {
167
store = Model as ListStore;
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);
177
// Add online plugins asynchronously so UI doesn't block.
178
RefreshOnlinePluginsAsync (goOnline);
181
protected void ScrollFirst (bool select)
183
if (addins.Count > 0) {
184
ScrollToCell (TreePath.NewFirst (), Columns [0], true, 0, 0);
185
if (select) Selection.SelectPath (TreePath.NewFirst ());
189
protected void RefreshOnlinePluginsAsync (bool goOnline)
194
store = Model as ListStore;
195
setup = new SetupService (AddinManager.Registry);
197
Thread th = new Thread ((ThreadStart) delegate {
199
setup.Repositories.UpdateAllRepositories (new ConsoleProgressStatus (true));
200
// Add addins from online repositories.
201
Application.Invoke (delegate {
203
foreach (AddinRepositoryEntry e in
204
setup.Repositories.GetAvailableAddins ()) {
205
if (!AddinShouldShow (e)) continue;
206
// If addin already made its way into the store,
208
string id = e.Addin.Id;
209
if (addins.ContainsKey (Addin.GetIdName (id)))
211
addins [Addin.GetIdName (id)] = id;
213
AddinManager.Registry.IsAddinEnabled (id),
219
// A crash may result if window is closed before this
225
th.IsBackground = true;
229
protected string Description (string name, string desc, string version)
231
return string.Format (DescriptionFormat, name, desc, version);
234
protected string Description (Addin a)
236
return Description (a.Name, a.Description.Description, a.Version);
239
protected string Description (AddinRepositoryEntry a)
241
return Description (a.Addin);
244
protected string Description (AddinHeader a)
246
return Description (a.Name, a.Description, a.Version);
249
public string[] GetSelectedAddins () {
254
if (Selection.CountSelectedRows () == 0)
255
return new string [0];
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 };
263
protected void OnPluginToggle (object sender, ToggledArgs args)
270
store = Model as ListStore;
271
if (!store.GetIter (out iter, new TreePath (args.Path)))
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);
278
if (null != PluginToggled) {
279
PluginToggled (addinId, !enabled);
281
store.SetValue (iter, (int)Column.Enabled,
282
AddinManager.Registry.IsAddinEnabled (addinId));
285
protected void OnSelectionChanged (object sender, EventArgs args)
287
if (null != PluginSelected) {
288
PluginSelected (this,
289
new PluginSelectionEventArgs (GetSelectedAddins ()));
293
public event PluginToggledDelegate PluginToggled;
294
public event PluginSelectedDelegate PluginSelected;
296
public delegate void PluginToggledDelegate (string id, bool enabled);
297
public delegate void PluginSelectedDelegate (object sender, PluginSelectionEventArgs args);