From fd38343ef53c9b2a48208f747100579703cc1814 Mon Sep 17 00:00:00 2001 From: zautrix Date: Thu, 21 Apr 2005 17:43:18 +0000 Subject: backup impl --- (limited to 'microkde') diff --git a/microkde/kapplication.cpp b/microkde/kapplication.cpp index 80a83e0..539397b 100644 --- a/microkde/kapplication.cpp +++ b/microkde/kapplication.cpp @@ -124,3 +124,123 @@ void KApplication::showText(QString caption, QString text) dia.exec(); } + +#include +#include +#include +#include +#include +#include +#include "kglobal.h" +#include "klocale.h" + +class KBackupPrefs : public QDialog +{ + public: + KBackupPrefs( QString message , QWidget *parent=0, const char *name=0 ) : + QDialog( parent, name, true ) + { + setCaption( i18n("Backup Failed!") ); + QVBoxLayout* lay = new QVBoxLayout( this ); + lay->setSpacing( 3 ); + lay->setMargin( 3 ); + QLabel * lab = new QLabel( message, this ); + lay->addWidget( lab ); + QButtonGroup* format = new QButtonGroup( 1, Horizontal, i18n("Choose action"), this ); + lay->addWidget( format ); + format->setExclusive ( true ) ; + vcal = new QRadioButton(i18n("Try again now"), format ); + tcal = new QRadioButton(i18n("Try again later"), format ); + ical = new QRadioButton(i18n("Try again tomorrow"), format ); + ocal = new QRadioButton(i18n("Disable backup"), format ); + vcal->setChecked( true ); + QPushButton * ok = new QPushButton( i18n("OK"), this ); + lay->addWidget(ok ); + connect ( ok,SIGNAL(clicked() ),this , SLOT ( accept() ) ); + } + + bool again() { return vcal->isChecked(); } + bool later() { return tcal->isChecked(); } + bool againTomorrow() { return ical->isChecked(); } +private: + QRadioButton* vcal, *ical, *ocal, *tcal; +}; +int KApplication::createBackup( QString fn, QString dp, int numBup ) +{ + if ( numBup < 1) return 3; + int ret = 3; + qDebug("KApplication::createBackup %s --- %s --- %d", fn.latin1(), dp.latin1(), numBup); + QDir bupDir ( dp ); + bool tryAgain = true; + while ( tryAgain ) { + if ( !bupDir.exists() ) { + KBackupPrefs noDir( i18n("Backup directory does not exist: ") + dp.right(30)); + if ( !noDir.exec() ) return 3; + if ( noDir.againTomorrow() ) { + return 0; + } else if ( noDir.later() ) { + return 3; + } else if ( !noDir.again() ) { + return 2; + } + } else { + tryAgain = false; + } + } + // we have a valid dir! + QStringList allFileList = bupDir.entryList(QDir::Files); + QFileInfo fileInfo ( fn ); + QString fName = fileInfo.fileName (); + QStringList fileList; + + int i; + for ( i = 0; i < allFileList.count(); ++i ) { + QString fi = allFileList[i]; + if ( fi. find( fName ) > -1 ) + fileList.append( fi ); + } + qDebug("%d backup files exist ", fileList.count()); + int count = fileList.count(); + fileList.sort(); + int remCount = 0; + while ( count >= numBup ) { + QString fnr = dp + "/"+fileList[remCount]; + QFile::remove( fnr ); + --count; + ++remCount; + } + QDateTime mRunStart = QDateTime::currentDateTime(); + QString file = "%1%2%3-%4%5%6-"; + file = file.arg( mRunStart.date().year(), 4).arg( mRunStart.date().month(),2 ).arg( mRunStart.date().day(), 2 ).arg( mRunStart.time().hour(),2 ).arg( mRunStart.time().minute(),2 ).arg( mRunStart.time().second(),2 ); + file.replace ( QRegExp (" "), "0" ); + file += fName ; + file = dp + "/"+file; + QString command; + int res = 0; +#ifdef _WIN32_ + command = "xcopy "+ fn+ " "+file; + command = QDir::convertSeparators( command ); +#else + command = "cp "+ fn+ " "+file; +#endif + qDebug("command %s ",command.latin1() ); + tryAgain = true; + while ( tryAgain ) { + res = system ( command.latin1() ); + qDebug("copy result %d ", res); + if ( res != 0 ) { + KBackupPrefs noDir( i18n("The backup copy command failed!")); + if ( !noDir.exec() ) return 3; + if ( noDir.againTomorrow() ) { + return 0; + } else if ( noDir.later() ) { + return 3; + } else if ( !noDir.again() ) { + return 2; + } + } else { + tryAgain = false; + } + } + return 1; +} diff --git a/microkde/kapplication.h b/microkde/kapplication.h index f7eb1ef..de5a084 100644 --- a/microkde/kapplication.h +++ b/microkde/kapplication.h @@ -25,6 +25,7 @@ class KApplication static void showFile(QString caption, QString file); static void showText(QString caption, QString text); static bool convert2latin1(QString file); + static int createBackup( QString fn, QString dp, int numBup ); }; -- cgit v0.9.0.2