1557fb9d8SNicolas Bonnefon /* 2557fb9d8SNicolas Bonnefon * Copyright (C) 2014 Nicolas Bonnefon and other contributors 3557fb9d8SNicolas Bonnefon * 4557fb9d8SNicolas Bonnefon * This file is part of glogg. 5557fb9d8SNicolas Bonnefon * 6557fb9d8SNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7557fb9d8SNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8557fb9d8SNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9557fb9d8SNicolas Bonnefon * (at your option) any later version. 10557fb9d8SNicolas Bonnefon * 11557fb9d8SNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12557fb9d8SNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13557fb9d8SNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14557fb9d8SNicolas Bonnefon * GNU General Public License for more details. 15557fb9d8SNicolas Bonnefon * 16557fb9d8SNicolas Bonnefon * You should have received a copy of the GNU General Public License 17557fb9d8SNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18557fb9d8SNicolas Bonnefon */ 19557fb9d8SNicolas Bonnefon 20557fb9d8SNicolas Bonnefon #include "dbusexternalcom.h" 21557fb9d8SNicolas Bonnefon 22557fb9d8SNicolas Bonnefon #include <QString> 23557fb9d8SNicolas Bonnefon 24557fb9d8SNicolas Bonnefon #include "log.h" 25557fb9d8SNicolas Bonnefon 26557fb9d8SNicolas Bonnefon static const char* DBUS_SERVICE_NAME = "org.bonnefon.glogg"; 27557fb9d8SNicolas Bonnefon 28557fb9d8SNicolas Bonnefon DBusExternalCommunicator::DBusExternalCommunicator() 29557fb9d8SNicolas Bonnefon { 30557fb9d8SNicolas Bonnefon if (!QDBusConnection::sessionBus().isConnected()) { 31557fb9d8SNicolas Bonnefon LOG(logERROR) << "Cannot connect to the D-Bus session bus.\n" 32557fb9d8SNicolas Bonnefon << "To start it, run:\n" 33557fb9d8SNicolas Bonnefon << "\teval `dbus-launch --auto-syntax`\n"; 34557fb9d8SNicolas Bonnefon throw CantCreateExternalErr(); 35557fb9d8SNicolas Bonnefon } 360dadbfafSNicolas Bonnefon 370dadbfafSNicolas Bonnefon dbus_iface_object_ = std::make_shared<DBusInterfaceExternalCommunicator>(); 38*09aff35dSNicolas Bonnefon 39*09aff35dSNicolas Bonnefon connect( dbus_iface_object_.get(), SIGNAL( signalLoadFile( const QString& ) ), 40*09aff35dSNicolas Bonnefon this, SIGNAL( loadFile( const QString& ) ) ); 41e07da50aSNicolas Bonnefon } 42557fb9d8SNicolas Bonnefon 43e07da50aSNicolas Bonnefon void DBusExternalCommunicator::startListening() 44e07da50aSNicolas Bonnefon { 45557fb9d8SNicolas Bonnefon if (!QDBusConnection::sessionBus().registerService( DBUS_SERVICE_NAME )) { 46557fb9d8SNicolas Bonnefon LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message()); 47557fb9d8SNicolas Bonnefon throw CantCreateExternalErr(); 48557fb9d8SNicolas Bonnefon } 49557fb9d8SNicolas Bonnefon 50e07da50aSNicolas Bonnefon if ( !QDBusConnection::sessionBus().registerObject( "/", 510dadbfafSNicolas Bonnefon dbus_iface_object_.get(), QDBusConnection::ExportAllContents ) ) { 52e07da50aSNicolas Bonnefon LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message()); 53e07da50aSNicolas Bonnefon throw CantCreateExternalErr(); 54e07da50aSNicolas Bonnefon } 55557fb9d8SNicolas Bonnefon } 56557fb9d8SNicolas Bonnefon 57557fb9d8SNicolas Bonnefon ExternalInstance* DBusExternalCommunicator::otherInstance() const 58557fb9d8SNicolas Bonnefon { 59557fb9d8SNicolas Bonnefon try { 60557fb9d8SNicolas Bonnefon return static_cast<ExternalInstance*>( new DBusExternalInstance() ); 61557fb9d8SNicolas Bonnefon } 62557fb9d8SNicolas Bonnefon catch ( CantCreateExternalErr ) { 63557fb9d8SNicolas Bonnefon LOG(logINFO) << "Cannot find external D-Bus correspondant, we are the only glogg out there."; 64557fb9d8SNicolas Bonnefon return nullptr; 65557fb9d8SNicolas Bonnefon } 66557fb9d8SNicolas Bonnefon } 67557fb9d8SNicolas Bonnefon 68e07da50aSNicolas Bonnefon qint32 DBusExternalCommunicator::version() const 69557fb9d8SNicolas Bonnefon { 700dadbfafSNicolas Bonnefon return 3; 710dadbfafSNicolas Bonnefon } 720dadbfafSNicolas Bonnefon 730dadbfafSNicolas Bonnefon qint32 DBusInterfaceExternalCommunicator::version() const 740dadbfafSNicolas Bonnefon { 75e07da50aSNicolas Bonnefon return 0x010000; 76557fb9d8SNicolas Bonnefon } 77557fb9d8SNicolas Bonnefon 780dadbfafSNicolas Bonnefon void DBusInterfaceExternalCommunicator::loadFile( const QString& file_name ) 790dadbfafSNicolas Bonnefon { 800dadbfafSNicolas Bonnefon LOG(logDEBUG) << "DBusInterfaceExternalCommunicator::loadFile()"; 810dadbfafSNicolas Bonnefon 820dadbfafSNicolas Bonnefon emit signalLoadFile( file_name ); 830dadbfafSNicolas Bonnefon } 840dadbfafSNicolas Bonnefon 85557fb9d8SNicolas Bonnefon DBusExternalInstance::DBusExternalInstance() 86557fb9d8SNicolas Bonnefon { 87557fb9d8SNicolas Bonnefon dbusInterface_ = std::make_shared<QDBusInterface>( 88e07da50aSNicolas Bonnefon DBUS_SERVICE_NAME, "/", "", QDBusConnection::sessionBus() ); 89557fb9d8SNicolas Bonnefon 90557fb9d8SNicolas Bonnefon if ( ! dbusInterface_->isValid() ) { 91557fb9d8SNicolas Bonnefon throw CantCreateExternalErr(); 92557fb9d8SNicolas Bonnefon } 93557fb9d8SNicolas Bonnefon } 94557fb9d8SNicolas Bonnefon 95*09aff35dSNicolas Bonnefon void DBusExternalInstance::loadFile( const QString& file_name ) const 96557fb9d8SNicolas Bonnefon { 97*09aff35dSNicolas Bonnefon QDBusReply<void> reply = dbusInterface_->call( "loadFile", file_name ); 98557fb9d8SNicolas Bonnefon 99557fb9d8SNicolas Bonnefon if ( ! reply.isValid() ) { 100557fb9d8SNicolas Bonnefon LOG( logWARNING ) << "Invalid reply from D-Bus call: " 101557fb9d8SNicolas Bonnefon << qPrintable( reply.error().message() ); 102557fb9d8SNicolas Bonnefon } 103557fb9d8SNicolas Bonnefon } 104e07da50aSNicolas Bonnefon 105e07da50aSNicolas Bonnefon uint32_t DBusExternalInstance::getVersion() const 106e07da50aSNicolas Bonnefon { 107e07da50aSNicolas Bonnefon QDBusReply<qint32> reply = dbusInterface_->call( "version" ); 108e07da50aSNicolas Bonnefon 109e07da50aSNicolas Bonnefon if ( ! reply.isValid() ) { 110e07da50aSNicolas Bonnefon LOG( logWARNING ) << "Invalid reply from D-Bus call: " 111e07da50aSNicolas Bonnefon << qPrintable( reply.error().message() ); 112e07da50aSNicolas Bonnefon return 0; 113e07da50aSNicolas Bonnefon } 114e07da50aSNicolas Bonnefon 115e07da50aSNicolas Bonnefon return (uint32_t) reply.value(); 116e07da50aSNicolas Bonnefon } 117