Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | #ifndef QT_NO_SYSTEMTRAYICON | - |
41 | | - |
42 | #include "qdbustrayicon_p.h" | - |
43 | #include "qdbusmenuconnection_p.h" | - |
44 | #include "qstatusnotifieritemadaptor_p.h" | - |
45 | #include "qdbusmenuadaptor_p.h" | - |
46 | #include "dbusmenu/qdbusplatformmenu_p.h" | - |
47 | #include "qxdgnotificationproxy_p.h" | - |
48 | | - |
49 | #include <qplatformmenu.h> | - |
50 | #include <qstring.h> | - |
51 | #include <qdebug.h> | - |
52 | #include <qrect.h> | - |
53 | #include <qloggingcategory.h> | - |
54 | #include <qplatformintegration.h> | - |
55 | #include <qplatformservices.h> | - |
56 | #include <qdbusconnectioninterface.h> | - |
57 | #include <private/qlockfile_p.h> | - |
58 | #include <private/qguiapplication_p.h> | - |
59 | | - |
60 | | - |
61 | #undef interface | - |
62 | | - |
63 | QT_BEGIN_NAMESPACE | - |
64 | | - |
65 | Q_LOGGING_CATEGORY(qLcTray, "qt.qpa.tray") | - |
66 | | - |
67 | static const QString KDEItemFormat = QStringLiteral("org.kde.StatusNotifierItem-%1-%2"); | - |
68 | static const QString KDEWatcherService = QStringLiteral("org.kde.StatusNotifierWatcher"); | - |
69 | static const QString TempFileTemplate = QDir::tempPath() + QStringLiteralQLatin1String("/qt-trayicon-XXXXXX.png"); | - |
70 | static const QString XdgNotificationService = QStringLiteral("org.freedesktop.Notifications"); | - |
71 | static const QString XdgNotificationPath = QStringLiteral("/org/freedesktop/Notifications"); | - |
72 | static const QString DefaultAction = QStringLiteral("default"); | - |
73 | static int instanceCount = 0; | - |
74 | | - |
75 | | - |
76 | | - |
77 | | - |
78 | | - |
79 | | - |
80 | QDBusTrayIcon::QDBusTrayIcon() | - |
81 | : m_dbusConnection(Q_NULLPTR) | - |
82 | , m_adaptor(new QStatusNotifierItemAdaptor(this)) | - |
83 | , m_menuAdaptor(Q_NULLPTR) | - |
84 | , m_menu(Q_NULLPTR) | - |
85 | , m_notifier(Q_NULLPTR) | - |
86 | , m_instanceId(KDEItemFormat.arg(QCoreApplication::applicationPid()).arg(++instanceCount)) | - |
87 | , m_category(QStringLiteral("ApplicationStatus")) | - |
88 | , m_defaultStatus(QStringLiteral("Active")) | - |
89 | , m_status(m_defaultStatus) | - |
90 | , m_tempIcon(Q_NULLPTR) | - |
91 | , m_tempAttentionIcon(Q_NULLPTR) | - |
92 | , m_registered(false) | - |
93 | { | - |
94 | qCDebug(qLcTray); | - |
95 | if (instanceCount == 1) { | - |
96 | QDBusMenuItem::registerDBusTypes(); | - |
97 | qDBusRegisterMetaType<QXdgDBusImageStruct>(); | - |
98 | qDBusRegisterMetaType<QXdgDBusImageVector>(); | - |
99 | qDBusRegisterMetaType<QXdgDBusToolTipStruct>(); | - |
100 | } | - |
101 | connect(this, SIGNAL(statusChanged(QString)), m_adaptor, SIGNAL(NewStatus(QString))); | - |
102 | connect(this, SIGNAL(tooltipChanged()), m_adaptor, SIGNAL(NewToolTip())); | - |
103 | connect(this, SIGNAL(iconChanged()), m_adaptor, SIGNAL(NewIcon())); | - |
104 | connect(this, SIGNAL(attention()), m_adaptor, SIGNAL(NewAttentionIcon())); | - |
105 | connect(this, SIGNAL(attention()), m_adaptor, SIGNAL(NewTitle())); | - |
106 | connect(&m_attentionTimer, SIGNAL(timeout()), this, SLOT(attentionTimerExpired())); | - |
107 | m_attentionTimer.setSingleShot(true); | - |
108 | } | - |
109 | | - |
110 | QDBusTrayIcon::~QDBusTrayIcon() | - |
111 | { | - |
112 | } | - |
113 | | - |
114 | void QDBusTrayIcon::init() | - |
115 | { | - |
116 | qCDebug(qLcTray) << "registering" << m_instanceId; | - |
117 | m_registered = dBusConnection()->registerTrayIcon(this); | - |
118 | } | - |
119 | | - |
120 | void QDBusTrayIcon::cleanup() | - |
121 | { | - |
122 | qCDebug(qLcTray) << "unregistering" << m_instanceId; | - |
123 | if (m_registered) | - |
124 | dBusConnection()->unregisterTrayIcon(this); | - |
125 | delete m_dbusConnection; | - |
126 | m_dbusConnection = Q_NULLPTR; | - |
127 | delete m_notifier; | - |
128 | m_notifier = Q_NULLPTR; | - |
129 | m_registered = false; | - |
130 | } | - |
131 | | - |
132 | void QDBusTrayIcon::attentionTimerExpired() | - |
133 | { | - |
134 | m_messageTitle = QString(); | - |
135 | m_message = QString(); | - |
136 | m_attentionIcon = QIcon(); | - |
137 | emit attention(); | - |
138 | emit tooltipChanged(); | - |
139 | setStatus(m_defaultStatus); | - |
140 | } | - |
141 | | - |
142 | void QDBusTrayIcon::setStatus(const QString &status) | - |
143 | { | - |
144 | qCDebug(qLcTray) << status; | - |
145 | if (m_status == status) | - |
146 | return; | - |
147 | m_status = status; | - |
148 | emit statusChanged(m_status); | - |
149 | } | - |
150 | | - |
151 | QTemporaryFile *QDBusTrayIcon::tempIcon(const QIcon &icon) | - |
152 | { | - |
153 | | - |
154 | | - |
155 | static bool necessity_checked = false; | - |
156 | static bool necessary = false; | - |
157 | if (!necessity_checked) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
158 | QDBusConnection session = QDBusConnection::sessionBus(); | - |
159 | uint pid = session.interface()->servicePid(KDEWatcherService).value(); | - |
160 | QString processName = QLockFilePrivate::processNameByPid(pid); | - |
161 | necessary = processName.endsWith(QStringLiteralQLatin1String("indicator-application-service")); | - |
162 | necessity_checked = true; | - |
163 | } never executed: end of block | 0 |
164 | if (!necessary)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
165 | return Q_NULLPTR; never executed: return nullptr; | 0 |
166 | qreal dpr = qGuiApp->devicePixelRatio(); | - |
167 | QTemporaryFile *ret = new QTemporaryFile(TempFileTemplate, this); | - |
168 | ret->open(); | - |
169 | icon.pixmap(QSize(22 * dpr, 22 * dpr)).save(ret); | - |
170 | ret->close(); | - |
171 | return ret; never executed: return ret; | 0 |
172 | } | - |
173 | | - |
174 | QDBusMenuConnection * QDBusTrayIcon::dBusConnection() | - |
175 | { | - |
176 | if (!m_dbusConnection) { | - |
177 | m_dbusConnection = new QDBusMenuConnection(this, m_instanceId); | - |
178 | m_notifier = new QXdgNotificationInterface(XdgNotificationService, | - |
179 | XdgNotificationPath, m_dbusConnection->connection(), this); | - |
180 | connect(m_notifier, SIGNAL(NotificationClosed(uint,uint)), this, SLOT(notificationClosed(uint,uint))); | - |
181 | connect(m_notifier, SIGNAL(ActionInvoked(uint,QString)), this, SLOT(actionInvoked(uint,QString))); | - |
182 | } | - |
183 | return m_dbusConnection; | - |
184 | } | - |
185 | | - |
186 | void QDBusTrayIcon::updateIcon(const QIcon &icon) | - |
187 | { | - |
188 | m_iconName = icon.name(); | - |
189 | m_icon = icon; | - |
190 | if (m_iconName.isEmpty()) { | - |
191 | if (m_tempIcon) | - |
192 | delete m_tempIcon; | - |
193 | m_tempIcon = tempIcon(icon); | - |
194 | if (m_tempIcon) | - |
195 | m_iconName = m_tempIcon->fileName(); | - |
196 | } | - |
197 | qCDebug(qLcTray) << m_iconName << icon.availableSizes(); | - |
198 | emit iconChanged(); | - |
199 | } | - |
200 | | - |
201 | void QDBusTrayIcon::updateToolTip(const QString &tooltip) | - |
202 | { | - |
203 | qCDebug(qLcTray) << tooltip; | - |
204 | m_tooltip = tooltip; | - |
205 | emit tooltipChanged(); | - |
206 | } | - |
207 | | - |
208 | QPlatformMenu *QDBusTrayIcon::createMenu() const | - |
209 | { | - |
210 | return new QDBusPlatformMenu(); | - |
211 | } | - |
212 | | - |
213 | void QDBusTrayIcon::updateMenu(QPlatformMenu * menu) | - |
214 | { | - |
215 | qCDebug(qLcTray) << menu; | - |
216 | QDBusPlatformMenu *newMenu = qobject_cast<QDBusPlatformMenu *>(menu); | - |
217 | if (m_menu != newMenu) { | - |
218 | if (m_menu) { | - |
219 | dBusConnection()->unregisterTrayIconMenu(this); | - |
220 | delete m_menuAdaptor; | - |
221 | } | - |
222 | m_menu = newMenu; | - |
223 | m_menuAdaptor = new QDBusMenuAdaptor(m_menu); | - |
224 | | - |
225 | connect(m_menu, SIGNAL(propertiesUpdated(QDBusMenuItemList,QDBusMenuItemKeysList)), | - |
226 | m_menuAdaptor, SIGNAL(ItemsPropertiesUpdated(QDBusMenuItemList,QDBusMenuItemKeysList))); | - |
227 | connect(m_menu, SIGNAL(updated(uint,int)), | - |
228 | m_menuAdaptor, SIGNAL(LayoutUpdated(uint,int))); | - |
229 | dBusConnection()->registerTrayIconMenu(this); | - |
230 | } | - |
231 | } | - |
232 | | - |
233 | void QDBusTrayIcon::showMessage(const QString &title, const QString &msg, const QIcon &icon, | - |
234 | QPlatformSystemTrayIcon::MessageIcon iconType, int msecs) | - |
235 | { | - |
236 | m_messageTitle = title; | - |
237 | m_message = msg; | - |
238 | m_attentionIcon = icon; | - |
239 | QStringList notificationActions; | - |
240 | switch (iconType) { | - |
241 | case Information: | - |
242 | m_attentionIconName = QStringLiteral("dialog-information"); | - |
243 | break; | - |
244 | case Warning: | - |
245 | m_attentionIconName = QStringLiteral("dialog-warning"); | - |
246 | break; | - |
247 | case Critical: | - |
248 | m_attentionIconName = QStringLiteral("dialog-error"); | - |
249 | | - |
250 | | - |
251 | | - |
252 | notificationActions << DefaultAction << tr("OK"); | - |
253 | break; | - |
254 | default: | - |
255 | m_attentionIconName.clear(); | - |
256 | break; | - |
257 | } | - |
258 | if (m_attentionIconName.isEmpty()) { | - |
259 | if (m_tempAttentionIcon) | - |
260 | delete m_tempAttentionIcon; | - |
261 | m_tempAttentionIcon = tempIcon(icon); | - |
262 | if (m_tempAttentionIcon) | - |
263 | m_attentionIconName = m_tempAttentionIcon->fileName(); | - |
264 | } | - |
265 | qCDebug(qLcTray) << title << msg << | - |
266 | QPlatformSystemTrayIcon::metaObject()->enumerator( | - |
267 | QPlatformSystemTrayIcon::staticMetaObject.indexOfEnumerator("MessageIcon")).valueToKey(iconType) | - |
268 | << m_attentionIconName << msecs; | - |
269 | setStatus(QStringLiteral("NeedsAttention")); | - |
270 | m_attentionTimer.start(msecs); | - |
271 | emit tooltipChanged(); | - |
272 | emit attention(); | - |
273 | | - |
274 | | - |
275 | QVariantMap hints; | - |
276 | | - |
277 | | - |
278 | int urgency = static_cast<int>(iconType) - 1; | - |
279 | if (urgency < 0) | - |
280 | urgency = 0; | - |
281 | hints.insert(QLatin1String("urgency"), QVariant(urgency)); | - |
282 | m_notifier->notify(QCoreApplication::applicationName(), 0, | - |
283 | m_attentionIconName, title, msg, notificationActions, hints, msecs); | - |
284 | } | - |
285 | | - |
286 | void QDBusTrayIcon::actionInvoked(uint id, const QString &action) | - |
287 | { | - |
288 | qCDebug(qLcTray) << id << action; | - |
289 | emit messageClicked(); | - |
290 | } | - |
291 | | - |
292 | void QDBusTrayIcon::notificationClosed(uint id, uint reason) | - |
293 | { | - |
294 | qCDebug(qLcTray) << id << reason; | - |
295 | } | - |
296 | | - |
297 | bool QDBusTrayIcon::isSystemTrayAvailable() const | - |
298 | { | - |
299 | QDBusMenuConnection * conn = const_cast<QDBusTrayIcon *>(this)->dBusConnection(); | - |
300 | qCDebug(qLcTray) << conn->isStatusNotifierHostRegistered(); | - |
301 | return conn->isStatusNotifierHostRegistered(); | - |
302 | } | - |
303 | | - |
304 | QT_END_NAMESPACE | - |
305 | #endif //QT_NO_SYSTEMTRAYICON | - |
306 | | - |
| | |