File indexing completed on 2024-12-22 04:52:50

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "thunderbirdaddressbook.h"
0008 #include "addressbook/MorkParser.h"
0009 
0010 #include "thunderbirdplugin_debug.h"
0011 #include <KContacts/Addressee>
0012 #include <KLocalizedString>
0013 #include <QUrl>
0014 
0015 ThunderBirdAddressBook::ThunderBirdAddressBook(const QDir &dir)
0016     : mDir(dir)
0017 {
0018 }
0019 
0020 ThunderBirdAddressBook::~ThunderBirdAddressBook() = default;
0021 
0022 void ThunderBirdAddressBook::importAddressBook()
0023 {
0024     const QString path = mDir.path();
0025     readAddressBook(path + QLatin1StringView("/impab.mab"));
0026     const QStringList filesimportab = mDir.entryList(QStringList(QStringLiteral("impab-[0-9]*.map")), QDir::Files, QDir::Name);
0027     for (const QString &file : filesimportab) {
0028         readAddressBook(path + QLatin1Char('/') + file);
0029     }
0030     readAddressBook(path + QLatin1StringView("/abook.mab"));
0031 
0032     const QStringList files = mDir.entryList(QStringList(QStringLiteral("abook-[0-9]*.map")), QDir::Files, QDir::Name);
0033     for (const QString &file : files) {
0034         readAddressBook(path + QLatin1Char('/') + file);
0035     }
0036     readAddressBook(path + QLatin1StringView("/history.mab"));
0037 
0038     cleanUp();
0039 }
0040 
0041 void ThunderBirdAddressBook::readAddressBook(const QString &filename)
0042 {
0043     MorkParser mork;
0044     if (!mork.open(filename)) {
0045         if (mork.error() == FailedToOpen) {
0046             addAddressBookImportError(i18n("Contacts file '%1' not found", filename));
0047         }
0048         qCDebug(THUNDERBIRDPLUGIN_LOG) << " error during read file " << filename << " Error type " << mork.error();
0049         return;
0050     }
0051     MorkTableMap *tables = mork.getTables(0x80);
0052     if (tables) {
0053         MorkTableMap::iterator tableIterEnd(tables->end());
0054         for (MorkTableMap::iterator tableIter = tables->begin(); tableIter != tableIterEnd; ++tableIter) {
0055             if (tableIter.key() != 0) {
0056                 MorkRowMap *rows = mork.getRows(0x80, &tableIter.value());
0057                 if (rows) {
0058                     MorkRowMap::iterator endRow(rows->end());
0059                     for (MorkRowMap::iterator rowIter = rows->begin(); rowIter != endRow; ++rowIter) {
0060                         if (rowIter.key() != 0) {
0061                             KContacts::Addressee contact;
0062                             MorkCells cells = rowIter.value();
0063                             MorkCells::iterator endCellIter = cells.end();
0064                             KContacts::Address homeAddr = KContacts::Address(KContacts::Address::Home);
0065                             KContacts::Address workAddr = KContacts::Address(KContacts::Address::Work);
0066                             int birthday = -1;
0067                             int birthmonth = -1;
0068                             int birthyear = -1;
0069 
0070                             for (MorkCells::iterator cellsIter = cells.begin(); cellsIter != endCellIter; ++cellsIter) {
0071                                 const QString value = mork.getValue(cellsIter.value());
0072                                 const QString column = mork.getColumn(cellsIter.key());
0073                                 qCDebug(THUNDERBIRDPLUGIN_LOG) << "column :" << column << " value :" << value;
0074                                 if (column == QLatin1StringView("LastModifiedDate")) {
0075                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0076                                 } else if (column == QLatin1StringView("RecordKey")) {
0077                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0078                                 } else if (column == QLatin1StringView("AddrCharSet")) {
0079                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0080                                 } else if (column == QLatin1StringView("LastRecordKey")) {
0081                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0082                                 } else if (column == QLatin1StringView("ns:addrbk:db:table:kind:pab")) {
0083                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0084                                 } else if (column == QLatin1StringView("ListName")) {
0085                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0086                                 } else if (column == QLatin1StringView("ListNickName")) {
0087                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0088                                 } else if (column == QLatin1StringView("ListDescription")) {
0089                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0090                                 } else if (column == QLatin1StringView("ListTotalAddresses")) {
0091                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0092                                 } else if (column == QLatin1StringView("LowercaseListName")) {
0093                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0094                                 } else if (column == QLatin1StringView("ns:addrbk:db:table:kind:deleted")) {
0095                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0096                                 } else if (column == QLatin1StringView("PhotoType")) {
0097                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0098                                 } else if (column == QLatin1StringView("PreferDisplayName")) {
0099                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0100                                 } else if (column == QLatin1StringView("PhotoURI")) {
0101                                     KContacts::Picture photo;
0102                                     photo.setUrl(value);
0103                                     contact.setLogo(photo);
0104                                 } else if (column == QLatin1StringView("PhotoName")) {
0105                                     // TODO: verify it
0106                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0107                                 } else if (column == QLatin1StringView("DbRowID")) {
0108                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0109                                 } else if (column == QLatin1StringView("ns:addrbk:db:row:scope:card:all")) {
0110                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0111                                 } else if (column == QLatin1StringView("ns:addrbk:db:row:scope:list:all")) {
0112                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0113                                 } else if (column == QLatin1StringView("ns:addrbk:db:row:scope:data:all")) {
0114                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0115                                 } else if (column == QLatin1StringView("FirstName")) {
0116                                     contact.setName(value);
0117                                 } else if (column == QLatin1StringView("LastName")) {
0118                                     contact.setFamilyName(value);
0119                                 } else if (column == QLatin1StringView("PhoneticFirstName")) {
0120                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0121                                 } else if (column == QLatin1StringView("PhoneticLastName")) {
0122                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0123                                 } else if (column == QLatin1StringView("DisplayName")) {
0124                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0125                                 } else if (column == QLatin1StringView("NickName")) {
0126                                     contact.setNickName(value);
0127                                 } else if (column == QLatin1StringView("PrimaryEmail")) {
0128                                     contact.setEmails(QStringList() << value);
0129                                 } else if (column == QLatin1StringView("LowercasePrimaryEmail")) {
0130                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0131                                 } else if (column == QLatin1StringView("SecondEmail")) {
0132                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0133                                 } else if (column == QLatin1StringView("PreferMailFormat")) {
0134                                     contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("MailPreferedFormatting"), value);
0135                                 } else if (column == QLatin1StringView("PopularityIndex")) {
0136                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0137                                 } else if (column == QLatin1StringView("AllowRemoteContent")) {
0138                                     contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("MailAllowToRemoteContent"), value);
0139                                 } else if (column == QLatin1StringView("WorkPhone")) {
0140                                     contact.insertPhoneNumber(KContacts::PhoneNumber(value, KContacts::PhoneNumber::Work));
0141                                 } else if (column == QLatin1StringView("HomePhone")) {
0142                                     contact.insertPhoneNumber(KContacts::PhoneNumber(value, KContacts::PhoneNumber::Home));
0143                                 } else if (column == QLatin1StringView("FaxNumber")) {
0144                                     contact.insertPhoneNumber(KContacts::PhoneNumber(value, KContacts::PhoneNumber::Fax));
0145                                 } else if (column == QLatin1StringView("PagerNumber")) {
0146                                     contact.insertPhoneNumber(KContacts::PhoneNumber(value, KContacts::PhoneNumber::Pager));
0147                                 } else if (column == QLatin1StringView("CellularNumber")) {
0148                                     contact.insertPhoneNumber(KContacts::PhoneNumber(value, KContacts::PhoneNumber::Cell));
0149                                 } else if (column == QLatin1StringView("WorkPhoneType")) {
0150                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0151                                 } else if (column == QLatin1StringView("HomePhoneType")) {
0152                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0153                                 } else if (column == QLatin1StringView("FaxNumberType")) {
0154                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0155                                 } else if (column == QLatin1StringView("PagerNumberType")) {
0156                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0157                                 } else if (column == QLatin1StringView("CellularNumberType")) {
0158                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0159                                 } else if (column == QLatin1StringView("HomeAddress")) {
0160                                     homeAddr.setStreet(value);
0161                                 } else if (column == QLatin1StringView("HomeAddress2")) {
0162                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0163                                 } else if (column == QLatin1StringView("HomeCity")) {
0164                                     homeAddr.setLocality(value);
0165                                 } else if (column == QLatin1StringView("HomeState")) {
0166                                     homeAddr.setRegion(value);
0167                                 } else if (column == QLatin1StringView("HomeZipCode")) {
0168                                     homeAddr.setPostalCode(value);
0169                                 } else if (column == QLatin1StringView("HomeCountry")) {
0170                                     homeAddr.setCountry(value);
0171                                 } else if (column == QLatin1StringView("WorkAddress")) {
0172                                     workAddr.setStreet(value);
0173                                 } else if (column == QLatin1StringView("WorkAddress2")) {
0174                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0175                                 } else if (column == QLatin1StringView("WorkCity")) {
0176                                     workAddr.setLocality(value);
0177                                 } else if (column == QLatin1StringView("WorkState")) {
0178                                     workAddr.setRegion(value);
0179                                 } else if (column == QLatin1StringView("WorkZipCode")) {
0180                                     workAddr.setPostalCode(value);
0181                                 } else if (column == QLatin1StringView("WorkCountry")) {
0182                                     workAddr.setCountry(value);
0183                                 } else if (column == QLatin1StringView("JobTitle")) {
0184                                     contact.setTitle(value);
0185                                 } else if (column == QLatin1StringView("Department")) {
0186                                     contact.setDepartment(value);
0187                                 } else if (column == QLatin1StringView("Company")) {
0188                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0189                                 } else if (column == QLatin1StringView("_AimScreenName")) {
0190                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0191                                 } else if (column == QLatin1StringView("AnniversaryYear")) {
0192                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0193                                 } else if (column == QLatin1StringView("AnniversaryMonth")) {
0194                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0195                                 } else if (column == QLatin1StringView("AnniversaryDay")) {
0196                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0197                                 } else if (column == QLatin1StringView("SpouseName")) {
0198                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0199                                 } else if (column == QLatin1StringView("FamilyName")) {
0200                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0201                                 } else if (column == QLatin1StringView("WebPage1")) {
0202                                     KContacts::ResourceLocatorUrl url;
0203                                     url.setUrl(QUrl(value));
0204                                     contact.setUrl(url);
0205                                 } else if (column == QLatin1StringView("WebPage2")) {
0206                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " column " << column << " found but not imported. Need to look at how to import it";
0207                                 } else if (column == QLatin1StringView("BirthYear")) {
0208                                     birthyear = value.toInt();
0209                                 } else if (column == QLatin1StringView("BirthMonth")) {
0210                                     birthmonth = value.toInt();
0211                                 } else if (column == QLatin1StringView("BirthDay")) {
0212                                     birthday = value.toInt();
0213                                 } else if (column == QLatin1StringView("Custom1")) {
0214                                     contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("Custom1"), value);
0215                                 } else if (column == QLatin1StringView("Custom2")) {
0216                                     contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("Custom2"), value);
0217                                 } else if (column == QLatin1StringView("Custom3")) {
0218                                     contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("Custom3"), value);
0219                                 } else if (column == QLatin1StringView("Custom4")) {
0220                                     contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("Custom4"), value);
0221                                 } else if (column == QLatin1StringView("Notes")) {
0222                                     contact.setNote(value);
0223                                 } else {
0224                                     qCDebug(THUNDERBIRDPLUGIN_LOG) << " Columnn not implemented " << column;
0225                                 }
0226                                 // qCDebug(THUNDERBIRDPLUGIN_LOG)<<" value :"<<value<<" column"<<column;
0227                             }
0228 
0229                             if (!homeAddr.isEmpty()) {
0230                                 contact.insertAddress(homeAddr);
0231                             }
0232                             if (!workAddr.isEmpty()) {
0233                                 contact.insertAddress(workAddr);
0234                             }
0235 
0236                             const QDate birthDate(birthyear, birthmonth, birthday);
0237                             if (birthDate.isValid()) {
0238                                 contact.setBirthday(birthDate);
0239                             }
0240                             addImportContactNote(contact, QStringLiteral("Thunderbird"));
0241                             createContact(contact);
0242                             qCDebug(THUNDERBIRDPLUGIN_LOG) << "-----------------------";
0243                         }
0244                     }
0245                 }
0246             }
0247         }
0248     }
0249 }
0250 
0251 #include "moc_thunderbirdaddressbook.cpp"