xref: /glogg/src/dbusexternalcom.cpp (revision 0dadbfaf7e57c8bc126b71f8395f219b607f4095) !
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 
40 void DBusExternalCommunicator::startListening()
41 {
42     if (!QDBusConnection::sessionBus().registerService( DBUS_SERVICE_NAME )) {
43         LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message());
44         throw CantCreateExternalErr();
45     }
46 
47     if ( !QDBusConnection::sessionBus().registerObject( "/",
48             dbus_iface_object_.get(), QDBusConnection::ExportAllContents ) ) {
49         LOG(logERROR) << qPrintable(QDBusConnection::sessionBus().lastError().message());
50         throw CantCreateExternalErr();
51     }
52 }
53 
54 ExternalInstance* DBusExternalCommunicator::otherInstance() const
55 {
56     try {
57         return static_cast<ExternalInstance*>( new DBusExternalInstance() );
58     }
59     catch ( CantCreateExternalErr ) {
60         LOG(logINFO) << "Cannot find external D-Bus correspondant, we are the only glogg out there.";
61         return nullptr;
62     }
63 }
64 
65 qint32 DBusExternalCommunicator::version() const
66 {
67     return 3;
68 }
69 
70 qint32 DBusInterfaceExternalCommunicator::version() const
71 {
72     return 0x010000;
73 }
74 
75 void DBusInterfaceExternalCommunicator::loadFile( const QString& file_name )
76 {
77     LOG(logDEBUG) << "DBusInterfaceExternalCommunicator::loadFile()";
78 
79     emit signalLoadFile( file_name );
80 }
81 
82 DBusExternalInstance::DBusExternalInstance()
83 {
84      dbusInterface_ = std::make_shared<QDBusInterface>(
85              DBUS_SERVICE_NAME, "/", "", QDBusConnection::sessionBus() );
86 
87      if ( ! dbusInterface_->isValid() ) {
88         throw CantCreateExternalErr();
89      }
90 }
91 
92 void DBusExternalInstance::loadFile( const std::string& file_name ) const
93 {
94     QDBusReply<QString> reply =
95         dbusInterface_->call( "loadFile", QString( file_name.c_str() ) );
96 
97     if ( ! reply.isValid() ) {
98         LOG( logWARNING ) << "Invalid reply from D-Bus call: "
99             << qPrintable( reply.error().message() );
100     }
101 }
102 
103 uint32_t DBusExternalInstance::getVersion() const
104 {
105     QDBusReply<qint32> reply = dbusInterface_->call( "version" );
106 
107     if ( ! reply.isValid() ) {
108         LOG( logWARNING ) << "Invalid reply from D-Bus call: "
109             << qPrintable( reply.error().message() );
110         return 0;
111     }
112 
113     return (uint32_t) reply.value();
114 }
115