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

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2014-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 "toasty.h"
0019 #include "toastyconstants.h"
0020 
0021 #include "libsnore/utils.h"
0022 
0023 #include <QNetworkReply>
0024 #include <QNetworkRequest>
0025 #include <QHttpMultiPart>
0026 #include <QFile>
0027 
0028 namespace SnorePlugin {
0029 
0030 void Toasty::slotNotify(Snore::Notification notification)
0031 {
0032     QString key = settingsValue(ToastyConstants::DeviceID).toString();
0033     if (key.isEmpty()) {
0034         return;
0035     }
0036     QNetworkRequest request(QUrl::fromUserInput(QLatin1String("http://api.supertoasty.com/notify/") + key));
0037     QHttpMultiPart *mp = new QHttpMultiPart(QHttpMultiPart::FormDataType);
0038 
0039     QHttpPart title;
0040     title.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"title\"")));
0041     title.setBody(notification.title().toUtf8().constData());
0042     mp->append(title);
0043 
0044     QHttpPart text;
0045     text.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"text\"")));
0046     text.setBody(notification.text().toUtf8().constData());
0047     mp->append(text);
0048 
0049     QHttpPart app;
0050     app.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"sender\"")));
0051     app.setBody(notification.application().name().toUtf8().constData());
0052     mp->append(app);
0053 
0054     QHttpPart icon;
0055 
0056     icon.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"image\"; filename=\"") + notification.icon().localUrl(QSize(128, 128)) + QLatin1Char('"')));
0057     icon.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(QLatin1String("image/png")));
0058     QFile *file = new QFile(notification.icon().localUrl(QSize(128, 128)));
0059     file->open(QIODevice::ReadOnly);
0060     icon.setBodyDevice(file);
0061     mp->append(icon);
0062 
0063     QNetworkReply *reply =  m_manager.post(request, mp);
0064     mp->setParent(reply);
0065     file->setParent(reply);
0066 
0067     connect(reply, &QNetworkReply::finished, [reply]() {
0068         qCDebug(SNORE) << reply->error();
0069         qCDebug(SNORE) << reply->readAll();
0070         reply->close();
0071         reply->deleteLater();
0072     });
0073 
0074 }
0075 
0076 void Toasty::setDefaultSettings()
0077 {
0078     setDefaultSettingsValue(ToastyConstants::DeviceID, QString());
0079     SnoreSecondaryBackend::setDefaultSettings();
0080 }
0081 
0082 }