Skip to content

Commit

Permalink
+ add clean-up function to recovery dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Mar 21, 2016
1 parent aa83224 commit 5393196
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Gui/Application.cpp
Expand Up @@ -1837,7 +1837,7 @@ void Application::checkForPreviousCrashes()
tmp.setFilter(QDir::Dirs);
QList<QFileInfo> dirs = tmp.entryInfoList();
if (dirs.isEmpty()) {
// delete the lock file immediately if not transient directories are related
// delete the lock file immediately if no transient directories are related
tmp.remove(fn);
}
else {
Expand All @@ -1852,7 +1852,7 @@ void Application::checkForPreviousCrashes()
if (tmp.rmdir(it->filePath()))
countDeletedDocs++;
}
// search for the existance of a recovery file
// search for the existence of a recovery file
else if (doc_dir.exists(QLatin1String("fc_recovery_file.xml"))) {
// store the transient directory in case it's not empty
restoreDocFiles << *it;
Expand Down
124 changes: 124 additions & 0 deletions src/Gui/DocumentRecovery.cpp
Expand Up @@ -36,6 +36,8 @@
# include <QFile>
# include <QFileInfo>
# include <QHeaderView>
# include <QMenu>
# include <QMessageBox>
# include <QPushButton>
# include <QTextStream>
# include <QTreeWidgetItem>
Expand All @@ -58,6 +60,7 @@
#include <Gui/Document.h>

#include <QDomDocument>
#include <boost/interprocess/sync/file_lock.hpp>

using namespace Gui;
using namespace Gui::Dialog;
Expand Down Expand Up @@ -445,4 +448,125 @@ DocumentRecoveryPrivate::XmlConfig DocumentRecoveryPrivate::readXmlFile(const QS
return cfg;
}

void DocumentRecovery::contextMenuEvent(QContextMenuEvent* ev)
{
QList<QTreeWidgetItem*> items = d_ptr->ui.treeWidget->selectedItems();
if (!items.isEmpty()) {
QMenu menu;
QAction* rem = menu.addAction(tr("Delete"), this, SLOT(onDeleteSection()));
menu.exec(ev->globalPos());
}
}

void DocumentRecovery::onDeleteSection()
{
QMessageBox msgBox(this);
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle(tr("Cleanup"));
msgBox.setText(tr("Are you sure you want to delete the selected transient directories?"));
msgBox.setInformativeText(tr("When deleting the selected transient directory you won't be able to recover any files afterwards."));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
int ret = msgBox.exec();
if (ret == QMessageBox::No)
return;

QList<QTreeWidgetItem*> items = d_ptr->ui.treeWidget->selectedItems();
QDir tmp = QString::fromUtf8(App::Application::getTempPath().c_str());
for (QList<QTreeWidgetItem*>::iterator it = items.begin(); it != items.end(); ++it) {
int index = d_ptr->ui.treeWidget->indexOfTopLevelItem(*it);
QTreeWidgetItem* item = d_ptr->ui.treeWidget->takeTopLevelItem(index);

QString projectFile = item->toolTip(0);
clearDirectory(QFileInfo(tmp.filePath(projectFile)));
tmp.rmdir(projectFile);
delete item;
}

int numItems = d_ptr->ui.treeWidget->topLevelItemCount();
if (numItems == 0) {
d_ptr->ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
d_ptr->ui.buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(true);
}
}

void DocumentRecovery::on_buttonCleanup_clicked()
{
QMessageBox msgBox(this);
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle(tr("Cleanup"));
msgBox.setText(tr("Are you sure you want to delete all transient directories?"));
msgBox.setInformativeText(tr("When deleting all transient directory you won't be able to recover any files afterwards."));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
int ret = msgBox.exec();
if (ret == QMessageBox::No)
return;

d_ptr->ui.treeWidget->clear();
d_ptr->ui.buttonCleanup->setEnabled(false);
d_ptr->ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
d_ptr->ui.buttonBox->button(QDialogButtonBox::Cancel)->setEnabled(true);

QDir tmp = QString::fromUtf8(App::Application::getTempPath().c_str());
tmp.setNameFilters(QStringList() << QString::fromLatin1("*.lock"));
tmp.setFilter(QDir::Files);

QList<QFileInfo> restoreDocFiles;
QString exeName = QString::fromLatin1(App::GetApplication().getExecutableName());
QList<QFileInfo> locks = tmp.entryInfoList();
for (QList<QFileInfo>::iterator it = locks.begin(); it != locks.end(); ++it) {
QString bn = it->baseName();
// ignore the lock file for this instance
QString pid = QString::number(QCoreApplication::applicationPid());
if (bn.startsWith(exeName) && bn.indexOf(pid) < 0) {
QString fn = it->absoluteFilePath();
boost::interprocess::file_lock flock((const char*)fn.toLocal8Bit());
if (flock.try_lock()) {
// OK, this file is a leftover from a previous crash
QString crashed_pid = bn.mid(exeName.length()+1);
// search for transient directories with this PID
QString filter;
QTextStream str(&filter);
str << exeName << "_Doc_*_" << crashed_pid;
tmp.setNameFilters(QStringList() << filter);
tmp.setFilter(QDir::Dirs);
QList<QFileInfo> dirs = tmp.entryInfoList();
if (!dirs.isEmpty()) {
for (QList<QFileInfo>::iterator jt = dirs.begin(); jt != dirs.end(); ++jt) {
clearDirectory(*jt);
tmp.rmdir(jt->fileName());
}
}
tmp.remove(it->fileName());
}
}
}

QMessageBox::information(this, tr("Finished"), tr("Transient directories deleted."));
}

void DocumentRecovery::clearDirectory(const QFileInfo& dir)
{
QDir qThisDir(dir.absoluteFilePath());
if (!qThisDir.exists())
return;

// Remove all files in this directory
qThisDir.setFilter(QDir::Files);
QStringList files = qThisDir.entryList();
for (QStringList::iterator it = files.begin(); it != files.end(); ++it) {
QString file = *it;
qThisDir.remove(file);
}

// Clear this directory of any sub-directories
qThisDir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList subdirs = qThisDir.entryInfoList();
for (QFileInfoList::iterator it = subdirs.begin(); it != subdirs.end(); ++it) {
clearDirectory(*it);
qThisDir.rmdir(it->fileName());
}
}

#include "moc_DocumentRecovery.cpp"
6 changes: 6 additions & 0 deletions src/Gui/DocumentRecovery.h
Expand Up @@ -50,7 +50,13 @@ class DocumentRecovery : public QDialog

protected:
void closeEvent(QCloseEvent*);
void contextMenuEvent(QContextMenuEvent*);
QString createProjectFile(const QString&);
void clearDirectory(const QFileInfo&);

protected Q_SLOTS:
void on_buttonCleanup_clicked();
void onDeleteSection();

private:
static std::string doctools;
Expand Down
23 changes: 18 additions & 5 deletions src/Gui/DocumentRecovery.ui
Expand Up @@ -14,7 +14,7 @@
<string>Document Recovery</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<item row="0" column="0" colspan="2">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -27,7 +27,7 @@
</property>
</spacer>
</item>
<item row="1" column="0">
<item row="1" column="0" colspan="2">
<widget class="QLabel" name="label">
<property name="text">
<string>Press 'Start Recovery' to start the recovery process of the doument listed below.
Expand All @@ -36,7 +36,7 @@ The 'Status' column shows whether the document could be recovered.</string>
</property>
</widget>
</item>
<item row="2" column="0">
<item row="2" column="0" colspan="2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -49,15 +49,18 @@ The 'Status' column shows whether the document could be recovered.</string>
</property>
</spacer>
</item>
<item row="3" column="0">
<item row="3" column="0" colspan="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Status of recovered documents:</string>
</property>
</widget>
</item>
<item row="4" column="0">
<item row="4" column="0" colspan="2">
<widget class="QTreeWidget" name="treeWidget">
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<column>
<property name="text">
<string>Document Name</string>
Expand All @@ -71,6 +74,16 @@ The 'Status' column shows whether the document could be recovered.</string>
</widget>
</item>
<item row="5" column="0">
<widget class="QPushButton" name="buttonCleanup">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Cleanup...</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand Down

0 comments on commit 5393196

Please sign in to comment.