KNewStuff

quickquestionlistener.cpp
1 /*
2  This file is part of KNewStuffQuick.
3  SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.1-or-later
6 */
7 
8 #include "quickquestionlistener.h"
9 
10 #include "core/question.h"
11 
12 #include <QCoreApplication>
13 
14 using namespace KNewStuffQuick;
15 
16 class QuickQuestionListenerHelper {
17 public:
18  QuickQuestionListenerHelper() : q(nullptr) {}
19  ~QuickQuestionListenerHelper() { }
20  QuickQuestionListenerHelper(const QuickQuestionListenerHelper &) = delete;
21  QuickQuestionListenerHelper& operator=(const QuickQuestionListenerHelper &) = delete;
22  QuickQuestionListener *q;
23 };
24 Q_GLOBAL_STATIC(QuickQuestionListenerHelper, s_kns3_quickQuestionListener)
25 
26 class QuickQuestionListener::Private {
27 public:
28  Private() {}
29  KNSCore::Question *question = nullptr;
30 };
31 
32 QuickQuestionListener *QuickQuestionListener::instance()
33 {
34  if(!s_kns3_quickQuestionListener()->q) {
35  new QuickQuestionListener;
36  }
37  return s_kns3_quickQuestionListener()->q;
38 }
39 
40 QuickQuestionListener::QuickQuestionListener()
41  : KNSCore::QuestionListener(nullptr)
42  , d(new Private)
43 {
44  setParent(qApp);
45  s_kns3_quickQuestionListener()->q = this;
46 }
47 
48 QuickQuestionListener::~QuickQuestionListener()
49 {
50  if (d->question) {
51  d->question->setResponse(KNSCore::Question::CancelResponse);
52  }
53  delete d;
54 }
55 
56 void QuickQuestionListener::askQuestion(KNSCore::Question *question)
57 {
58  d->question = question;
59  switch(question->questionType())
60  {
61  case KNSCore::Question::SelectFromListQuestion:
62  Q_EMIT askListQuestion(question->title(), question->question(), question->list());
63  break;
64  case KNSCore::Question::ContinueCancelQuestion:
65  Q_EMIT askContinueCancelQuestion(d->question->title(), d->question->question());
66  break;
67  case KNSCore::Question::InputTextQuestion:
68  Q_EMIT askTextInputQuestion(d->question->title(), d->question->question());
69  break;
70  case KNSCore::Question::PasswordQuestion:
71  Q_EMIT askPasswordQuestion(d->question->title(), d->question->question());
72  break;
73  case KNSCore::Question::YesNoQuestion:
74  default:
75  Q_EMIT askYesNoQuestion(d->question->title(), d->question->question());
76  break;
77  }
78 }
79 
80 void KNewStuffQuick::QuickQuestionListener::passResponse(bool responseIsContinue, QString input)
81 {
82  if (d->question) {
83  if (responseIsContinue) {
84  d->question->setResponse(input);
85  switch(d->question->questionType())
86  {
87  case KNSCore::Question::ContinueCancelQuestion:
88  d->question->setResponse(KNSCore::Question::ContinueResponse);
89  break;
90  case KNSCore::Question::YesNoQuestion:
91  d->question->setResponse(KNSCore::Question::YesResponse);
92  break;
93  case KNSCore::Question::SelectFromListQuestion:
94  case KNSCore::Question::InputTextQuestion:
95  case KNSCore::Question::PasswordQuestion:
96  default:
97  d->question->setResponse(KNSCore::Question::OKResponse);
98  break;
99  }
100  } else {
101  switch(d->question->questionType())
102  {
103  case KNSCore::Question::YesNoQuestion:
104  d->question->setResponse(KNSCore::Question::NoResponse);
105  break;
106  case KNSCore::Question::SelectFromListQuestion:
107  case KNSCore::Question::InputTextQuestion:
108  case KNSCore::Question::PasswordQuestion:
109  case KNSCore::Question::ContinueCancelQuestion:
110  default:
111  d->question->setResponse(KNSCore::Question::CancelResponse);
112  break;
113  }
114  }
115  d->question = nullptr;
116  }
117 }
A way to ask a user a question from inside a GUI-less library (like KNewStuffCore) ...
Definition: question.h:41
Contains the core functionality for handling interaction with NewStuff providers. ...
void setParent(QObject *parent)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 18 2021 22:43:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.