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/>.
23
using System.Collections.Generic;
26
using Mono.Addins.Gui;
27
using Mono.Addins.Setup;
36
/// PluginManager serves as Do's primary interface to Mono.Addins.
38
public static class PluginManager {
40
public static string AllPluginsRepository {
42
return "All Available Plugins";
46
private const string DefaultPluginIcon = "folder_tar";
48
private static string[] ExtensionPaths {
57
private static Dictionary<string, List<string>> repository_urls;
58
public static IDictionary<string, List<string>> RepositoryUrls {
60
if (null == repository_urls) {
61
repository_urls = new Dictionary<string, List<string>> ();
63
repository_urls ["Official Plugins"] = new List<string> ();
64
repository_urls ["Official Plugins"].Add
65
("http://do.davebsd.com/repo/" + Version +"/official");
67
repository_urls ["Community Plugins"] = new List<string> ();
68
repository_urls ["Community Plugins"].Add
69
("http://do.davebsd.com/repo/" + Version +"/community");
71
repository_urls ["Local Plugins"] = new List<string> ();
72
foreach (string repo in Paths.SystemPlugins) {
73
if (Directory.Exists (repo)) {
74
repository_urls ["Local Plugins"].Add ("file://" + repo);
79
return repository_urls;;
83
private static string Version {
85
System.Reflection.AssemblyName name;
87
name = typeof (PluginManager).Assembly.GetName ();
88
return String.Format ("{0}.{1}.{2}",
89
name.Version.Major, name.Version.Minor, name.Version.Build);
94
/// Performs plugin system initialization. Should be called before this
95
/// class or any Mono.Addins class is used.
97
internal static void Initialize ()
99
// Initialize Mono.Addins.
100
AddinManager.Initialize (Paths.UserPlugins);
101
AddinManager.AddExtensionNodeHandler ("/Do/ItemSource", OnIObjectChange);
102
AddinManager.AddExtensionNodeHandler ("/Do/Action", OnIObjectChange);
104
// Register repositories.
105
SetupService setup = new SetupService (AddinManager.Registry);
106
foreach (List<string> urls in RepositoryUrls.Values) {
107
foreach (string url in urls) {
108
if (!setup.Repositories.ContainsRepository (url)) {
109
setup.Repositories.RegisterRepository (null, url, false);
113
InstallLocalPlugins (setup);
116
public static bool AddinIsFromRepository (Addin a, string name)
119
if (name == AllPluginsRepository) return true;
120
RepositoryUrls.TryGetValue (name, out urls);
121
foreach (string url in urls) {
122
if (a.Description.Url.StartsWith (url))
128
public static bool AddinIsFromRepository (AddinRepositoryEntry e,
132
if (name == AllPluginsRepository) return true;
133
RepositoryUrls.TryGetValue (name, out urls);
134
foreach (string url in urls) {
135
if (e.RepositoryUrl.StartsWith (url))
142
/// Given an addin ID, returns an icon that may represent that addin.
144
/// <param name="id">
145
/// A <see cref="System.String"/> containing an addin ID.
148
/// A <see cref="System.String"/> containing an icon name. Can be loaded
149
/// via IconProvider.
151
public static string IconForAddin (string id)
153
// First look for an icon among ItemSources:
154
foreach (IItemSource obj in ObjectsForAddin<IItemSource> (id)) {
156
if (null != obj.Icon)
160
// If no icon found among ItemSources, look for an icon among
162
foreach (IAction obj in ObjectsForAddin<IAction> (id)) {
164
if (null != obj.Icon)
168
return DefaultPluginIcon;
172
/// Finds all plugged-in item sources.
175
/// A <see cref="ICollection`1"/> of DoItemSource instances loaded from
178
internal static ICollection<DoItemSource> GetItemSources () {
179
List<DoItemSource> sources;
181
sources = new List<DoItemSource> ();
182
foreach (IItemSource source in
183
AddinManager.GetExtensionObjects ("/Do/ItemSource")) {
184
sources.Add (new DoItemSource (source));
190
/// Finds all plugged-in actions.
193
/// A <see cref="ICollection`1"/> of DoAction instances loaded from
196
internal static ICollection<DoAction> GetActions () {
197
List<DoAction> actions;
199
actions = new List<DoAction> ();
200
foreach (IAction action in
201
AddinManager.GetExtensionObjects ("/Do/Action")) {
202
actions.Add (new DoAction (action));
208
/// Install all available plugin updates, either
209
/// graphically or non-graphically.
211
/// <param name="graphical">
212
/// A <see cref="System.Boolean"/> to determine whether installer should
213
/// be graphical (true iff graphical installer should be used).
216
/// A <see cref="System.Boolean"/> indicating whether any updates were
219
internal static bool InstallAvailableUpdates (bool graphical)
222
List<string> updates;
223
IAddinInstaller installer;
225
updates = new List<string> ();
226
setup = new SetupService (AddinManager.Registry);
227
installer = graphical ? new DoAddinInstaller () as IAddinInstaller
228
: new ConsoleAddinInstaller () as IAddinInstaller ;
230
setup.Repositories.UpdateAllRepositories (
231
new ConsoleProgressStatus (true));
232
foreach (AddinRepositoryEntry rep in
233
setup.Repositories.GetAvailableAddins ()) {
236
installed = AddinManager.Registry.GetAddin (Addin.GetIdName (
238
if (null == installed) continue;
239
if (Addin.CompareVersions (installed.Version, rep.Addin.Version) > 0) {
240
updates.Add (rep.Addin.Id);
243
if (updates.Count > 0) {
244
installer.InstallAddins (
245
AddinManager.Registry, string.Empty, updates.ToArray ());
247
return updates.Count > 0;
251
/// Checks if there are any updates available for download/installatition
254
/// A <see cref="System.Boolean"/> representing whether or not there
255
/// are any updates available for install
257
public static bool UpdatesAvailable ()
260
setup = new SetupService (AddinManager.Registry);
261
setup.Repositories.UpdateAllRepositories (
262
new ConsoleProgressStatus (true));
263
foreach (AddinRepositoryEntry rep in
264
setup.Repositories.GetAvailableAddins ()) {
267
installed = AddinManager.Registry.GetAddin (Addin.GetIdName (
269
if (null == installed) continue;
270
if (Addin.CompareVersions (installed.Version, rep.Addin.Version) > 0) {
278
/// Installs plugins that are located in the <see
279
/// cref="Paths.UserPlugins"/> directory. This will build addins
280
/// (mpack files) and install them.
282
/// <param name="setup">
283
/// A <see cref="SetupService"/>
285
internal static void InstallLocalPlugins (SetupService setup)
287
// Create mpack (addin packages) out of dlls.
288
foreach (string file in
289
Directory.GetFiles (Paths.UserPlugins, "*.dll")) {
292
path = Path.Combine (Paths.UserPlugins, file);
293
setup.BuildPackage (new ConsoleProgressStatus (false),
294
Paths.UserPlugins, new string [] { path });
297
//Delete dlls. If we do it earlier why might delete dll's brought in as
298
//dependancies. Doing it now has the same effect without breakage.
299
foreach (string file in
300
Directory.GetFiles (Paths.UserPlugins, "*.dll")) {
303
path = Path.Combine (Paths.UserPlugins, file);
306
// Install each mpack file, deleting each file when finished
308
foreach (string file in
309
Directory.GetFiles (Paths.UserPlugins, "*.mpack")) {
312
path = Path.Combine (Paths.UserPlugins, file);
313
setup.Install (new ConsoleProgressStatus (false),
314
new string [] { path });
319
internal static void OnIObjectChange (object s,
320
ExtensionNodeEventArgs args)
322
TypeExtensionNode node;
324
node = args.ExtensionNode as TypeExtensionNode;
325
if (args.Change.Equals (ExtensionChange.Add)) {
327
// plugin is to be used only for inspection here.
328
IObject plugin = node.GetInstance () as IObject;
329
// Wrap in a DoObject for safety.
330
IObject o = new DoObject (plugin);
331
if (plugin is Pluggable)
332
(plugin as Pluggable).NotifyLoad ();
333
Log.Info ("Loaded \"{0}\".", o.Name);
334
} catch (Exception e) {
335
Log.Error ("Encountered error loading \"{0}\": {0}",
337
Log.Debug (e.StackTrace);
341
IObject plugin = node.GetInstance() as IObject;
342
IObject o = new DoObject (plugin);
343
if (plugin is Pluggable)
344
(plugin as Pluggable).NotifyUnload ();
345
Log.Info ("Unloaded \"{0}\".", o.Name);
346
} catch (Exception e) {
347
Log.Error ("Encountered error unloading plugin: {0}",
349
Log.Debug (e.StackTrace);
355
/// Get all objects conforming to type T provided by a given addin.
357
/// <param name="id">
358
/// A <see cref="System.String"/> containing an addin id.
361
/// A <see cref="ICollection`1"/> of instances of type T.
363
private static ICollection<T> ObjectsForAddin<T> (string id)
367
obs = new List<T> ();
368
foreach (string path in ExtensionPaths) {
369
foreach (TypeExtensionNode n in
370
AddinManager.GetExtensionNodes (path)) {
372
bool addinMatch, typeMatch;
375
instance = n.GetInstance ();
380
Addin.GetIdName (id) == Addin.GetIdName (n.Addin.Id);
382
typeof (T).IsAssignableFrom (instance.GetType ());
383
if (addinMatch && typeMatch) {
384
obs.Add ((T) instance);
392
/// Get all IConfigurable instances loaded from plugins.
394
/// <param name="id">
395
/// A <see cref="System.String"/> containing an addin id.
398
/// A <see cref="ICollection`1"/> of <see cref="IConfigurable"/>
399
/// provided by the addin for that id.
401
internal static ICollection<IConfigurable> ConfigurablesForAddin (string id)
403
List<IConfigurable> cons;
405
cons = new List<IConfigurable> ();
406
foreach (IConfigurable con in ObjectsForAddin<IConfigurable> (id))
407
cons.Add (new DoObject (con));
b'\\ No newline at end of file'