kpilot

idmapperxml.cc

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2006 Bertjan Broeksema <bbroeksema@bluebottle.com>
00003 */
00004 
00005 /*
00006 ** This program is free software; you can redistribute it and/or modify
00007 ** it under the terms of the GNU Lesser General Public License as published by
00008 ** the Free Software Foundation; either version 2.1 of the License, or
00009 ** (at your option) any later version.
00010 **
00011 ** This program is distributed in the hope that it will be useful,
00012 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014 ** GNU Lesser General Public License for more details.
00015 **
00016 ** You should have received a copy of the GNU Lesser General Public License
00017 ** along with this program in a file called COPYING; if not, write to
00018 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00019 ** MA 02110-1301, USA.
00020 */
00021 
00022 /*
00023 ** Bug reports and questions can be sent to kde-pim@kde.org
00024 */
00025 
00026 #include "idmapperxml.h"
00027 
00028 #include "options.h"
00029 
00030 IDMapperXml::IDMapperXml( const QString &file ) : fFile(file)
00031     , fCurrentMapping( 0l )
00032 {
00033 }
00034 
00035 IDMapperXml::~IDMapperXml()
00036 {
00037     FUNCTIONSETUP;
00038 }
00039 
00040 bool IDMapperXml::open()
00041 {
00042     FUNCTIONSETUP;
00043     
00044     root = doc.createElement( CSL1("mappings") );
00045     QDomNode node = doc.createProcessingInstruction(CSL1("xml")
00046         ,CSL1("version=\"1.0\" encoding=\"UTF-8\""));
00047     
00048     doc.appendChild( node );
00049     doc.appendChild( root );
00050     
00051     if( !fFile.exists() )
00052     {
00053         DEBUGKPILOT << fname << ": Creating " << fFile.name() << endl;
00054         
00055         if( fFile.open( IO_ReadWrite ) )
00056         {
00057             QTextStream out( &fFile );
00058             doc.save( out, 4 );
00059             fFile.close();
00060             return true;
00061         }
00062         else
00063         {
00064             DEBUGKPILOT << fname << ": Could not create " << fFile.name() << endl;
00065             return false;
00066         }
00067     }
00068     else
00069     {
00070         DEBUGKPILOT << fname << ": Parsing file " << fFile.name() << endl;
00071         QXmlSimpleReader reader;
00072         reader.setContentHandler( this );
00073         
00074         // Make sure that the file is closed after parsing.
00075         bool result = reader.parse( fFile );
00076         fFile.close();
00077         
00078         return result;
00079     }
00080 }
00081 
00082 void IDMapperXml::save()
00083 {
00084     FUNCTIONSETUP;
00085     
00086     DEBUGKPILOT << fname << ": Saving " << fMappings.count()
00087         << " mappings..." << endl;
00088     DEBUGKPILOT << fname << ": ";
00089     
00090     QValueList<IDMapping>::const_iterator it;
00091     for ( it = fMappings.begin(); it != fMappings.end(); ++it )
00092     {
00093         DEBUGKPILOT << ".";
00094         
00095         IDMapping mapping = (*it);
00096         
00097         DEBUGKPILOT << fname << ": " << mapping.conduit();
00098         
00099         QDomElement mappingElement = doc.createElement( CSL1("mapping") );
00100         mappingElement.setAttribute( CSL1("conduit"), mapping.conduit() );
00101         
00102         if( !mapping.uid().isNull() )
00103         {
00104             QDomElement uidElement = doc.createElement( CSL1("uid") );
00105             uidElement.setAttribute( CSL1("value"), mapping.uid() );
00106             mappingElement.appendChild( uidElement );
00107         }
00108         
00109         if( mapping.pid() != 0 )
00110         {
00111             QDomElement uidElement = doc.createElement( CSL1("pid") );
00112             uidElement.setAttribute( CSL1("value"), mapping.pid() );
00113             mappingElement.appendChild( uidElement );
00114         }
00115         
00116         if( !mapping.lastSyncTime().isNull() )
00117         {
00118             QDomElement uidElement = doc.createElement( CSL1("pid") );
00119             uidElement.setAttribute( CSL1("value"), QString::number( mapping.pid() ) );
00120             mappingElement.appendChild( uidElement );
00121         }
00122         
00123         root.appendChild( mappingElement );
00124     }
00125     
00126     if( fFile.open( IO_ReadWrite ) )
00127     {
00128         QTextStream out( &fFile );
00129         doc.save( out, 4 );
00130         fFile.close();
00131         
00132         DEBUGKPILOT << endl << fname << ": finished saving." << endl;
00133     }
00134 }
00135 
00136 void IDMapperXml::addMapping( const IDMapping &mapping )
00137 {
00138     FUNCTIONSETUP;
00139     
00140     DEBUGKPILOT << fname << ": " << mapping.conduit() << endl;
00141     
00142     fMappings.append( mapping );
00143     
00144     DEBUGKPILOT << fname << ": " << fMappings.first().conduit() << endl;
00145 }
00146 
00147 QValueList<IDMapping>& IDMapperXml::mappings()
00148 {
00149     return fMappings;
00150 }
00151 
00152 bool IDMapperXml::startElement( const QString &namespaceURI
00153     , const QString &localName, const QString &qName
00154     , const QXmlAttributes &attribs )
00155 {
00156     FUNCTIONSETUP;
00157     Q_UNUSED(namespaceURI);
00158     Q_UNUSED(localName);
00159     
00160     if( qName == CSL1("mapping") )
00161     {
00162         QString conduit( attribs.value( CSL1("conduit") ) );
00163         
00164         fCurrentMapping = new IDMapping( conduit );
00165     }
00166     else if( qName == CSL1("uid") )
00167     {
00168         fCurrentMapping->setUid( attribs.value( CSL1("value") ) );
00169     }
00170     else if( qName == CSL1("pid") )
00171     {
00172         fCurrentMapping->setPid( attribs.value( CSL1("value") ).toULong() );
00173     }
00174     else if( qName == CSL1("lastsync") )
00175     {
00176         // NOTE: this isn't very robuust!
00177         // Parses only dates in the form: dd-mm-yyyy hh:mm:ss
00178         QString date = attribs.value( CSL1("value") );
00179         int day = date.left(2).toInt();
00180         int month = date.mid(3,2).toInt();
00181         int year = date.mid(6, 4).toInt();
00182         
00183         int hour = date.mid(11,2).toInt();
00184         int minute = date.mid(14,2).toInt();
00185         int second = date.mid(17,2).toInt();
00186         
00187         QDate tmpDate = QDate( year, month, day );
00188         QTime tmpTime = QTime( hour, minute, second );
00189         
00190         fCurrentMapping->setLastSyncTime( QDateTime( tmpDate, tmpTime ) );
00191     }
00192 
00193     return true;
00194 }
00195 
00196 bool IDMapperXml::endElement( const QString &namespaceURI
00197     , const QString &localName, const QString &qName )
00198 {
00199     FUNCTIONSETUP;
00200 
00201     Q_UNUSED(namespaceURI);
00202     Q_UNUSED(localName);
00203     Q_UNUSED(qName);
00204     
00205     if( qName == CSL1("mapping") )
00206     {
00207         addMapping( *fCurrentMapping );
00208         delete fCurrentMapping;
00209         fCurrentMapping = 0l;
00210     }
00211     
00212     return true;
00213 }