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*e07da50aSNicolas Bonnefon } 37557fb9d8SNicolas Bonnefon 38*e07da50aSNicolas Bonnefon void DBusExternalCommunicator::startListening() 39*e07da50aSNicolas Bonnefon { 40557fb9d8SNicolas Bonnefon if (!QDBusConnection::sessionBus().registerService( DBUS_SERVICE_NAME )) { 41557fb9d8SNicolas Bonnefon LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message()); 42557fb9d8SNicolas Bonnefon throw CantCreateExternalErr(); 43557fb9d8SNicolas Bonnefon } 44557fb9d8SNicolas Bonnefon 45*e07da50aSNicolas Bonnefon if ( !QDBusConnection::sessionBus().registerObject("/", 46*e07da50aSNicolas Bonnefon this, QDBusConnection::ExportAllSlots) ) { 47*e07da50aSNicolas Bonnefon LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message()); 48*e07da50aSNicolas Bonnefon throw CantCreateExternalErr(); 49*e07da50aSNicolas Bonnefon } 50557fb9d8SNicolas Bonnefon } 51557fb9d8SNicolas Bonnefon 52557fb9d8SNicolas Bonnefon ExternalInstance* DBusExternalCommunicator::otherInstance() const 53557fb9d8SNicolas Bonnefon { 54557fb9d8SNicolas Bonnefon try { 55557fb9d8SNicolas Bonnefon return static_cast<ExternalInstance*>( new DBusExternalInstance() ); 56557fb9d8SNicolas Bonnefon } 57557fb9d8SNicolas Bonnefon catch ( CantCreateExternalErr ) { 58557fb9d8SNicolas Bonnefon LOG(logINFO) << "Cannot find external D-Bus correspondant, we are the only glogg out there."; 59557fb9d8SNicolas Bonnefon return nullptr; 60557fb9d8SNicolas Bonnefon } 61557fb9d8SNicolas Bonnefon } 62557fb9d8SNicolas Bonnefon 63*e07da50aSNicolas Bonnefon qint32 DBusExternalCommunicator::version() const 64557fb9d8SNicolas Bonnefon { 65*e07da50aSNicolas Bonnefon return 0x010000; 66557fb9d8SNicolas Bonnefon } 67557fb9d8SNicolas Bonnefon 68557fb9d8SNicolas Bonnefon DBusExternalInstance::DBusExternalInstance() 69557fb9d8SNicolas Bonnefon { 70557fb9d8SNicolas Bonnefon dbusInterface_ = std::make_shared<QDBusInterface>( 71*e07da50aSNicolas Bonnefon DBUS_SERVICE_NAME, "/", "", QDBusConnection::sessionBus() ); 72557fb9d8SNicolas Bonnefon 73557fb9d8SNicolas Bonnefon if ( ! dbusInterface_->isValid() ) { 74557fb9d8SNicolas Bonnefon throw CantCreateExternalErr(); 75557fb9d8SNicolas Bonnefon } 76557fb9d8SNicolas Bonnefon } 77557fb9d8SNicolas Bonnefon 78557fb9d8SNicolas Bonnefon void DBusExternalInstance::loadFile( const std::string& file_name ) const 79557fb9d8SNicolas Bonnefon { 80557fb9d8SNicolas Bonnefon QDBusReply<QString> reply = 81557fb9d8SNicolas Bonnefon dbusInterface_->call( "loadFile", QString( file_name.c_str() ) ); 82557fb9d8SNicolas Bonnefon 83557fb9d8SNicolas Bonnefon if ( ! reply.isValid() ) { 84557fb9d8SNicolas Bonnefon LOG( logWARNING ) << "Invalid reply from D-Bus call: " 85557fb9d8SNicolas Bonnefon << qPrintable( reply.error().message() ); 86557fb9d8SNicolas Bonnefon } 87557fb9d8SNicolas Bonnefon } 88*e07da50aSNicolas Bonnefon 89*e07da50aSNicolas Bonnefon uint32_t DBusExternalInstance::getVersion() const 90*e07da50aSNicolas Bonnefon { 91*e07da50aSNicolas Bonnefon QDBusReply<qint32> reply = dbusInterface_->call( "version" ); 92*e07da50aSNicolas Bonnefon 93*e07da50aSNicolas Bonnefon if ( ! reply.isValid() ) { 94*e07da50aSNicolas Bonnefon LOG( logWARNING ) << "Invalid reply from D-Bus call: " 95*e07da50aSNicolas Bonnefon << qPrintable( reply.error().message() ); 96*e07da50aSNicolas Bonnefon return 0; 97*e07da50aSNicolas Bonnefon } 98*e07da50aSNicolas Bonnefon 99*e07da50aSNicolas Bonnefon return (uint32_t) reply.value(); 100*e07da50aSNicolas Bonnefon } 101