diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 232bf33..3fab685 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -15,6 +15,8 @@ set(plaYUver_App_SRCS PlaYUVerAppModuleIf.cpp GridManager.cpp Settings.cpp + PlaYUVerAppAdaptor.cpp + RunningInstanceInfo.cpp # Dialogs AboutDialog.cpp ConfigureFormatDialog.cpp diff --git a/app/PlaYUVerAppAdaptor.cpp b/app/PlaYUVerAppAdaptor.cpp new file mode 100644 index 0000000..065c1b1 --- /dev/null +++ b/app/PlaYUVerAppAdaptor.cpp @@ -0,0 +1,70 @@ +/* This file is a part of plaYUVer project + * Copyright (C) 2014 by Luis Lucas (luisfrlucas@gmail.com) + * Joao Carreira (jfmcarreira@gmail.com) + * + * 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. + */ + +/** + * \file PlaYUVerAppAdaptor.h + * \brief Define function to manage the interface in DBus + */ + +#include +#include "PlaYUVerAppAdaptor.h" +#include "plaYUVerApp.h" + +namespace plaYUVer +{ + +PlaYUVerAppAdaptor::PlaYUVerAppAdaptor( plaYUVerApp *app ) : + QDBusAbstractAdaptor( app ), + m_app( app ) +{ + // application dbus interface + QDBusConnection::sessionBus().registerObject( QStringLiteral( PLAYUVER_DBUS_PATH ), this, QDBusConnection::ExportAllSlots ); +} + +PlaYUVerAppAdaptor::~PlaYUVerAppAdaptor() +{ + QDBusConnection::sessionBus().unregisterObject( QStringLiteral( PLAYUVER_DBUS_PATH ) ); +} + +void PlaYUVerAppAdaptor::activate() +{ + if( !m_app ) + { + return; + } + + m_app->show(); + //m_app->activateWindow(); + //m_app->raise(); + +} + +QString PlaYUVerAppAdaptor::activeSession() +{ + //return m_app->sessionManager()->activeSession()->name(); + return QString( "PlaYUVer" ); +} + +bool PlaYUVerAppAdaptor::loadFile( QString filename ) +{ + m_app->loadFile( filename ); + return true; +} + +} // NAMESPACE diff --git a/app/PlaYUVerAppAdaptor.h b/app/PlaYUVerAppAdaptor.h new file mode 100644 index 0000000..55a0b23 --- /dev/null +++ b/app/PlaYUVerAppAdaptor.h @@ -0,0 +1,83 @@ +/* This file is a part of plaYUVer project + * Copyright (C) 2014 by Luis Lucas (luisfrlucas@gmail.com) + * Joao Carreira (jfmcarreira@gmail.com) + * + * 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. + */ + +/** + * \file PlaYUVerAppAdaptor.h + * \brief Define function to manage the interface in DBus + */ + +#ifndef __PlaYUVerAppAdaptor_H__ +#define __PlaYUVerAppAdaptor_H__ + +#include +#include + +namespace plaYUVer +{ + +#define PLAYUVER_DBUS_SESSION_NAME "org.pixlra.PlaYUVer.application" +#define PLAYUVER_DBUS_PATH "/MainApplication" + +class plaYUVerApp; + +class PlaYUVerAppAdaptor: public QDBusAbstractAdaptor +{ +Q_OBJECT +Q_CLASSINFO("D-Bus Interface", PLAYUVER_DBUS_SESSION_NAME) +Q_PROPERTY(QString activeSession READ activeSession) +public: + PlaYUVerAppAdaptor( plaYUVerApp *app ); + ~PlaYUVerAppAdaptor(); + /** + * emit the exiting signal + */ + //void emitExiting(); + //void emitDocumentClosed(const QString &token); +public Q_SLOTS: + /** + * open a file with given url and encoding + * will get view created + * @param url url of the file + * @param encoding encoding name + * @return success + */ + bool loadFile( QString filename ); + + /** + * activate this kate instance + */ + void activate(); + +Q_SIGNALS: + /** + * Notify the world that this kate instance is exiting. + * All apps should stop using the dbus interface of this instance after this signal got emitted. + */ + //void exiting(); + //void documentClosed(const QString &token); +public: + QString activeSession(); +private: + plaYUVerApp *m_app; +}; + +} // NAMESPACE + +#endif // __PlaYUVerAppAdaptor_H__ + diff --git a/app/RunningInstanceInfo.cpp b/app/RunningInstanceInfo.cpp new file mode 100644 index 0000000..c4c4098 --- /dev/null +++ b/app/RunningInstanceInfo.cpp @@ -0,0 +1,83 @@ +/* This file is a part of plaYUVer project + * Copyright (C) 2014 by Luis Lucas (luisfrlucas@gmail.com) + * Joao Carreira (jfmcarreira@gmail.com) + * + * 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. + */ + +/** + * \file RunningInstanceInfo.cpp + * \brief Control running instances + */ + +#include "RunningInstanceInfo.h" +#include +#include +#include +#include +#include + +namespace plaYUVer +{ + +int RunningInstanceInfo::dummy_session = 0; + +bool fillinRunningAppInstances(RunningInstanceInfoMap *map) +{ + QDBusConnectionInterface *i = QDBusConnection::sessionBus().interface(); + + // look up all running kate instances and there sessions + QDBusReply servicesReply = i->registeredServiceNames(); + QStringList services; + if (servicesReply.isValid()) { + services = servicesReply.value(); + } + + QString serviceName; + + QString my_pid = QString::number(QCoreApplication::applicationPid()); + + foreach(const QString & s, services) { + if (s.startsWith(QStringLiteral("org.pixlra.PlaYUVer"))) { + if (s.contains(my_pid)) { + continue; + } + qDebug() << s ; + RunningInstanceInfo *rii = new RunningInstanceInfo(s); + if (rii->valid) { + if (map->contains(rii->sessionName)) { + return false; //ERROR no two instances may have the same session name + } + map->insert(rii->sessionName, rii); + //std::cerr<sessionName.toUtf8().data()<constBegin(); it != map->constEnd(); ++it) { + delete it.value(); + } + map->clear(); +} + +} // NAMESPACE + + diff --git a/app/RunningInstanceInfo.h b/app/RunningInstanceInfo.h new file mode 100644 index 0000000..26ae16d --- /dev/null +++ b/app/RunningInstanceInfo.h @@ -0,0 +1,100 @@ +/* This file is a part of plaYUVer project + * Copyright (C) 2014 by Luis Lucas (luisfrlucas@gmail.com) + * Joao Carreira (jfmcarreira@gmail.com) + * + * 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. + */ + +/** + * \file RunningInstanceInfo.h + * \brief Control running instances + */ + +#ifndef _PLAYUVER_RUNNING_INSTANCE_INFO_ +#define _PLAYUVER_RUNNING_INSTANCE_INFO_ + +#include +#include +#include +#include +#include +#include "lib/PlaYUVerDefs.h" +#include "PlaYUVerAppAdaptor.h" + +namespace plaYUVer +{ + +class RunningInstanceInfo: public QObject +{ +Q_OBJECT + +public: + RunningInstanceInfo( const QString &serviceName_ ) : + QObject(), + valid( false ), + serviceName( serviceName_ ), + dbus_if( new QDBusInterface( serviceName_, QStringLiteral( PLAYUVER_DBUS_PATH ), QString(), //I don't know why it does not work if I specify org.pixlra.PlaYUVer.application here + QDBusConnection::sessionBus(), this ) ) + { + if( !dbus_if->isValid() ) + { + std::cerr << qPrintable( QDBusConnection::sessionBus().lastError().message() ) + << std::endl; + } +// QVariant a_s = dbus_if->property( "activeSession" ); +// std::cerr< RunningInstanceInfoMap; + +Q_DECL_EXPORT bool fillinRunningAppInstances( RunningInstanceInfoMap *map ); +Q_DECL_EXPORT void cleanupRunningAppInstanceMap( RunningInstanceInfoMap *map ); + +} // NAMESPACE + +#endif // _PLAYUVER_RUNNING_INSTANCE_INFO_ + diff --git a/app/main.cpp b/app/main.cpp index b2bc25c..fbee25c 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -23,8 +23,14 @@ */ #include "config.h" +#include "lib/PlaYUVerDefs.h" #include +#include #include +#include +#include +#include "PlaYUVerAppAdaptor.h" +#include "RunningInstanceInfo.h" #include "plaYUVerApp.h" #include "SubWindowHandle.h" #ifdef USE_FERVOR @@ -39,10 +45,10 @@ using namespace plaYUVer; int main( int argc, char *argv[] ) { - qRegisterMetaType("PlaYUVerStreamInfoVector"); - qRegisterMetaTypeStreamOperators("PlaYUVerStreamInfoVector"); + qRegisterMetaType( "PlaYUVerStreamInfoVector" ); + qRegisterMetaTypeStreamOperators( "PlaYUVerStreamInfoVector" ); - QApplication a( argc, argv ); + QApplication application( argc, argv ); QApplication::setApplicationName( "PlaYUVer" ); QApplication::setApplicationVersion( PLAYUVER_VERSION_STRING ); QApplication::setOrganizationName( "pixlra" ); @@ -52,17 +58,123 @@ int main( int argc, char *argv[] ) * use dbus, if available * allows for resuse of running Kate instances */ - if( QDBusConnectionInterface * const sessionBusInterface = QDBusConnection::sessionBus().interface() ) + QDBusConnectionInterface * const sessionBusInterface = QDBusConnection::sessionBus().interface(); + if( sessionBusInterface ) { + RunningInstanceInfoMap mapSessionRii; + if( !fillinRunningAppInstances( &mapSessionRii ) ) + { + return 1; + } + QStringList playuverServices; + for( RunningInstanceInfoMap::const_iterator it = mapSessionRii.constBegin(); it != mapSessionRii.constEnd(); ++it ) + { + playuverServices << ( *it )->serviceName; + } + QString serviceName; + //const QStringList urls = parser.positionalArguments(); + + Bool force_new = false; + + //cleanup map + cleanupRunningAppInstanceMap( &mapSessionRii ); + + //if no new instance is forced and no already opened session is requested, + //check if a pid is given, which should be reused. + // two possibilities: pid given or not... +// if( ( !force_new ) && serviceName.isEmpty() ) +// { +// if( /*( parser.isSet( usePidOption ) )*/|| ( !qgetenv( "PLAYUVER_PID" ).isEmpty() ) ) +// { +// QString usePid = ( parser.isSet( usePidOption ) ) ? parser.value( usePidOption ) : QString::fromLocal8Bit( qgetenv( "KATE_PID" ) ); +// +// serviceName = QStringLiteral("org.kde.kate-")+ usePid; +// if( !playuverServices.contains( serviceName ) ) +// { +// serviceName.clear(); +// } +// } +// } + + if( ( !force_new ) && ( serviceName.isEmpty() ) ) + { + if( playuverServices.count() > 0 ) + { + serviceName = playuverServices[0]; + } + } + + //check again if service is still running + bool foundRunningService = false; + if( !serviceName.isEmpty() ) + { + QDBusReply there = sessionBusInterface->isServiceRegistered( serviceName ); + foundRunningService = there.isValid() && there.value(); + } + + if( foundRunningService ) + { + // open given session + QDBusMessage m = QDBusMessage::createMethodCall( serviceName, QStringLiteral( PLAYUVER_DBUS_PATH ), QStringLiteral( PLAYUVER_DBUS_SESSION_NAME ), + QStringLiteral( "activate" ) ); + + QDBusConnection::sessionBus().call( m ); + + // only block, if files to open there.... + bool needToBlock = false; + + QStringList tokens; + + QStringList filenameList; + filenameList.append( QString( argv[1] ) ); + + // open given files... + foreach(const QString & file, filenameList){ + QDBusMessage m = QDBusMessage::createMethodCall(serviceName, + QStringLiteral(PLAYUVER_DBUS_PATH), QStringLiteral(PLAYUVER_DBUS_SESSION_NAME), QStringLiteral("loadFile")); + + QList dbusargs; + + dbusargs.append( file ); + + m.setArguments(dbusargs); + + QDBusMessage res = QDBusConnection::sessionBus().call(m); + if (res.type() == QDBusMessage::ReplyMessage) + { + if (res.arguments().count() == 1) + { + QVariant v = res.arguments()[0]; + if (v.isValid()) + { + QString s = v.toString(); + if ((!s.isEmpty()) && (s != QStringLiteral("ERROR"))) + { + tokens << s; + } + } + } + } + } + + // this will wait until exiting is emitted by the used instance, if wanted... + return needToBlock ? application.exec() : 0; + } } - plaYUVerApp w; - w.show(); - w.parseArgs( argc, argv ); + /** + * if we arrive here, we need to start a new playuver instance! + */ + plaYUVerApp mainwindow; + mainwindow.show(); + mainwindow.parseArgs( argc, argv ); #ifdef USE_FERVOR - FvUpdater::sharedUpdater()->SetFeedURL("http://192.168.96.201/share/jcarreira.it.pub/plaYUVer/PlaYUVerUpdate.xml"); + FvUpdater::sharedUpdater()->SetFeedURL("http://192.168.96.201/share/pixLRA/plaYUVer/PlaYUVerUpdate.xml"); FvUpdater::sharedUpdater()->SetDependencies(DEPENDENCIES_STRING); #endif - return a.exec(); + + QDBusConnection::sessionBus().registerService( PLAYUVER_DBUS_SESSION_NAME ); + + return application.exec(); } diff --git a/app/plaYUVerApp.cpp b/app/plaYUVerApp.cpp index 8d9dc01..4c3e858 100644 --- a/app/plaYUVerApp.cpp +++ b/app/plaYUVerApp.cpp @@ -22,19 +22,14 @@ * \brief Main definition of the plaYUVerApp app */ -#include "plaYUVerApp.h" -#include "lib/PlaYUVerStream.h" -#include "DialogSubWindowSelector.h" + +#include #ifdef USE_FERVOR #include "fvupdater.h" #endif -#if( QT_VERSION_PLAYUVER == 5 ) -#include -#elif( QT_VERSION_PLAYUVER == 4 ) -#include -#endif -#include +#include "plaYUVerApp.h" +#include "DialogSubWindowSelector.h" #include "WidgetFrameNumber.h" #include "Settings.h" @@ -48,6 +43,9 @@ plaYUVerApp::plaYUVerApp() setWindowModality( Qt::ApplicationModal ); + // dbus + m_pDBusAdaptor = new PlaYUVerAppAdaptor(this); + mdiArea = new PlaYUVerMdiArea; setCentralWidget( mdiArea ); //mdiArea->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded ); diff --git a/app/plaYUVerApp.h b/app/plaYUVerApp.h index 9f4cee2..2889253 100644 --- a/app/plaYUVerApp.h +++ b/app/plaYUVerApp.h @@ -26,13 +26,14 @@ #define __PLAYUVERAPP_H__ #include "config.h" -#include "lib/PlaYUVerDefs.h" #if( QT_VERSION_PLAYUVER == 5 ) #include #elif( QT_VERSION_PLAYUVER == 4 ) #include #endif #include +#include "lib/PlaYUVerDefs.h" +#include "PlaYUVerAppAdaptor.h" #include "SubWindowHandle.h" #include "ModulesHandle.h" #include "PropertiesSidebar.h" @@ -52,6 +53,7 @@ Q_OBJECT public: plaYUVerApp(); Void parseArgs( Int argc, Char *argv[] ); + Void loadFile( QString fileName, PlaYUVerStreamInfo* streamInfo = NULL ); protected: Void closeEvent( QCloseEvent *event ); @@ -143,6 +145,7 @@ private: QPixmap m_pixmapLogo; }; + PlaYUVerAppAdaptor* m_pDBusAdaptor; PlaYUVerMdiArea *mdiArea; SubWindowHandle *m_pcCurrentSubWindow; ModulesHandle *m_pcModulesHandle; @@ -153,8 +156,6 @@ private: QTime m_cTimer; UInt m_uiAveragePlayInterval; - Void loadFile( QString fileName, PlaYUVerStreamInfo* streamInfo = NULL ); - Void readRecentFileList(); Void writeRecentFileList();