~jconti/ubuntu/precise/emesene/fix-956422

« back to all changes in this revision

Viewing changes to PluginManagerDialog.py

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2010-04-14 01:33:51 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20100414013351-r2icbt5gs4ai71j8
Tags: 1.6.1-0ubuntu1
* New upstream release (LP: #562646).
* Fix missing-debian-source-format lintian warning.
* Refresh 20_dont_build_own_libmimic.patch patch.
* Bump Standards-Version to 3.8.4.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
'''Handles the Plugin Dialog: starts/stops plugins, show description, ect.'''
3
 
 
4
 
#   This file is part of emesene.
5
 
#
6
 
#    Emesene is free software; you can redistribute it and/or modify
7
 
#    it under the terms of the GNU General Public License as published by
8
 
#    the Free Software Foundation; either version 2 of the License, or
9
 
#    (at your option) any later version.
10
 
#
11
 
#    emesene is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU General Public License for more details.
15
 
#
16
 
#    You should have received a copy of the GNU General Public License
17
 
#    along with emesene; if not, write to the Free Software
18
 
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 
20
 
import gtk
21
 
import pango
22
 
 
23
 
import dialog
24
 
 
25
 
class PluginManagerDialog( gtk.Window ):
26
 
    '''This class show and manage all the plugins installed
27
 
    in the plugins directory'''
28
 
    
29
 
    def __init__( self, pluginManager, parent, config ):
30
 
        '''Constructor'''
31
 
        
32
 
        gtk.Window.__init__( self )
33
 
 
34
 
        self.config = config
35
 
        
36
 
        self.pluginManager = pluginManager
37
 
        self.set_transient_for( parent )
38
 
        self.set_type_hint( gtk.gdk.WINDOW_TYPE_HINT_DIALOG )
39
 
        pix = self.render_icon(gtk.STOCK_EXECUTE, gtk.ICON_SIZE_MENU)
40
 
        self.set_icon(pix)
41
 
        
42
 
        self.set_default_size( 500, 350 )
43
 
        #self.set_geometry_hints( self, 500, 350, 500, 350 )
44
 
        self.set_title( _( 'Plugin Manager' ) )
45
 
        self.set_role( _( 'Plugin Manager' ) )
46
 
 
47
 
        self.set_border_width( 12 )
48
 
        self.set_resizable( True )
49
 
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
50
 
 
51
 
        vbox = gtk.VBox()
52
 
        vbox.set_spacing( 12 )
53
 
        
54
 
        self.listStore = gtk.ListStore( str, str, str, bool )
55
 
        
56
 
        cellRendererTextName = gtk.CellRendererText()
57
 
        cellRendererToggle = gtk.CellRendererToggle()
58
 
        cellRendererTextDesc = gtk.CellRendererText()
59
 
        
60
 
        cellRendererToggle.set_property( 'activatable', True )
61
 
        
62
 
        plugOn = gtk.TreeViewColumn(_('Active'), cellRendererToggle, active=3)
63
 
        plugOn.set_resizable( False )
64
 
        plugName = gtk.TreeViewColumn(_('Name'), cellRendererTextName, markup=0)
65
 
        plugName.add_attribute( cellRendererTextName, 'text', 0 )
66
 
        plugName.set_resizable( True )
67
 
        plugName.set_min_width( 60 )
68
 
        plugDesc = gtk.TreeViewColumn(_('Description'), cellRendererTextDesc)
69
 
        cellRendererTextDesc.set_property( 'ellipsize', pango.ELLIPSIZE_END )
70
 
        plugDesc.add_attribute( cellRendererTextDesc, 'text', 2 )
71
 
        plugDesc.set_expand( True )
72
 
        plugDesc.set_sizing( gtk.TREE_VIEW_COLUMN_FIXED )
73
 
        
74
 
        self.list = gtk.TreeView( self.listStore )
75
 
        self.list.append_column( plugOn )
76
 
        self.list.append_column( plugName )
77
 
        self.list.append_column( plugDesc )
78
 
        self.list.set_reorderable( False )
79
 
        self.list.set_headers_visible( True )
80
 
        self.list.set_rules_hint( True )
81
 
        
82
 
        self.scroll = gtk.ScrolledWindow()
83
 
        self.scroll.set_policy( gtk.POLICY_NEVER , gtk.POLICY_AUTOMATIC )
84
 
        self.scroll.set_shadow_type( gtk.SHADOW_OUT )
85
 
        self.scroll.add( self.list )
86
 
        
87
 
        table = gtk.Table( 3, 2 )
88
 
        table.set_row_spacings( 6 )
89
 
        table.set_col_spacings( 12 )
90
 
        hButBox = gtk.HButtonBox()
91
 
        hButBox.set_spacing( 6 )
92
 
        hButBox.set_layout( gtk.BUTTONBOX_END )
93
 
                
94
 
        self.description = gtk.Label() 
95
 
        self.author = gtk.Label() 
96
 
        self.web = gtk.Label() 
97
 
        
98
 
        self.description.set_alignment( 0.0, 0.0 )
99
 
        self.author.set_alignment( 0.0, 0.5 )
100
 
        self.web.set_alignment( 0.0, 0.5 )
101
 
        
102
 
        self.description.set_line_wrap( True )
103
 
        self.author.set_line_wrap( True )
104
 
        self.web.set_line_wrap( True )
105
 
        
106
 
        self.description.set_size_request( 476, 48 )
107
 
        
108
 
        auth = gtk.Label( '<b>' + _( 'Author:' ) + '</b>' )
109
 
        auth.set_alignment( 0.0, 0.5 )
110
 
        auth.set_use_markup( True )
111
 
        web = gtk.Label( '<b>' + _( 'Website:' ) + '</b>' )
112
 
        web.set_alignment( 0.0, 0.5 )
113
 
        web.set_use_markup( True )
114
 
        
115
 
        table.attach( self.description , 0, 2, 0, 1, gtk.FILL | gtk.EXPAND )
116
 
        
117
 
        table.attach( auth, 0, 1, 1, 2, gtk.FILL )
118
 
        table.attach( self.author , 1, 2, 1, 2 )
119
 
        
120
 
        table.attach( web, 0, 1, 2, 3, gtk.FILL )
121
 
        table.attach( self.web , 1, 2, 2, 3 )
122
 
        
123
 
        self.bConfigure = gtk.Button( _( 'Configure' ), gtk.STOCK_PROPERTIES )
124
 
        self.bReload = gtk.Button( stock=gtk.STOCK_REFRESH )
125
 
        self.bLoadNew = gtk.Button( _( 'Load New Plugins' ))
126
 
        self.bclose = gtk.Button( stock=gtk.STOCK_CLOSE )
127
 
 
128
 
        self.bConfigure.connect( 'clicked', self.configurePlugin )
129
 
        self.bReload.connect( 'clicked', self.reloadPlugin )
130
 
        self.bLoadNew.connect( 'clicked', self.loadNewPlugins )
131
 
        self.bclose.connect( 'clicked', self.close )
132
 
 
133
 
        hButBox.pack_start( self.bConfigure )
134
 
        hButBox.pack_start( self.bReload )
135
 
        hButBox.pack_start( self.bLoadNew )
136
 
        hButBox.pack_start( self.bclose )
137
 
                        
138
 
        vbox.pack_start( self.scroll, True, True )
139
 
        vbox.pack_start( table, False, False )
140
 
        vbox.pack_start( hButBox, False, False )
141
 
       
142
 
        vbox.show_all()
143
 
        
144
 
        self.add( vbox )
145
 
        
146
 
        self.fillList()
147
 
        self.list.columns_autosize()
148
 
        
149
 
        self.connect( 'delete-event', self.close )
150
 
        self.list.connect( 'cursor-changed', self.row_selected )
151
 
        cellRendererToggle.connect( 'toggled', self.active_toggled, \
152
 
            ( self.listStore, 3 ) )
153
 
    
154
 
    def fillList( self ):
155
 
        '''fills the plugin list'''
156
 
        self.listStore.clear()
157
 
        
158
 
        for i in sorted(self.pluginManager.getPlugins(), key=lambda x: x.lower()):
159
 
            plugin = self.pluginManager.getPluginData( i )
160
 
            if plugin:
161
 
                self.listStore.append(['<b>' + plugin['displayName'] + '</b>',
162
 
                    i, plugin['description'], self.pluginManager.isEnabled(i)])
163
 
        
164
 
    def close( self, *args ):
165
 
        '''close the window'''
166
 
        
167
 
        self.destroy()
168
 
        
169
 
    def getSelected( self ):
170
 
        '''Return the selected item'''
171
 
        model = self.list.get_model()
172
 
        selected = self.list.get_selection().get_selected()
173
 
        if selected[1]:
174
 
            return model.get(selected[1], 1)[0]
175
 
        else:
176
 
            return None
177
 
        
178
 
    def row_selected( self, *args ):
179
 
        '''callback for the row_selected event'''
180
 
        
181
 
        name = self.getSelected()
182
 
        plugin_instance = self.pluginManager.getPlugin(name)
183
 
        self.setDescription(name)
184
 
        if hasattr(plugin_instance, "configure") and callable(plugin_instance.configure) \
185
 
            and self.pluginManager.isEnabled(name):
186
 
            self.bConfigure.set_sensitive(True)
187
 
        else:
188
 
            self.bConfigure.set_sensitive(False)
189
 
        
190
 
    def configurePlugin( self, *args ):
191
 
        '''call the configure method in the plugin'''
192
 
        plugin_instance = self.pluginManager.getPlugin( self.getSelected() )
193
 
        if hasattr(plugin_instance, "configure") and callable(plugin_instance.configure):
194
 
            plugin_instance.configure()
195
 
    
196
 
    def reloadPlugin( self, *args ):
197
 
        '''Reloads the plugin python code'''
198
 
        name = self.getSelected()
199
 
        self.pluginManager.restartPlugin(name)
200
 
        # update the metadata
201
 
        self.fillList()
202
 
        self.setDescription(name)
203
 
 
204
 
    def loadNewPlugins( self, *args ):
205
 
        '''Loads new plugins'''
206
 
        for i in self.pluginManager.getNewModules():
207
 
            if i not in self.pluginManager.plugin_data:
208
 
                self.pluginManager.inspectPlugin(i)
209
 
            self.pluginManager.loadPlugin(i)
210
 
        self.fillList()
211
 
 
212
 
    def setDescription( self, name ):
213
 
        plugin_data = self.pluginManager.getPluginData(name)
214
 
        if plugin_data:
215
 
            self.description.set_label( plugin_data['description'] )
216
 
            
217
 
            string = ''
218
 
            
219
 
            for (author,mail) in plugin_data['authors'].iteritems():
220
 
                string += author + ' (' + mail + ')\n'
221
 
                
222
 
            
223
 
            self.author.set_label( '' + string[ :-1 ] )
224
 
            
225
 
            self.web.set_label( '' + plugin_data['website'] )
226
 
        
227
 
    def active_toggled(self, cell, path, user_data):
228
 
        '''enable or disable the plugin'''
229
 
        
230
 
        model, column = user_data
231
 
        iterator= model.get_iter_from_string(path)
232
 
        
233
 
        plugin_name = model.get_value(iterator, 1)
234
 
        plugin_instance = self.pluginManager.getPlugin(plugin_name, True)
235
 
 
236
 
        if not plugin_instance:
237
 
            return
238
 
        if self.pluginManager.isEnabled(plugin_name):
239
 
            self.pluginManager.stopPlugin(plugin_name)
240
 
            model.set_value(iterator, column, False)
241
 
        
242
 
        plugins = self.config.user['activePlugins'].split( ',' )
243
 
        
244
 
        if plugin_name in plugins:
245
 
            plugins.pop( plugins.index(plugin_name))
246
 
            self.config.user['activePlugins'] = ','.join(plugins)
247
 
        else:
248
 
            status = self.pluginManager.checkPlugin(plugin_name)
249
 
                
250
 
            if status[0]:
251
 
                self.pluginManager.startPlugin(plugin_name)
252
 
                model.set_value(iterator, column, True)
253
 
            
254
 
                plugins = self.config.user['activePlugins'].split(',')
255
 
                
256
 
                if plugins[0] == '' and len(plugins) == 1:
257
 
                    self.config.user['activePlugins'] = plugin_name
258
 
                elif not plugin_name in plugins:
259
 
                    s = ','.join(plugins) + ',' + plugin_name
260
 
                    self.config.user['activePlugins'] = s
261
 
            else:
262
 
                dialog.error(_('Plugin initialization failed: \n') \
263
 
                    + status[1])