source: SMSSender/gateways/CoopMobile/business/contactimporter/contactimporter.cpp @ 461:6282b93fcfca

new-gtwy-coopmobile
Last change on this file since 461:6282b93fcfca was 461:6282b93fcfca, checked in by Sämy Zehnder <saemy.zehnder@…>, 4 years ago

Implements the contact importer for the CoopMobile? gateway.

File size: 4.6 KB
Line 
1/*
2  Coop Mobile gateway plugin - The smssender plugin for the Coop Mobile platform.
3  Copyright (C) 2014-2015, 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 "contactimporter.h"
19
20#include <QRegExp>
21
22#include "common/domain/scontact.h"
23#include "common/domain/snumber.h"
24#include "common/network/snetworkreply.h"
25#include "gateways/CoopMobile/business/account.h"
26#include "gateways/CoopMobile/business/gateway.h"
27
28namespace CoopMobile {
29
30ContactImporter::ContactImporter(Account *account)
31    : m_account(account)
32{
33}
34
35util::Status ContactImporter::importContacts(QSet<SContact *> *contacts) {
36    // Fetches the contacts.
37    QString nextUrl = Gateway::URL_ADDRESS_BOOK;
38    int pageId = 1;
39    do {
40        SNetworkReply reply;
41        STATUS_RETURN_IF_ERROR(m_account->httpHelper()->syncGet(nextUrl, &reply));
42        QString html = QString::fromUtf8(reply->readAll());
43
44        STATUS_RETURN_IF_ERROR(parseContacts(html, contacts));
45        // TODO: Parse the groups.
46
47        // Fetches the url of the next page.
48        QString nextUrlTmp =
49                QString("/contacts/index/lang/de/page/%1").arg(++pageId);
50        nextUrl = html.contains(QString("<a id=\"nextpage\" href=\"%1\">").arg(nextUrlTmp))
51            ? Gateway::URL_BASE + nextUrlTmp
52            : "";
53    } while (!nextUrl.isEmpty());
54
55    return util::StatusOk();
56}
57
58
59static util::Status readField(const QString &html, const QString &name,
60                              QString *value) {
61    QRegExp rx(QString("<input type=\"text\" name=\"%1\" value=\"([^\"]+)\" />")
62               .arg(name));
63    if (rx.indexIn(html) == -1) {
64        return util::StatusParseError(
65                    QString("Couldn't find the '%1' field.").arg(name), html);
66    }
67
68    *value = rx.cap(1);
69    return util::StatusOk();
70}
71
72util::Status ContactImporter::parseContacts(const QString &html,
73                                            QSet<SContact *> *contacts) {
74    // Parses the contacts.
75    QRegExp rx("contactCheck_(\\d+)");
76    int pos = 0;
77    while ((pos = rx.indexIn(html, pos)) != -1) {
78        pos += rx.matchedLength();
79
80        QString contactId = rx.cap(1);
81        QString contactUrl =
82                QString("https://coop.nth.ch/contacts/edit/contact_id/%1/lang/de")
83                    .arg(contactId);
84
85        SNetworkReply reply;
86        STATUS_RETURN_IF_ERROR(
87                    m_account->httpHelper()->syncGet(contactUrl, &reply));
88        QString contactHtml = QString::fromUtf8(reply->readAll());
89
90        // Parses the current contact.
91        QString prename;
92        QString lastname;
93        QString numberStr;
94        STATUS_RETURN_IF_ERROR(readField(contactHtml, "first_name", &prename));
95        STATUS_RETURN_IF_ERROR(readField(contactHtml, "last_name", &lastname));
96        STATUS_RETURN_IF_ERROR(readField(contactHtml, "mobile", &numberStr));
97
98        // Creates and saves the contact.
99        QScopedPointer<SContact> contact(new SContact);
100        contact->setName(QString("%1 %2").arg(prename).arg(lastname));
101        contact->setNumber(SNumber(numberStr, "'00'ccaau"));
102
103        contacts->insert(contact.take());
104    }
105
106    return util::StatusOk();
107}
108
109/********************************** Builder ***********************************/
110
111ContactImporter::Builder::Builder(IAccountIntegrationHelper *accountHelper)
112    : m_accountHelper(accountHelper)
113{
114}
115
116QString ContactImporter::Builder::description() const {
117    if (isApplicable()) {
118        return QObject::tr("Import contacts from the CoopMobile website.");
119    } else {
120        return QObject::tr("Import contacts from the CoopMobile website. (No logged in accounts)");
121    }
122}
123
124bool ContactImporter::Builder::isApplicable() const {
125    return m_accountHelper->hasLoggedInAccounts();
126}
127
128void ContactImporter::Builder::createImporters(
129        QSet<IContactImporter *> *importers) {
130    foreach (IAccount *iaccount, m_accountHelper->loggedInAccounts()) {
131        Account *account = static_cast<Account*>(iaccount);
132
133        importers->insert(new ContactImporter(account));
134    }
135}
136
137} // namespace CoopMobile
Note: See TracBrowser for help on using the repository browser.