1 /*
2 This file is part of Choqok, the KDE micro-blogging client
3
4 Copyright (C) 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, see http://www.gnu.org/licenses/
21
22 */
23 #include "systrayicon.h"
24
25 #include <QFontDatabase>
26 #include <QPainter>
27 #include <QTimer>
28
29 #include <KColorScheme>
30 #include <KLocalizedString>
31
32 #include "choqokdebug.h"
33
SysTrayIcon(Choqok::UI::MainWindow * parent)34 SysTrayIcon::SysTrayIcon(Choqok::UI::MainWindow *parent)
35 : KStatusNotifierItem(parent), _mainwin(parent), isOffline(false)
36 {
37 qCDebug(CHOQOK);
38 unread = 0;
39 setAssociatedWidget(parent);
40 setCategory(ApplicationStatus);
41 setStandardActionsEnabled(false);
42 // setStatus(Active);
43 setIconByName(currentIconName());
44 }
45
~SysTrayIcon()46 SysTrayIcon::~SysTrayIcon()
47 {
48 qCDebug(CHOQOK);
49 }
50
resetUnreadCount()51 void SysTrayIcon::resetUnreadCount()
52 {
53 updateUnreadCount(-unread);
54 }
55
currentIconName()56 QString SysTrayIcon::currentIconName()
57 {
58 if (isOffline) {
59 return QLatin1String("choqok_offline");
60 } else {
61 return QLatin1String("choqok");
62 }
63 }
64
updateUnreadCount(int changeOfUnreadPosts)65 void SysTrayIcon::updateUnreadCount(int changeOfUnreadPosts)
66 {
67 qCDebug(CHOQOK);
68 unread += changeOfUnreadPosts;
69
70 if (unread <= 0) {
71 setIconByName(currentIconName());
72 unread = 0;
73 setStatus(Passive);
74 } else {
75 setStatus(Active);
76 int oldWidth = 22;
77
78 QString countStr = QString::number(unread);
79 QFont f = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
80 f.setBold(true);
81
82 auto pointSize = f.pointSizeF();
83 QFontMetrics fm(f);
84 int w = fm.horizontalAdvance(countStr);
85 if (w > (oldWidth - 2)) {
86 pointSize *= float(oldWidth - 2) / float(w);
87 f.setPointSizeF(pointSize);
88 }
89
90 // overlay
91 QPixmap overlayImg = QIcon::fromTheme(currentIconName()).pixmap(22, 22);
92 QPainter p(&overlayImg);
93 p.setFont(f);
94
95 fm = QFontMetrics(f);
96 QRect boundingRect = fm.tightBoundingRect(countStr);
97 boundingRect.adjust(0, 0, 0, 2);
98 boundingRect.setHeight(qMin(boundingRect.height(), oldWidth));
99 boundingRect.moveTo((oldWidth - boundingRect.width()) / 2,
100 ((oldWidth - boundingRect.height()) / 2) - 1);
101 p.setOpacity(0.7);
102 QBrush br(QColor(255, 255, 255), Qt::SolidPattern);
103 p.setBrush(br);
104 p.setPen(QColor(255, 255, 255));
105 p.drawRoundedRect(boundingRect, 2.0, 2.0);
106
107 p.setBrush(Qt::NoBrush);
108 p.setPen(QColor(0, 0, 0));
109 p.setOpacity(1.0);
110 p.drawText(overlayImg.rect(), Qt::AlignCenter, countStr);
111 setIconByPixmap(overlayImg);
112 }
113 this->setToolTip(QLatin1String("choqok"), i18n("Choqok"), i18np("1 unread post", "%1 unread posts", unread));
114 }
115
setTimeLineUpdatesEnabled(bool isEnabled)116 void SysTrayIcon::setTimeLineUpdatesEnabled(bool isEnabled)
117 {
118 if (isEnabled) {
119 setToolTip(QLatin1String("choqok"), i18n("Choqok"), QString());
120 setIconByName(QLatin1String("choqok"));
121 } else {
122 setToolTip(QLatin1String("choqok"), i18n("Choqok - Disabled"), QString());
123 setIconByName(QLatin1String("choqok_offline"));
124 }
125 isOffline = !isEnabled;
126 updateUnreadCount(0);
127 }
128
slotJobDone(Choqok::JobResult result)129 void SysTrayIcon::slotJobDone(Choqok::JobResult result)
130 {
131 qCDebug(CHOQOK);
132 if (result == Choqok::Success) {
133 setOverlayIconByName(QLatin1String("task-complete"));
134 } else {
135 setOverlayIconByName(QLatin1String("task-reject"));
136 }
137 QTimer::singleShot(5000, this, &SysTrayIcon::slotRestoreIcon);
138 }
139
slotRestoreIcon()140 void SysTrayIcon::slotRestoreIcon()
141 {
142 setOverlayIconByName(QString());
143 updateUnreadCount(0);
144 }
145
unreadCount() const146 int SysTrayIcon::unreadCount() const
147 {
148 return unread;
149 }
150
151