summaryrefslogtreecommitdiff
authorar <ar>2004-07-25 21:41:01 (UTC)
committer ar <ar>2004-07-25 21:41:01 (UTC)
commitd61db324b7bac184b067b6f798ec3c85749bac15 (patch) (unidiff)
tree14f9d2f24a82fa8eaf12bb378f2d4125979f7751
parent8b3a81d53c7db43bf748c2390b429dde2ab9aa82 (diff)
downloadopie-d61db324b7bac184b067b6f798ec3c85749bac15.zip
opie-d61db324b7bac184b067b6f798ec3c85749bac15.tar.gz
opie-d61db324b7bac184b067b6f798ec3c85749bac15.tar.bz2
- implementation of custom backup- and restore-locations
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/backup/backuprestore.cpp84
-rw-r--r--noncore/settings/backup/backuprestore.h4
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,569 +1,593 @@
1#include "backuprestore.h" 1#include "backuprestore.h"
2#include "errordialog.h" 2#include "errordialog.h"
3 3
4 4
5/* OPIE */ 5/* OPIE */
6#include <opie2/odebug.h> 6#include <opie2/odebug.h>
7#include <opie2/ostorageinfo.h> 7#include <opie2/ostorageinfo.h>
8using namespace Opie::Core; 8using namespace Opie::Core;
9 9
10#include <qpe/qpeapplication.h> 10#include <qpe/qpeapplication.h>
11#include <qpe/resource.h> 11#include <qpe/resource.h>
12#include <qpe/config.h> 12#include <qpe/config.h>
13 13
14/* QT */ 14/* QT */
15#include <qapplication.h> 15#include <qapplication.h>
16#include <qmultilineedit.h> 16#include <qmultilineedit.h>
17#include <qdir.h> 17#include <qdir.h>
18#include <qfile.h> 18#include <qfile.h>
19#include <qfileinfo.h> 19#include <qfileinfo.h>
20#include <qlistview.h> 20#include <qlistview.h>
21#include <qpushbutton.h> 21#include <qpushbutton.h>
22#include <qheader.h> 22#include <qheader.h>
23#include <qmessagebox.h> 23#include <qmessagebox.h>
24#include <qcombobox.h> 24#include <qcombobox.h>
25#include <qlist.h> 25#include <qlist.h>
26#include <qregexp.h> 26#include <qregexp.h>
27#include <qtextstream.h> 27#include <qtextstream.h>
28#include <qtextview.h> 28#include <qtextview.h>
29#include <qlineedit.h> 29#include <qlineedit.h>
30#include <qstringlist.h>
30 31
31/* STD */ 32/* STD */
32#include <errno.h> 33#include <errno.h>
33#include <stdlib.h> 34#include <stdlib.h>
34#include <unistd.h> 35#include <unistd.h>
35#include <sys/stat.h> 36#include <sys/stat.h>
36#include <dirent.h> 37#include <dirent.h>
37 38
38#define HEADER_NAME 0 39#define HEADER_NAME 0
39#define HEADER_BACKUP 1 40#define HEADER_BACKUP 1
40#define BACKUP_LOCATION 2 41#define BACKUP_LOCATION 2
41 42
42#define EXTENSION ".bck" 43#define EXTENSION ".bck"
43 44
44const QString tempFileName = "/tmp/backup.err"; 45const QString tempFileName = "/tmp/backup.err";
45 46
46BackupAndRestore::BackupAndRestore( QWidget* parent, const char* name, WFlags fl) 47BackupAndRestore::BackupAndRestore( QWidget* parent, const char* name, WFlags fl)
47 : BackupAndRestoreBase(parent, name, fl) 48 : BackupAndRestoreBase(parent, name, fl)
48{ 49{
49 backupList->header()->hide(); 50 backupList->header()->hide();
50 restoreList->header()->hide(); 51 restoreList->header()->hide();
51 locationList->header()->hide(); 52 locationList->header()->hide();
52 connect( backupButton, SIGNAL( clicked() ), this, SLOT( backup() ) ); 53 connect( backupButton, SIGNAL( clicked() ), this, SLOT( backup() ) );
53 connect( restoreButton, SIGNAL( clicked() ), this, SLOT( restore() ) ); 54 connect( restoreButton, SIGNAL( clicked() ), this, SLOT( restore() ) );
54 connect( backupList, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( selectItem( QListViewItem* ) ) ); 55 connect( backupList, SIGNAL( clicked( QListViewItem* ) ), this, SLOT( selectItem( QListViewItem* ) ) );
55 connect( restoreSource, SIGNAL( activated( int ) ), this, SLOT( sourceDirChanged( int ) ) ); 56 connect( restoreSource, SIGNAL( activated( int ) ), this, SLOT( sourceDirChanged( int ) ) );
56 connect( addLocationButton, SIGNAL( clicked() ), this, SLOT( addLocation() ) ); 57 connect( addLocationButton, SIGNAL( clicked() ), this, SLOT( addLocation() ) );
57 connect( removeLocationButton, SIGNAL( clicked() ), this, SLOT( removeLocation() ) ); 58 connect( removeLocationButton, SIGNAL( clicked() ), this, SLOT( removeLocation() ) );
58 connect( saveLocationsButton, SIGNAL( clicked() ), this, SLOT( saveLocations() ) ); 59 connect( saveLocationsButton, SIGNAL( clicked() ), this, SLOT( saveLocations() ) );
59 60
60 //add directorys for backing up 61 //add directorys for backing up
61 applicationSettings = new QListViewItem(backupList, "Application Settings", "", "Settings/"); 62 applicationSettings = new QListViewItem(backupList, "Application Settings", "", "Settings/");
62 selectItem(applicationSettings); 63 selectItem(applicationSettings);
63 applicationSettings = new QListViewItem(backupList, "Application Data", "", "Applications/"); 64 applicationSettings = new QListViewItem(backupList, "Application Data", "", "Applications/");
64 selectItem(applicationSettings); 65 selectItem(applicationSettings);
65 documents= new QListViewItem(backupList, "Documents", "", "Documents/"); 66 documents= new QListViewItem(backupList, "Documents", "", "Documents/");
66 selectItem(documents); 67 selectItem(documents);
67 68
68 scanForApplicationSettings(); 69 scanForApplicationSettings();
69 refreshBackupLocations();
70 refreshLocations(); 70 refreshLocations();
71 71 refreshBackupLocations();
72 Config config("BackupAndRestore");
73 //read last locations
74 config.setGroup("LastLocation");
75 QString lastStoreLocation = config.readEntry( "LastStoreLocation", "" );
76 QString lastRestoreLocation = config.readEntry( "LastRestoreLocation", "" );
77 int locationIndex = 0;
78
79 QMap<QString, QString>::Iterator it;
80 for( it = backupLocations.begin(); it != backupLocations.end(); ++it )
81 {
82 storeToLocation->insertItem(it.key());
83 restoreSource->insertItem(it.key());
84
85 //check for last locations
86 if ( it.key() == lastStoreLocation )
87 storeToLocation->setCurrentItem( locationIndex );
88 if ( it.key() == lastRestoreLocation )
89 restoreSource->setCurrentItem( locationIndex );
90 locationIndex++;
91 }
92 72
93 // Read the list of items to ignore. 73 // Read the list of items to ignore.
94 QList<QString> dontBackupList; 74 QList<QString> dontBackupList;
95 dontBackupList.setAutoDelete(true); 75 dontBackupList.setAutoDelete(true);
76 Config config("BackupAndRestore");
96 config.setGroup("DontBackup"); 77 config.setGroup("DontBackup");
97 int total = config.readNumEntry("Total", 0); 78 int total = config.readNumEntry("Total", 0);
98 for(int i = 0; i < total; i++) 79 for(int i = 0; i < total; i++)
99 { 80 {
100 dontBackupList.append(new QString(config.readEntry(QString("%1").arg(i), ""))); 81 dontBackupList.append(new QString(config.readEntry(QString("%1").arg(i), "")));
101 } 82 }
102 83
103 QList<QListViewItem> list; 84 QList<QListViewItem> list;
104 getAllItems(backupList->firstChild(), list); 85 getAllItems(backupList->firstChild(), list);
105 86
106 for(uint i = 0; i < list.count(); i++) 87 for(uint i = 0; i < list.count(); i++)
107 { 88 {
108 QString text = list.at(i)->text(HEADER_NAME); 89 QString text = list.at(i)->text(HEADER_NAME);
109 for(uint i2 = 0; i2 < dontBackupList.count(); i2++) 90 for(uint i2 = 0; i2 < dontBackupList.count(); i2++)
110 { 91 {
111 if(*dontBackupList.at(i2) == text) 92 if(*dontBackupList.at(i2) == text)
112 { 93 {
113 selectItem(list.at(i)); 94 selectItem(list.at(i));
114 break; 95 break;
115 } 96 }
116 } 97 }
117 } 98 }
118 QPEApplication::showWidget( this ); 99 QPEApplication::showWidget( this );
119} 100}
120 101
121BackupAndRestore::~BackupAndRestore() 102BackupAndRestore::~BackupAndRestore()
122{ 103{
123 QList<QListViewItem> list; 104 QList<QListViewItem> list;
124 getAllItems(backupList->firstChild(), list); 105 getAllItems(backupList->firstChild(), list);
125 106
126 Config config("BackupAndRestore"); 107 Config config("BackupAndRestore");
127 config.setGroup("DontBackup"); 108 config.setGroup("DontBackup");
128 config.clearGroup(); 109 config.clearGroup();
129 110
130 int count = 0; 111 int count = 0;
131 for(uint i = 0; i < list.count(); i++) 112 for(uint i = 0; i < list.count(); i++)
132 { 113 {
133 if(list.at(i)->text(HEADER_BACKUP) == "") 114 if(list.at(i)->text(HEADER_BACKUP) == "")
134 { 115 {
135 config.writeEntry(QString("%1").arg(count), list.at(i)->text(HEADER_NAME)); 116 config.writeEntry(QString("%1").arg(count), list.at(i)->text(HEADER_NAME));
136 count++; 117 count++;
137 } 118 }
138 } 119 }
139 config.writeEntry("Total", count); 120 config.writeEntry("Total", count);
140 121
141 // Remove Temp File 122 // Remove Temp File
142 if ( QFile::exists( tempFileName ) ) 123 if ( QFile::exists( tempFileName ) )
143 QFile::remove( tempFileName ); 124 QFile::remove( tempFileName );
144} 125}
145 126
146void BackupAndRestore::refreshBackupLocations() 127void BackupAndRestore::refreshBackupLocations()
147{ 128{
148 backupLocations.clear(); 129 backupLocations.clear();
149 // Add cards 130 // Add cards
150 Opie::Core::OStorageInfo storage; 131 Opie::Core::OStorageInfo storage;
151 backupLocations.insert( "Documents", QDir::homeDirPath() + "/Documents" ); 132 backupLocations.insert( "Documents", QDir::homeDirPath() + "/Documents" );
152 if ( storage.hasCf() ) 133 if ( storage.hasCf() )
153 { 134 {
154 backupLocations.insert( "CF", storage.cfPath() ); 135 backupLocations.insert( "CF", storage.cfPath() );
155 odebug << "Cf Path: " + storage.cfPath() << oendl; 136 odebug << "Cf Path: " + storage.cfPath() << oendl;
156 } 137 }
157 if ( storage.hasSd() ) 138 if ( storage.hasSd() )
158 { 139 {
159 backupLocations.insert( "SD", storage.sdPath() ); 140 backupLocations.insert( "SD", storage.sdPath() );
160 odebug << " Sd Path: " + storage.sdPath() << oendl; 141 odebug << " Sd Path: " + storage.sdPath() << oendl;
161 } 142 }
162 if ( storage.hasMmc() ) 143 if ( storage.hasMmc() )
163 { 144 {
164 backupLocations.insert( "MMC", storage.mmcPath() ); 145 backupLocations.insert( "MMC", storage.mmcPath() );
165 odebug << "Mmc Path: " + storage.mmcPath() << oendl; 146 odebug << "Mmc Path: " + storage.mmcPath() << oendl;
166 } 147 }
167 148
168 // Add own locations from locationList 149 for ( QListViewItemIterator it( locationList ); it.current(); ++it )
169 // todo implementation 150 {
151 backupLocations.insert( it.current()->text( 0 ), it.current()->text( 0 ) );
152 }
153
154 //update QComboBox
155 storeToLocation->clear();
156 restoreSource->clear();
157
158 //read last locations
159 Config config("BackupAndRestore");
160 config.setGroup("LastLocation");
161 QString lastStoreLocation = config.readEntry( "LastStoreLocation", "" );
162 QString lastRestoreLocation = config.readEntry( "LastRestoreLocation", "" );
163 int locationIndex = 0;
164
165 //fill QComboBox
166 QMap<QString, QString>::Iterator it;
167 for( it = backupLocations.begin(); it != backupLocations.end(); ++it )
168 {
169 storeToLocation->insertItem(it.key());
170 restoreSource->insertItem(it.key());
171
172 //check for last locations
173 if ( it.key() == lastStoreLocation )
174 storeToLocation->setCurrentItem( locationIndex );
175 if ( it.key() == lastRestoreLocation )
176 restoreSource->setCurrentItem( locationIndex );
177 locationIndex++;
178 }
170} 179}
171 180
172QList<QListViewItem> BackupAndRestore::getAllItems(QListViewItem *item, QList<QListViewItem> &list) 181QList<QListViewItem> BackupAndRestore::getAllItems(QListViewItem *item, QList<QListViewItem> &list)
173{ 182{
174 while(item) 183 while(item)
175 { 184 {
176 if(item->childCount() > 0) 185 if(item->childCount() > 0)
177 getAllItems(item->firstChild(), list); 186 getAllItems(item->firstChild(), list);
178 list.append(item); 187 list.append(item);
179 item = item->nextSibling(); 188 item = item->nextSibling();
180 } 189 }
181 return list; 190 return list;
182} 191}
183 192
184/** 193/**
185 * Selects and unselects the item by setting the HEADER_BACKUP to B or !. 194 * Selects and unselects the item by setting the HEADER_BACKUP to B or !.
186 * and changing the icon to match 195 * and changing the icon to match
187 * @param currentItem the item to swich the selection choice. 196 * @param currentItem the item to swich the selection choice.
188 */ 197 */
189void BackupAndRestore::selectItem(QListViewItem *currentItem) 198void BackupAndRestore::selectItem(QListViewItem *currentItem)
190{ 199{
191 if(!currentItem) 200 if(!currentItem)
192 return; 201 return;
193 202
194 if(currentItem->text(HEADER_BACKUP) == "B") 203 if(currentItem->text(HEADER_BACKUP) == "B")
195 { 204 {
196 currentItem->setPixmap(HEADER_NAME, Resource::loadPixmap("backup/null")); 205 currentItem->setPixmap(HEADER_NAME, Resource::loadPixmap("backup/null"));
197 currentItem->setText(HEADER_BACKUP, ""); 206 currentItem->setText(HEADER_BACKUP, "");
198 } 207 }
199 else 208 else
200 { 209 {
201 currentItem->setPixmap(HEADER_NAME, Resource::loadPixmap("backup/check")); 210 currentItem->setPixmap(HEADER_NAME, Resource::loadPixmap("backup/check"));
202 currentItem->setText(HEADER_BACKUP, "B"); 211 currentItem->setText(HEADER_BACKUP, "B");
203 } 212 }
204} 213}
205 214
206void BackupAndRestore::scanForApplicationSettings() 215void BackupAndRestore::scanForApplicationSettings()
207{ 216{
208 QDir d( QDir::homeDirPath() + "/" + QString( applicationSettings->text(BACKUP_LOCATION) ) ); 217 QDir d( QDir::homeDirPath() + "/" + QString( applicationSettings->text(BACKUP_LOCATION) ) );
209 d.setFilter( QDir::Dirs | QDir::Files | QDir::NoSymLinks ); 218 d.setFilter( QDir::Dirs | QDir::Files | QDir::NoSymLinks );
210 const QFileInfoList *list = d.entryInfoList(); 219 const QFileInfoList *list = d.entryInfoList();
211 QFileInfoListIterator it( *list ); 220 QFileInfoListIterator it( *list );
212 QFileInfo *fi; 221 QFileInfo *fi;
213 while ( (fi=it.current()) ) 222 while ( (fi=it.current()) )
214 { 223 {
215 //odebug << (d.path()+"/"+fi->fileName()).latin1() << oendl; 224 //odebug << (d.path()+"/"+fi->fileName()).latin1() << oendl;
216 if ( ( fi->fileName() != "." ) && ( fi->fileName() != ".." ) ) 225 if ( ( fi->fileName() != "." ) && ( fi->fileName() != ".." ) )
217 { 226 {
218 QListViewItem *newItem = new QListViewItem(applicationSettings, fi->fileName()); 227 QListViewItem *newItem = new QListViewItem(applicationSettings, fi->fileName());
219 selectItem(newItem); 228 selectItem(newItem);
220 } 229 }
221 ++it; 230 ++it;
222 } 231 }
223} 232}
224 233
225/** 234/**
226 * The "Backup" button has been pressed. Get a list of all of the files that 235 * The "Backup" button has been pressed. Get a list of all of the files that
227 * should be backed up. If there are no files, emit and error and exit. 236 * should be backed up. If there are no files, emit and error and exit.
228 * Determine the file name to store the backup in. Backup the file(s) using 237 * Determine the file name to store the backup in. Backup the file(s) using
229 * tar and gzip --best. Report failure or success 238 * tar and gzip --best. Report failure or success
230 */ 239 */
231void BackupAndRestore::backup() 240void BackupAndRestore::backup()
232{ 241{
233 QString backupFiles; 242 QString backupFiles;
234 if(getBackupFiles(backupFiles, NULL) == 0) 243 if(getBackupFiles(backupFiles, NULL) == 0)
235 { 244 {
236 QMessageBox::critical(this, "Message", 245 QMessageBox::critical(this, "Message",
237 "No items selected.",QString("Ok") ); 246 "No items selected.",QString("Ok") );
238 return; 247 return;
239 } 248 }
240 249
241 setCaption(tr("Backup and Restore... working...")); 250 setCaption(tr("Backup and Restore... working..."));
242 QString outputFile = backupLocations[storeToLocation->currentText()]; 251 QString outputFile = backupLocations[storeToLocation->currentText()];
243 252
244 QDateTime datetime = QDateTime::currentDateTime(); 253 QDateTime datetime = QDateTime::currentDateTime();
245 QString dateString = QString::number( datetime.date().year() ) + QString::number( datetime.date().month() ).rightJustify(2, '0') + 254 QString dateString = QString::number( datetime.date().year() ) + QString::number( datetime.date().month() ).rightJustify(2, '0') +
246 QString::number( datetime.date().day() ).rightJustify(2, '0'); 255 QString::number( datetime.date().day() ).rightJustify(2, '0');
247 256
248 outputFile += "/" + dateString; 257 outputFile += "/" + dateString;
249 258
250 QString t = outputFile; 259 QString t = outputFile;
251 int c = 1; 260 int c = 1;
252 while(QFile::exists(outputFile + EXTENSION)) 261 while(QFile::exists(outputFile + EXTENSION))
253 { 262 {
254 outputFile = t + QString("%1").arg(c); 263 outputFile = t + QString("%1").arg(c);
255 c++; 264 c++;
256 } 265 }
257 266
258 // We execute tar and compressing its output with gzip.. 267 // We execute tar and compressing its output with gzip..
259 // The error output will be written into a temp-file which could be provided 268 // The error output will be written into a temp-file which could be provided
260 // for debugging.. 269 // for debugging..
261 odebug << "Storing file: " << outputFile.latin1() << "" << oendl; 270 odebug << "Storing file: " << outputFile.latin1() << "" << oendl;
262 outputFile += EXTENSION; 271 outputFile += EXTENSION;
263 272
264 QString commandLine = QString( "cd %1 && (tar -X %1 -cz %2 Applications/backup/exclude -f %3 ) 2> %4" ).arg( QDir::homeDirPath() ) 273 QString commandLine = QString( "cd %1 && (tar -X %1 -cz %2 Applications/backup/exclude -f %3 ) 2> %4" ).arg( QDir::homeDirPath() )
265 .arg( getExcludeFile() ) 274 .arg( getExcludeFile() )
266 .arg( backupFiles ) 275 .arg( backupFiles )
267 .arg( outputFile.latin1() ) 276 .arg( outputFile.latin1() )
268 .arg( tempFileName.latin1() ); 277 .arg( tempFileName.latin1() );
269 278
270 odebug << commandLine << oendl; 279 odebug << commandLine << oendl;
271 280
272 int r = system( commandLine ); 281 int r = system( commandLine );
273 282
274 if(r != 0) 283 if(r != 0)
275 { 284 {
276 perror("Error: "); 285 perror("Error: ");
277 QString errorMsg= tr( "Error from System:\n" ) + (QString)strerror( errno ); 286 QString errorMsg= tr( "Error from System:\n" ) + (QString)strerror( errno );
278 287
279 switch( QMessageBox::critical(this, tr( "Message" ), tr( "Backup Failed!" ) + "\n" 288 switch( QMessageBox::critical(this, tr( "Message" ), tr( "Backup Failed!" ) + "\n"
280 + errorMsg, QString( tr( "Ok" ) ), QString( tr( "Details" ) ) ) ) 289 + errorMsg, QString( tr( "Ok" ) ), QString( tr( "Details" ) ) ) )
281 { 290 {
282 291
283 case 1: 292 case 1:
284 owarn << "Details pressed !" << oendl; 293 owarn << "Details pressed !" << oendl;
285 ErrorDialog* pErrDialog = new ErrorDialog( this, NULL, true ); 294 ErrorDialog* pErrDialog = new ErrorDialog( this, NULL, true );
286 QFile errorFile( tempFileName ); 295 QFile errorFile( tempFileName );
287 if ( errorFile.open(IO_ReadOnly) ) 296 if ( errorFile.open(IO_ReadOnly) )
288 { 297 {
289 QTextStream t( &errorFile ); 298 QTextStream t( &errorFile );
290 QString s; 299 QString s;
291 while ( !t.eof() ) 300 while ( !t.eof() )
292 { // until end of file... 301 { // until end of file...
293 s += t.readLine(); // line of text excluding '\n' 302 s += t.readLine(); // line of text excluding '\n'
294 } 303 }
295 errorFile.close(); 304 errorFile.close();
296 305
297 pErrDialog->m_textarea->setText( s ); 306 pErrDialog->m_textarea->setText( s );
298 } 307 }
299 else 308 else
300 { 309 {
301 pErrDialog->m_textarea->setText( "Unable to open File: /tmp/backup.er" ); 310 pErrDialog->m_textarea->setText( "Unable to open File: /tmp/backup.er" );
302 } 311 }
303 QPEApplication::execDialog( pErrDialog ); 312 QPEApplication::execDialog( pErrDialog );
304 delete pErrDialog; 313 delete pErrDialog;
305 break; 314 break;
306 } 315 }
307 setCaption(tr("Backup and Restore.. Failed !!")); 316 setCaption(tr("Backup and Restore.. Failed !!"));
308 return; 317 return;
309 } 318 }
310 else 319 else
311 { 320 {
312 QMessageBox::information(this, tr( "Message" ), tr( "Backup Successful." ), QString(tr( "Ok" ) ) ); 321 QMessageBox::information(this, tr( "Message" ), tr( "Backup Successful." ), QString(tr( "Ok" ) ) );
313
314 } 322 }
315 323
316 //write store-location 324 //write store-location
317 Config config( "BackupAndRestore" ); 325 Config config( "BackupAndRestore" );
318 config.setGroup( "LastLocation" ); 326 config.setGroup( "LastLocation" );
319 config.writeEntry( "LastStoreLocation", storeToLocation->currentText() ); 327 config.writeEntry( "LastStoreLocation", storeToLocation->currentText() );
320 328
321 setCaption(tr("Backup and Restore")); 329 setCaption(tr("Backup and Restore"));
322} 330}
323 331
324/*** 332/***
325 * Get a list of all of the files to backup. 333 * Get a list of all of the files to backup.
326 */ 334 */
327int BackupAndRestore::getBackupFiles(QString &backupFiles, QListViewItem *parent) 335int BackupAndRestore::getBackupFiles(QString &backupFiles, QListViewItem *parent)
328{ 336{
329 QListViewItem * currentItem; 337 QListViewItem * currentItem;
330 QString currentHome; 338 QString currentHome;
331 if(!parent) 339 if(!parent)
332 currentItem = backupList->firstChild(); 340 currentItem = backupList->firstChild();
333 else 341 else
334 { 342 {
335 currentItem = parent->firstChild(); 343 currentItem = parent->firstChild();
336 currentHome = parent->text(BACKUP_LOCATION); 344 currentHome = parent->text(BACKUP_LOCATION);
337 } 345 }
338 346
339 uint count = 0; 347 uint count = 0;
340 while( currentItem != 0 ) 348 while( currentItem != 0 )
341 { 349 {
342 if(currentItem->text(HEADER_BACKUP) == "B" ) 350 if(currentItem->text(HEADER_BACKUP) == "B" )
343 { 351 {
344 if(currentItem->childCount() == 0 ) 352 if(currentItem->childCount() == 0 )
345 { 353 {
346 if(parent == NULL) 354 if(parent == NULL)
347 backupFiles += currentItem->text(BACKUP_LOCATION); 355 backupFiles += currentItem->text(BACKUP_LOCATION);
348 else 356 else
349 backupFiles += currentHome + currentItem->text(HEADER_NAME); 357 backupFiles += currentHome + currentItem->text(HEADER_NAME);
350 backupFiles += " "; 358 backupFiles += " ";
351 count++; 359 count++;
352 } 360 }
353 else 361 else
354 { 362 {
355 count += getBackupFiles(backupFiles, currentItem); 363 count += getBackupFiles(backupFiles, currentItem);
356 } 364 }
357 } 365 }
358 currentItem = currentItem->nextSibling(); 366 currentItem = currentItem->nextSibling();
359 } 367 }
360 return count; 368 return count;
361} 369}
362 370
363void BackupAndRestore::sourceDirChanged(int selection) 371void BackupAndRestore::sourceDirChanged(int selection)
364{ 372{
365 restoreList->clear(); 373 restoreList->clear();
366 rescanFolder(backupLocations[restoreSource->text(selection)]); 374 rescanFolder(backupLocations[restoreSource->text(selection)]);
367} 375}
368 376
369void BackupAndRestore::fileListUpdate() 377void BackupAndRestore::fileListUpdate()
370{ 378{
371 owarn << "void BackupAndRestore::fileListUpdate()" << oendl; 379 owarn << "void BackupAndRestore::fileListUpdate()" << oendl;
372 restoreList->clear(); 380 restoreList->clear();
373 rescanFolder( backupLocations[restoreSource->currentText()] ); 381 rescanFolder( backupLocations[restoreSource->currentText()] );
374} 382}
375 383
376/** 384/**
377 * Scans directory for any backup files. Will recursivly go down, 385 * Scans directory for any backup files. Will recursivly go down,
378 * but will not follow symlinks. 386 * but will not follow symlinks.
379 * @param directory - the directory to look in. 387 * @param directory - the directory to look in.
380 */ 388 */
381void BackupAndRestore::rescanFolder(QString directory) 389void BackupAndRestore::rescanFolder(QString directory)
382{ 390{
383 //odebug << QString("rescanFolder: ") + directory.latin1() << oendl; 391 //odebug << QString("rescanFolder: ") + directory.latin1() << oendl;
384 QDir d(directory); 392 QDir d(directory);
385 if(!d.exists()) 393 if(!d.exists())
386 return; 394 return;
387 395
388 d.setFilter( QDir::Files | QDir::Hidden | QDir::Dirs); 396 d.setFilter( QDir::Files | QDir::Hidden | QDir::Dirs);
389 const QFileInfoList *list = d.entryInfoList(); 397 const QFileInfoList *list = d.entryInfoList();
390 QFileInfoListIterator it( *list ); 398 QFileInfoListIterator it( *list );
391 QFileInfo *file; 399 QFileInfo *file;
392 while ( (file=it.current()) ) 400 while ( (file=it.current()) )
393 { // for each file... 401 { // for each file...
394 // If it is a dir and not .. or . then add it as a tab and go down. 402 // If it is a dir and not .. or . then add it as a tab and go down.
395 if(file->isDir()) 403 if(file->isDir())
396 { 404 {
397 if(file->fileName() != ".." && file->fileName() != ".") 405 if(file->fileName() != ".." && file->fileName() != ".")
398 { 406 {
399 rescanFolder(directory + "/" + file->fileName()); 407 rescanFolder(directory + "/" + file->fileName());
400 } 408 }
401 } 409 }
402 else 410 else
403 { 411 {
404 // If it is a backup file add to list. 412 // If it is a backup file add to list.
405 if(file->fileName().contains(EXTENSION)) 413 if(file->fileName().contains(EXTENSION))
406 (void)new QListViewItem(restoreList, file->fileName()); 414 (void)new QListViewItem(restoreList, file->fileName());
407 } 415 }
408 ++it; 416 ++it;
409 } 417 }
410} 418}
411 419
412/** 420/**
413 * Restore a backup file. 421 * Restore a backup file.
414 * Report errors or success 422 * Report errors or success
415 */ 423 */
416void BackupAndRestore::restore() 424void BackupAndRestore::restore()
417{ 425{
418 QListViewItem *restoreItem = restoreList->currentItem(); 426 QListViewItem *restoreItem = restoreList->currentItem();
419 if(!restoreItem) 427 if(!restoreItem)
420 { 428 {
421 QMessageBox::critical(this, tr( "Message" ), 429 QMessageBox::critical(this, tr( "Message" ),
422 tr( "Please select something to restore." ),QString( tr( "Ok") ) ); 430 tr( "Please select something to restore." ),QString( tr( "Ok") ) );
423 return; 431 return;
424 } 432 }
425 setCaption(tr("Backup and Restore... working...")); 433 setCaption(tr("Backup and Restore... working..."));
426 434
427 QString restoreFile = backupLocations[restoreSource->currentText()]; 435 QString restoreFile = backupLocations[restoreSource->currentText()];
428 436
429 restoreFile += "/" + restoreItem->text(0); 437 restoreFile += "/" + restoreItem->text(0);
430 438
431 odebug << restoreFile << oendl; 439 odebug << restoreFile << oendl;
432 440
433 //check if backup file come from opie 1.0.x 441 //check if backup file come from opie 1.0.x
434 442
435 QString commandLine = QString( "tar -tzf %1 | grep Applications/backup/exclude" ).arg( restoreFile.latin1() ); 443 QString commandLine = QString( "tar -tzf %1 | grep Applications/backup/exclude" ).arg( restoreFile.latin1() );
436 444
437 int r = system( commandLine ); 445 int r = system( commandLine );
438 446
439 QString startDir; 447 QString startDir;
440 448
441 if( r != 0 ) //Applications/backup/exclude not found - old backup file 449 if( r != 0 ) //Applications/backup/exclude not found - old backup file
442 { 450 {
443 startDir = QString( "/" ); 451 startDir = QString( "/" );
444 } else 452 } else
445 { 453 {
446 startDir = QDir::homeDirPath(); 454 startDir = QDir::homeDirPath();
447 } 455 }
448 456
449 //unpack backup file 457 //unpack backup file
450 commandLine = QString( "cd %1 && tar -zxf %2 2> %3" ).arg( startDir ) 458 commandLine = QString( "cd %1 && tar -zxf %2 2> %3" ).arg( startDir )
451 .arg( restoreFile.latin1() ) 459 .arg( restoreFile.latin1() )
452 .arg( tempFileName.latin1() ); 460 .arg( tempFileName.latin1() );
453 461
454 odebug << commandLine << oendl; 462 odebug << commandLine << oendl;
455 463
456 r = system( commandLine ); 464 r = system( commandLine );
457 465
458 //error handling 466 //error handling
459 if(r != 0) 467 if(r != 0)
460 { 468 {
461 QString errorMsg= tr( "Error from System:\n" ) + (QString)strerror( errno ); 469 QString errorMsg= tr( "Error from System:\n" ) + (QString)strerror( errno );
462 switch( QMessageBox::critical(this, tr( "Message" ), tr( "Restore Failed." ) + "\n" 470 switch( QMessageBox::critical(this, tr( "Message" ), tr( "Restore Failed." ) + "\n"
463 + errorMsg, QString( tr( "Ok") ), QString( tr( "Details" ) ) ) ) 471 + errorMsg, QString( tr( "Ok") ), QString( tr( "Details" ) ) ) )
464 { 472 {
465 case 1: 473 case 1:
466 owarn << "Details pressed !" << oendl; 474 owarn << "Details pressed !" << oendl;
467 ErrorDialog* pErrDialog = new ErrorDialog( this, NULL, true ); 475 ErrorDialog* pErrDialog = new ErrorDialog( this, NULL, true );
468 QFile errorFile( tempFileName ); 476 QFile errorFile( tempFileName );
469 if ( errorFile.open(IO_ReadOnly) ) 477 if ( errorFile.open(IO_ReadOnly) )
470 { 478 {
471 QTextStream t( &errorFile ); 479 QTextStream t( &errorFile );
472 QString s; 480 QString s;
473 while ( !t.eof() ) 481 while ( !t.eof() )
474 { // until end of file... 482 { // until end of file...
475 s += t.readLine(); // line of text excluding '\n' 483 s += t.readLine(); // line of text excluding '\n'
476 } 484 }
477 errorFile.close(); 485 errorFile.close();
478 486
479 pErrDialog->m_textarea->setText( s ); 487 pErrDialog->m_textarea->setText( s );
480 } 488 }
481 else 489 else
482 { 490 {
483 pErrDialog->m_textarea->setText( tr( "Unable to open File: %1" ).arg( "/tmp/backup.er" ) ); 491 pErrDialog->m_textarea->setText( tr( "Unable to open File: %1" ).arg( "/tmp/backup.er" ) );
484 } 492 }
485 QPEApplication::execDialog( pErrDialog ); 493 QPEApplication::execDialog( pErrDialog );
486 delete pErrDialog; 494 delete pErrDialog;
487 495
488 setCaption(tr("Backup and Restore.. Failed !!")); 496 setCaption(tr("Backup and Restore.. Failed !!"));
489 return; 497 return;
490 498
491 break; 499 break;
492 500
493 } 501 }
494 } 502 }
495 else 503 else
496 { 504 {
497 QMessageBox::information(this, tr( "Message" ), tr( "Restore Successful." ), QString( tr( "Ok") ) ); 505 QMessageBox::information(this, tr( "Message" ), tr( "Restore Successful." ), QString( tr( "Ok") ) );
498 } 506 }
499 507
500 //write restore-location 508 //write restore-location
501 Config config( "BackupAndRestore" ); 509 Config config( "BackupAndRestore" );
502 config.setGroup( "LastLocation" ); 510 config.setGroup( "LastLocation" );
503 config.writeEntry( "LastRestoreLocation", restoreSource->currentText() ); 511 config.writeEntry( "LastRestoreLocation", restoreSource->currentText() );
504 512
505 setCaption(tr("Backup and Restore")); 513 setCaption(tr("Backup and Restore"));
506} 514}
507 515
508/** 516/**
509 * Check for exclude in Applications/backup 517 * Check for exclude in Applications/backup
510 * If it does not exist, the function will create the file with *.bck as content 518 * If it does not exist, the function will create the file with *.bck as content
511 * The exclude_files is read by tar and will provide to exclude special files out from backup. 519 * The exclude_files is read by tar and will provide to exclude special files out from backup.
512 * e.g. alle *.bck files (backup-files) will not be backed up by default 520 * e.g. alle *.bck files (backup-files) will not be backed up by default
513 */ 521 */
514 522
515QString BackupAndRestore::getExcludeFile() 523QString BackupAndRestore::getExcludeFile()
516{ 524{
517 QString excludeFileName = Global::applicationFileName( "backup", "exclude" ); 525 QString excludeFileName = Global::applicationFileName( "backup", "exclude" );
518 if ( !QFile::exists( excludeFileName ) ) 526 if ( !QFile::exists( excludeFileName ) )
519 { 527 {
520 QFile excludeFile( excludeFileName); 528 QFile excludeFile( excludeFileName);
521 if ( excludeFile.open( IO_WriteOnly ) == true ) 529 if ( excludeFile.open( IO_WriteOnly ) == true )
522 { 530 {
523 QTextStream writeStream( &excludeFile ); 531 QTextStream writeStream( &excludeFile );
524 writeStream << "*.bck" << "\n"; 532 writeStream << "*.bck" << "\n";
525 excludeFile.close(); 533 excludeFile.close();
526 } 534 }
527 else 535 else
528 { 536 {
529 return QString::null; 537 return QString::null;
530 } 538 }
531 } 539 }
532 540
533 return excludeFileName; 541 return excludeFileName;
534} 542}
535 543
536void BackupAndRestore::refreshLocations() 544void BackupAndRestore::refreshLocations()
537{ 545{
538 locationList->clear(); 546 locationList->clear();
547
539 //todo: implement add locations 548 //todo: implement add locations
540 odebug << "not implemented yet" << oendl; 549 Config config( "BackupAndRestore" );
550 config.setGroup( "Locations" );
551
552 QStringList locations( config.readListEntry( "locations", '|' ) );
553
554 for ( QStringList::Iterator it = locations.begin(); it != locations.end(); ++it ) {
555 (void) new QListViewItem( locationList, *it );
556 }
541} 557}
542 558
543void BackupAndRestore::addLocation() 559void BackupAndRestore::addLocation()
544{ 560{
545 if ( ( !locationEdit->text().isEmpty() ) && 561 if ( ( !locationEdit->text().isEmpty() ) &&
546 ( QDir( locationEdit->text() ).exists() ) ) 562 ( QDir( locationEdit->text() ).exists() ) )
547 { 563 {
548 (void) new QListViewItem( locationList, locationEdit->text() ); 564 (void) new QListViewItem( locationList, locationEdit->text() );
549 locationEdit->setText( "" ); 565 locationEdit->setText( "" );
550 } 566 }
551} 567}
552 568
553void BackupAndRestore::removeLocation() 569void BackupAndRestore::removeLocation()
554{ 570{
555 if ( locationList->selectedItem() ) 571 if ( locationList->selectedItem() )
556 { 572 {
557 delete( locationList->selectedItem() ); 573 delete( locationList->selectedItem() );
558 } 574 }
559} 575}
560 576
561void BackupAndRestore::saveLocation() 577void BackupAndRestore::saveLocations()
562{ 578{
563 //todo: implement 579 Config config("BackupAndRestore");
564 odebug << "not implemented yet" << oendl; 580 config.setGroup("Locations");
565}
566 581
582 QStringList locations;
583 for ( QListViewItemIterator it( locationList ); it.current(); ++it )
584 {
585 locations.append( it.current()->text( 0 ) );
586 }
587 config.writeEntry( "locations", locations, '|' );
588
589 refreshBackupLocations();
590}
567 591
568// backuprestore.cpp 592// backuprestore.cpp
569 593
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 @@
1#ifndef WINDOW_H 1#ifndef WINDOW_H
2#define WINDOW_H 2#define WINDOW_H
3 3
4#include <qmainwindow.h> 4#include <qmainwindow.h>
5#include "backuprestorebase.h" 5#include "backuprestorebase.h"
6#include <qmap.h> 6#include <qmap.h>
7#include <qlist.h> 7#include <qlist.h>
8 8
9class QListViewItem; 9class QListViewItem;
10class QStringList;
10 11
11class BackupAndRestore : public BackupAndRestoreBase 12class BackupAndRestore : public BackupAndRestoreBase
12{ 13{
13 14
14 Q_OBJECT 15 Q_OBJECT
15 16
16public: 17public:
17 18
18 BackupAndRestore( QWidget* parent = 0, const char* name = 0, WFlags fl = 0); 19 BackupAndRestore( QWidget* parent = 0, const char* name = 0, WFlags fl = 0);
19 ~BackupAndRestore(); 20 ~BackupAndRestore();
20 21
21 static QString appName() { return QString::fromLatin1("backup"); } 22 static QString appName() { return QString::fromLatin1("backup"); }
22 23
23private slots: 24private slots:
24 void backup(); 25 void backup();
25 void restore(); 26 void restore();
26 void selectItem(QListViewItem *currentItem); 27 void selectItem(QListViewItem *currentItem);
27 void sourceDirChanged(int); 28 void sourceDirChanged(int);
28 void rescanFolder(QString directory); 29 void rescanFolder(QString directory);
29 void fileListUpdate(); 30 void fileListUpdate();
30 31
31 void addLocation(); 32 void addLocation();
32 void removeLocation(); 33 void removeLocation();
33 void saveLocation(); 34 void saveLocations();
34 35
35private: 36private:
36 void scanForApplicationSettings(); 37 void scanForApplicationSettings();
37 int getBackupFiles(QString &backupFiles, QListViewItem *parent); 38 int getBackupFiles(QString &backupFiles, QListViewItem *parent);
38 QString getExcludeFile(); 39 QString getExcludeFile();
39 QMap<QString, QString> backupLocations; 40 QMap<QString, QString> backupLocations;
40 QList<QListViewItem> getAllItems(QListViewItem *item, QList<QListViewItem> &list); 41 QList<QListViewItem> getAllItems(QListViewItem *item, QList<QListViewItem> &list);
41 void refreshBackupLocations(); 42 void refreshBackupLocations();
42 void refreshLocations(); 43 void refreshLocations();
43 44
44 QListViewItem *systemSettings; 45 QListViewItem *systemSettings;
45 QListViewItem *applicationSettings; 46 QListViewItem *applicationSettings;
46 QListViewItem *documents; 47 QListViewItem *documents;
47
48}; 48};
49 49
50#endif 50#endif
51 51
52// backuprestore.h 52// backuprestore.h
53 53