summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/oscarversionupdater.cpp
blob: 7d5806e3ac86e1544f0feccc0cac9c28463bb943 (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
/*
    oscarversionupdater.cpp  -  Version Updater

    Copyright (c) 2006 by Roman Jarosz <kedgedev@centrum.cz>
    Kopete    (c) 2006 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 "oscarversionupdater.h"

#include <tqdom.h>
#include <tqmutex.h>

#include <kdebug.h>
#include <tdeio/job.h>
#include <tdeconfig.h>
#include <tdeglobal.h>


TQMutex updateMutex;
OscarVersionUpdater *OscarVersionUpdater::versionUpdaterStatic = 0L;

OscarVersionUpdater::OscarVersionUpdater()
: mStamp( 1 ), mUpdating( false )
{
	initICQVersionInfo();
}

OscarVersionUpdater::~OscarVersionUpdater()
{
}

OscarVersionUpdater *OscarVersionUpdater::self()
{
	if ( !versionUpdaterStatic )
		versionUpdaterStatic = new OscarVersionUpdater();
	
	return versionUpdaterStatic;
}

bool OscarVersionUpdater::update( unsigned int stamp )
{
	kdDebug(OSCAR_GEN_DEBUG) << k_funcinfo << endl;
	bool doUpdate = false;
	bool isUpdating = false;
	
	updateMutex.lock();
	if ( !mUpdating && stamp == mStamp )
	{
		doUpdate = true;
		mUpdating = true;
	}
	isUpdating = mUpdating;
	updateMutex.unlock();
	
	if ( doUpdate )
	{
		mVersionData.resize( 0 );
		
		TDEConfigGroup config( TDEGlobal::config(), "Oscar" );
		TQString url = config.readEntry( "NewVersionURL", "http://kopete.kde.org/oscarversions.xml" );
		mTransferJob = TDEIO::get ( url );
		kdDebug(OSCAR_GEN_DEBUG) << k_funcinfo << "Download version info from server."<< endl;
		
		connect ( mTransferJob, TQT_SIGNAL ( result ( TDEIO::Job* ) ),
		          this, TQT_SLOT ( slotTransferResult ( TDEIO::Job* ) ) );
		connect ( mTransferJob, TQT_SIGNAL ( data ( TDEIO::Job*, const TQByteArray& ) ),
		          this, TQT_SLOT ( slotTransferData ( TDEIO::Job*, const TQByteArray& ) ) );
	}
	return isUpdating;
}

unsigned int OscarVersionUpdater::stamp() const
{
	return mStamp;
}

void OscarVersionUpdater::initICQVersionInfo()
{
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << endl;
	TDEConfigGroup config( TDEGlobal::config(), "ICQVersion" );

	mICQVersion.clientString = config.readEntry( "ClientString", "ICQ Client" );
	mICQVersion.clientId = config.readEntry( "ClientId", "0x010A" ).toUShort( 0, 0 );
	mICQVersion.major = config.readEntry( "Major", "0x0006" ).toUShort( 0, 0 );
	mICQVersion.minor = config.readEntry( "Minor", "0x0000" ).toUShort( 0, 0 );
	mICQVersion.point = config.readEntry( "Point", "0x0000" ).toUShort( 0, 0 );
	mICQVersion.build = config.readEntry( "Build", "0x17AB" ).toUShort( 0, 0 );
	mICQVersion.other = config.readEntry( "Other", "0x00007535" ).toUInt( 0, 0 );
	mICQVersion.country = config.readEntry( "Country", "us" );
	mICQVersion.lang = config.readEntry( "Lang", "en" );
}

void OscarVersionUpdater::printDebug()
{
	kdDebug(OSCAR_RAW_DEBUG) << "*************** ICQ VERSION INFO ***************" << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "client string: " << mICQVersion.clientString << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "client id: "  << TQString::number( mICQVersion.clientId, 16 ) << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "major: "  << TQString::number( mICQVersion.major, 16 ) << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "minor: "  << TQString::number( mICQVersion.minor, 16 ) << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "point: "  << TQString::number( mICQVersion.point, 16 ) << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "build: "  << TQString::number( mICQVersion.build, 16 ) << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "other: "  << TQString::number( mICQVersion.other, 16 ) << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "country: "  << mICQVersion.country << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "lang: "  << mICQVersion.lang << endl;
	kdDebug(OSCAR_RAW_DEBUG) << "************************************************" << endl;
}

void OscarVersionUpdater::slotTransferData ( TDEIO::Job */*job*/, const TQByteArray &data )
{
	unsigned oldSize = mVersionData.size();
	mVersionData.resize ( oldSize + data.size() );
	memcpy ( &mVersionData.data()[oldSize], data.data(), data.size() );
	
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Data size " << mVersionData.size() << endl;
}

void OscarVersionUpdater::slotTransferResult ( TDEIO::Job *job )
{
	bool bUpdate = false;
	if ( job->error() || mTransferJob->isErrorPage() )
	{
		//TODO show error
		kdDebug(OSCAR_GEN_DEBUG) << k_funcinfo << "Download of version info has faild!" << endl;
	}
	else
	{
		kdDebug(OSCAR_GEN_DEBUG) << k_funcinfo << "Updating version info" << endl;
		
		TQDomDocument doc;
		if ( doc.setContent ( mVersionData ) )
		{
			Oscar::ClientVersion tmpICQ = mICQVersion;
			
			parseDocument( doc );
			
			if ( !equal( tmpICQ, mICQVersion ) )
			{
				storeVersionInfo( "ICQVersion", mICQVersion );
				bUpdate = true;
			}
		}
	}
	
	// clear
	mVersionData.resize( 0 );
	mTransferJob = 0;
	
	updateMutex.lock();
	if ( bUpdate )
		mStamp++;
	mUpdating = false;
	updateMutex.unlock();
}

void OscarVersionUpdater::parseDocument( TQDomDocument& doc )
{
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << endl;
	
	TQDomElement root = doc.documentElement();
	if ( root.tagName() != "oscar" )
		return;
	
	TQDomElement versionElement = root.firstChild().toElement();
	while( !versionElement.isNull() )
	{
		if ( versionElement.tagName() == "icq" )
			parseVersion( mICQVersion, versionElement );
		versionElement = versionElement.nextSibling().toElement();
	}
}

bool OscarVersionUpdater::parseVersion( Oscar::ClientVersion& version, TQDomElement& element )
{
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << endl;
	
	// clear structure
	version.clientString = TQString();
	version.clientId = 0x0000;
	version.major = 0x0000;
	version.minor = 0x0000;
	version.point = 0x0000;
	version.build = 0x0000;
	version.other = 0x00000000;
	version.country = TQString();
	version.lang = TQString();
	
	TQDomElement versionChild = element.firstChild().toElement();
	while ( !versionChild.isNull() )
	{
		if ( versionChild.tagName() == "client" )
			version.clientString = versionChild.text();
		else if ( versionChild.tagName() == "clientId" )
			version.clientId = versionChild.text().toUShort( 0, 0);
		else if ( versionChild.tagName() == "major" )
			version.major = versionChild.text().toUShort( 0, 0 );
		else if ( versionChild.tagName() == "minor" )
			version.minor = versionChild.text().toUShort( 0, 0 );
		else if ( versionChild.tagName() == "point" )
			version.point = versionChild.text().toUShort( 0, 0 );
		else if ( versionChild.tagName() == "build" )
			version.build = versionChild.text().toUShort( 0, 0 );
		else if ( versionChild.tagName() == "other" )
			version.other = versionChild.text().toUInt( 0, 0 );
		else if ( versionChild.tagName() == "country" )
			version.country = versionChild.text();
		else if ( versionChild.tagName() == "lang" )
			version.lang = versionChild.text();
		
		versionChild = versionChild.nextSibling().toElement();
	}
	
	return true;
}

void OscarVersionUpdater::storeVersionInfo( const TQString& group, const Oscar::ClientVersion& version ) const
{
	kdDebug(OSCAR_GEN_DEBUG) << k_funcinfo << "Storing version info to group: " << group << endl;
	TDEConfigGroup config( TDEGlobal::config(), group );
	
	config.writeEntry( "ClientString", version.clientString );
	config.writeEntry( "ClientId", version.clientId );
	config.writeEntry( "Major", version.major );
	config.writeEntry( "Minor", version.minor );
	config.writeEntry( "Point", version.point );
	config.writeEntry( "Build", version.build );
	config.writeEntry( "Other", version.other );
	config.writeEntry( "Country", version.country );
	config.writeEntry( "Lang", version.lang );
	config.sync();
}

bool OscarVersionUpdater::equal( const Oscar::ClientVersion& a, const Oscar::ClientVersion& b ) const
{
	if ( a.clientString != b.clientString || a.clientId != b.clientId ||
	     a.major != b.major|| a.minor != b.minor ||
	     a.point != b.point || a.build != b.build ||
	     a.other != b.other || a.country != b.country ||
	     a.lang != b.lang )
	{
		return false;
	}
	
	return true;
}

#include "oscarversionupdater.moc"