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

« back to all changes in this revision

Viewing changes to plugins/whatis/whatis.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
# License: GNU General Public License
 
3
#
 
4
# Plugin written by: Jasper Huzen
 
5
# Comments: feaster@xs4all.nl
 
6
# Version: 1.0 
 
7
##      
 
8
 
 
9
namespace eval ::whatis {
 
10
        
 
11
        #
 
12
   # The initial procedure called when the plugin is started by AMSN
 
13
        #       
 
14
        proc init { dir }       {
 
15
                ::plugins::RegisterPlugin "whatis"
 
16
      
 
17
                # Register event when change status
 
18
                ::plugins::RegisterEvent "whatis" new_chatwindow addButtons
 
19
                        
 
20
                plugins_log "whatis" "plugin registered"
 
21
        }
 
22
 
 
23
        proc manual_langpair { w } {
 
24
                toplevel .whatislp
 
25
                pack [label .whatislp.lbl -text "Enter from and to language codes, separated by space\n(From language code can also be \"auto\")"]
 
26
                pack [entry .whatislp.en]
 
27
                pack [button .whatislp.btn -text "OK"]
 
28
                bind .whatislp.en <Return> [list ::whatis::validateAns $w]
 
29
                bind .whatislp.btn <ButtonRelease-1> [list ::whatis::validateAns $w]
 
30
        }
 
31
 
 
32
        proc validateAns { w } {
 
33
                set langpair [string trim [.whatislp.en get]]
 
34
                if { $langpair == "" } {
 
35
                        destroy .whatislp
 
36
                        return
 
37
                }
 
38
                set spacepos [string first " " $langpair]
 
39
                if { $spacepos < 0 } {
 
40
                        tk_messageBox -message "Please enter a valid language pair, separated by space" -type ok
 
41
                        return
 
42
                }
 
43
                set fromLang [string range $langpair 0 [expr {$spacepos - 1}]]
 
44
                set toLang [string range $langpair [expr {$spacepos + 1}] end]
 
45
                translateText $w $fromLang $toLang
 
46
                destroy .whatislp
 
47
        }
 
48
        
 
49
        #
 
50
        # Get the selected text from the chatWindow
 
51
        # 
 
52
        proc getSelectedText { w } {
 
53
           if { $::version == "0.94" } {
 
54
                        set window $w.f.bottom.in.input
 
55
                
 
56
                        if { [ catch {$window tag ranges sel}]} {
 
57
                                set window $w
 
58
                        }
 
59
                
 
60
                        set index [$window tag ranges sel]
 
61
                
 
62
                        if { $index == "" } {
 
63
                                set window $w.f.out.text
 
64
                                catch {set index [$window tag ranges sel]}
 
65
                                if { $index == "" } {  return }
 
66
                        }
 
67
                } else { 
 
68
                        if { [ catch {set window [::ChatWindow::GetInputText $w]} ]} {
 
69
                                set window $w
 
70
                        }
 
71
                        
 
72
                        set index [$window tag ranges sel]
 
73
                
 
74
                        if { $index == "" } {
 
75
                                set window [::ChatWindow::GetOutText $w]
 
76
                                catch {set index [$window tag ranges sel]}
 
77
                                if { $index == "" } {  return }
 
78
                        }
 
79
                }
 
80
                        
 
81
                set dump [$window dump -text [lindex $index 0] [lindex $index 1]]
 
82
                
 
83
                if { $dump == "" } return
 
84
                
 
85
                foreach { text output index } $dump {
 
86
                        append tpmVar $output
 
87
                }
 
88
                
 
89
                return $tpmVar  
 
90
                        
 
91
        }
 
92
        
 
93
        #
 
94
        # Add menu items to the rightbutton menu
 
95
        #
 
96
        proc addButtons { event evpar } {
 
97
                upvar 2 $evpar args
 
98
                
 
99
                plugins_log "whatis" "Add buttons to window"
 
100
                        
 
101
                #Define variables
 
102
                set w $args(win)
 
103
                set copymenu $w.copy
 
104
                addEntriesToMenu $copymenu $w
 
105
                set copypastemenu $w.copypaste
 
106
                addEntriesToMenu $copypastemenu $w
 
107
        }
 
108
 
 
109
        proc addEntriesToMenu { copymenu w } {
 
110
 
 
111
                $copymenu add cascade -label "What is" -menu $copymenu.whatis
 
112
                menu $copymenu.whatis -tearoff 0 -type normal
 
113
                
 
114
                $copymenu.whatis add cascade -label "Wikipedia" -menu $copymenu.whatis.wikipedia
 
115
                menu $copymenu.whatis.wikipedia -tearoff 0 -type normal
 
116
                $copymenu.whatis.wikipedia add command -label "English encyclopedia" -command "whatis::getFromWikipida $w en"   
 
117
                $copymenu.whatis.wikipedia add command -label "Dutch encyclopedia" -command "whatis::getFromWikipida $w nl"     
 
118
                $copymenu.whatis.wikipedia add command -label "Spanish encyclopedia" -command "whatis::getFromWikipida $w es"   
 
119
                $copymenu.whatis.wikipedia add command -label "German encyclopedia" -command "whatis::getFromWikipida $w de"    
 
120
                $copymenu.whatis.wikipedia add command -label "French encyclopedia" -command "whatis::getFromWikipida $w fr"    
 
121
                $copymenu.whatis.wikipedia add command -label "Italian encyclopedia" -command "whatis::getFromWikipida $w it"   
 
122
                $copymenu.whatis.wikipedia add command -label "Portuguese encyclopedia" -command "whatis::getFromWikipida $w pt"        
 
123
                $copymenu.whatis.wikipedia add command -label "Polish encyclopedia" -command "whatis::getFromWikipida $w pl"    
 
124
                $copymenu.whatis.wikipedia add command -label "Sweden encyclopedia" -command "whatis::getFromWikipida $w sv"    
 
125
                
 
126
                $copymenu.whatis add cascade -label "Translate" -menu $copymenu.whatis.translate
 
127
                menu $copymenu.whatis.translate -tearoff 0 -type normal
 
128
                $copymenu.whatis.translate add command -label "Auto-detect to English" -command "whatis::translateText $w auto en"
 
129
                $copymenu.whatis.translate add command -label "English to Spanish" -command "whatis::translateText $w en es"    
 
130
                $copymenu.whatis.translate add command -label "English to French" -command "whatis::translateText $w en fr"
 
131
                $copymenu.whatis.translate add command -label "English to German" -command "whatis::translateText $w en de"     
 
132
                $copymenu.whatis.translate add command -label "English to Italian" -command "whatis::translateText $w en it"
 
133
                $copymenu.whatis.translate add command -label "English to Dutch" -command "whatis::translateText $w en nl"      
 
134
                $copymenu.whatis.translate add command -label "English to Portuguese" -command "whatis::translateText $w en pt"
 
135
                $copymenu.whatis.translate add command -label "English to Russian" -command "whatis::translateText $w en ru"    
 
136
                $copymenu.whatis.translate add command -label "English to Greek" -command "whatis::translateText $w en el"
 
137
                $copymenu.whatis.translate add command -label "English to Japanese"  -command "whatis::translateText $w en ja"
 
138
                $copymenu.whatis.translate add command -label "English to Chinese (simpl)" -command "whatis::translateText $w en zh"
 
139
                $copymenu.whatis.translate add command -label "Dutch to English" -command "whatis::translateText $w nl en"
 
140
                $copymenu.whatis.translate add command -label "Spanish to English" -command "whatis::translateText $w es en"    
 
141
                $copymenu.whatis.translate add command -label "German to English" -command "whatis::translateText $w de en"     
 
142
                $copymenu.whatis.translate add command -label "Italian to English" -command "whatis::translateText $w it en"    
 
143
                $copymenu.whatis.translate add command -label "Portuguese to English" -command "whatis::translateText $w pt en" 
 
144
                $copymenu.whatis.translate add command -label "French to English" -command "whatis::translateText $w fr en"             
 
145
                $copymenu.whatis.translate add command -label "Russian to English" -command "whatis::translateText $w ru en"    
 
146
                $copymenu.whatis.translate add command -label "Greek to English" -command "whatis::translateText $w el en"
 
147
                $copymenu.whatis.translate add command -label "Japanese to English" -command "whatis::translateText $w ja en"
 
148
                $copymenu.whatis.translate add command -label "Chinese (simpl) to English" -command "whatis::translateText $w zh en"
 
149
                $copymenu.whatis.translate add command -label "Other..." -command "whatis::manual_langpair $w"
 
150
        }
 
151
        
 
152
        #
 
153
        # This proc will call a Wikipedia page
 
154
        #
 
155
        proc getFromWikipida { w dict} {
 
156
                set searchText [getSelectedText $w]
 
157
                status_log "whatis" "Check Wikipedia: $searchText"
 
158
        
 
159
                launch_browser "http://$dict.wikipedia.org/wiki/[string tolower [ urlencode $searchText]]" 
 
160
                
 
161
                # Write a seperated line to the chat window
 
162
                # Versions above 0.94 can use a graphical build-in line so I use this call
 
163
                ::amsn::WinWrite [::ChatWindow::Name $w] "\n" green
 
164
                        
 
165
                # Write a seperated line to the chat window
 
166
                # Versions above 0.94 can use a graphical build-in line so I use this call
 
167
                if { $::version == "0.94" } {
 
168
                                ::amsn::WinWrite [::ChatWindow::Name $w] "--------------------------------------------------" gray_italic        
 
169
                } else {
 
170
                                ::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3 
 
171
                } 
 
172
                ::amsn::WinWrite [::ChatWindow::Name $w] "\n" green
 
173
                ::amsn::WinWriteIcon [::ChatWindow::Name $w] miniinfo 
 
174
                ::amsn::WinWrite [::ChatWindow::Name $w] " Encyclopedia started at internet browser!\n" green
 
175
                
 
176
                # Write a seperated line to the chat window
 
177
                # Versions above 0.94 can use a graphical build-in line so I use this call
 
178
                if { $::version == "0.94" } {
 
179
                                ::amsn::WinWrite [::ChatWindow::Name $w] "--------------------------------------------------\n" gray_italic      
 
180
                } else {
 
181
                                ::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3 
 
182
                } 
 
183
        }
 
184
 
 
185
        #Thanks to the Translate plugin for this one!
 
186
        proc convert {string} {
 
187
 
 
188
                set x [string length $string]
 
189
                for {set i 0} {$i<$x} {incr i} {
 
190
                        append output "&#[scan [string range $string $i $i] %c];"
 
191
                }
 
192
                return $output
 
193
        }
 
194
        
 
195
        #
 
196
        # This proc will translate a text 
 
197
        #
 
198
        proc translateText { w fromLang toLang } {
 
199
                set searchText [getSelectedText $w]
 
200
                set html ""
 
201
                if { $searchText == "" } {
 
202
                        ::amsn::messageBox "No text selected for translation!" ok error 
 
203
                } else { 
 
204
                #set searchText [encoding convertto utf-8 $searchText]
 
205
                
 
206
                        set url "http://translate.google.com/translate_t?sl=$fromLang&tl=$toLang"
 
207
                        set query [::http::formatQuery hl "en" ie "UTF8" text [convert $searchText] sl $fromLang tl $toLang]
 
208
                        if {[catch {::http::geturl $url -query $query -timeout 72500 -headers {User-Agent "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.6) Gecko/20091215 Ubuntu/9.10 (karmic) Firefox/3.5.6"} -command [list ::whatis::translateCB $w $fromLang $toLang]} res ]} {
 
209
                                # Write translated text to the chatwindow
 
210
                                ::amsn::WinWrite [::ChatWindow::Name $w] "\n" green
 
211
                                if { $::version == "0.94" } {
 
212
                                                ::amsn::WinWrite [::ChatWindow::Name $w] "--------------------------------------------------" gray_italic
 
213
                                } else {
 
214
                                                ::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3
 
215
                                }
 
216
                                ::amsn::WinWrite [::ChatWindow::Name $w] "\n" green
 
217
                                ::amsn::WinWriteIcon [::ChatWindow::Name $w] miniinfo
 
218
                                ::amsn::WinWrite [::ChatWindow::Name $w] "\nError in translation: $res\n" red
 
219
                                if { $::version == "0.94" } {
 
220
                                                ::amsn::WinWrite [::ChatWindow::Name $w] "--------------------------------------------------\n" gray_italic
 
221
                                } else {
 
222
                                                ::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3
 
223
                                }
 
224
                                
 
225
                        }
 
226
                }
 
227
        }
 
228
 
 
229
        proc translateCB {w fromLang toLang http} {
 
230
                set html [::http::data $http]
 
231
                # Set a language title
 
232
                switch $toLang {
 
233
                                "nl" { set langTitle "Dutch" } 
 
234
                                "es" { set langTitle "Spanish" }                
 
235
                                "de" { set langTitle "German" }         
 
236
                                "fr" { set langTitle "French"}          
 
237
                                "it" { set langTitle "Italian"}         
 
238
                                "pt" { set langTitle "Portuguese" }             
 
239
                                "ru" { set langTitle "Russian" }                
 
240
                                "el" { set langTitle "Greek" }
 
241
                                "ja" { set langTitle "Japanese" }
 
242
                                "zh" { set langTitle "Chinese (simpl)" }
 
243
                                "en" { set langTitle "English" }
 
244
                                default { set langTitle "selected language" }
 
245
                }
 
246
                # Strip HTML before translated text 
 
247
                set substring "<span id=result_box"
 
248
                set start [string first $substring $html]
 
249
                if { $start == -1 } {
 
250
                        set translation "Problem parsing return text. Translation engine probably returned an error"
 
251
                } else {
 
252
                        set start [expr { $start + [string length $substring]} ]
 
253
                        set direction [string range $html $start [expr { [string first "\"" $html $start] - 1}]]
 
254
                        set start [string first ">" $html $start]
 
255
                        set substring "<span title=\""
 
256
                        set start [expr { $start + [string length $substring]} ]
 
257
                        set start [string first ">" $html $start]
 
258
                        set start [expr { $start + 1 }]
 
259
                
 
260
                        # Stript HTML after translated text
 
261
                        set htmlPart [string range $html $start end]
 
262
                        set end [expr { [string first "</" $htmlPart] - 1}]
 
263
                        set translation [string range $htmlPart 0 $end]
 
264
                        if { $direction == "rtl" } { set translation [string reverse $translation] }
 
265
                        #set translation [encoding convertfrom utf-8 $translation]
 
266
                }
 
267
                # Write translated text to the chatwindow
 
268
                ::amsn::WinWrite [::ChatWindow::Name $w] "\n" green
 
269
                if { $::version == "0.94" } {
 
270
                                ::amsn::WinWrite [::ChatWindow::Name $w] "--------------------------------------------------" gray_italic        
 
271
                } else {
 
272
                                ::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3 
 
273
                } 
 
274
                ::amsn::WinWrite [::ChatWindow::Name $w] "\n" green
 
275
                ::amsn::WinWriteIcon [::ChatWindow::Name $w] miniinfo 
 
276
                ::amsn::WinWrite [::ChatWindow::Name $w] " Translated text to $langTitle:" green
 
277
                ::amsn::WinWrite [::ChatWindow::Name $w] "\n$translation\n" gray
 
278
                if { $::version == "0.94" } {
 
279
                                ::amsn::WinWrite [::ChatWindow::Name $w] "--------------------------------------------------\n" gray_italic      
 
280
                } else {
 
281
                                ::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3 
 
282
                } 
 
283
        }
 
284
 
 
285
        #
 
286
   # The deinitial procedure called when the plugin is unload by AMSN
 
287
        #
 
288
        proc deInit { } {
 
289
                
 
290
                # Unregister the events from the plugin
 
291
                ::plugins::UnRegisterEvents "whatis"
 
292
                plugins_log "whatis" "plugin unregistered"
 
293
                
 
294
        }
 
295
        
 
296