source: SMSSender/frontend/business/contactimporter/googlecontactimporter_base.cpp @ 375:c57f1c92bcb8

separation-frontend-backend
Last change on this file since 375:c57f1c92bcb8 was 375:c57f1c92bcb8, checked in by Sämy Zehnder <saemy.zehnder@…>, 7 years ago
  • Removes explicit listing of CAs wherever possible (uses the systems trusted CAs).
  • Cleans up the locales in the resource files.
File size: 6.7 KB
Line 
1/*
2 smssender - A frontend for fast and easy SMS sending over different gateways.
3 Copyright (C) 2007-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 "googlecontactimporter_base.h"
19
20#include <QRegExp>
21
22#include "common/domain/scontact.h"
23#include "common/domain/snumber.h"
24#include "common/exceptions/eexception.h"
25#include "common/network/snetworkreply.h"
26
27BaseGoogleContactImporter::BaseGoogleContactImporter(const BaseGoogleContactImporter::AuthData &authData)
28    : m_authData(authData)
29{
30}
31
32void BaseGoogleContactImporter::importContacts(QSet<SContact *> *contacts) {
33    // TODO Use OAuth2
34    requestAuthToken();
35    m_httpHelper.defaultHeaders().insert("GData-Version", "3.0");
36
37    QString nextUrl = "http://www.google.com/m8/feeds/contacts/default/full?max-results=10000";
38    do {
39        QString contactsFeed =
40                QString::fromUtf8(m_httpHelper.syncGet(nextUrl)->readAll());
41        contactsFeed = SNetworkHelper::urldecodeString(contactsFeed);
42        parseContactsFeed(contactsFeed, contacts);
43
44        QRegExp rx("<link rel='next' type='application/atom\\+xml' href='([^']+)'/>");
45        if (rx.indexIn(contactsFeed) != -1) {
46            nextUrl = rx.cap(1);
47        } else {
48            break;
49        }
50    } while (true);
51}
52
53void BaseGoogleContactImporter::parseContactsFeed(const QString& feed, QSet<SContact *> *contacts) {
54    /*<entry gd:etag=""SXszeTVSLyt7ImA9WxFbFk8ORQU."">
55        <id>http://www.google.com/m8/feeds/contacts/saemy.zehnder%40gmail.com/base/59ea8d658e87e360</id>
56        <updated>2010-07-08T21:23:38.581Z</updated>
57        <app:edited>2010-07-08T21:23:38.581Z</app:edited>
58        <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
59        <title>test2</title>
60        <link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/%avoidwarninghere%*" href="http://www.google.com/m8/feeds/photos/media/saemy.zehnder%40gmail.com/59ea8d658e87e360" gd:etag=""VxFIFH0fSit7ImA0G0AqEU8PIQEjJkMyazA.""/>
61        <link rel="self" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/saemy.zehnder%40gmail.com/thin/59ea8d658e87e360"/>
62        <link rel="edit" type="application/atom+xml" href="http://www.google.com/m8/feeds/contacts/saemy.zehnder%40gmail.com/thin/59ea8d658e87e360"/>
63        <gd:name>
64            <gd:fullName>test2</gd:fullName>
65            <gd:givenName>test2</gd:givenName>
66        </gd:name>
67        <gContact:nickname>nick1</gContact:nickname>
68        <gd:phoneNumber rel="http://schemas.google.com/g/2005#mobile">+41 079 12345 67</gd:phoneNumber>
69        <gd:phoneNumber rel="http://schemas.google.com/g/2005#mobile">+41 079 12345 68</gd:phoneNumber>
70        <gContact:groupMembershipInfo deleted="false" href="http://www.google.com/m8/feeds/groups/saemy.zehnder%40gmail.com/base/6"/>
71      </entry> */
72    QRegExp rx("<entry.*>(.*)</entry>");
73    rx.setMinimal(true);
74
75    int pos = 0;
76    while ((pos = rx.indexIn(feed, pos)) != -1) {
77        pos += rx.matchedLength();
78
79        try {
80            QString entry = rx.cap(1);
81
82            QRegExp rx2;
83            rx2.setMinimal(true);
84
85            rx2.setPattern("<gd:name>.*<gd:fullName>(.*)</gd:fullName>.*</gd:name>");
86            if (rx2.indexIn(entry) == -1) continue;
87            QString name = rx2.cap(1);
88
89            rx2.setPattern("<gd:phoneNumber rel='http://schemas.google.com/g/2005#mobile'.*>(.*)</gd:phoneNumber>");
90            if (rx2.indexIn(entry) == -1) continue;
91            SNumber number(rx2.cap(1));
92
93            if (!name.isEmpty() && number.isValid()) {
94                QScopedPointer<SContact> contact(new SContact);
95                contact->setName(name);
96                contact->setNumber(number);
97
98                rx2.setPattern("<gContact:nickname>(.*)</gContact:nickname>");
99                if ((rx2.indexIn(entry) != -1) && !rx2.cap(1).isEmpty()) {
100                    //qDebug() << rx2.cap(1);
101                    QStringList aliases;
102                    aliases.append(rx2.cap(1));
103                    contact->setAliases(aliases);
104                }
105
106#ifdef FRONTEND_GUI
107                rx2.setPattern("<link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/\\*' href='([^']+)' gd:etag='([^']+)'/>");
108                if (rx2.indexIn(entry) != -1) {
109                    QImage img = QImage::fromData(m_httpHelper.syncGet(rx2.cap(1))->readAll());
110                    if (!img.isNull()) {
111                        contact->setImage(img);
112                    }
113                }
114#endif
115
116                contacts->insert(contact.take());
117            }
118        } catch (...) {
119            // don't fail to import all contacts because of one...
120        }
121    }
122}
123
124void BaseGoogleContactImporter::requestAuthToken() {
125    QList<IParam*> posts;
126    posts.append(new SKeyValueParam("accountType", "GOOGLE"));
127    posts.append(new SKeyValueParam("Email", m_authData.username.toUtf8()));
128    posts.append(new SKeyValueParam("Passwd", m_authData.password.toUtf8()));
129    posts.append(new SKeyValueParam("service", "cp"));
130    posts.append(new SKeyValueParam("source", "gorrion-smssender-3"));
131
132    QString result;
133    try {
134        result = m_httpHelper.syncPost(QUrl::fromEncoded("https://www.google.com/accounts/ClientLogin"), posts)->readAll();
135    } catch (EException& e) {
136        result = "";
137    }
138
139    QRegExp rx("Auth=(\\S+)");
140    if (rx.indexIn(result) != -1) {
141        m_httpHelper.defaultHeaders().insert("Authorization", QString("GoogleLogin auth=" + rx.cap(1)).toUtf8());
142    } else {
143        EException("Login failed. You have probably entered incorrect userdata.")
144                .raise();
145    }
146}
147
148
149/********************************** BUILDER ***********************************/
150
151
152QString BaseGoogleContactImporter::Builder::description() const {
153    return QObject::tr("Import contacts from Google contacts.");
154}
155
156bool BaseGoogleContactImporter::Builder::isApplicable() const {
157    return true;
158}
159
160void BaseGoogleContactImporter::Builder::init() {
161    m_authData.username.clear();
162    m_authData.password.clear();
163}
164
165void BaseGoogleContactImporter::Builder::createImporters(
166        QSet<IContactImporter *> *importers) {
167    importers->insert(new BaseGoogleContactImporter(m_authData));
168}
Note: See TracBrowser for help on using the repository browser.