File indexing completed on 2024-06-16 04:29:31

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2015  Hannah von Reth <vonreth@kde.org>
0004 
0005     SnoreNotify is free software: you can redistribute it and/or modify
0006     it under the terms of the GNU Lesser General Public License as published by
0007     the Free Software Foundation, either version 3 of the License, or
0008     (at your option) any later version.
0009 
0010     SnoreNotify is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public License
0016     along with SnoreNotify.  If not, see <http://www.gnu.org/licenses/>.
0017 */
0018 #include "pushover.h"
0019 #include "pushoverconstants.h"
0020 
0021 #include "libsnore/utils.h"
0022 #include "libsnore/notification/notification_p.h"
0023 
0024 #include <QNetworkReply>
0025 #include <QNetworkRequest>
0026 #include <QHttpMultiPart>
0027 
0028 namespace SnorePlugin {
0029 
0030 void Pushover::slotNotify(Snore::Notification notification)
0031 {
0032     if (notification.data()->sourceAndTargetAreSimilar(this)) {
0033         return;
0034     }
0035 
0036     QString key = settingsValue(PushoverConstants::UserKey).toString();
0037     if (key.isEmpty()) {
0038         return;
0039     }
0040 
0041     QNetworkRequest request(QUrl::fromEncoded("https://api.pushover.net/1/messages.json"));
0042     QHttpMultiPart *mp = new QHttpMultiPart(QHttpMultiPart::FormDataType);
0043 
0044     QHttpPart title;
0045     title.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"title\"")));
0046     title.setBody(notification.title().toUtf8().constData());
0047     mp->append(title);
0048 
0049     QString textString;
0050     if (notification.constHints().value("use-markup").toBool()) {
0051         textString = notification.text(Snore::Utils::Href | Snore::Utils::Bold | Snore::Utils::Underline | Snore::Utils::Font | Snore::Utils::Italic);
0052 
0053         QHttpPart html;
0054         html.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"html\"")));
0055         html.setBody("1");
0056         mp->append(html);
0057     } else {
0058         textString = notification.text();
0059     }
0060 
0061     QHttpPart text;
0062     text.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"message\"")));
0063     qCDebug(SNORE) << "Use Markup" << notification.constHints().value("use-markup").toBool();
0064     qCDebug(SNORE) << "Message" << textString;
0065     text.setBody(textString.toUtf8().constData());
0066     mp->append(text);
0067 
0068     QHttpPart priority;
0069     priority.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"priority\"")));
0070     priority.setBody(QString::number(notification.priority()).toUtf8().constData());
0071     mp->append(priority);
0072     if (notification.priority() == Snore::Notification::Emergency) {
0073 
0074         QHttpPart retry;
0075         retry.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"retry\"")));
0076         retry.setBody("30");// rety every 30 s
0077         mp->append(retry);
0078 
0079         QHttpPart expire;
0080         expire.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"expire\"")));
0081         expire.setBody("300");//5 min
0082         mp->append(expire);
0083     }
0084 
0085     QHttpPart sound;
0086     sound.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"sound\"")));
0087     if (notification.hints().value("silent").toBool()) {
0088         sound.setBody("none");
0089     } else {
0090         sound.setBody((settingsValue(PushoverConstants::Sound)).toString().toUtf8().constData());
0091     }
0092     mp->append(sound);
0093 
0094     if (!settingsValue(PushoverConstants::Devices).toString().isEmpty()) {
0095         QHttpPart devices;
0096         devices.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"device\"")));
0097         devices.setBody(settingsValue(PushoverConstants::Devices).toString().toUtf8().constData());
0098         mp->append(devices);
0099     }
0100 
0101     QHttpPart token;
0102     token.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"token\"")));
0103     token.setBody(notification.application().constHints().value("pushover-token").toString().toUtf8().constData());
0104     mp->append(token);
0105 
0106     QHttpPart user;
0107     user.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"user\"")));
0108     user.setBody(key.toUtf8().constData());
0109     mp->append(user);
0110 
0111     QNetworkReply *reply =  m_manager.post(request, mp);
0112     mp->setParent(reply);
0113 
0114     connect(reply, &QNetworkReply::finished, [reply]() {
0115         qCDebug(SNORE) << reply->error();
0116         qCDebug(SNORE) << reply->readAll();
0117         reply->close();
0118         reply->deleteLater();
0119     });
0120 
0121 }
0122 
0123 void Pushover::setDefaultSettings()
0124 {
0125     setDefaultSettingsValue(PushoverConstants::UserKey, QString());
0126     setDefaultSettingsValue(PushoverConstants::Sound, QLatin1String("pushover"));
0127     setDefaultSettingsValue(PushoverConstants::Devices, QString());
0128     SnoreSecondaryBackend::setDefaultSettings();
0129 }
0130 
0131 }