File indexing completed on 2024-11-24 04:53:24
0001 /* Copyright (C) 2012 Thomas Lübking <thomas.luebking@gmail.com> 0002 0003 This file is part of the Trojita Qt IMAP e-mail client, 0004 http://trojita.flaska.net/ 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public License as 0008 published by the Free Software Foundation; either version 2 of 0009 the License or (at your option) version 3 or any later version 0010 accepted by the membership of KDE e.V. (or its successor approved 0011 by the membership of KDE e.V.), which shall act as a proxy 0012 defined in Section 14 of version 3 of the license. 0013 0014 This program is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 GNU General Public License for more details. 0018 0019 You should have received a copy of the GNU General Public License 0020 along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 0023 #include <QDateTime> 0024 0025 #include <QApplication> 0026 #include <QDir> 0027 #include <QSettings> 0028 0029 #include "Plugins/AddressbookPlugin.h" 0030 #include "Plugins/PluginManager.h" 0031 0032 #include "Common/SettingsCategoryGuard.h" 0033 0034 #ifdef QT_STATICPLUGIN 0035 #include <QtPlugin> 0036 Q_IMPORT_PLUGIN(trojita_plugin_AbookAddressbookPlugin) 0037 #endif 0038 0039 int main(int argc, char **argv) { 0040 if (argc > 1 && argv[1][0] != '-') { 0041 QCoreApplication a(argc, argv); 0042 if (qstrcmp(argv[1], "import")) { 0043 qWarning("unknown command \"%s\"", argv[1]); 0044 return 1; 0045 } 0046 if (argc == 2) { 0047 qWarning("you must specify an address string to import, eg.\n" 0048 "%s import \"Joe User <joe@users.com>\"", argv[0]); 0049 return 2; 0050 } 0051 QMap<QString, QString> imports; 0052 for (int i = 2; i < argc; ++i) { 0053 QString arg = QString::fromLocal8Bit(argv[i]); 0054 QString mail, name; 0055 QStringList contact = arg.split(QStringLiteral(" "), Qt::SkipEmptyParts); 0056 foreach (const QString &token, contact) { 0057 if (token.contains(QLatin1Char('@'))) 0058 mail = token; 0059 else 0060 name = name.isEmpty() ? token : name + QLatin1String(" ") + token; 0061 } 0062 if (mail.isEmpty()) { 0063 qWarning("error, \"%s\" does not seem to contain a mail address", arg.toLocal8Bit().data()); 0064 continue; 0065 } 0066 mail.remove(QLatin1Char('<')); 0067 mail.remove(QLatin1Char('>')); 0068 if (name.isEmpty()) 0069 name = mail; 0070 imports.insert(name, mail); 0071 } 0072 if (imports.isEmpty()) { 0073 qWarning("nothing to import"); 0074 return 2; 0075 } 0076 0077 int lastContact = -1, updates = 0, adds = 0; 0078 QSettings abook(QDir::homePath() + QLatin1String("/.abook/addressbook"), QSettings::IniFormat); 0079 abook.setIniCodec("UTF-8"); 0080 QStringList contacts = abook.childGroups(); 0081 foreach (const QString &contact, contacts) { 0082 Common::SettingsCategoryGuard guard(&abook, contact); 0083 int id = contact.toInt(); 0084 if (id > lastContact) 0085 lastContact = id; 0086 const QString name = abook.value(QStringLiteral("name"), QString()).toString(); 0087 QMap<QString,QString>::iterator it = imports.begin(), end = imports.end(); 0088 while (it != end) { 0089 if (it.key() == name) { 0090 QStringList mails = abook.value(QStringLiteral("email"), QString()).toStringList(); 0091 if (!mails.contains(it.value())) { 0092 ++updates; 0093 mails << it.value(); 0094 abook.setValue(QStringLiteral("email"), mails); 0095 } 0096 it = imports.erase(it); 0097 continue; 0098 } 0099 else 0100 ++it; 0101 } 0102 } 0103 QMap<QString,QString>::const_iterator it = imports.constBegin(), end = imports.constEnd(); 0104 while (it != end) { 0105 ++adds; 0106 Common::SettingsCategoryGuard guard(&abook, QString::number(++lastContact)); 0107 abook.setValue(QStringLiteral("name"), it.key()); 0108 abook.setValue(QStringLiteral("email"), it.value()); 0109 ++it; 0110 } 0111 qWarning("updated %d and added %d contacts", updates, adds); 0112 return 0; 0113 } 0114 QApplication a(argc, argv); 0115 a.setAttribute(Qt::AA_UseHighDpiPixmaps); 0116 QSettings s(QStringLiteral("flaska.net"), QStringLiteral("be.contacts")); 0117 s.setValue(QStringLiteral("plugin"), QLatin1String("abookaddressbook")); 0118 Plugins::PluginManager m(nullptr, &s, QStringLiteral("plugin"), QStringLiteral("plugin"), QStringLiteral("plugin")); 0119 Plugins::AddressbookPlugin *addressbook = m.addressbook(); 0120 if (addressbook) { 0121 addressbook->openAddressbookWindow(); 0122 return a.exec(); 0123 } else { 0124 qWarning("cannot load abookaddressbook plugin"); 0125 return 1; 0126 } 0127 }