// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "TrayIcon.h" #include #include #include #include #include #include #include #include #if !defined(Q_OS_WIN) #include #endif #if defined(Q_OS_WIN) #include #endif namespace koutnet { namespace { struct PresenceEntry { int value; const char *iconName; }; constexpr PresenceEntry kPresences[] = { {0, "user-online"}, {1, "user-away"}, {2, "user-busy"}, {3, "user-invisible"}, }; QString presenceLabel(int presence) { switch (presence) { case 1: return i18nc("@item:inmenu presence, the user is not at the machine", "Away"); case 2: return i18nc("@item:inmenu presence, the user does not want to be disturbed", "Busy"); case 3: return i18nc("@item:inmenu presence, appear offline to peers", "Invisible"); default: return i18nc("@item:inmenu presence, reachable and saying so", "Online"); } } } static QIcon adaptiveTrayIcon(const QString &name) { const QColor windowColor = QGuiApplication::palette().color(QPalette::Active, QPalette::Window); if (windowColor.lightnessF() < 0.5) { const QString symbolic = QStringLiteral("%1-symbolic").arg(name); if (QIcon::hasThemeIcon(symbolic)) return QIcon::fromTheme(symbolic); } QIcon icon = QIcon::fromTheme(name); if (icon.isNull()) icon = QIcon(QStringLiteral(":/koutnet/app/assets/512-apps-io.github.bitzuka.koutnet.png")); return icon; } static QIcon appTrayIcon() { QIcon icon = QIcon::fromTheme(QStringLiteral("io.github.bitzuka.koutnet")); if (icon.isNull()) icon = QIcon(QStringLiteral(":/koutnet/app/assets/512-apps-io.github.bitzuka.koutnet.png")); return icon; } TrayIcon::TrayIcon(QObject *parent) : QObject(parent) { #if !defined(Q_OS_WIN) m_item = new KStatusNotifierItem(QStringLiteral("koutnet"), this); m_item->setTitle(i18nc("@title the application, as the system tray names it", "KOutNet")); m_item->setCategory(KStatusNotifierItem::Communications); m_item->setStandardActionsEnabled(false); #endif auto *menu = new QMenu(); m_showHideAction = menu->addAction(QString()); connect(m_showHideAction, &QAction::triggered, this, &TrayIcon::showHideRequested); menu->addSeparator(); m_muteAction = menu->addAction(QString()); connect(m_muteAction, &QAction::triggered, this, &TrayIcon::muteToggleRequested); m_deafenAction = menu->addAction(QString()); connect(m_deafenAction, &QAction::triggered, this, &TrayIcon::deafenToggleRequested); menu->addSeparator(); auto *presenceMenu = menu->addMenu(i18nc("@title:menu what to tell peers you are", "Presence")); presenceMenu->setIcon(adaptiveTrayIcon(QStringLiteral("user-online"))); m_presenceGroup = new QActionGroup(this); m_presenceGroup->setExclusive(true); for (const auto &entry : kPresences) { auto *action = presenceMenu->addAction(adaptiveTrayIcon(QString::fromLatin1(entry.iconName)), presenceLabel(entry.value)); action->setCheckable(true); action->setData(entry.value); m_presenceGroup->addAction(action); connect(action, &QAction::triggered, this, [this, entry] { Q_EMIT presenceRequested(entry.value); }); } menu->addSeparator(); auto *quitAction = menu->addAction(adaptiveTrayIcon(QStringLiteral("application-exit")), i18nc("@action:inmenu", "Quit")); connect(quitAction, &QAction::triggered, this, &TrayIcon::quitRequested); #if !defined(Q_OS_WIN) m_item->setContextMenu(menu); #else m_tray = new QSystemTrayIcon(this); m_tray->setIcon(appTrayIcon()); m_tray->setContextMenu(menu); connect(m_tray, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::DoubleClick) Q_EMIT showHideRequested(); }); m_tray->show(); #endif refresh(); refreshMenuLabels(); } TrayIcon::~TrayIcon() { #if defined(Q_OS_WIN) if (m_tray) m_tray->hide(); #endif } void TrayIcon::attachWindow(QWindow *window) { #if !defined(Q_OS_WIN) if (m_item) m_item->setAssociatedWindow(window); #else Q_UNUSED(window); #endif } void TrayIcon::setUnreadCount(int count) { const int clamped = count < 0 ? 0 : count; if (m_unreadCount == clamped) return; m_unreadCount = clamped; refresh(); Q_EMIT unreadCountChanged(); } void TrayIcon::setMicMuted(bool muted) { if (m_micMuted == muted) return; m_micMuted = muted; refreshMenuLabels(); Q_EMIT micMutedChanged(); } void TrayIcon::setDeafened(bool deafened) { if (m_deafened == deafened) return; m_deafened = deafened; refreshMenuLabels(); Q_EMIT deafenedChanged(); } void TrayIcon::setPresence(int presence) { if (m_presence == presence) return; m_presence = presence; refresh(); refreshMenuLabels(); Q_EMIT presenceChanged(); } void TrayIcon::setWindowVisible(bool visible) { if (m_windowVisible == visible) return; m_windowVisible = visible; refreshMenuLabels(); Q_EMIT windowVisibleChanged(); } void TrayIcon::refresh() { const QString tooltip = m_unreadCount > 0 ? i18ncp("@info:tooltip %1 is a number of unread messages", "%1 unread message", "%1 unread messages", m_unreadCount) : presenceLabel(m_presence); #if !defined(Q_OS_WIN) if (!m_item) return; m_item->setIconByName(QStringLiteral("io.github.bitzuka.koutnet")); if (QIcon::fromTheme(QStringLiteral("io.github.bitzuka.koutnet")).isNull()) m_item->setIconByPixmap(appTrayIcon()); if (m_unreadCount > 0) { m_item->setStatus(KStatusNotifierItem::NeedsAttention); m_item->setOverlayIconByName(QStringLiteral("mail-unread")); m_item->setToolTip(QStringLiteral("io.github.bitzuka.koutnet"), i18nc("@info:tooltip the system tray icon", "KOutNet"), tooltip); return; } m_item->setStatus(KStatusNotifierItem::Active); m_item->setOverlayIconByName(QString()); m_item->setToolTip(QStringLiteral("io.github.bitzuka.koutnet"), i18nc("@info:tooltip the system tray icon", "KOutNet"), tooltip); #else if (!m_tray) return; m_tray->setIcon(appTrayIcon()); m_tray->setToolTip(i18nc("@info:tooltip %1 is the current status summary", "KOutNet — %1", tooltip)); #endif } void TrayIcon::refreshMenuLabels() { if (m_showHideAction) { m_showHideAction->setText(m_windowVisible ? i18nc("@action:inmenu put the window away without quitting", "Hide the window") : i18nc("@action:inmenu bring the window back from the tray", "Show the window")); m_showHideAction->setIcon(adaptiveTrayIcon(m_windowVisible ? QStringLiteral("window-minimize") : QStringLiteral("window"))); } if (m_muteAction) { m_muteAction->setText(m_micMuted ? i18nc("@action:inmenu let your microphone be heard again", "Unmute microphone") : i18nc("@action:inmenu silence your own microphone", "Mute microphone")); m_muteAction->setIcon( adaptiveTrayIcon((m_micMuted || m_deafened) ? QStringLiteral("microphone-sensitivity-muted") : QStringLiteral("audio-input-microphone"))); m_muteAction->setEnabled(!m_deafened); } if (m_deafenAction) { m_deafenAction->setText(m_deafened ? i18nc("@action:inmenu start hearing calls again", "Undeafen") : i18nc("@action:inmenu stop hearing calls, and stop being heard", "Deafen")); m_deafenAction->setIcon(adaptiveTrayIcon(m_deafened ? QStringLiteral("audio-volume-muted") : QStringLiteral("audio-volume-high"))); } if (m_presenceGroup) { const auto actions = m_presenceGroup->actions(); for (QAction *action : actions) action->setChecked(action->data().toInt() == m_presence); } } }