2
# License: GNU General Public License
4
# Plugin written by: Jasper Huzen
5
# Comments: feaster@xs4all.nl
9
namespace eval ::whatis {
12
# The initial procedure called when the plugin is started by AMSN
15
::plugins::RegisterPlugin "whatis"
17
# Register event when change status
18
::plugins::RegisterEvent "whatis" new_chatwindow addButtons
20
plugins_log "whatis" "plugin registered"
23
proc manual_langpair { w } {
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]
32
proc validateAns { w } {
33
set langpair [string trim [.whatislp.en get]]
34
if { $langpair == "" } {
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
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
50
# Get the selected text from the chatWindow
52
proc getSelectedText { w } {
53
if { $::version == "0.94" } {
54
set window $w.f.bottom.in.input
56
if { [ catch {$window tag ranges sel}]} {
60
set index [$window tag ranges sel]
63
set window $w.f.out.text
64
catch {set index [$window tag ranges sel]}
65
if { $index == "" } { return }
68
if { [ catch {set window [::ChatWindow::GetInputText $w]} ]} {
72
set index [$window tag ranges sel]
75
set window [::ChatWindow::GetOutText $w]
76
catch {set index [$window tag ranges sel]}
77
if { $index == "" } { return }
81
set dump [$window dump -text [lindex $index 0] [lindex $index 1]]
83
if { $dump == "" } return
85
foreach { text output index } $dump {
94
# Add menu items to the rightbutton menu
96
proc addButtons { event evpar } {
99
plugins_log "whatis" "Add buttons to window"
104
addEntriesToMenu $copymenu $w
105
set copypastemenu $w.copypaste
106
addEntriesToMenu $copypastemenu $w
109
proc addEntriesToMenu { copymenu w } {
111
$copymenu add cascade -label "What is" -menu $copymenu.whatis
112
menu $copymenu.whatis -tearoff 0 -type normal
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"
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"
153
# This proc will call a Wikipedia page
155
proc getFromWikipida { w dict} {
156
set searchText [getSelectedText $w]
157
status_log "whatis" "Check Wikipedia: $searchText"
159
launch_browser "http://$dict.wikipedia.org/wiki/[string tolower [ urlencode $searchText]]"
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
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
170
::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3
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
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
181
::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3
185
#Thanks to the Translate plugin for this one!
186
proc convert {string} {
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];"
196
# This proc will translate a text
198
proc translateText { w fromLang toLang } {
199
set searchText [getSelectedText $w]
201
if { $searchText == "" } {
202
::amsn::messageBox "No text selected for translation!" ok error
204
#set searchText [encoding convertto utf-8 $searchText]
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
214
::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3
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
222
::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3
229
proc translateCB {w fromLang toLang http} {
230
set html [::http::data $http]
231
# Set a language title
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" }
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"
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 }]
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]
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
272
::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3
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
281
::amsn::WinWriteIcon [::ChatWindow::Name $w] greyline 3
286
# The deinitial procedure called when the plugin is unload by AMSN
290
# Unregister the events from the plugin
291
::plugins::UnRegisterEvents "whatis"
292
plugins_log "whatis" "plugin unregistered"