Skip to content

Commit 6971e65

Browse files
committed
#3248 owncloudservice: use local network manager in loadTodoItems
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent 963b84b commit 6971e65

File tree

2 files changed

+86
-57
lines changed

2 files changed

+86
-57
lines changed

src/services/owncloudservice.cpp

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -314,53 +314,6 @@ void OwnCloudService::slotReplyFinished(QNetworkReply *reply) {
314314
if (settingsDialog != nullptr) {
315315
settingsDialog->refreshTodoCalendarList(calendarDataList);
316316
}
317-
} else if (!todoCalendarServerUrlPath.isEmpty() &&
318-
urlPath.startsWith(todoCalendarServerUrlPath)) {
319-
// check if we have a reply from a calendar item request
320-
if (urlPath.endsWith(QStringLiteral(".ics"))) {
321-
qDebug() << "Reply from ownCloud calendar item ics page";
322-
// qDebug() << data;
323-
324-
// a workaround for a ownCloud error message
325-
if (data.indexOf(QStringLiteral("<s:message>Unable to generate a URL for the named"
326-
" route \"tasksplus.page.index\" as such route"
327-
" does not exist.</s:message>")) > 20) {
328-
data = QString();
329-
}
330-
331-
if (todoDialog != nullptr) {
332-
// this will mostly happen after the PUT request to update
333-
// or create a task item
334-
if (data.isEmpty()) {
335-
// reload the task list from server
336-
todoDialog->reloadTodoList();
337-
}
338-
339-
// increment the progress bar
340-
todoDialog->todoItemLoadingProgressBarIncrement();
341-
}
342-
343-
// fetch the calendar item, that was already stored
344-
// by loadTodoItems()
345-
CalendarItem calItem =
346-
CalendarItem::fetchByUrlAndCalendar(url.toString(), calendarName);
347-
if (calItem.isFetched()) {
348-
// update the item with the ics data
349-
bool wasUpdated = calItem.updateWithICSData(data);
350-
351-
// if item wasn't updated (for example because it was no
352-
// VTODO item) we will remove it
353-
if (!wasUpdated) {
354-
calItem.remove();
355-
} else if (todoDialog != nullptr) {
356-
// reload the task list items
357-
todoDialog->reloadTodoListItems();
358-
}
359-
360-
// qDebug() << __func__ << " - 'calItem':
361-
// " << calItem;
362-
}
363-
}
364317
} else if (urlPath.startsWith(serverUrlPath % webdavPath())) {
365318
// this should be the reply of a calendar item list request
366319
qDebug() << "Reply from ownCloud webdav";
@@ -660,9 +613,6 @@ void OwnCloudService::settingsGetCalendarList(SettingsDialog *dialog) {
660613
*/
661614
void OwnCloudService::todoGetTodoList(const QString &calendarName, TodoDialog *dialog) {
662615
this->todoDialog = dialog;
663-
// TODO: Don't set the calendarName globally after all requests are made with lambda functions!
664-
this->calendarName = calendarName;
665-
666616
SettingsService settings;
667617
QStringList todoCalendarEnabledList =
668618
settings.value(QStringLiteral("ownCloud/todoCalendarEnabledList")).toStringList();
@@ -716,6 +666,10 @@ void OwnCloudService::todoGetTodoList(const QString &calendarName, TodoDialog *d
716666
// Use a local QNetworkAccessManager
717667
auto *calNetworkManager = new QNetworkAccessManager(this);
718668

669+
QObject::connect(calNetworkManager,
670+
SIGNAL(authenticationRequired(QNetworkReply *, QAuthenticator *)), this,
671+
SLOT(slotCalendarAuthenticationRequired(QNetworkReply *, QAuthenticator *)));
672+
719673
// Connect the finished signal to a lambda function
720674
connect(calNetworkManager, &QNetworkAccessManager::finished, [=](QNetworkReply *reply) {
721675
if (reply->error() == QNetworkReply::NoError) {
@@ -726,10 +680,12 @@ void OwnCloudService::todoGetTodoList(const QString &calendarName, TodoDialog *d
726680
qDebug() << __func__ << " - 'responseData': " << responseData;
727681

728682
// Parse the responseData to extract the to-do items
729-
loadTodoItems(data);
683+
loadTodoItems(calendarName, data);
730684
} else {
731-
// Handle the error
732-
qDebug() << __func__ << " - 'reply->errorString()': " << reply->errorString();
685+
// For error codes see http://doc.qt.io/qt-5/qnetworkreply.html#NetworkError-enum
686+
qWarning() << "QNetworkReply error " + QString::number(reply->error()) + " from url " +
687+
url.toString() + ":"
688+
<< reply->errorString();
733689
}
734690

735691
reply->deleteLater();
@@ -1463,7 +1419,7 @@ QList<CalDAVCalendarData> OwnCloudService::parseCalendarData(QString &data) {
14631419
return resultList;
14641420
}
14651421

1466-
void OwnCloudService::loadTodoItems(QString &data) {
1422+
void OwnCloudService::loadTodoItems(const QString &calendarName, QString &data) {
14671423
QDomDocument doc;
14681424
doc.setContent(data, true);
14691425

@@ -1549,7 +1505,81 @@ void OwnCloudService::loadTodoItems(QString &data) {
15491505
QNetworkRequest r(calendarItemUrl);
15501506
addCalendarAuthHeader(&r);
15511507

1552-
QNetworkReply *reply = calendarNetworkManager->get(r);
1508+
// Use a local QNetworkAccessManager
1509+
auto *calNetworkManager = new QNetworkAccessManager(this);
1510+
1511+
QObject::connect(
1512+
calNetworkManager,
1513+
SIGNAL(authenticationRequired(QNetworkReply *, QAuthenticator *)),
1514+
this,
1515+
SLOT(slotCalendarAuthenticationRequired(QNetworkReply *,
1516+
QAuthenticator *)));
1517+
1518+
// Connect the finished signal to a lambda function
1519+
connect(
1520+
calNetworkManager, &QNetworkAccessManager::finished,
1521+
[=](QNetworkReply *reply) {
1522+
QUrl url = reply->url();
1523+
1524+
if (reply->error() == QNetworkReply::NoError) {
1525+
QByteArray responseData = reply->readAll();
1526+
QString data = QString(responseData);
1527+
qDebug() << "Reply from ownCloud calendar item ics page"
1528+
<< calendarItemUrl;
1529+
// qDebug() << data;
1530+
1531+
// Workaround for a ownCloud error message
1532+
if (data.indexOf(QStringLiteral(
1533+
"<s:message>Unable to generate a URL for the named"
1534+
" route \"tasksplus.page.index\" as such route"
1535+
" does not exist.</s:message>")) > 20) {
1536+
data = QString();
1537+
}
1538+
1539+
if (todoDialog != nullptr) {
1540+
// This will mostly happen after the PUT request to
1541+
// update or create a task item
1542+
if (data.isEmpty()) {
1543+
// Reload the task list from server
1544+
todoDialog->reloadTodoList();
1545+
}
1546+
1547+
// Increment the progress bar
1548+
todoDialog->todoItemLoadingProgressBarIncrement();
1549+
}
1550+
1551+
// Fetch the calendar item, that was already stored by
1552+
// loadTodoItems()
1553+
CalendarItem calItem = CalendarItem::fetchByUrlAndCalendar(
1554+
url.toString(), calendarName);
1555+
if (calItem.isFetched()) {
1556+
// Update the item with the ics data
1557+
bool wasUpdated = calItem.updateWithICSData(data);
1558+
1559+
// If item wasn't updated (for example because it was no
1560+
// VTODO item) we will remove it
1561+
if (!wasUpdated) {
1562+
calItem.remove();
1563+
} else if (todoDialog != nullptr) {
1564+
// reload the task list items
1565+
todoDialog->reloadTodoListItems();
1566+
}
1567+
}
1568+
} else {
1569+
// For error codes see
1570+
// http://doc.qt.io/qt-5/qnetworkreply.html#NetworkError-enum
1571+
qWarning() << "QNetworkReply error " +
1572+
QString::number(reply->error()) +
1573+
" from url " + url.toString() + ":"
1574+
<< reply->errorString();
1575+
}
1576+
1577+
reply->deleteLater();
1578+
calNetworkManager->deleteLater();
1579+
});
1580+
1581+
// QNetworkReply *reply = calendarNetworkManager->get(r);
1582+
QNetworkReply *reply = calNetworkManager->get(r);
15531583
ignoreSslErrorsIfAllowed(reply);
15541584

15551585
requestCount++;

src/services/owncloudservice.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ class OwnCloudService : public QObject {
124124
QString trashDeletePath;
125125
QPointer<SettingsDialog> settingsDialog;
126126
TodoDialog *todoDialog;
127-
QString calendarName;
128127

129128
void checkAppInfo(QNetworkReply *reply);
130129

@@ -143,7 +142,7 @@ class OwnCloudService : public QObject {
143142

144143
QList<CalDAVCalendarData> parseCalendarData(QString &data);
145144

146-
void loadTodoItems(QString &data);
145+
void loadTodoItems(const QString &calendarName, QString &data);
147146

148147
static void ignoreSslErrorsIfAllowed(QNetworkReply *reply);
149148

0 commit comments

Comments
 (0)