summaryrefslogtreecommitdiffstats
path: root/kopete/kopete/kconf_update/kopete-nameTracking.cpp
blob: 391e5132340418945e4a84ba47aa593f9a34c15f (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
/*
    kconf_update app for updating the contactlist format ( <= 0.9.0) for MetaContacts to
    track the name of a subcontact.

    Kopete    (c) 2002-2004 by the Kopete developers  <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include <qdir.h>
#include <qtextstream.h>
#include <qdom.h>

#include <kstandarddirs.h>

static QTextStream qcerr( stderr, IO_WriteOnly );

int main()
{
	KInstance* inst = new KInstance( "Update script" );
	QString filename = locateLocal( "data", QString::fromLatin1( "kopete/contactlist.xml" ) );
	
	// Load contact list & save backup.
	QFile contactListFile( filename );
	contactListFile.open( IO_ReadOnly );
	QDomDocument contactList;
	contactList.setContent( &contactListFile );
	contactListFile.close();
	QDir().rename( filename, filename + QString::fromLatin1( ".bak" ) );
	
	// parse the XML file
	QDomElement list = contactList.documentElement();
	QDomElement mcElement = list.firstChild().toElement();
	
	while( !mcElement.isNull() )
	{
		
		// update all the MetaContacts
		if( mcElement.tagName() == QString::fromLatin1("meta-contact") )
		{
			QDomElement displayName;
			QDomElement subcontact;
			
			QDomElement elem = mcElement.firstChild().toElement();
			while( !elem.isNull() )
			{
				if( elem.tagName() == QString::fromLatin1( "display-name" ) )
					displayName = elem;
				if( elem.tagName() == QString::fromLatin1( "plugin-data" ) )
				{
					// check if it's a contact by checking for "protocol" substring in the tag,
					// and the presence of a contactId child element.
					QString pluginId = elem.attribute( QString::fromLatin1( "plugin-id" ) );
					bool isProtocol = ( pluginId.contains( "protocol", false ) > 0 ); // case-insensitive search
					bool hasContactId = false;
					QDomNode field = elem.firstChild();
					while( !field.isNull() )
					{
						QDomElement fieldElem = field.toElement();
						
						if( !fieldElem.isNull() &&
							fieldElem.tagName() == QString::fromLatin1( "plugin-data-field" ) && 
							fieldElem.attribute( QString::fromLatin1( "key" ) ) == QString::fromLatin1( "contactId" ) )
						{
							hasContactId = true;
							break;
						}
						field = field.nextSibling();
					}
					
					if( isProtocol && hasContactId )
						subcontact = elem;
				}
				
				elem = elem.nextSibling().toElement();
			} // end while
			
			// check if we're even tracking the subcontact's name
			// if displayName.isNull(), it simply won't find the attribute; no harm done
			bool tracking = 
				( displayName.attribute( QString::fromLatin1( "trackChildNameChanges" ),
				QString::fromLatin1( "0" ) ) == QString::fromLatin1( "1" ) );
			if( !displayName.isNull() && !subcontact.isNull() && tracking )
			{
				// collect info
				QString nsCID;
				QString nsPID;
				QString nsAID;

				nsPID = subcontact.attribute( QString::fromLatin1( "plugin-id" ) );
				QDomNode field = subcontact.firstChild();
				while( !field.isNull() )
				{
					QDomElement fieldElem = field.toElement();
						
					if( !fieldElem.isNull() && fieldElem.tagName() == QString::fromLatin1( "plugin-data-field" ) )
					{
						 if( fieldElem.attribute( QString::fromLatin1( "key" ) ) == QString::fromLatin1( "contactId" ) )
							 nsCID = fieldElem.text();
						 if( fieldElem.attribute( QString::fromLatin1( "key" ) ) == QString::fromLatin1( "accountId" ) )
							 nsAID = fieldElem.text();
					}
					field = field.nextSibling();
				}
				
				// create the tracking info
				displayName.setAttribute( QString::fromLatin1( "nameSourceContactId" ), nsCID );
				displayName.setAttribute( QString::fromLatin1( "nameSourcePluginId" ), nsPID );
				displayName.setAttribute( QString::fromLatin1( "nameSourceAccountId" ), nsAID );
			}
		}	
		
		mcElement = mcElement.nextSibling().toElement();
	}

	// Save converted contactlist
	contactListFile.open( IO_WriteOnly );
	QTextStream stream( &contactListFile );
	stream.setEncoding( QTextStream::UnicodeUTF8 );
	stream << contactList.toString( 4 );
	contactListFile.flush();
	contactListFile.close();

	return 0;
}

// vim: set noet ts=4 sts=4 sw=4: