importwindowcontact.cpp 16.7 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
   Copyright (C) 2020 Laurent Montel <montel@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "importwindowcontact.h"
#include "importexportwindowscontactplugin_debug.h"
#include <KLocalizedString>
#include <KMessageBox>
#include <QDomDocument>
#include <QFile>

27 28
//https://docs.microsoft.com/en-us/previous-versions//ms735869(v=vs.85)

Laurent Montel's avatar
Laurent Montel committed
29 30 31 32 33 34 35 36 37 38
ImportWindowContact::ImportWindowContact()
{

}

ImportWindowContact::~ImportWindowContact()
{

}

Laurent Montel's avatar
Laurent Montel committed
39
KContacts::Addressee::List ImportWindowContact::importFile(const QString &fileName)
Laurent Montel's avatar
Laurent Montel committed
40
{
Laurent Montel's avatar
Laurent Montel committed
41
    KContacts::Addressee::List lst;
Laurent Montel's avatar
Laurent Montel committed
42 43
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
44
        if (!mAutoTest) {
Laurent Montel's avatar
Laurent Montel committed
45 46 47 48 49
            const QString msg = i18n("<qt>Unable to open <b>%1</b> for reading.</qt>", fileName);
            KMessageBox::error(mParentWidget, msg);
        } else {
            qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "Impossible to open file: " << fileName;
        }
Laurent Montel's avatar
Laurent Montel committed
50
        return lst;
Laurent Montel's avatar
Laurent Montel committed
51 52 53 54 55 56 57
    }
    QDomDocument doc;
    if (loadDomElement(doc, &file)) {
        QDomElement list = doc.documentElement();
        if (list.isNull()) {
            qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "No list defined in file";
        } else {
58 59 60 61
            KContacts::Addressee contact;
            if (mAutoTest) {
                contact.setUid(QStringLiteral("foo"));
            }
Laurent Montel's avatar
Laurent Montel committed
62 63
            for (QDomElement e = list.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
                const QString tag = e.tagName();
64 65 66 67 68 69 70 71 72 73 74 75 76
                if (tag == QLatin1String("c:EmailAddressCollection")) {
                    KContacts::Email::List lstEmails;
                    for (QDomElement emails = e.firstChildElement(); !emails.isNull(); emails = emails.nextSiblingElement()) {
                        const QString emailsTag = emails.tagName();
                        if (emailsTag == QLatin1String("c:EmailAddress")) {
                            KContacts::Email email;
                            for (QDomElement addresses = emails.firstChildElement(); !addresses.isNull(); addresses = addresses.nextSiblingElement()) {
                                const QString addressesTag = addresses.tagName();
                                if (addressesTag == QLatin1String("c:Type")) {

                                } else if (addressesTag == QLatin1String("c:Address")) {
                                    email.setEmail(addresses.text());
                                } else {
77
                                    qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " address tag not supported yet " << addressesTag;
78 79
                                }
                            }
80 81 82
                            if (email.isValid()) {
                                lstEmails << email;
                            }
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
                        }
                        contact.setEmailList(lstEmails);
                    }
                } else if (tag == QLatin1String("c:NameCollection")) {
                    for (QDomElement name = e.firstChildElement(); !name.isNull(); name = name.nextSiblingElement()) {
                        const QString nameTag = name.tagName();
                        if (nameTag == QLatin1String("c:Name")) {
                            for (QDomElement nameInfo = name.firstChildElement(); !nameInfo.isNull(); nameInfo = nameInfo.nextSiblingElement()) {
                                const QString nameInfoTag = nameInfo.tagName();
                                if (nameInfoTag == QLatin1String("c:FormattedName")) {
                                    contact.setName(nameInfo.text());
                                } else if (nameInfoTag == QLatin1String("c:GivenName")) {
                                    contact.setGivenName(nameInfo.text());
                                } else if (nameInfoTag == QLatin1String("c:FamilyName")) {
                                    contact.setFamilyName(nameInfo.text());
                                } else if (nameInfoTag == QLatin1String("c:FormattedName")) {
                                    contact.setFormattedName(nameInfo.text());
100 101 102 103 104 105 106 107
                                } else if (nameInfoTag == QLatin1String("c:Title")) {
                                    contact.setTitle(nameInfo.text());
                                } else if (nameInfoTag == QLatin1String("c:NickName")) {
                                    contact.setNickName(nameInfo.text());
                                } else if (nameInfoTag == QLatin1String("c:Prefix")) {
                                    contact.setPrefix(nameInfo.text());
                                } else if (nameInfoTag == QLatin1String("c:Suffix")) {
                                    contact.setSuffix(nameInfo.text());
108
                                } else {
109
                                    qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " name tag not supported yet " << nameInfoTag;
110 111 112
                                }
                            }
                        } else {
113
                            qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " name tag unknown:" << nameTag;
114 115
                        }
                    }
116
                } else if (tag == QLatin1String("c:PhoneNumberCollection")) {
117 118 119 120 121 122 123 124 125
                    for (QDomElement number = e.firstChildElement(); !number.isNull(); number = number.nextSiblingElement()) {
                        const QString numberTag = number.tagName();
                        if (numberTag == QLatin1String("c:PhoneNumber")) {
                            KContacts::PhoneNumber phoneNumber;
                            for (QDomElement numberInfo = number.firstChildElement(); !numberInfo.isNull(); numberInfo = numberInfo.nextSiblingElement()) {
                                const QString numberInfoTag = numberInfo.tagName();
                                if (numberInfoTag == QLatin1String("c:Number")) {
                                    phoneNumber.setNumber(numberInfo.text());
                                } else {
126
                                    qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " number info tag not supported yet:" << numberInfoTag;
127 128 129 130 131 132
                                }
                            }
                            if (!phoneNumber.isEmpty()) {
                                contact.insertPhoneNumber(phoneNumber);
                            }
                        } else {
133
                            qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " number tag unknown:" << numberTag;
134 135 136
                        }
                    }
                } else if (tag == QLatin1String("c:IMAddressCollection")) {
137 138 139 140 141 142 143 144 145
                    for (QDomElement im = e.firstChildElement(); !im.isNull(); im = im.nextSiblingElement()) {
                        const QString imTag = im.tagName();
                        if (imTag == QLatin1String("c:IMAddress")) {
                            KContacts::Impp impp;
                            for (QDomElement imInfo = im.firstChildElement(); !imInfo.isNull(); imInfo = imInfo.nextSiblingElement()) {
                                const QString imInfoTag = imInfo.tagName();
                                if (imInfoTag == QLatin1String("c:Value")) {
                                    impp.setAddress(QUrl(imInfo.text()));
                                } else {
146
                                    qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " im info tag not supported yet " << imInfoTag;
147 148 149 150 151 152
                                }
                            }
                            if (impp.isValid()) {
                                contact.insertImpp(impp);
                            }
                        } else {
153
                            qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " im tag unknown:" << imTag;
154 155
                        }
                    }
156
                } else if (tag == QLatin1String("c:PhotoCollection")) {
157 158 159 160 161 162 163 164
                    for (QDomElement photo = e.firstChildElement(); !photo.isNull(); photo = photo.nextSiblingElement()) {
                        const QString photoTag = photo.tagName();
                        if (photoTag == QLatin1String("c:Photo")) {
                            KContacts::Picture picture;
                            for (QDomElement photoInfo = photo.firstChildElement(); !photoInfo.isNull(); photoInfo = photoInfo.nextSiblingElement()) {
                                const QString photoInfoTag = photoInfo.tagName();
                                if (photoInfoTag == QLatin1String("c:Value")) {
                                    const QString contentType = photoInfo.attribute(QStringLiteral("c:ContentType"));
165
                                    picture.setRawData(photoInfo.text().toUtf8(), contentType);
166 167 168
                                } else if (photoInfoTag == QLatin1String("c:Url")) {
                                    picture.setUrl(photoInfo.text());
                                } else {
169
                                    qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " photo info tag not supported yet " << photoInfoTag;
170 171 172 173 174 175
                                }
                            }
                            if (!picture.isEmpty()) {
                                contact.setPhoto(picture);
                            }
                        } else {
176
                            qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " photo tag unknown:" << photoTag;
177 178
                        }
                    }
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                } else if (tag == QLatin1String("c:PhysicalAddressCollection")) {
                    for (QDomElement address = e.firstChildElement(); !address.isNull(); address = address.nextSiblingElement()) {
                        const QString addressTag = address.tagName();
                        if (addressTag == QLatin1String("c:PhysicalAddress")) {
                            KContacts::Address addressType;
                            for (QDomElement addressInfo = address.firstChildElement(); !addressInfo.isNull(); addressInfo = addressInfo.nextSiblingElement()) {
                                const QString addressInfoTag = addressInfo.tagName();
                                if (addressInfoTag == QLatin1String("c:AddressLabel")) {
                                    addressType.setLabel(addressInfo.text());
                                } else if (addressInfoTag == QLatin1String("c:Street")) {
                                    addressType.setStreet(addressInfo.text());
                                } else if (addressInfoTag == QLatin1String("c:Locality")) {
                                    addressType.setLocality(addressInfo.text());
                                } else if (addressInfoTag == QLatin1String("c:Region")) {
                                    addressType.setRegion(addressInfo.text());
                                } else if (addressInfoTag == QLatin1String("c:Country")) {
                                    addressType.setCountry(addressInfo.text());
                                } else if (addressInfoTag == QLatin1String("c:PostalCode")) {
                                    addressType.setPostalCode(addressInfo.text());
                                } else if (addressInfoTag == QLatin1String("c:POBox")) {
                                    addressType.setPostOfficeBox(addressInfo.text());
                                } else {
201
                                    qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " address info tag not supported yet " << addressInfoTag;
202 203 204 205 206 207
                                }
                            }
                            if (!addressType.isEmpty()) {
                                contact.insertAddress(addressType);
                            }
                        } else {
208
                            qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " address tag unknown:" << addressTag;
209 210
                        }
                    }
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
                } else if (tag == QLatin1String("c:PositionCollection")) {
                    for (QDomElement position = e.firstChildElement(); !position.isNull(); position = position.nextSiblingElement()) {
                        const QString positionTag = position.tagName();
                        if (positionTag == QLatin1String("c:Position")) {
                            for (QDomElement positionInfo = position.firstChildElement(); !positionInfo.isNull(); positionInfo = positionInfo.nextSiblingElement()) {
                                const QString positionInfoTag = positionInfo.tagName();
                                if (positionInfoTag == QLatin1String("c:Organization")) {
                                    contact.setOrganization(positionInfo.text());
                                } else if (positionInfoTag == QLatin1String("c:Department")) {
                                    contact.setDepartment(positionInfo.text());
                                } else if (positionInfoTag == QLatin1String("c:Office")) {
                                    contact.setOffice(positionInfo.text());
                                } else if (positionInfoTag == QLatin1String("c:Profession")) {
                                    contact.setProfession(positionInfo.text());
                                } else if (positionInfoTag == QLatin1String("c:Role")) {
                                    contact.setRole(positionInfo.text());
                                } else {
228
                                    qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " position info tag not supported yet " << positionInfoTag;
229 230 231
                                }
                            }
                        } else {
232
                            qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " position tag unknown:" << positionTag;
233 234
                        }
                    }
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
                } else if (tag == QLatin1String("c:Gender")) { //TODO verify it
                    KContacts::Gender gender;
                    gender.setGender(e.text());
                    contact.setGender(gender);
                } else if (tag == QLatin1String("c:Notes")) { //TODO verify it
                    contact.setNote(e.text());
                } else if (tag == QLatin1String("c:UrlCollection")) { //TODO verify it
                    for (QDomElement url = e.firstChildElement(); !url.isNull(); url = url.nextSiblingElement()) {
                        const QString urlTag = url.tagName();
                        if (urlTag == QLatin1String("c:Url")) {
                            for (QDomElement urlInfo = url.firstChildElement(); !urlInfo.isNull(); urlInfo = urlInfo.nextSiblingElement()) {
                                const QString urlInfoTag = urlInfo.tagName();
                                if (urlInfoTag == QLatin1String("c:Value")) {
                                    KContacts::ResourceLocatorUrl url;
                                    url.setUrl(QUrl::fromUserInput(urlInfo.text()));
                                    contact.insertExtraUrl(url);
                                } else {
                                   qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " url info tag not supported yet " << urlInfoTag;
                                }
                            }
                        } else {
                            qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << " url tag unknown:" << urlTag;
                        }
                    }
259
                } else {
260
                    qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "unknown tag " << tag;
261
                }
Laurent Montel's avatar
Laurent Montel committed
262
            }
263
            lst << contact;
Laurent Montel's avatar
Laurent Montel committed
264 265
        }
    }
Laurent Montel's avatar
Laurent Montel committed
266
    return lst;
Laurent Montel's avatar
Laurent Montel committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
}

bool ImportWindowContact::loadDomElement(QDomDocument &doc, QFile *file)
{
    QString errorMsg;
    int errorRow;
    int errorCol;
    if (!doc.setContent(file, &errorMsg, &errorRow, &errorCol)) {
        qCWarning(IMPORTEXPORTWINDOWSCONTACTPLUGIN_LOG) << "Unable to load document.Parse error in line " << errorRow
                               << ", col " << errorCol << ": " << errorMsg;
        return false;
    }
    return true;
}

void ImportWindowContact::setParentWidget(QWidget *parentWidget)
{
    mParentWidget = parentWidget;
}
Laurent Montel's avatar
Laurent Montel committed
286

287
void ImportWindowContact::setAutoTests(bool b)
Laurent Montel's avatar
Laurent Montel committed
288
{
289
    mAutoTest = b;
Laurent Montel's avatar
Laurent Montel committed
290
}