~nmu-sscheel/gtg/rework-task-editor

« back to all changes in this revision

Viewing changes to GTG/core/plugins/engine.py

  • Committer: Paulo Cabido
  • Date: 2009-07-07 15:12:42 UTC
  • mto: (269.2.1 gtg)
  • mto: This revision was merged to the branch mainline in revision 292.
  • Revision ID: paulo.cabido@gmail.com-20090707151242-jp3sgh74pj42e0hx
Added some files that where missing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# -----------------------------------------------------------------------------
 
3
# Gettings Things Gnome! - a personnal organizer for the GNOME desktop
 
4
# Copyright (c) 2008-2009 - Lionel Dricot & Bertrand Rousseau
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify it under
 
7
# the terms of the GNU General Public License as published by the Free Software
 
8
# Foundation, either version 3 of the License, or (at your option) any later
 
9
# version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 
14
# details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along with
 
17
# this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
# -----------------------------------------------------------------------------
 
19
 
 
20
import pkgutil
 
21
import imp
 
22
 
 
23
try:
 
24
    import pygtk
 
25
    pygtk.require("2.0")
 
26
except:
 
27
    sys.exit(1)
 
28
try:
 
29
    import gtk
 
30
except:
 
31
    sys.exit(1)
 
32
 
 
33
 
 
34
 
 
35
# this class manages the plug-ins
 
36
class PluginEngine:
 
37
        
 
38
    # initializes the plug-in engine
 
39
    # NOTE: the path has to be a list of paths
 
40
    def __init__(self, plugin_path):
 
41
        self.Plugins = []
 
42
        self.plugin_path = plugin_path
 
43
        self.initialized_plugins = []
 
44
                
 
45
    # loads the plugins from the plugin dir
 
46
    def LoadPlugins(self):
 
47
        plugins = {}
 
48
        try:
 
49
            for loader, name, ispkg in pkgutil.iter_modules(self.plugin_path):
 
50
                file, pathname, desc = imp.find_module(name, self.plugin_path)
 
51
                plugins[name] = imp.load_module(name, file, pathname, desc)
 
52
        except Exception, e:
 
53
            print "Error: %s" % e
 
54
            
 
55
        for name, plugin in plugins.items():
 
56
            self.Plugins.append(self.loadPlugin(plugin))
 
57
                        
 
58
        return self.Plugins
 
59
                
 
60
    # checks if the module loaded is a plugin and gets the main class
 
61
    def loadPlugin(self, plugin):
 
62
        plugin_locals = plugin.__dict__
 
63
        is_plugin = False
 
64
        loaded_plugin = {}
 
65
        
 
66
        # find the plugin class
 
67
        for key in plugin_locals.keys():
 
68
            try:
 
69
                is_plugin = getattr(plugin_locals[key], 'PLUGIN_NAME', None)
 
70
            except TypeError:
 
71
                continue
 
72
            
 
73
            # loads the plugin info
 
74
            if is_plugin:
 
75
                try:
 
76
                    loaded_plugin['plugin'] = plugin.__name__
 
77
                    loaded_plugin['class_name'] = key
 
78
                    loaded_plugin['class'] = plugin_locals[key]
 
79
                    loaded_plugin['name'] = plugin_locals[key].PLUGIN_NAME
 
80
                    loaded_plugin['version'] = plugin_locals[key].PLUGIN_VERSION
 
81
                    loaded_plugin['authors'] = plugin_locals[key].PLUGIN_AUTHORS
 
82
                    loaded_plugin['description'] = plugin_locals[key].PLUGIN_DESCRIPTION
 
83
                    loaded_plugin['state'] = plugin_locals[key].PLUGIN_ENABLED
 
84
                    loaded_plugin['instance'] = None
 
85
                except:
 
86
                    continue
 
87
                                
 
88
        return loaded_plugin
 
89
        
 
90
    def enabledPlugins(self, plugins):
 
91
        pe = []
 
92
        for p in plugins:
 
93
            if p['state']:
 
94
                pe.append(p['name'])
 
95
        return pe
 
96
    
 
97
    def disabledPlugins(self, plugins):
 
98
        pd = []
 
99
        for p in plugins:
 
100
            if not p['state']:
 
101
                pd.append(p['name'])
 
102
        return pd
 
103
    
 
104
    # activates the plugins
 
105
    def activatePlugins(self, plugins, plugin_api):
 
106
        for plgin in plugins:
 
107
            if plgin['state']:
 
108
                plgin['instance'] = plgin['class']()
 
109
                plgin['instance'].activate(plugin_api)
 
110
                                
 
111
    # loads the plug-in features for a task
 
112
    def onTaskLoad(self, plugins, plugin_api):
 
113
        for plgin in plugins:
 
114
            if plgin['state']:
 
115
                plgin['instance'].onTaskOpened(plugin_api)
 
116
        
 
117
    # actions to do when a task is deleted
 
118
    def onTaskDelete():
 
119
        pass
 
120
        
 
121
    def recheckPlugins(self, plugins, plugin_api):
 
122
        for plgin in plugins:
 
123
            if plgin['instance'] != None and plgin['state'] == False:
 
124
                print "deactivating plugin: " + plgin['name']
 
125
                plgin['instance'].deactivate(plugin_api)
 
126
                plgin['instance'] = None
 
127
                        
 
128
            if plgin['instance'] == None and plgin['state'] == True:
 
129
                print "activating plugin: " + plgin['name']
 
130
                plgin['instance'] = plgin['class']()
 
131
                plgin['instance'].activate(plugin_api)
 
132
                
 
133
                
 
134
#
 
135
# extends GTK actions
 
136
#
 
137
class PluginAPI:
 
138
    def __init__(self, plugin_engine, window, wTree, requester, task=None, textview=None):
 
139
        # private vars
 
140
        global  __wTree, __window, __plugin_engine, __requester
 
141
        
 
142
        __plugin_engine = plugin_engine
 
143
        __window = window
 
144
        __wTree = wTree
 
145
        __requester = requester
 
146
        
 
147
        if task:
 
148
            self.task = task
 
149
                 
 
150
        if textview:
 
151
            self.textview = textview
 
152
            
 
153
                
 
154
    # adds items to the MenuBar of the Main Window (TaskBrowser)
 
155
    def AddMenuItem(self, item):
 
156
        __wTree.get_widget('menu_plugin').get_submenu().append(item)
 
157
        item.show()
 
158
        
 
159
    # removes the item from the MenuBar         
 
160
    def RemoveMenuItem(self, item):
 
161
        try:
 
162
            __wTree.get_widget('menu_plugin').get_submenu().remove(item)
 
163
        except Exception, e:
 
164
            print "Error removing menu item: %s" % e
 
165
                
 
166
    def AddToolbarItem(self, item):
 
167
        # calculates the number of items on the ToolBar and adds the item 
 
168
        # on the end
 
169
        try:
 
170
            i = 0
 
171
            while __wTree.get_widget('task_tb').get_nth_item(i) is not None:
 
172
                i = i + 1
 
173
            __wTree.get_widget('task_tb').insert(item, i)
 
174
            item.show()
 
175
        except Exception, e:
 
176
            print "Error adding a toolbar item: %s" % e
 
177
        
 
178
    def RemoveToolbarItem(self, item):
 
179
        try:
 
180
            __wTree.get_widget('task_tb').remove(item)
 
181
        except Exception, e:
 
182
            print "Error removing a toolbar item: %s" % e
 
183
    
 
184
    # adds items to the Task Menu 
 
185
    def AddTaskToolbarItem(self, item):
 
186
        try:
 
187
            i = 0
 
188
            while __wTree.get_widget('task_tb1').get_nth_item(i) is not None:
 
189
                i = i + 1
 
190
            __wTree.get_widget('task_tb1').insert(item, i)
 
191
            item.show()
 
192
        except Exception, e:
 
193
            print "Error adding a toolbar item in to the TaskEditor: %s" % e
 
194
                        
 
195
        # passes the requester to the plugin
 
196
    def getRequester(self):
 
197
        return __requester
 
198
                        
 
199
        # changes the tasks TreeStore
 
200
    def changeTaskTreeStore(self, treestore):
 
201
        task_tview = __wTree.get_widget("task_tview")
 
202
        task_tview.set_model(treestore)
 
203
        
 
204
    # adds a tag, updated the text buffer, inserting the tag at the end of
 
205
    # the task
 
206
    def add_tag(self, tag):
 
207
        self.task.add_tag("@" + tag)
 
208
        self.textview.insert_text("\n@" + tag)
 
209
        
 
210
    # adds a attribute to a tag
 
211
    def add_tag_attribute(self, tag, attrib_name, attrib_value):
 
212
        try:
 
213
            tags = self.task.get_tags()
 
214
            for t in tags:
 
215
                if t.get_name() == tag:
 
216
                    t.set_attribute(attrib_name, attrib_value)
 
217
                    return True
 
218
        except:
 
219
            return False
 
220
    
 
221
    # pass all the tags to the plug-in
 
222
    def get_tags(self):
 
223
        return self.task.get_tags()
 
224
        
 
225
    # this will allow plugins to use the textview properties
 
226
    def get_textview(self):
 
227
        return self.textview