File indexing completed on 2024-04-14 04:50:14

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "systrayicon.h"
0010 
0011 #include <QFontDatabase>
0012 #include <QPainter>
0013 #include <QTimer>
0014 
0015 #include <KColorScheme>
0016 #include <KLocalizedString>
0017 
0018 #include "choqokdebug.h"
0019 
0020 SysTrayIcon::SysTrayIcon(Choqok::UI::MainWindow *parent)
0021     : KStatusNotifierItem(parent), _mainwin(parent), isOffline(false)
0022 {
0023     qCDebug(CHOQOK);
0024     unread = 0;
0025     setAssociatedWidget(parent);
0026     setCategory(ApplicationStatus);
0027     setStandardActionsEnabled(false);
0028 //     setStatus(Active);
0029     setIconByName(currentIconName());
0030 }
0031 
0032 SysTrayIcon::~SysTrayIcon()
0033 {
0034     qCDebug(CHOQOK);
0035 }
0036 
0037 void SysTrayIcon::resetUnreadCount()
0038 {
0039     updateUnreadCount(-unread);
0040 }
0041 
0042 QString SysTrayIcon::currentIconName()
0043 {
0044     if (isOffline) {
0045         return QLatin1String("choqok_offline");
0046     } else {
0047         return QLatin1String("choqok");
0048     }
0049 }
0050 
0051 void SysTrayIcon::updateUnreadCount(int changeOfUnreadPosts)
0052 {
0053     qCDebug(CHOQOK);
0054     unread += changeOfUnreadPosts;
0055 
0056     if (unread <= 0) {
0057         setIconByName(currentIconName());
0058         unread = 0;
0059         setStatus(Passive);
0060     } else {
0061         setStatus(Active);
0062         int oldWidth = 22;
0063 
0064         QString countStr = QString::number(unread);
0065         QFont f = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
0066         f.setBold(true);
0067 
0068         auto pointSize = f.pointSizeF();
0069         QFontMetrics fm(f);
0070         int w = fm.horizontalAdvance(countStr);
0071         if (w > (oldWidth - 2)) {
0072             pointSize *= float(oldWidth - 2) / float(w);
0073             f.setPointSizeF(pointSize);
0074         }
0075 
0076         // overlay
0077         QPixmap overlayImg = QIcon::fromTheme(currentIconName()).pixmap(22, 22);
0078         QPainter p(&overlayImg);
0079         p.setFont(f);
0080 
0081         fm = QFontMetrics(f);
0082         QRect boundingRect = fm.tightBoundingRect(countStr);
0083         boundingRect.adjust(0, 0, 0, 2);
0084         boundingRect.setHeight(qMin(boundingRect.height(), oldWidth));
0085         boundingRect.moveTo((oldWidth - boundingRect.width()) / 2,
0086                             ((oldWidth - boundingRect.height()) / 2) - 1);
0087         p.setOpacity(0.7);
0088         QBrush br(QColor(255, 255, 255), Qt::SolidPattern);
0089         p.setBrush(br);
0090         p.setPen(QColor(255, 255, 255));
0091         p.drawRoundedRect(boundingRect, 2.0, 2.0);
0092 
0093         p.setBrush(Qt::NoBrush);
0094         p.setPen(QColor(0, 0, 0));
0095         p.setOpacity(1.0);
0096         p.drawText(overlayImg.rect(), Qt::AlignCenter, countStr);
0097         setIconByPixmap(overlayImg);
0098     }
0099     this->setToolTip(QLatin1String("choqok"), i18n("Choqok"), i18np("1 unread post", "%1 unread posts", unread));
0100 }
0101 
0102 void SysTrayIcon::setTimeLineUpdatesEnabled(bool isEnabled)
0103 {
0104     if (isEnabled) {
0105         setToolTip(QLatin1String("choqok"), i18n("Choqok"), QString());
0106         setIconByName(QLatin1String("choqok"));
0107     } else {
0108         setToolTip(QLatin1String("choqok"), i18n("Choqok - Disabled"), QString());
0109         setIconByName(QLatin1String("choqok_offline"));
0110     }
0111     isOffline = !isEnabled;
0112     updateUnreadCount(0);
0113 }
0114 
0115 void SysTrayIcon::slotJobDone(Choqok::JobResult result)
0116 {
0117     qCDebug(CHOQOK);
0118     if (result == Choqok::Success) {
0119         setOverlayIconByName(QLatin1String("task-complete"));
0120     } else {
0121         setOverlayIconByName(QLatin1String("task-reject"));
0122     }
0123     QTimer::singleShot(5000, this, &SysTrayIcon::slotRestoreIcon);
0124 }
0125 
0126 void SysTrayIcon::slotRestoreIcon()
0127 {
0128     setOverlayIconByName(QString());
0129     updateUnreadCount(0);
0130 }
0131 
0132 int SysTrayIcon::unreadCount() const
0133 {
0134     return unread;
0135 }
0136 
0137 #include "moc_systrayicon.cpp"