1 /* 2 * Copyright (C) 2014 Nicolas Bonnefon and other contributors 3 * 4 * This file is part of glogg. 5 * 6 * glogg is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * glogg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "dbusexternalcom.h" 21 22 #include <QString> 23 24 #include "log.h" 25 26 static const char* DBUS_SERVICE_NAME = "org.bonnefon.glogg"; 27 28 DBusExternalCommunicator::DBusExternalCommunicator() 29 { 30 if (!QDBusConnection::sessionBus().isConnected()) { 31 LOG(logERROR) << "Cannot connect to the D-Bus session bus.\n" 32 << "To start it, run:\n" 33 << "\teval `dbus-launch --auto-syntax`\n"; 34 throw CantCreateExternalErr(); 35 } 36 37 dbus_iface_object_ = std::make_shared<DBusInterfaceExternalCommunicator>(); 38 39 connect( dbus_iface_object_.get(), SIGNAL( signalLoadFile( const QString& ) ), 40 this, SIGNAL( loadFile( const QString& ) ) ); 41 } 42 43 void DBusExternalCommunicator::startListening() 44 { 45 if (!QDBusConnection::sessionBus().registerService( DBUS_SERVICE_NAME )) { 46 LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message()); 47 throw CantCreateExternalErr(); 48 } 49 50 if ( !QDBusConnection::sessionBus().registerObject( "/", 51 dbus_iface_object_.get(), QDBusConnection::ExportAllContents ) ) { 52 LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message()); 53 throw CantCreateExternalErr(); 54 } 55 } 56 57 ExternalInstance* DBusExternalCommunicator::otherInstance() const 58 { 59 try { 60 return static_cast<ExternalInstance*>( new DBusExternalInstance() ); 61 } 62 catch ( CantCreateExternalErr ) { 63 LOG(logINFO) << "Cannot find external D-Bus correspondant, we are the only glogg out there."; 64 return nullptr; 65 } 66 } 67 68 qint32 DBusExternalCommunicator::version() const 69 { 70 return 3; 71 } 72 73 qint32 DBusInterfaceExternalCommunicator::version() const 74 { 75 return 0x010000; 76 } 77 78 void DBusInterfaceExternalCommunicator::loadFile( const QString& file_name ) 79 { 80 LOG(logDEBUG) << "DBusInterfaceExternalCommunicator::loadFile()"; 81 82 emit signalLoadFile( file_name ); 83 } 84 85 DBusExternalInstance::DBusExternalInstance() 86 { 87 dbusInterface_ = std::make_shared<QDBusInterface>( 88 DBUS_SERVICE_NAME, "/", "", QDBusConnection::sessionBus() ); 89 90 if ( ! dbusInterface_->isValid() ) { 91 throw CantCreateExternalErr(); 92 } 93 } 94 95 void DBusExternalInstance::loadFile( const QString& file_name ) const 96 { 97 QDBusReply<void> reply = dbusInterface_->call( "loadFile", file_name ); 98 99 if ( ! reply.isValid() ) { 100 LOG( logWARNING ) << "Invalid reply from D-Bus call: " 101 << qPrintable( reply.error().message() ); 102 } 103 } 104 105 uint32_t DBusExternalInstance::getVersion() const 106 { 107 QDBusReply<qint32> reply = dbusInterface_->call( "version" ); 108 109 if ( ! reply.isValid() ) { 110 LOG( logWARNING ) << "Invalid reply from D-Bus call: " 111 << qPrintable( reply.error().message() ); 112 return 0; 113 } 114 115 return (uint32_t) reply.value(); 116 } 117