source: SMSSender/gateways/Post/src/business/account.cpp @ 315:e6c8bb244ecd

separation-frontend-backend
Last change on this file since 315:e6c8bb244ecd was 315:e6c8bb244ecd, checked in by Sämy Zehnder <saemy.zehnder@…>, 5 years ago
  • Separates the frontend from the backend code.
  • At this point the schoolnet gateway is converted only.
  • Removes bc & da prefixes from the filenames.
File size: 4.4 KB
Line 
1/*
2  Swiss Post gateway plugin - The smssender plugin for the Swiss Post platform.
3  Copyright (C) 2011-2012, 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 "account.h"
19
20#include <algorithm>
21#include <math.h>
22
23#include <QDebug>
24#include <QDialog>
25#include <QThread>
26
27#include <business/impl/defaultlogininittask.h>
28#include <domain/snumber.h>
29#include <exceptions/stdexceptions.h>
30#include <exceptions/loginaccountexceptions.h>
31#include <network/snetworkreply.h>
32#include <persistence/impl/defaultaccountstoragehelper.h>
33#include <validation/defaultaccountvalidator.h>
34
35#include "business/account_tasks.h"
36#include "business/gateway.h"
37#include "library.h"
38
39namespace Post {
40
41Account::Account(IGateway *gateway)
42    : AbstractAccount(gateway)
43    , m_httpHelper(new SNetworkHelper)
44    , m_longSMSLength(435) // Default value from post.ch
45{
46    m_httpHelper->addTrustedCA(":/certs/VerisignCA3.crt");
47
48    connect(this, SIGNAL(initStateChanged(IAccount*)),
49            this, SLOT(onInitStateChanged()));
50}
51
52Persistence::IStorageHelper* Account::createStorageHelper(Persistence::IMainStorage *storage) {
53    return new Persistence::DefaultAccountStorageHelper(this, storage, "s2uh98dWe9D_wf98h23:*EOnqei8D");
54}
55IValidator* Account::createValidator() const {
56    return new DefaultAccountValidator(this);
57}
58
59IAccountInitTask* Account::doCreateInitTask() {
60    return new DefaultLoginInitTask(this);
61}
62IAccountLoginTask* Account::doCreateLoginTask() {
63    return new AccountLoginTask(this);
64}
65ISMSSendTask* Account::doCreateSMSSendTask(const QString &message, const QSet<SContact> &recipients) {
66    return new SMSSendTask(this, message, recipients);
67}
68
69
70SNetworkHelper *Account::httpHelper() const {
71    return m_httpHelper.data();
72}
73
74void Account::cancelSMSSending() {
75    // TODO: Implement this
76}
77
78bool Account::checkStillLoggedIn() {
79    //haltIfCancelled();
80    qDebug() << "Starting still-logged-in-check";
81
82    bool result;
83    try {
84        QString html = httpHelper()->syncGet(URL_SMSFORM)->readAll();
85        // TODO: Check if we are at https://sso.post.ch/NO_SERVICE
86        result = html.contains("Error", Qt::CaseSensitive);
87    } catch (EException &e) {
88        qWarning() << "Error while checking if we are still logged in: " + e.what();
89        qWarning() << "But ignoring it, since we just do a new login attempt.";
90        result = false;
91    }
92
93    qDebug() << "Ended still-logged-in-check";
94    return result;
95}
96
97void Account::reloadOnlineParams() {
98    QString html = httpHelper()->syncGet(URL_SMSFORM)->readAll();
99    reloadOnlineParams(html);
100}
101void Account::reloadOnlineParams(QString html) {
102    reloadLongSMSLength(html);
103    reloadFreeSMSCount(html);
104}
105void Account::reloadLongSMSLength(QString html) {
106    /*<textarea name="sms_body" style="width: 100%; height: 117px" cols=2 rows=2 maxlength="435" onKeyUp="left_char(this)"></textarea>*/
107
108    QRegExp rx("<textarea name=\"sms_body\"[^>]*maxlength=\"(\\d+)\"");
109    rx.setMinimal(true);
110    if (rx.indexIn(html) == -1) {
111        EException(tr("Could not get the longSMSLength."))
112                .raise();
113    }
114    setLongSMSLength(rx.cap(1).toInt());
115    qDebug() << "  Long SMS length: " + rx.cap(1);
116}
117
118void Account::reloadFreeSMSCount(QString html) {
119    /*<td class="label">50 von 50 TextSMS im Monat xxx</td> */
120
121    QRegExp rx("<td class=\"label\">(\\d+) von \\d+ TextSMS im Monat [a-zA-Zäöü]+</td>");
122    rx.setCaseSensitivity(Qt::CaseSensitive);
123    if (rx.indexIn(html) == -1) {
124        EException(tr("Could not get the free sms count."))
125                .addDebugInfo("html", html)
126                .raise(); // Will be made userfriendly in parsePage
127    }
128    setFreeSMSCount(rx.cap(1).toInt());
129    qDebug() << "  Free SMS count: " + rx.cap(1);
130}
131
132
133void Account::onInitStateChanged() {
134    if (!isInitialized()) {
135        // Reset the current state
136        httpHelper()->clearCookies();
137    }
138}
139
140} // namespace Post
Note: See TracBrowser for help on using the repository browser.