summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/winpopup/wpprotocol.cpp
blob: 8e21a16daf5a78cd55cacde8df3d69e19f3f96c0 (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
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
/***************************************************************************
                          wpprotocol.cpp  -  WP Plugin
                             -------------------
    begin                : Fri Apr 26 2002
    copyright            : (C) 2002 by Gav Wood
    email                : gav@kde.org

    Based on code from   : (C) 2002 by Duncan Mac-Vicar Prett
    email                : duncan@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.                                   *
 *                                                                         *
 ***************************************************************************/

// QT Includes
#include <tqmap.h>
#include <tqdict.h>

// KDE Includes
#include <kapplication.h>
#include <kdebug.h>
#include <kgenericfactory.h>
#include <kmessagebox.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>

// Kopete Includes
#include "kopeteaccountmanager.h"
#include "kopeteuiglobal.h"

// Local Includes
#include "wpprotocol.h"
#include "wpeditaccount.h"
#include "wpaccount.h"
#include "wpcontact.h"

class KPopupMenu;

WPProtocol *WPProtocol::sProtocol = 0;

typedef KGenericFactory<WPProtocol> WPProtocolFactory;
K_EXPORT_COMPONENT_FACTORY( kopete_wp, WPProtocolFactory( "kopete_wp" )  )

// WP Protocol
WPProtocol::WPProtocol( TQObject *parent, const char *name, const TQStringList & /* args */ )
: Kopete::Protocol( WPProtocolFactory::instance(), parent, name ),
	WPOnline(  Kopete::OnlineStatus::Online,  25, this, 0,  TQString(), i18n("Online"),  i18n("Online")),
	WPAway(    Kopete::OnlineStatus::Away,    20, this, 1,  "wp_away",     i18n("Away"),    i18n("Away")),
	WPOffline( Kopete::OnlineStatus::Offline, 0,  this, 2,  TQString(), i18n("Offline"), i18n("Offline"))
{
//	kdDebug(14170) << "WPProtocol::WPProtocol()" << endl;

	sProtocol = this;

	// Load Status Actions
//	initActions();
	// TODO: Maybe use this in the future?

	addAddressBookField( "messaging/winpopup", Kopete::Plugin::MakeIndexField );

	readConfig();

	popupClient = new WinPopupLib(smbClientBin, groupCheckFreq);
	TQObject::connect(popupClient, TQT_SIGNAL(signalNewMessage(const TQString &, const TQDateTime &, const TQString &)),
		this, TQT_SLOT(slotReceivedMessage(const TQString &, const TQDateTime &, const TQString &)));
}

// Destructor
WPProtocol::~WPProtocol()
{
//	kdDebug(14170) <<  "WPProtocol::~WPProtocol()" << endl;

	sProtocol = 0;
}

AddContactPage *WPProtocol::createAddContactWidget(TQWidget *parent, Kopete::Account *theAccount)
{
//	kdDebug(14170) << "WPProtocol::createAddContactWidget(<parent>, " << theAccount << ")" << endl;

	return new WPAddContact(parent, dynamic_cast<WPAccount *>(theAccount));
}

Kopete::Contact *WPProtocol::deserializeContact( Kopete::MetaContact *metaContact,
	const TQMap<TQString, TQString> &serializedData,
	const TQMap<TQString, TQString> & /* addressBookData */ )
{
	TQString contactId = serializedData[ "contactId" ];
	TQString accountId = serializedData[ "accountId" ];

	WPAccount *theAccount = static_cast<WPAccount *>(Kopete::AccountManager::self()->findAccount(protocol()->pluginId(), accountId));
	if(!theAccount)	{
		kdDebug(14170) <<  "Account " << accountId << " not found" << endl;
		return 0;
	}

	if(theAccount->contacts()[contactId]) {
		kdDebug(14170) << "User " << contactId << " already in contacts map" << endl;
		return 0;
	}

	theAccount->addContact(contactId, metaContact, Kopete::Account::DontChangeKABC);
	return theAccount->contacts()[contactId];
}

KopeteEditAccountWidget *WPProtocol::createEditAccountWidget(Kopete::Account *account, TQWidget *parent)
{
	return new WPEditAccount(parent, account);
}

Kopete::Account *WPProtocol::createNewAccount(const TQString &accountId)
{
	return new WPAccount(this, accountId);
}

void WPProtocol::settingsChanged()
{
	kdDebug(14170) <<  "WPProtocol::slotSettingsChanged()" << endl;

	readConfig();
	popupClient->settingsChanged(smbClientBin, groupCheckFreq);
}

void WPProtocol::readConfig()
{
	KGlobal::config()->setGroup("WinPopup");
	smbClientBin = KGlobal::config()->readEntry("SmbcPath", "/usr/bin/smbclient");
	groupCheckFreq = KGlobal::config()->readNumEntry("HostCheckFreq", 60);
}

void WPProtocol::installSamba()
{
//	kdDebug(14170) <<  "WPProtocol::installSamba()" endl;

	TQStringList args;
	args += KStandardDirs::findExe("winpopup-install.sh");
	args += KStandardDirs::findExe("winpopup-send.sh");
	if (TDEApplication::tdeinitExecWait("tdesu", args) == 0)
		KMessageBox::information(Kopete::UI::Global::mainWidget(), i18n("The Samba configuration file is modified."), i18n("Configuration Succeeded"));
	else
		KMessageBox::error(Kopete::UI::Global::mainWidget(), i18n("Updating the Samba configuration file failed."), i18n("Configuration Failed"));
}

/**
 * search the contact for the new message and give it to its account
 */
void WPProtocol::slotReceivedMessage(const TQString &Body, const TQDateTime &Time, const TQString &From)
{
	bool foundContact = false;
	TQString accountKey = TQString();
	TQDict<Kopete::Account> Accounts = Kopete::AccountManager::self()->accounts(protocol());
	for (TQDictIterator<Kopete::Account> it(Accounts); it.current(); ++it) {
		TQDict<Kopete::Contact> Contacts = it.current()->contacts();
		Kopete::Contact *theContact = Contacts[From];
		if (theContact != 0) {
			foundContact = true;
			dynamic_cast<WPAccount *>(it.current())->slotGotNewMessage(Body, Time, From);
			break;
		}

		if (accountKey.isEmpty() && it.current()->isConnected()) accountKey = it.currentKey();
	}

	// What to do with messages with no contact?
	// Maybe send them to the next online account? GF
	if (!foundContact) {
		if (!accountKey.isEmpty())
			dynamic_cast<WPAccount *>(Accounts[accountKey])->slotGotNewMessage(Body, Time, From);
		else
			kdDebug(14170) << "No contact or connected account found!" << endl;
	}
}

void WPProtocol::sendMessage(const TQString &Body, const TQString &Destination)
{
	popupClient->sendMessage(Body, Destination);
}

#include "wpprotocol.moc"

// vim: set noet ts=4 sts=4 sw=4:
// kate: tab-width 4; indent-width 4; replace-trailing-space-save on;