Skip to content

Commit

Permalink
orphaned image deletion implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pbek committed Sep 27, 2016
1 parent 2304ce6 commit 6cb2b99
Show file tree
Hide file tree
Showing 28 changed files with 4,691 additions and 3,570 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- added RTL support for the markdown preview
(for [Issue #335](https://github.com/pbek/QOwnNotes/issues/335))
- you can turn it on in the interface settings
- you now can **delete orphaned images** in a dialog with an image preview
(for [Issue #204](https://github.com/pbek/QOwnNotes/issues/204))
- you will find a new menu entry in the edit menu to open the dialog

## 16.09.15
- you can now not only solve equations with `Ctrl + Space` but also
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ qt5_wrap_ui(dialogs/ui_welcomedialog.h welcomedialog.ui)
qt5_wrap_ui(dialogs/ui_tagadddialog.h tagadddialog.ui)
qt5_wrap_ui(widgets/ui_fontcolorwidget.h fontcolorwidget.ui)
qt5_wrap_ui(widgets/ui_evernoteimportdialog.h ui_evernoteimportdialog.ui)
qt5_wrap_ui(dialogs/ui_orphanedimagesdialog.h orphanedimagesdialog.ui)
qt5_wrap_ui(libraries/qmarkdowntextedit/ui_qtexteditsearchwidget.h
libraries/qmarkdowntextedit/qtexteditsearchwidget.ui)

Expand Down Expand Up @@ -95,6 +96,9 @@ set(SOURCE_FILES
dialogs/evernoteimportdialog.cpp
dialogs/evernoteimportdialog.h
dialogs/evernoteimportdialog.ui
dialogs/orphanedimagesdialog.cpp
dialogs/orphanedimagesdialog.h
dialogs/orphanedimagesdialog.ui
entities/calendaritem.cpp
entities/calendaritem.h
entities/note.cpp
Expand Down
9 changes: 6 additions & 3 deletions src/QOwnNotes.pro
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ SOURCES += main.cpp\
widgets/combobox.cpp \
dialogs/sharedialog.cpp \
widgets/fontcolorwidget.cpp \
dialogs/evernoteimportdialog.cpp
dialogs/evernoteimportdialog.cpp \
dialogs/orphanedimagesdialog.cpp


HEADERS += mainwindow.h \
Expand Down Expand Up @@ -140,7 +141,8 @@ HEADERS += mainwindow.h \
widgets/combobox.h \
dialogs/sharedialog.h \
widgets/fontcolorwidget.h \
dialogs/evernoteimportdialog.h
dialogs/evernoteimportdialog.h \
dialogs/orphanedimagesdialog.h

FORMS += mainwindow.ui \
dialogs/notediffdialog.ui \
Expand All @@ -157,7 +159,8 @@ FORMS += mainwindow.ui \
dialogs/logdialog.ui \
dialogs/sharedialog.ui \
widgets/fontcolorwidget.ui \
dialogs/evernoteimportdialog.ui
dialogs/evernoteimportdialog.ui \
dialogs/orphanedimagesdialog.ui

RESOURCES += \
images.qrc \
Expand Down
1 change: 1 addition & 0 deletions src/breeze-dark-qownnotes.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@
<file alias="16x16/document-import.svg">icons/breeze-dark-qownnotes/16x16/document-import.svg</file>
<file alias="16x16/document-export.svg">icons/breeze-dark-qownnotes/16x16/document-export.svg</file>
<file alias="16x16/drive-removable-media.svg">icons/breeze-dark-qownnotes/16x16/drive-removable-media.svg</file>
<file alias="16x16/archive-remove.svg">icons/breeze-dark-qownnotes/16x16/archive-remove.svg</file>
</qresource>
</RCC>
1 change: 1 addition & 0 deletions src/breeze-qownnotes.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@
<file alias="16x16/document-import.svg">icons/breeze-qownnotes/16x16/document-import.svg</file>
<file alias="16x16/document-export.svg">icons/breeze-qownnotes/16x16/document-export.svg</file>
<file alias="16x16/drive-removable-media.svg">icons/breeze-qownnotes/16x16/drive-removable-media.svg</file>
<file alias="16x16/archive-remove.svg">icons/breeze-qownnotes/16x16/archive-remove.svg</file>
</qresource>
</RCC>
162 changes: 162 additions & 0 deletions src/dialogs/orphanedimagesdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include <QDebug>
#include <QKeyEvent>
#include <entities/notefolder.h>
#include <entities/note.h>
#include <QTreeWidgetItem>
#include <QGraphicsPixmapItem>
#include <QtWidgets/QMessageBox>
#include "orphanedimagesdialog.h"
#include "ui_orphanedimagesdialog.h"

OrphanedImagesDialog::OrphanedImagesDialog(QWidget *parent) :
MasterDialog(parent),
ui(new Ui::OrphanedImagesDialog) {
ui->setupUi(this);
ui->fileTreeWidget->installEventFilter(this);

QDir mediaDir(NoteFolder::currentMediaPath());

if (!mediaDir.exists()) {
ui->progressBar->setValue(ui->progressBar->maximum());
return;
}

QStringList orphanedFiles = mediaDir.entryList(
QStringList("*"), QDir::Files, QDir::Time);
orphanedFiles.removeDuplicates();

QList<Note> noteList = Note::fetchAll();
int noteListCount = noteList.count();

ui->progressBar->setMaximum(noteListCount);
ui->progressBar->show();

Q_FOREACH(Note note, noteList) {
QString text = note.getNoteText();

// match image links like ![media-qV920](file://media/608766373.gif)
QRegularExpression re(
"!\\[.*?\\]\\(file:\\/\\/media/(.+?)\\)");
QRegularExpressionMatchIterator i = re.globalMatch(text);

// remove all found images from the orphaned files list
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString fileName = match.captured(1);
orphanedFiles.removeAll(fileName);
}

ui->progressBar->setValue(ui->progressBar->value() + 1);
}

ui->progressBar->hide();

Q_FOREACH(QString fileName, orphanedFiles) {
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0, fileName);
item->setData(0, Qt::UserRole, fileName);

QString filePath = getFilePath(item);
QFileInfo info(filePath);
item->setToolTip(0, tr("Last modified at %1").arg(
info.lastModified().toString()));

ui->fileTreeWidget->addTopLevelItem(item);
}
}

OrphanedImagesDialog::~OrphanedImagesDialog() {
delete ui;
}

/**
* Shows the currently selected image
*
* @param current
* @param previous
*/
void OrphanedImagesDialog::on_fileTreeWidget_currentItemChanged(
QTreeWidgetItem *current, QTreeWidgetItem *previous) {
Q_UNUSED(previous);

QGraphicsScene *scene = new QGraphicsScene();
QString filePath = getFilePath(current);

if (!filePath.isEmpty()) {
scene->addPixmap(QPixmap(filePath));
}

ui->graphicsView->setScene(scene);
}

/**
* Gets the file path of a tree widget item
*
* @param item
* @return
*/
QString OrphanedImagesDialog::getFilePath(QTreeWidgetItem *item) {
if (item == Q_NULLPTR) {
return "";
}

QString fileName = NoteFolder::currentMediaPath() + QDir::separator() +
item->data(0, Qt::UserRole).toString();
return fileName;
}

/**
* Deletes selected images
*/
void OrphanedImagesDialog::on_deleteButton_clicked() {
int selectedItemsCount = ui->fileTreeWidget->selectedItems().count();

if (selectedItemsCount == 0) {
return;
}

if (QMessageBox::information(
this,
tr("Delete selected files"),
tr("Delete <strong>%n</strong> selected files(s)?",
"", selectedItemsCount),
tr("&Delete"), tr("&Cancel"), QString::null,
0, 1) == 1) {
return;
}

// delete all selected files
Q_FOREACH(QTreeWidgetItem *item, ui->fileTreeWidget->selectedItems()) {
QString filePath = getFilePath(item);
bool removed = QFile::remove(filePath);

if (removed) {
delete item;
}
}
}

/**
* Event filters
*
* @param obj
* @param event
* @return
*/
bool OrphanedImagesDialog::eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

if (obj == ui->fileTreeWidget) {
// delete the currently selected images
if ((keyEvent->key() == Qt::Key_Delete) ||
(keyEvent->key() == Qt::Key_Backspace)) {
on_deleteButton_clicked();
return true;
}
return false;
}
}

return MasterDialog::eventFilter(obj, event);
}
33 changes: 33 additions & 0 deletions src/dialogs/orphanedimagesdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <QDialog>
#include <QTreeWidgetItem>
#include <QEvent>
#include "masterdialog.h"

namespace Ui {
class OrphanedImagesDialog;
}

class OrphanedImagesDialog : public MasterDialog
{
Q_OBJECT

public:
explicit OrphanedImagesDialog(QWidget *parent = 0);
~OrphanedImagesDialog();

protected:
bool eventFilter(QObject *obj, QEvent *event);

private slots:
void on_fileTreeWidget_currentItemChanged(
QTreeWidgetItem *current, QTreeWidgetItem *previous);

void on_deleteButton_clicked();

private:
Ui::OrphanedImagesDialog *ui;

QString getFilePath(QTreeWidgetItem *item);
};
Loading

0 comments on commit 6cb2b99

Please sign in to comment.