kitchensync

backup.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library 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 GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "backup.h"
00023 
00024 #include "konnectorview.h"
00025 #include "backupview.h"
00026 
00027 #include <konnector.h>
00028 #include <konnectormanager.h>
00029 #include <konnectorinfo.h>
00030 #include <mainwindow.h>
00031 #include <calendarsyncee.h>
00032 #include <engine.h>
00033 
00034 #include <kaboutdata.h>
00035 #include <kiconloader.h>
00036 #include <kparts/genericfactory.h>
00037 #include <kmessagebox.h>
00038 #include <kdialog.h>
00039 #include <kdialogbase.h>
00040 #include <kstandarddirs.h>
00041 #include <kprocess.h>
00042 
00043 #include <qlabel.h>
00044 #include <qlistview.h>
00045 #include <qcombobox.h>
00046 #include <qpushbutton.h>
00047 #include <qtextview.h>
00048 #include <qlayout.h>
00049 #include <qdatetime.h>
00050 #include <qcheckbox.h>
00051 #include <qvbox.h>
00052 #include <qdir.h>
00053 
00054 using namespace KCal;
00055 using namespace KSync;
00056 
00057 typedef KParts::GenericFactory< Backup> BackupFactory;
00058 K_EXPORT_COMPONENT_FACTORY( libksync_backup, BackupFactory )
00059 
00060 Backup::Backup( QWidget *parent, const char *name,
00061                     QObject *, const char *,const QStringList & )
00062   : ActionPart( parent, name ), mWidget( 0 ), mActive( false )
00063 {
00064   mPixmap = KGlobal::iconLoader()->loadIcon("kcmdrkonqi", KIcon::Desktop, 48 );
00065 }
00066 
00067 KAboutData *Backup::createAboutData()
00068 {
00069   return new KAboutData("KSyncBackup", I18N_NOOP("Sync Backup Part"), "0.0" );
00070 }
00071 
00072 Backup::~Backup()
00073 {
00074   delete mWidget;
00075 }
00076 
00077 QString Backup::type() const
00078 {
00079   return QString::fromLatin1("backup");
00080 }
00081 
00082 QString Backup::title() const
00083 {
00084   return i18n("Konnector Backup");
00085 }
00086 
00087 QString Backup::description() const
00088 {
00089   return i18n("Backup for Konnectors");
00090 }
00091 
00092 QPixmap *Backup::pixmap()
00093 {
00094   return &mPixmap;
00095 }
00096 
00097 QString Backup::iconName() const
00098 {
00099   return QString::fromLatin1("kcmsystem");
00100 }
00101 
00102 bool Backup::hasGui() const
00103 {
00104   return true;
00105 }
00106 
00107 QWidget *Backup::widget()
00108 {
00109   if( !mWidget ) {
00110     mWidget = new QWidget;
00111     QBoxLayout *topLayout = new QVBoxLayout( mWidget );
00112     topLayout->setSpacing( KDialog::spacingHint() );
00113     
00114     QBoxLayout *konnectorLayout = new QHBoxLayout( topLayout );
00115 
00116     mKonnectorList = new KonnectorView( mWidget );
00117     konnectorLayout->addWidget( mKonnectorList, 1 );
00118 
00119     mKonnectorList->updateKonnectorList();
00120 
00121     mBackupView = new BackupView( mWidget );
00122     konnectorLayout->addWidget( mBackupView );
00123     connect( mBackupView, SIGNAL( backupDeleted( const QString & ) ),
00124              SLOT( slotBackupDeleted( const QString & ) ) );
00125     
00126     mBackupView->updateBackupList();
00127 
00128     mLogView = new QTextView( mWidget );
00129     mLogView->setTextFormat( LogText );
00130     topLayout->addWidget( mLogView );
00131 
00132     logMessage( i18n("Ready.") );
00133   }
00134   return mWidget;
00135 }
00136 
00137 void Backup::logMessage( const QString &message )
00138 {
00139   QString text = "<b>" + QTime::currentTime().toString() + "</b>: ";
00140   text += message;
00141 
00142   kdDebug() << "LOG: " << text << endl;
00143 
00144   mLogView->append( text );
00145 }
00146 
00147 void Backup::executeAction()
00148 {
00149   logMessage( i18n("Starting backup") );
00150 
00151   mBackupView->createBackupDir();
00152 
00153   Konnector::List konnectors = core()->engine()->konnectors();
00154   Konnector *k;
00155   for( k = konnectors.first(); k; k = konnectors.next() ) {
00156     backupKonnector( k );
00157   }
00158 
00159   logMessage( i18n("Backup finished.") );
00160 
00161   mBackupView->updateBackupList();
00162 }
00163 
00164 void Backup::backupKonnector( Konnector *k )
00165 {
00166   logMessage( i18n("Syncees read from '%1'").arg( k->resourceName() ) );
00167 
00168   SynceeList syncees = k->syncees();
00169 
00170   if ( syncees.count() == 0 ) {
00171     logMessage( i18n("Syncee list is empty.") );
00172   } else {
00173     logMessage( i18n("Performing backup.") );
00174   
00175     SynceeList::ConstIterator it;
00176     for( it = syncees.begin(); it != syncees.end(); ++it ) {
00177       if ( !(*it)->isValid() ) continue;
00178       QString filename = mBackupView->backupFile( k, *it );
00179       kdDebug() << "FILENAME: " << filename << endl;
00180       QString type = (*it)->type();
00181       if ( (*it)->writeBackup( filename ) ) {
00182         logMessage( i18n("Wrote backup for %1.").arg( type ) );
00183       } else {
00184         logMessage( i18n("<b>Error:</b> Can't write backup for %1.")
00185                     .arg( type ) );
00186       }
00187     }
00188   }
00189 }
00190 
00191 void Backup::slotBackupDeleted( const QString &name )
00192 {
00193   logMessage( i18n("Backup '%1' deleted").arg( name ) );
00194 }
00195 
00196 #include "backup.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys