| 1 | // -*- c-basic-offset: 2 -*- |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of Licq, an instant messaging client for UNIX. |
|---|
| 4 | * Copyright (C) 2007 Licq developers |
|---|
| 5 | * |
|---|
| 6 | * Licq is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * Licq is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with Licq; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 19 | */ |
|---|
| 20 | #include "systemtrayicon.h" |
|---|
| 21 | |
|---|
| 22 | #include "config.h" |
|---|
| 23 | |
|---|
| 24 | #ifdef USE_KDE |
|---|
| 25 | #include <KDE/KPassivePopup> |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | #include "config/general.h" |
|---|
| 29 | |
|---|
| 30 | using namespace LicqQtGui; |
|---|
| 31 | |
|---|
| 32 | SystemTrayIcon::SystemTrayIcon(QMenu* menu) |
|---|
| 33 | : DockIcon(), |
|---|
| 34 | myTimerToggle(false) |
|---|
| 35 | { |
|---|
| 36 | myTrayIcon = new QSystemTrayIcon(this); |
|---|
| 37 | myTrayIcon->setContextMenu(menu); |
|---|
| 38 | connect(myTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), |
|---|
| 39 | SLOT(trayActivated(QSystemTrayIcon::ActivationReason))); |
|---|
| 40 | connect(this, SIGNAL(newToolTip(QString)), SLOT(updateToolTip(QString))); |
|---|
| 41 | |
|---|
| 42 | updateConfig(); |
|---|
| 43 | |
|---|
| 44 | updateIconStatus(); |
|---|
| 45 | |
|---|
| 46 | myTrayIcon->show(); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | void SystemTrayIcon::updateIconMessages(int newMsg, int sysMsg) |
|---|
| 50 | { |
|---|
| 51 | static int timerId = 0; |
|---|
| 52 | |
|---|
| 53 | bool blink = (myBlink && (newMsg > 0 || sysMsg > 0)); |
|---|
| 54 | |
|---|
| 55 | if (blink && timerId == 0) |
|---|
| 56 | { |
|---|
| 57 | timerId = startTimer(500); |
|---|
| 58 | } |
|---|
| 59 | else if (!blink && timerId != 0) |
|---|
| 60 | { |
|---|
| 61 | killTimer(timerId); |
|---|
| 62 | timerId = 0; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | DockIcon::updateIconMessages(newMsg, sysMsg); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | #ifdef USE_KDE |
|---|
| 69 | void SystemTrayIcon::popupMessage(QString title, QString message, const QPixmap& icon, int timeout) |
|---|
| 70 | #else |
|---|
| 71 | void SystemTrayIcon::popupMessage(QString title, QString message, const QPixmap& /* icon */, int timeout) |
|---|
| 72 | #endif |
|---|
| 73 | { |
|---|
| 74 | #ifdef USE_KDE |
|---|
| 75 | // Escape HTML |
|---|
| 76 | title.replace('&', "&"); |
|---|
| 77 | title.replace('<', "<"); |
|---|
| 78 | title.replace('>', ">"); |
|---|
| 79 | message.replace('&', "&"); |
|---|
| 80 | message.replace('<', "<"); |
|---|
| 81 | message.replace('>', ">"); |
|---|
| 82 | KPassivePopup::message(title, message, icon, myTrayIcon, timeout); |
|---|
| 83 | #else |
|---|
| 84 | if (myTrayIcon->supportsMessages()) |
|---|
| 85 | myTrayIcon->showMessage(title, message, QSystemTrayIcon::NoIcon, timeout); |
|---|
| 86 | #endif |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | void SystemTrayIcon::updateStatusIcon() |
|---|
| 90 | { |
|---|
| 91 | DockIcon::updateStatusIcon(); |
|---|
| 92 | updateIcon(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | void SystemTrayIcon::updateEventIcon() |
|---|
| 96 | { |
|---|
| 97 | DockIcon::updateEventIcon(); |
|---|
| 98 | updateIcon(); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void SystemTrayIcon::updateConfig() |
|---|
| 102 | { |
|---|
| 103 | myBlink = Config::General::instance()->trayBlink(); |
|---|
| 104 | updateIconMessages(myNewMsg, mySysMsg); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | void SystemTrayIcon::timerEvent(QTimerEvent* /* event */) |
|---|
| 108 | { |
|---|
| 109 | myTimerToggle = !myTimerToggle; |
|---|
| 110 | updateIcon(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void SystemTrayIcon::updateIcon() |
|---|
| 114 | { |
|---|
| 115 | if (myEventIcon == NULL || |
|---|
| 116 | myEventIcon->isNull() || |
|---|
| 117 | (myBlink && myTimerToggle)) |
|---|
| 118 | myTrayIcon->setIcon(*myStatusIcon); |
|---|
| 119 | else |
|---|
| 120 | myTrayIcon->setIcon(*myEventIcon); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | void SystemTrayIcon::trayActivated(QSystemTrayIcon::ActivationReason reason) |
|---|
| 124 | { |
|---|
| 125 | switch (reason) |
|---|
| 126 | { |
|---|
| 127 | case QSystemTrayIcon::Trigger: |
|---|
| 128 | emit clicked(); |
|---|
| 129 | break; |
|---|
| 130 | |
|---|
| 131 | case QSystemTrayIcon::MiddleClick: |
|---|
| 132 | emit middleClicked(); |
|---|
| 133 | break; |
|---|
| 134 | |
|---|
| 135 | default: |
|---|
| 136 | break; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void SystemTrayIcon::updateToolTip(QString toolTip) |
|---|
| 141 | { |
|---|
| 142 | myTrayIcon->setToolTip(toolTip); |
|---|
| 143 | } |
|---|