etesyncresource.cpp 7.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (C) 2020 by Shashwat Jolly <shashwat.jolly@gmail.com>
 *
 * 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.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "etesyncresource.h"
19

Shashwat Jolly's avatar
Shashwat Jolly committed
20
#include <KLocalizedString>
21
#include <QDBusConnection>
22

23
#include "etesync_debug.h"
24
#include "etesyncadapter.h"
25 26 27 28 29
#include "settings.h"
#include "settingsadaptor.h"

using namespace Akonadi;

Shashwat Jolly's avatar
Shashwat Jolly committed
30
etesyncResource::etesyncResource(const QString &id)
31 32
    : ResourceBase(id)
{
33
    Settings::instance(KSharedConfig::openConfig());
34
    new SettingsAdaptor(Settings::self());
35

36 37 38
    QDBusConnection::sessionBus().registerObject(QStringLiteral("/Settings"),
                                                 Settings::self(),
                                                 QDBusConnection::ExportAdaptors);
39

Shashwat Jolly's avatar
Shashwat Jolly committed
40
    setName(i18n("EteSync Resource"));
41 42 43 44

    setNeedsNetwork(true);

    connect(this, &Akonadi::AgentBase::reloadConfiguration, this, &etesyncResource::onReloadConfiguration);
45

46
    qCDebug(ETESYNC_LOG) << "Resource started";
47 48 49 50
}

etesyncResource::~etesyncResource() {}

51 52
void etesyncResource::retrieveCollections()
{
Shashwat Jolly's avatar
Shashwat Jolly committed
53
    EteSyncJournal **journals = etesync_journal_manager_list(journalManager);
54 55 56

    Collection::List list;

Shashwat Jolly's avatar
Shashwat Jolly committed
57 58
    for (EteSyncJournal **iter = journals; *iter; iter++) {
        EteSyncJournal *journal = *iter;
59

Shashwat Jolly's avatar
Shashwat Jolly committed
60
        EteSyncCryptoManager *cryptoManager = etesync_journal_get_crypto_manager(journal, derived, keypair);
61

Shashwat Jolly's avatar
Shashwat Jolly committed
62
        EteSyncCollectionInfo *info = etesync_journal_get_info(journal, cryptoManager);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

        if (QStringFromCharArr(etesync_collection_info_get_type(info)) != QStringLiteral("ADDRESS_BOOK"))
            continue;

        Collection c;
        c.setParentCollection(Collection::root());
        c.setRemoteId(QStringFromCharArr(etesync_journal_get_uid(journal)));
        c.setName(QStringFromCharArr(etesync_collection_info_get_display_name(info)));

        QStringList mimeTypes;
        mimeTypes << KContacts::Addressee::mimeType();
        c.setContentMimeTypes(mimeTypes);

        list << c;

        etesync_collection_info_destroy(info);
        etesync_crypto_manager_destroy(cryptoManager);
        etesync_journal_destroy(journal);
    }

    free(journals);
    collectionsRetrieved(list);
85 86
}

Shashwat Jolly's avatar
Shashwat Jolly committed
87
void etesyncResource::retrieveItems(const Akonadi::Collection &collection)
88
{
Shashwat Jolly's avatar
Shashwat Jolly committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    const QString journalUid = collection.remoteId();

    EteSyncJournal *journal = etesync_journal_manager_fetch(journalManager, journalUid);

    EteSyncCryptoManager *cryptoManager = etesync_journal_get_crypto_manager(journal, derived, keypair);

    EteSyncEntryManager *entryManager = etesync_entry_manager_new(etesync, journalUid);

    EteSyncEntry **entries = etesync_entry_manager_list(entryManager, NULL, 0);

    char *prevUid = NULL;

    Item::List items;

    QMap<QString, KContacts::Addressee> contacts;
    QMap<QString, QString> remoteIDs;

    for (EteSyncEntry **iter = entries; *iter; iter++) {
        EteSyncEntry *entry = *iter;

        EteSyncSyncEntry *syncEntry = etesync_entry_get_sync_entry(entry, cryptoManager, prevUid);

111
        const QString action = QStringFromCharArr(etesync_sync_entry_get_action(syncEntry));
Shashwat Jolly's avatar
Shashwat Jolly committed
112 113 114 115 116 117
        KContacts::VCardConverter converter;
        const char *contentStr = etesync_sync_entry_get_content(syncEntry);
        QByteArray content(contentStr);
        const KContacts::Addressee contact = converter.parseVCard(content);

        if (action == QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_ADD) || action == QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_CHANGE)) {
Shashwat Jolly's avatar
Shashwat Jolly committed
118 119
            // qCDebug(ETESYNC_LOG) << action;
            // qCDebug(ETESYNC_LOG) << contact.uid();
Shashwat Jolly's avatar
Shashwat Jolly committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            contacts[contact.uid()] = contact;
            remoteIDs[contact.uid()] = QStringFromCharArr(etesync_entry_get_uid(entry));
        } else if (action == QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_DELETE)) {
            contacts.remove(contact.uid());
        }

        free(prevUid);
        prevUid = etesync_entry_get_uid(entry);

        etesync_sync_entry_destroy(syncEntry);

        etesync_entry_destroy(entry);
    }

    for (auto it = contacts.constBegin(); it != contacts.constEnd(); it++) {
        Item item(it.key());
        item.setMimeType(KContacts::Addressee::mimeType());
        item.setRemoteId(remoteIDs[it.key()]);
        item.setPayload<KContacts::Addressee>(it.value());
        items << item;
    }

    itemsRetrieved(items);
143 144
}

Shashwat Jolly's avatar
Shashwat Jolly committed
145 146
bool etesyncResource::retrieveItem(const Akonadi::Item &item,
                                   const QSet<QByteArray> &parts)
147 148 149 150
{
    // TODO: this method is called when Akonadi wants more data for a given
    // item. You can only provide the parts that have been requested but you are
    // allowed to provide all in one go
151

152
    return true;
153 154
}

155 156 157 158
void etesyncResource::aboutToQuit()
{
    // TODO: any cleanup you need to do while there is still an active
    // event loop. The resource will terminate after this method returns
159 160
}

161
void etesyncResource::onReloadConfiguration()
162
{
Shashwat Jolly's avatar
Shashwat Jolly committed
163
    Settings::self()->load();
164 165 166 167
    serverUrl = Settings::self()->baseUrl();
    username = Settings::self()->username();
    password = Settings::self()->password();
    encryptionPassword = Settings::self()->encryptionPassword();
168

169
    qCDebug(ETESYNC_LOG) << "Resource config reload";
170

171 172 173 174 175 176
    initialise();
}

void etesyncResource::initialise()
{
    // Initialise EteSync client state
177 178 179 180 181 182 183
    etesync = etesync_new(QStringLiteral("Akonadi EteSync Resource"), serverUrl);
    derived = etesync_crypto_derive_key(etesync, username, encryptionPassword);
    QString token = etesync_auth_get_token(etesync, username, password);
    etesync_set_auth_token(etesync, token);
    journalManager = etesync_journal_manager_new(etesync);

    // Get user keypair
Shashwat Jolly's avatar
Shashwat Jolly committed
184 185 186
    EteSyncUserInfoManager *userInfoManager = etesync_user_info_manager_new(etesync);
    EteSyncUserInfo *userInfo = etesync_user_info_manager_fetch(userInfoManager, username);
    EteSyncCryptoManager *userInfoCryptoManager = etesync_user_info_get_crypto_manager(userInfo, derived);
187 188 189 190 191 192

    keypair = etesync_user_info_get_keypair(userInfo, userInfoCryptoManager);
    etesync_crypto_manager_destroy(userInfoCryptoManager);
    etesync_user_info_destroy(userInfo);
    etesync_user_info_manager_destroy(userInfoManager);
    synchronize();
193 194
}

Shashwat Jolly's avatar
Shashwat Jolly committed
195 196
void etesyncResource::itemAdded(const Akonadi::Item &item,
                                const Akonadi::Collection &collection)
197 198 199 200
{
    // TODO: this method is called when somebody else, e.g. a client
    // application, has created an item in a collection managed by your
    // resource.
201

202 203
    // NOTE: There is an equivalent method for collections, but it isn't part
    // of this template code to keep it simple
204 205
}

Shashwat Jolly's avatar
Shashwat Jolly committed
206 207
void etesyncResource::itemChanged(const Akonadi::Item &item,
                                  const QSet<QByteArray> &parts)
208 209 210
{
    // TODO: this method is called when somebody else, e.g. a client
    // application, has changed an item managed by your resource.
211

212 213
    // NOTE: There is an equivalent method for collections, but it isn't part
    // of this template code to keep it simple
214 215
}

Shashwat Jolly's avatar
Shashwat Jolly committed
216
void etesyncResource::itemRemoved(const Akonadi::Item &item)
217 218 219
{
    // TODO: this method is called when somebody else, e.g. a client
    // application, has deleted an item managed by your resource.
220

221 222
    // NOTE: There is an equivalent method for collections, but it isn't part
    // of this template code to keep it simple
223 224 225
}

AKONADI_RESOURCE_MAIN(etesyncResource)