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