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 } 36*0dadbfafSNicolas Bonnefon 37*0dadbfafSNicolas Bonnefon dbus_iface_object_ = std::make_shared<DBusInterfaceExternalCommunicator>(); 38e07da50aSNicolas Bonnefon } 39557fb9d8SNicolas Bonnefon 40e07da50aSNicolas Bonnefon void DBusExternalCommunicator::startListening() 41e07da50aSNicolas Bonnefon { 42557fb9d8SNicolas Bonnefon if (!QDBusConnection::sessionBus().registerService( DBUS_SERVICE_NAME )) { 43557fb9d8SNicolas Bonnefon LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message()); 44557fb9d8SNicolas Bonnefon throw CantCreateExternalErr(); 45557fb9d8SNicolas Bonnefon } 46557fb9d8SNicolas Bonnefon 47e07da50aSNicolas Bonnefon if ( !QDBusConnection::sessionBus().registerObject( "/", 48*0dadbfafSNicolas Bonnefon dbus_iface_object_.get(), QDBusConnection::ExportAllContents ) ) { 49e07da50aSNicolas Bonnefon LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message()); 50e07da50aSNicolas Bonnefon throw CantCreateExternalErr(); 51e07da50aSNicolas Bonnefon } 52557fb9d8SNicolas Bonnefon } 53557fb9d8SNicolas Bonnefon 54557fb9d8SNicolas Bonnefon ExternalInstance* DBusExternalCommunicator::otherInstance() const 55557fb9d8SNicolas Bonnefon { 56557fb9d8SNicolas Bonnefon try { 57557fb9d8SNicolas Bonnefon return static_cast<ExternalInstance*>( new DBusExternalInstance() ); 58557fb9d8SNicolas Bonnefon } 59557fb9d8SNicolas Bonnefon catch ( CantCreateExternalErr ) { 60557fb9d8SNicolas Bonnefon LOG(logINFO) << "Cannot find external D-Bus correspondant, we are the only glogg out there."; 61557fb9d8SNicolas Bonnefon return nullptr; 62557fb9d8SNicolas Bonnefon } 63557fb9d8SNicolas Bonnefon } 64557fb9d8SNicolas Bonnefon 65e07da50aSNicolas Bonnefon qint32 DBusExternalCommunicator::version() const 66557fb9d8SNicolas Bonnefon { 67*0dadbfafSNicolas Bonnefon return 3; 68*0dadbfafSNicolas Bonnefon } 69*0dadbfafSNicolas Bonnefon 70*0dadbfafSNicolas Bonnefon qint32 DBusInterfaceExternalCommunicator::version() const 71*0dadbfafSNicolas Bonnefon { 72e07da50aSNicolas Bonnefon return 0x010000; 73557fb9d8SNicolas Bonnefon } 74557fb9d8SNicolas Bonnefon 75*0dadbfafSNicolas Bonnefon void DBusInterfaceExternalCommunicator::loadFile( const QString& file_name ) 76*0dadbfafSNicolas Bonnefon { 77*0dadbfafSNicolas Bonnefon LOG(logDEBUG) << "DBusInterfaceExternalCommunicator::loadFile()"; 78*0dadbfafSNicolas Bonnefon 79*0dadbfafSNicolas Bonnefon emit signalLoadFile( file_name ); 80*0dadbfafSNicolas Bonnefon } 81*0dadbfafSNicolas Bonnefon 82557fb9d8SNicolas Bonnefon DBusExternalInstance::DBusExternalInstance() 83557fb9d8SNicolas Bonnefon { 84557fb9d8SNicolas Bonnefon dbusInterface_ = std::make_shared<QDBusInterface>( 85e07da50aSNicolas Bonnefon DBUS_SERVICE_NAME, "/", "", QDBusConnection::sessionBus() ); 86557fb9d8SNicolas Bonnefon 87557fb9d8SNicolas Bonnefon if ( ! dbusInterface_->isValid() ) { 88557fb9d8SNicolas Bonnefon throw CantCreateExternalErr(); 89557fb9d8SNicolas Bonnefon } 90557fb9d8SNicolas Bonnefon } 91557fb9d8SNicolas Bonnefon 92557fb9d8SNicolas Bonnefon void DBusExternalInstance::loadFile( const std::string& file_name ) const 93557fb9d8SNicolas Bonnefon { 94557fb9d8SNicolas Bonnefon QDBusReply<QString> reply = 95557fb9d8SNicolas Bonnefon dbusInterface_->call( "loadFile", QString( file_name.c_str() ) ); 96557fb9d8SNicolas Bonnefon 97557fb9d8SNicolas Bonnefon if ( ! reply.isValid() ) { 98557fb9d8SNicolas Bonnefon LOG( logWARNING ) << "Invalid reply from D-Bus call: " 99557fb9d8SNicolas Bonnefon << qPrintable( reply.error().message() ); 100557fb9d8SNicolas Bonnefon } 101557fb9d8SNicolas Bonnefon } 102e07da50aSNicolas Bonnefon 103e07da50aSNicolas Bonnefon uint32_t DBusExternalInstance::getVersion() const 104e07da50aSNicolas Bonnefon { 105e07da50aSNicolas Bonnefon QDBusReply<qint32> reply = dbusInterface_->call( "version" ); 106e07da50aSNicolas Bonnefon 107e07da50aSNicolas Bonnefon if ( ! reply.isValid() ) { 108e07da50aSNicolas Bonnefon LOG( logWARNING ) << "Invalid reply from D-Bus call: " 109e07da50aSNicolas Bonnefon << qPrintable( reply.error().message() ); 110e07da50aSNicolas Bonnefon return 0; 111e07da50aSNicolas Bonnefon } 112e07da50aSNicolas Bonnefon 113e07da50aSNicolas Bonnefon return (uint32_t) reply.value(); 114e07da50aSNicolas Bonnefon } 115