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