Skip to content

Commit 4e37fdd

Browse files
committed
#3357 nextclouddeckdialog: allow selection of notes from list
Signed-off-by: Patrizio Bekerle <[email protected]>
1 parent 7f3779c commit 4e37fdd

File tree

4 files changed

+119
-27
lines changed

4 files changed

+119
-27
lines changed

src/dialogs/nextclouddeckdialog.cpp

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <QTimeZone>
66

77
#include "mainwindow.h"
8+
#include "services/metricsservice.h"
89
#include "services/nextclouddeckservice.h"
910
#include "services/settingsservice.h"
1011
#include "ui_nextclouddeckdialog.h"
@@ -14,6 +15,7 @@ NextcloudDeckDialog::NextcloudDeckDialog(QWidget *parent)
1415
ui->setupUi(this);
1516
afterSetupUI();
1617
setupUi();
18+
_currentCard = NextcloudDeckService::Card();
1719

1820
ui->dueDateTimeEdit->setDateTime(QDateTime::currentDateTime());
1921
ui->saveButton->setEnabled(false);
@@ -91,9 +93,13 @@ void NextcloudDeckDialog::on_saveButton_clicked() {
9193
// We want to set the seconds to 0
9294
dateTime->setTime(dateTime->time().addSecs(0 - dateTime->time().second()));
9395
const QString &title = ui->titleLineEdit->text();
94-
int cardId =
95-
nextcloudDeckService.createCard(title, ui->descriptionTextEdit->toPlainText(),
96-
ui->dueDateTimeCheckBox->isChecked() ? dateTime : nullptr);
96+
97+
// Check if we're updating an existing card or creating a new one
98+
int cardIdToUpdate = (_currentCard.id > 0) ? _currentCard.id : -1;
99+
100+
int cardId = nextcloudDeckService.storeCard(
101+
title, ui->descriptionTextEdit->toPlainText(),
102+
ui->dueDateTimeCheckBox->isChecked() ? dateTime : nullptr, cardIdToUpdate);
97103

98104
if (cardId > 0) {
99105
auto linkText =
@@ -105,9 +111,14 @@ void NextcloudDeckDialog::on_saveButton_clicked() {
105111
mainWindow->activeNoteTextEdit()->insertPlainText(linkText);
106112
}
107113
#endif
114+
115+
// Reload the card list to reflect the changes
116+
reloadCardList();
108117
}
109118

110-
close();
119+
if (cardIdToUpdate == -1) {
120+
close();
121+
}
111122
}
112123

113124
void NextcloudDeckDialog::on_add1HourButton_clicked() {
@@ -163,15 +174,15 @@ void NextcloudDeckDialog::refreshUi() { reloadCardList(); }
163174

164175
void NextcloudDeckDialog::reloadCardList() {
165176
NextcloudDeckService nextcloudDeckService(this);
166-
auto cards = nextcloudDeckService.getCards();
177+
_cards = nextcloudDeckService.getCards();
167178

168-
qDebug() << __func__ << " - 'cards': " << cards;
179+
qDebug() << __func__ << " - 'cards': " << _cards;
169180

170181
// Clear existing items
171182
ui->cardItemTreeWidget->clear();
172183

173184
// Populate the tree widget with cards
174-
for (const auto &card : cards) {
185+
for (const auto &card : _cards) {
175186
auto *item = new QTreeWidgetItem(ui->cardItemTreeWidget);
176187

177188
// Set the summary (title) in the first column
@@ -197,3 +208,49 @@ void NextcloudDeckDialog::reloadCardList() {
197208
ui->cardItemTreeWidget->resizeColumnToContents(0);
198209
ui->cardItemTreeWidget->resizeColumnToContents(1);
199210
}
211+
212+
void NextcloudDeckDialog::resetEditFrameControls() {
213+
ui->editFrame->setEnabled(false);
214+
ui->titleLineEdit->setText(QString());
215+
ui->descriptionTextEdit->setPlainText(QString());
216+
ui->dueDateTimeCheckBox->setChecked(false);
217+
ui->dueDateTimeEdit->hide();
218+
ui->saveButton->setEnabled(false);
219+
_currentCard = NextcloudDeckService::Card();
220+
}
221+
222+
void NextcloudDeckDialog::on_cardItemTreeWidget_currentItemChanged(QTreeWidgetItem *current,
223+
QTreeWidgetItem *previous) {
224+
Q_UNUSED(previous)
225+
226+
// in case all items were removed
227+
if (current == nullptr) {
228+
resetEditFrameControls();
229+
return;
230+
}
231+
232+
MetricsService::instance()->sendVisitIfEnabled(QStringLiteral("deck/card/changed"));
233+
234+
int id = current->data(0, Qt::UserRole).toInt();
235+
236+
// Find the card in our hash using the ID
237+
if (_cards.contains(id)) {
238+
_currentCard = _cards[id];
239+
240+
ui->titleLineEdit->setText(_currentCard.title);
241+
ui->descriptionTextEdit->setPlainText(_currentCard.description);
242+
243+
// Set the due date
244+
if (_currentCard.duedate.isValid()) {
245+
ui->dueDateTimeCheckBox->setChecked(true);
246+
ui->dueDateTimeEdit->setDateTime(_currentCard.duedate);
247+
} else {
248+
ui->dueDateTimeCheckBox->setChecked(false);
249+
}
250+
251+
ui->saveButton->setEnabled(true);
252+
ui->editFrame->setEnabled(true);
253+
} else {
254+
resetEditFrameControls();
255+
}
256+
}

src/dialogs/nextclouddeckdialog.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
#define NEXTCLOUDDECKDIALOG_H
33

44
#include <QDialog>
5+
#include <QHash>
6+
#include <QList>
57
#include <QSplitter>
8+
#include <QTreeWidgetItem>
69

710
#include "masterdialog.h"
11+
#include "services/nextclouddeckservice.h"
812

913
namespace Ui {
1014
class NextcloudDeckDialog;
@@ -39,12 +43,18 @@ class NextcloudDeckDialog : public MasterDialog {
3943

4044
void reloadCardList();
4145

46+
void on_cardItemTreeWidget_currentItemChanged(QTreeWidgetItem *current,
47+
QTreeWidgetItem *previous);
48+
4249
private:
4350
Ui::NextcloudDeckDialog *ui;
4451
void setupMainSplitter();
4552
QSplitter *mainSplitter;
4653
void setupUi();
4754
void refreshUi();
55+
void resetEditFrameControls();
56+
QHash<int, NextcloudDeckService::Card> _cards;
57+
NextcloudDeckService::Card _currentCard;
4858
};
4959

5060
#endif // NEXTCLOUDDECKDIALOG_H

src/services/nextclouddeckservice.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ bool NextcloudDeckService::isEnabled() {
3636
this->cloudConnection.getNextcloudDeckEnabled();
3737
}
3838

39-
int NextcloudDeckService::createCard(const QString& title, const QString& description,
40-
QDateTime* dueDateTime) {
41-
int cardId = -1;
39+
int NextcloudDeckService::storeCard(const QString& title, const QString& description,
40+
QDateTime* dueDateTime, int cardId) {
41+
int resultCardId = -1;
4242
auto* manager = new QNetworkAccessManager();
4343
QEventLoop loop;
4444
QTimer timer;
@@ -51,14 +51,28 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
5151
// 10 sec timeout for the request
5252
timer.start(10000);
5353

54-
QUrl url(serverUrl + "/index.php/apps/deck/api/v1.1/boards/" + QString::number(this->boardId) +
55-
"/stacks/" + QString::number(this->stackId) + "/cards");
54+
QString urlString = serverUrl + "/index.php/apps/deck/api/v1.1/boards/" +
55+
QString::number(this->boardId) + "/stacks/" +
56+
QString::number(this->stackId) + "/cards";
57+
bool isUpdate = (cardId > 0);
58+
59+
if (isUpdate) {
60+
// URL for updating an existing card
61+
urlString += "/" + QString::number(cardId);
62+
}
63+
64+
const QUrl url(urlString);
65+
5666
qDebug() << __func__ << " - 'url': " << url;
67+
qDebug() << __func__ << " - 'isUpdate': " << isUpdate;
5768

5869
QJsonObject bodyJson;
5970
bodyJson["title"] = title;
6071
bodyJson["type"] = "plain";
61-
bodyJson["order"] = 0;
72+
73+
if (!isUpdate) {
74+
bodyJson["order"] = 0;
75+
}
6276

6377
if (description != "") {
6478
bodyJson["description"] = description;
@@ -90,7 +104,11 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
90104
networkRequest.setRawHeader("OCS-APIRequest", "true");
91105
addAuthHeader(networkRequest);
92106

93-
reply = manager->post(networkRequest, bodyJsonDoc.toJson());
107+
if (isUpdate) {
108+
reply = manager->put(networkRequest, bodyJsonDoc.toJson());
109+
} else {
110+
reply = manager->post(networkRequest, bodyJsonDoc.toJson());
111+
}
94112

95113
loop.exec();
96114

@@ -106,16 +124,23 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
106124

107125
QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
108126
QJsonObject jsonObject = jsonDoc.object();
109-
cardId = jsonObject["id"].toInt();
127+
resultCardId = jsonObject["id"].toInt();
128+
129+
// If we're updating, use the original cardId if the response doesn't contain one
130+
if (isUpdate && resultCardId <= 0) {
131+
resultCardId = cardId;
132+
}
110133

111-
qDebug() << __func__ << " - 'cardId': " << cardId;
134+
qDebug() << __func__ << " - 'resultCardId': " << resultCardId;
112135
qDebug() << __func__ << " - 'jsonDoc': " << jsonDoc;
113136
} else {
114137
QString errorString = reply->errorString();
115-
Utils::Gui::warning(nullptr, tr("Error while creating card"),
116-
tr("Creating a card failed with status code %1 and message: %2")
138+
QString operation = isUpdate ? tr("updating") : tr("creating");
139+
Utils::Gui::warning(nullptr, tr("Error while %1 card").arg(operation),
140+
tr("%1 a card failed with status code %2 and message: %3")
141+
.arg(operation.left(1).toUpper() + operation.mid(1))
117142
.arg(QString::number(returnStatusCode), errorString),
118-
"nextcloud-deck-create-failed");
143+
"nextcloud-deck-create-update-failed");
119144

120145
qDebug() << __func__ << " - error: " << returnStatusCode;
121146
qDebug() << __func__ << " - 'errorString': " << errorString;
@@ -125,7 +150,7 @@ int NextcloudDeckService::createCard(const QString& title, const QString& descri
125150
reply->deleteLater();
126151
delete (manager);
127152

128-
return cardId;
153+
return resultCardId;
129154
}
130155

131156
QString NextcloudDeckService::getCardLinkForId(int cardId) {
@@ -241,7 +266,7 @@ QList<NextcloudDeckService::Board> NextcloudDeckService::getBoards() {
241266
return boards;
242267
}
243268

244-
QList<NextcloudDeckService::Card> NextcloudDeckService::getCards() {
269+
QHash<int, NextcloudDeckService::Card> NextcloudDeckService::getCards() {
245270
auto* manager = new QNetworkAccessManager();
246271
QEventLoop loop;
247272
QTimer timer;
@@ -280,7 +305,7 @@ QList<NextcloudDeckService::Card> NextcloudDeckService::getCards() {
280305
reply = manager->get(networkRequest);
281306

282307
loop.exec();
283-
QList<NextcloudDeckService::Card> cards;
308+
QHash<int, NextcloudDeckService::Card> cards;
284309

285310
// if we didn't get a timeout let us return the content
286311
if (timer.isActive()) {
@@ -344,7 +369,7 @@ QList<NextcloudDeckService::Card> NextcloudDeckService::getCards() {
344369
}
345370

346371
qDebug() << __func__ << " - found card: " << card;
347-
cards.append(card);
372+
cards[card.id] = card;
348373
}
349374

350375
// We found our stack, no need to continue

src/services/nextclouddeckservice.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class NextcloudDeckService : public QObject {
2626
// }
2727
};
2828

29+
public:
2930
struct Card {
3031
int id;
3132
QString title;
@@ -46,15 +47,14 @@ class NextcloudDeckService : public QObject {
4647
}
4748
};
4849

49-
public:
5050
explicit NextcloudDeckService(QObject* parent, int cloudConnectionId = -1);
51-
int createCard(const QString& title, const QString& description = "",
52-
QDateTime* dueDateTime = nullptr);
51+
int storeCard(const QString& title, const QString& description = "",
52+
QDateTime* dueDateTime = nullptr, int cardId = -1);
5353
QString getCardLinkForId(int cardId);
5454
bool isEnabledAndValid();
5555
bool isEnabled();
5656
QList<Board> getBoards();
57-
QList<Card> getCards();
57+
QHash<int, Card> getCards();
5858

5959
private:
6060
CloudConnection cloudConnection;

0 commit comments

Comments
 (0)