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

    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 3 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 <http://www.gnu.org/licenses/>.
*/


#include "kpeople-actions-plugin.h"

#include <QAction>

#include <KIcon>
#include <KLocalizedString>
#include <KPluginFactory>
#include <kdemacros.h>

#include "KTp/contact.h"
#include "KTp/actions.h"
#include "KTp/im-persons-data-source.h"

#include <TelepathyQt/Account>
#include <TelepathyQt/ContactManager>


#include <kpeople/personpluginmanager.h>
#include <KPeople/PersonData>

enum IMActionType {
    TextChannel,
    AudioChannel,
    VideoChannel,
    FileTransfer,
    LogViewer
};

class IMAction : public QAction {
    Q_OBJECT
public:
    IMAction(const QString &text, const KIcon &icon, const KTp::ContactPtr &contact,
             const Tp::AccountPtr &account, IMActionType type, QObject *parent);
    KTp::ContactPtr contact() const;
    Tp::AccountPtr account() const;
    IMActionType type() const;
private:
    KTp::ContactPtr m_contact;
    Tp::AccountPtr m_account;
    IMActionType m_type;
};

IMAction::IMAction(const QString &text, const KIcon &icon, const KTp::ContactPtr &contact,
                   const Tp::AccountPtr &account, IMActionType type, QObject *parent):
    QAction(icon, text, parent),
    m_contact(contact),
    m_account(account),
    m_type(type)
{
}

KTp::ContactPtr IMAction::contact() const
{
    return m_contact;
}

Tp::AccountPtr IMAction::account() const
{
    return m_account;
}


IMActionType IMAction::type() const
{
    return m_type;
}

KPeopleActionsPlugin::KPeopleActionsPlugin(QObject* parent, const QVariantList &args):
AbstractPersonPlugin(parent)
{
}

QList<QAction*> KPeopleActionsPlugin::actionsForPerson(const KPeople::PersonDataPtr &personData, QObject *parent)
{
    QList<QAction*> actions;

    IMPersonsDataSource *dataSource = dynamic_cast<IMPersonsDataSource*>(KPeople::PersonPluginManager::presencePlugin());
    if (!dataSource) {
        return actions;
    }

    QStringList imContactsIds = personData->imAccounts();

    for (int i=0;i<imContactsIds.size();i+=3) { //FIXME imAccounts() returns a silly datatype
        const QString contactId = imContactsIds[i+2];
        const KTp::ContactPtr contact = dataSource->contactForContactId(contactId);
        if (!contact || !contact->manager()) {
            continue;
        }
        const Tp::AccountPtr account = dataSource->accountForContact(contact);

        if (!account) {
            continue;
        }

        if (true) { //no such query for text chat capability, added an "if true" because makes the code look consistent
            QAction *action = new IMAction(i18n("Start Chat"),
                                KIcon(QLatin1String("text-x-generic")),
                                contact,
                                account,
                                TextChannel,
                                parent);
            connect (action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
            actions << action;
        }
        if (contact->audioCallCapability()) {
            QAction *action = new IMAction(i18n("Start Audio Call"),
                                KIcon(QLatin1String("audio-headset")),
                                contact,
                                account,
                                AudioChannel,
                                parent);
            connect (action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
            actions << action;
        }
        if (contact->videoCallCapability()) {
            QAction *action = new IMAction(i18n("Start Video Call"),
                                KIcon(QLatin1String("camera-web")),
                                contact,
                                account,
                                VideoChannel,
                                parent);
            connect (action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
            actions << action;
        }

        if (contact->fileTransferCapability()) {
            QAction *action = new IMAction(i18n("Send a file"),
                                        KIcon(QLatin1String("mail-attachment")),
                                        contact,
                                        account,
                                        FileTransfer,
                                        parent);
            action->setDisabled(true); //FIXME: we need to prompt for file
            connect (action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
            actions << action;
        }

        QAction *action = new IMAction(i18n("Open Log Viewer"),
                                    KIcon(QLatin1String("documentation")),
                                    contact,
                                    account,
                                    LogViewer,
                                    parent);
        connect(action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
        actions << action;
    }
    return actions;
}

void KPeopleActionsPlugin::onActionTriggered()
{
    IMAction *action = qobject_cast<IMAction*>(sender());
    KTp::ContactPtr contact = action->contact();
    Tp::AccountPtr account = action->account();
    IMActionType type = action->type();

    switch (type) {
        case TextChannel:
            KTp::Actions::startChat(account, contact);
            break;
        case AudioChannel:
            KTp::Actions::startAudioCall(account, contact);
            break;<commit_msg>Add account display name to the KPeople actions<commit_after>/*
    Copyright (C) 2013  <NAME> <<EMAIL>>

    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 3 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 <http://www.gnu.org/licenses/>.
*/


#include "kpeople-actions-plugin.h"

#include <QAction>

#include <KIcon>
#include <KLocalizedString>
#include <KPluginFactory>
#include <kdemacros.h>

#include "KTp/contact.h"
#include "KTp/actions.h"
#include "KTp/im-persons-data-source.h"

#include <TelepathyQt/Account>
#include <TelepathyQt/ContactManager>


#include <kpeople/personpluginmanager.h>
#include <KPeople/PersonData>

enum IMActionType {
    TextChannel,
    AudioChannel,
    VideoChannel,
    FileTransfer,
    LogViewer
};

class IMAction : public QAction {
    Q_OBJECT
public:
    IMAction(const QString &text, const KIcon &icon, const KTp::ContactPtr &contact,
             const Tp::AccountPtr &account, IMActionType type, QObject *parent);
    KTp::ContactPtr contact() const;
    Tp::AccountPtr account() const;
    IMActionType type() const;
private:
    KTp::ContactPtr m_contact;
    Tp::AccountPtr m_account;
    IMActionType m_type;
};

IMAction::IMAction(const QString &text, const KIcon &icon, const KTp::ContactPtr &contact,
                   const Tp::AccountPtr &account, IMActionType type, QObject *parent):
    QAction(icon, text, parent),
    m_contact(contact),
    m_account(account),
    m_type(type)
{
}

KTp::ContactPtr IMAction::contact() const
{
    return m_contact;
}

Tp::AccountPtr IMAction::account() const
{
    return m_account;
}


IMActionType IMAction::type() const
{
    return m_type;
}

KPeopleActionsPlugin::KPeopleActionsPlugin(QObject* parent, const QVariantList &args):
AbstractPersonPlugin(parent)
{
}

QList<QAction*> KPeopleActionsPlugin::actionsForPerson(const KPeople::PersonDataPtr &personData, QObject *parent)
{
    QList<QAction*> actions;

    IMPersonsDataSource *dataSource = dynamic_cast<IMPersonsDataSource*>(KPeople::PersonPluginManager::presencePlugin());
    if (!dataSource) {
        return actions;
    }

    QStringList imContactsIds = personData->imAccounts();

    for (int i=0;i<imContactsIds.size();i+=3) { //FIXME imAccounts() returns a silly datatype
        const QString contactId = imContactsIds[i+2];
        const KTp::ContactPtr contact = dataSource->contactForContactId(contactId);
        if (!contact || !contact->manager()) {
            continue;
        }
        const Tp::AccountPtr account = dataSource->accountForContact(contact);

        if (!account) {
            continue;
        }

        if (true) { //no such query for text chat capability, added an "if true" because makes the code look consistent
            QAction *action = new IMAction(i18n("Start Chat Using %1...", account->displayName()),
                                KIcon(QLatin1String("text-x-generic")),
                                contact,
                                account,
                                TextChannel,
                                parent);
            connect (action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
            actions << action;
        }
        if (contact->audioCallCapability()) {
            QAction *action = new IMAction(i18n("Start Audio Call Using %1...", account->displayName()),
                                KIcon(QLatin1String("audio-headset")),
                                contact,
                                account,
                                AudioChannel,
                                parent);
            connect (action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
            actions << action;
        }
        if (contact->videoCallCapability()) {
            QAction *action = new IMAction(i18n("Start Video Call Using %1...", account->displayName()),
                                KIcon(QLatin1String("camera-web")),
                                contact,
                                account,
                                VideoChannel,
                                parent);
            connect (action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
            actions << action;
        }

        if (contact->fileTransferCapability()) {
            QAction *action = new IMAction(i18n("Send a File Using %1...", account->displayName()),
                                        KIcon(QLatin1String("mail-attachment")),
                                        contact,
                                        account,
                                        FileTransfer,
                                        parent);
            action->setDisabled(true); //FIXME: we need to prompt for file
            connect (action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
            actions << action;
        }

        QAction *action = new IMAction(i18n("Open Log Viewer..."),
                                    KIcon(QLatin1String("documentation")),
                                    contact,
                                    account,
                                    LogViewer,
                                    parent);
        connect(action, SIGNAL(triggered(bool)), SLOT(onActionTriggered()));
        actions << action;
    }
    return actions;
}

void KPeopleActionsPlugin::onActionTriggered()
{
    IMAction *action = qobject_cast<IMAction*>(sender());
    KTp::ContactPtr contact = action->contact();
    Tp::AccountPtr account = action->account();
    IMActionType type = action->type();

    switch (type) {
        case TextChannel:
            KTp::Actions::startChat(account, contact);
            break;
        case AudioChannel:
            KTp::Actions::startAudioCall(account, contact);
            break;
{'fraction_non_alphanumeric': 0.10080025866946891, 'fraction_numerical': 0.004041710451863229, 'mean_word_length': 2.04055050380929, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7550924', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3170, 'n_words': 836}