summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/translator/translatorguiclient.cpp
blob: 70f1266f1bf31627d0e9bf5f1229c63ab52fcc8a (plain)
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
/*
    translatorguiclient.cpp

    Kopete Translator plugin

    Copyright (c) 2003-2004 by Olivier Goffart <ogoffart @ kde.org>

    Kopete    (c) 2003-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.                                   *
    *                                                                       *
    *************************************************************************
*/

#include <tqvariant.h>

#include <kdebug.h>
#include <tdeaction.h>
#include <tdelocale.h>

#include "kopetechatsession.h"
#include "kopeteview.h"
#include "kopetecontact.h"
#include "kopetemetacontact.h"
#include "kopetemessage.h"

#include "translatorplugin.h"
#include "translatorguiclient.h"
#include "translatorlanguages.h"

TranslatorGUIClient::TranslatorGUIClient( Kopete::ChatSession *parent, const char *name )
: TQObject( parent, name ), KXMLGUIClient( parent )
{
	setInstance( TranslatorPlugin::plugin()->instance() );
	connect( TranslatorPlugin::plugin(), TQT_SIGNAL( destroyed( TQObject * ) ), this, TQT_SLOT( deleteLater() ) );

	m_manager = parent;

	new TDEAction( i18n( "Translate" ), "locale", CTRL + Key_T, this, TQT_SLOT( slotTranslateChat() ), actionCollection(), "translateCurrentMessage" );
	
	setXMLFile( "translatorchatui.rc" );
}

TranslatorGUIClient::~TranslatorGUIClient()
{
}

void TranslatorGUIClient::slotTranslateChat()
{
	if ( !m_manager->view() )
		return;

	Kopete::Message msg = m_manager->view()->currentMessage();
	TQString body = msg.plainBody();
	if ( body.isEmpty() )
		return;

	TQString src_lang = TranslatorPlugin::plugin()->m_myLang;
	TQString dst_lang;

	TQPtrList<Kopete::Contact> list = m_manager->members();
	Kopete::MetaContact *to = list.first()->metaContact();
	dst_lang = to->pluginData( TranslatorPlugin::plugin(), "languageKey" );
	if ( dst_lang.isEmpty() || dst_lang == "null" )
	{
		kdDebug( 14308 ) << k_funcinfo << "Cannot determine dst Metacontact language (" << to->displayName() << ")" << endl;
		return;
	}

	// We search for src_dst
	TranslatorPlugin::plugin()->translateMessage( body, src_lang, dst_lang, this, TQT_SLOT( messageTranslated( const TQVariant & ) ) );
}

void TranslatorGUIClient::messageTranslated( const TQVariant &result )
{
	TQString translated = result.toString();
	if ( translated.isEmpty() )
	{
		kdDebug( 14308 ) << k_funcinfo << "Empty string returned"  << endl;
		return;
	}

	//if the user close the window before the translation arrive, return
	if ( !m_manager->view() )
		return;

	Kopete::Message msg = m_manager->view()->currentMessage();
	msg.setBody( translated );
	m_manager->view()->setCurrentMessage( msg );
}

#include "translatorguiclient.moc"