File indexing completed on 2025-07-06 12:43:42
0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu> 0002 // SPDX-License-Identifier: GPL-3.0-or-later 0003 0004 #include "notificationhandler.h" 0005 #include "account.h" 0006 #include <KLocalizedString> 0007 #include <KNotification> 0008 #include <QNetworkAccessManager> 0009 #include <QPainter> 0010 0011 NotificationHandler::NotificationHandler(QNetworkAccessManager *nam, QObject *parent) 0012 : QObject(parent) 0013 , m_nam(nam) 0014 { 0015 } 0016 0017 void NotificationHandler::handle(std::shared_ptr<Notification> notification, AbstractAccount *account) 0018 { 0019 KNotification *knotification; 0020 0021 switch (notification->type()) { 0022 case Notification::Favorite: 0023 knotification = new KNotification("favorite"); 0024 knotification->setTitle(i18n("%1 favorited your post", notification->identity()->displayName())); 0025 break; 0026 case Notification::Follow: 0027 knotification = new KNotification("follow"); 0028 knotification->setTitle(i18n("%1 followed you", notification->identity()->displayName())); 0029 break; 0030 case Notification::Repeat: 0031 knotification = new KNotification("boost"); 0032 knotification->setTitle(i18n("%1 boosted your post", notification->identity()->displayName())); 0033 break; 0034 case Notification::Mention: 0035 knotification = new KNotification("mention"); 0036 knotification->setTitle(notification->identity()->displayName()); 0037 break; 0038 case Notification::Update: 0039 knotification = new KNotification("update"); 0040 knotification->setTitle(i18n("%1 edited a post", notification->identity()->displayName())); 0041 break; 0042 default: 0043 Q_UNREACHABLE(); 0044 } 0045 0046 if (notification->post() != nullptr) { 0047 knotification->setText(notification->post()->content()); 0048 } 0049 knotification->setHint(QStringLiteral("x-kde-origin-name"), account->identity()->displayName()); 0050 0051 if (!notification->identity()->avatarUrl().isEmpty()) { 0052 const auto avatarUrl = notification->identity()->avatarUrl(); 0053 auto request = QNetworkRequest(avatarUrl); 0054 auto reply = m_nam->get(request); 0055 connect(reply, &QNetworkReply::finished, this, [reply, knotification]() { 0056 reply->deleteLater(); 0057 if (reply->error() != QNetworkReply::NoError) { 0058 knotification->sendEvent(); 0059 return; 0060 } 0061 QPixmap img; 0062 img.loadFromData(reply->readAll()); 0063 0064 QImage roundedImage(img.width(), img.height(), QImage::Format_ARGB32); 0065 0066 QPainter painter(&roundedImage); 0067 painter.setRenderHint(QPainter::Antialiasing); 0068 0069 QBrush brush(img); 0070 painter.setBrush(brush); 0071 0072 painter.drawRoundedRect(0, 0, img.width(), img.height(), img.width(), img.height()); 0073 0074 knotification->setPixmap(QPixmap::fromImage(roundedImage)); 0075 knotification->sendEvent(); 0076 }); 0077 } else { 0078 knotification->sendEvent(); 0079 } 0080 }