From kde-commits Sun Jun 30 20:44:44 2013 From: Sven Brauch Date: Sun, 30 Jun 2013 20:44:44 +0000 To: kde-commits Subject: [kte-collaborative/ktp-integration] kte-plugin: (non-working, non-compiling) API draft for KTp integ Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=137262509531217 Git commit c441d2d52a3ade94b96844d6ba0788a1a08af3a7 by Sven Brauch. Committed on 30/06/2013 at 20:44. Pushed by brauch into branch 'ktp-integration'. (non-working, non-compiling) API draft for KTp integration M +1 -0 kte-plugin/CMakeLists.txt A +24 -0 kte-plugin/ktpintegration/CMakeLists.txt A +168 -0 kte-plugin/ktpintegration/inftube.cpp [License: GPL (v2+= )] A +141 -0 kte-plugin/ktpintegration/inftube.h [License: GPL (v2+)] http://commits.kde.org/kte-collaborative/c441d2d52a3ade94b96844d6ba0788a1a0= 8af3a7 diff --git a/kte-plugin/CMakeLists.txt b/kte-plugin/CMakeLists.txt index d080612..33e9dbd 100644 --- a/kte-plugin/CMakeLists.txt +++ b/kte-plugin/CMakeLists.txt @@ -8,6 +8,7 @@ cmake_minimum_required(VERSION 2.8) = add_subdirectory(settings) +add_subdirectory(ktpintegration) = find_package(KDE4 4.5.0 REQUIRED) include(KDE4Defaults) diff --git a/kte-plugin/ktpintegration/CMakeLists.txt b/kte-plugin/ktpinteg= ration/CMakeLists.txt new file mode 100644 index 0000000..6324952 --- /dev/null +++ b/kte-plugin/ktpintegration/CMakeLists.txt @@ -0,0 +1,24 @@ +set(tubestest_SRCS + tubestest.cpp +) + +set(IS_KTP_INTERNAL_MODULE TRUE) # aw yeah +find_package(TelepathyQt4 0.8.9 REQUIRED) +find_package(KTp REQUIRED) +include_directories( + ${TELEPATHY_QT4_INCLUDE_DIR} + ${KTP_INCLUDE_DIR} +) + +add_library(inftube SHARED ${tubestest_SRCS}) + +target_link_libraries(inftube + ${KDE4_KDEUI_LIBS} + ${TELEPATHY_QT4_LIBRARIES} + ${KTP_LIBRARIES} + ${KDE4_KIO_LIBS} + ${KTP_MODELS_LIBRARIES} + ${KTP_WIDGETS_LIBRARIES} +) + +install(TARGETS inftube lib) diff --git a/kte-plugin/ktpintegration/inftube.cpp b/kte-plugin/ktpintegrat= ion/inftube.cpp new file mode 100644 index 0000000..a01c87b --- /dev/null +++ b/kte-plugin/ktpintegration/inftube.cpp @@ -0,0 +1,168 @@ +/*************************************************************************= ** + * Copyright (C) %{CURRENT_YEAR} by %{AUTHOR} <%{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 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, write to the = * + * Free Software Foundation, Inc., = * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . = * + *************************************************************************= **/ + +#include "inftube.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void InfTubeBase::initialize() +{ + Tp::registerTypes(); + KTp::Debug::installCallback(true); + + Tp::AccountFactoryPtr accountFactory =3D Tp::AccountFactory::create(QD= BusConnection::sessionBus(), + Tp:= :Features() << Tp::Account::FeatureCore + << = Tp::Account::FeatureAvatar + << = Tp::Account::FeatureProtocolInfo + << = Tp::Account::FeatureProfile); + + Tp::ConnectionFactoryPtr connectionFactory =3D Tp::ConnectionFactory::= create(QDBusConnection::sessionBus(), + = Tp::Features() << Tp::Connection::FeatureCore + = << Tp::Connection::FeatureRosterGroups + = << Tp::Connection::FeatureRoster + = << Tp::Connection::FeatureSelfContact); + + Tp::ContactFactoryPtr contactFactory =3D KTp::ContactFactory::create(T= p::Features() << Tp::Contact::FeatureAlias + << T= p::Contact::FeatureAvatarData + << T= p::Contact::FeatureSimplePresence + << T= p::Contact::FeatureCapabilities); + + Tp::ChannelFactoryPtr channelFactory =3D Tp::ChannelFactory::create(QD= BusConnection::sessionBus()); + + m_accountManager =3D Tp::AccountManager::create(QDBusConnection::sessi= onBus(), + accountFactory, + connectionFactory, + channelFactory, + contactFactory); + +} + +unsigned int InfTubeBase::localPort() const +{ + return m_port; +} + +KUrl InfTubeBase::localUrl() const +{ + KUrl url; + url.setProtocol(QLatin1String("inf")); + url.setHost(QLatin1String("localhost")); + url.setPort(m_port); + return url; +} + +InfTubeBase::ConnectionStatus InfTubeBase::status() const +{ + return m_status; +} + +InfTubeServer::InfTubeServer(QObject* parent) +{ + initialize(); + m_tubeServer =3D Tp::StreamTubeServer::create(m_accountManager, QStrin= gList() << "infinity-collaborative"); +} + +bool InfTubeServer::offer(const Tp::Account& account, const Tp::ContactPtr= & contact, const KUrl& document) +{ + offer(ContactList() << contact, DocumentList() << document); +} + +bool InfTubeServer::offer(const Tp::Account& account, const ContactList& c= ontacts, const DocumentList& initialDocuments) +{ + qDebug() << "starting infinoted"; + startInfinoted(); + m_tubeServer->exportTcpSocket(QHostAddress(QHostAddress::LocalHost), m= _port); + foreach ( const KUrl& document, initialDocuments ) { + KUrl x =3D localUrl(); + x.setFileName(document.fileName()); + KIO::TransferJob* job =3D KIO::put(x, -1); + connect(job, SIGNAL(finished(KJob*)), this, SLOT(testFileCreated(K= Job*))); + } + Tp::PendingChannelRequest* channelRequest =3D 0; + QVariantMap request; + request.insert(TP_QT_IFACE_CHANNEL + QLatin1String(".ChannelType"), + TP_QT_IFACE_CHANNEL_TYPE_STREAM_TUBE); + request.insert(TP_QT_IFACE_CHANNEL + QLatin1String(".TargetHandleType"= ), + (uint) Tp::HandleTypeContact); + request.insert(TP_QT_IFACE_CHANNEL_TYPE_STREAM_TUBE + QLatin1String(".= Service"), + QLatin1String("tubetest")); + // TODO !!! + request.insert(TP_QT_IFACE_CHANNEL + QLatin1String(".TargetHandle"), + contacts.first()->handle().at(0)); + account.ensureChannel(request, + QDateTime::currentDateTime(), + "org.freedesktop.Telepathy.Client.KTp.infinity-c= ollaborative"); + + connect(channelRequest, SIGNAL(finished(Tp::PendingOperation*)), + this, SLOT(onCreateTubeFinished(Tp::PendingOperation*))); +} + +void InfTubeServer::startInfinoted() +{ + m_serverProcess =3D new QProcess; + m_serverProcess->setEnvironment(QStringList() << "LIBINFINITY_DEBUG_PR= INT_TRAFFIC=3D1"); + m_serverProcess->setStandardOutputFile(serverDirectory() + "/infinoted= .log"); + m_serverProcess->setStandardErrorFile(serverDirectory() + "/infinoted.= errors"); + m_serverProcess->start("/usr/bin/env", QStringList() << "infinoted-0.5= " << "--security-policy=3Dno-tls" + << "-r" << serverDirectory() <<= "-p" << QString::number(m_port)); + m_serverProcess->waitForStarted(500); + while ( true ) { + sleep(1); + QTcpSocket s; + s.connectToHost("localhost", m_port); + if ( s.waitForConnected(100) ) { + break; + } + } + qDebug() << "successfully started infinioted on port" << m_port << "( = root dir" << serverDirectory() << ")"; +} + +const QString InfTubeServer::serverDirectory() const +{ + return QLatin1String("/tmp"); +} + +void InfTubeClient::listen() +{ + m_tubeClient =3D Tp::StreamTubeClient::create(m_accountManager, QStrin= gList() << "tubetest", QStringList(), QString(), true, true); + m_tubeClient->setToAcceptAsTcp(); + connect(m_tubeClient.data(), SIGNAL(tubeAcceptedAsTcp(QHostAddress,qui= nt16,QHostAddress,quint16,Tp::AccountPtr,Tp::IncomingStreamTubeChannelPtr)), + this, SLOT(tubeAcceptedAsTcp(QHostAddress,quint16,QHostAddress= ,quint16,Tp::AccountPtr,Tp::IncomingStreamTubeChannelPtr))); +} + +void InfTubeClient::tubeAcceptedAsTcp(QHostAddress , quint16 , QHostAddres= s , quint16 , Tp::AccountPtr , Tp::IncomingStreamTubeChannelPtr ) +{ + kDebug() << "Tube accepted as Tcp"; +} + +#include "inftube.moc" diff --git a/kte-plugin/ktpintegration/inftube.h b/kte-plugin/ktpintegratio= n/inftube.h new file mode 100644 index 0000000..e3b2b21 --- /dev/null +++ b/kte-plugin/ktpintegration/inftube.h @@ -0,0 +1,141 @@ +/*************************************************************************= ** + * Copyright (C) %{CURRENT_YEAR} by %{AUTHOR} <%{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 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, write to the = * + * Free Software Foundation, Inc., = * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . = * + *************************************************************************= **/ + +#ifndef TUBESTEST_H +#define TUBESTEST_H + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace Tp { + class StreamTubeClient; +} + +typedef QList ContactList; +typedef QList DocumentList; + +/** + * This class defines the API for routing infinity traffic through a TP tu= be + * which is common to both the receiving an the offering side. + */ +class InfTubeBase : public QObject { +Q_OBJECT +public: + InfTubeBase(QObject* parent =3D 0) =3D 0; + virtual ~InfTubeBase() =3D 0; + + enum ConnectionStatus { + StatusConnected, + StatusConnecting, + StatusDisconnected + }; + + /** + * @brief Get the local port this tube ends in. + * @return unsigned int the port + */ + unsigned int localPort() const; + + /** + * @brief Get the URL which points to the infinote server. + * @return KUrl the URL, something like inf://localhost:foo + */ + KUrl localUrl() const; + + /** + * @brief Get the status of the connection. + */ + ConnectionStatus status() const; + +signals: + /** + * @brief Emitted when the connection has been established + */ + void connected(InfTubeBase* self); + + +protected: + Tp::AccountManagerPtr m_accountManager; + ConnectionStatus m_status; + unsigned int m_port; + + /** + * @brief Performs some initialization tasks. Call before everything e= lse. + */ + void initialize(); +}; + +/** + * @brief This class implements InfTubeBase and is used on the receiving e= nd. + */ +class InfTubeClient : public InfTubeBase { +public: + InfTubeClient(QObject* parent =3D 0); + virtual ~InfTubeClient(); + + /** + * @brief Begin waiting for incoming connections. + */ + void listen(); + +private: + Tp::StreamTubeClientPtr m_tubeClient; +public slots: + void tubeAcceptedAsTcp(QHostAddress,quint16,QHostAddress,quint16,Tp::A= ccountPtr,Tp::IncomingStreamTubeChannelPtr); +}; + +/** + * @brief This class implements InfTubeBase and can be used to offer some = documents to a contact. + */ +class InfTubeServer : public InfTubeBase { +public: + InfTubeServer(QObject* parent =3D 0); + virtual ~InfTubeServer(); + + /** + * @brief Initiate editing a list of documents with the given contacts. + * + * @param contacts The list of contacts to edit documents with + * @param initialDocuments The documents all contacts should have open= ed initially + * @return bool true if the request was successful + */ + bool offer(const Tp::Account& account, const ContactList& contacts, co= nst DocumentList& initialDocuments); + + /** + * @brief Convenience overload, for offering a single document to a si= ngle contact. + */ + bool offer(const Tp::Account& account, const Tp::ContactPtr& contact, = const KUrl& document); + +private: + Tp::StreamTubeServerPtr m_tubeServer; + QProcess* m_serverProcess; + + void startInfinoted(); + const QString serverDirectory() const; +}; + +#endif