summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/statistics/statisticsplugin.cpp
blob: 49ddfb1886e1e88004c2d386b664e69d84092c8f (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
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
/*
    statisticsplugin.cpp

    Copyright (c) 2003-2004 by Marc Cramdal        <marc.cramdal@gmail.com>


    *************************************************************************
    *                                                                       *
    * 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 <tqfile.h>
#include <tqdict.h>
#include <tqtimer.h>

#include <kgenericfactory.h>
#include <tdeaboutdata.h>
#include <tdeaction.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <tdeversion.h>

#include "kopetechatsessionmanager.h"
#include "kopetemetacontact.h"
#include "kopeteview.h"
#include "kopetecontactlist.h"
#include "kopeteuiglobal.h"
#include "kopetemessageevent.h"
#include "kopeteonlinestatus.h"
#include "kopeteaccountmanager.h"
#include "kopeteaccount.h"

#include "statisticscontact.h"
#include "statisticsdialog.h"
#include "statisticsplugin.h"
#include "statisticsdb.h"

typedef KGenericFactory<StatisticsPlugin> StatisticsPluginFactory;


static const TDEAboutData aboutdata("kopete_statistics", I18N_NOOP("Statistics") , "0.1" );
K_EXPORT_COMPONENT_FACTORY( kopete_statistics, StatisticsPluginFactory( &aboutdata )  )

StatisticsPlugin::StatisticsPlugin( TQObject *parent, const char *name, const TQStringList &)
      : DCOPObject("StatisticsDCOPIface"), 
        Kopete::Plugin( StatisticsPluginFactory::instance(), parent, name )
      

{
	TDEAction *viewMetaContactStatistics = new TDEAction( i18n("View &Statistics" ),
		TQString::fromLatin1( "log" ), 0, this, TQT_SLOT(slotViewStatistics()),
		actionCollection(), "viewMetaContactStatistics" );
	viewMetaContactStatistics->setEnabled(Kopete::ContactList::self()->selectedMetaContacts().count() == 1);

	connect(Kopete::ChatSessionManager::self(),TQT_SIGNAL(chatSessionCreated(Kopete::ChatSession*)),
				this, TQT_SLOT(slotViewCreated(Kopete::ChatSession*)));
	connect(Kopete::ChatSessionManager::self(),TQT_SIGNAL(aboutToReceive(Kopete::Message&)),
				this, TQT_SLOT(slotAboutToReceive(Kopete::Message&)));
		
	connect(Kopete::ContactList::self(), TQT_SIGNAL(metaContactSelected(bool)),
		viewMetaContactStatistics, TQT_SLOT(setEnabled(bool)));
	connect(Kopete::ContactList::self(), TQT_SIGNAL(metaContactAdded(Kopete::MetaContact*)),
			this, TQT_SLOT(slotMetaContactAdded(Kopete::MetaContact*)));	
	connect(Kopete::ContactList::self(), TQT_SIGNAL(metaContactRemoved(Kopete::MetaContact*)),
			this, TQT_SLOT(slotMetaContactRemoved(Kopete::MetaContact*)));	

	setXMLFile("statisticsui.rc");

	/* Initialization reads the database, so it could be a bit time-consuming
	due to disk access. This should overcome the problem and makes it non-blocking. */
	TQTimer::singleShot(0, this, TQT_SLOT(slotInitialize()));
}	

void StatisticsPlugin::slotInitialize()
{
	// Initializes the database
	m_db = new StatisticsDB();
	
	TQPtrList<Kopete::MetaContact> list = Kopete::ContactList::self()->metaContacts();
	TQPtrListIterator<Kopete::MetaContact> it( list );
	for (; it.current(); ++it)
	{
		slotMetaContactAdded(it.current());
	}
}

StatisticsPlugin::~StatisticsPlugin()
{
	TQMap<Kopete::MetaContact*, StatisticsContact*>::Iterator it;
        for ( it = statisticsMetaContactMap.begin(); it != statisticsMetaContactMap.end(); ++it )
	{
		delete it.data();
	}
	delete m_db;
}

void StatisticsPlugin::slotAboutToReceive(Kopete::Message& m)
{
	if ( statisticsMetaContactMap.contains(m.from()->metaContact()) )
		statisticsMetaContactMap[m.from()->metaContact()]->newMessageReceived(m);
}

void StatisticsPlugin::slotViewCreated(Kopete::ChatSession* session)
{
	connect(session, TQT_SIGNAL(closing(Kopete::ChatSession*)), this, TQT_SLOT(slotViewClosed(Kopete::ChatSession*)));
}

void StatisticsPlugin::slotViewClosed(Kopete::ChatSession* session)
{
	TQPtrList<Kopete::Contact> list = session->members();
	TQPtrListIterator<Kopete::Contact> it( list );
	
	for (; it.current(); ++it)
	{
		// If this contact is not in other chat sessions
		if (!it.current()->manager() && statisticsMetaContactMap.contains(it.current()->metaContact()))
			statisticsMetaContactMap[it.current()->metaContact()]->setIsChatWindowOpen(false);
	}
}

void StatisticsPlugin::slotViewStatistics()
{
	Kopete::MetaContact *mc=Kopete::ContactList::self()->selectedMetaContacts().first();
	
	kdDebug() << k_funcinfo << "statistics - dialog :"+ mc->displayName() << endl;
	
	if ( mc && statisticsMetaContactMap.contains(mc) )
	{
		(new StatisticsDialog(statisticsMetaContactMap[mc], db()))->show();
	}
}

void StatisticsPlugin::slotOnlineStatusChanged(Kopete::MetaContact *mc, Kopete::OnlineStatus::StatusType status)
{
	if ( statisticsMetaContactMap.contains(mc) )
		statisticsMetaContactMap[mc]->onlineStatusChanged(status);
}

void StatisticsPlugin::slotMetaContactAdded(Kopete::MetaContact *mc)
{
	statisticsMetaContactMap[mc] = new StatisticsContact(mc, db());
	
	TQPtrList<Kopete::Contact> clist = mc->contacts();
	Kopete::Contact *contact;
	
	// we need to call slotContactAdded if MetaContact allready have contacts
	for ( contact = clist.first(); contact; contact = clist.next() )
	{
		this->slotContactAdded(contact);
	}
	
	connect(mc, TQT_SIGNAL(onlineStatusChanged( Kopete::MetaContact *, Kopete::OnlineStatus::StatusType)), this,
					TQT_SLOT(slotOnlineStatusChanged(Kopete::MetaContact*, Kopete::OnlineStatus::StatusType)));
	connect(mc, TQT_SIGNAL(contactAdded( Kopete::Contact *)), this,
					TQT_SLOT(slotContactAdded( Kopete::Contact *)));
	connect(mc, TQT_SIGNAL(contactRemoved( Kopete::Contact *)), this,
					TQT_SLOT(slotContactRemoved( Kopete::Contact *)));
}

void StatisticsPlugin::slotMetaContactRemoved(Kopete::MetaContact *mc)
{
	if (statisticsMetaContactMap.contains(mc))
	{
		StatisticsContact *sc = statisticsMetaContactMap[mc];
		statisticsMetaContactMap.remove(mc);
		sc->removeFromDB();
		delete sc;
	}
}

void StatisticsPlugin::slotContactAdded( Kopete::Contact *c)
{
	if (statisticsMetaContactMap.contains(c->metaContact()))
	{
		StatisticsContact *sc = statisticsMetaContactMap[c->metaContact()];
		sc->contactAdded(c);
		statisticsContactMap[c->contactId()] = sc;
	}
}

void StatisticsPlugin::slotContactRemoved( Kopete::Contact *c)
{
	if (statisticsMetaContactMap.contains(c->metaContact()))
		statisticsMetaContactMap[c->metaContact()]->contactRemoved(c);
	
	statisticsContactMap.remove(c->contactId());
}

void StatisticsPlugin::dcopStatisticsDialog(TQString id)
{
	kdDebug() << k_funcinfo << "statistics - DCOP dialog :" << id << endl;
	
	if (statisticsContactMap.contains(id))
	{
		(new StatisticsDialog(statisticsContactMap[id], db()))->show();
	}	
}

bool StatisticsPlugin::dcopWasOnline(TQString id, int timeStamp)
{
	TQDateTime dt;
	dt.setTime_t(timeStamp);	
	return dcopWasStatus(id, dt, Kopete::OnlineStatus::Online); 
}

bool StatisticsPlugin::dcopWasOnline(TQString id, TQString dateTime)
{
	return dcopWasStatus(id, TQDateTime::fromString(dateTime), Kopete::OnlineStatus::Online);
}

bool StatisticsPlugin::dcopWasAway(TQString id, int timeStamp)
{
	TQDateTime dt;
	dt.setTime_t(timeStamp);	
	return dcopWasStatus(id, dt, Kopete::OnlineStatus::Away); 
}

bool StatisticsPlugin::dcopWasAway(TQString id, TQString dateTime)
{
	return dcopWasStatus(id, TQDateTime::fromString(dateTime), Kopete::OnlineStatus::Away);
}

bool StatisticsPlugin::dcopWasOffline(TQString id, int timeStamp)
{
	TQDateTime dt;
	dt.setTime_t(timeStamp);	
	return dcopWasStatus(id, dt, Kopete::OnlineStatus::Offline); 
}

bool StatisticsPlugin::dcopWasOffline(TQString id, TQString dateTime)
{
	return dcopWasStatus(id, TQDateTime::fromString(dateTime), Kopete::OnlineStatus::Offline);
}

bool StatisticsPlugin::dcopWasStatus(TQString id, TQDateTime dateTime, Kopete::OnlineStatus::StatusType status)
{
	kdDebug() << k_funcinfo << "statistics - DCOP wasOnline :" << id << endl;
	
	if (dateTime.isValid() && statisticsContactMap.contains(id))
	{
		return statisticsContactMap[id]->wasStatus(dateTime, status);
	}
	
	return false;	
}

TQString StatisticsPlugin::dcopStatus(TQString id, int timeStamp)
{
	TQDateTime dt;
	dt.setTime_t(timeStamp);
	return dcopStatus(id, dt.toString());

}

TQString StatisticsPlugin::dcopStatus(TQString id, TQString dateTime)
{
	TQDateTime dt = TQDateTime::fromString(dateTime);
	
	if (dt.isValid() && statisticsContactMap.contains(id))
	{
		return statisticsContactMap[id]->statusAt(dt);
	}
	
	return "";
}

TQString StatisticsPlugin::dcopMainStatus(TQString id, int timeStamp)
{
	TQDateTime dt;
	dt.setTime_t(timeStamp);
	if (dt.isValid() && statisticsContactMap.contains(id))
	{
		return statisticsContactMap[id]->mainStatusDate(dt.date());
	}
	
	return "";
}
#include "statisticsplugin.moc"