void BackupDialog::moveToAnotherFolder() { KUrl selectedURL = KDirSelectDialog::selectDirectory( /*startDir=*/Global::savesFolder(), /*localOnly=*/true, /*parent=*/0, /*caption=*/i18n("Choose a Folder Where to Move Baskets")); if (!selectedURL.isEmpty()) { QString folder = selectedURL.path(); QDir dir(folder); // The folder should not exists, or be empty (because KDirSelectDialog will likely create it anyway): if (dir.exists()) { // Get the content of the folder: QStringList content = dir.entryList(); if (content.count() > 2) { // "." and ".." int result = KMessageBox::questionYesNo( 0, "<qt>" + i18n("The folder <b>%1</b> is not empty. Do you want to override it?", folder), i18n("Override Folder?"), KGuiItem(i18n("&Override"), "document-save") ); if (result == KMessageBox::No) return; } Tools::deleteRecursively(folder); } FormatImporter copier; copier.moveFolder(Global::savesFolder(), folder); Backup::setFolderAndRestart(folder, i18n("Your baskets have been successfully moved to <b>%1</b>. %2 is going to be restarted to take this change into account.")); } }
void Archive::loadExtractedBaskets(const QString &extractionFolder, QDomNode &basketNode, QMap<QString, QString> &folderMap, Basket *parent) { bool basketSetAsCurrent = (parent != 0); QDomNode n = basketNode; while ( ! n.isNull() ) { QDomElement element = n.toElement(); if ( (!element.isNull()) && element.tagName() == "basket" ) { QString folderName = element.attribute("folderName"); if (!folderName.isEmpty()) { // Move the basket folder to its destination, while renaming it uniquely: QString newFolderName = folderMap[folderName]; FormatImporter copier; // The folder has been "reserved" by creating it. Avoid asking the user to override: QDir dir; dir.rmdir(Global::basketsFolder() + newFolderName); copier.moveFolder(extractionFolder + "baskets/" + folderName, Global::basketsFolder() + newFolderName); // Append and load the basket in the tree: Basket *basket = Global::bnpView->loadBasket(newFolderName); BasketListViewItem *basketItem = Global::bnpView->appendBasket(basket, (basket && parent ? Global::bnpView->listViewItemForBasket(parent) : 0)); basketItem->setExpanded(!XMLWork::trueOrFalse(element.attribute("folded", "false"), false)); QDomElement properties = XMLWork::getElement(element, "properties"); importBasketIcon(properties, extractionFolder); // Rename the icon fileName if necessary basket->loadProperties(properties); // Open the first basket of the archive: if (!basketSetAsCurrent) { Global::bnpView->setCurrentBasket(basket); basketSetAsCurrent = true; } QDomNode node = element.firstChild(); loadExtractedBaskets(extractionFolder, node, folderMap, basket); } } n = n.nextSibling(); } }
void BackupDialog::restore() { // Get last backup folder: KConfig *config = KGlobal::config().data(); KConfigGroup configGroup(config, "Backups"); QString folder = configGroup.readEntry("lastFolder", QDir::homePath()) + "/"; // Ask a file name to the user: QString filter = "*.tar.gz|" + i18n("Tar Archives Compressed by Gzip") + "\n*|" + i18n("All Files"); QString path = KFileDialog::getOpenFileName(folder, filter, this, i18n("Open Basket Archive")); if (path.isEmpty()) // User has canceled return; // Before replacing the basket data folder with the backup content, we safely backup the current baskets to the home folder. // So if the backup is corrupted or something goes wrong while restoring (power cut...) the user will be able to restore the old working data: QString safetyPath = Backup::newSafetyFolder(); FormatImporter copier; copier.moveFolder(Global::savesFolder(), safetyPath); // Add the README file for user to cancel a bad restoration: QString readmePath = safetyPath + i18n("README.txt"); QFile file(readmePath); if (file.open(QIODevice::WriteOnly)) { QTextStream stream(&file); stream << i18n("This is a safety copy of your baskets like they were before you started to restore the backup %1.", KUrl(path).fileName()) + "\n\n" << i18n("If the restoration was a success and you restored what you wanted to restore, you can remove this folder.") + "\n\n" << i18n("If something went wrong during the restoration process, you can re-use this folder to store your baskets and nothing will be lost.") + "\n\n" << i18n("Choose \"Basket\" -> \"Backup & Restore...\" -> \"Use Another Existing Folder...\" and select that folder.") + "\n"; file.close(); } QString message = "<p><nobr>" + i18n("Restoring <b>%1</b>. Please wait...", KUrl(path).fileName()) + "</nobr></p><p>" + i18n("If something goes wrong during the restoration process, read the file <b>%1</b>.", readmePath); KProgressDialog *dialog = new KProgressDialog(0, i18n("Restore Baskets"), message); dialog->setModal(/*modal=*/true); dialog->setAllowCancel(false); dialog->setAutoClose(true); dialog->show(); QProgressBar *progress = dialog->progressBar(); progress->setRange(0, 0/*Busy/Undefined*/); progress->setValue(0); progress->setTextVisible(false); // Uncompress: RestoreThread thread(path, Global::savesFolder()); thread.start(); while (thread.isRunning()) { progress->setValue(progress->value() + 1); // Or else, the animation is not played! kapp->processEvents(); usleep(300); // Not too long because if the restore process is finished, we wait for nothing } dialog->hide(); // The restore is finished, do not continue to show it while telling the user the application is going to be restarted delete dialog; // If we only hidden it, it reappeared just after having restored a small backup... Very strange. dialog = 0; // This was annoying since it is modal and the "BasKet Note Pads is going to be restarted" message was not reachable. //kapp->processEvents(); // Check for errors: if (!thread.success()) { // Restore the old baskets: QDir dir; dir.remove(readmePath); copier.moveFolder(safetyPath, Global::savesFolder()); // Tell the user: KMessageBox::error(0, i18n("This archive is either not a backup of baskets or is corrupted. It cannot be imported. Your old baskets have been preserved instead."), i18n("Restore Error")); return; } // Note: The safety backup is not removed now because the code can has been wrong, somehow, or the user perhapse restored an older backup by error... // The restore process will not be called very often (it is possible it will only be called once or twice arround the world during the next years). // So it is rare enough to force the user to remove the safety folder, but keep him in control and let him safely recover from restoration errors. Backup::setFolderAndRestart(Global::savesFolder()/*No change*/, i18n("Your backup has been successfuly restored to <b>%1</b>. %2 is going to be restarted to take this change into account.")); }