File indexing completed on 2024-11-24 04:42:25

0001 /*
0002  *  messagebox.h  -  enhanced KMessageBox class
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2004-2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include <KStandardGuiItem>
0012 #include <KMessageBox>
0013 #include <KLocalizedString>
0014 
0015 /**
0016  *  @short Enhanced KMessageBox.
0017  *
0018  *  The KAMessageBox class provides an extension to KMessageBox, including the option for
0019  *  Continue/Cancel message boxes to have a default button of Cancel.
0020  *
0021  *  Note that this class is not called simply MessageBox due to a name clash on Windows.
0022  *
0023  *  @author David Jarvie <djarvie@kde.org>
0024  */
0025 class KAMessageBox
0026 {
0027 public:
0028     /** Gets the default button for the Continue/Cancel message box with the specified
0029      * "don't ask again" name.
0030      *  @param dontAskAgainName The identifier controlling whether the message box is suppressed.
0031      */
0032     static KMessageBox::ButtonCode getContinueDefault(const QString& dontAskAgainName);
0033 
0034     /** Sets the default button for the Continue/Cancel message box with the specified
0035      * "don't ask again" name.
0036      *  @param dontAskAgainName The identifier controlling whether the message box is suppressed.
0037      *  @param defaultButton The default button for the message box. Valid values are Continue or Cancel.
0038      */
0039     static void setContinueDefault(const QString& dontAskAgainName, KMessageBox::ButtonCode defaultButton);
0040 
0041     /** If there is no current setting for whether a non-Yes/No message box should be
0042      *  shown, sets it to @p defaultShow.
0043      *  If a Continue/Cancel message box has Cancel as the default button, either
0044      *  setContinueDefault() or warningContinueCancel() must have been called
0045      *  previously to set this for the specified @p dontShowAgainName value.
0046      *  @return true if @p defaultShow was written.
0047      */
0048     static bool setDefaultShouldBeShownContinue(const QString& dontShowAgainName, bool defaultShow);
0049 
0050     /** Returns whether a non-Yes/No message box should be shown.
0051      *  If the message box has Cancel as the default button, either setContinueDefault()
0052      *  or warningContinueCancel() must have been called previously to set this for the
0053      *  specified @p dontShowAgainName value.
0054      *  @param dontShowAgainName The identifier controlling whether the message box is suppressed.
0055      */
0056     static bool shouldBeShownContinue(const QString& dontShowAgainName);
0057 
0058     /** Stores whether the Yes/No message box should or should not be shown again.
0059      *  @param dontShowAgainName The identifier controlling whether the message box is suppressed.
0060      *  @param dontShow If true, the message box will be suppressed and will return @p result.
0061      *  @param result The button code to return if the message box is suppressed.
0062      */
0063     static void saveDontShowAgainYesNo(const QString& dontShowAgainName, bool dontShow = true, KMessageBox::ButtonCode result = KMessageBox::ButtonCode::SecondaryAction);
0064 
0065     /** Stores whether a non-Yes/No message box should or should not be shown again.
0066      *  If the message box has Cancel as the default button, either setContinueDefault()
0067      *  or warningContinueCancel() must have been called previously to set this for the
0068      *  specified @p dontShowAgainName value.
0069      *  @param dontShowAgainName The identifier controlling whether the message box is suppressed.
0070      *  @param dontShow If true, the message box will be suppressed and will return Continue.
0071      */
0072     static void saveDontShowAgainContinue(const QString& dontShowAgainName, bool dontShow = true);
0073 
0074     /** Same as KMessageBox::detailedError() except that it defaults to window-modal,
0075      *  not application-modal. */
0076     static void detailedError(QWidget* parent, const QString& text, const QString& details,
0077                               const QString& caption = QString(),
0078                               KMessageBox::Options options = KMessageBox::Options(KMessageBox::Notify|KMessageBox::WindowModal))
0079     { KMessageBox::detailedError(parent, text, details, caption, options); }
0080 
0081     /** Same as KMessageBox::informationList() except that it defaults to window-modal,
0082      *  not application-modal. */
0083     static void informationList(QWidget* parent, const QString& text, const QStringList& details,
0084                               const QString& caption = QString(),
0085                               const QString& dontShowAgainName = QString(),
0086                               KMessageBox::Options options = KMessageBox::Options(KMessageBox::Notify|KMessageBox::WindowModal))
0087     { KMessageBox::informationList(parent, text, details, caption, dontShowAgainName, options); }
0088 
0089     /** Same as KMessageBox::error() except that it defaults to window-modal,
0090      *  not application-modal. */
0091     static void error(QWidget* parent, const QString& text, const QString& caption = QString(),
0092                       KMessageBox::Options options = KMessageBox::Options(KMessageBox::Notify|KMessageBox::WindowModal))
0093     { KMessageBox::error(parent, text, caption, options); }
0094 
0095     /** Same as KMessageBox::information() except that it defaults to window-modal,
0096      *  not application-modal. */
0097     static void information(QWidget* parent, const QString& text, const QString& caption = QString(),
0098                             const QString& dontShowAgainName = QString(),
0099                             KMessageBox::Options options = KMessageBox::Options(KMessageBox::Notify|KMessageBox::WindowModal))
0100     { KMessageBox::information(parent, text, caption, dontShowAgainName, options); }
0101 
0102     /** Same as KMessageBox::questionYesNo() except that it defaults to window-modal,
0103      *  not application-modal. */
0104     static int questionYesNo(QWidget* parent, const QString& text, const QString& caption = QString(),
0105                              const KGuiItem& buttonYes = KGuiItem(i18n("Yes")),
0106                              const KGuiItem& buttonNo = KGuiItem(i18n("No")),
0107                              const QString& dontAskAgainName = QString(),
0108                              KMessageBox::Options options = KMessageBox::Options(KMessageBox::Notify|KMessageBox::WindowModal))
0109     { return KMessageBox::questionTwoActions(parent, text, caption, buttonYes, buttonNo, dontAskAgainName, options); }
0110 
0111     /** Same as KMessageBox::questionYesNoCancel() except that it defaults
0112      *  to window-modal, not application-modal. */
0113     static int questionYesNoCancel(QWidget* parent, const QString& text, const QString& caption = QString(),
0114                                    const KGuiItem& buttonYes = KGuiItem(i18n("Yes")),
0115                                    const KGuiItem& buttonNo = KGuiItem(i18n("No")),
0116                                    const KGuiItem& buttonCancel = KStandardGuiItem::cancel(),
0117                                    const QString& dontAskAgainName = QString(),
0118                                    KMessageBox::Options options = KMessageBox::Options(KMessageBox::Notify|KMessageBox::WindowModal))
0119     { return KMessageBox::questionTwoActionsCancel(parent, text, caption, buttonYes, buttonNo, buttonCancel, dontAskAgainName, options); }
0120     /** Same as KMessageBox::warningContinueCancel() except that the
0121      * default button is Cancel, and it defaults to window-modal, not
0122      * application-modal.
0123      * @param parent           Parent widget
0124      * @param text             Message string
0125      * @param caption          Caption (window title) of the message box
0126      * @param buttonContinue   The text for the first button (default = "Continue")
0127      * @param buttonCancel     The text for the second button (default = "Cancel")
0128      * @param dontAskAgainName If specified, the message box will only be suppressed
0129      *                         if the user chose Continue last time
0130      * @param options          Other options
0131      */
0132     static int warningCancelContinue(QWidget* parent, const QString& text, const QString& caption = QString(),
0133                                      const KGuiItem& buttonContinue = KStandardGuiItem::cont(),
0134                                      const KGuiItem& buttonCancel = KStandardGuiItem::cancel(),
0135                                      const QString& dontAskAgainName = QString(),
0136                                      KMessageBox::Options options = KMessageBox::Options(KMessageBox::Notify|KMessageBox::WindowModal))
0137     { return KMessageBox::warningContinueCancel(parent, text, caption, buttonContinue, buttonCancel, dontAskAgainName, KMessageBox::Options(options | KMessageBox::Dangerous)); }
0138 
0139     /** Same as KMessageBox::warningContinueCancel() except that it
0140      *  defaults to window-modal, not application-modal. */
0141     static int warningContinueCancel(QWidget* parent, const QString& text, const QString& caption = QString(),
0142                                      const KGuiItem& buttonContinue = KStandardGuiItem::cont(),
0143                                      const KGuiItem& buttonCancel = KStandardGuiItem::cancel(),
0144                                      const QString& dontAskAgainName = QString(),
0145                                      KMessageBox::Options options = KMessageBox::Options(KMessageBox::Notify|KMessageBox::WindowModal))
0146     { return KMessageBox::warningContinueCancel(parent, text, caption, buttonContinue, buttonCancel, dontAskAgainName, options); }
0147 
0148     /** Same as KMessageBox::warningYesNo() except that it defaults to window-modal,
0149      *  not application-modal. */
0150     static int warningYesNo(QWidget* parent, const QString& text, const QString& caption = QString(),
0151                             const KGuiItem& buttonYes = KGuiItem(i18n("Yes")),
0152                             const KGuiItem& buttonNo = KGuiItem(i18n("No")),
0153                             const QString& dontAskAgainName = QString(),
0154                             KMessageBox::Options options = KMessageBox::Options(KMessageBox::Notify|KMessageBox::Dangerous|KMessageBox::WindowModal))
0155     { return KMessageBox::questionTwoActions(parent, text, caption, buttonYes, buttonNo, dontAskAgainName, options); }
0156     /** Shortcut to represent Options(Notify | WindowModal). */
0157     static const KMessageBox::Options NoAppModal;
0158 
0159 private:
0160     static void saveDontShowAgain(const QString& dontShowAgainName, bool yesno, bool dontShow, const char* yesnoResult = nullptr);
0161     static QMap<QString, KMessageBox::ButtonCode> mContinueDefaults;
0162 };
0163 
0164 // vim: et sw=4: