From f2d2ac6c8ec6d7f78afa0131c6a2ecdd82980841 Mon Sep 17 00:00:00 2001 From: Zach Tibbitts Date: Thu, 15 Feb 2007 21:30:17 +0000 Subject: [PATCH] Example Plugin --- plugins/ExamplePlugin/example-plugin.png | Bin 0 -> 1418 bytes plugins/ExamplePlugin/example.glade | 94 ++++++++++++++++++++ plugins/ExamplePlugin/plugin.py | 105 ++++++++++++++++++++--- plugins/HelloWorld/plugin.py | 14 +-- plugins/TorrentSearch/plugin.py | 2 +- src/delugegtk.py | 13 ++- src/delugeplugins.py | 17 ++-- 7 files changed, 210 insertions(+), 35 deletions(-) create mode 100644 plugins/ExamplePlugin/example-plugin.png create mode 100644 plugins/ExamplePlugin/example.glade diff --git a/plugins/ExamplePlugin/example-plugin.png b/plugins/ExamplePlugin/example-plugin.png new file mode 100644 index 0000000000000000000000000000000000000000..1f2b3b392f16cd41f9b400436e444bf303e785c8 GIT binary patch literal 1418 zcmV;51$Fv~P)~?1s6#~ zK~y-)jg@Om6jv0-|95s~c4lFhg(BgRCazjilNy^^sjp!DpwZe`ZH>|Rg8G1#)Fy3; zq{w;#$99@_LKCzG7q z`LgUFZoLlgiJyJ+vWzqgGGhKssD9G9Gj@BuABh?`FRCbVmX13giX0y z2mmm~oOKPgqxQ?Pyw5bvEu+qig+`;%R!V8dw|l?I9o{4esvrmm1OgC6k#NpQRdwZz zy;XZ=21B7k^X6tx3rE6xVxj}FMdHJ+QA^0N{{i8PRAIkw^rFVZiBhLRD1&0Hvm9 zBoqLO3X2Cqp`hS!IA9nCTrL+%XXA_$13(Fc)4ks#4rqqqKK2HT6DuOB8*a#bAEr6D|S_sU!L{GoR_9gf7N4I z7GzmQx4)a8IC;EnY>k&JUb=8Xati!@KN6D?(bCe~y}j^r-?+((t;WXs+sl?Me>vt+ zkH>?~u1c zjhj+q&9E#Bs;VL`E)J3;!8A?u_4T2%vlEn3P)d=Jk%qJFXTzeyv0&VQR8?253rE7A z%$=Q`Hf73`RK^(k`}?4(E|{hXO6j;~Mn*dB-Rp#+C`e0B!-WgysbyKS^YaR>3=fE= zYgO;9SVi1!w~JB=UDt5-+!;MS-mT1dZ6*xEfN7dwj6o0t+_`(pzIOc&`>iE!J03iE zNF0K|0V2i}5C1DvMV;S#{AibL+q$L=@*i57djf&JcOO4~bpLeQ&-TQL6JZzzGBY#z z#g5;@4 + + + + + 5 + Example Plugin + 313 + 167 + False + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_ENTER_NOTIFY_MASK + 2 + + + True + 2 + 2 + + + True + + + 1 + 2 + + + + + True + + + 1 + 2 + 1 + 2 + + + + + True + First Value + + + + + True + Second Value + + + 1 + 2 + + + + + 1 + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_ENTER_NOTIFY_MASK + GTK_BUTTONBOX_END + + + True + gtk-cancel + True + + + + + True + gtk-ok + True + 1 + + + 1 + + + + + False + GTK_PACK_END + + + + + + diff --git a/plugins/ExamplePlugin/plugin.py b/plugins/ExamplePlugin/plugin.py index 5f17807f6..6a872b24d 100644 --- a/plugins/ExamplePlugin/plugin.py +++ b/plugins/ExamplePlugin/plugin.py @@ -1,27 +1,106 @@ -#!/usr/bin/python +# An example plugin for use with Deluge -class plugin_Example: +# This plugin is intended to be used with Deluge's default GTK interface +class plugin_Example: # The plugin's class + ## Your plugin's contructor should follow this format + ## path = A string containing the path to the plugin + ## deluge_core = The active instance of the Deluge Manager + ## deluge_interface = The active instance of the Deluge Interface def __init__(self, path, deluge_core, deluge_interface): + # Save the path, interface, and core so they can be used later self.path = path self.core = deluge_core self.interface = deluge_interface + # Classes must be imported as they are needed from within + # the plugin's functions + import dcommon, gtk, gtk.glade, dgtk + # Create an options file and try to load existing Values + self.config_file = dcommon.CONFIG_DIR + "/example.conf" + self.config = dcommon.DelugePreferences() + try: + self.config.load_from_file(self.config_file) + except IOError: + # File does not exist + pass + + # Extract the configuration dialog from the gladefile + self.glade = gtk.glade.XML(path + "/example.glade") + self.dialog = self.glade.get_widget("dialog") + self.dialog.set_icon_from_file(self.path + "/example-plugin.png") + # Access the interface's toolbar + self.toolbar = self.interface.toolbar + # Make a toolbar button + icon = gtk.Image() + icon.set_from_file(self.path + "/example-plugin.png") # Toolbar items should be 22x22 pixel images + self.button = gtk.ToolButton(icon_widget=icon, label="Example Plugin") + self.button.connect("clicked", self.clicked) # Connect the signal handler for the button + self.toolbar.add(self.button) # Add button to toolbar + self.button.show_all() # Show the button + + print "Example Plugin loaded" + + ## unload is called when the plugin is removed or Deluge is shut down def unload(self): + self.toolbar.remove(self.button) # Remove the button from the toolbar + self.config.save_to_file(self.config_file) print "Example Plugin unloaded" + ## update will be called every UPDATE_INTERVAL (usually about 1 second) def update(self): - print "Hello World, from Example Plugin" - + print "Example Plugin has been updated" + + ## This will be only called if your plugin is configurable + def configure(self): + entry1 = self.glade.get_widget("entry1") + entry2 = self.glade.get_widget("entry2") + try: + entry1.set_text(self.config.get("option1")) + entry2.set_text(self.config.get("option2")) + except KeyError: + entry1.set_text("") + entry2.set_text("") + self.dialog.show() + response = self.dialog.run() + self.dialog.hide() + if response: + self.config.set("option1", entry1.get_text()) + self.config.set("option2", entry2.get_text()) + + + ## This will be called whenever self.button is clicked + def clicked(self, button): + # Build a dialog from scratch rather than from a glade file + import gtk + dialog = gtk.Dialog(title="Example Plugin", parent=self.interface.window, + buttons=(gtk.STOCK_OK, 0)) + dialog.set_icon_from_file(self.path + "/example-plugin.png") + try: + text = "This is a popup notification from Example Plugin\n" + \ + "Your value for option1 is %s\n"%self.config.get("option1") + \ + "and option2 is %s"%self.config.get("option2") + except KeyError: + text = "This is a popup notification from Example Plugin\n" + \ + "If you had set options by configuring this plugin,\n" + \ + "they would appear here" + label = gtk.Label(text) + dialog.vbox.pack_start(label) + + dialog.show_all() + dialog.run() + dialog.hide() + dialog.destroy() -register_plugin("Example Plugin", - plugin_Example, - "0.2", - "An example plugin", - config=False, - default=False, - requires="0.5.0", - interface="gtk", - required_plugins=None) +register_plugin("Example Plugin", # The name of the plugin + plugin_Example, # The plugin's class + "0.5.0", # The plugin's version number + "An example plugin", # A description of the plugin + config=True, # If the plugin can be configured + default=False, # If the plugin should be loaded by default + requires="0.5.0", # Required version of Deluge + interface="gtk", # Required Deluge interface + required_plugins=None # Any plugins that must be loaded before this + ) diff --git a/plugins/HelloWorld/plugin.py b/plugins/HelloWorld/plugin.py index ea7ca7466..232e3a417 100644 --- a/plugins/HelloWorld/plugin.py +++ b/plugins/HelloWorld/plugin.py @@ -1,25 +1,19 @@ +# A simple plugin to display Hello, World + class plugin_Hello: def __init__(self, path, deluge_core, deluge_interface): self.path = path self.core = deluge_core self.interface = deluge_interface - print "Hello World loaded" def unload(self): - print "Hello World unloaded" + pass def update(self): print "Hello, World!" - - - register_plugin("Hello World", plugin_Hello, "1.0", 'Displays "Hello, World"', - config=False, - default=False, - requires="0.5.0", - interface=None, - required_plugins=None) + requires="0.5.0") diff --git a/plugins/TorrentSearch/plugin.py b/plugins/TorrentSearch/plugin.py index bcbf8f9c1..a3aca3951 100644 --- a/plugins/TorrentSearch/plugin.py +++ b/plugins/TorrentSearch/plugin.py @@ -1,4 +1,4 @@ -#!/usr/bin/python + class plugin_Search: def __init__(self, path, deluge_core, deluge_interface): diff --git a/src/delugegtk.py b/src/delugegtk.py index 2a01a118d..8564e5be2 100755 --- a/src/delugegtk.py +++ b/src/delugegtk.py @@ -202,7 +202,10 @@ class DelugeGTK(dbus.service.Object): version = plugin['version'] config = plugin['config'] description = plugin['description'] - self.prf_glade.get_widget("plugin_conf").set_sensitive(config) + if name in self.plugins.get_enabled_plugins(): + self.prf_glade.get_widget("plugin_conf").set_sensitive(config) + else: + self.prf_glade.get_widget("plugin_conf").set_sensitive(False) self.prf_glade.get_widget("plugin_text").get_buffer( ).set_text("%s\nVersion: %s\n\n%s"% (name, version, description)) @@ -215,6 +218,8 @@ class DelugeGTK(dbus.service.Object): self.plugin_store.set_value(plugin_iter, 1, plugin_value) if plugin_value: self.plugins.enable_plugin(plugin_name) + self.prf_glade.get_widget("plugin_conf").set_sensitive( + self.plugins.get_plugin(plugin_name)['config']) else: self.plugins.disable_plugin(plugin_name) @@ -373,6 +378,8 @@ class DelugeGTK(dbus.service.Object): self.plugin_store.append( (plugin, True) ) else: self.plugin_store.append( (plugin, False) ) + self.prf_glade.get_widget("plugin_text").get_buffer().set_text("") + self.prf_glade.get_widget("plugin_conf").set_sensitive(False) self.plugin_dlg.show() self.plugin_dlg.run() self.plugin_dlg.hide() @@ -686,6 +693,10 @@ class DelugeGTK(dbus.service.Object): self.share_column.set_visible(obj.get_active()) def quit(self, obj=None): + self.shutdown() + + def shutdown(self): + self.plugins.shutdown_all_plugins() self.manager.quit() gtk.main_quit() diff --git a/src/delugeplugins.py b/src/delugeplugins.py index 0362921c2..3d02e5f97 100644 --- a/src/delugeplugins.py +++ b/src/delugeplugins.py @@ -67,16 +67,13 @@ class PluginManager: for name in self.enabled_plugins.keys(): self.enabled_plugins[name].update() - def register_plugin(self, - name, - plugin_class, - version, - description, - config=False, - default=False, - requires=None, - interface=None, - required_plugins=None): + def shutdown_all_plugins(self): + for name in self.enabled_plugins.keys(): + self.enabled_plugins[name].unload() + self.enabled_plugins.clear() + + def register_plugin(self, name, plugin_class, version, description, config=False, + default=False, requires=None, interface=None, required_plugins=None): self.available_plugins[name] = {'class': plugin_class, 'version': version, 'description': description,