• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kpilot

dataproxy.cc

Go to the documentation of this file.
00001 /* dataproxy.cc         KPilot
00002 **
00003 ** Copyright (C) 2007 by Bertjan Broeksema <b.broeksema@kdemail.net>
00004 ** Copyright (C) 2007 by Jason "vanRijn" Kasper <vr@movingparts.net>
00005 */
00006 
00007 /*
00008 ** This program is free software; you can redistribute it and/or modify
00009 ** it under the terms of the GNU Lesser General Public License as published by
00010 ** the Free Software Foundation; either version 2.1 of the License, or
00011 ** (at your option) any later version.
00012 **
00013 ** This program is distributed in the hope that it will be useful,
00014 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016 ** GNU Lesser General Public License for more details.
00017 **
00018 ** You should have received a copy of the GNU Lesser General Public License
00019 ** along with this program in a file called COPYING; if not, write to
00020 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00021 ** MA 02110-1301, USA.
00022 */
00023 
00024 /*
00025 ** Bug reports and questions can be sent to kde-pim@kde.org
00026 */
00027 
00028 #include "dataproxy.h"
00029 #include "record.h"
00030 
00031 #include "options.h"
00032 
00033 DataProxy::DataProxy() : fIterator( fRecords )
00034 {
00035     FUNCTIONSETUP;
00036 }
00037 
00038 DataProxy::~DataProxy()
00039 {
00040     FUNCTIONSETUP;
00041     
00042     qDeleteAll( fRecords );
00043 }
00044 
00045 QString DataProxy::create( Record *record )
00046 {
00047     FUNCTIONSETUP;
00048     
00049     // Temporary id.
00050     QString uniqueId = generateUniqueId();
00051     
00052     // Make sure that the new record has the right id and add the record.
00053     record->setId( uniqueId );
00054     
00055     DEBUGKPILOT << "Record created with id: [" << uniqueId << "]";
00056     
00057     fRecords.insert( uniqueId, record );
00058     
00059     // Update rollback/volatility information.
00060     fCreated.insert( uniqueId, false );
00061     fCounter.created();
00062     
00063     return uniqueId;
00064 }
00065 
00066 void DataProxy::remove( const QString &id )
00067 {
00068     FUNCTIONSETUP;
00069     
00070     Record *rec = fRecords.value( id );
00071     if( rec == 0L )
00072     {
00073         // No record
00074         return;
00075     }
00076     
00077     DEBUGKPILOT << "Removing record id: [" << id << "]";
00078     // Remove record.
00079     fRecords.remove( id );
00080     
00081     // Update rollback/volatility information.
00082     fDeletedRecords.insert( rec->id(), rec );
00083     fDeleted.insert( rec->id(), false );
00084     fCounter.deleted();
00085 }
00086 
00087 void DataProxy::update( const QString &id, Record *newRecord )
00088 {
00089     FUNCTIONSETUP;
00090     
00091     Record *oldRecord = fRecords.value( id );
00092     if( oldRecord == 0L )
00093     {
00094         // No record, should not happen.
00095         DEBUGKPILOT << "There is no record with id: [" << id
00096             << "]. Record not updated and not added.";
00097         return;
00098     }
00099     DEBUGKPILOT << "Updating record id: [" << id << "]";
00100     // Make sure that the new record has the right id and update the old record.
00101     newRecord->setId( id );
00102     fRecords.insert( id, newRecord );
00103     
00104     // Update rollback/volatility information.
00105     fOldRecords.insert( id, oldRecord );
00106     fCounter.updated();
00107 }
00108 
00109 QList<QString> DataProxy::ids() const
00110 {
00111     return fRecords.keys();
00112 }
00113 
00114 const CUDCounter* DataProxy::counter() const
00115 {
00116     FUNCTIONSETUP;
00117     
00118     return &fCounter;
00119 }
00120 
00121 void DataProxy::setEndcount()
00122 {
00123     FUNCTIONSETUP;
00124     
00125     fCounter.setEndCount( fRecords.size() );
00126 }
00127 
00128 void DataProxy::setIterateMode( const Mode m )
00129 {
00130     FUNCTIONSETUP;
00131     
00132     fMode = m;
00133 }
00134 
00135 unsigned int DataProxy::recordCount() const
00136 {
00137     return fRecords.size();
00138 }
00139 
00140 Record* DataProxy::find( const QString &id ) const
00141 {
00142     FUNCTIONSETUP;
00143     return fRecords.value( id );
00144 }
00145 
00146 void DataProxy::resetIterator()
00147 {
00148     fIterator = QMapIterator<QString, Record*>( fRecords );
00149 }
00150 
00151 bool DataProxy::hasNext() const
00152 {
00153     FUNCTIONSETUP;
00154     
00155     if( fMode == All )
00156     {
00157         return fIterator.hasNext();
00158     }
00159     else
00160     {
00161         QMapIterator<QString, Record*> tmpIt = fIterator;
00162         while( tmpIt.hasNext() )
00163         {
00164             Record *rec = tmpIt.next().value();
00165             if( rec->isModified() )
00166             {
00167                 return true;
00168             }
00169         }
00170     }
00171     
00172     return false;
00173 }
00174 
00175 Record* DataProxy::next()
00176 {
00177     FUNCTIONSETUP;
00178     
00179     if( fMode == All )
00180     {
00181             return fIterator.next().value();
00182     }
00183     else
00184     {
00185         while( fIterator.hasNext() )
00186         {
00187             Record *rec = fIterator.next().value();
00188             if( rec->isModified() )
00189             {
00190                 return rec;
00191             }
00192         }
00193     }
00194     
00195     return 0L;
00196 }
00197 
00198 bool DataProxy::commit()
00199 {
00200     FUNCTIONSETUP;
00201     
00202     // Commit created records.
00203     QStringListIterator it( fCreated.keys() );
00204     
00205     DEBUGKPILOT << "Committing: [" << fCreated.size() << "] records.";
00206     
00207     // Reset the map
00208     fCreated.clear();
00209     
00210     while( it.hasNext() )
00211     {
00212         QString id = it.next();
00213         
00214         DEBUGKPILOT << "Committing id: [" << id << "]";
00215         
00216         Record *rec = find( id );
00217         if( rec )
00218         {
00219             if( !commitCreate( rec ) )
00220             {
00221                 // Commit failed.
00222                 return false;
00223             }
00224             
00225             // Commit succeeded.
00226             
00227             // Put the record with the new id in.
00228             if( rec->id() != id )
00229             {
00230                 fCreated.remove( id );
00231                 fCreated.insert( rec->id(), true );
00232                 
00233                 fRecords.remove( id );
00234                 fRecords.insert( rec->id(), rec );
00235             
00236                 fChangedIds.insert( id, rec->id() );
00237             }
00238             else
00239             {
00240                 fCreated.insert( rec->id(), true );
00241             }
00242         }
00243         else
00244         {
00245             DEBUGKPILOT << "Record with id: [" << id << "] not found!";
00246         }
00247     }
00248     
00249     // Commit updated records.
00250     DEBUGKPILOT << "Updating: [" << fOldRecords.size() << "] records.";
00251     
00252     QListIterator<Record*> i( fOldRecords.values() );
00253     while( i.hasNext() )
00254     {
00255         // i.next() contains the old values.
00256         Record *oldRec = i.next();
00257         QString id = oldRec->id();
00258         
00259         // Look up the new values
00260         Record *rec = find( id );
00261         
00262         if( rec && !fCreated.value( id ) )
00263         {
00264             if( !commitUpdate( rec ) )
00265             {
00266                 // Commit failed.
00267                 return false;
00268             }
00269             
00270             // Commit succeeded.
00271             
00272             QString newId = rec->id();
00273             
00274             if( newId != id )
00275             {
00276                 oldRec->setId( newId );
00277                 
00278                 fChangedIds.insert( id, newId );
00279             }
00280             fUpdated.insert( rec->id(), true );
00281         }
00282     }
00283     
00284     // Commit deleted records
00285     DEBUGKPILOT << "Deleting: [" << fDeletedRecords.size() << "] records.";
00286     
00287     i = QListIterator<Record*>( fDeletedRecords.values() );
00288     while( i.hasNext() )
00289     {
00290         Record *oldRec = i.next();
00291         
00292         if( !fDeleted.value( oldRec->id() ) )
00293         {
00294             DEBUGKPILOT << "Deleting record: [" << oldRec->id() << "].";
00295             
00296             if( !commitDelete( oldRec ) )
00297             {
00298                 // Commit failed.
00299                 return false;
00300             }
00301             
00302             // Commit succeeded.
00303             
00304             fDeleted.insert( oldRec->id(), true );
00305         }
00306     }
00307     
00308     // Give implementing classes the change to do things if necessary.
00309     return _commit();
00310 }
00311 
00312 bool DataProxy::rollback()
00313 {
00314     FUNCTIONSETUP;
00315     
00316     // Delete committed new records.
00317     QStringListIterator it( fCreated.keys() );
00318     
00319     while( it.hasNext() )
00320     {
00321         QString id = it.next();
00322         
00323         // Only undo creates that are committed.
00324         Record *rec = find( id );
00325         if( rec && fCreated.value( id ) )
00326         {
00327             DEBUGKPILOT << "Deleting created record: [" << rec->id() << "].";
00328             
00329             commitDelete( rec );
00330             fCreated.insert( rec->id(), false );
00331         }
00332     }
00333     
00334     // Reset the map
00335     fCreated.clear();
00336     
00337     // Undo changes to updated records.
00338     QListIterator<Record*> i( fOldRecords.values() );
00339     while( i.hasNext() )
00340     {
00341         Record *oldRec = i.next();
00342         if( fUpdated.value( oldRec->id() ) )
00343         {
00344             DEBUGKPILOT << "Restoring changed record: [" << oldRec->id() << "].";
00345             
00346             QString oldId = oldRec->id();
00347             commitUpdate( oldRec );
00348             
00349             // Id might have changed
00350             if( oldRec->id() != oldId )
00351             {
00352                 fUpdated.remove( oldId );
00353                 
00354                 fChangedIds.insert( oldId, oldRec->id() );
00355             }
00356             
00357             fUpdated.insert( oldRec->id(), false );
00358         }
00359     }
00360     
00361     // Restore deleted records.
00362     i = QListIterator<Record*>( fDeletedRecords.values() );
00363     while( i.hasNext() )
00364     {
00365         Record *oldRec = i.next();
00366         
00367         if( fDeleted.value( oldRec->id() ) )
00368         {
00369             DEBUGKPILOT << "Restoring deleted record: [" << oldRec->id() << "].";
00370         
00371             QString oldId = oldRec->id();
00372             commitCreate( oldRec );
00373             
00374             // Id might have changed
00375             if( oldRec->id() != oldId )
00376             {
00377                 fDeleted.remove( oldId );
00378                 
00379                 fChangedIds.insert( oldId, oldRec->id() );
00380             }
00381             
00382             fDeleted.insert( oldRec->id(), false );
00383         }
00384     }
00385     
00386     // Give implementing classes the change to rollback things if necessary.
00387     return _rollback();
00388 }
00389 
00390 QMap<QString,QString> DataProxy::changedIds()
00391 {
00392     return fChangedIds;
00393 }

kpilot

Skip menu "kpilot"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal