File: MessageBox.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (247 lines) | stat: -rw-r--r-- 9,524 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/***************************************************************************
          MessageBox.cpp -  threasafe wrapper for KMessageBox
                             -------------------
    begin                : Sun Apr 13 2008
    copyright            : (C) 2008 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <new>

#include <QApplication>
#include <QThread>
#include <QWidget>

#include <KMessageBox>

#include "libkwave/MessageBox.h"

//***************************************************************************
Kwave::MessageBox::MessageBox(KMessageBox::DialogType mode, QWidget *parent,
    QString message, QString caption,
    const QString &button1, const QString &button2,
    const QString &dontAskAgainName)
    :QObject(nullptr), m_semaphore(0), m_retval(-1),
     m_mode(mode), m_parent(parent), m_message(message), m_caption(caption),
     m_button1(button1), m_button2(button2),
     m_dont_ask_again_name(dontAskAgainName)
{
    if (QThread::currentThread() == QApplication::instance()->thread()) {
        // we are already in the GUI thread -> direct call
        show();
    } else {
        // schedule execution in the GUI thread ...
        Trigger *trigger = new(std::nothrow) Trigger(*this);
        Q_ASSERT(trigger);
        if (!trigger) return;
        trigger->deleteLater();

        // ... and wait
        m_semaphore.acquire();
    }
}

//***************************************************************************
int Kwave::MessageBox::retval() const
{
    return m_retval;
}

//***************************************************************************
Kwave::MessageBox::~MessageBox()
{
}

//***************************************************************************
int Kwave::MessageBox::questionYesNo(QWidget *parent,
    QString message, QString caption,
    const QString buttonYes, const QString buttonNo,
    const QString &dontAskAgainName)
{
    return Kwave::MessageBox::exec(KMessageBox::QuestionTwoActions,
        parent, message, caption, buttonYes, buttonNo,
        dontAskAgainName);
}

//***************************************************************************
int Kwave::MessageBox::questionYesNoCancel(QWidget *parent,
    QString message, QString caption,
    const QString buttonYes, const QString buttonNo,
    const QString &dontAskAgainName)
{
    return Kwave::MessageBox::exec(KMessageBox::QuestionTwoActionsCancel,
        parent, message, caption, buttonYes, buttonNo,
        dontAskAgainName);
}

//***************************************************************************
int Kwave::MessageBox::sorry(QWidget *parent,
    QString message, QString caption)
{
    return Kwave::MessageBox::exec(KMessageBox::Error,
        parent, message, caption);
}

//***************************************************************************
int Kwave::MessageBox::warningYesNo(QWidget *parent,
    QString message, QString caption,
    const QString buttonYes, const QString buttonNo,
    const QString &dontAskAgainName)
{
    return Kwave::MessageBox::exec(KMessageBox::WarningTwoActions,
        parent, message, caption, buttonYes, buttonNo,
        dontAskAgainName);
}

//***************************************************************************
int Kwave::MessageBox::warningYesNoCancel(QWidget *parent,
    QString message, QString caption,
    const QString buttonYes, const QString buttonNo,
    const QString &dontAskAgainName)
{
    return Kwave::MessageBox::exec(KMessageBox::WarningTwoActionsCancel,
        parent, message, caption, buttonYes, buttonNo,
        dontAskAgainName);
}

//***************************************************************************
int Kwave::MessageBox::warningContinueCancel(QWidget *parent,
    QString message, QString caption,
    const QString buttonContinue, const QString buttonCancel,
    const QString &dontAskAgainName)
{
    return Kwave::MessageBox::exec(KMessageBox::WarningContinueCancel,
        parent, message, caption, buttonContinue, buttonCancel,
        dontAskAgainName);
}

//***************************************************************************
int Kwave::MessageBox::error(QWidget *parent,
    QString message, QString caption)
{
    return Kwave::MessageBox::exec(KMessageBox::Error,
        parent, message, caption);
}

//***************************************************************************
int Kwave::MessageBox::exec(KMessageBox::DialogType mode, QWidget *parent,
    QString message, QString caption,
    const QString &button1, const QString &button2,
    const QString &dontAskAgainName)
{
    Kwave::MessageBox box(
        mode, parent, message, caption,
        button1, button2,
        dontAskAgainName
    );
    return box.retval();
}

//***************************************************************************
KGuiItem Kwave::MessageBox::yes()
{
    return KGuiItem(QApplication::translate("KStandardGuiItem", "&Yes"),
                    QStringLiteral("dialog-ok"));
}

//***************************************************************************
KGuiItem Kwave::MessageBox::no()
{
    return KGuiItem(QApplication::translate("KStandardGuiItem", "&No"),
                    QStringLiteral("dialog-cancel"));
}

//***************************************************************************
void Kwave::MessageBox::show()
{
    switch (m_mode) {
        case KMessageBox::QuestionTwoActions:
            m_retval = KMessageBox::questionTwoActions(m_parent,
                m_message, m_caption,
                (!m_button1.isEmpty()) ? KGuiItem(m_button1) :
                                         Kwave::MessageBox::yes(),
                (!m_button2.isEmpty()) ? KGuiItem(m_button2) :
                                         Kwave::MessageBox::no(),
                m_dont_ask_again_name);
            break;
        case KMessageBox::QuestionTwoActionsCancel:
            m_retval = KMessageBox::questionTwoActionsCancel(m_parent,
                m_message, m_caption,
                (!m_button1.isEmpty()) ? KGuiItem(m_button1) :
                                         Kwave::MessageBox::yes(),
                (!m_button2.isEmpty()) ? KGuiItem(m_button2) :
                                         Kwave::MessageBox::no(),
                KStandardGuiItem::cancel(),
                m_dont_ask_again_name);
            break;
        case KMessageBox::WarningTwoActionsCancel:
            m_retval = KMessageBox::warningTwoActionsCancel(m_parent,
                m_message, m_caption,
                (!m_button1.isEmpty()) ? KGuiItem(m_button1) :
                                         Kwave::MessageBox::yes(),
                (!m_button2.isEmpty()) ? KGuiItem(m_button2) :
                                         Kwave::MessageBox::no(),
                KStandardGuiItem::cancel(),
                m_dont_ask_again_name);
            break;
        case KMessageBox::WarningTwoActions:
            m_retval = KMessageBox::warningTwoActions(m_parent,
                m_message, m_caption,
                (!m_button1.isEmpty()) ? KGuiItem(m_button1) :
                                         Kwave::MessageBox::yes(),
                (!m_button2.isEmpty()) ? KGuiItem(m_button2) :
                                         Kwave::MessageBox::no(),
                m_dont_ask_again_name);
            break;
        case KMessageBox::WarningContinueCancel:
            m_retval = KMessageBox::warningContinueCancel(m_parent,
                m_message, m_caption,
                (!m_button1.isEmpty()) ? KGuiItem(m_button1) :
                                         KStandardGuiItem::cont(),
                (!m_button2.isEmpty()) ? KGuiItem(m_button2) :
                                         KStandardGuiItem::cancel(),
                m_dont_ask_again_name);
            break;
        case KMessageBox::Error:
            KMessageBox::error(m_parent, m_message, m_caption);
            break;
        case KMessageBox::Information:
            KMessageBox::information(m_parent, m_message, m_caption,
                                     m_dont_ask_again_name);
            break;
        default:
            qWarning("unsupported messagebox mode");
            Q_ASSERT(0);
    }

    m_semaphore.release();
}

//***************************************************************************
Kwave::MessageBox::Trigger::Trigger(Kwave::MessageBox &box)
    :QObject(nullptr), m_box(box)
{
    moveToThread(QApplication::instance()->thread());
}

//***************************************************************************
Kwave::MessageBox::Trigger::~Trigger()
{
    m_box.show();
}

//***************************************************************************
//***************************************************************************

#include "moc_MessageBox.cpp"