File indexing completed on 2024-04-28 05:50:49

0001 /*
0002     SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
0003     SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
0004     SPDX-FileCopyrightText: 2021 Jonah BrĂ¼chert <jbb@kaidan.im>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "TerminalBell.h"
0010 #include "TerminalDisplay.h"
0011 
0012 #include <QTimer>
0013 
0014 #include <KLocalizedString>
0015 #include <KNotification>
0016 
0017 #include <chrono>
0018 
0019 using namespace std::literals::chrono_literals;
0020 
0021 namespace Konsole
0022 {
0023 constexpr auto MASK_TIMEOUT = 500ms;
0024 
0025 TerminalBell::TerminalBell(Enum::BellModeEnum bellMode)
0026     : _bellMode(bellMode)
0027 {
0028 }
0029 
0030 void TerminalBell::bell(TerminalDisplay *terminalDisplay, const QString &message, bool terminalHasFocus)
0031 {
0032     switch (_bellMode) {
0033     case Enum::SystemBeepBell:
0034         KNotification::beep();
0035         break;
0036     case Enum::NotifyBell: {
0037         // STABLE API:
0038         //     Please note that these event names, "BellVisible" and "BellInvisible",
0039         //     should not change and should be kept stable, because other applications
0040         //     that use this code via KPart rely on these names for notifications.
0041         KNotification *notification =
0042             KNotification::event(terminalHasFocus ? QStringLiteral("BellVisible") : QStringLiteral("BellInvisible"), message, QPixmap());
0043         auto action = notification->addDefaultAction(i18n("Show session"));
0044         connect(action, &KNotificationAction::activated, this, [notification, terminalDisplay]() {
0045             terminalDisplay->notificationClicked(notification->xdgActivationToken());
0046         });
0047     } break;
0048     case Enum::VisualBell:
0049         Q_EMIT visualBell();
0050         break;
0051     default:
0052         break;
0053     }
0054 
0055     // limit the rate at which bells can occur.
0056     // ...mainly for sound effects where rapid bells in sequence
0057     // produce a horrible noise.
0058     _bellMasked = true;
0059     QTimer::singleShot(MASK_TIMEOUT, this, [this]() {
0060         _bellMasked = false;
0061     });
0062 }
0063 
0064 void TerminalBell::setBellMode(Enum::BellModeEnum mode)
0065 {
0066     _bellMode = mode;
0067 }
0068 
0069 }
0070 
0071 #include "moc_TerminalBell.cpp"