summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Klapetek <[email protected]>2015-03-30 19:01:22 +0200
committerMartin Klapetek <[email protected]>2015-03-30 19:01:22 +0200
commit0b5d45d855ffed8cef991b5687971cdbeeea6e8f (patch)
tree2b139e3c0d9d80976a3df79a6cc6fb4769315c1c
parent6df24079b18e638ad9a32b3a3e54afa1acda684a (diff)
Add support for "Connect and start chat" action into kpeople actions
When the contact is offline, special action is returned which will first connect the account and then start the chat. This is done for text chat only as there is no way of knowing if the target contact has a support for audio/video calls before it's connected, while text chats will work in 98% of the cases. REVIEW: 123185
-rw-r--r--kpeople/actionsplugin/CMakeLists.txt1
-rw-r--r--kpeople/actionsplugin/kpeople-actions-plugin.cpp70
-rw-r--r--kpeople/actionsplugin/kpeople-actions-plugin.h3
3 files changed, 73 insertions, 1 deletions
diff --git a/kpeople/actionsplugin/CMakeLists.txt b/kpeople/actionsplugin/CMakeLists.txt
index f0f4dd5..c4ac2d7 100644
--- a/kpeople/actionsplugin/CMakeLists.txt
+++ b/kpeople/actionsplugin/CMakeLists.txt
@@ -5,6 +5,7 @@ add_library (ktp_kpeople_plugin MODULE
target_link_libraries (ktp_kpeople_plugin
KF5::People
KF5::PeopleBackend
+ KF5::PeopleWidgets
KF5::I18n
KF5::Service
KTp::CommonInternals
diff --git a/kpeople/actionsplugin/kpeople-actions-plugin.cpp b/kpeople/actionsplugin/kpeople-actions-plugin.cpp
index d065840..000dd35 100644
--- a/kpeople/actionsplugin/kpeople-actions-plugin.cpp
+++ b/kpeople/actionsplugin/kpeople-actions-plugin.cpp
@@ -35,6 +35,7 @@
#include <TelepathyQt/Constants>
#include <KPeople/PersonData>
+#include <kpeople/widgets/actions.h> // pretty headers are currently broken for this
enum IMActionType {
TextChannel,
@@ -57,6 +58,7 @@ public:
IMActionType type() const;
QUrl uri() const;
private:
+ void setActionType();
KTp::ContactPtr m_contact;
Tp::AccountPtr m_account;
QUrl m_uri;
@@ -70,6 +72,7 @@ IMAction::IMAction(const QString &text, const QIcon &icon, const KTp::ContactPtr
m_account(account),
m_type(type)
{
+ setActionType();
}
IMAction::IMAction(const QString &text, const QIcon &icon, const QUrl &uri,
@@ -78,6 +81,28 @@ IMAction::IMAction(const QString &text, const QIcon &icon, const QUrl &uri,
m_uri(uri),
m_type(type)
{
+ setActionType();
+}
+
+void IMAction::setActionType()
+{
+ switch (m_type) {
+ case TextChannel:
+ setProperty("actionType", KPeople::TextChatAction);
+ break;
+ case AudioChannel:
+ setProperty("actionType", KPeople::AudioCallAction);
+ break;
+ case VideoChannel:
+ setProperty("actionType", KPeople::VideoCallAction);
+ break;
+ case FileTransfer:
+ setProperty("actionType", KPeople::SendFileAction);
+ break;
+ default:
+ setProperty("actionType", KPeople::OtherAction);
+ break;
+ }
}
KTp::ContactPtr IMAction::contact() const
@@ -157,7 +182,19 @@ QList<QAction*> KPeopleActionsPlugin::actionsForPerson(const KPeople::PersonData
}
const KTp::ContactPtr contact = KTp::contactManager()->contactForContactId(accountPath, contactId);
- if (!contact || !contact->manager()) {
+ if (!contact || !contact->manager() || account->currentPresence().type() == Tp::ConnectionPresenceTypeOffline) {
+ // if there is no valid contact or if the contact is online,
+ // add a special action that will first connect the account
+ // and then start a chat right away
+ QAction *action = new IMAction(i18nc("Context menu action; means 'Bring your account online and then start a chat using %1 account'",
+ "Connect and Start Chat Using %1...", account->displayName()),
+ QIcon::fromTheme(QStringLiteral("text-x-generic")),
+ contactId,
+ TextChannel,
+ parent);
+ action->setProperty("accountPath", accountPath);
+ connect(action, SIGNAL(triggered(bool)), SLOT(onConnectAndActionTriggered()));
+ actions << action;
continue;
}
@@ -262,6 +299,37 @@ void KPeopleActionsPlugin::onActionTriggered()
}
}
+void KPeopleActionsPlugin::onConnectAndActionTriggered()
+{
+ IMAction *action = qobject_cast<IMAction*>(sender());
+
+ const Tp::AccountPtr account = KTp::contactManager()->accountForAccountPath(action->property("accountPath").toString());
+ account->setProperty("contactId", action->uri());
+ connect(account.data(), &Tp::Account::connectionStatusChanged, this, &KPeopleActionsPlugin::onAccountConnectionStatusChanged);
+ account->setRequestedPresence(Tp::Presence::available());
+}
+
+void KPeopleActionsPlugin::onAccountConnectionStatusChanged(Tp::ConnectionStatus status)
+{
+ Tp::AccountPtr account = Tp::AccountPtr(qobject_cast<Tp::Account*>(sender()));
+ if (!account || status != Tp::ConnectionStatusConnected) {
+ return;
+ }
+
+ QString contactId = account->property("contactId").toString();
+
+ if (contactId.isEmpty()) {
+ return;
+ }
+
+ account->ensureTextChat(contactId,
+ QDateTime::currentDateTime(),
+ QLatin1String("org.freedesktop.Telepathy.Client.KTp.TextUi"));
+
+ // avoid calling this slot repeatedly on account connection changes
+ disconnect(account.data(), &Tp::Account::connectionStatusChanged, this, &KPeopleActionsPlugin::onAccountConnectionStatusChanged);
+}
+
K_PLUGIN_FACTORY( KPeopleActionsPluginFactory, registerPlugin<KPeopleActionsPlugin>(); )
K_EXPORT_PLUGIN( KPeopleActionsPluginFactory("ktp_kpeople_plugin", "ktp-common-internals") )
diff --git a/kpeople/actionsplugin/kpeople-actions-plugin.h b/kpeople/actionsplugin/kpeople-actions-plugin.h
index b80bef8..4208e1e 100644
--- a/kpeople/actionsplugin/kpeople-actions-plugin.h
+++ b/kpeople/actionsplugin/kpeople-actions-plugin.h
@@ -20,6 +20,7 @@
#define IM_PLUGIN_H
#include <KPeopleBackend/AbstractPersonAction>
+#include <TelepathyQt/Connection>
class KPeopleActionsPlugin : public KPeople::AbstractPersonAction
{
@@ -31,6 +32,8 @@ public:
private Q_SLOTS:
void onActionTriggered();
+ void onConnectAndActionTriggered();
+ void onAccountConnectionStatusChanged(Tp::ConnectionStatus status);
};
#endif // IM_PLUGIN_H