File indexing completed on 2024-11-10 07:17:53
0001 /* This file is part of the KDE project 0002 Copyright (C) 2004-2015 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 "KDbMessageHandler.h" 0021 0022 #include "KDbResult.h" 0023 0024 class Q_DECL_HIDDEN KDbMessageGuard::Private 0025 { 0026 public: 0027 Private() {} 0028 const KDbResult *result; 0029 KDbResultable *resultable; 0030 KDbMessageHandler *handler; 0031 private: 0032 Q_DISABLE_COPY(Private) 0033 }; 0034 0035 KDbMessageGuard::KDbMessageGuard(KDbResultable *resultable) 0036 : d(new Private) 0037 { 0038 d->result = nullptr; 0039 d->resultable = resultable; 0040 d->handler = nullptr; 0041 } 0042 0043 KDbMessageGuard::KDbMessageGuard(const KDbResult &result, KDbMessageHandler *handler) 0044 : d(new Private) 0045 { 0046 d->result = &result; 0047 d->resultable = nullptr; 0048 d->handler = handler; 0049 } 0050 0051 KDbMessageGuard::~KDbMessageGuard() 0052 { 0053 if (d->handler && d->result && d->result->isError()) { // variant 1 0054 d->handler->showErrorMessage(*d->result); 0055 } 0056 else if (d->resultable && d->resultable->messageHandler() && d->resultable->result().isError()){ // variant 2 0057 d->resultable->messageHandler()->showErrorMessage(d->resultable->result()); 0058 } 0059 delete d; 0060 } 0061 0062 //------------------------------------------------ 0063 0064 KDbMessageTitleSetter::KDbMessageTitleSetter(KDbResult* result, const QString& message) 0065 : m_result(result) 0066 , m_prevMsgTitle(result->messageTitle()) 0067 { 0068 m_result->setMessageTitle(message); 0069 } 0070 0071 KDbMessageTitleSetter::KDbMessageTitleSetter(KDbResultable* resultable, const QString& message) 0072 : m_result(&resultable->m_result) 0073 , m_prevMsgTitle(resultable->result().messageTitle()) 0074 { 0075 m_result->setMessageTitle(message); 0076 } 0077 0078 KDbMessageTitleSetter::~KDbMessageTitleSetter() 0079 { 0080 m_result->setMessageTitle(m_prevMsgTitle); 0081 } 0082 0083 //------------------------------------------------ 0084 0085 class Q_DECL_HIDDEN KDbGuiItem::Private 0086 { 0087 public: 0088 Private() {} 0089 bool dummy; 0090 }; 0091 0092 KDbGuiItem::KDbGuiItem() 0093 : d(new Private) 0094 { 0095 } 0096 0097 KDbGuiItem::~KDbGuiItem() 0098 { 0099 delete d; 0100 } 0101 0102 //------------------------------------------------ 0103 0104 class Q_DECL_HIDDEN KDbMessageHandler::Private 0105 { 0106 public: 0107 Private() 0108 : messageRedirection(nullptr) 0109 , enableMessages(true) 0110 { 0111 } 0112 0113 QPointer<QWidget> messageHandlerParentWidget; 0114 KDbMessageHandler *messageRedirection; 0115 bool enableMessages; 0116 private: 0117 Q_DISABLE_COPY(Private) 0118 }; 0119 0120 //------------------------------------------------ 0121 0122 KDbMessageHandler::KDbMessageHandler(QWidget *parent) 0123 : d(new Private) 0124 { 0125 d->messageHandlerParentWidget = parent; 0126 } 0127 0128 KDbMessageHandler::~KDbMessageHandler() 0129 { 0130 delete d; 0131 } 0132 0133 bool KDbMessageHandler::messagesEnabled() const 0134 { 0135 return d->enableMessages; 0136 } 0137 0138 void KDbMessageHandler::setMessagesEnabled(bool enable) 0139 { 0140 d->enableMessages = enable; 0141 } 0142 0143 KDbMessageHandler::ButtonCode KDbMessageHandler::askQuestion( 0144 KDbMessageHandler::QuestionType messageType, 0145 const QString& message, 0146 const QString &caption, 0147 KDbMessageHandler::ButtonCode defaultResult, 0148 const KDbGuiItem &buttonYes, 0149 const KDbGuiItem &buttonNo, 0150 const QString &dontShowAskAgainName, 0151 KDbMessageHandler::Options options, 0152 KDbMessageHandler* msgHandler) 0153 { 0154 if (d->enableMessages && d->messageRedirection) { 0155 return d->messageRedirection->askQuestion(messageType, message, caption, defaultResult, 0156 buttonYes, buttonNo, dontShowAskAgainName, 0157 options, msgHandler); 0158 } 0159 return defaultResult; 0160 } 0161 0162 KDbMessageHandler* KDbMessageHandler::redirection() 0163 { 0164 return d->messageRedirection; 0165 } 0166 0167 const KDbMessageHandler* KDbMessageHandler::redirection() const 0168 { 0169 return d->messageRedirection; 0170 } 0171 0172 void KDbMessageHandler::setRedirection(KDbMessageHandler *otherHandler) 0173 { 0174 d->messageRedirection = otherHandler; 0175 } 0176 0177 QWidget* KDbMessageHandler::parentWidget() 0178 { 0179 return d->messageHandlerParentWidget; 0180 }