2015-09-23 15:08:46 +02:00
|
|
|
#include "trashdialog.h"
|
2020-02-13 17:35:54 +01:00
|
|
|
|
2020-07-28 18:05:31 +02:00
|
|
|
#include <services/owncloudservice.h>
|
2015-09-23 15:08:46 +02:00
|
|
|
#include <utils/gui.h>
|
|
|
|
#include <utils/misc.h>
|
2020-02-13 17:35:54 +01:00
|
|
|
|
2015-09-23 15:08:46 +02:00
|
|
|
#include <QDebug>
|
2016-05-04 17:12:36 +02:00
|
|
|
#include <QJSValue>
|
|
|
|
#include <QJSValueIterator>
|
2019-07-27 17:05:14 +02:00
|
|
|
#include <QPushButton>
|
2019-10-04 10:22:35 +02:00
|
|
|
#include <QSettings>
|
2020-01-15 17:01:11 +05:00
|
|
|
#include <QSplitter>
|
2020-02-13 17:35:54 +01:00
|
|
|
|
2020-01-15 17:01:11 +05:00
|
|
|
#include "mainwindow.h"
|
2015-09-23 15:08:46 +02:00
|
|
|
#include "ui_trashdialog.h"
|
|
|
|
|
2022-10-27 14:02:02 +05:00
|
|
|
TrashDialog::TrashDialog(const QJSValue ¬es, QWidget *parent)
|
2016-02-06 13:55:32 +01:00
|
|
|
: MasterDialog(parent), ui(new Ui::TrashDialog) {
|
2015-09-23 15:08:46 +02:00
|
|
|
ui->setupUi(this);
|
2016-01-31 19:53:52 +01:00
|
|
|
|
2016-05-26 18:22:07 +02:00
|
|
|
// init the note browser search frame
|
|
|
|
ui->noteBrowser->initSearchFrame(ui->noteBrowserSearchFrame);
|
|
|
|
|
2015-09-23 15:08:46 +02:00
|
|
|
setupMainSplitter();
|
|
|
|
|
|
|
|
QPushButton *button;
|
|
|
|
ui->buttonBox->clear();
|
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
button = new QPushButton(tr("&Restore selected note on server"));
|
2023-01-26 16:58:47 +05:00
|
|
|
button->setToolTip(
|
|
|
|
Utils::Misc::replaceOwnCloudText(tr("<h3>Slower, but with note versions</h3>"
|
|
|
|
"<p>The note will be restored on your ownCloud "
|
|
|
|
"server with all versions.</p>"
|
|
|
|
"<p>You will have to wait until it is synced to "
|
|
|
|
"QOwnNotes by ownCloud sync.</p>")));
|
2016-02-06 13:55:32 +01:00
|
|
|
button->setProperty("ActionRole", RestoreOnServer);
|
|
|
|
button->setDefault(false);
|
2023-01-26 16:58:47 +05:00
|
|
|
button->setIcon(QIcon::fromTheme(QStringLiteral("view-restore"),
|
|
|
|
QIcon(":/icons/breeze-qownnotes/16x16/view-restore.svg")));
|
2016-02-06 13:55:32 +01:00
|
|
|
ui->buttonBox->addButton(button, QDialogButtonBox::ActionRole);
|
|
|
|
|
|
|
|
button = new QPushButton(tr("&Download selected note"));
|
2019-07-27 17:05:14 +02:00
|
|
|
button->setToolTip(Utils::Misc::replaceOwnCloudText(
|
|
|
|
tr("<h3>Faster, but without versions</h3>"
|
2016-02-06 13:55:32 +01:00
|
|
|
"<p>The note will be created with the text from the preview.</p>"
|
|
|
|
"<p>The note versions on your ownCloud server will not be restored "
|
|
|
|
"and the note will remain in the trash.</p>"
|
2019-07-27 17:05:14 +02:00
|
|
|
"<p>You can always restore the note and its versions later.</p>")));
|
2016-02-06 13:55:32 +01:00
|
|
|
button->setProperty("ActionRole", Download);
|
|
|
|
button->setDefault(false);
|
2023-01-26 16:58:47 +05:00
|
|
|
button->setIcon(QIcon::fromTheme(QStringLiteral("edit-download"),
|
|
|
|
QIcon(":/icons/breeze-qownnotes/16x16/edit-download.svg")));
|
2016-02-06 13:55:32 +01:00
|
|
|
ui->buttonBox->addButton(button, QDialogButtonBox::ActionRole);
|
|
|
|
|
2020-07-28 18:05:31 +02:00
|
|
|
button = new QPushButton(tr("&Delete"));
|
|
|
|
button->setToolTip(tr("Delete selected note on server"));
|
|
|
|
button->setProperty("ActionRole", DeleteOnServer);
|
|
|
|
button->setDefault(false);
|
2023-01-26 16:58:47 +05:00
|
|
|
button->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete"),
|
|
|
|
QIcon(":/icons/breeze-qownnotes/16x16/edit-delete.svg")));
|
2020-07-28 18:05:31 +02:00
|
|
|
ui->buttonBox->addButton(button, QDialogButtonBox::ActionRole);
|
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
button = new QPushButton(tr("&Cancel"));
|
|
|
|
button->setProperty("ActionRole", Cancel);
|
2023-01-26 16:58:47 +05:00
|
|
|
button->setIcon(QIcon::fromTheme(QStringLiteral("dialog-cancel"),
|
|
|
|
QIcon(":/icons/breeze-qownnotes/16x16/dialog-cancel.svg")));
|
2016-02-06 13:55:32 +01:00
|
|
|
button->setDefault(true);
|
|
|
|
ui->buttonBox->addButton(button, QDialogButtonBox::ActionRole);
|
|
|
|
|
|
|
|
connect(this->ui->buttonBox, SIGNAL(clicked(QAbstractButton *)),
|
|
|
|
SLOT(dialogButtonClicked(QAbstractButton *)));
|
|
|
|
connect(this, SIGNAL(finished(int)), this, SLOT(storeSettings()));
|
2015-09-23 15:08:46 +02:00
|
|
|
|
|
|
|
QString itemName;
|
|
|
|
QString dateString;
|
|
|
|
QString data;
|
2015-09-30 18:24:03 +02:00
|
|
|
int timestamp;
|
2015-09-23 15:08:46 +02:00
|
|
|
ui->trashListWidget->clear();
|
|
|
|
dataList = new QStringList();
|
2015-09-30 18:24:03 +02:00
|
|
|
timestampList = new QList<int>;
|
2015-09-23 15:08:46 +02:00
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
// init the iterator for the versions
|
2016-05-04 17:12:36 +02:00
|
|
|
QJSValueIterator notesIterator(notes);
|
2015-09-23 15:08:46 +02:00
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
// iterate over the trashed notes
|
|
|
|
while (notesIterator.hasNext()) {
|
2015-09-23 15:08:46 +02:00
|
|
|
notesIterator.next();
|
|
|
|
|
2023-01-26 16:58:47 +05:00
|
|
|
QJSValue property = notesIterator.value().property(QStringLiteral("noteName"));
|
2016-05-11 12:48:53 +02:00
|
|
|
|
|
|
|
if (property.isUndefined()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemName = property.toString();
|
|
|
|
|
2019-12-13 13:07:18 +05:00
|
|
|
if (itemName.isEmpty()) {
|
2016-05-11 12:48:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-26 16:58:47 +05:00
|
|
|
dateString = notesIterator.value().property(QStringLiteral("dateString")).toString();
|
|
|
|
data = notesIterator.value().property(QStringLiteral("data")).toString();
|
|
|
|
timestamp = notesIterator.value().property(QStringLiteral("timestamp")).toInt();
|
|
|
|
QString fileName = notesIterator.value().property(QStringLiteral("fileName")).toString();
|
2015-09-23 15:08:46 +02:00
|
|
|
|
2019-05-30 09:32:28 +02:00
|
|
|
auto *item = new QListWidgetItem();
|
2016-02-06 13:55:32 +01:00
|
|
|
item->setText(itemName);
|
2016-03-28 18:29:24 +02:00
|
|
|
item->setData(Qt::UserRole, fileName);
|
2016-02-06 13:55:32 +01:00
|
|
|
item->setToolTip(dateString);
|
|
|
|
ui->trashListWidget->addItem(item);
|
|
|
|
dataList->append(data);
|
|
|
|
timestampList->append(timestamp);
|
2015-09-23 15:08:46 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
ui->trashListWidget->setCurrentRow(0);
|
2016-05-27 19:50:24 +02:00
|
|
|
if (dataList->count() > 0) {
|
2017-09-06 16:24:18 +02:00
|
|
|
ui->noteBrowser->setPlainText(dataList->at(0));
|
2016-05-27 19:50:24 +02:00
|
|
|
}
|
2015-09-23 15:08:46 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
void TrashDialog::setupMainSplitter() {
|
2016-05-26 18:22:07 +02:00
|
|
|
trashSplitter = new QSplitter;
|
2015-09-23 15:08:46 +02:00
|
|
|
|
2019-10-04 10:22:35 +02:00
|
|
|
trashSplitter->addWidget(ui->listFrame);
|
2016-05-26 18:22:07 +02:00
|
|
|
trashSplitter->addWidget(ui->noteBrowserFrame);
|
2015-09-23 15:08:46 +02:00
|
|
|
|
|
|
|
// restore splitter sizes
|
|
|
|
QSettings settings;
|
2023-01-26 16:58:47 +05:00
|
|
|
QByteArray state = settings.value(QStringLiteral("trashSplitterSizes")).toByteArray();
|
2016-05-26 18:22:07 +02:00
|
|
|
trashSplitter->restoreState(state);
|
2015-09-23 15:08:46 +02:00
|
|
|
|
2016-05-26 18:22:07 +02:00
|
|
|
ui->gridLayout->layout()->addWidget(trashSplitter);
|
|
|
|
ui->gridLayout->layout()->addWidget(ui->buttonBox);
|
2015-09-23 15:08:46 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
void TrashDialog::storeSettings() {
|
2015-09-23 15:08:46 +02:00
|
|
|
// store the splitter sizes
|
|
|
|
QSettings settings;
|
2023-01-26 16:58:47 +05:00
|
|
|
settings.setValue(QStringLiteral("trashSplitterSizes"), trashSplitter->saveState());
|
2015-09-23 15:08:46 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
TrashDialog::~TrashDialog() { delete ui; }
|
2015-09-23 15:08:46 +02:00
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
void TrashDialog::on_trashListWidget_currentRowChanged(int currentRow) {
|
2017-09-06 16:24:18 +02:00
|
|
|
ui->noteBrowser->setPlainText(dataList->value(currentRow));
|
2015-09-23 15:08:46 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 13:55:32 +01:00
|
|
|
void TrashDialog::dialogButtonClicked(QAbstractButton *button) {
|
2022-02-06 19:13:51 +05:00
|
|
|
if (ui->trashListWidget->currentItem() != nullptr) {
|
2016-05-27 19:50:24 +02:00
|
|
|
int actionRole = button->property("ActionRole").toInt();
|
|
|
|
|
|
|
|
QString name = ui->trashListWidget->currentItem()->text();
|
2023-01-26 16:58:47 +05:00
|
|
|
QString fileName = ui->trashListWidget->currentItem()->data(Qt::UserRole).toString();
|
2016-05-27 19:50:24 +02:00
|
|
|
|
|
|
|
switch (actionRole) {
|
|
|
|
case Download: {
|
2023-01-26 16:58:47 +05:00
|
|
|
QString text = dataList->value(ui->trashListWidget->currentRow());
|
2022-10-27 14:02:02 +05:00
|
|
|
MainWindow::instance()->createNewNote(name, text);
|
2016-05-27 19:50:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RestoreOnServer: {
|
2023-01-26 16:58:47 +05:00
|
|
|
int timestamp = this->timestampList->value(ui->trashListWidget->currentRow());
|
2016-05-27 19:50:24 +02:00
|
|
|
qDebug() << name << timestamp;
|
|
|
|
|
2022-10-27 14:02:02 +05:00
|
|
|
MainWindow::instance()->restoreTrashedNoteOnServer(fileName, timestamp);
|
2016-05-27 19:50:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:05:31 +02:00
|
|
|
case DeleteOnServer: {
|
2023-01-26 16:58:47 +05:00
|
|
|
if (Utils::Gui::question(this, tr("Delete note on server"),
|
|
|
|
tr("Delete selected trashed note on server?"),
|
|
|
|
"trashdialog-delete", QMessageBox::Yes | QMessageBox::No,
|
|
|
|
QMessageBox::No) == QMessageBox::No) {
|
2020-07-28 18:05:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-26 16:58:47 +05:00
|
|
|
const int timestamp = this->timestampList->value(ui->trashListWidget->currentRow());
|
2020-07-28 18:05:31 +02:00
|
|
|
auto currentItem = ui->trashListWidget->currentItem();
|
|
|
|
OwnCloudService *ownCloud = OwnCloudService::instance();
|
|
|
|
ui->trashListWidget->setDisabled(true);
|
|
|
|
ui->buttonBox->setDisabled(true);
|
|
|
|
|
|
|
|
// delete trashed note on server
|
2023-01-26 16:58:47 +05:00
|
|
|
const int statusCode = ownCloud->deleteTrashedNoteOnServer(fileName, timestamp);
|
2020-07-28 18:05:31 +02:00
|
|
|
|
|
|
|
if (statusCode >= 200 && statusCode < 300) {
|
|
|
|
delete currentItem;
|
|
|
|
} else {
|
2023-01-26 16:58:47 +05:00
|
|
|
Utils::Gui::warning(this, tr("Error while deleting note"),
|
|
|
|
tr("Deleting trashed note failed with status code: %1")
|
|
|
|
.arg(QString::number(statusCode)),
|
|
|
|
"trashdialog-delete-failed");
|
2020-07-28 18:05:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ui->trashListWidget->setDisabled(false);
|
|
|
|
ui->buttonBox->setDisabled(false);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-27 19:50:24 +02:00
|
|
|
default:
|
|
|
|
break;
|
2015-09-30 18:24:03 +02:00
|
|
|
}
|
2015-09-23 15:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->close();
|
|
|
|
}
|
2019-10-04 10:22:35 +02:00
|
|
|
|
|
|
|
void TrashDialog::on_searchLineEdit_textChanged(const QString &arg1) {
|
|
|
|
Utils::Gui::searchForTextInListWidget(ui->trashListWidget, arg1);
|
|
|
|
}
|