Changeset View
Changeset View
Standalone View
Standalone View
src/qtquick/quickquestionlistener.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | This file is part of KNewStuffQuick. | ||||
3 | | ||||
4 | Copyright (c) 2016 Dan Leinir Turthra Jensen <admin@leinir.dk> | ||||
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 | using namespace KNewStuffQuick; | ||||
25 | | ||||
26 | class QuickQuestionListenerHelper { | ||||
27 | public: | ||||
28 | QuickQuestionListenerHelper() : q(nullptr) {} | ||||
29 | ~QuickQuestionListenerHelper() { delete q; } | ||||
30 | QuickQuestionListenerHelper(const QuickQuestionListenerHelper &) = delete; | ||||
31 | QuickQuestionListenerHelper& operator=(const QuickQuestionListenerHelper &) = delete; | ||||
32 | QuickQuestionListener *q; | ||||
33 | }; | ||||
34 | Q_GLOBAL_STATIC(QuickQuestionListenerHelper, s_kns3_quickQuestionListener) | ||||
35 | | ||||
36 | class QuickQuestionListener::Private { | ||||
37 | public: | ||||
38 | Private() {} | ||||
39 | KNSCore::Question* question = nullptr; | ||||
40 | }; | ||||
41 | | ||||
42 | QuickQuestionListener* QuickQuestionListener::instance() | ||||
43 | { | ||||
44 | if(!s_kns3_quickQuestionListener()->q) { | ||||
45 | new QuickQuestionListener; | ||||
46 | } | ||||
47 | return s_kns3_quickQuestionListener()->q; | ||||
48 | } | ||||
49 | | ||||
50 | QuickQuestionListener::QuickQuestionListener() | ||||
51 | : KNSCore::QuestionListener(nullptr) | ||||
52 | , d(new Private) | ||||
53 | { | ||||
54 | s_kns3_quickQuestionListener()->q = this; | ||||
55 | } | ||||
56 | | ||||
57 | QuickQuestionListener::~QuickQuestionListener() | ||||
58 | { | ||||
59 | if (d->question) { | ||||
60 | d->question->setResponse(KNSCore::Question::CancelResponse); | ||||
61 | } | ||||
62 | delete d; | ||||
63 | } | ||||
64 | | ||||
65 | void QuickQuestionListener::askQuestion(KNSCore::Question* question) | ||||
66 | { | ||||
67 | d->question = question; | ||||
68 | switch(question->questionType()) | ||||
69 | { | ||||
70 | case KNSCore::Question::SelectFromListQuestion: | ||||
71 | emit askListQuestion(question->title(), question->question(), question->list()); | ||||
72 | break; | ||||
73 | case KNSCore::Question::ContinueCancelQuestion: | ||||
74 | emit askContinueCancelQuestion(d->question->title(), d->question->question()); | ||||
75 | break; | ||||
76 | case KNSCore::Question::InputTextQuestion: | ||||
77 | emit askTextInputQuestion(d->question->title(), d->question->question()); | ||||
78 | break; | ||||
79 | case KNSCore::Question::PasswordQuestion: | ||||
80 | emit askPasswordQuestion(d->question->title(), d->question->question()); | ||||
81 | break; | ||||
82 | case KNSCore::Question::YesNoQuestion: | ||||
83 | default: | ||||
84 | emit askYesNoQuestion(d->question->title(), d->question->question()); | ||||
85 | break; | ||||
86 | } | ||||
87 | } | ||||
88 | | ||||
89 | void KNewStuffQuick::QuickQuestionListener::passResponse(bool responseIsContinue, QString input) | ||||
90 | { | ||||
91 | if (d->question) { | ||||
92 | if (responseIsContinue) { | ||||
93 | d->question->setResponse(input); | ||||
94 | switch(d->question->questionType()) | ||||
95 | { | ||||
96 | case KNSCore::Question::ContinueCancelQuestion: | ||||
97 | d->question->setResponse(KNSCore::Question::ContinueResponse); | ||||
98 | break; | ||||
99 | case KNSCore::Question::YesNoQuestion: | ||||
100 | d->question->setResponse(KNSCore::Question::YesResponse); | ||||
101 | break; | ||||
102 | case KNSCore::Question::SelectFromListQuestion: | ||||
103 | case KNSCore::Question::InputTextQuestion: | ||||
104 | case KNSCore::Question::PasswordQuestion: | ||||
105 | default: | ||||
106 | d->question->setResponse(KNSCore::Question::OKResponse); | ||||
107 | break; | ||||
108 | } | ||||
109 | } else { | ||||
110 | switch(d->question->questionType()) | ||||
111 | { | ||||
112 | case KNSCore::Question::YesNoQuestion: | ||||
113 | d->question->setResponse(KNSCore::Question::NoResponse); | ||||
114 | break; | ||||
115 | case KNSCore::Question::SelectFromListQuestion: | ||||
116 | case KNSCore::Question::InputTextQuestion: | ||||
117 | case KNSCore::Question::PasswordQuestion: | ||||
118 | case KNSCore::Question::ContinueCancelQuestion: | ||||
119 | default: | ||||
120 | d->question->setResponse(KNSCore::Question::CancelResponse); | ||||
121 | break; | ||||
122 | } | ||||
123 | } | ||||
124 | d->question = nullptr; | ||||
125 | } | ||||
126 | } |