<commit_before>/*
    Copyright (C) 2013  <NAME> <<EMAIL>>

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

    This library 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
    Lesser General Public License for more details.

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


#include "ktptranslationproxy.h"
#include "personsmodel.h"
#include "personpluginmanager.h"
#include "plugins/ktp-plugin/impersonsdatasource.h"

#include <KTp/types.h>
#include <KDebug>

KTpTranslationProxy::KTpTranslationProxy(QObject *parent)
    : QIdentityProxyModel(parent)
{
}

KTpTranslationProxy::~KTpTranslationProxy()
{
}

QVariant KTpTranslationProxy::data(const QModelIndex &proxyIndex, int role) const
{
    if (!proxyIndex.isValid()) {
        return QVariant();
    }

    //cache the status from our sourceModel
    QString status;

    IMPersonsDataSource *imPlugin = qobject_cast<IMPersonsDataSource*>(PersonPluginManager::presencePlugin());

    if (!imPlugin) {
        kWarning() << "No imPlugin";
        return QVariant();
    }

    switch (role) {
        case KTp::ContactPresenceTypeRole:
            return mostOnlinePresence(translatePresence(mapToSource(proxyIndex).data(PersonsModel::PresenceTypeRole)));
        case Qt::DisplayRole:
            return mapToSource(proxyIndex).data(Qt::DisplayRole);
        case KTp::RowTypeRole:
            if (sourceModel()->rowCount(mapToSource(proxyIndex)) <= 1) {
                return KTp::ContactRowType;
            } else {
                return KTp::PersonRowType;
            }
        case KTp::ContactAvatarPathRole:
            return mapToSource(proxyIndex).data(PersonsModel::PhotoRole);
        case KTp::IdRole:
            return mapToSource(proxyIndex).data(PersonsModel::IMRole);
        case KTp::HeaderTotalUsersRole:
            return mapToSource(proxyIndex).data(PersonsModel::ContactsCountRole);
    }

    const QString contactId = mapToSource(proxyIndex).data(PersonsModel::IMRole).toString();
    const KTp::ContactPtr contact = imPlugin->contactForContactId(contactId);

    if (!contact.isNull()) {
        switch (role) {
            case KTp::ContactRole:
                return QVariant::fromValue<KTp::ContactPtr>(contact);
            case KTp::ContactGroupsRole:
                return contact->groups();
            case KTp::AccountRole:
//                 if (!contact.isNull()) {
                    return QVariant::fromValue<Tp::AccountPtr>(imPlugin->accountForContact(contact));
                    /*} else {
                    QString accountId = queryNepomukForAccountId(contactId);
                    //nepomuk stores account path, which is in form /org/freedesktop/Telepathy/Account/...
                    //while GCM looks for uniqueIdentifier(), which is without this^ prefix, so we need to chop it off first<commit_msg>Return KTp::Group role from PersonsModel<commit_after>/*
    Copyright (C) 2013  <NAME> <<EMAIL>>

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

    This library 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
    Lesser General Public License for more details.

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


#include "ktptranslationproxy.h"
#include "personsmodel.h"
#include "personpluginmanager.h"
#include "plugins/ktp-plugin/impersonsdatasource.h"

#include <KTp/types.h>
#include <KDebug>

KTpTranslationProxy::KTpTranslationProxy(QObject *parent)
    : QIdentityProxyModel(parent)
{
}

KTpTranslationProxy::~KTpTranslationProxy()
{
}

QVariant KTpTranslationProxy::data(const QModelIndex &proxyIndex, int role) const
{
    if (!proxyIndex.isValid()) {
        return QVariant();
    }

    //cache the status from our sourceModel
    QString status;

    IMPersonsDataSource *imPlugin = qobject_cast<IMPersonsDataSource*>(PersonPluginManager::presencePlugin());

    if (!imPlugin) {
        kWarning() << "No imPlugin";
        return QVariant();
    }

    switch (role) {
        case KTp::ContactPresenceTypeRole:
            return mostOnlinePresence(translatePresence(mapToSource(proxyIndex).data(PersonsModel::PresenceTypeRole)));
        case Qt::DisplayRole:
            return mapToSource(proxyIndex).data(Qt::DisplayRole);
        case KTp::RowTypeRole:
            if (sourceModel()->rowCount(mapToSource(proxyIndex)) <= 1) {
                return KTp::ContactRowType;
            } else {
                return KTp::PersonRowType;
            }
        case KTp::ContactAvatarPathRole:
            return mapToSource(proxyIndex).data(PersonsModel::PhotoRole);
        case KTp::IdRole:
            return mapToSource(proxyIndex).data(PersonsModel::IMRole);
        case KTp::HeaderTotalUsersRole:
            return mapToSource(proxyIndex).data(PersonsModel::ContactsCountRole);
        case KTp::ContactGroupsRole:
            return mapToSource(proxyIndex).data(PersonsModel::ContactGroupsRole);
    }

    const QString contactId = mapToSource(proxyIndex).data(PersonsModel::IMRole).toString();
    const KTp::ContactPtr contact = imPlugin->contactForContactId(contactId);

    if (!contact.isNull()) {
        switch (role) {
            case KTp::ContactRole:
                return QVariant::fromValue<KTp::ContactPtr>(contact);
            case KTp::AccountRole:
//                 if (!contact.isNull()) {
                    return QVariant::fromValue<Tp::AccountPtr>(imPlugin->accountForContact(contact));
                    /*} else {
                    QString accountId = queryNepomukForAccountId(contactId);
                    //nepomuk stores account path, which is in form /org/freedesktop/Telepathy/Account/...
                    //while GCM looks for uniqueIdentifier(), which is without this^ prefix, so we need to chop it off first
{'fraction_non_alphanumeric': 0.09428950863213811, 'fraction_numerical': 0.005312084993359893, 'mean_word_length': 3.0225519287833826, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3280413', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1792, 'n_words': 515}