File indexing completed on 2024-05-19 11:43:34
0001 /* 0002 This file is part of KNewStuffCore. 0003 SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #include "widgetquestionlistener.h" 0009 0010 #include "core/question.h" 0011 0012 #include <KLocalizedString> 0013 #include <KMessageBox> 0014 #include <KPasswordDialog> 0015 #include <QInputDialog> 0016 0017 using namespace KNS3; 0018 0019 class WidgetQuestionListenerHelper 0020 { 0021 public: 0022 WidgetQuestionListenerHelper() 0023 : q(nullptr) 0024 { 0025 } 0026 ~WidgetQuestionListenerHelper() 0027 { 0028 delete q; 0029 } 0030 WidgetQuestionListenerHelper(const WidgetQuestionListenerHelper &) = delete; 0031 WidgetQuestionListenerHelper &operator=(const WidgetQuestionListenerHelper &) = delete; 0032 WidgetQuestionListener *q; 0033 }; 0034 Q_GLOBAL_STATIC(WidgetQuestionListenerHelper, s_kns3_widgetQuestionListener) 0035 0036 WidgetQuestionListener *WidgetQuestionListener::instance() 0037 { 0038 if (!s_kns3_widgetQuestionListener()->q) { 0039 new WidgetQuestionListener; 0040 } 0041 return s_kns3_widgetQuestionListener()->q; 0042 } 0043 0044 WidgetQuestionListener::WidgetQuestionListener() 0045 : KNSCore::QuestionListener(nullptr) 0046 { 0047 s_kns3_widgetQuestionListener()->q = this; 0048 } 0049 0050 WidgetQuestionListener::~WidgetQuestionListener() 0051 { 0052 } 0053 0054 void KNS3::WidgetQuestionListener::askQuestion(KNSCore::Question *question) 0055 { 0056 switch (question->questionType()) { 0057 case KNSCore::Question::SelectFromListQuestion: { 0058 bool ok = false; 0059 question->setResponse(QInputDialog::getItem(nullptr, question->title(), question->question(), question->list(), 0, false, &ok)); 0060 if (ok) { 0061 question->setResponse(KNSCore::Question::OKResponse); 0062 } else { 0063 question->setResponse(KNSCore::Question::CancelResponse); 0064 } 0065 } break; 0066 case KNSCore::Question::ContinueCancelQuestion: { 0067 KMessageBox::ButtonCode response = KMessageBox::warningContinueCancel(nullptr, question->question(), question->title()); 0068 if (response == KMessageBox::Continue) { 0069 question->setResponse(KNSCore::Question::ContinueResponse); 0070 } else { 0071 question->setResponse(KNSCore::Question::CancelResponse); 0072 } 0073 } break; 0074 case KNSCore::Question::PasswordQuestion: { 0075 KPasswordDialog dlg; 0076 dlg.setPrompt(question->question()); 0077 if (dlg.exec()) { 0078 question->setResponse(dlg.password()); 0079 question->setResponse(KNSCore::Question::ContinueResponse); 0080 } else { 0081 question->setResponse(KNSCore::Question::CancelResponse); 0082 } 0083 } break; 0084 case KNSCore::Question::YesNoQuestion: 0085 default: { 0086 KMessageBox::ButtonCode response = KMessageBox::questionTwoActions(nullptr, 0087 question->question(), 0088 question->title(), 0089 KGuiItem(i18nc("@action:button", "Yes"), QStringLiteral("dialog-ok")), 0090 KGuiItem(i18nc("@action:button", "No"), QStringLiteral("dialog-cancel"))); 0091 if (response == KMessageBox::PrimaryAction) { 0092 question->setResponse(KNSCore::Question::YesResponse); 0093 } else { 0094 question->setResponse(KNSCore::Question::NoResponse); 0095 } 0096 } break; 0097 } 0098 } 0099 0100 #include "moc_widgetquestionlistener.cpp"