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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/*
translatorplugin.cpp
Kopete Translator plugin
Copyright (c) 2001-2002 by Duncan Mac-Vicar Prett <duncan@kde.org>
Copyright (c) 2002-2004 by Olivier Goffart <ogoffart @ kde.org>
Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
*************************************************************************
* *
* This program 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. *
* *
*************************************************************************
Patched by Francesco Rossi <redsh@email.it> in order to support new
google translation page tqlayout (13-sept-2007)
*/
#include <tqapplication.h>
#include <tqregexp.h>
#include <tqsignal.h>
#include <tqstring.h>
#include <kdebug.h>
#include <kaction.h>
#include <kgenericfactory.h>
#include <kglobal.h>
#include <kconfig.h>
#include <kdeversion.h>
#include <kaboutdata.h>
#include "kopetemetacontact.h"
#include "kopetecontactlist.h"
#include "kopetechatsessionmanager.h"
#include "kopetecontact.h"
#include "translatorplugin.h"
#include "translatordialog.h"
#include "translatorguiclient.h"
#include "translatorlanguages.h"
typedef KGenericFactory<TranslatorPlugin> TranslatorPluginFactory;
#if KDE_IS_VERSION(3,2,90)
static const KAboutData aboutdata("kopete_translator", I18N_NOOP("Translator") , "1.0" );
K_EXPORT_COMPONENT_FACTORY( kopete_translator, TranslatorPluginFactory( &aboutdata ) )
#else
K_EXPORT_COMPONENT_FACTORY( kopete_translator, TranslatorPluginFactory( "kopete_translator" ) )
#endif
TranslatorPlugin::TranslatorPlugin( TQObject *tqparent, const char *name, const TQStringList & /* args */ )
: Kopete::Plugin( TranslatorPluginFactory::instance(), tqparent, name )
{
kdDebug( 14308 ) << k_funcinfo << endl;
if ( pluginStatic_ )
kdWarning( 14308 ) << k_funcinfo << "Translator already initialized" << endl;
else
pluginStatic_ = this;
m_languages = new TranslatorLanguages;
connect( Kopete::ChatSessionManager::self(), TQT_SIGNAL( aboutToDisplay( Kopete::Message & ) ),
this, TQT_SLOT( slotIncomingMessage( Kopete::Message & ) ) );
connect( Kopete::ChatSessionManager::self(), TQT_SIGNAL( aboutToSend( Kopete::Message & ) ),
this, TQT_SLOT( slotOutgoingMessage( Kopete::Message & ) ) );
connect( Kopete::ChatSessionManager::self(), TQT_SIGNAL( chatSessionCreated( Kopete::ChatSession * ) ),
this, TQT_SLOT( slotNewKMM( Kopete::ChatSession * ) ) );
TQStringList keys;
TQMap<TQString, TQString> m = m_languages->languagesMap();
for ( int k = 0; k <= m_languages->numLanguages(); k++ )
keys << m[ m_languages->languageKey( k ) ];
m_actionLanguage = new KSelectAction( i18n( "Set &Language" ), "locale", 0, actionCollection(), "contactLanguage" );
m_actionLanguage->setItems( keys );
connect( m_actionLanguage, TQT_SIGNAL( activated() ), this, TQT_SLOT(slotSetLanguage() ) );
connect( Kopete::ContactList::self(), TQT_SIGNAL( metaContactSelected( bool ) ), this, TQT_SLOT( slotSelectionChanged( bool ) ) );
setXMLFile( "translatorui.rc" );
//Add GUI action to all already existing kmm (if the plugin is launched when kopete already rining)
TQValueList<Kopete::ChatSession*> sessions = Kopete::ChatSessionManager::self()->sessions();
for (TQValueListIterator<Kopete::ChatSession*> it= sessions.begin(); it!=sessions.end() ; ++it)
slotNewKMM( *it );
loadSettings();
connect( this, TQT_SIGNAL( settingsChanged() ), this, TQT_SLOT( loadSettings() ) );
}
TranslatorPlugin::~TranslatorPlugin()
{
kdDebug( 14308 ) << k_funcinfo << endl;
pluginStatic_ = 0L;
}
TranslatorPlugin* TranslatorPlugin::plugin()
{
return pluginStatic_;
}
TranslatorPlugin* TranslatorPlugin::pluginStatic_ = 0L;
void TranslatorPlugin::loadSettings()
{
KConfig *config = KGlobal::config();
int mode = 0;
config->setGroup( "Translator Plugin" );
m_myLang = m_languages->languageKey( config->readNumEntry( "myLang" , 0 ) );
m_service = m_languages->serviceKey( config->readNumEntry( "Service", 0 ) );
if ( config->readBoolEntry( "IncomingDontTranslate", true ) )
mode = 0;
else if ( config->readBoolEntry( "IncomingShowOriginal", false ) )
mode = 1;
else if ( config->readBoolEntry( "IncomingTranslate", false ) )
mode = 2;
m_incomingMode = mode;
if ( config->readBoolEntry( "OutgoingDontTranslate", true ) )
mode = 0;
else if ( config->readBoolEntry( "OutgoingShowOriginal", false ) )
mode = 1;
else if ( config->readBoolEntry( "OutgoingTranslate", false ) )
mode = 2;
else if ( config->readBoolEntry( "OutgoingAsk", false ) )
mode = 3;
m_outgoingMode = mode;
}
void TranslatorPlugin::slotSelectionChanged( bool b )
{
m_actionLanguage->setEnabled( b );
if ( !b )
return;
Kopete::MetaContact *m = Kopete::ContactList::self()->selectedMetaContacts().first();
if( !m )
return;
TQString languageKey = m->pluginData( this, "languageKey" );
if ( !languageKey.isEmpty() && languageKey != "null" )
m_actionLanguage->setCurrentItem( m_languages->languageIndex( languageKey ) );
else
m_actionLanguage->setCurrentItem( m_languages->languageIndex( "null" ) );
}
void TranslatorPlugin::slotNewKMM( Kopete::ChatSession *KMM )
{
new TranslatorGUIClient( KMM );
}
void TranslatorPlugin::slotIncomingMessage( Kopete::Message &msg )
{
if ( m_incomingMode == DontTranslate )
return;
TQString src_lang;
TQString dst_lang;
if ( ( msg.direction() == Kopete::Message::Inbound ) && !msg.plainBody().isEmpty() )
{
Kopete::MetaContact *from = msg.from()->metaContact();
if ( !from )
{
// kdDebug( 14308 ) << k_funcinfo << "No metaContact for source contact" << endl;
return;
}
src_lang = from->pluginData( this, "languageKey" );
if( src_lang.isEmpty() || src_lang == "null" )
{
// kdDebug( 14308 ) << k_funcinfo << "Cannot determine src Metacontact language (" << from->displayName() << ")" << endl;
return;
}
dst_lang = m_myLang;
sendTranslation( msg, translateMessage( msg.plainBody(), src_lang, dst_lang ) );
}
}
void TranslatorPlugin::slotOutgoingMessage( Kopete::Message &msg )
{
if ( m_outgoingMode == DontTranslate )
return;
TQString src_lang;
TQString dst_lang;
if ( ( msg.direction() == Kopete::Message::Outbound ) && ( !msg.plainBody().isEmpty() ) )
{
src_lang = m_myLang;
// Sad, we have to consider only the first To: metacontact
Kopete::MetaContact *to = msg.to().first()->metaContact();
if ( !to )
{
// kdDebug( 14308 ) << k_funcinfo << "No metaContact for first contact" << endl;
return;
}
dst_lang = to->pluginData( this, "languageKey" );
if ( dst_lang.isEmpty() || dst_lang == "null" )
{
// kdDebug( 14308 ) << k_funcinfo << "Cannot determine dst Metacontact language (" << to->displayName() << ")" << endl;
return;
}
sendTranslation( msg, translateMessage( msg.plainBody(), src_lang, dst_lang ) );
}
}
void TranslatorPlugin::translateMessage( const TQString &msg, const TQString &from, const TQString &to, TQObject *obj, const char* slot )
{
TQSignal completeSignal;
completeSignal.connect( obj, slot );
TQString result = translateMessage( msg, from, to );
if(!result.isNull())
{
completeSignal.setValue( result );
completeSignal.activate();
}
}
TQString TranslatorPlugin::translateMessage( const TQString &msg, const TQString &from, const TQString &to )
{
if ( from == to )
{
kdDebug( 14308 ) << k_funcinfo << "Src and Dst languages are the same" << endl;
return TQString();
}
// We search for src_dst
if(! m_languages->supported( m_service ).tqcontains( from + "_" + to ) )
{
kdDebug( 14308 ) << k_funcinfo << from << "_" << to << " is not supported by service " << m_service << endl;
return TQString();
}
if ( m_service == "babelfish" )
return babelTranslateMessage( msg ,from, to );
else if ( m_service == "google" )
return googleTranslateMessage( msg ,from, to );
else
return TQString();
}
TQString TranslatorPlugin::googleTranslateMessage( const TQString &msg, const TQString &from, const TQString &to )
{
KURL translatorURL ( "http://translate.google.com/translate_t");
TQString body = KURL::encode_string( msg );
TQString lp = from + "|" + to;
TQCString postData = TQString( "text=" + body + "&langpair=" + lp ).utf8();
TQString gurl = "http://translate.google.com/translate_t?text=" + body +"&langpair=" + lp;
kdDebug(14308) << k_funcinfo << " URL: " << gurl << endl;
KURL geturl ( gurl );
KIO::TransferJob *job = KIO::get( geturl, false, true );
//job = KIO::http_post( translatorURL, postData, true );
//job->addMetaData( "content-type", "application/x-www-form-urlencoded" );
//job->addMetaData( "referrer", "http://www.google.com" );
TQObject::connect( job, TQT_SIGNAL( data( KIO::Job *, const TQByteArray & ) ), this, TQT_SLOT( slotDataReceived( KIO::Job *, const TQByteArray & ) ) );
TQObject::connect( job, TQT_SIGNAL( result( KIO::Job * ) ), this, TQT_SLOT( slotJobDone( KIO::Job * ) ) );
// KIO is async and we use a sync API, so use the processEvents hack to work around that
// FIXME: We need to make the libkopete API async to get rid of this processEvents.
// It often causes crashes in the code. - Martijn
while ( !m_completed[ job ] )
tqApp->processEvents();
TQString data = TQString::tqfromLatin1( m_data[ job ] );
// After hacks, we need to clean
m_data.remove( job );
m_completed.remove( job );
// kdDebug( 14308 ) << k_funcinfo << "Google response:"<< endl << data << endl;
// TQRegExp re( "<textarea name=q rows=5 cols=45 wrap=PHYSICAL>(.*)</textarea>" );
TQRegExp re( "<textarea name=utrans wrap=PHYSICAL dilr=ltr rows=5 id=suggestion>(.*)</textarea>");
re.setMinimal( true );
re.search( data );
return re.cap( 1 );
}
TQString TranslatorPlugin::babelTranslateMessage( const TQString &msg, const TQString &from, const TQString &to )
{
TQString body = KURL::encode_string( msg);
TQString lp = from + "_" + to;
TQString gurl = "http://babelfish.altavista.com/babelfish/tr?enc=utf8&doit=done&tt=urltext&urltext=" + body + "&lp=" + lp;
KURL geturl ( gurl );
kdDebug( 14308 ) << k_funcinfo << "URL: " << gurl << endl;
KIO::TransferJob *job = KIO::get( geturl, false, true );
TQObject::connect( job, TQT_SIGNAL( data( KIO::Job *, const TQByteArray & ) ), this, TQT_SLOT( slotDataReceived( KIO::Job *, const TQByteArray & ) ) );
TQObject::connect( job, TQT_SIGNAL( result( KIO::Job * ) ), this, TQT_SLOT( slotJobDone( KIO::Job * ) ) );
// KIO is async and we use a sync API, so use the processEvents hack to work around that
// FIXME: We need to make the libkopete API async to get rid of this processEvents.
// It often causes crashes in the code. - Martijn
while ( !m_completed[ job ] )
tqApp->processEvents();
TQString data = TQString::fromUtf8( m_data[ job ] );
// After hacks, we need to clean
m_data.remove( job );
m_completed.remove( job );
//kdDebug( 14308 ) << k_funcinfo << "Babelfish response: " << endl << data << endl;
// TQRegExp re( "<Div style=padding:10px; lang=..>(.*)</div" );
TQRegExp re( "<div style=padding:10px;>(.*)</div>" );
re.setMinimal( true );
re.search( data );
return re.cap( 1 );
}
void TranslatorPlugin::sendTranslation( Kopete::Message &msg, const TQString &translated )
{
if ( translated.isEmpty() )
{
kdWarning( 14308 ) << k_funcinfo << "Translated text is empty" << endl;
return;
}
TranslateMode mode = DontTranslate;
switch ( msg.direction() )
{
case Kopete::Message::Outbound:
mode = TranslateMode( m_outgoingMode );
break;
case Kopete::Message::Inbound:
mode = TranslateMode( m_incomingMode );
break;
default:
kdWarning( 14308 ) << k_funcinfo << "Can't determine if it is an incoming or outgoing message" << endl;
};
switch ( mode )
{
case JustTranslate:
msg.setBody( translated, msg.format() );
break;
case ShowOriginal:
msg.setBody( i18n( "%2 \nAuto Translated: \n%1" ).tqarg( translated, msg.plainBody() ), msg.format() );
break;
case ShowDialog:
{
TranslatorDialog *d = new TranslatorDialog( translated );
d->exec();
msg.setBody( d->translatedText(), msg.format() );
delete d;
break;
}
case DontTranslate:
default:
//do nothing
break;
};
}
void TranslatorPlugin::slotDataReceived ( KIO::Job *job, const TQByteArray &data )
{
m_data[ job ] += TQCString( data, data.size() + 1 );
}
void TranslatorPlugin::slotJobDone ( KIO::Job *job )
{
m_completed[ job ] = true;
TQObject::disconnect( job, TQT_SIGNAL( data( KIO::Job *, const TQByteArray & ) ), this, TQT_SLOT( slotDataReceived( KIO::Job *, const TQByteArray & ) ) );
TQObject::disconnect( job, TQT_SIGNAL( result( KIO::Job * ) ), this, TQT_SLOT( slotJobDone( KIO::Job * ) ) );
}
void TranslatorPlugin::slotSetLanguage()
{
Kopete::MetaContact *m = Kopete::ContactList::self()->selectedMetaContacts().first();
if( m && m_actionLanguage )
m->setPluginData( this, "languageKey", m_languages->languageKey( m_actionLanguage->currentItem() ) );
}
#include "translatorplugin.moc"
// vim: set noet ts=4 sts=4 sw=4:
|