kpilot

cleanupstate.cc

Go to the documentation of this file.
00001 /* KPilot
00002 **
00003 ** Copyright (C) 2006 by Bertjan Broeksema <b.broeksema@gmail.com>
00004 **
00005 ** This file is the implementation of the CleanUpState.
00006 */
00007 
00008 /*
00009 ** This program is free software; you can redistribute it and/or modify
00010 ** it under the terms of the GNU General Public License as published by
00011 ** the Free Software Foundation; either version 2 of the License, or
00012 ** (at your option) any later version.
00013 **
00014 ** This program is distributed in the hope that it will be useful,
00015 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017 ** GNU General Public License for more details.
00018 **
00019 ** You should have received a copy of the GNU General Public License
00020 ** along with this program in a file called COPYING; if not, write to
00021 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022 ** MA 02110-1301, USA.
00023 */
00024 
00025 /*
00026 ** Bug reports and questions can be sent to kde-pim@kde.org
00027 */
00028 
00029 #include <options.h>
00030 
00031 #include <kio/netaccess.h>
00032 #include <qfile.h>
00033 
00034 #include "pilotDatabase.h"
00035 
00036 #include "vcal-conduitbase.h"
00037 #include "vcalconduitSettings.h"
00038 #include "cleanupstate.h"
00039 
00040 
00041 CleanUpState::CleanUpState()
00042 {
00043     fState = eCleanUp;
00044 }
00045 
00046 CleanUpState::~CleanUpState()
00047 {
00048 }
00049 
00050 void CleanUpState::startSync( ConduitAction *ca )
00051 {
00052     FUNCTIONSETUP;
00053 
00054     VCalConduitBase *vccb = dynamic_cast<VCalConduitBase*>(ca);
00055     if( !vccb )
00056     {
00057         return;
00058     }
00059 
00060     DEBUGKPILOT << fname << ": Starting CleanUpState." << endl;
00061 
00062     vccb->addLogMessage( i18n( "Cleaning up ..." ) );
00063     vccb->postSync();
00064 
00065     if ( vccb->database() )
00066     {
00067         vccb->database()->resetSyncFlags();
00068         vccb->database()->cleanup();
00069     }
00070     if ( vccb->localDatabase() )
00071     {
00072         vccb->localDatabase()->resetSyncFlags();
00073         vccb->localDatabase()->cleanup();
00074     }
00075 
00076     KCal::Calendar *fCalendar = vccb->calendar();
00077     QString fCalendarFile = vccb->calendarFile();
00078 
00079     if ( fCalendar )
00080     {
00081         KURL kurl( vccb->config()->calendarFile() );
00082         switch( vccb->config()->calendarType() )
00083         {
00084         case VCalConduitSettings::eCalendarLocal:
00085             dynamic_cast<KCal::CalendarLocal*>(fCalendar)->save( fCalendarFile );
00086             if(!kurl.isLocalFile())
00087             {
00088                 if( !KIO::NetAccess::upload( fCalendarFile
00089                     , vccb->config()->calendarFile(), 0L) )
00090                 {
00091                     vccb->addLogError( i18n( "An error occurred while uploading"
00092                         " \"%1\". You can try to upload "
00093                         "the temporary local file \"%2\" manually.")
00094                         .arg(vccb->config()->calendarFile()).arg(fCalendarFile));
00095                 }
00096                 else {
00097                     KIO::NetAccess::removeTempFile( fCalendarFile );
00098                 }
00099                 QFile backup( fCalendarFile + CSL1( "~" ) );
00100                 backup.remove();
00101             }
00102             break;
00103         case VCalConduitSettings::eCalendarResource:
00104             fCalendar->save();
00105             break;
00106         default:
00107             break;
00108         }
00109         fCalendar->close();
00110     }
00111 
00112     vccb->setHasNextRecord( false );
00113 }
00114 
00115 void CleanUpState::handleRecord( ConduitAction * )
00116 {
00117     FUNCTIONSETUP;
00118 }
00119 
00120 void CleanUpState::finishSync( ConduitAction *ca )
00121 {
00122     FUNCTIONSETUP;
00123 
00124     VCalConduitBase *vccb = dynamic_cast<VCalConduitBase*>(ca);
00125     if( !vccb )
00126     {
00127         return;
00128     }
00129 
00130     DEBUGKPILOT << fname << ": Finished CleanUpState." << endl;
00131     vccb->setState( 0L );
00132 }