KNewStuff

quickquestionlistener.cpp
1 /*
2  This file is part of KNewStuffQuick.
3 
4  Copyright (c) 2016 Dan Leinir Turthra Jensen <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "quickquestionlistener.h"
21 
22 #include "core/question.h"
23 
24 #include <QCoreApplication>
25 
26 using namespace KNewStuffQuick;
27 
28 class QuickQuestionListenerHelper {
29 public:
30  QuickQuestionListenerHelper() : q(nullptr) {}
31  ~QuickQuestionListenerHelper() { }
32  QuickQuestionListenerHelper(const QuickQuestionListenerHelper &) = delete;
33  QuickQuestionListenerHelper& operator=(const QuickQuestionListenerHelper &) = delete;
34  QuickQuestionListener *q;
35 };
36 Q_GLOBAL_STATIC(QuickQuestionListenerHelper, s_kns3_quickQuestionListener)
37 
38 class QuickQuestionListener::Private {
39 public:
40  Private() {}
41  KNSCore::Question *question = nullptr;
42 };
43 
44 QuickQuestionListener *QuickQuestionListener::instance()
45 {
46  if(!s_kns3_quickQuestionListener()->q) {
47  new QuickQuestionListener;
48  }
49  return s_kns3_quickQuestionListener()->q;
50 }
51 
52 QuickQuestionListener::QuickQuestionListener()
53  : KNSCore::QuestionListener(nullptr)
54  , d(new Private)
55 {
56  setParent(qApp);
57  s_kns3_quickQuestionListener()->q = this;
58 }
59 
60 QuickQuestionListener::~QuickQuestionListener()
61 {
62  if (d->question) {
63  d->question->setResponse(KNSCore::Question::CancelResponse);
64  }
65  delete d;
66 }
67 
68 void QuickQuestionListener::askQuestion(KNSCore::Question *question)
69 {
70  d->question = question;
71  switch(question->questionType())
72  {
73  case KNSCore::Question::SelectFromListQuestion:
74  emit askListQuestion(question->title(), question->question(), question->list());
75  break;
76  case KNSCore::Question::ContinueCancelQuestion:
77  emit askContinueCancelQuestion(d->question->title(), d->question->question());
78  break;
79  case KNSCore::Question::InputTextQuestion:
80  emit askTextInputQuestion(d->question->title(), d->question->question());
81  break;
82  case KNSCore::Question::PasswordQuestion:
83  emit askPasswordQuestion(d->question->title(), d->question->question());
84  break;
85  case KNSCore::Question::YesNoQuestion:
86  default:
87  emit askYesNoQuestion(d->question->title(), d->question->question());
88  break;
89  }
90 }
91 
92 void KNewStuffQuick::QuickQuestionListener::passResponse(bool responseIsContinue, QString input)
93 {
94  if (d->question) {
95  if (responseIsContinue) {
96  d->question->setResponse(input);
97  switch(d->question->questionType())
98  {
99  case KNSCore::Question::ContinueCancelQuestion:
100  d->question->setResponse(KNSCore::Question::ContinueResponse);
101  break;
102  case KNSCore::Question::YesNoQuestion:
103  d->question->setResponse(KNSCore::Question::YesResponse);
104  break;
105  case KNSCore::Question::SelectFromListQuestion:
106  case KNSCore::Question::InputTextQuestion:
107  case KNSCore::Question::PasswordQuestion:
108  default:
109  d->question->setResponse(KNSCore::Question::OKResponse);
110  break;
111  }
112  } else {
113  switch(d->question->questionType())
114  {
115  case KNSCore::Question::YesNoQuestion:
116  d->question->setResponse(KNSCore::Question::NoResponse);
117  break;
118  case KNSCore::Question::SelectFromListQuestion:
119  case KNSCore::Question::InputTextQuestion:
120  case KNSCore::Question::PasswordQuestion:
121  case KNSCore::Question::ContinueCancelQuestion:
122  default:
123  d->question->setResponse(KNSCore::Question::CancelResponse);
124  break;
125  }
126  }
127  d->question = nullptr;
128  }
129 }
A way to ask a user a question from inside a GUI-less library (like KNewStuffCore) ...
Definition: question.h:53
Contains the core functionality for handling interaction with NewStuff providers. ...
void setParent(QObject *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Aug 9 2020 22:43:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.