summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/backup/backuprestore.cpp274
-rw-r--r--noncore/settings/backup/backuprestore.h42
-rw-r--r--noncore/settings/backup/backuprestore.pro10
-rw-r--r--noncore/settings/backup/backuprestorebase.ui221
-rw-r--r--noncore/settings/backup/check.pngbin0 -> 130 bytes
-rw-r--r--noncore/settings/backup/main.cpp14
-rw-r--r--noncore/settings/backup/null.pngbin0 -> 100 bytes
7 files changed, 561 insertions, 0 deletions
diff --git a/noncore/settings/backup/backuprestore.cpp b/noncore/settings/backup/backuprestore.cpp
new file mode 100644
index 0000000..5776f3c
--- a/dev/null
+++ b/noncore/settings/backup/backuprestore.cpp
@@ -0,0 +1,274 @@
1#include "backuprestore.h"
2#include <qdir.h>
3#include <qfile.h>
4#include <qfileinfo.h>
5#include <qlistview.h>
6#include <qpushbutton.h>
7#include <qheader.h>
8#include <qpe/resource.h>
9#include <qpe/config.h>
10#include <qmessagebox.h>
11#include <qcombobox.h>
12#include <qlist.h>
13#include <stdlib.h>
14#include <qregexp.h>
15
16#define HEADER_NAME 0
17#define HEADER_BACKUP 1
18#define BACKUP_LOCATION 2
19
20#define EXTENSION ".bck"
21
22BackupAndRestore::BackupAndRestore( QWidget* parent, const char* name) : BackupAndRestoreBase(parent, name){
23 this->showMaximized();
24 backupList->header()->hide();
25 restoreList->header()->hide();
26 connect(backupButton, SIGNAL(clicked()), this, SLOT(backupPressed()));
27 connect(restoreButton, SIGNAL(clicked()), this, SLOT(restore()));
28 connect(backupList, SIGNAL(clicked( QListViewItem * )), this, SLOT(selectItem(QListViewItem*)));
29 connect(restoreSource, SIGNAL(activated( int )), this, SLOT(sourceDirChanged(int)));
30
31 systemSettings = new QListViewItem(backupList, "System Settings", "", "/home/etc");
32 selectItem(systemSettings);
33 applicationSettings = new QListViewItem(backupList, "Application Settings", "", QDir::homeDirPath() + "/Settings/");
34 selectItem(applicationSettings);
35 documents= new QListViewItem(backupList, "Documents", "", QDir::homeDirPath() + "/Documents/");
36 selectItem(documents);
37
38 scanForApplicationSettings();
39
40 Config config("BackupAndRestore");
41 config.setGroup("General");
42 int totalLocations = config.readNumEntry("totalLocations",0);
43
44 if(totalLocations == 0){
45 backupLocations.insert("Documents", "/home/root/Documents");
46 backupLocations.insert("CF", "/mnt/cf");
47 backupLocations.insert("SD", "/mnt/card");
48 }
49 else{
50 for(int i = 0; i < totalLocations; i++){
51 backupLocations.insert(config.readEntry(QString("backupLocationName_%1").arg(i)), config.readEntry(QString("backupLocation_%1").arg(i)));
52 }
53 }
54 QMap<QString, QString>::Iterator it;
55 for( it = backupLocations.begin(); it != backupLocations.end(); ++it ){
56 storeToLocation->insertItem(it.key());
57 restoreSource->insertItem(it.key());
58 }
59
60 // Read the list of items to ignore.
61 QList<QString> dontBackupList;
62 dontBackupList.setAutoDelete(true);
63 config.setGroup("DontBackup");
64 int total = config.readNumEntry("Total", 0);
65 for(int i = 0; i < total; i++){
66 dontBackupList.append(new QString(config.readEntry(QString("%1").arg(i), "")));
67 }
68
69 QList<QListViewItem> list;
70 getAllItems(backupList->firstChild(), list);
71
72 for(uint i = 0; i < list.count(); i++){
73 QString text = list.at(i)->text(HEADER_NAME);
74 for(uint i2 = 0; i2 < dontBackupList.count(); i2++){
75 if(*dontBackupList.at(i2) == text){
76 selectItem(list.at(i));
77 break;
78 }
79 }
80 }
81}
82
83BackupAndRestore::~BackupAndRestore(){
84 QList<QListViewItem> list;
85 getAllItems(backupList->firstChild(), list);
86
87 Config config("BackupAndRestore");
88 config.setGroup("DontBackup");
89 config.clearGroup();
90
91 int count = 0;
92 for(uint i = 0; i < list.count(); i++){
93 if(list.at(i)->text(HEADER_BACKUP) == ""){
94 config.writeEntry(QString("%1").arg(count), list.at(i)->text(HEADER_NAME));
95 count++;
96 }
97 }
98 config.writeEntry("Total", count);
99}
100
101QList<QListViewItem> BackupAndRestore::getAllItems(QListViewItem *item, QList<QListViewItem> &list){
102 while(item){
103 if(item->childCount() > 0)
104 getAllItems(item->firstChild(), list);
105 list.append(item);
106 item = item->nextSibling();
107 }
108 return list;
109}
110
111/**
112 * Selects and unselects the item by setting the HEADER_BACKUP to B or !.
113 * and changing the icon to match
114 * @param currentItem the item to swich the selection choice.
115 */
116void BackupAndRestore::selectItem(QListViewItem *currentItem){
117 if(!currentItem)
118 return;
119
120 if(currentItem->text(HEADER_BACKUP) == "B"){
121 currentItem->setPixmap(HEADER_NAME, Resource::loadPixmap("null"));
122 currentItem->setText(HEADER_BACKUP, "");
123 }
124 else{
125 currentItem->setPixmap(HEADER_NAME, Resource::loadPixmap("check"));
126 currentItem->setText(HEADER_BACKUP, "B");
127 }
128}
129
130void BackupAndRestore::scanForApplicationSettings(){
131 QDir d(applicationSettings->text(BACKUP_LOCATION));
132 d.setFilter( QDir::Files | QDir::NoSymLinks );
133 const QFileInfoList *list = d.entryInfoList();
134 QFileInfoListIterator it( *list );
135 QFileInfo *fi;
136 while ( (fi=it.current()) ) {
137 //qDebug((d.path()+fi->fileName()).latin1());
138 QListViewItem *newItem = new QListViewItem(applicationSettings, fi->fileName());
139 selectItem(newItem);
140 ++it;
141 }
142}
143
144/**
145 * The "Backup" button has been pressed. Get a list of all of the files that
146 * should be backed up. If there are no files, emit and error and exit.
147 * Determine the file name to store the backup in. Backup the file(s) using
148 * tar and gzip --best. Report failure or success
149 */
150void BackupAndRestore::backupPressed(){
151 QString backupFiles;
152 if(getBackupFiles(backupFiles, NULL) == 0){
153 QMessageBox::critical(this, "Message", "No items selected.",QString("Ok") );
154 return;
155 }
156
157 QString outputFile = backupLocations[storeToLocation->currentText()];
158
159 QDateTime time = QDateTime::currentDateTime();
160 QString dateString = time.date().toString().replace(QRegExp(" "), "");
161 outputFile += "/" + dateString;
162
163 QString t = outputFile;
164 int c = 1;
165 while(QFile::exists(outputFile + EXTENSION)){
166 outputFile = t + QString("%1").arg(c);
167 c++;
168 }
169 outputFile += EXTENSION;
170
171 int r = system(QString("tar -c %1 | gzip --best > %2").arg(backupFiles).arg(outputFile).latin1());
172 if(r != 0){
173 QMessageBox::critical(this, "Message", "Backup Failed.",QString("Ok") );
174 return;
175 }
176 else{
177 QMessageBox::critical(this, "Message", "Backup Successfull.",QString("Ok") );
178 }
179}
180
181/***
182 * Get a list of all of the files to backup.
183 */
184int BackupAndRestore::getBackupFiles(QString &backupFiles, QListViewItem *parent){
185 QListViewItem * currentItem;
186 QString currentHome;
187 if(!parent)
188 currentItem = backupList->firstChild();
189 else{
190 currentItem = parent->firstChild();
191 currentHome = parent->text(BACKUP_LOCATION);
192 }
193
194 uint count = 0;
195 while( currentItem != 0 ){
196 if(currentItem->text(HEADER_BACKUP) == "B" ){
197 if(currentItem->childCount() == 0 ){
198 if(parent == NULL)
199 backupFiles += currentItem->text(BACKUP_LOCATION);
200 else
201 backupFiles += currentHome + currentItem->text(HEADER_NAME);
202 backupFiles += " ";
203 count++;
204 }
205 else{
206 count += getBackupFiles(backupFiles, currentItem);
207 }
208 }
209 currentItem = currentItem->nextSibling();
210 }
211 return count;
212}
213
214void BackupAndRestore::sourceDirChanged(int selection){
215 restoreList->clear();
216 rescanFolder(backupLocations[restoreSource->text(selection)]);
217}
218
219/**
220 * Scans directory for any backup files. Will recursivly go down,
221 * but will not follow symlinks.
222 * @param directory - the directory to look in.
223 */
224void BackupAndRestore::rescanFolder(QString directory){
225 //qDebug(QString("rescanFolder: ") + directory.latin1());
226 QDir d(directory);
227 if(!d.exists())
228 return;
229
230 d.setFilter( QDir::Files | QDir::Hidden | QDir::Dirs);
231 const QFileInfoList *list = d.entryInfoList();
232 QFileInfoListIterator it( *list );
233 QFileInfo *file;
234 while ( (file=it.current()) ) { // for each file...
235 // If it is a dir and not .. or . then add it as a tab and go down.
236 if(file->isDir()){
237 if(file->fileName() != ".." && file->fileName() != ".") {
238 rescanFolder(directory + "/" + file->fileName());
239 }
240 }
241 else{
242 // If it is a backup file add to list.
243 if(file->fileName().contains(EXTENSION))
244 (void)new QListViewItem(restoreList, file->fileName());
245 }
246 ++it;
247 }
248}
249
250/**
251 * Restore a backup file.
252 * Report errors or success
253 */
254void BackupAndRestore::restore(){
255 QListViewItem *restoreItem = restoreList->currentItem();
256 if(!restoreItem){
257 QMessageBox::critical(this, "Message", "Please select something to restore.",QString("Ok") );
258 return;
259 }
260 QString restoreFile = backupLocations[restoreSource->currentText()];
261
262 restoreFile += "/" + restoreItem->text(0);
263
264 int r = system(QString("tar -C / -zxf %1").arg(restoreFile).latin1());
265 if(r != 0){
266 QMessageBox::critical(this, "Message", "Restore Failed.",QString("Ok") );
267 }
268 else{
269 QMessageBox::critical(this, "Message", "Restore Successfull.",QString("Ok") );
270 }
271}
272
273// backuprestore.cpp
274
diff --git a/noncore/settings/backup/backuprestore.h b/noncore/settings/backup/backuprestore.h
new file mode 100644
index 0000000..b0cf4cd
--- a/dev/null
+++ b/noncore/settings/backup/backuprestore.h
@@ -0,0 +1,42 @@
1#ifndef WINDOW_H
2#define WINDOW_H
3
4#include <qmainwindow.h>
5#include "backuprestorebase.h"
6#include <qmap.h>
7#include <qlist.h>
8
9class QListViewItem;
10
11class BackupAndRestore : public BackupAndRestoreBase {
12
13Q_OBJECT
14
15public:
16 BackupAndRestore( QWidget* parent = 0, const char* name = 0);
17 ~BackupAndRestore();
18
19
20private slots:
21 void backupPressed();
22 void restore();
23 void selectItem(QListViewItem *currentItem);
24 void sourceDirChanged(int);
25 void rescanFolder(QString directory);
26
27private:
28 void scanForApplicationSettings();
29 int getBackupFiles(QString &backupFiles, QListViewItem *parent);
30 QMap<QString, QString> backupLocations;
31 QList<QListViewItem> getAllItems(QListViewItem *item, QList<QListViewItem> &list);
32
33 QListViewItem *systemSettings;
34 QListViewItem *applicationSettings;
35 QListViewItem *documents;
36
37};
38
39#endif
40
41// backuprestore.h
42
diff --git a/noncore/settings/backup/backuprestore.pro b/noncore/settings/backup/backuprestore.pro
new file mode 100644
index 0000000..9498df3
--- a/dev/null
+++ b/noncore/settings/backup/backuprestore.pro
@@ -0,0 +1,10 @@
1 TEMPLATE= app
2 #CONFIG = qt warn_on debug
3 CONFIG = qt warn_on release
4 HEADERS = backuprestore.h
5 SOURCES = main.cpp backuprestore.cpp
6 INCLUDEPATH+= $(QPEDIR)/include
7 DEPENDPATH+= $(QPEDIR)/include
8LIBS += -lqpe
9 INTERFACES= backuprestorebase.ui
10 TARGET = backupandrestore
diff --git a/noncore/settings/backup/backuprestorebase.ui b/noncore/settings/backup/backuprestorebase.ui
new file mode 100644
index 0000000..92e37ee
--- a/dev/null
+++ b/noncore/settings/backup/backuprestorebase.ui
@@ -0,0 +1,221 @@
1<!DOCTYPE UI><UI>
2<class>BackupAndRestoreBase</class>
3<widget>
4 <class>QWidget</class>
5 <property stdset="1">
6 <name>name</name>
7 <cstring>BackupAndRestoreBase</cstring>
8 </property>
9 <property stdset="1">
10 <name>geometry</name>
11 <rect>
12 <x>0</x>
13 <y>0</y>
14 <width>234</width>
15 <height>216</height>
16 </rect>
17 </property>
18 <property stdset="1">
19 <name>caption</name>
20 <string>Backup And Restore</string>
21 </property>
22 <property>
23 <name>layoutMargin</name>
24 </property>
25 <vbox>
26 <property stdset="1">
27 <name>margin</name>
28 <number>0</number>
29 </property>
30 <property stdset="1">
31 <name>spacing</name>
32 <number>6</number>
33 </property>
34 <widget>
35 <class>QTabWidget</class>
36 <property stdset="1">
37 <name>name</name>
38 <cstring>tabWidget</cstring>
39 </property>
40 <widget>
41 <class>QWidget</class>
42 <property stdset="1">
43 <name>name</name>
44 <cstring>Widget2</cstring>
45 </property>
46 <attribute>
47 <name>title</name>
48 <string>Backup</string>
49 </attribute>
50 <grid>
51 <property stdset="1">
52 <name>margin</name>
53 <number>11</number>
54 </property>
55 <property stdset="1">
56 <name>spacing</name>
57 <number>6</number>
58 </property>
59 <widget row="1" column="0" rowspan="1" colspan="3" >
60 <class>QListView</class>
61 <column>
62 <property>
63 <name>text</name>
64 <string>Applications</string>
65 </property>
66 <property>
67 <name>clickable</name>
68 <bool>true</bool>
69 </property>
70 <property>
71 <name>resizeable</name>
72 <bool>true</bool>
73 </property>
74 </column>
75 <property stdset="1">
76 <name>name</name>
77 <cstring>backupList</cstring>
78 </property>
79 <property stdset="1">
80 <name>allColumnsShowFocus</name>
81 <bool>true</bool>
82 </property>
83 <property stdset="1">
84 <name>rootIsDecorated</name>
85 <bool>true</bool>
86 </property>
87 </widget>
88 <widget row="0" column="1" >
89 <class>QComboBox</class>
90 <property stdset="1">
91 <name>name</name>
92 <cstring>storeToLocation</cstring>
93 </property>
94 </widget>
95 <widget row="0" column="0" >
96 <class>QLabel</class>
97 <property stdset="1">
98 <name>name</name>
99 <cstring>TextLabel1</cstring>
100 </property>
101 <property stdset="1">
102 <name>text</name>
103 <string>Save To</string>
104 </property>
105 </widget>
106 <spacer row="0" column="2" >
107 <property>
108 <name>name</name>
109 <cstring>Spacer1</cstring>
110 </property>
111 <property stdset="1">
112 <name>orientation</name>
113 <enum>Horizontal</enum>
114 </property>
115 <property stdset="1">
116 <name>sizeType</name>
117 <enum>Expanding</enum>
118 </property>
119 <property>
120 <name>sizeHint</name>
121 <size>
122 <width>20</width>
123 <height>20</height>
124 </size>
125 </property>
126 </spacer>
127 <widget row="2" column="0" rowspan="1" colspan="3" >
128 <class>QPushButton</class>
129 <property stdset="1">
130 <name>name</name>
131 <cstring>backupButton</cstring>
132 </property>
133 <property stdset="1">
134 <name>text</name>
135 <string>&amp;Backup</string>
136 </property>
137 </widget>
138 </grid>
139 </widget>
140 <widget>
141 <class>QWidget</class>
142 <property stdset="1">
143 <name>name</name>
144 <cstring>Widget3</cstring>
145 </property>
146 <attribute>
147 <name>title</name>
148 <string>Restore</string>
149 </attribute>
150 <grid>
151 <property stdset="1">
152 <name>margin</name>
153 <number>11</number>
154 </property>
155 <property stdset="1">
156 <name>spacing</name>
157 <number>6</number>
158 </property>
159 <widget row="0" column="1" >
160 <class>QComboBox</class>
161 <property stdset="1">
162 <name>name</name>
163 <cstring>restoreSource</cstring>
164 </property>
165 </widget>
166 <widget row="0" column="0" >
167 <class>QLabel</class>
168 <property stdset="1">
169 <name>name</name>
170 <cstring>TextLabel1_2</cstring>
171 </property>
172 <property stdset="1">
173 <name>text</name>
174 <string>Select Source</string>
175 </property>
176 </widget>
177 <widget row="1" column="0" rowspan="1" colspan="2" >
178 <class>QListView</class>
179 <column>
180 <property>
181 <name>text</name>
182 <string>Column 1</string>
183 </property>
184 <property>
185 <name>clickable</name>
186 <bool>true</bool>
187 </property>
188 <property>
189 <name>resizeable</name>
190 <bool>true</bool>
191 </property>
192 </column>
193 <property stdset="1">
194 <name>name</name>
195 <cstring>restoreList</cstring>
196 </property>
197 <property stdset="1">
198 <name>minimumSize</name>
199 <size>
200 <width>0</width>
201 <height>100</height>
202 </size>
203 </property>
204 </widget>
205 <widget row="2" column="0" rowspan="1" colspan="2" >
206 <class>QPushButton</class>
207 <property stdset="1">
208 <name>name</name>
209 <cstring>restoreButton</cstring>
210 </property>
211 <property stdset="1">
212 <name>text</name>
213 <string>&amp;Restore</string>
214 </property>
215 </widget>
216 </grid>
217 </widget>
218 </widget>
219 </vbox>
220</widget>
221</UI>
diff --git a/noncore/settings/backup/check.png b/noncore/settings/backup/check.png
new file mode 100644
index 0000000..2890a7a
--- a/dev/null
+++ b/noncore/settings/backup/check.png
Binary files differ
diff --git a/noncore/settings/backup/main.cpp b/noncore/settings/backup/main.cpp
new file mode 100644
index 0000000..7e36741
--- a/dev/null
+++ b/noncore/settings/backup/main.cpp
@@ -0,0 +1,14 @@
1#include "backuprestore.h"
2#include "qnetworkprotocol.h"
3#include <qpe/qpeapplication.h>
4
5int main(int argc, char *argv[]) {
6 QPEApplication a( argc, argv );
7
8 BackupAndRestore app(0, "mainwindow");
9 app.show();
10 return a.exec();
11}
12
13// main.cpp
14
diff --git a/noncore/settings/backup/null.png b/noncore/settings/backup/null.png
new file mode 100644
index 0000000..b67dcc6
--- a/dev/null
+++ b/noncore/settings/backup/null.png
Binary files differ