summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/history/converter.cpp
blob: 2bf748ce8e4157e05c7e5f7b77b0e94a12456ed5 (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
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
//Olivier Goffart <ogoffart @ kde.org>
// 2003 06 26

#include "historyplugin.h" //just needed because we are a member of this class
                           // we don't use any history function here

/**-----------------------------------------------------------
 * CONVERTER from the old kopete history.
 * it port history from kopete 0.6, 0.5 and above the actual
 * this should be placed in a perl script handled by KConf_update
 * but i need to acess to some info i don't have with perl, like
 * the accountId, to know each protocol id, and more
 *-----------------------------------------------------------*/

#include "kopetepluginmanager.h"
#include "kopeteaccount.h"
#include "kopeteaccountmanager.h"
#include "kopetecontact.h"
#include "kopetemessage.h"
#include "kopeteprotocol.h"
#include "kopeteuiglobal.h"

#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kmessagebox.h>
#include <kprogress.h>
#include <kapplication.h>
#include <ksavefile.h>
#include <tqdir.h>
#include <tqdom.h>
#include <tqregexp.h>

#define CBUFLENGTH 512 // buffer length for fgets()

void HistoryPlugin::convertOldHistory()
{
	bool deleteFiles=  KMessageBox::questionYesNo( Kopete::UI::Global::mainWidget(),
		i18n( "Would you like to remove old history files?" ) , i18n( "History Converter" ), KStdGuiItem::del(), i18n("Keep") ) == KMessageBox::Yes;

	KProgressDialog *progressDlg=new KProgressDialog(Kopete::UI::Global::mainWidget() , "history_progress_dlg" , i18n( "History converter" ) ,
		 TQString() , true); //modal  to  make sure the user will not doing stupid things (we have a kapp->processEvents())
	progressDlg->setAllowCancel(false); //because i am too lazy to allow to cancel


	TQString kopetedir=locateLocal( "data", TQString::fromLatin1( "kopete"));
	TQDir d( kopetedir ); //d should point to ~/.trinity/share/apps/kopete/

	d.setFilter( TQDir::Dirs  );

	const TQFileInfoList *list = d.entryInfoList();
	TQFileInfoListIterator it( *list );
	TQFileInfo *fi;
	while ( (fi = it.current()) != 0 )
	{
		TQString protocolId;
		TQString accountId;

		if( Kopete::Protocol *p = dynamic_cast<Kopete::Protocol *>( Kopete::PluginManager::self()->plugin( fi->fileName() ) ) )
		{
			protocolId=p->pluginId();
			TQDictIterator<Kopete::Account> it(Kopete::AccountManager::self()->accounts(p));
			Kopete::Account *a = it.current();
			if(a)
				accountId=a->accountId();
		}

		if(accountId.isNull() || protocolId.isNull())
		{
			if(fi->fileName() == "MSNProtocol" || fi->fileName() == "msn_logs" )
			{
				protocolId="MSNProtocol";
				KGlobal::config()->setGroup("MSN");
				accountId=KGlobal::config()->readEntry( "UserID" );
			}
			else if(fi->fileName() == "ICTQProtocol" || fi->fileName() == "icq_logs" )
			{
				protocolId="ICTQProtocol";
				KGlobal::config()->setGroup("ICQ");
				accountId=KGlobal::config()->readEntry( "UIN" );
			}
			else if(fi->fileName() == "AIMProtocol" || fi->fileName() == "aim_logs" )
			{
				protocolId="AIMProtocol";
				KGlobal::config()->setGroup("AIM");
				accountId=KGlobal::config()->readEntry( "UserID" );
			}
			else if(fi->fileName() == "OscarProtocol" )
			{
				protocolId="AIMProtocol";
				KGlobal::config()->setGroup("OSCAR");
				accountId=KGlobal::config()->readEntry( "UserID" );
			}
			else if(fi->fileName() == "JabberProtocol" || fi->fileName() == "jabber_logs")
			{
				protocolId="JabberProtocol";
				KGlobal::config()->setGroup("Jabber");
				accountId=KGlobal::config()->readEntry( "UserID" );
			}
			//TODO: gadu, wp
		}

		if(!protocolId.isEmpty() || !accountId.isEmpty())
		{
			TQDir d2( fi->absFilePath() );
			d2.setFilter( TQDir::Files  );
			d2.setNameFilter("*.log");
			const TQFileInfoList *list = d2.entryInfoList();
			TQFileInfoListIterator it2( *list );
			TQFileInfo *fi2;

			progressDlg->progressBar()->reset();
			progressDlg->progressBar()->setTotalSteps(d2.count());
			progressDlg->setLabel(i18n("Parsing old history in %1").arg(fi->fileName()));
			progressDlg->show(); //if it was not already showed...

			while ( (fi2 = it2.current()) != 0 )
			{
				//we assume that all "-" are dots.  (like in hotmail.com)
				TQString contactId=fi2->fileName().replace(".log" , TQString()).replace("-" , ".");

				if(!contactId.isEmpty() )
				{
					progressDlg->setLabel(i18n("Parsing old history in %1:\n%2").arg(fi->fileName()).arg(contactId));
					kapp->processEvents(0); //make sure the text is updated in the progressDlg

					int month=0;
					int year=0;
					TQDomDocument doc;
					TQDomElement docElem;

					TQDomElement msgelement;
					TQDomNode node;
					TQDomDocument xmllist;
					Kopete::Message::MessageDirection dir;
					TQString body, date, nick;
					TQString buffer, msgBlock;
					char cbuf[CBUFLENGTH]; // buffer for the log file

					TQString logFileName = fi2->absFilePath();

					// open the file
					FILE *f = fopen(TQFile::encodeName(logFileName), "r");

					// create a new <message> block
					while ( ! feof( f ) )
					{
						fgets(cbuf, CBUFLENGTH, f);
						buffer = TQString::fromUtf8(cbuf);

						while ( strchr(cbuf, '\n') == NULL && !feof(f) )
						{
							fgets( cbuf, CBUFLENGTH, f );
							buffer += TQString::fromUtf8(cbuf);
						}

						if( buffer.startsWith( TQString::fromLatin1( "<message " ) ) )
						{
							msgBlock = buffer;

							// find the end of the message block
							while( !feof( f ) && buffer != TQString::fromLatin1( "</message>\n" ) /*strcmp("</message>\n", cbuf )*/ )
							{
								fgets(cbuf, CBUFLENGTH, f);
								buffer = TQString::fromUtf8(cbuf);

								while ( strchr(cbuf, '\n') == NULL && !feof(f) )
								{
									fgets( cbuf, CBUFLENGTH, f );
									buffer += TQString::fromUtf8(cbuf);
								}
								msgBlock.append(buffer);
							}

							// now let's work on this new block
							xmllist.setContent(msgBlock, false);
							msgelement = xmllist.documentElement();
							node = msgelement.firstChild();

							if( msgelement.attribute( TQString::fromLatin1( "direction" ) ) == TQString::fromLatin1( "inbound" ) )
								dir = Kopete::Message::Inbound;
							else
								dir = Kopete::Message::Outbound;

							// Read all the elements.
							TQString tagname;
							TQDomElement element;

							while ( ! node.isNull() )
							{
								if ( node.isElement() )
								{
									element = node.toElement();
									tagname = element.tagName();

									if( tagname == TQString::fromLatin1( "srcnick" ) )
										nick = element.text();

									else if( tagname == TQString::fromLatin1( "date" ) )
										date = element.text();
									else if( tagname == TQString::fromLatin1( "body" ) )
										body = element.text().stripWhiteSpace();
								}

								node = node.nextSibling();
							}
							//FIXME!! The date in logs writed with kopete running with QT 3.0 is Localised.
							// so QT can't parse it correctly.
							TQDateTime dt=TQDateTime::fromString(date);
							if(dt.date().month() != month || dt.date().year() != year)
							{
								if(!docElem.isNull())
								{
									TQDate date(year,month,1);
									TQString name = protocolId.replace( TQRegExp( TQString::fromLatin1( "[./~?*]" ) ), TQString::fromLatin1( "-" ) ) +
											TQString::fromLatin1( "/" ) +
											contactId.replace( TQRegExp( TQString::fromLatin1( "[./~?*]" ) ), TQString::fromLatin1( "-" ) ) +
											date.toString(".yyyyMM");
									KSaveFile file(  locateLocal( "data", TQString::fromLatin1( "kopete/logs/" ) + name+ TQString::fromLatin1( ".xml" ) )  );
									if( file.status() == 0 )
									{
										TQTextStream *stream = file.textStream();
										//stream->setEncoding( TQTextStream::UnicodeUTF8 ); //???? oui ou non?
										doc.save( *stream , 1 );
										file.close();
									}
								}


								month=dt.date().month();
								year=dt.date().year();
								docElem=TQDomElement();
							}

							if(docElem.isNull())
							{
								doc=TQDomDocument("Kopete-History");
								docElem= doc.createElement( "kopete-history" );
								docElem.setAttribute ( "version" , "0.7" );
								doc.appendChild( docElem );
								TQDomElement headElem = doc.createElement( "head" );
								docElem.appendChild( headElem );
								TQDomElement dateElem = doc.createElement( "date" );
								dateElem.setAttribute( "year",  TQString::number(year) );
								dateElem.setAttribute( "month", TQString::number(month) );
								headElem.appendChild(dateElem);
								TQDomElement myselfElem = doc.createElement( "contact" );
								myselfElem.setAttribute( "type",  "myself" );
								myselfElem.setAttribute( "contactId", accountId  );
								headElem.appendChild(myselfElem);
								TQDomElement contactElem = doc.createElement( "contact" );
								contactElem.setAttribute( "contactId", contactId );
								headElem.appendChild(contactElem);
								TQDomElement importElem = doc.createElement( "imported" );
								importElem.setAttribute( "from",  fi->fileName() );
								importElem.setAttribute( "date", TQDateTime::currentDateTime().toString()  );
								headElem.appendChild(importElem);
							}
							TQDomElement msgElem = doc.createElement( "msg" );
							msgElem.setAttribute( "in",  dir==Kopete::Message::Outbound ? "0" : "1" );
							msgElem.setAttribute( "from", dir==Kopete::Message::Outbound ? accountId : contactId  );
							msgElem.setAttribute( "nick",  nick ); //do we have to set this?
							msgElem.setAttribute( "time",  TQString::number(dt.date().day()) + " " +  TQString::number(dt.time().hour()) + ":" + TQString::number(dt.time().minute())  );
							TQDomText msgNode = doc.createTextNode( body.stripWhiteSpace() );
							docElem.appendChild( msgElem );
							msgElem.appendChild( msgNode );
						}
					}

					fclose( f );
					if(deleteFiles)
						d2.remove(fi2->fileName() , false);

					if(!docElem.isNull())
					{
						TQDate date(year,month,1);
						TQString name = protocolId.replace( TQRegExp( TQString::fromLatin1( "[./~?*]" ) ), TQString::fromLatin1( "-" ) ) +
								TQString::fromLatin1( "/" ) +
								contactId.replace( TQRegExp( TQString::fromLatin1( "[./~?*]" ) ), TQString::fromLatin1( "-" ) ) +
								date.toString(".yyyyMM");
						KSaveFile file(  locateLocal( "data", TQString::fromLatin1( "kopete/logs/" ) + name+ TQString::fromLatin1( ".xml" ) )  );
						if( file.status() == 0 )
						{
							TQTextStream *stream = file.textStream();
							//stream->setEncoding( TQTextStream::UnicodeUTF8 ); //???? oui ou non?
							doc.save( *stream ,1 );
							file.close();
						}
					}

				}
				progressDlg->progressBar()->setProgress(progressDlg->progressBar()->progress()+1);
				++it2;
			}
		}
		++it;
	}
	delete progressDlg;

}


bool HistoryPlugin::detectOldHistory()
{
	KGlobal::config()->setGroup("History Plugin");
	TQString version=KGlobal::config()->readEntry( "Version" ,"0.6" );

	if(version != "0.6")
		return false;


	TQDir d( locateLocal( "data", TQString::fromLatin1( "kopete/logs")) );
	d.setFilter( TQDir::Dirs  );
	if(d.count() >= 3)  // '.' and '..' are included
		return false;  //the new history already exists

	TQDir d2( locateLocal( "data", TQString::fromLatin1( "kopete")) );
	d2.setFilter( TQDir::Dirs  );
	const TQFileInfoList *list = d2.entryInfoList();
	TQFileInfoListIterator it( *list );
	TQFileInfo *fi;
	while ( (fi = it.current()) != 0 )
	{
		if( dynamic_cast<Kopete::Protocol *>( Kopete::PluginManager::self()->plugin( fi->fileName() ) ) )
			return true;

		if(fi->fileName() == "MSNProtocol" || fi->fileName() == "msn_logs" )
			return true;
		else if(fi->fileName() == "ICTQProtocol" || fi->fileName() == "icq_logs" )
			return true;
		else if(fi->fileName() == "AIMProtocol" || fi->fileName() == "aim_logs" )
			return true;
		else if(fi->fileName() == "OscarProtocol" )
			return true;
		else if(fi->fileName() == "JabberProtocol" || fi->fileName() == "jabber_logs")
			return true;
		++it;
	}
	return false;
}