1 /*
2     SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
3     SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
4     SPDX-FileCopyrightText: 2021 Jonah Brüchert <jbb@kaidan.im>
5 
6     SPDX-License-Identifier: GPL-2.0-or-later
7 */
8 
9 #include "TerminalBell.h"
10 
11 #include <QTimer>
12 
13 #include <KNotification>
14 
15 #include <chrono>
16 
17 using namespace std::literals::chrono_literals;
18 
19 namespace Konsole
20 {
21 constexpr auto MASK_TIMEOUT = 500ms;
22 
TerminalBell(Enum::BellModeEnum bellMode)23 TerminalBell::TerminalBell(Enum::BellModeEnum bellMode)
24     : _bellMode(bellMode)
25 {
26 }
27 
bell(const QString & message,bool terminalHasFocus)28 void TerminalBell::bell(const QString &message, bool terminalHasFocus)
29 {
30     switch (_bellMode) {
31     case Enum::SystemBeepBell:
32         KNotification::beep();
33         break;
34     case Enum::NotifyBell:
35         // STABLE API:
36         //     Please note that these event names, "BellVisible" and "BellInvisible",
37         //     should not change and should be kept stable, because other applications
38         //     that use this code via KPart rely on these names for notifications.
39         KNotification::event(terminalHasFocus ? QStringLiteral("BellVisible") : QStringLiteral("BellInvisible"), message, QPixmap());
40         break;
41     case Enum::VisualBell:
42         Q_EMIT visualBell();
43         break;
44     default:
45         break;
46     }
47 
48     // limit the rate at which bells can occur.
49     // ...mainly for sound effects where rapid bells in sequence
50     // produce a horrible noise.
51     _bellMasked = true;
52     QTimer::singleShot(MASK_TIMEOUT, this, [this]() {
53         _bellMasked = false;
54     });
55 }
56 
setBellMode(Enum::BellModeEnum mode)57 void TerminalBell::setBellMode(Enum::BellModeEnum mode)
58 {
59     _bellMode = mode;
60 }
61 
62 }
63