-rw-r--r-- | noncore/settings/backup/backuprestore.cpp | 84 | ||||
-rw-r--r-- | noncore/settings/backup/backuprestore.h | 4 |
2 files changed, 56 insertions, 32 deletions
diff --git a/noncore/settings/backup/backuprestore.cpp b/noncore/settings/backup/backuprestore.cpp index 3889744..478b22f 100644 --- a/noncore/settings/backup/backuprestore.cpp +++ b/noncore/settings/backup/backuprestore.cpp @@ -1,217 +1,226 @@ #include "backuprestore.h" #include "errordialog.h" /* OPIE */ #include <opie2/odebug.h> #include <opie2/ostorageinfo.h> using namespace Opie::Core; #include <qpe/qpeapplication.h> #include <qpe/resource.h> #include <qpe/config.h> /* QT */ #include <qapplication.h> #include <qmultilineedit.h> #include <qdir.h> #include <qfile.h> #include <qfileinfo.h> #include <qlistview.h> #include <qpushbutton.h> #include <qheader.h> #include <qmessagebox.h> #include <qcombobox.h> #include <qlist.h> #include <qregexp.h> #include <qtextstream.h> #include <qtextview.h> #include <qlineedit.h> +#include <qstringlist.h> /* STD */ #include <errno.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <dirent.h> #define HEADER_NAME 0 #define HEADER_BACKUP 1 #define BACKUP_LOCATION 2 #define EXTENSION ".bck" const QString tempFileName = "/tmp/backup.err"; BackupAndRestore::BackupAndRestore( QWidget* parent, const char* name, WFlags fl) : BackupAndRestoreBase(parent, name, fl) { backupList->header()->hide(); restoreList->header()->hide(); locationList->header()->hide(); connect( backupButton, SIGNAL( clicked() ), this, SLOT( backup() ) ); connect( restoreButton, SIGNAL( clicked() ), this, SLOT( restore() ) ); connect( backupList, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( selectItem( QListViewItem* ) ) ); connect( restoreSource, SIGNAL( activated( int ) ), this, SLOT( sourceDirChanged( int ) ) ); connect( addLocationButton, SIGNAL( clicked() ), this, SLOT( addLocation() ) ); connect( removeLocationButton, SIGNAL( clicked() ), this, SLOT( removeLocation() ) ); connect( saveLocationsButton, SIGNAL( clicked() ), this, SLOT( saveLocations() ) ); //add directorys for backing up applicationSettings = new QListViewItem(backupList, "Application Settings", "", "Settings/"); selectItem(applicationSettings); applicationSettings = new QListViewItem(backupList, "Application Data", "", "Applications/"); selectItem(applicationSettings); documents= new QListViewItem(backupList, "Documents", "", "Documents/"); selectItem(documents); scanForApplicationSettings(); - refreshBackupLocations(); refreshLocations(); - - Config config("BackupAndRestore"); - //read last locations - config.setGroup("LastLocation"); - QString lastStoreLocation = config.readEntry( "LastStoreLocation", "" ); - QString lastRestoreLocation = config.readEntry( "LastRestoreLocation", "" ); - int locationIndex = 0; - - QMap<QString, QString>::Iterator it; - for( it = backupLocations.begin(); it != backupLocations.end(); ++it ) - { - storeToLocation->insertItem(it.key()); - restoreSource->insertItem(it.key()); - - //check for last locations - if ( it.key() == lastStoreLocation ) - storeToLocation->setCurrentItem( locationIndex ); - if ( it.key() == lastRestoreLocation ) - restoreSource->setCurrentItem( locationIndex ); - locationIndex++; - } + refreshBackupLocations(); // Read the list of items to ignore. QList<QString> dontBackupList; dontBackupList.setAutoDelete(true); + Config config("BackupAndRestore"); config.setGroup("DontBackup"); int total = config.readNumEntry("Total", 0); for(int i = 0; i < total; i++) { dontBackupList.append(new QString(config.readEntry(QString("%1").arg(i), ""))); } QList<QListViewItem> list; getAllItems(backupList->firstChild(), list); for(uint i = 0; i < list.count(); i++) { QString text = list.at(i)->text(HEADER_NAME); for(uint i2 = 0; i2 < dontBackupList.count(); i2++) { if(*dontBackupList.at(i2) == text) { selectItem(list.at(i)); break; } } } QPEApplication::showWidget( this ); } BackupAndRestore::~BackupAndRestore() { QList<QListViewItem> list; getAllItems(backupList->firstChild(), list); Config config("BackupAndRestore"); config.setGroup("DontBackup"); config.clearGroup(); int count = 0; for(uint i = 0; i < list.count(); i++) { if(list.at(i)->text(HEADER_BACKUP) == "") { config.writeEntry(QString("%1").arg(count), list.at(i)->text(HEADER_NAME)); count++; } } config.writeEntry("Total", count); // Remove Temp File if ( QFile::exists( tempFileName ) ) QFile::remove( tempFileName ); } void BackupAndRestore::refreshBackupLocations() { backupLocations.clear(); // Add cards Opie::Core::OStorageInfo storage; backupLocations.insert( "Documents", QDir::homeDirPath() + "/Documents" ); if ( storage.hasCf() ) { backupLocations.insert( "CF", storage.cfPath() ); odebug << "Cf Path: " + storage.cfPath() << oendl; } if ( storage.hasSd() ) { backupLocations.insert( "SD", storage.sdPath() ); odebug << " Sd Path: " + storage.sdPath() << oendl; } if ( storage.hasMmc() ) { backupLocations.insert( "MMC", storage.mmcPath() ); odebug << "Mmc Path: " + storage.mmcPath() << oendl; } - // Add own locations from locationList - // todo implementation + for ( QListViewItemIterator it( locationList ); it.current(); ++it ) + { + backupLocations.insert( it.current()->text( 0 ), it.current()->text( 0 ) ); + } + + //update QComboBox + storeToLocation->clear(); + restoreSource->clear(); + + //read last locations + Config config("BackupAndRestore"); + config.setGroup("LastLocation"); + QString lastStoreLocation = config.readEntry( "LastStoreLocation", "" ); + QString lastRestoreLocation = config.readEntry( "LastRestoreLocation", "" ); + int locationIndex = 0; + + //fill QComboBox + QMap<QString, QString>::Iterator it; + for( it = backupLocations.begin(); it != backupLocations.end(); ++it ) + { + storeToLocation->insertItem(it.key()); + restoreSource->insertItem(it.key()); + + //check for last locations + if ( it.key() == lastStoreLocation ) + storeToLocation->setCurrentItem( locationIndex ); + if ( it.key() == lastRestoreLocation ) + restoreSource->setCurrentItem( locationIndex ); + locationIndex++; + } } QList<QListViewItem> BackupAndRestore::getAllItems(QListViewItem *item, QList<QListViewItem> &list) { while(item) { if(item->childCount() > 0) getAllItems(item->firstChild(), list); list.append(item); item = item->nextSibling(); } return list; } /** * Selects and unselects the item by setting the HEADER_BACKUP to B or !. * and changing the icon to match * @param currentItem the item to swich the selection choice. */ void BackupAndRestore::selectItem(QListViewItem *currentItem) { if(!currentItem) return; if(currentItem->text(HEADER_BACKUP) == "B") { currentItem->setPixmap(HEADER_NAME, Resource::loadPixmap("backup/null")); currentItem->setText(HEADER_BACKUP, ""); } else { currentItem->setPixmap(HEADER_NAME, Resource::loadPixmap("backup/check")); currentItem->setText(HEADER_BACKUP, "B"); } } void BackupAndRestore::scanForApplicationSettings() { QDir d( QDir::homeDirPath() + "/" + QString( applicationSettings->text(BACKUP_LOCATION) ) ); d.setFilter( QDir::Dirs | QDir::Files | QDir::NoSymLinks ); const QFileInfoList *list = d.entryInfoList(); QFileInfoListIterator it( *list ); QFileInfo *fi; while ( (fi=it.current()) ) { //odebug << (d.path()+"/"+fi->fileName()).latin1() << oendl; if ( ( fi->fileName() != "." ) && ( fi->fileName() != ".." ) ) { @@ -265,97 +274,96 @@ void BackupAndRestore::backup() .arg( getExcludeFile() ) .arg( backupFiles ) .arg( outputFile.latin1() ) .arg( tempFileName.latin1() ); odebug << commandLine << oendl; int r = system( commandLine ); if(r != 0) { perror("Error: "); QString errorMsg= tr( "Error from System:\n" ) + (QString)strerror( errno ); switch( QMessageBox::critical(this, tr( "Message" ), tr( "Backup Failed!" ) + "\n" + errorMsg, QString( tr( "Ok" ) ), QString( tr( "Details" ) ) ) ) { case 1: owarn << "Details pressed !" << oendl; ErrorDialog* pErrDialog = new ErrorDialog( this, NULL, true ); QFile errorFile( tempFileName ); if ( errorFile.open(IO_ReadOnly) ) { QTextStream t( &errorFile ); QString s; while ( !t.eof() ) { // until end of file... s += t.readLine(); // line of text excluding '\n' } errorFile.close(); pErrDialog->m_textarea->setText( s ); } else { pErrDialog->m_textarea->setText( "Unable to open File: /tmp/backup.er" ); } QPEApplication::execDialog( pErrDialog ); delete pErrDialog; break; } setCaption(tr("Backup and Restore.. Failed !!")); return; } else { QMessageBox::information(this, tr( "Message" ), tr( "Backup Successful." ), QString(tr( "Ok" ) ) ); - } //write store-location Config config( "BackupAndRestore" ); config.setGroup( "LastLocation" ); config.writeEntry( "LastStoreLocation", storeToLocation->currentText() ); setCaption(tr("Backup and Restore")); } /*** * Get a list of all of the files to backup. */ int BackupAndRestore::getBackupFiles(QString &backupFiles, QListViewItem *parent) { QListViewItem * currentItem; QString currentHome; if(!parent) currentItem = backupList->firstChild(); else { currentItem = parent->firstChild(); currentHome = parent->text(BACKUP_LOCATION); } uint count = 0; while( currentItem != 0 ) { if(currentItem->text(HEADER_BACKUP) == "B" ) { if(currentItem->childCount() == 0 ) { if(parent == NULL) backupFiles += currentItem->text(BACKUP_LOCATION); else backupFiles += currentHome + currentItem->text(HEADER_NAME); backupFiles += " "; count++; } else { count += getBackupFiles(backupFiles, currentItem); } } currentItem = currentItem->nextSibling(); } return count; } @@ -491,79 +499,95 @@ void BackupAndRestore::restore() break; } } else { QMessageBox::information(this, tr( "Message" ), tr( "Restore Successful." ), QString( tr( "Ok") ) ); } //write restore-location Config config( "BackupAndRestore" ); config.setGroup( "LastLocation" ); config.writeEntry( "LastRestoreLocation", restoreSource->currentText() ); setCaption(tr("Backup and Restore")); } /** * Check for exclude in Applications/backup * If it does not exist, the function will create the file with *.bck as content * The exclude_files is read by tar and will provide to exclude special files out from backup. * e.g. alle *.bck files (backup-files) will not be backed up by default */ QString BackupAndRestore::getExcludeFile() { QString excludeFileName = Global::applicationFileName( "backup", "exclude" ); if ( !QFile::exists( excludeFileName ) ) { QFile excludeFile( excludeFileName); if ( excludeFile.open( IO_WriteOnly ) == true ) { QTextStream writeStream( &excludeFile ); writeStream << "*.bck" << "\n"; excludeFile.close(); } else { return QString::null; } } return excludeFileName; } void BackupAndRestore::refreshLocations() { locationList->clear(); + //todo: implement add locations - odebug << "not implemented yet" << oendl; + Config config( "BackupAndRestore" ); + config.setGroup( "Locations" ); + + QStringList locations( config.readListEntry( "locations", '|' ) ); + + for ( QStringList::Iterator it = locations.begin(); it != locations.end(); ++it ) { + (void) new QListViewItem( locationList, *it ); + } } void BackupAndRestore::addLocation() { if ( ( !locationEdit->text().isEmpty() ) && ( QDir( locationEdit->text() ).exists() ) ) { (void) new QListViewItem( locationList, locationEdit->text() ); locationEdit->setText( "" ); } } void BackupAndRestore::removeLocation() { if ( locationList->selectedItem() ) { delete( locationList->selectedItem() ); } } -void BackupAndRestore::saveLocation() +void BackupAndRestore::saveLocations() { - //todo: implement - odebug << "not implemented yet" << oendl; -} + Config config("BackupAndRestore"); + config.setGroup("Locations"); + QStringList locations; + for ( QListViewItemIterator it( locationList ); it.current(); ++it ) + { + locations.append( it.current()->text( 0 ) ); + } + config.writeEntry( "locations", locations, '|' ); + + refreshBackupLocations(); +} // backuprestore.cpp diff --git a/noncore/settings/backup/backuprestore.h b/noncore/settings/backup/backuprestore.h index 527b3cf..caf6243 100644 --- a/noncore/settings/backup/backuprestore.h +++ b/noncore/settings/backup/backuprestore.h @@ -1,53 +1,53 @@ #ifndef WINDOW_H #define WINDOW_H #include <qmainwindow.h> #include "backuprestorebase.h" #include <qmap.h> #include <qlist.h> class QListViewItem; +class QStringList; class BackupAndRestore : public BackupAndRestoreBase { Q_OBJECT public: BackupAndRestore( QWidget* parent = 0, const char* name = 0, WFlags fl = 0); ~BackupAndRestore(); static QString appName() { return QString::fromLatin1("backup"); } private slots: void backup(); void restore(); void selectItem(QListViewItem *currentItem); void sourceDirChanged(int); void rescanFolder(QString directory); void fileListUpdate(); void addLocation(); void removeLocation(); - void saveLocation(); + void saveLocations(); private: void scanForApplicationSettings(); int getBackupFiles(QString &backupFiles, QListViewItem *parent); QString getExcludeFile(); QMap<QString, QString> backupLocations; QList<QListViewItem> getAllItems(QListViewItem *item, QList<QListViewItem> &list); void refreshBackupLocations(); void refreshLocations(); QListViewItem *systemSettings; QListViewItem *applicationSettings; QListViewItem *documents; - }; #endif // backuprestore.h |