source: SMSSender/gateways/Schoolnet/gui/ui/senddialogintegration.cpp @ 385:5f0e735f46e1

separation-frontend-backend
Last change on this file since 385:5f0e735f46e1 was 385:5f0e735f46e1, checked in by Sämy Zehnder <saemy.zehnder@…>, 5 years ago
  • Fixes crash when switching to another question in the Schoolnet send-integration and then providing the answer for it.
File size: 4.2 KB
Line 
1/*
2 Schoolnet gateway plugin - The smssender plugin for the Schoolnet sms platform.
3 Copyright (C) 2010-2014, gorrión. See http://smssender.gorrion.ch
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18#include "senddialogintegration.h"
19#include "senddialogintegration_p.h"
20
21#include <QGridLayout>
22#include <QSize>
23#include <QWidget>
24
25#include "gateways/Schoolnet/business/deepthought/datatypes.h"
26#include "gateways/Schoolnet/business/deepthought/questionmanager.h"
27
28namespace Schoolnet {
29namespace UI {
30
31SendDialogIntegration::SendDialogIntegration(const IGuiIntegrationHelper *guiHelper,
32                                             DeepThought::DeepThoughtHelper *deepThoughtHelper)
33    : m_deepThoughtHelper(deepThoughtHelper)
34{
35    connect(guiHelper, SIGNAL(sendingDialogCreated(ISendingDialog*)),
36            this, SLOT(installSendDialogIntegration(ISendingDialog*)),
37            Qt::DirectConnection); // Make sure we are called in GUI thread
38
39}
40
41void SendDialogIntegration::installSendDialogIntegration(ISendingDialog *dialog) {
42    // We need to be in the gui thread
43    Q_ASSERT(QThread::currentThread() == qApp->thread());
44
45    const Account &account =
46            static_cast<const Account&>(*dialog->smsSendTask()->account());
47
48    // Create the integration helper
49    new SendDialogIntegrationHelper(dialog, m_deepThoughtHelper, account); // Self destructive
50}
51
52
53/************ SendDialogIntegrationHelper ************/
54
55
56SendDialogIntegrationHelper::SendDialogIntegrationHelper(
57        ISendingDialog *sendDialog,
58        DeepThought::DeepThoughtHelper *deepThoughtHelper,
59        const Account &account)
60    : QObject(sendDialog)
61    , m_sendDialog(sendDialog)
62    , m_deepThoughtHelper(deepThoughtHelper)
63    , m_account(account)
64    , m_question(NULL)
65{
66    m_sendDialog->preventClose();
67
68    loadNextQuestion();
69    if (m_question) {
70        // Installs the question dialog.
71        QGridLayout *layout = qobject_cast<QGridLayout*>(m_sendDialog->layout());
72        Q_ASSERT(layout);
73
74        m_questionBox.reset(new DeepThought::UI::QuestionBox(m_question, sendDialog));
75        connect(m_questionBox.data(), SIGNAL(answerProvided(DeepThought::Answer*)),
76                this, SLOT(onAnswerProvided(DeepThought::Answer*)),
77                Qt::DirectConnection);
78        connect(m_questionBox.data(), SIGNAL(nextQuestionRequested()),
79                this, SLOT(loadNextQuestion()),
80                Qt::DirectConnection);
81        connect(m_questionBox.data(), SIGNAL(rejected()),
82                this, SLOT(deleteLater()),
83                Qt::DirectConnection);
84
85        layout->addWidget(m_questionBox.data(), layout->rowCount()+1, 0,
86                          layout->columnCount()-1, layout->columnCount());
87        m_sendDialog->adjustSize();
88    } else {
89        deleteLater();
90    }
91}
92
93SendDialogIntegrationHelper::~SendDialogIntegrationHelper() {
94    m_questionBox.reset();
95    m_sendDialog->adjustSize();
96
97    m_sendDialog->allowClose();
98}
99
100void SendDialogIntegrationHelper::onAnswerProvided(DeepThought::Answer *answer) {
101    m_deepThoughtHelper->proposeAnswer(&m_account, answer);
102    deleteLater();
103}
104
105void SendDialogIntegrationHelper::loadNextQuestion() {
106    // Push the current question back
107    if (m_question) {
108        m_deepThoughtHelper->questionManager().pushQuestionBack(*m_question);
109    }
110
111    // Load the next question
112    QList<DeepThought::Question *> openQuestions =
113            m_deepThoughtHelper->questionManager().openQuestions(m_account);
114    m_question = !openQuestions.empty() ? openQuestions.first() : NULL;
115    if (m_question && m_questionBox) {
116        m_questionBox->loadQuestion(m_question);
117    }
118}
119
120} // namespace UI
121} // namespace Schoolnet
Note: See TracBrowser for help on using the repository browser.