kpilot

idmapper.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 "idmapper.h"
00027 #include "idmapperxml.h"
00028 #include "options.h"
00029 
00030 #include <qsqldatabase.h>
00031 #include <qfile.h>
00032 
00033 #include <kglobal.h>
00034 #include <kstandarddirs.h>
00035 
00036 class IDMapperPrivate
00037 {
00038 public:
00039     IDMapperPrivate()
00040     {
00041         fXmlSource = 0L;
00042     }
00043     
00044     ~IDMapperPrivate()
00045     {
00046         FUNCTIONSETUP;
00047         
00048         KPILOT_DELETE(fXmlSource);
00049     }
00050     
00051     IDMapperXml *fXmlSource;
00052 };
00053 
00054 IDMapper::IDMapper()
00055 {
00056     FUNCTIONSETUP;
00057     
00058     fP = new IDMapperPrivate();
00059     
00060     QString dbPath = KGlobal::dirs()->
00061         saveLocation("data", CSL1("kpilot/") );
00062     QString dbFile = dbPath + CSL1("mapping.xml");
00063     
00064     if( !openDatasource( dbFile ) )
00065     {
00066         DEBUGKPILOT << fname << "Could not open or create xml file." << endl;
00067     }
00068 }
00069 
00070 IDMapper::IDMapper( const QString &file)
00071 {
00072     FUNCTIONSETUP;
00073     
00074     fP = new IDMapperPrivate();
00075     
00076     if( !openDatasource( file ) )
00077     {
00078         DEBUGKPILOT << fname << "Could not open or create xml file." << endl;
00079     }
00080 }
00081 
00082 IDMapper::~IDMapper()
00083 {
00084     KPILOT_DELETE(fP);
00085 }
00086 
00087 bool IDMapper::openDatasource( const QString &file )
00088 {
00089     FUNCTIONSETUP;
00090     
00091     fP->fXmlSource = new IDMapperXml( file );
00092     return fP->fXmlSource->open();
00093 }
00094 
00095 void IDMapper::registerPCObjectId( const QString &conduit, const QString &uid )
00096 {
00097     FUNCTIONSETUP;
00098     
00099     IDMapping mapping = IDMapping( conduit );
00100     mapping.setUid( uid );
00101     
00102     fP->fXmlSource->addMapping( mapping );
00103     fP->fXmlSource->save();
00104 }
00105 
00106 void IDMapper::registerHHObjectId( const QString &conduit, recordid_t pid )
00107 {
00108     FUNCTIONSETUP;
00109     
00110     IDMapping mapping = IDMapping( conduit );
00111     mapping.setPid( pid );
00112     
00113     fP->fXmlSource->addMapping( mapping );
00114     fP->fXmlSource->save();
00115 }
00116 
00117 QValueList<QString> IDMapper::getPCObjectIds( const QString &conduit )
00118 {
00119     FUNCTIONSETUP;
00120     
00121     QValueList<IDMapping> &mappings = fP->fXmlSource->mappings();
00122     QValueList<IDMapping>::iterator it;
00123     QValueList<QString> uids;
00124     
00125     DEBUGKPILOT << fname << ": total " << mappings.count() << endl;
00126     
00127     for ( it = mappings.begin(); it != mappings.end(); ++it )
00128     {
00129         IDMapping &mapping = (*it);
00130         
00131         DEBUGKPILOT << fname << ": mapping.conduit() = " << mapping.conduit() << endl;
00132         DEBUGKPILOT << fname << ": conduit = " << conduit << endl;
00133         
00134         if( (mapping.conduit() == conduit) && !mapping.uid().isNull() )
00135         {
00136             DEBUGKPILOT << fname << ": mapping.conduit() == conduit" << endl;
00137             uids.append( mapping.uid() );
00138         }
00139     }
00140     
00141     return uids;
00142 }
00143 
00144 QValueList<recordid_t> IDMapper::getHHObjectIds( const QString &conduit )
00145 {
00146     FUNCTIONSETUP;
00147     
00148     QValueList<IDMapping> &mappings = fP->fXmlSource->mappings();
00149     QValueList<IDMapping>::iterator it;
00150     QValueList<recordid_t> pids;
00151     
00152     for ( it = mappings.begin(); it != mappings.end(); ++it )
00153     {
00154         IDMapping &mapping = *it;
00155         DEBUGKPILOT << fname << ": mapping.conduit() = " << mapping.conduit() << endl;
00156         DEBUGKPILOT << fname << ": " << mapping.pid() << endl;
00157         if( mapping.conduit() == conduit && mapping.pid() != 0 )
00158         {
00159             DEBUGKPILOT << fname << ": mapping.conduit() == conduit" << endl;
00160             pids.append( mapping.pid() );
00161         }
00162     }
00163     
00164     return pids;
00165 }
00166 
00167 bool IDMapper::hasPCId( const QString &conduit, recordid_t pid )
00168 {
00169     FUNCTIONSETUP;
00170     
00171     QValueList<IDMapping> &mappings = fP->fXmlSource->mappings();
00172     QValueList<IDMapping>::iterator it;
00173     
00174     for ( it = mappings.begin(); it != mappings.end(); ++it )
00175     {
00176         IDMapping &mapping = *it;
00177         if( mapping.conduit() == conduit && mapping.pid() == pid )
00178         {
00179             return !mapping.uid().isNull();
00180         }
00181     }
00182     
00183     return false;
00184 }
00185 
00186 bool IDMapper::hasHHId( const QString &conduit, const QString &uid )
00187 {
00188     FUNCTIONSETUP;
00189     
00190     QValueList<IDMapping> &mappings = fP->fXmlSource->mappings();
00191     QValueList<IDMapping>::iterator it;
00192     
00193     for ( it = mappings.begin(); it != mappings.end(); ++it )
00194     {
00195         IDMapping &mapping = *it;
00196         if( mapping.conduit() == conduit && mapping.uid() == uid )
00197         {
00198             return mapping.pid() != 0;
00199         }
00200     }
00201     
00202     return false;
00203 }
00204 
00205 void IDMapper::setHHObjectId( const QString &conduit, const QString &uid
00206         , recordid_t pid )
00207 {
00208     FUNCTIONSETUP;
00209     
00210     bool modified = false;
00211     
00212     QValueList<IDMapping> &mappings = fP->fXmlSource->mappings();
00213     QValueList<IDMapping>::iterator it;
00214     
00215     for ( it = mappings.begin(); it != mappings.end(); ++it )
00216     {
00217         IDMapping &mapping = *it;
00218         if( mapping.conduit() == conduit && mapping.uid() == uid )
00219         {
00220             mapping.setPid( pid );
00221             fP->fXmlSource->save();
00222             modified = true;
00223         }
00224     }
00225 }
00226 
00227 void IDMapper::setPCObjectId( const QString &conduit, recordid_t pid
00228     , const QString &uid )
00229 {
00230     FUNCTIONSETUP;
00231     
00232     bool modified = false;
00233     
00234     QValueList<IDMapping> &mappings = fP->fXmlSource->mappings();
00235     QValueList<IDMapping>::iterator it;
00236     
00237     for ( it = mappings.begin(); it != mappings.end(); ++it )
00238     {
00239         IDMapping &mapping = *it;
00240         if( mapping.conduit() == conduit && mapping.pid() == pid )
00241         {
00242             mapping.setUid( uid );
00243             fP->fXmlSource->save();
00244             modified = true;
00245         }
00246     }
00247 }