1 /*
2 * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "notificationconfirmjob.h"
16 #include "networkjobs.h"
17 #include "account.h"
18
19 #include <QBuffer>
20
21 namespace OCC {
22
23 Q_LOGGING_CATEGORY(lcNotificationsJob, "nextcloud.gui.notifications", QtInfoMsg)
24
NotificationConfirmJob(AccountPtr account)25 NotificationConfirmJob::NotificationConfirmJob(AccountPtr account)
26 : AbstractNetworkJob(account, "")
27 {
28 setIgnoreCredentialFailure(true);
29 }
30
setLinkAndVerb(const QUrl & link,const QByteArray & verb)31 void NotificationConfirmJob::setLinkAndVerb(const QUrl &link, const QByteArray &verb)
32 {
33 _link = link;
34 _verb = verb;
35 }
36
start()37 void NotificationConfirmJob::start()
38 {
39 if (!_link.isValid()) {
40 qCWarning(lcNotificationsJob) << "Attempt to trigger invalid URL: " << _link.toString();
41 return;
42 }
43 QNetworkRequest req;
44 req.setRawHeader("Ocs-APIREQUEST", "true");
45 req.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
46
47 sendRequest(_verb, _link, req);
48
49 AbstractNetworkJob::start();
50 }
51
finished()52 bool NotificationConfirmJob::finished()
53 {
54 int replyCode = 0;
55 // FIXME: check for the reply code!
56 const QString replyStr = reply()->readAll();
57
58 if (replyStr.contains("<?xml version=\"1.0\"?>")) {
59 const QRegularExpression rex("<statuscode>(\\d+)</statuscode>");
60 const auto rexMatch = rex.match(replyStr);
61 if (rexMatch.hasMatch()) {
62 // this is a error message coming back from ocs.
63 replyCode = rexMatch.captured(1).toInt();
64 }
65 }
66 emit jobFinished(replyStr, replyCode);
67
68 return true;
69 }
70 }
71