summaryrefslogtreecommitdiffstats
path: root/kpilot/lib/idmapperxml.cc
blob: a2a78a0c964276499fd056b4865925f85744a2ae (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
/*
** Copyright (C) 2006 Bertjan Broeksema <bbroeksema@bluebottle.com>
*/

/*
** This program 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.1 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to kde-pim@kde.org
*/

#include "idmapperxml.h"

#include "options.h"

IDMapperXml::IDMapperXml( const QString &file ) : fFile(file)
	, fCurrentMapping( 0l )
{
}

IDMapperXml::~IDMapperXml()
{
	FUNCTIONSETUP;
}

bool IDMapperXml::open()
{
	FUNCTIONSETUP;
	
	root = doc.createElement( CSL1("mappings") );
	QDomNode node = doc.createProcessingInstruction(CSL1("xml")
		,CSL1("version=\"1.0\" encoding=\"UTF-8\""));
	
	doc.appendChild( node );
	doc.appendChild( root );
	
	if( !fFile.exists() )
	{
		DEBUGKPILOT << fname << ": Creating " << fFile.name() << endl;
		
		if( fFile.open( IO_ReadWrite ) )
		{
			QTextStream out( &fFile );
			doc.save( out, 4 );
			fFile.close();
			return true;
		}
		else
		{
			DEBUGKPILOT << fname << ": Could not create " << fFile.name() << endl;
			return false;
		}
	}
	else
	{
		DEBUGKPILOT << fname << ": Parsing file " << fFile.name() << endl;
		QXmlSimpleReader reader;
		reader.setContentHandler( this );
		
		// Make sure that the file is closed after parsing.
		bool result = reader.parse( fFile );
		fFile.close();
		
		return result;
	}
}

void IDMapperXml::save()
{
	FUNCTIONSETUP;
	
	DEBUGKPILOT << fname << ": Saving " << fMappings.count()
		<< " mappings..." << endl;
	DEBUGKPILOT << fname << ": ";
	
	QValueList<IDMapping>::const_iterator it;
	for ( it = fMappings.begin(); it != fMappings.end(); ++it )
	{
		DEBUGKPILOT << ".";
		
		IDMapping mapping = (*it);
		
		DEBUGKPILOT << fname << ": " << mapping.conduit();
		
		QDomElement mappingElement = doc.createElement( CSL1("mapping") );
		mappingElement.setAttribute( CSL1("conduit"), mapping.conduit() );
		
		if( !mapping.uid().isNull() )
		{
			QDomElement uidElement = doc.createElement( CSL1("uid") );
			uidElement.setAttribute( CSL1("value"), mapping.uid() );
			mappingElement.appendChild( uidElement );
		}
		
		if( mapping.pid() != 0 )
		{
			QDomElement uidElement = doc.createElement( CSL1("pid") );
			uidElement.setAttribute( CSL1("value"), mapping.pid() );
			mappingElement.appendChild( uidElement );
		}
		
		if( !mapping.lastSyncTime().isNull() )
		{
			QDomElement uidElement = doc.createElement( CSL1("pid") );
			uidElement.setAttribute( CSL1("value"), QString::number( mapping.pid() ) );
			mappingElement.appendChild( uidElement );
		}
		
		root.appendChild( mappingElement );
	}
	
	if( fFile.open( IO_ReadWrite ) )
	{
		QTextStream out( &fFile );
		doc.save( out, 4 );
		fFile.close();
		
		DEBUGKPILOT << endl << fname << ": finished saving." << endl;
	}
}

void IDMapperXml::addMapping( const IDMapping &mapping )
{
	FUNCTIONSETUP;
	
	DEBUGKPILOT << fname << ": " << mapping.conduit() << endl;
	
	fMappings.append( mapping );
	
	DEBUGKPILOT << fname << ": " << fMappings.first().conduit() << endl;
}

QValueList<IDMapping>& IDMapperXml::mappings()
{
	return fMappings;
}

bool IDMapperXml::startElement( const QString &namespaceURI
	, const QString &localName, const QString &qName
	, const QXmlAttributes &attribs )
{
	FUNCTIONSETUP;
	Q_UNUSED(namespaceURI);
	Q_UNUSED(localName);
	
	if( qName == CSL1("mapping") )
	{
		QString conduit( attribs.value( CSL1("conduit") ) );
		
		fCurrentMapping = new IDMapping( conduit );
	}
	else if( qName == CSL1("uid") )
	{
		fCurrentMapping->setUid( attribs.value( CSL1("value") ) );
	}
	else if( qName == CSL1("pid") )
	{
		fCurrentMapping->setPid( attribs.value( CSL1("value") ).toULong() );
	}
	else if( qName == CSL1("lastsync") )
	{
		// NOTE: this isn't very robuust!
		// Parses only dates in the form: dd-mm-yyyy hh:mm:ss
		QString date = attribs.value( CSL1("value") );
		int day = date.left(2).toInt();
		int month = date.mid(3,2).toInt();
		int year = date.mid(6, 4).toInt();
		
		int hour = date.mid(11,2).toInt();
		int minute = date.mid(14,2).toInt();
		int second = date.mid(17,2).toInt();
		
		QDate tmpDate = QDate( year, month, day );
		QTime tmpTime = QTime( hour, minute, second );
		
		fCurrentMapping->setLastSyncTime( QDateTime( tmpDate, tmpTime ) );
	}

	return true;
}

bool IDMapperXml::endElement( const QString &namespaceURI
	, const QString &localName, const QString &qName )
{
	FUNCTIONSETUP;

	Q_UNUSED(namespaceURI);
	Q_UNUSED(localName);
	Q_UNUSED(qName);
	
	if( qName == CSL1("mapping") )
	{
		addMapping( *fCurrentMapping );
		delete fCurrentMapping;
		fCurrentMapping = 0l;
	}
	
	return true;
}