Commit 4a4ec171 authored by Peter Simonsson's avatar Peter Simonsson

Add unread messages count to taskbar

parent 9e1d628f
......@@ -26,6 +26,7 @@ set(core_SRCS
connectionsettings.cpp
identity.cpp
identitydialog.cpp
taskbarupdater.cpp
#=== GUI
urlcatcher.cpp
......@@ -117,6 +118,7 @@ ki18n_wrap_ui(config_SRCS
config/theme_configui.ui
config/warnings_configui.ui
config/watchednicknames_configui.ui
config/generalnotifications_config.ui
)
#=== Viewer
......
......@@ -26,6 +26,7 @@
#include "notificationhandler.h"
#include "awaymanager.h"
#include "konversation_log.h"
#include "taskbarupdater.h"
#include <KIO/JobUiDelegate>
#include <KIO/OpenUrlJob>
......@@ -176,6 +177,8 @@ QT_WARNING_POP
Preferences::self();
readOptions();
m_taskbarUpdater = new TaskbarUpdater(this);
// Images object providing LEDs, NickIcons
m_images = new Images();
......
......@@ -31,6 +31,7 @@ class Images;
class ServerGroupSettings;
class QStandardItemModel;
class QCommandLineParser;
class TaskbarUpdater;
class KTextEdit;
......@@ -134,6 +135,8 @@ class Application : public QApplication
void setCommandLineParser(QCommandLineParser *parser) { m_commandLineParser = parser; }
QCommandLineParser *commandLineParser() const { return m_commandLineParser; }
TaskbarUpdater *taskbarUpdater() const { return m_taskbarUpdater; }
Q_SIGNALS:
void serverGroupsChanged(const Konversation::ServerGroupSettingsPtr serverGroup);
void appearanceChanged(); // FIXME TODO: Rather than relying on this catch-all, consumers should be rewritten to catch appropriate QEvents.
......@@ -203,6 +206,8 @@ QT_WARNING_POP
QCommandLineParser *m_commandLineParser;
QStringList m_restartArguments;
TaskbarUpdater *m_taskbarUpdater;
Q_DISABLE_COPY(Application)
};
......
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>GeneralNotifications_Config</class>
<widget class="QWidget" name="GeneralNotifications_Config">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>351</width>
<height>167</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QCheckBox" name="kcfg_ShowUnreadOnTaskbar">
<property name="text">
<string>Show number of unread messages on the taskbar</string>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
......@@ -1024,4 +1024,9 @@
<label></label>
</entry>
</group>
<group name="TaskbarNotifications">
<entry key="ShowUnreadOnTaskbar" type="Bool">
<default>false</default>
</entry>
</group>
</kcfg>
......@@ -29,6 +29,7 @@
#include "ui_watchednicknames_configui.h"
#include "ui_tabnotifications_config.h"
#include "konversation_log.h"
#include "ui_generalnotifications_config.h"
#include <config-konversation.h>
......@@ -150,6 +151,12 @@ KonviSettingsDialog::KonviSettingsDialog( QWidget *parent) :
m_confDCCWdg = new DCC_Config(this, "DCC");
konviAddSubPage(behaviorGroup, m_confDCCWdg, i18nc("@title:tab", "DCC"), QStringLiteral("arrow-right-double"));
//Notifications/General
Ui::GeneralNotifications_Config confGeneralNotifications;
w = new QWidget();
confGeneralNotifications.setupUi(w);
konviAddSubPage(notificationGroup, w, i18n("General Notifications"), QStringLiteral("preferences-desktop-notification"));
//Notifications/Tab Bar
Ui::TabNotifications_Config confTabNotifications;
w = new QWidget();
......
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2018-2020 Laurent Montel <montel@kde.org>
SPDX-FileCopyrightText: 2020-2021 Peter Simonsson <peter.simonsson@gmail.com>
*/
#include "taskbarupdater.h"
#include "konversation_log.h"
#include "preferences.h"
#include <QApplication>
#include <QDBusMessage>
#include <QDBusConnection>
#include <QDBusServiceWatcher>
#include <QDBusPendingCall>
#include <QDBusPendingReply>
#include <QDBusConnectionInterface>
TaskbarUpdater::TaskbarUpdater(QObject *parent) :
QObject(parent),
m_unityServiceWatcher(new QDBusServiceWatcher(this)),
m_unityServiceAvailable(false),
m_unread(0)
{
initUnityService();
}
void TaskbarUpdater::updateUnread()
{
if(!m_unityServiceAvailable)
return;
const QString launcherId = qApp->desktopFileName() + QLatin1String(".desktop");
bool showCount = m_unread > 0 && Preferences::self()->showUnreadOnTaskbar();
const QVariantMap properties{
{QStringLiteral("count-visible"), showCount},
{QStringLiteral("count"), m_unread}
};
QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/org/kde/konversation/UnityLauncher"),
QStringLiteral("com.canonical.Unity.LauncherEntry"),
QStringLiteral("Update"));
message.setArguments({launcherId, properties});
QDBusConnection::sessionBus().send(message);
}
void TaskbarUpdater::initUnityService()
{
m_unityServiceWatcher->setConnection(QDBusConnection::sessionBus());
m_unityServiceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration | QDBusServiceWatcher::WatchForRegistration);
m_unityServiceWatcher->addWatchedService(QStringLiteral("com.canonical.Unity"));
connect(m_unityServiceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this](const QString &service) {
Q_UNUSED(service)
m_unityServiceAvailable = true;
updateUnread();
});
connect(m_unityServiceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this](const QString &service) {
Q_UNUSED(service)
m_unityServiceAvailable = false;
});
// QDBusConnectionInterface::isServiceRegistered blocks
QDBusPendingCall listNamesCall = QDBusConnection::sessionBus().interface()->asyncCall(QStringLiteral("ListNames"));
auto callWatcher = new QDBusPendingCallWatcher(listNamesCall, this);
connect(callWatcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<QStringList> reply = *watcher;
watcher->deleteLater();
if (reply.isError()) {
qCWarning(KONVERSATION_LOG) << "DBus reported an error " << reply.error().message();
return;
}
const QStringList &services = reply.value();
m_unityServiceAvailable = services.contains(QLatin1String("com.canonical.Unity"));
if (m_unityServiceAvailable) {
updateUnread();
}
});
}
void TaskbarUpdater::increaseUnread()
{
m_unread++;
updateUnread();
}
void TaskbarUpdater::decreaseUnread(uint count)
{
m_unread -= count;
updateUnread();
}
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2020-2021 Peter Simonsson <peter.simonsson@gmail.com>
*/
#ifndef TASKBARUPDATER_H
#define TASKBARUPDATER_H
#include <QObject>
class QDBusServiceWatcher;
/**
* Uses the com.canonical.Unity DBUS service to set the number of unread
* messages in the taskbar.
*/
class TaskbarUpdater : public QObject
{
Q_OBJECT
public:
TaskbarUpdater(QObject *parent);
public Q_SLOTS:
void increaseUnread();
void decreaseUnread(uint count);
private Q_SLOTS:
void updateUnread();
private:
void initUnityService();
QDBusServiceWatcher *m_unityServiceWatcher;
bool m_unityServiceAvailable;
uint m_unread;
};
#endif // TASKBARUPDATER_H
......@@ -43,6 +43,7 @@ ChatWindow::ChatWindow(QWidget* parent) : QWidget(parent)
m_notificationsEnabled = true;
m_channelEncodingSupported = false;
m_currentTabNotify = Konversation::tnfNone;
m_unread = 0;
}
ChatWindow::~ChatWindow()
......@@ -283,12 +284,22 @@ void ChatWindow::append(const QString& nickname, const QString& message, const Q
{
if(!textView) return;
textView->append(nickname, message, messageTags, label);
if(!isVisible() || !Application::instance()->activeWindow())
{
increaseUnread();
}
}
void ChatWindow::appendQuery(const QString& nickname, const QString& message, const QHash<QString, QString> &messageTags, bool inChannel)
{
if(!textView) return ;
textView->appendQuery(nickname, message, messageTags, inChannel);
if(!isVisible() || !Application::instance()->activeWindow())
{
increaseUnread();
}
}
void ChatWindow::appendAction(const QString& nickname, const QString& message, const QHash<QString, QString> &messageTags)
......@@ -299,6 +310,11 @@ void ChatWindow::appendAction(const QString& nickname, const QString& message, c
textView->appendQueryAction(nickname, message, messageTags);
else
textView->appendChannelAction(nickname, message, messageTags);
if(!isVisible() || !Application::instance()->activeWindow())
{
increaseUnread();
}
}
void ChatWindow::appendServerMessage(const QString& type, const QString& message, const QHash<QString, QString> &messageTags, bool parseURL)
......@@ -689,6 +705,7 @@ void ChatWindow::activateTabNotification(Konversation::TabNotifyType type)
void ChatWindow::resetTabNotification()
{
m_currentTabNotify = Konversation::tnfNone;
resetUnread();
}
void ChatWindow::msgHelper(const QString& recipient, const QString& message)
......@@ -759,3 +776,18 @@ void ChatWindow::activateView()
emit showView(this);
}
void ChatWindow::increaseUnread()
{
m_unread++;
emit unreadIncreased();
}
void ChatWindow::resetUnread()
{
if (!isVisible())
return;
emit unreadReset(m_unread);
m_unread = 0;
}
......@@ -151,6 +151,9 @@ class ChatWindow : public QWidget
void closing(ChatWindow* myself);
void showView(ChatWindow* myself);
void unreadIncreased();
void unreadReset(uint oldCount);
public Q_SLOTS:
virtual void updateAppearance();
......@@ -172,6 +175,8 @@ class ChatWindow : public QWidget
void activateTabNotification(Konversation::TabNotifyType type);
void resetTabNotification();
void resetUnread();
protected Q_SLOTS:
///Used to disable functions when not connected
virtual void serverOnline(bool online);
......@@ -196,6 +201,8 @@ class ChatWindow : public QWidget
int spacing();
int margin();
void increaseUnread();
protected:
bool firstLog;
QString name;
......@@ -224,6 +231,8 @@ class ChatWindow : public QWidget
bool m_recreationScheduled;
uint m_unread;
Q_DISABLE_COPY(ChatWindow)
};
......
......@@ -34,6 +34,7 @@
#include "viewtree.h"
#include "viewspringloader.h"
#include "konversation_log.h"
#include "taskbarupdater.h"
#include <KMessageBox>
#include <KIO/OpenUrlJob>
......@@ -1399,6 +1400,15 @@ void ViewContainer::addView(ChatWindow* view, const QString& label, bool weiniti
connect(view, &ChatWindow::closing, this, &ViewContainer::cleanupAfterClose);
connect(view, &ChatWindow::showView, this, &ViewContainer::showView);
connect(view, &ChatWindow::unreadIncreased,
Application::instance()->taskbarUpdater(),
&TaskbarUpdater::increaseUnread);
connect(view, &ChatWindow::unreadReset,
Application::instance()->taskbarUpdater(),
&TaskbarUpdater::decreaseUnread);
connect(Application::instance()->getMainWindow(), &MainWindow::endNotification,
view, &ChatWindow::resetUnread);
switch (view->getType())
{
case ChatWindow::Channel:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment