source: SMSSender/gateways/CoopMobile/business/contactimporter/contactimporter.cpp @ 454:586c0997938b

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

Initial commit of the CoopMobile? gateway. It is not completely tested yet.

File size: 3.9 KB
Line 
1/*
2  Coop Mobile gateway plugin - The smssender plugin for the Coop Mobile 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 "contactimporter.h"
19
20#include "common/domain/scontact.h"
21#include "common/domain/snumber.h"
22#include "common/network/snetworkreply.h"
23#include "gateways/CoopMobile/business/account.h"
24#include "gateways/CoopMobile/business/gateway.h"
25
26namespace CoopMobile {
27
28const QString ContactImporter::URL_EXPORT_CONTACTS =
29        "https://organizer.sso.post.ch/addresses/adr_csv_export_action.html";
30
31ContactImporter::ContactImporter(Account *account)
32    : m_account(account)
33{
34}
35
36util::Status ContactImporter::importContacts(QSet<SContact *> *contacts) {
37    // Fetch the contacts
38    SNetworkReply reply;
39    STATUS_RETURN_IF_ERROR(
40                m_account->httpHelper()->syncGet(URL_EXPORT_CONTACTS, &reply));
41    QString csv = reply->readAll();
42    csv.replace("\r", "");
43
44    // Split csv into rows
45    QStringList rows = csv.split('\n', QString::SkipEmptyParts);
46
47    // Get the interresting columns
48    QString columnDescriptorRow = rows.takeFirst();
49    QStringList columnDescriptor = splitIntoCells(columnDescriptorRow);
50    int prenameCol = columnDescriptor.indexOf(QRegExp("^Vorname$"));
51    int lastnameCol = columnDescriptor.indexOf(QRegExp("^Nachname$"));
52    int mobileCol = columnDescriptor.indexOf(QRegExp("^Mobil-Nummer$"));
53    int mobileCol2 = columnDescriptor.indexOf(QRegExp("^Mobil-Nummer at$"));
54
55
56    // Parse the contacts
57    foreach (QString row, rows) {
58        QStringList contactData = splitIntoCells(row);
59
60        QString name = QString("%1 %2")
61                .arg(contactData.value(prenameCol))
62                .arg(contactData.value(lastnameCol))
63                .trimmed();
64        SNumber number(contactData.value(mobileCol));
65        if (!number.isValid()) {
66            number = SNumber(contactData.value(mobileCol2));
67        }
68
69        if (!name.isEmpty() && number.isValid()) {
70            QScopedPointer<SContact> contact(new SContact);
71            contact->setName(name);
72            contact->setNumber(number);
73
74            contacts->insert(contact.take());
75        }
76    }
77
78    return util::StatusOk();
79}
80
81QStringList ContactImporter::splitIntoCells(QString csvRow) {
82    csvRow = csvRow.mid(1, csvRow.length()-2); // Remove first and last "
83    return csvRow.split(QRegExp("\";\""), QString::KeepEmptyParts);
84}
85
86/********************************** Builder ***********************************/
87
88ContactImporter::Builder::Builder(IAccountIntegrationHelper *accountHelper)
89    : m_accountHelper(accountHelper)
90{
91}
92
93QString ContactImporter::Builder::description() const {
94    if (isApplicable()) {
95        return QObject::tr("Import contacts from the Post website.");
96    } else {
97        return QObject::tr("Import contacts from the Post website. (No logged in accounts)");
98    }
99}
100
101bool ContactImporter::Builder::isApplicable() const {
102    return m_accountHelper->hasLoggedInAccounts();
103}
104
105void ContactImporter::Builder::createImporters(
106        QSet<IContactImporter *> *importers) {
107    foreach (IAccount *iaccount, m_accountHelper->loggedInAccounts()) {
108        Account *account = static_cast<Account*>(iaccount);
109
110        importers->insert(new ContactImporter(account));
111    }
112}
113
114} // namespace CoopMobile
Note: See TracBrowser for help on using the repository browser.