source: SMSSender/gateways/Post/business/account_task_sendsms.cpp @ 376:0d8179eea17f

separation-frontend-backend
Last change on this file since 376:0d8179eea17f was 376:0d8179eea17f, checked in by Sämy Zehnder <saemy.zehnder@…>, 5 years ago
  • Separates backend and frontend code in the Post gateway.
File size: 5.1 KB
Line 
1/*
2  Swiss Post gateway plugin - The smssender plugin for the Swiss Post platform.
3  Copyright (C) 2011-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 "account_tasks.h"
19
20#include <math.h>
21
22#include "common/domain/sdatatypes.h"
23#include "common/domain/snumber.h"
24#include "common/network/snetworkreply.h"
25#include "gateways/common/business/baseaccount_utils.h"
26#include "gateways/Post/business/gateway.h"
27
28namespace Post {
29
30SMSSendTask::SMSSendTask(Account *account, const QString &message,
31                         const QSet<const SContact *> &recipients)
32    : BaseSMSSendTask(account, message, recipients)
33{
34}
35
36void SMSSendTask::doSendSMS() {
37    Q_ASSERT(!message().isEmpty());
38    Q_ASSERT(!recipients().isEmpty());
39
40    qDebug() << "Start sending SMS";
41
42    /* PROGRESS:
43     *
44     * Login:    10%
45     *
46     * Send SMS: 80%
47     *   - Remove recipients: 20%
48     *   - Add recipients:    60%
49     *   - Send SMS:          20%
50     *
51     * Logout:   10%
52     *
53     */
54
55    try {
56        setProgress(0);
57        try {
58            account()->reloadOnlineParams();
59
60            CHECK_FREE_SMS_COUNT()
61
62            QStringList longSMSList = account()->divideMessage(message());
63            setProgress(10);
64
65            int percent_per_sms = 70 / (longSMSList.size() * ceil(recipients().size() / (double)MAX_RECIPIENTS_PER_SMS));
66            QSetIterator<const SContact *> i(recipients());
67            foreach (QString msgPart, longSMSList) {
68                i.toFront();
69                do {
70                    // Generate recipients string
71                    setDetails(Action::AddingRecipients);
72                    QString recipientsStr;
73                    int recipientsCount = 0;
74                    while (i.hasNext() && (recipientsCount < MAX_RECIPIENTS_PER_SMS)) {
75                        addRecipient(*i.next(), &recipientsStr);
76                        ++recipientsCount;
77                    }
78
79                    haltIfCancelRequested();
80
81                    // Send the message
82                    setDetails(Action::SendingSMS);
83                    sendLongSMS(recipientsStr, msgPart);
84
85                    setProgress(progress() + percent_per_sms);
86                } while(i.hasNext()); // While there are recipients, for which the sms has not been sent yet
87            }
88
89            setSpecialResult(srSuccess);
90        } catch (EAbortException &) {
91            // Processing of this is done later in this method
92        }
93
94        account()->reloadOnlineParams();
95
96        setProgress(90);
97    } catch (EException& e) {
98        qWarning() << "Error occured while sending sms.";
99        setSpecialResult(srError);
100        throw;
101    }
102
103    if (isCancelled()) {
104        uint oldSpecialResult = specialResult();
105        setSpecialResult(srCancelled);
106
107        if (oldSpecialResult == srSuccess) {
108            EException(tr("The sending of the sms was requested to cancel, but (a part of) the sms has already been sent."))
109                    .raise();
110        }
111    }
112
113    qDebug() << "SMS has been sent";
114}
115
116void SMSSendTask::addRecipient(const SContact& recipient, QString *recipientsStr) const {
117    if (!recipientsStr->isEmpty()) {
118        *recipientsStr += ",";
119    }
120    *recipientsStr += recipient.number().toString("aaau");
121}
122void SMSSendTask::sendLongSMS(const QString &recipientsStr, const QString &text) {
123    qDebug() << "Start of sending part of SMS";
124
125    QList<IParam*> posts;
126    posts.append(new SKeyValueParam("my_action", "sms"));
127    posts.append(new SKeyValueParam("draft_id", ""));
128    posts.append(new SKeyValueParam("later_id", ""));
129    posts.append(new SKeyValueParam("sms_number", recipientsStr.toUtf8()));
130    posts.append(new SKeyValueParam("sms_body", text.toUtf8()));
131
132    SNetworkReply reply = httpHelper()->syncPost(URL_SENDSMS, posts, SNetworkHelper::ptUrlEncoded);
133    QString response = QString::fromUtf8(reply->readAll());
134
135    // Check for error responses
136    // <script>alert('E 03-005: Es trat ein Fehler beim Versenden auf. Folgende Nachrichten konnten nicht versendet werden:\n\n- 0041791111111');</script>
137    QRegExp rx("<script>alert\\('(E \\d{2}-\\d{3}:[^']+)", Qt::CaseInsensitive);
138    if (response.indexOf(rx) > -1) {
139        EException(tr("Error during sending the sms."))
140                .addDebugInfo(rx.cap(1))
141                .raise();
142    }
143
144    qDebug() << "Part of SMS sent";
145}
146
147Account* SMSSendTask::account() const {
148    return static_cast<Account*>(BaseSMSSendTask::account());
149}
150
151SNetworkHelper *SMSSendTask::httpHelper() const {
152    return account()->httpHelper();
153}
154
155}
Note: See TracBrowser for help on using the repository browser.