importwindowcontact.cpp 8.33 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
                if (tag == QLatin1String("c:EmailAddressCollection")) {
                    KContacts::Email::List lstEmails;
                    for (QDomElement emails = e.firstChildElement(); !emails.isNull(); emails = emails.nextSiblingElement()) {
                        const QString emailsTag = emails.tagName();
                        qDebug() << " emailstag "<< emailsTag;
                        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 {
                                    qDebug() << " address tag not supported yet " << addressesTag;
                                }
                            }
                            lstEmails << email;
                        }
                        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());
99 100 101 102 103 104 105 106
                                } 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());
107 108 109 110 111
                                } else {
                                    qDebug() << " name tag not supported yet " << nameInfoTag;
                                }
                            }
                        } else {
112
                            qDebug() << " name tag unknown:" << nameTag;
113 114
                        }
                    }
115 116
                } else if (tag == QLatin1String("c:PhoneNumberCollection")) {

117 118
                } else if (tag == QLatin1String("c:PhotoCollection")) {

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
                } 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 {
                                    qDebug() << " position info tag not supported yet " << positionInfoTag;
                                }
                            }
                        } else {
                            qDebug() << " position tag unknown:" << positionTag;
                        }
                    }
143 144 145
                } else {
                    qDebug() << "unknown tag " << tag;
                }
Laurent Montel's avatar
Laurent Montel committed
146
            }
147
            lst << contact;
Laurent Montel's avatar
Laurent Montel committed
148 149
        }
    }
Laurent Montel's avatar
Laurent Montel committed
150
    return lst;
Laurent Montel's avatar
Laurent Montel committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
}

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
170

171
void ImportWindowContact::setAutoTests(bool b)
Laurent Montel's avatar
Laurent Montel committed
172
{
173
    mAutoTest = b;
Laurent Montel's avatar
Laurent Montel committed
174
}