File indexing completed on 2024-05-12 16:39:37

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004-2013 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kexiguimsghandler.h"
0021 #include "kexi.h"
0022 #include <kexiutils/utils.h>
0023 
0024 #include <KDbUtils>
0025 #include <KDb>
0026 
0027 #include <KMessageBox>
0028 #include <KLocalizedString>
0029 
0030 KexiGUIMessageHandler::KexiGUIMessageHandler(QWidget *parent)
0031         : KDbMessageHandler(parent)
0032 {
0033 }
0034 
0035 KexiGUIMessageHandler::~KexiGUIMessageHandler()
0036 {
0037 }
0038 
0039 KexiGUIMessageHandler* KexiGUIMessageHandler::guiRedirection()
0040 {
0041     return dynamic_cast<KexiGUIMessageHandler*>(redirection());
0042 }
0043 
0044 void
0045 KexiGUIMessageHandler::showErrorMessage(const QString& message, const KDbResultable* resultable)
0046 {
0047     if (!messagesEnabled()) {
0048         return;
0049     }
0050     if (guiRedirection()) {
0051         guiRedirection()->showErrorMessage(message, resultable);
0052         return;
0053     }
0054     QString msg(message);
0055     if (!resultable) {
0056         showErrorMessage(msg, QString());
0057         return;
0058     }
0059     QString details;
0060     KDb::getHTMLErrorMesage(*resultable, &msg, &details);
0061     showErrorMessage(msg, details);
0062 }
0063 
0064 void
0065 KexiGUIMessageHandler::showErrorMessage(const QString &title, const QString &details)
0066 {
0067     if (!messagesEnabled()) {
0068         return;
0069     }
0070     if (guiRedirection()) {
0071         guiRedirection()->showErrorMessage(title, details);
0072         return;
0073     }
0074     showMessage(Error, title, details);
0075 }
0076 
0077 void
0078 KexiGUIMessageHandler::showSorryMessage(const QString &title, const QString &details)
0079 {
0080     if (!messagesEnabled()) {
0081         return;
0082     }
0083     if (guiRedirection()) {
0084         guiRedirection()->showSorryMessage(title, details);
0085         return;
0086     }
0087     showMessage(Sorry, title, details);
0088 }
0089 
0090 void KexiGUIMessageHandler::showErrorMessage(const QString &message, const QString &details,
0091                                              const KDbResultable *resultable)
0092 {
0093     if (!messagesEnabled()) {
0094         return;
0095     }
0096     if (guiRedirection()) {
0097         guiRedirection()->showErrorMessage(message, details, resultable);
0098         return;
0099     }
0100     QString msg(message);
0101     if (!resultable) {
0102         showErrorMessage(msg, details);
0103         return;
0104     }
0105     QString _details(details);
0106     KDb::getHTMLErrorMesage(*resultable, &msg, &_details);
0107     showErrorMessage(msg, _details);
0108 }
0109 
0110 void
0111 KexiGUIMessageHandler::showErrorMessage(Kexi::ObjectStatus *status)
0112 {
0113     if (!messagesEnabled()) {
0114         return;
0115     }
0116     if (guiRedirection()) {
0117         guiRedirection()->showErrorMessage(status);
0118         return;
0119     }
0120     showErrorMessage("", status);
0121 }
0122 
0123 void
0124 KexiGUIMessageHandler::showErrorMessage(const QString &message, Kexi::ObjectStatus *status)
0125 {
0126     if (!messagesEnabled()) {
0127         return;
0128     }
0129     if (guiRedirection()) {
0130         guiRedirection()->showErrorMessage(message, status);
0131         return;
0132     }
0133     if (status && status->error()) {
0134         QString msg(message);
0135         if (msg.isEmpty() || msg == status->message) {
0136             msg = status->message;
0137             status->message = status->description;
0138             status->description = "";
0139         }
0140         QString desc;
0141         if (!status->message.isEmpty()) {
0142             if (status->description.isEmpty()) {
0143                 desc = status->message;
0144             } else {
0145                 msg += (QString("<br><br>") + status->message);
0146                 desc = status->description;
0147             }
0148         }
0149         showErrorMessage(msg, desc, status->resultable());
0150     } else {
0151         showErrorMessage(message, QString());
0152     }
0153     if (status) {
0154         status->clearStatus();
0155     }
0156 }
0157 
0158 void
0159 KexiGUIMessageHandler::showMessage(MessageType type,
0160                                    const QString &title, const QString &details,
0161                                    const QString& dontShowAgainName)
0162 {
0163     if (!messagesEnabled()) {
0164         return;
0165     }
0166     if (guiRedirection()) {
0167         guiRedirection()->showMessage(type, title, details, dontShowAgainName);
0168         return;
0169     }
0170     //'wait' cursor is a nonsense now
0171     KexiUtils::removeWaitCursor();
0172 
0173     QString msg(title);
0174     if (title.isEmpty())
0175         msg = xi18n("Unknown error");
0176     msg = "<qt><p>" + msg + "</p>";
0177     if (!details.isEmpty()) {
0178         switch (type) {
0179         case Information:
0180             KMessageBox::information(parentWidget(), title, dontShowAgainName);
0181             break;
0182         case Error:
0183             KMessageBox::detailedError(parentWidget(), msg, details);
0184             break;
0185         case Warning:
0186             showWarningContinueMessage(title, details, dontShowAgainName);
0187             break;
0188         default: //Sorry
0189             KMessageBox::detailedSorry(parentWidget(), msg, details);
0190         }
0191     } else {
0192         KMessageBox::DialogType msgType;
0193         switch (type) {
0194         case Information: msgType = KMessageBox::Information;
0195             break;
0196         case Error: msgType = KMessageBox::Error;
0197             break;
0198         default:
0199             msgType = KMessageBox::Sorry;
0200         }
0201         KMessageBox::messageBox(parentWidget(), msgType, msg);
0202     }
0203 }
0204 
0205 void KexiGUIMessageHandler::showWarningContinueMessage(const QString &title,
0206                                                        const QString &details,
0207                                                        const QString& dontShowAgainName)
0208 {
0209     if (!messagesEnabled()) {
0210         return;
0211     }
0212     if (guiRedirection()) {
0213         guiRedirection()->showWarningContinueMessage(title, details, dontShowAgainName);
0214         return;
0215     }
0216     if (!KMessageBox::shouldBeShownContinue(dontShowAgainName))
0217         return;
0218     //! @todo what about the result?
0219     (void)KMessageBox::warningContinueCancel(parentWidget(),
0220                                        title + (details.isEmpty() ? QString() : (QString("\n") + details)),
0221                                        QString(),
0222                                        KStandardGuiItem::cont(),
0223                                        KStandardGuiItem::cancel(),
0224                                        dontShowAgainName,
0225                                        KMessageBox::Notify | KMessageBox::AllowLink);
0226 }
0227 
0228 static KGuiItem toGuiItem(const KDbGuiItem &item)
0229 {
0230     KGuiItem result;
0231     if (item.hasProperty("text")) {
0232         result.setText(item.property("text").toString());
0233     }
0234     if (item.hasProperty("icon")) {
0235         result.setIcon(item.property("icon").value<QIcon>());
0236     }
0237     if (item.hasProperty("iconName")) {
0238         result.setIconName(item.property("iconName").toString());
0239     }
0240     if (item.hasProperty("toolTip")) {
0241         result.setToolTip(item.property("toolTip").toString());
0242     }
0243     if (item.hasProperty("whatsThis")) {
0244         result.setWhatsThis(item.property("whatsThis").toString());
0245     }
0246     return result;
0247 }
0248 
0249 KDbMessageHandler::ButtonCode KexiGUIMessageHandler::askQuestion(
0250                                        KDbMessageHandler::QuestionType messageType,
0251                                        const QString &message,
0252                                        const QString &caption,
0253                                        KDbMessageHandler::ButtonCode defaultResult,
0254                                        const KDbGuiItem &buttonYes,
0255                                        const KDbGuiItem &buttonNo,
0256                                        const QString &dontShowAskAgainName,
0257                                        KDbMessageHandler::Options options,
0258                                        KDbMessageHandler* msgHandler)
0259 
0260 //                                               KMessageBox::DialogType dlgType, KMessageBox::ButtonCode defaultResult,
0261 //                                               const KGuiItem &buttonYes,
0262 //                                               const KGuiItem &buttonNo,
0263 //                                               const QString &dontShowAskAgainName,
0264 //                                               KMessageBox::Options options)
0265 {
0266     if (!messagesEnabled()) {
0267         return defaultResult;
0268     }
0269     if (redirection()) {
0270         return redirection()->askQuestion(messageType, message, caption, defaultResult,
0271                                           buttonYes, buttonNo, dontShowAskAgainName,
0272                                           options, msgHandler);
0273     }
0274     KMessageBox::Options kmsgboxOptions = 0;
0275     if (options <= (KDbMessageHandler::Notify|KDbMessageHandler::AllowLink|KDbMessageHandler::Dangerous)) {
0276         kmsgboxOptions = static_cast<KMessageBox::Options>(int(options));
0277     }
0278 
0279     if (KDbMessageHandler::WarningContinueCancel == messageType) {
0280         return static_cast<KDbMessageHandler::ButtonCode>(
0281             KMessageBox::warningContinueCancel(parentWidget(),
0282                 message, caption, toGuiItem(buttonYes), KStandardGuiItem::cancel(),
0283                 dontShowAskAgainName, kmsgboxOptions));
0284     }
0285     else {
0286         return static_cast<KDbMessageHandler::ButtonCode>(
0287                     KMessageBox::messageBox(parentWidget(),
0288                                        static_cast<KMessageBox::DialogType>(messageType),
0289                                        message, caption, toGuiItem(buttonYes),
0290                                        toGuiItem(buttonNo),
0291                                        KStandardGuiItem::cancel(),
0292                                        dontShowAskAgainName, kmsgboxOptions));
0293     }
0294 }
0295 
0296 void KexiGUIMessageHandler::showErrorMessage(KDbMessageHandler::MessageType messageType,
0297                                              const QString &message, const QString &details,
0298                                              const QString &caption)
0299 {
0300     if (!messagesEnabled()) {
0301         return;
0302     }
0303     if (redirection()) {
0304         redirection()->showErrorMessage(messageType, message, details, caption);
0305         return;
0306     }
0307     showMessage(messageType, message, details);
0308 }
0309 
0310 void KexiGUIMessageHandler::showErrorMessage(const KDbResult& result,
0311                                              KDbMessageHandler::MessageType messageType,
0312                                              const QString& message,
0313                                              const QString& caption)
0314 {
0315     if (!messagesEnabled()) {
0316         return;
0317     }
0318     if (redirection()) {
0319         redirection()->showErrorMessage(result, messageType, message, caption);
0320         return;
0321     }
0322     showMessage(messageType, result.message() + '\n' + message, QString());
0323 }