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"
|
2012-10-25 14:06:33 -04:00
|
|
|
#include "rshare.h"
|
2014-07-24 16:01:12 -04:00
|
|
|
#include "gui/settings/rsharesettings.h"
|
2012-08-05 16:12:55 -04:00
|
|
|
|
|
|
|
UserNotify::UserNotify(QObject *parent) :
|
|
|
|
QObject(parent)
|
2014-07-06 06:32:13 -04:00
|
|
|
{
|
|
|
|
mMainToolButton = NULL;
|
|
|
|
mMainAction = NULL;
|
|
|
|
mListItem = NULL;
|
|
|
|
mTrayIcon = NULL;
|
|
|
|
mNotifyIcon = NULL;
|
|
|
|
mNewCount = 0;
|
2012-10-24 09:24:38 -04:00
|
|
|
mLastBlinking = false;
|
|
|
|
|
2014-07-06 06:32:13 -04:00
|
|
|
connect(rApp, SIGNAL(blink(bool)), this, SLOT(blink(bool)));
|
|
|
|
}
|
|
|
|
|
|
|
|
UserNotify::~UserNotify()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-24 16:01:12 -04:00
|
|
|
bool UserNotify::notifyEnabled()
|
|
|
|
{
|
|
|
|
QString group;
|
|
|
|
if (!hasSetting(NULL, &group) || group.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Settings->valueFromGroup(group, "TrayNotifyEnable", true).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UserNotify::notifyCombined()
|
|
|
|
{
|
|
|
|
QString group;
|
|
|
|
if (!hasSetting(NULL, &group) || group.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Settings->valueFromGroup(group, "TrayNotifyCombined", false).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UserNotify::notifyBlink()
|
|
|
|
{
|
|
|
|
QString group;
|
|
|
|
if (!hasSetting(NULL, &group) || group.isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Settings->valueFromGroup(group, "TrayNotifyBlink", false).toBool();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserNotify::setNotifyEnabled(bool enabled, bool combined, bool blink)
|
|
|
|
{
|
|
|
|
QString group;
|
|
|
|
if (!hasSetting(NULL, &group) || group.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings->beginGroup(group);
|
|
|
|
Settings->setValue("TrayNotifyEnable", enabled);
|
|
|
|
Settings->setValue("TrayNotifyCombined", combined);
|
|
|
|
Settings->setValue("TrayNotifyBlink", blink);
|
|
|
|
Settings->endGroup();
|
|
|
|
}
|
|
|
|
|
2014-07-06 06:32:13 -04:00
|
|
|
void UserNotify::initialize(QToolBar *mainToolBar, QAction *mainAction, QListWidgetItem *listItem)
|
|
|
|
{
|
|
|
|
mMainAction = mainAction;
|
|
|
|
if (mMainAction) {
|
2012-10-24 04:34:42 -04:00
|
|
|
mButtonText = mMainAction->text();
|
2012-08-08 06:48:29 -04:00
|
|
|
if (mainToolBar) {
|
2014-07-06 06:32:13 -04:00
|
|
|
mMainToolButton = dynamic_cast<QToolButton*>(mainToolBar->widgetForAction(mMainAction));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mListItem = listItem;
|
|
|
|
if (mListItem && !mMainAction) {
|
|
|
|
mButtonText = mMainAction->text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserNotify::createIcons(QMenu *notifyMenu)
|
2012-08-05 16:12:55 -04:00
|
|
|
{
|
|
|
|
#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);
|
|
|
|
}
|
2012-10-24 04:34:42 -04:00
|
|
|
} else {
|
|
|
|
DELETE_OBJECT(mNotifyIcon);
|
|
|
|
|
|
|
|
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)));
|
|
|
|
}
|
2012-08-05 16:12:55 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DELETE_OBJECT(mTrayIcon);
|
|
|
|
DELETE_OBJECT(mNotifyIcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef DELETE_OBJECT
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserNotify::updateIcon()
|
2014-07-06 06:32:13 -04:00
|
|
|
{
|
|
|
|
startUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserNotify::startUpdate()
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserNotify::update()
|
2012-08-05 16:12:55 -04:00
|
|
|
{
|
|
|
|
unsigned int count = getNewCount();
|
|
|
|
|
2012-08-08 06:48:29 -04:00
|
|
|
if (mMainAction) {
|
|
|
|
mMainAction->setIcon(getMainIcon(count > 0));
|
2012-10-24 04:34:42 -04:00
|
|
|
mMainAction->setText((count > 0) ? QString("%1 (%2)").arg(mButtonText).arg(count) : mButtonText);
|
2012-08-08 06:48:29 -04:00
|
|
|
|
|
|
|
QFont font = mMainAction->font();
|
|
|
|
font.setBold(count > 0);
|
2014-07-06 06:32:13 -04:00
|
|
|
mMainAction->setFont(font);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mListItem) {
|
|
|
|
mListItem->setIcon(getMainIcon(count > 0));
|
|
|
|
mListItem->setText((count > 0) ? QString("%1 (%2)").arg(mButtonText).arg(count) : mButtonText);
|
|
|
|
|
|
|
|
QFont font = mListItem->font();
|
|
|
|
font.setBold(count > 0);
|
|
|
|
mListItem->setFont(font);
|
|
|
|
}
|
|
|
|
if (mMainToolButton) {
|
|
|
|
mMainToolButton->setStyleSheet((count > 0) ? "QToolButton { color: #E21D3A; }" : "");
|
|
|
|
|
2012-08-08 06:48:29 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 04:34:42 -04:00
|
|
|
if (mNewCount != count) {
|
2012-08-05 16:12:55 -04:00
|
|
|
emit countChanged();
|
|
|
|
}
|
|
|
|
|
2012-10-24 04:34:42 -04:00
|
|
|
mNewCount = 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();
|
|
|
|
}
|
|
|
|
}
|
2012-10-24 09:24:38 -04:00
|
|
|
|
2012-10-25 14:06:33 -04:00
|
|
|
void UserNotify::blink(bool on)
|
2012-10-24 09:24:38 -04:00
|
|
|
{
|
|
|
|
if (mTrayIcon) {
|
2012-10-27 11:59:12 -04:00
|
|
|
bool blinking = notifyBlink();
|
|
|
|
|
2012-10-24 09:24:38 -04:00
|
|
|
if (blinking) {
|
|
|
|
/* blink icon */
|
2012-10-25 14:06:33 -04:00
|
|
|
mTrayIcon->setIcon(on ? getIcon() : QIcon());
|
2012-10-24 09:24:38 -04:00
|
|
|
} else {
|
|
|
|
if (mLastBlinking) {
|
|
|
|
/* reset icon */
|
|
|
|
mTrayIcon->setIcon(getIcon());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-27 11:59:12 -04:00
|
|
|
mLastBlinking = blinking;
|
|
|
|
}
|
2012-10-24 09:24:38 -04:00
|
|
|
}
|