~ubuntu-branches/ubuntu/saucy/amsn/saucy

« back to all changes in this revision

Viewing changes to plugins/emoticons_importer/emoticons_importer.tcl

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2010-04-13 23:21:29 UTC
  • mfrom: (1.1.11 upstream) (3.1.8 sid)
  • Revision ID: james.westby@ubuntu.com-20100413232129-vgpx20brdd2qavs7
Tags: 0.98.3-0ubuntu1
* Merge from Debian unstable (LP: #449072), remaining Ubuntu changes:
  - add 08_use_aplay_for_sound.dpatch patch by Festor Wailon Dacoba to use
    aplay to play sounds
  + debian/control:
    - modify iceweasel to firefox | abrowser in amsn Suggests field
    - add xdg-utils and gstreamer0.10-nice to amsn Depends field
    - mofify sox to alsa-utils in amsn Suggests field as we are now using
      aplay
* New upstream release (LP: #562619), tarball repacked according to
  debian/README.source.
* Fix missing-debian-source-format lintian warning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###################################################################
 
2
#               aMSN Emoticon Importer Plugin                     #
 
3
#                    By Youness Alaoui                            #
 
4
#                        KaKaRoTo                                 #
 
5
#                                                                 #
 
6
###################################################################
 
7
 
 
8
namespace eval ::emoticons_importer {
 
9
        variable config
 
10
        variable configlist
 
11
 
 
12
        #######################################################################################
 
13
        #######################################################################################
 
14
        ####            Initialization Procedure (Called by the Plugins System)            ####
 
15
        #######################################################################################
 
16
        #######################################################################################
 
17
 
 
18
        proc Init { dir } {
 
19
                variable config
 
20
                variable configlist
 
21
 
 
22
                ::plugins::RegisterPlugin emoticons_importer
 
23
 
 
24
                array set config {}
 
25
 
 
26
                #Load lang files
 
27
                set langdir [file join $dir "lang"]
 
28
                set lang [::config::getGlobalKey language]
 
29
                load_lang en $langdir
 
30
                load_lang $lang $langdir
 
31
 
 
32
                set configlist [list [list frame ::emoticons_importer::populateframe ""] ]
 
33
        }
 
34
        proc populateframe { win } {
 
35
                set ::emoticons_importer::importdir ""
 
36
                # Show some kind of UI for choosing a directory and importing from it.
 
37
                frame $win.1
 
38
                frame $win.2
 
39
                pack $win.1 $win.2 -anchor w -side top -padx 0 -pady 5 -fill none
 
40
                label $win.1.path -text "[trans importdir] :" -padx 5 -font sboldf
 
41
                entry $win.1.importdir -width 45  -textvariable ::emoticons_importer::importdir
 
42
                button $win.1.browse -text [trans browse] -command "Browse_Dialog_dir ::emoticons_importer::importdir"
 
43
 
 
44
                grid $win.1.path -row 1 -column 1 -sticky w
 
45
                grid $win.1.importdir -row 1 -column 2 -sticky w
 
46
                grid $win.1.browse -row 1 -column 3 -sticky w
 
47
 
 
48
                button $win.2.import -text [trans import] -command "::emoticons_importer::importSmileys"
 
49
                pack $win.2.import -anchor w -side top -padx 0 -pady 5 -fill none
 
50
        }
 
51
 
 
52
 
 
53
        proc importSmileys { } {
 
54
                if {[info exists ::emoticons_importer::importdir] } {
 
55
                        importSmileysFromDir $::emoticons_importer::importdir
 
56
                }
 
57
        }
 
58
 
 
59
        # Return value
 
60
        # 0 : success
 
61
        # -1 : directory not found or file not a directory
 
62
        proc importSmileysFromDir { directory } {
 
63
                if { ![file exists $directory]  || ![file isdirectory $directory] } {
 
64
                        return -1
 
65
                }
 
66
 
 
67
                # Check for a kopete compatible emoticon pack
 
68
                if {[file exists [file join $directory emoticons.xml]]  &&
 
69
                    [file readable [file join $directory emoticons.xml]]} {
 
70
                        if {[catch {
 
71
                                set xml_fd [open [file join $directory emoticons.xml]]
 
72
                                set xml [xml2list [read $xml_fd]]
 
73
                                close $xml_fd
 
74
 
 
75
                                plugins_log emoticons_importer "Found an emoticons.xml file in $directory"
 
76
                                set i 0
 
77
                                while { 1 } {
 
78
                                        set node [GetXmlNode $xml "messaging-emoticon-map:emoticon" $i]
 
79
                                        if {$node == "" } {
 
80
                                                break
 
81
                                        }
 
82
                                        incr i
 
83
 
 
84
                                        set file [GetXmlAttribute $node "emoticon" "file"]
 
85
                                        set triggers [list]
 
86
                                        set j 0
 
87
                                        while { 1 } {
 
88
                                                set string [GetXmlEntry $node "emoticon:string" $j]
 
89
                                                if {$string == "" } {
 
90
                                                        break
 
91
                                                }
 
92
                                                incr j
 
93
                                                append triggers "$string "
 
94
                                        }
 
95
 
 
96
                                        set name [file rootname $file]
 
97
                                        set r [AddNewCustomEmoticon $name [file join $directory $file] $triggers]
 
98
                                        plugins_log emoticons_importer "Added custom emoticon : $name $file '$triggers' : $r"
 
99
                                }
 
100
                        } res ] } {
 
101
                                plugins_log emoticons_importer "Error importing from emoticons.xml : $res"
 
102
                        } else {
 
103
                                return 0
 
104
                        }
 
105
                }
 
106
                plugins_log emoticons_importer "Globing for files in $directory"
 
107
                set files [glob -nocomplain -tails -type f -directory $directory *]
 
108
                foreach file $files {
 
109
                        set name [file rootname $file]
 
110
                        set triggers "\[$name\]"
 
111
                        set r [AddNewCustomEmoticon $name [file join $directory $file] $triggers]
 
112
                        plugins_log emoticons_importer "Added custom emoticon : $name $file '$triggers' : $r"
 
113
                }
 
114
                return 0
 
115
        }
 
116
 
 
117
        # Return value :
 
118
        # 0 : success
 
119
        # -1 : invalid field
 
120
        # -2 : smiley already exists with that name
 
121
        # -3 : could not find sound file
 
122
        # -4 : could not find the smiley file
 
123
        # -5 : file could not be loaded, wrong format or whatever
 
124
        # -6 : Error converting/resizing the file
 
125
        proc AddNewCustomEmoticon { name file text {casesensitive 0} {sound ""}} {
 
126
                global custom_emotions HOME
 
127
                
 
128
                #Check for needed fields
 
129
                if { $name == "" || $file == "" || $text == "" } {
 
130
                        return -1
 
131
                }
 
132
                if { [info exists custom_emotions($name)] } {
 
133
                        #Smiley exists
 
134
                        return -2
 
135
                }
 
136
 
 
137
                #Check for sound, and copy it
 
138
                if { $sound != "" } {
 
139
                        set filename [getfilename [::skin::GetSkinFile sounds $sound]]
 
140
                        if { $filename == "null" } {
 
141
                                return -3
 
142
                        } else {
 
143
                                create_dir [file join $HOME sounds]
 
144
                                catch { file copy [::skin::GetSkinFile sounds $sound] [file join $HOME sounds]}
 
145
                        }
 
146
                        set emotion(sound) $filename
 
147
                }
 
148
                
 
149
                set filename [getfilename [::skin::GetSkinFile smileys $file]]
 
150
                if { $filename == "null" } {
 
151
                        return -4
 
152
                } 
 
153
                
 
154
                create_dir [file join $HOME smileys]
 
155
                
 
156
                #Check for animation
 
157
                if { [ catch {set emotion(animated) [::picture::IsAnimated [::skin::GetSkinFile smileys $file] ] } res ] } {
 
158
                        #There is an error with the file, wront format or doesn't exist
 
159
                        return -5
 
160
                }
 
161
                if { $emotion(animated) == 0 } { unset emotion(animated) }
 
162
                
 
163
                if { ![info exists emotion(animated)] || $emotion(animated) == 0 } {
 
164
                        image create photo tmp -file [::skin::GetSkinFile smileys $file]
 
165
                        
 
166
                        set filetail_noext [filenoext [file tail $file]]
 
167
                        set destfile [file join $HOME smileys $filetail_noext]
 
168
                        
 
169
                        if { [image width tmp] > 50 || [image height tmp] > 50 } {
 
170
                                #MSN can't show static smileys which are bigger than 50x50 so we resize it
 
171
                                set file [convert_image_plus [::skin::GetSkinFile smileys $file] smileys 50x50]
 
172
                        } else {
 
173
                                #The smiley has size between 19x19 and 50x50 and user doesn't want to resize it so we just convert it to PNG
 
174
                                set filetail_noext [filenoext [file tail $file]]
 
175
                                set destfile [file join $HOME smileys $filetail_noext]
 
176
                                ::picture::Convert [::skin::GetSkinFile smileys $file] "${destfile}.png"
 
177
                                set file "${destfile}.png"
 
178
                        }
 
179
 
 
180
                        # Don't forget to delete the temp image...!
 
181
                        image delete tmp
 
182
 
 
183
                        if { ![file exists $file] } { set file "" }
 
184
                } else {
 
185
                        #We convert animated smiley to animated gif even if we can just load animated gif and save to animated gif
 
186
                        #Don't care of extension : as the smiley is animated TkCximage will save to GIF format
 
187
                        set filetail_noext [filenoext [file tail $file]]
 
188
                        set destfile [file join $HOME smileys $filetail_noext]
 
189
                        ::picture::Convert [::skin::GetSkinFile smileys $file] "${destfile}.png"
 
190
                        set file "${destfile}.png"
 
191
                        if { ![file exists $file] } { set file "" }
 
192
                }
 
193
                
 
194
                if { $file == "" } {
 
195
                        return -6
 
196
                }
 
197
                
 
198
                set emotion(file) $file
 
199
                set emotion(name) $name
 
200
                set emotion(reachable) 1
 
201
                
 
202
                #Create a list of symbols
 
203
                set emotion(text) [list]
 
204
                foreach symbol [split $text] {
 
205
                        if { $symbol != "" } {
 
206
                                lappend emotion(text) $symbol
 
207
                        }
 
208
                }
 
209
                
 
210
                if { $casesensitive == 1} {
 
211
                        set emotion(casesensitive) 1
 
212
                } else {
 
213
                        if { [info exist emotion(casesensitive)] } {unset emotion(casesensitive)}
 
214
                } 
 
215
                
 
216
                
 
217
                
 
218
                set emotion(image_name) [image create photo emoticonCustom_std_$emotion(text) -file $emotion(file) -format cximage]
 
219
                set emotion(preview) [image create photo emoticonCustom_preview_$emotion(text)]
 
220
                $emotion(preview) copy emoticonCustom_std_$emotion(text)
 
221
 
 
222
                set custom_emotions($name) [array get emotion]
 
223
 
 
224
                #load_smileys
 
225
                if { [winfo exists .smile_selector]} {destroy .smile_selector}
 
226
 
 
227
                #After modifying, clear sortedemotions, could need sorting again
 
228
                if {[info exists ::smiley::sortedemotions]} { unset ::smiley::sortedemotions }
 
229
                
 
230
                                
 
231
                #Immediately save settings.xml and update the UI
 
232
                save_config
 
233
                update
 
234
 
 
235
                return 0
 
236
        }
 
237
 
 
238
}