From 5b1fa1288df9627499aca770462334502810fa57 Mon Sep 17 00:00:00 2001 From: mateus Date: Sun, 5 Jun 2016 17:27:43 -0300 Subject: [PATCH 1/5] buddy list and presence --- apps/telepathy/Connection.cxx | 287 ++++++++++++++++++++++++++++++++-- apps/telepathy/Connection.hxx | 116 ++++++++------ 2 files changed, 338 insertions(+), 65 deletions(-) diff --git a/apps/telepathy/Connection.cxx b/apps/telepathy/Connection.cxx index 43b57f4d3f..b387585f07 100644 --- a/apps/telepathy/Connection.cxx +++ b/apps/telepathy/Connection.cxx @@ -30,6 +30,7 @@ #include "Common.hxx" #include "SipCallChannel.hxx" + using namespace recon; using namespace resip; using namespace std; @@ -80,7 +81,7 @@ tr::Connection::Connection(const QDBusConnection &dbusConnection, const QString << TP_QT_IFACE_CONNECTION << TP_QT_IFACE_CONNECTION_INTERFACE_CONTACT_LIST << TP_QT_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE - //<< TP_QT_IFACE_CONNECTION_INTERFACE_ALIASING + << TP_QT_IFACE_CONNECTION_INTERFACE_ALIASING << TP_QT_IFACE_CONNECTION_INTERFACE_REQUESTS //<< TP_QT_IFACE_CONNECTION_INTERFACE_AVATARS ); @@ -97,14 +98,21 @@ tr::Connection::Connection(const QDBusConnection &dbusConnection, const QString mContactListInterface->setContactListPersists(true); mContactListInterface->setCanChangeContactList(true); mContactListInterface->setDownloadAtConnection(true); - //mContactListInterface->setGetContactListAttributesCallback(Tp::memFun(this, &Connection::getContactListAttributes)); - //mContactListInterface->setRequestSubscriptionCallback(Tp::memFun(this, &Connection::requestSubscription)); - //mContactListInterface->setAuthorizePublicationCallback(Tp::memFun(this, &Connection::authorizePublication)); - //mContactListInterface->setRemoveContactsCallback(Tp::memFun(this, &Connection::removeContacts)); + mContactListInterface->setGetContactListAttributesCallback(Tp::memFun(this, &Connection::getContactListAttributes)); + mContactListInterface->setRequestSubscriptionCallback(Tp::memFun(this, &Connection::requestSubscription)); + // mContactListInterface->setAuthorizePublicationCallback(Tp::memFun(this, &Connection::authorizePublication)); + mContactListInterface->setRemoveContactsCallback(Tp::memFun(this, &Connection::removeContacts)); //mContactListInterface->setUnsubscribeCallback(Tp::memFun(this, &Connection::unsubscribe)); //mContactListInterface->setUnpublishCallback(Tp::memFun(this, &Connection::unpublish)); plugInterface(Tp::AbstractConnectionInterfacePtr::dynamicCast(mContactListInterface)); + /* Connection.Interface.Aliasing */ + mAliasingInterface = Tp::BaseConnectionAliasingInterface::create(); + mAliasingInterface->setGetAliasesCallback(Tp::memFun(this, &Connection::getAliases)); + mAliasingInterface->setSetAliasesCallback(Tp::memFun(this, &Connection::setAliases)); + plugInterface(Tp::AbstractConnectionInterfacePtr::dynamicCast(mAliasingInterface)); + + /* Connection.Interface.Requests */ mRequestsInterface = Tp::BaseConnectionRequestsInterface::create(this); /* Fill requestableChannelClasses */ @@ -137,6 +145,7 @@ tr::Connection::Connection(const QDBusConnection &dbusConnection, const QString uint _self = ensureHandle(account); setSelfContact(_self, account); + setConnectCallback(Tp::memFun(this, &Connection::doConnect)); setInspectHandlesCallback(Tp::memFun(this, &Connection::inspectHandles)); setCreateChannelCallback(Tp::memFun(this, &Connection::createChannel)); @@ -144,6 +153,154 @@ tr::Connection::Connection(const QDBusConnection &dbusConnection, const QString connect(this, SIGNAL(disconnected()), SLOT(doDisconnect())); } + +void +tr::Connection::getContactsFromFile(Tp::DBusError *error) +{ + QFile file(fileWithContacts); + + if(file.open(QFile::ReadOnly)){ + QTextStream in(&file); + QString line = in.readLine(); + Tp::AliasMap aliases; + while(!line.isNull()){ + uint handle = ensureHandle(line.split(" ").at(0)); + aliases[handle] = line.split(" ").at(1); + line = in.readLine(); + } + setAliases(aliases, error); + } else{ + file.open(QFile::WriteOnly); + } + + mContactListInterface->setContactListState(Tp::ContactListStateSuccess); +} + +void +tr::Connection::setContactsInFile() +{ + QFile file(fileWithContacts); + + if(file.open(QIODevice::ReadWrite | QIODevice::Truncate)){ + QTextStream stream(&file); + QMap::iterator it; + for(it = mHandles.begin(); it != mHandles.end(); it++){ + if(it.key() == selfHandle()) continue; + + stream << it.value() << " " << mAliases[it.key()] << endl; + } + file.close(); + } else{ + ErrLog(<<"couldn't write contacts to file" << endl); + } +} + +Tp::AliasMap +tr::Connection::getAliases(const Tp::UIntList& handles, Tp::DBusError *error) +{ + qDebug() << Q_FUNC_INFO << handles; + + Tp::AliasMap aliases; + foreach (uint handle, handles) { + aliases[handle] = mAliases[handle]; + } + + return aliases; +} + +void +tr::Connection::setAliases(const Tp::AliasMap &aliases, Tp::DBusError *error) +{ + qDebug() << Q_FUNC_INFO << aliases; + + for(Tp::AliasMap::const_iterator it = aliases.begin(); it != aliases.end(); it++){ + mAliases[it.key()] = it.value(); + } + +} + +void +tr::Connection::deleteContacts(const QStringList& contacts) +{ + + Tp::ContactSubscriptionMap changes; + Tp::HandleIdentifierMap identifiers; + Tp::HandleIdentifierMap removals; + Q_FOREACH(const QString &contact, contacts) { + uint id = mIdentifiers[contact]; + removals[id] = contact; + + mIdentifiers.remove(contact); + mHandles.remove(id); + } + mContactListInterface->contactsChangedWithID(changes, identifiers, removals); + + setContactsInFile(); +} + + +Tp::ContactAttributesMap +tr::Connection::getContactListAttributes(const QStringList &interfaces, bool hold, Tp::DBusError *error) +{ + Tp::UIntList handles = mHandles.keys(); + handles.removeOne(selfHandle()); + + StackLog(<<"getContactListAttributes()"); + qDebug() << handles; + + return getContactAttributes(handles, interfaces, error); +} + +void +tr::Connection::requestSubscription(const Tp::UIntList &handles, const QString &message, Tp::DBusError *error) +{ + + QStringList contacts = inspectHandles(Tp::HandleTypeContact, handles, error); + + if (error->isValid()) { + return; + } + + if(contacts.isEmpty()){ + error->set(TP_QT_ERROR_INVALID_HANDLE, QLatin1String("Invalid handle(s)")); + } + + Tp::ContactSubscriptionMap changes; + Tp::HandleIdentifierMap identifiers; + Tp::HandleIdentifierMap removals; + Q_FOREACH(const QString &contact, contacts) { + uint handle = ensureHandle(contact); + + Tp::ContactSubscriptions change; + change.publish = Tp::SubscriptionStateYes; + change.publishRequest = QString(); + change.subscribe = Tp::SubscriptionStateUnknown; + + changes[handle] = change; + identifiers[handle] = contact; + } + mContactListInterface->contactsChangedWithID(changes, identifiers, removals); + + setContactsInFile(); +} + +void +tr::Connection::removeContacts(const Tp::UIntList &handles, Tp::DBusError *error) +{ + QStringList contacts = inspectHandles(Tp::HandleTypeContact, handles, error); + + if(error->isValid()){ + return; + } + + if(contacts.isEmpty()){ + error->set(TP_QT_ERROR_INVALID_HANDLE, QLatin1String("Invalid handle(s)")); + } + + deleteContacts(contacts); +} + + void tr::Connection::doConnect(Tp::DBusError *error) { @@ -153,8 +310,12 @@ tr::Connection::doConnect(Tp::DBusError *error) ua->startup(); myConversationManager->startup(); ua->run(); - + statusMap = getSimpleStatusSpecMap(); + mContactListInterface->setContactListState(Tp::ContactListStateWaiting); + + fileWithContacts = QString::fromStdString("data.txt"); + getContactsFromFile(error); } void @@ -163,19 +324,103 @@ tr::Connection::onConnected() setStatus(Tp::ConnectionStatusConnected, Tp::ConnectionStatusReasonRequested); Tp::SimpleContactPresences presences; - mSelfPresence.type = Tp::ConnectionPresenceTypeAvailable; - mSelfPresence.status = QLatin1String("available"); + Tp::DBusError error; + setPresence(QLatin1String("available"), QLatin1String(""), &error); + presences[selfHandle()] = mSelfPresence; + // TODO: get presence from contacts mSimplePresenceInterface->setPresences(presences); } + +// got this method from https://github.com/TelepathyQt/telepathy-morse/blob/master/connection.cpp +Tp::SimpleStatusSpecMap +tr::Connection::getSimpleStatusSpecMap() +{ + + Tp::SimpleStatusSpec spAvailable; + spAvailable.type = Tp::ConnectionPresenceTypeAvailable; + spAvailable.maySetOnSelf = true; + spAvailable.canHaveMessage = true; + + Tp::SimpleStatusSpec spBusy; + spBusy.type = Tp::ConnectionPresenceTypeBusy; + spBusy.maySetOnSelf = true; + spBusy.canHaveMessage = true; + + Tp::SimpleStatusSpec spAway; + spAway.type = Tp::ConnectionPresenceTypeAway; + spAway.maySetOnSelf = true; + spAway.canHaveMessage = true; + + Tp::SimpleStatusSpec spOffline; + spOffline.type = Tp::ConnectionPresenceTypeOffline; + spOffline.maySetOnSelf = true; + spOffline.canHaveMessage = true; + + + Tp::SimpleStatusSpec spHidden; + spHidden.type = Tp::ConnectionPresenceTypeHidden; + spHidden.maySetOnSelf = true; + spHidden.canHaveMessage = true; + + Tp::SimpleStatusSpec spUnknown; + spUnknown.type = Tp::ConnectionPresenceTypeUnknown; + spUnknown.maySetOnSelf = false; + spUnknown.canHaveMessage = true; + + Tp::SimpleStatusSpecMap specs; + specs.insert(QLatin1String("available"), spAvailable); + specs.insert(QLatin1String("dnd"), spBusy); + specs.insert(QLatin1String("away"), spAway); + specs.insert(QLatin1String("offline"), spOffline); + specs.insert(QLatin1String("hidden"), spHidden); + specs.insert(QLatin1String("unknown"), spUnknown); + return specs; +} + uint tr::Connection::setPresence(const QString &status, const QString &message, Tp::DBusError *error) { - //FIXME - return selfHandle(); + // TODO: find out why message is always getting here empty + + StackLog(<<"setPresence()"); + + mSelfPresence.type = statusMap[status].type; + mSelfPresence.status = status; + if(statusMap[status].canHaveMessage){ + mSelfPresence.statusMessage = message; + } + + qDebug() << "status = " << mSelfPresence.status << " type = " << mSelfPresence.type << " message = " << message; + + return selfHandle(); +} + +Tp::SimpleContactPresences +tr::Connection::getPresences(const Tp::UIntList &handles) +{ + StackLog(<<"getPresences()"); + + Tp::SimpleContactPresences presences; + foreach (uint handle, handles) { + presences[handle] = getPresence(handle); + } + + return presences; } +Tp::SimplePresence +tr::Connection::getPresence(uint handle) +{ + StackLog(<<"getPresence()"); + if (!mPresences.contains(handle)) { + return Tp::SimplePresence(); + } + qDebug() << "presence = " << mPresences.value(handle).status; + + return mPresences.value(handle); +} void tr::Connection::doDisconnect() @@ -264,8 +509,9 @@ tr::Connection::requestHandles(uint handleType, const QStringList &identifiers, } Q_FOREACH(const QString &identifier, identifiers) { - ensureHandle(identifier); - result.append(mIdentifiers[identifier]); + ensureHandle(identifier); + if(mIdentifiers.find(identifier) != mIdentifiers.end()) + result.append(mIdentifiers[identifier]); } return result; @@ -347,21 +593,28 @@ Tp::ContactAttributesMap tr::Connection::getContactAttributes(const Tp::UIntList &handles, const QStringList &ifaces, Tp::DBusError *error) { StackLog(<<"getContactAttributes"); - qDebug() << "getContactAttributes" << handles << ifaces; Tp::ContactAttributesMap attributesMap; Q_FOREACH(uint handle, handles) { QVariantMap attributes; - QStringList inspectedHandles = inspectHandles(Tp::HandleTypeContact, Tp::UIntList() << handle, error); - if (inspectedHandles.size() > 0) { - attributes[TP_QT_IFACE_CONNECTION+"/contact-id"] = inspectedHandles.at(0); + QStringList inspectedHandle = inspectHandles(Tp::HandleTypeContact, Tp::UIntList() << handle, error); + if (inspectedHandle.size() > 0) { + attributes[TP_QT_IFACE_CONNECTION+"/contact-id"] = inspectedHandle.at(0); } else { continue; } + qDebug() << "handle = " << handle << " inspectedHandle = " << inspectedHandle; + if (ifaces.contains(TP_QT_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE)) { - attributes[TP_QT_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE+"/presence"] = QVariant::fromValue(mSelfPresence); + attributes[TP_QT_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE+"/presence"] = QVariant::fromValue(getPresence(handle)); } + + if (ifaces.contains(TP_QT_IFACE_CONNECTION_INTERFACE_ALIASING)) { + attributes[TP_QT_IFACE_CONNECTION_INTERFACE_ALIASING + QLatin1String("/alias")] = QVariant::fromValue(mAliases[handle]); + } + attributesMap[handle] = attributes; } + return attributesMap; } diff --git a/apps/telepathy/Connection.hxx b/apps/telepathy/Connection.hxx index 9de94ad197..38c7ec3ce9 100644 --- a/apps/telepathy/Connection.hxx +++ b/apps/telepathy/Connection.hxx @@ -19,7 +19,7 @@ #define CONNECTION_HXX #if defined(HAVE_CONFIG_H) - #include "config.h" +#include "config.h" #endif @@ -39,53 +39,73 @@ namespace tr { -class MyConversationManager; -class MyUserAgent; - -class Connection : public Tp::BaseConnection -{ - Q_OBJECT -public: - Connection(const QDBusConnection &dbusConnection, - const QString &cmName, const QString &protocolName, - const QVariantMap ¶meters); - - MyConversationManager& getConversationManager() { return *myConversationManager.get(); }; - -private: - uint setPresence(const QString &status, const QString &message, Tp::DBusError *error); - QStringList inspectHandles(uint handleType, const Tp::UIntList &handles, Tp::DBusError *error); - Tp::UIntList requestHandles(uint handleType, const QStringList &identifiers, Tp::DBusError *error); - Tp::BaseChannelPtr createChannel(const QVariantMap &request, Tp::DBusError *error); - Tp::ContactAttributesMap getContactAttributes(const Tp::UIntList &handles, const QStringList &ifaces, Tp::DBusError *error); - uint ensureHandle(const QString& identifier); - - -private slots: - void doConnect(Tp::DBusError *error); - void onConnected(); - void doDisconnect(); - void setStatusSlot(uint newStatus, uint reason); - void onIncomingCall(const QString & caller, uint callHandle); - - -private: - resip::SharedPtr mUAProfile; - resip::SharedPtr mConversationProfile; - tr::MyUserAgent* ua; - std::auto_ptr myConversationManager; - - Tp::BaseConnectionContactsInterfacePtr mContactsInterface; - Tp::BaseConnectionSimplePresenceInterfacePtr mSimplePresenceInterface; - Tp::BaseConnectionContactListInterfacePtr mContactListInterface; - Tp::BaseConnectionRequestsInterfacePtr mRequestsInterface; - - long nextHandleId; - QMap mHandles; - QMap mIdentifiers; - - Tp::SimplePresence mSelfPresence; -}; + class MyConversationManager; + class MyUserAgent; + + class Connection : public Tp::BaseConnection + { + Q_OBJECT + public: + Connection(const QDBusConnection &dbusConnection, + const QString &cmName, const QString &protocolName, + const QVariantMap ¶meters); + + MyConversationManager& getConversationManager() { return *myConversationManager.get(); }; + + Tp::ContactAttributesMap getContactListAttributes(const QStringList &interfaces, bool hold, Tp::DBusError *error); + + void requestSubscription(const Tp::UIntList &handles, const QString &message, Tp::DBusError *error); + void removeContacts(const Tp::UIntList &handles, Tp::DBusError *error); + Tp::AliasMap getAliases(const Tp::UIntList& handles, Tp::DBusError *error); + void setAliases(const Tp::AliasMap &aliases, Tp::DBusError *error); + void getContactsFromFile(Tp::DBusError *error); + void setContactsInFile(); + void deleteContacts(const QStringList& contacts); + Tp::SimpleStatusSpecMap getSimpleStatusSpecMap(); + Tp::SimpleContactPresences getPresences(const Tp::UIntList &handles); + Tp::SimplePresence getPresence(uint handle); + + + private: + uint setPresence(const QString &status, const QString &message, Tp::DBusError *error); + QStringList inspectHandles(uint handleType, const Tp::UIntList &handles, Tp::DBusError *error); + Tp::UIntList requestHandles(uint handleType, const QStringList &identifiers, Tp::DBusError *error); + Tp::BaseChannelPtr createChannel(const QVariantMap &request, Tp::DBusError *error); + Tp::ContactAttributesMap getContactAttributes(const Tp::UIntList &handles, const QStringList &ifaces, Tp::DBusError *error); + uint ensureHandle(const QString& identifier); + + + private slots: + void doConnect(Tp::DBusError *error); + void onConnected(); + void doDisconnect(); + void setStatusSlot(uint newStatus, uint reason); + void onIncomingCall(const QString & caller, uint callHandle); + + + private: + resip::SharedPtr mUAProfile; + resip::SharedPtr mConversationProfile; + tr::MyUserAgent* ua; + std::auto_ptr myConversationManager; + + Tp::BaseConnectionContactsInterfacePtr mContactsInterface; + Tp::BaseConnectionAliasingInterfacePtr mAliasingInterface; + Tp::BaseConnectionSimplePresenceInterfacePtr mSimplePresenceInterface; + Tp::BaseConnectionContactListInterfacePtr mContactListInterface; + Tp::BaseConnectionRequestsInterfacePtr mRequestsInterface; + + long nextHandleId; + QMap mHandles; + QMap mIdentifiers; + Tp::AliasMap mAliases; + + Tp::SimpleStatusSpecMap statusMap; + Tp::SimplePresence mSelfPresence; + Tp::SimpleContactPresences mPresences; + + QString fileWithContacts; + }; } From 1b44e5f86269c1286b5bdc80229d956eaab7bc45 Mon Sep 17 00:00:00 2001 From: mateus Date: Sat, 11 Jun 2016 11:32:21 -0300 Subject: [PATCH 2/5] buddy list and presence refactored --- apps/telepathy/Connection.cxx | 359 +++++++++++++++++----------------- apps/telepathy/Connection.hxx | 118 ++++++----- 2 files changed, 241 insertions(+), 236 deletions(-) diff --git a/apps/telepathy/Connection.cxx b/apps/telepathy/Connection.cxx index b387585f07..0cdb40c885 100644 --- a/apps/telepathy/Connection.cxx +++ b/apps/telepathy/Connection.cxx @@ -30,13 +30,14 @@ #include "Common.hxx" #include "SipCallChannel.hxx" - using namespace recon; using namespace resip; using namespace std; #define RESIPROCATE_SUBSYSTEM ReconSubsystem::RECON +static const QString c_fileWithContacts = QLatin1String("data.txt"); + tr::Connection::Connection(const QDBusConnection &dbusConnection, const QString &cmName, const QString &protocolName, const QVariantMap ¶meters) : Tp::BaseConnection(dbusConnection, cmName, protocolName, parameters), mUAProfile(new TelepathyMasterProfile(parameters)), @@ -93,25 +94,24 @@ tr::Connection::Connection(const QDBusConnection &dbusConnection, const QString mSimplePresenceInterface->setSetPresenceCallback(Tp::memFun(this, &Connection::setPresence)); plugInterface(Tp::AbstractConnectionInterfacePtr::dynamicCast(mSimplePresenceInterface)); - /* Connection.Interface.ContactList */ - mContactListInterface = Tp::BaseConnectionContactListInterface::create(); - mContactListInterface->setContactListPersists(true); - mContactListInterface->setCanChangeContactList(true); - mContactListInterface->setDownloadAtConnection(true); - mContactListInterface->setGetContactListAttributesCallback(Tp::memFun(this, &Connection::getContactListAttributes)); - mContactListInterface->setRequestSubscriptionCallback(Tp::memFun(this, &Connection::requestSubscription)); - // mContactListInterface->setAuthorizePublicationCallback(Tp::memFun(this, &Connection::authorizePublication)); - mContactListInterface->setRemoveContactsCallback(Tp::memFun(this, &Connection::removeContacts)); - //mContactListInterface->setUnsubscribeCallback(Tp::memFun(this, &Connection::unsubscribe)); - //mContactListInterface->setUnpublishCallback(Tp::memFun(this, &Connection::unpublish)); - plugInterface(Tp::AbstractConnectionInterfacePtr::dynamicCast(mContactListInterface)); - - /* Connection.Interface.Aliasing */ - mAliasingInterface = Tp::BaseConnectionAliasingInterface::create(); - mAliasingInterface->setGetAliasesCallback(Tp::memFun(this, &Connection::getAliases)); - mAliasingInterface->setSetAliasesCallback(Tp::memFun(this, &Connection::setAliases)); - plugInterface(Tp::AbstractConnectionInterfacePtr::dynamicCast(mAliasingInterface)); - + /* Connection.Interface.ContactList */ + mContactListInterface = Tp::BaseConnectionContactListInterface::create(); + mContactListInterface->setContactListPersists(true); + mContactListInterface->setCanChangeContactList(true); + mContactListInterface->setDownloadAtConnection(true); + mContactListInterface->setGetContactListAttributesCallback(Tp::memFun(this, &Connection::getContactListAttributes)); + mContactListInterface->setRequestSubscriptionCallback(Tp::memFun(this, &Connection::requestSubscription)); + // mContactListInterface->setAuthorizePublicationCallback(Tp::memFun(this, &Connection::authorizePublication)); + mContactListInterface->setRemoveContactsCallback(Tp::memFun(this, &Connection::removeContacts)); + //mContactListInterface->setUnsubscribeCallback(Tp::memFun(this, &Connection::unsubscribe)); + //mContactListInterface->setUnpublishCallback(Tp::memFun(this, &Connection::unpublish)); + plugInterface(Tp::AbstractConnectionInterfacePtr::dynamicCast(mContactListInterface)); + + /* Connection.Interface.Aliasing */ + mAliasingInterface = Tp::BaseConnectionAliasingInterface::create(); + mAliasingInterface->setGetAliasesCallback(Tp::memFun(this, &Connection::getAliases)); + mAliasingInterface->setSetAliasesCallback(Tp::memFun(this, &Connection::setAliases)); + plugInterface(Tp::AbstractConnectionInterfacePtr::dynamicCast(mAliasingInterface)); /* Connection.Interface.Requests */ mRequestsInterface = Tp::BaseConnectionRequestsInterface::create(this); @@ -144,7 +144,6 @@ tr::Connection::Connection(const QDBusConnection &dbusConnection, const QString const QString& account = parameters.value(QLatin1String("account")).toString(); uint _self = ensureHandle(account); setSelfContact(_self, account); - setConnectCallback(Tp::memFun(this, &Connection::doConnect)); setInspectHandlesCallback(Tp::memFun(this, &Connection::inspectHandles)); @@ -152,70 +151,84 @@ tr::Connection::Connection(const QDBusConnection &dbusConnection, const QString setRequestHandlesCallback(Tp::memFun(this, &Connection::requestHandles)); connect(this, SIGNAL(disconnected()), SLOT(doDisconnect())); } - void tr::Connection::getContactsFromFile(Tp::DBusError *error) { - QFile file(fileWithContacts); - - if(file.open(QFile::ReadOnly)){ - QTextStream in(&file); - QString line = in.readLine(); - Tp::AliasMap aliases; - while(!line.isNull()){ - uint handle = ensureHandle(line.split(" ").at(0)); - aliases[handle] = line.split(" ").at(1); - line = in.readLine(); - } - setAliases(aliases, error); - } else{ - file.open(QFile::WriteOnly); - } - - mContactListInterface->setContactListState(Tp::ContactListStateSuccess); + if (error->isValid()) { + return; + } + + QFile file(c_fileWithContacts); + + if(file.open(QFile::ReadOnly)){ + QTextStream in(&file); + QString line = in.readLine(); + Tp::AliasMap aliases; + while(!line.isNull()){ + uint handle = ensureHandle(line.split(" ").at(0)); + aliases[handle] = line.split(" ").at(1); + line = in.readLine(); + } + setAliases(aliases, error); + } else{ + file.open(QFile::WriteOnly); + } + + mContactListInterface->setContactListState(Tp::ContactListStateSuccess); } void tr::Connection::setContactsInFile() { - QFile file(fileWithContacts); - - if(file.open(QIODevice::ReadWrite | QIODevice::Truncate)){ - QTextStream stream(&file); - QMap::iterator it; - for(it = mHandles.begin(); it != mHandles.end(); it++){ - if(it.key() == selfHandle()) continue; - - stream << it.value() << " " << mAliases[it.key()] << endl; - } - file.close(); - } else{ - ErrLog(<<"couldn't write contacts to file" << endl); - } + QFile file(c_fileWithContacts); + + if(file.open(QIODevice::ReadWrite | QIODevice::Truncate)){ + QTextStream stream(&file); + QMap::iterator it; + for(it = mHandles.begin(); it != mHandles.end(); it++){ + if(it.key() == selfHandle()) continue; + + stream << it.value() << " " << mAliases[it.key()] << endl; + } + file.close(); + } else{ + ErrLog(<<"couldn't write contacts to file" << endl); + } } Tp::AliasMap tr::Connection::getAliases(const Tp::UIntList& handles, Tp::DBusError *error) { - qDebug() << Q_FUNC_INFO << handles; + if (error->isValid()) { + return Tp::AliasMap(); + } + + qDebug() << Q_FUNC_INFO << handles; - Tp::AliasMap aliases; - foreach (uint handle, handles) { - aliases[handle] = mAliases[handle]; + Tp::AliasMap aliases; + Q_FOREACH(uint handle, handles) { + if (mAliases.find(handle) == mAliases.end()) { + error->set(TP_QT_ERROR_INVALID_HANDLE, QLatin1String("Invalid handle(s)")); + } + aliases[handle] = mAliases[handle]; } - return aliases; + return aliases; } void tr::Connection::setAliases(const Tp::AliasMap &aliases, Tp::DBusError *error) { - qDebug() << Q_FUNC_INFO << aliases; + if (error->isValid()) { + return; + } + + qDebug() << Q_FUNC_INFO << aliases; - for(Tp::AliasMap::const_iterator it = aliases.begin(); it != aliases.end(); it++){ - mAliases[it.key()] = it.value(); - } + for(Tp::AliasMap::const_iterator it = aliases.begin(); it != aliases.end(); it++){ + mAliases[it.key()] = it.value(); + } } @@ -223,81 +236,78 @@ void tr::Connection::deleteContacts(const QStringList& contacts) { - Tp::ContactSubscriptionMap changes; - Tp::HandleIdentifierMap identifiers; - Tp::HandleIdentifierMap removals; - Q_FOREACH(const QString &contact, contacts) { - uint id = mIdentifiers[contact]; - removals[id] = contact; - - mIdentifiers.remove(contact); - mHandles.remove(id); - } - mContactListInterface->contactsChangedWithID(changes, identifiers, removals); + Tp::ContactSubscriptionMap changes; + Tp::HandleIdentifierMap identifiers; + Tp::HandleIdentifierMap removals; + Q_FOREACH(const QString &contact, contacts) { + uint id = mIdentifiers[contact]; + removals[id] = contact; + } + mContactListInterface->contactsChangedWithID(changes, identifiers, removals); - setContactsInFile(); + setContactsInFile(); } Tp::ContactAttributesMap tr::Connection::getContactListAttributes(const QStringList &interfaces, bool hold, Tp::DBusError *error) { - Tp::UIntList handles = mHandles.keys(); - handles.removeOne(selfHandle()); + Tp::UIntList handles = mHandles.keys(); + handles.removeOne(selfHandle()); - StackLog(<<"getContactListAttributes()"); - qDebug() << handles; + StackLog(<<"getContactListAttributes()"); + qDebug() << handles; - return getContactAttributes(handles, interfaces, error); + return getContactAttributes(handles, interfaces, error); } void tr::Connection::requestSubscription(const Tp::UIntList &handles, const QString &message, Tp::DBusError *error) { - QStringList contacts = inspectHandles(Tp::HandleTypeContact, handles, error); + QStringList contacts = inspectHandles(Tp::HandleTypeContact, handles, error); - if (error->isValid()) { - return; - } + if (error->isValid()) { + return; + } - if(contacts.isEmpty()){ - error->set(TP_QT_ERROR_INVALID_HANDLE, QLatin1String("Invalid handle(s)")); - } - - Tp::ContactSubscriptionMap changes; - Tp::HandleIdentifierMap identifiers; - Tp::HandleIdentifierMap removals; - Q_FOREACH(const QString &contact, contacts) { - uint handle = ensureHandle(contact); + if (contacts.isEmpty()) { + error->set(TP_QT_ERROR_INVALID_HANDLE, QLatin1String("Invalid handle(s)")); + } + + Tp::ContactSubscriptionMap changes; + Tp::HandleIdentifierMap identifiers; + Tp::HandleIdentifierMap removals; + Q_FOREACH(const QString &contact, contacts) { + uint handle = ensureHandle(contact); - Tp::ContactSubscriptions change; - change.publish = Tp::SubscriptionStateYes; - change.publishRequest = QString(); - change.subscribe = Tp::SubscriptionStateUnknown; + Tp::ContactSubscriptions change; + change.publish = Tp::SubscriptionStateYes; + change.publishRequest = QString(); + change.subscribe = Tp::SubscriptionStateUnknown; - changes[handle] = change; - identifiers[handle] = contact; - } - mContactListInterface->contactsChangedWithID(changes, identifiers, removals); + changes[handle] = change; + identifiers[handle] = contact; + } + mContactListInterface->contactsChangedWithID(changes, identifiers, removals); - setContactsInFile(); + setContactsInFile(); } void tr::Connection::removeContacts(const Tp::UIntList &handles, Tp::DBusError *error) { - QStringList contacts = inspectHandles(Tp::HandleTypeContact, handles, error); + QStringList contacts = inspectHandles(Tp::HandleTypeContact, handles, error); - if(error->isValid()){ - return; - } + if (error->isValid()) { + return; + } - if(contacts.isEmpty()){ - error->set(TP_QT_ERROR_INVALID_HANDLE, QLatin1String("Invalid handle(s)")); - } + if (contacts.isEmpty()) { + error->set(TP_QT_ERROR_INVALID_HANDLE, QLatin1String("Invalid handle(s)")); + } - deleteContacts(contacts); + deleteContacts(contacts); } @@ -314,7 +324,6 @@ tr::Connection::doConnect(Tp::DBusError *error) mContactListInterface->setContactListState(Tp::ContactListStateWaiting); - fileWithContacts = QString::fromStdString("data.txt"); getContactsFromFile(error); } @@ -338,45 +347,45 @@ Tp::SimpleStatusSpecMap tr::Connection::getSimpleStatusSpecMap() { - Tp::SimpleStatusSpec spAvailable; - spAvailable.type = Tp::ConnectionPresenceTypeAvailable; - spAvailable.maySetOnSelf = true; - spAvailable.canHaveMessage = true; - - Tp::SimpleStatusSpec spBusy; - spBusy.type = Tp::ConnectionPresenceTypeBusy; - spBusy.maySetOnSelf = true; - spBusy.canHaveMessage = true; - - Tp::SimpleStatusSpec spAway; - spAway.type = Tp::ConnectionPresenceTypeAway; - spAway.maySetOnSelf = true; - spAway.canHaveMessage = true; - - Tp::SimpleStatusSpec spOffline; - spOffline.type = Tp::ConnectionPresenceTypeOffline; - spOffline.maySetOnSelf = true; - spOffline.canHaveMessage = true; - - - Tp::SimpleStatusSpec spHidden; - spHidden.type = Tp::ConnectionPresenceTypeHidden; - spHidden.maySetOnSelf = true; - spHidden.canHaveMessage = true; - - Tp::SimpleStatusSpec spUnknown; - spUnknown.type = Tp::ConnectionPresenceTypeUnknown; - spUnknown.maySetOnSelf = false; - spUnknown.canHaveMessage = true; - - Tp::SimpleStatusSpecMap specs; - specs.insert(QLatin1String("available"), spAvailable); - specs.insert(QLatin1String("dnd"), spBusy); - specs.insert(QLatin1String("away"), spAway); - specs.insert(QLatin1String("offline"), spOffline); - specs.insert(QLatin1String("hidden"), spHidden); - specs.insert(QLatin1String("unknown"), spUnknown); - return specs; + Tp::SimpleStatusSpec spAvailable; + spAvailable.type = Tp::ConnectionPresenceTypeAvailable; + spAvailable.maySetOnSelf = true; + spAvailable.canHaveMessage = true; + + Tp::SimpleStatusSpec spBusy; + spBusy.type = Tp::ConnectionPresenceTypeBusy; + spBusy.maySetOnSelf = true; + spBusy.canHaveMessage = true; + + Tp::SimpleStatusSpec spAway; + spAway.type = Tp::ConnectionPresenceTypeAway; + spAway.maySetOnSelf = true; + spAway.canHaveMessage = true; + + Tp::SimpleStatusSpec spOffline; + spOffline.type = Tp::ConnectionPresenceTypeOffline; + spOffline.maySetOnSelf = true; + spOffline.canHaveMessage = true; + + + Tp::SimpleStatusSpec spHidden; + spHidden.type = Tp::ConnectionPresenceTypeHidden; + spHidden.maySetOnSelf = true; + spHidden.canHaveMessage = true; + + Tp::SimpleStatusSpec spUnknown; + spUnknown.type = Tp::ConnectionPresenceTypeUnknown; + spUnknown.maySetOnSelf = false; + spUnknown.canHaveMessage = true; + + Tp::SimpleStatusSpecMap specs; + specs.insert(QLatin1String("available"), spAvailable); + specs.insert(QLatin1String("dnd"), spBusy); + specs.insert(QLatin1String("away"), spAway); + specs.insert(QLatin1String("offline"), spOffline); + specs.insert(QLatin1String("hidden"), spHidden); + specs.insert(QLatin1String("unknown"), spUnknown); + return specs; } uint @@ -384,42 +393,42 @@ tr::Connection::setPresence(const QString &status, const QString &message, Tp::D { // TODO: find out why message is always getting here empty - StackLog(<<"setPresence()"); + StackLog(<<"setPresence()"); - mSelfPresence.type = statusMap[status].type; - mSelfPresence.status = status; - if(statusMap[status].canHaveMessage){ - mSelfPresence.statusMessage = message; - } + mSelfPresence.type = statusMap[status].type; + mSelfPresence.status = status; + if (statusMap[status].canHaveMessage) { + mSelfPresence.statusMessage = message; + } - qDebug() << "status = " << mSelfPresence.status << " type = " << mSelfPresence.type << " message = " << message; + qDebug() << "status = " << mSelfPresence.status << " type = " << mSelfPresence.type << " message = " << message; - return selfHandle(); + return selfHandle(); } Tp::SimpleContactPresences tr::Connection::getPresences(const Tp::UIntList &handles) { - StackLog(<<"getPresences()"); - - Tp::SimpleContactPresences presences; - foreach (uint handle, handles) { - presences[handle] = getPresence(handle); - } + StackLog(<<"getPresences()"); + + Tp::SimpleContactPresences presences; + Q_FOREACH(uint handle, handles) { + presences[handle] = getPresence(handle); + } - return presences; + return presences; } Tp::SimplePresence tr::Connection::getPresence(uint handle) { - StackLog(<<"getPresence()"); - if (!mPresences.contains(handle)) { - return Tp::SimplePresence(); - } - qDebug() << "presence = " << mPresences.value(handle).status; + StackLog(<<"getPresence()"); + if (!mPresences.contains(handle)) { + return Tp::SimplePresence(); + } + qDebug() << "presence = " << mPresences.value(handle).status; - return mPresences.value(handle); + return mPresences.value(handle); } void @@ -486,7 +495,7 @@ tr::Connection::inspectHandles(uint handleType, const Tp::UIntList &handles, Tp: QStringList result; - foreach (uint handle, handles) { + Q_FOREACH(uint handle, handles) { if(!mHandles.contains(handle)) { return QStringList(); } @@ -509,9 +518,7 @@ tr::Connection::requestHandles(uint handleType, const QStringList &identifiers, } Q_FOREACH(const QString &identifier, identifiers) { - ensureHandle(identifier); - if(mIdentifiers.find(identifier) != mIdentifiers.end()) - result.append(mIdentifiers[identifier]); + result.append(ensureHandle(identifier)); } return result; @@ -605,11 +612,11 @@ tr::Connection::getContactAttributes(const Tp::UIntList &handles, const QStringL qDebug() << "handle = " << handle << " inspectedHandle = " << inspectedHandle; if (ifaces.contains(TP_QT_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE)) { - attributes[TP_QT_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE+"/presence"] = QVariant::fromValue(getPresence(handle)); + attributes[TP_QT_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE+"/presence"] = QVariant::fromValue(getPresence(handle)); } if (ifaces.contains(TP_QT_IFACE_CONNECTION_INTERFACE_ALIASING)) { - attributes[TP_QT_IFACE_CONNECTION_INTERFACE_ALIASING + QLatin1String("/alias")] = QVariant::fromValue(mAliases[handle]); + attributes[TP_QT_IFACE_CONNECTION_INTERFACE_ALIASING + QLatin1String("/alias")] = QVariant::fromValue(mAliases[handle]); } attributesMap[handle] = attributes; diff --git a/apps/telepathy/Connection.hxx b/apps/telepathy/Connection.hxx index 38c7ec3ce9..a493bfae9a 100644 --- a/apps/telepathy/Connection.hxx +++ b/apps/telepathy/Connection.hxx @@ -19,7 +19,7 @@ #define CONNECTION_HXX #if defined(HAVE_CONFIG_H) -#include "config.h" + #include "config.h" #endif @@ -39,73 +39,71 @@ namespace tr { - class MyConversationManager; - class MyUserAgent; +class MyConversationManager; +class MyUserAgent; - class Connection : public Tp::BaseConnection - { - Q_OBJECT - public: - Connection(const QDBusConnection &dbusConnection, - const QString &cmName, const QString &protocolName, - const QVariantMap ¶meters); +class Connection : public Tp::BaseConnection +{ + Q_OBJECT +public: + Connection(const QDBusConnection &dbusConnection, + const QString &cmName, const QString &protocolName, + const QVariantMap ¶meters); - MyConversationManager& getConversationManager() { return *myConversationManager.get(); }; + MyConversationManager& getConversationManager() { return *myConversationManager.get(); }; - Tp::ContactAttributesMap getContactListAttributes(const QStringList &interfaces, bool hold, Tp::DBusError *error); + Tp::ContactAttributesMap getContactListAttributes(const QStringList &interfaces, bool hold, Tp::DBusError *error); - void requestSubscription(const Tp::UIntList &handles, const QString &message, Tp::DBusError *error); - void removeContacts(const Tp::UIntList &handles, Tp::DBusError *error); - Tp::AliasMap getAliases(const Tp::UIntList& handles, Tp::DBusError *error); - void setAliases(const Tp::AliasMap &aliases, Tp::DBusError *error); - void getContactsFromFile(Tp::DBusError *error); - void setContactsInFile(); - void deleteContacts(const QStringList& contacts); - Tp::SimpleStatusSpecMap getSimpleStatusSpecMap(); - Tp::SimpleContactPresences getPresences(const Tp::UIntList &handles); - Tp::SimplePresence getPresence(uint handle); + void requestSubscription(const Tp::UIntList &handles, const QString &message, Tp::DBusError *error); + void removeContacts(const Tp::UIntList &handles, Tp::DBusError *error); + Tp::AliasMap getAliases(const Tp::UIntList& handles, Tp::DBusError *error); + void setAliases(const Tp::AliasMap &aliases, Tp::DBusError *error); + void getContactsFromFile(Tp::DBusError *error); + void setContactsInFile(); + void deleteContacts(const QStringList& contacts); + Tp::SimpleStatusSpecMap getSimpleStatusSpecMap(); + Tp::SimpleContactPresences getPresences(const Tp::UIntList &handles); + Tp::SimplePresence getPresence(uint handle); - private: - uint setPresence(const QString &status, const QString &message, Tp::DBusError *error); - QStringList inspectHandles(uint handleType, const Tp::UIntList &handles, Tp::DBusError *error); - Tp::UIntList requestHandles(uint handleType, const QStringList &identifiers, Tp::DBusError *error); - Tp::BaseChannelPtr createChannel(const QVariantMap &request, Tp::DBusError *error); - Tp::ContactAttributesMap getContactAttributes(const Tp::UIntList &handles, const QStringList &ifaces, Tp::DBusError *error); - uint ensureHandle(const QString& identifier); +private: + uint setPresence(const QString &status, const QString &message, Tp::DBusError *error); + QStringList inspectHandles(uint handleType, const Tp::UIntList &handles, Tp::DBusError *error); + Tp::UIntList requestHandles(uint handleType, const QStringList &identifiers, Tp::DBusError *error); + Tp::BaseChannelPtr createChannel(const QVariantMap &request, Tp::DBusError *error); + Tp::ContactAttributesMap getContactAttributes(const Tp::UIntList &handles, const QStringList &ifaces, Tp::DBusError *error); + uint ensureHandle(const QString& identifier); - private slots: - void doConnect(Tp::DBusError *error); - void onConnected(); - void doDisconnect(); - void setStatusSlot(uint newStatus, uint reason); - void onIncomingCall(const QString & caller, uint callHandle); - - - private: - resip::SharedPtr mUAProfile; - resip::SharedPtr mConversationProfile; - tr::MyUserAgent* ua; - std::auto_ptr myConversationManager; - - Tp::BaseConnectionContactsInterfacePtr mContactsInterface; - Tp::BaseConnectionAliasingInterfacePtr mAliasingInterface; - Tp::BaseConnectionSimplePresenceInterfacePtr mSimplePresenceInterface; - Tp::BaseConnectionContactListInterfacePtr mContactListInterface; - Tp::BaseConnectionRequestsInterfacePtr mRequestsInterface; - - long nextHandleId; - QMap mHandles; - QMap mIdentifiers; - Tp::AliasMap mAliases; - - Tp::SimpleStatusSpecMap statusMap; - Tp::SimplePresence mSelfPresence; - Tp::SimpleContactPresences mPresences; - - QString fileWithContacts; - }; +private slots: + void doConnect(Tp::DBusError *error); + void onConnected(); + void doDisconnect(); + void setStatusSlot(uint newStatus, uint reason); + void onIncomingCall(const QString & caller, uint callHandle); + + +private: + resip::SharedPtr mUAProfile; + resip::SharedPtr mConversationProfile; + tr::MyUserAgent* ua; + std::auto_ptr myConversationManager; + + Tp::BaseConnectionContactsInterfacePtr mContactsInterface; + Tp::BaseConnectionAliasingInterfacePtr mAliasingInterface; + Tp::BaseConnectionSimplePresenceInterfacePtr mSimplePresenceInterface; + Tp::BaseConnectionContactListInterfacePtr mContactListInterface; + Tp::BaseConnectionRequestsInterfacePtr mRequestsInterface; + + long nextHandleId; + QMap mHandles; + QMap mIdentifiers; + Tp::AliasMap mAliases; + + Tp::SimpleStatusSpecMap statusMap; + Tp::SimplePresence mSelfPresence; + Tp::SimpleContactPresences mPresences; +}; } From fe386b3a81c7d15c6a1172fa88a651eb5e663fc0 Mon Sep 17 00:00:00 2001 From: mateus Date: Sat, 11 Jun 2016 11:51:39 -0300 Subject: [PATCH 3/5] removed extra spaces --- apps/telepathy/Connection.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/telepathy/Connection.hxx b/apps/telepathy/Connection.hxx index a493bfae9a..0ed84719ee 100644 --- a/apps/telepathy/Connection.hxx +++ b/apps/telepathy/Connection.hxx @@ -73,8 +73,8 @@ private: Tp::BaseChannelPtr createChannel(const QVariantMap &request, Tp::DBusError *error); Tp::ContactAttributesMap getContactAttributes(const Tp::UIntList &handles, const QStringList &ifaces, Tp::DBusError *error); uint ensureHandle(const QString& identifier); - - + + private slots: void doConnect(Tp::DBusError *error); void onConnected(); From be07b9000e25e69ae381f34d7cb3ea0103fb250a Mon Sep 17 00:00:00 2001 From: mateus Date: Sat, 11 Jun 2016 11:55:35 -0300 Subject: [PATCH 4/5] removed another extra spaces --- apps/telepathy/Connection.cxx | 2 +- apps/telepathy/Connection.hxx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/telepathy/Connection.cxx b/apps/telepathy/Connection.cxx index 0cdb40c885..efcdc53d77 100644 --- a/apps/telepathy/Connection.cxx +++ b/apps/telepathy/Connection.cxx @@ -144,7 +144,7 @@ tr::Connection::Connection(const QDBusConnection &dbusConnection, const QString const QString& account = parameters.value(QLatin1String("account")).toString(); uint _self = ensureHandle(account); setSelfContact(_self, account); - + setConnectCallback(Tp::memFun(this, &Connection::doConnect)); setInspectHandlesCallback(Tp::memFun(this, &Connection::inspectHandles)); setCreateChannelCallback(Tp::memFun(this, &Connection::createChannel)); diff --git a/apps/telepathy/Connection.hxx b/apps/telepathy/Connection.hxx index 0ed84719ee..0f4eea9057 100644 --- a/apps/telepathy/Connection.hxx +++ b/apps/telepathy/Connection.hxx @@ -41,7 +41,7 @@ namespace tr { class MyConversationManager; class MyUserAgent; - + class Connection : public Tp::BaseConnection { Q_OBJECT From c66e87b53828c64cb1cc5ef3e7557e2955e0ce5b Mon Sep 17 00:00:00 2001 From: mateus Date: Sat, 11 Jun 2016 18:06:21 -0300 Subject: [PATCH 5/5] problem pull request #43 --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 49b00f8320..4d6318a6a0 100644 --- a/configure.ac +++ b/configure.ac @@ -14,7 +14,7 @@ AC_SUBST(LIBTOOL_VERSION_RELEASE) AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_MACRO_DIR([m4]) -AM_INIT_AUTOMAKE([foreign subdir-objects]) +AM_INIT_AUTOMAKE m4_pattern_forbid([^PKG_]) AC_CANONICAL_HOST