2012-08-08 06:48:29 -04:00
|
|
|
/****************************************************************
|
|
|
|
* RetroShare is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 RetroShare Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
****************************************************************/
|
|
|
|
|
2012-08-05 16:12:55 -04:00
|
|
|
#include <QMenu>
|
2012-08-08 06:48:29 -04:00
|
|
|
#include <QToolBar>
|
|
|
|
#include <QToolButton>
|
|
|
|
|
2012-08-05 16:12:55 -04:00
|
|
|
#include "UserNotify.h"
|
|
|
|
|
|
|
|
UserNotify::UserNotify(QObject *parent) :
|
|
|
|
QObject(parent)
|
|
|
|
{
|
2012-08-08 06:48:29 -04:00
|
|
|
mMainToolButton = NULL;
|
|
|
|
mMainAction = NULL;
|
2012-08-05 16:12:55 -04:00
|
|
|
mTrayIcon = NULL;
|
|
|
|
mNotifyIcon = NULL;
|
|
|
|
newCount = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-08 06:48:29 -04:00
|
|
|
void UserNotify::initialize(QToolBar *mainToolBar, QAction *mainAction)
|
2012-08-05 16:12:55 -04:00
|
|
|
{
|
2012-08-08 06:48:29 -04:00
|
|
|
mMainAction = mainAction;
|
|
|
|
if (mMainAction) {
|
|
|
|
buttonText = mMainAction->text();
|
|
|
|
if (mainToolBar) {
|
|
|
|
mMainToolButton = dynamic_cast<QToolButton*>(mainToolBar->widgetForAction(mMainAction));
|
|
|
|
}
|
|
|
|
}
|
2012-08-05 16:12:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserNotify::createIcons(QMenu *notifyMenu)
|
|
|
|
{
|
|
|
|
#define DELETE_OBJECT(x) if (x) { delete(x); x = NULL; }
|
|
|
|
|
|
|
|
/* Create systray icons or actions */
|
|
|
|
if (notifyEnabled()) {
|
|
|
|
if (notifyCombined()) {
|
|
|
|
DELETE_OBJECT(mTrayIcon);
|
|
|
|
|
|
|
|
if (mNotifyIcon == NULL) {
|
|
|
|
mNotifyIcon = notifyMenu->addAction(getIcon(), "", this, SLOT(trayIconClicked()));
|
|
|
|
mNotifyIcon->setVisible(false);
|
|
|
|
}
|
|
|
|
} else if (mTrayIcon == NULL) {
|
|
|
|
/* Create the tray icon for messages */
|
|
|
|
mTrayIcon = new QSystemTrayIcon(this);
|
|
|
|
mTrayIcon->setIcon(getIcon());
|
|
|
|
connect(mTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DELETE_OBJECT(mTrayIcon);
|
|
|
|
DELETE_OBJECT(mNotifyIcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef DELETE_OBJECT
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserNotify::updateIcon()
|
|
|
|
{
|
|
|
|
unsigned int count = getNewCount();
|
|
|
|
|
2012-08-08 06:48:29 -04:00
|
|
|
if (mMainAction) {
|
|
|
|
mMainAction->setIcon(getMainIcon(count > 0));
|
|
|
|
mMainAction->setText((count > 0) ? QString("%1 (%2)").arg(buttonText).arg(count) : buttonText);
|
|
|
|
|
|
|
|
QFont font = mMainAction->font();
|
|
|
|
font.setBold(count > 0);
|
|
|
|
mMainAction->setFont(font);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMainToolButton) {
|
|
|
|
mMainToolButton->setStyleSheet((count > 0) ? "QToolButton { color: #E21D3A; }" : "");
|
|
|
|
|
|
|
|
QFont font = mMainToolButton->font();
|
|
|
|
font.setBold(count > 0);
|
|
|
|
mMainToolButton->setFont(font);
|
2012-08-05 16:12:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mTrayIcon) {
|
|
|
|
if (count) {
|
|
|
|
mTrayIcon->setToolTip("RetroShare\n" + getTrayMessage(count > 1).arg(count));
|
|
|
|
mTrayIcon->show();
|
|
|
|
} else {
|
|
|
|
mTrayIcon->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mNotifyIcon) {
|
|
|
|
mNotifyIcon->setData(count);
|
|
|
|
if (count) {
|
|
|
|
mNotifyIcon->setText(getNotifyMessage(count > 1).arg(count));
|
|
|
|
mNotifyIcon->setVisible(true);
|
|
|
|
} else {
|
|
|
|
mNotifyIcon->setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newCount != count) {
|
|
|
|
emit countChanged();
|
|
|
|
}
|
|
|
|
|
2012-08-08 06:48:29 -04:00
|
|
|
newCount = count;
|
2012-08-05 16:12:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString UserNotify::getTrayMessage(bool plural)
|
|
|
|
{
|
|
|
|
return plural ? tr("You have %1 new messages") : tr("You have %1 new message");
|
|
|
|
}
|
|
|
|
|
|
|
|
QString UserNotify::getNotifyMessage(bool plural)
|
|
|
|
{
|
|
|
|
return plural ? tr("%1 new messages") : tr("%1 new message");
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserNotify::trayIconClicked(QSystemTrayIcon::ActivationReason e)
|
|
|
|
{
|
|
|
|
if (e == QSystemTrayIcon::Trigger || e == QSystemTrayIcon::DoubleClick) {
|
|
|
|
iconClicked();
|
|
|
|
}
|
|
|
|
}
|