File: SmilieWindow.py

package info (click to toggle)
emesene 1.0-dist-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,596 kB
  • ctags: 3,006
  • sloc: python: 25,171; makefile: 14; sh: 1
file content (258 lines) | stat: -rw-r--r-- 8,415 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import gtk
from gobject import timeout_add, source_remove

from Theme import resizePixbuf

import stock
import dialog

class SmilieWindow(gtk.Window):
    '''this is the window that opens when you press the smilie button on the
    conversation window'''

    def __init__(self, controller, callback, parent=None):
        '''class constructor'''
        gtk.Window.__init__(self)

        self.set_decorated(False)
        self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)

        if parent:
            self.set_transient_for(parent)

        self.set_position(gtk.WIN_POS_MOUSE)
        self.set_resizable(False)
        self.set_title(_('Smilies'))
        self.set_border_width(2)

        self.callback = callback
        
        self.vbox = gtk.VBox()
        table1 = gtk.Table(10)
        
        self.theme = controller.theme
        
        self.customEmoticons = controller.customEmoticons
        self.config = controller.config
        
        self.closed = True
        
        self.tooltips = gtk.Tooltips()
        
        x = 0
        y = 0

        smilie_list = self.theme.getSingleSmileysList()
        smilie_list.sort()

        for i in smilie_list:
            if x == 10:
                x = 0
                y += 1
            try:
                button = gtk.Button()
                button.set_relief(gtk.RELIEF_NONE)
                self.tooltips.set_tip(button, i)
                smilieImage = gtk.Image()
                pixbuf = self.theme.getSmiley(i)
                if type(pixbuf) == gtk.gdk.PixbufAnimation:
                    smilieImage.set_from_animation(pixbuf)
                else:
                    smilieImage.set_from_pixbuf(pixbuf)
                button.set_image(smilieImage)
                button.connect('clicked', self.clicked, i)
                button.connect('enter-notify-event', 
                    self.on_enter_notify_event)
                table1.attach(button, x, x+1, y, y+1)
            except Exception, e:
                print 'error adding smilie ', i
                print e
                x -= 1
            x += 1

        ceLabel = gtk.Label('Custom Emoticons')
        self.vbox.pack_start(table1)
        self.vbox.pack_start(ceLabel)
        self.fillCETable()
        
        self.add(self.vbox)
        self.vbox.show_all()
        self.connect('delete-event', self.on_delete_event)
        self.connect('leave-notify-event', self.on_leave_notify_event)
        self.connect('enter-notify-event', self.on_enter_notify_event)

        self.tag = None

    def on_leave_notify_event(self, *args):
        if not self.tag and not self.closed:
            self.tag = timeout_add(500, self.hide)

    def on_enter_notify_event(self, *args):
        if (self.tag):
            source_remove(self.tag)
            self.tag = None

    def on_delete_event(self, *args):
        self.hide()
        self.closed = True
        return True
    
    def clicked(self, button, smilie):
        self.hide()
        self.callback(smilie)

    def event(self, button, event, smilie):
        if event.type == gtk.gdk.BUTTON_PRESS and event.button == 1:
            self.hide()
            self.callback(smilie)
        if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3:
        
            # -- Custom Emoticon Menu --
            self.emoMenu = gtk.Menu()
            
            self.shortcutItem = gtk.ImageMenuItem(_("Change shortcut"))
            self.shortcutItem.set_image(gtk.image_new_from_stock(
                gtk.STOCK_EDIT, gtk.ICON_SIZE_MENU))
            self.shortcutItem.connect("activate", self.onEditShortcut, 
                smilie)
            
            self.deleteItem = gtk.ImageMenuItem(_("Delete"))
            self.deleteItem.set_image(gtk.image_new_from_stock(
                gtk.STOCK_DELETE, gtk.ICON_SIZE_MENU))
            self.deleteItem.connect("activate", self.onDeleteEmo, smilie)
            
            self.emoMenu.add(self.shortcutItem)
            self.emoMenu.add(self.deleteItem)
            
            self.emoMenu.show_all()
            # -----
            
            self.emoMenu.popup(None, None, None, event.button, event.time)
    
    def onDeleteEmo(self, button, shortcut):
        self.customEmoticons.delete(shortcut)
        self.fillCETable()
    
    def onEditShortcut(self, button, shortcut):
        self.hide()

        def _on_ce_edit_cb(response, text=''):
            '''method called when the edition is done'''

            if response == stock.ACCEPT:
                if text:
                    ret, msg = self.customEmoticons.chageShortcut(shortcut, 
                        text)
                    if not ret:
                        dialog.error(msg)

                else:
                    dialog.error(_("Empty shortcut"))

        window = dialog.entry_window(_("New shortcut"), shortcut, 
            _on_ce_edit_cb, _("Change shortcut"))
        window.show()
            
    def clickedAdd(self, button, *args):
        self.hide()

    def show(self):
        self.fillCETable()
        gtk.Window.show(self)
        self.tag = None
        self.closed = False

    def hide(self):
        gtk.Window.hide(self)
        if (self.tag):
            source_remove(self.tag)
            self.tag = None
        self.closed = True
        
    def fillCETable(self):
        try:
            self.vbox.remove(self.ceTable)
        except: pass

        if not self.customEmoticons:
            print "self.customEmoticons is None"
            return

        self.ceTable = gtk.Table()
        x = 0
        y = 0
        
        for shortcut, filename in self.customEmoticons.list.iteritems():
            if x == 10:
                x = 0
                y += 1
            try:
                pixbuf = gtk.gdk.pixbuf_new_from_file(filename)
                pixbuf = resizePixbuf(pixbuf, 24, 24)
                button = gtk.Button()
                button.set_relief(gtk.RELIEF_NONE)
                self.tooltips.set_tip(button, shortcut)
                smilieImage = gtk.Image()
                smilieImage.set_from_pixbuf(pixbuf)
                button.set_image(smilieImage)
                button.connect('event', self.event, shortcut)
                button.connect('enter-notify-event', 
                    self.on_enter_notify_event)
                self.ceTable.attach(button, x, x+1, y, y+1)
            except:
                print 'Error in smiley', shortcut, filename
                x -= 1
            x += 1
            
        button = gtk.Button(None, gtk.STOCK_ADD)
        button.set_image(gtk.image_new_from_stock(gtk.STOCK_ADD, 
            gtk.ICON_SIZE_BUTTON))
        button.connect('clicked', self.addClicked)
        button.connect('enter-notify-event', self.on_enter_notify_event)
        self.ceTable.attach(button, x, x+1, y, y+1)
        
        self.vbox.pack_start(self.ceTable)
        self.vbox.show_all()
        
    def addClicked(self, button):
        self.hide()

        def _on_ce_choosed(response, path, shortcut, size):
            '''method called when the ce is selected'''

            if response != stock.ACCEPT:
                return

            self.config.user['emoticonDir']= os.path.dirname(path) 

            if size == dialog.CEChooser.SMALL:
                size = 0
            else:
                size = 1
            
            ret,msg =  self.customEmoticons.create(shortcut, path, size)
            if not ret:
                dialog.error(msg)

        dialog.set_custom_emoticon(self.config.user['emoticonDir'], 
            _on_ce_choosed)