| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | # This file is part of emesene. |
|---|
| 4 | # |
|---|
| 5 | # Emesene is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # emesene is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with emesene; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 18 | |
|---|
| 19 | import os |
|---|
| 20 | import Plugin |
|---|
| 21 | import gtk |
|---|
| 22 | import dialog |
|---|
| 23 | import stock |
|---|
| 24 | import desktop |
|---|
| 25 | #from ImageFileChooser import ImageFileChooserDialog |
|---|
| 26 | #from ImageAreaSelector import ImageAreaSelectorDialog |
|---|
| 27 | from Theme import resizePixbuf |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | class BackDialog(gtk.Window): |
|---|
| 31 | '''a dialog to set the background image''' |
|---|
| 32 | #def __init__(self, parent, config, filename, resize): |
|---|
| 33 | def __init__(self, response, filename, resize): |
|---|
| 34 | #gtk.Dialog.__init__(self , _('Background config'), parent , \ |
|---|
| 35 | # gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT , \ |
|---|
| 36 | # (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, \ |
|---|
| 37 | # gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)) |
|---|
| 38 | |
|---|
| 39 | gtk.Window.__init__(self) |
|---|
| 40 | self.set_title(_('Background Config')) |
|---|
| 41 | self.set_border_width(4) |
|---|
| 42 | self.set_position(gtk.WIN_POS_CENTER) |
|---|
| 43 | |
|---|
| 44 | #self.config = config |
|---|
| 45 | self.filename = filename |
|---|
| 46 | self.resize = resize |
|---|
| 47 | |
|---|
| 48 | #self.set_default_response(gtk.RESPONSE_ACCEPT) |
|---|
| 49 | self.response_ok = response |
|---|
| 50 | self.hbox = gtk.HBox() |
|---|
| 51 | self.imagebox = gtk.HBox() |
|---|
| 52 | |
|---|
| 53 | self.image = gtk.Image() |
|---|
| 54 | |
|---|
| 55 | try: |
|---|
| 56 | pixbuf = gtk.gdk.pixbuf_new_from_file(self.filename) |
|---|
| 57 | pixbuf = resizePixbuf(pixbuf, 128,128) |
|---|
| 58 | self.image.set_from_pixbuf(pixbuf) |
|---|
| 59 | except: |
|---|
| 60 | self.filename = None |
|---|
| 61 | |
|---|
| 62 | self.button = gtk.Button(_('Image')) |
|---|
| 63 | self.button.connect('clicked', self.clickPixmap) |
|---|
| 64 | |
|---|
| 65 | self.imagebox.pack_start(self.image) |
|---|
| 66 | self.hbox.pack_start(self.button) |
|---|
| 67 | |
|---|
| 68 | self.vbox = gtk.VBox(spacing=4) |
|---|
| 69 | self.vbox.pack_start(self.imagebox) |
|---|
| 70 | self.vbox.pack_start(self.hbox) |
|---|
| 71 | |
|---|
| 72 | self.check = gtk.CheckButton(_('Resize image')) |
|---|
| 73 | #if self.resize: |
|---|
| 74 | self.check.set_active(resize) |
|---|
| 75 | |
|---|
| 76 | self.vbox.pack_start(self.check) |
|---|
| 77 | |
|---|
| 78 | b_accept = gtk.Button(stock=gtk.STOCK_OK) |
|---|
| 79 | b_cancel = gtk.Button(stock=gtk.STOCK_CANCEL) |
|---|
| 80 | |
|---|
| 81 | b_accept.connect('clicked', self._on_response_ok) |
|---|
| 82 | b_cancel.connect('clicked', self._on_response_cancel) |
|---|
| 83 | |
|---|
| 84 | hbbox = gtk.HButtonBox() |
|---|
| 85 | hbbox.set_spacing(4) |
|---|
| 86 | hbbox.set_layout(gtk.BUTTONBOX_END) |
|---|
| 87 | |
|---|
| 88 | hbbox.pack_start(b_cancel, False) |
|---|
| 89 | hbbox.pack_start(b_accept, False) |
|---|
| 90 | |
|---|
| 91 | #self.vbox.pack_start(self.hbox) |
|---|
| 92 | #self.vbox.pack_start(self.check) |
|---|
| 93 | #self.vbox.show_all() |
|---|
| 94 | self.vbox.pack_start(hbbox) |
|---|
| 95 | |
|---|
| 96 | self.add(self.vbox) |
|---|
| 97 | |
|---|
| 98 | self.show_all() |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | def clickPixmap(self, arg): |
|---|
| 102 | #self.filename = ImageAreaSelectorDialog(self.config).run()[1] |
|---|
| 103 | def _on_image_selected(response, path): |
|---|
| 104 | self.filename = path |
|---|
| 105 | try: |
|---|
| 106 | pixbuf = gtk.gdk.pixbuf_new_from_file(self.filename) |
|---|
| 107 | pixbuf = resizePixbuf(pixbuf, 128,128) |
|---|
| 108 | self.image.set_from_pixbuf(pixbuf) |
|---|
| 109 | self.image.show() |
|---|
| 110 | except: |
|---|
| 111 | self.filename = None |
|---|
| 112 | self.image.hide() |
|---|
| 113 | |
|---|
| 114 | dialog.ImageChooser(os.path.expanduser('~'), _on_image_selected).show() |
|---|
| 115 | |
|---|
| 116 | #def run(self): |
|---|
| 117 | #option = gtk.Dialog.run(self) |
|---|
| 118 | #self.resize = self.check.get_active() |
|---|
| 119 | #self.destroy() |
|---|
| 120 | #return (self.filename, self.resize) |
|---|
| 121 | |
|---|
| 122 | def _on_response_ok(self, button): |
|---|
| 123 | self.destroy() |
|---|
| 124 | self.response_ok(self.filename, |
|---|
| 125 | int(self.check.get_active())) |
|---|
| 126 | |
|---|
| 127 | def _on_response_cancel(self, button): |
|---|
| 128 | self.destroy() |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | class MainClass( Plugin.Plugin ): |
|---|
| 132 | '''Main plugin class''' |
|---|
| 133 | |
|---|
| 134 | def __init__( self, controller, msn ): |
|---|
| 135 | '''Contructor''' |
|---|
| 136 | |
|---|
| 137 | Plugin.Plugin.__init__( self, controller, msn ) |
|---|
| 138 | |
|---|
| 139 | self.description = _('Changes conversation background.') |
|---|
| 140 | self.authors = { 'Andre Ramaciotti da Silva' : 'andre.ramaciotti@gmail.com' } |
|---|
| 141 | self.displayName = _('Background') |
|---|
| 142 | self.name = 'Background' |
|---|
| 143 | |
|---|
| 144 | self.controller = controller |
|---|
| 145 | self.config = controller.config |
|---|
| 146 | self.config.readPluginConfig(self.name) |
|---|
| 147 | self.filename = self.config.getPluginValue(self.name, 'filename', \ |
|---|
| 148 | None) |
|---|
| 149 | #self.resize = self.config.getPluginValue(self.name, 'resize', \ |
|---|
| 150 | # False) |
|---|
| 151 | self.resize = int(self.config.getPluginValue(self.name, 'resize', '1')) |
|---|
| 152 | self.handlerId = None |
|---|
| 153 | self.enabled = False |
|---|
| 154 | |
|---|
| 155 | def start( self ): |
|---|
| 156 | '''start the plugin''' |
|---|
| 157 | self.convmanagerId = self.controller.conversationManager.connect( \ |
|---|
| 158 | 'new-conversation-ui', self.openWindow) |
|---|
| 159 | try: |
|---|
| 160 | self.bg = gtk.gdk.pixbuf_new_from_file(self.filename) |
|---|
| 161 | except: |
|---|
| 162 | self.bg = None |
|---|
| 163 | |
|---|
| 164 | self.enabled = True |
|---|
| 165 | |
|---|
| 166 | def openWindow(self, conversationManager, conversation, window): |
|---|
| 167 | '''A new window (or tab) has been created...''' |
|---|
| 168 | if self.bg: |
|---|
| 169 | if self.resize: |
|---|
| 170 | h, w = conversation.ui.textview.window.get_size() |
|---|
| 171 | #self.bg = resizePixbuf(self.bg , h,w) |
|---|
| 172 | scaled = self.bg.scale_simple(h, w, gtk.gdk.INTERP_BILINEAR) |
|---|
| 173 | pixmap = scaled.render_pixmap_and_mask()[0] |
|---|
| 174 | else: |
|---|
| 175 | pixmap = self.bg.render_pixmap_and_mask()[0] |
|---|
| 176 | |
|---|
| 177 | style = conversation.ui.textview.get_style() |
|---|
| 178 | textwnd = conversation.ui.textview.get_window(gtk.TEXT_WINDOW_TEXT) |
|---|
| 179 | |
|---|
| 180 | textwnd.set_back_pixmap(pixmap, False) |
|---|
| 181 | |
|---|
| 182 | def stop( self ): |
|---|
| 183 | '''stop the plugin''' |
|---|
| 184 | self.controller.conversationManager.disconnect(self.convmanagerId) |
|---|
| 185 | self.enabled = False |
|---|
| 186 | |
|---|
| 187 | def check(self): |
|---|
| 188 | return (True, 'Ok') |
|---|
| 189 | |
|---|
| 190 | def configure(self): |
|---|
| 191 | #self.filename, self.resize = BackDialog(None, self.config, \ |
|---|
| 192 | # self.filename, self.resize).run() |
|---|
| 193 | |
|---|
| 194 | def _on_configure_ok(filename, resize): |
|---|
| 195 | |
|---|
| 196 | self.filename = filename |
|---|
| 197 | self.resize = resize |
|---|
| 198 | |
|---|
| 199 | self.config.setPluginValue(self.name, 'filename', filename) |
|---|
| 200 | self.config.setPluginValue(self.name, 'resize', resize) |
|---|
| 201 | try: |
|---|
| 202 | self.bg = gtk.gdk.pixbuf_new_from_file(self.filename) |
|---|
| 203 | except: |
|---|
| 204 | self.bg = None |
|---|
| 205 | |
|---|
| 206 | BackDialog(_on_configure_ok, self.filename, self.resize).show() |
|---|
| 207 | return True |
|---|
| 208 | |
|---|