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