diff options
Diffstat (limited to 'utils/rbutilqt/gui/backupdialog.cpp')
-rw-r--r-- | utils/rbutilqt/gui/backupdialog.cpp | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/utils/rbutilqt/gui/backupdialog.cpp b/utils/rbutilqt/gui/backupdialog.cpp new file mode 100644 index 0000000000..f12c47b570 --- /dev/null +++ b/utils/rbutilqt/gui/backupdialog.cpp | |||
@@ -0,0 +1,152 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * Copyright (C) 2012 by Dominik Riebeling | ||
10 | * | ||
11 | * All files in this archive are subject to the GNU General Public License. | ||
12 | * See the file COPYING in the source tree root for full license agreement. | ||
13 | * | ||
14 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
15 | * KIND, either express or implied. | ||
16 | * | ||
17 | ****************************************************************************/ | ||
18 | |||
19 | #include <QThread> | ||
20 | #include <QDialog> | ||
21 | #include <QMessageBox> | ||
22 | #include <QFileDialog> | ||
23 | #include "backupdialog.h" | ||
24 | #include "ui_backupdialogfrm.h" | ||
25 | #include "rbsettings.h" | ||
26 | #include "progressloggergui.h" | ||
27 | #include "ziputil.h" | ||
28 | #include "rockboxinfo.h" | ||
29 | #include "Logger.h" | ||
30 | |||
31 | class BackupSizeThread : public QThread | ||
32 | { | ||
33 | public: | ||
34 | void run(void); | ||
35 | void setPath(QString p) { m_path = p; } | ||
36 | qint64 currentSize(void) { return m_currentSize; } | ||
37 | |||
38 | private: | ||
39 | QString m_path; | ||
40 | qint64 m_currentSize; | ||
41 | }; | ||
42 | |||
43 | |||
44 | void BackupSizeThread::run(void) | ||
45 | { | ||
46 | LOG_INFO() << "Thread started, calculating" << m_path; | ||
47 | m_currentSize = 0; | ||
48 | |||
49 | QDirIterator it(m_path, QDirIterator::Subdirectories); | ||
50 | while(it.hasNext()) { | ||
51 | m_currentSize += QFileInfo(it.next()).size(); | ||
52 | } | ||
53 | LOG_INFO() << "Thread done, sum:" << m_currentSize; | ||
54 | } | ||
55 | |||
56 | |||
57 | BackupDialog::BackupDialog(QWidget* parent) : QDialog(parent) | ||
58 | { | ||
59 | ui.setupUi(this); | ||
60 | |||
61 | m_thread = new BackupSizeThread(); | ||
62 | connect(m_thread, SIGNAL(finished()), this, SLOT(updateSizeInfo())); | ||
63 | connect(m_thread, SIGNAL(terminated()), this, SLOT(updateSizeInfo())); | ||
64 | |||
65 | connect(ui.buttonCancel, SIGNAL(clicked()), this, SLOT(close())); | ||
66 | connect(ui.buttonCancel, SIGNAL(clicked()), m_thread, SLOT(quit())); | ||
67 | connect(ui.buttonChange, SIGNAL(clicked()), this, SLOT(changeBackupPath())); | ||
68 | connect(ui.buttonBackup, SIGNAL(clicked()), this, SLOT(backup())); | ||
69 | |||
70 | ui.backupSize->setText(tr("Installation size: calculating ...")); | ||
71 | m_mountpoint = RbSettings::value(RbSettings::Mountpoint).toString(); | ||
72 | |||
73 | m_backupName = RbSettings::value(RbSettings::BackupPath).toString(); | ||
74 | if(m_backupName.isEmpty()) { | ||
75 | m_backupName = m_mountpoint; | ||
76 | } | ||
77 | RockboxInfo info(m_mountpoint); | ||
78 | m_backupName += "/.backup/rockbox-backup-" + info.version() + ".zip"; | ||
79 | ui.backupLocation->setText(QDir::toNativeSeparators(m_backupName)); | ||
80 | |||
81 | m_thread->setPath(m_mountpoint + "/.rockbox"); | ||
82 | m_thread->start(); | ||
83 | } | ||
84 | |||
85 | |||
86 | void BackupDialog::changeBackupPath(void) | ||
87 | { | ||
88 | QString backupString = QFileDialog::getSaveFileName(this, | ||
89 | tr("Select Backup Filename"), m_backupName, "*.zip"); | ||
90 | // only update if a filename was entered, ignore if cancelled | ||
91 | if(!backupString.isEmpty()) { | ||
92 | m_backupName = backupString; | ||
93 | ui.backupLocation->setText(QDir::toNativeSeparators(m_backupName)); | ||
94 | RbSettings::setValue(RbSettings::BackupPath, QFileInfo(m_backupName).absolutePath()); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | |||
99 | void BackupDialog::updateSizeInfo(void) | ||
100 | { | ||
101 | double size = m_thread->currentSize() / (1024 * 1024); | ||
102 | QString unit = "MiB"; | ||
103 | |||
104 | if(size > 1024) { | ||
105 | size /= 1024; | ||
106 | unit = "GiB"; | ||
107 | } | ||
108 | |||
109 | ui.backupSize->setText(tr("Installation size: %L1 %2").arg(size, 0, 'g', 4).arg(unit)); | ||
110 | } | ||
111 | |||
112 | |||
113 | void BackupDialog::backup(void) | ||
114 | { | ||
115 | if(QFileInfo(m_backupName).isFile()) { | ||
116 | if(QMessageBox::warning(this, tr("File exists"), | ||
117 | tr("The selected backup file already exists. Overwrite?"), | ||
118 | QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) { | ||
119 | return; | ||
120 | } | ||
121 | } | ||
122 | m_logger = new ProgressLoggerGui(this); | ||
123 | connect(m_logger, SIGNAL(closed()), this, SLOT(close())); | ||
124 | m_logger->show(); | ||
125 | m_logger->addItem(tr("Starting backup ..."),LOGINFO); | ||
126 | QCoreApplication::processEvents(); | ||
127 | |||
128 | // create dir, if it doesnt exist | ||
129 | QFileInfo backupFile(m_backupName); | ||
130 | if(!QDir(backupFile.path()).exists()) | ||
131 | { | ||
132 | QDir a; | ||
133 | a.mkpath(backupFile.path()); | ||
134 | } | ||
135 | |||
136 | // create backup | ||
137 | ZipUtil zip(this); | ||
138 | connect(&zip, SIGNAL(logProgress(int, int)), m_logger, SLOT(setProgress(int, int))); | ||
139 | connect(&zip, SIGNAL(logItem(QString, int)), m_logger, SLOT(addItem(QString, int))); | ||
140 | zip.open(m_backupName, QuaZip::mdCreate); | ||
141 | |||
142 | QString mp = m_mountpoint + "/.rockbox"; | ||
143 | if(zip.appendDirToArchive(mp, m_mountpoint)) { | ||
144 | m_logger->addItem(tr("Backup successful."), LOGINFO); | ||
145 | } | ||
146 | else { | ||
147 | m_logger->addItem(tr("Backup failed!"), LOGERROR); | ||
148 | } | ||
149 | zip.close(); | ||
150 | m_logger->setFinished(); | ||
151 | } | ||
152 | |||