summaryrefslogtreecommitdiffstats
path: root/src/tdebluezauth/application.cpp
blob: b546a893166924ca353df0f334c79fd25f678412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 *
 *  Authorization Agent implementation of bluez5
 *
 *  Copyright (C) 2018  Emanoil Kotsev <deloptes@gmail.com>
 *
 *
 *  This file is part of tdebluezauth.
 *
 *  tdebluezauth is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  tdebluezauth is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with kbluetooth; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
#include <tqdbusobjectpath.h>
#include <tdecmdlineargs.h>
#include <tdemessagebox.h>

#include <adapterImpl.h>
#include "application.h"
#include "authservice.h"

#define DBUS_AUTH_SERVICE "TDEBluezAuth"
#define DBUS_AUTH_SERVICE_NAME  "org.trinitydesktop.tdebluez"

TDEBluezAuth::TDEBluezAuth() :
        KUniqueApplication()
{
    m_connection = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus, DBUS_AUTH_SERVICE);

    if (!m_connection.isConnected())
    {
        kdError() << "Failed to open connection to system message bus: " << m_connection.lastError().message() << endl;
        exit(-1);
    }

    // try to get a specific service name
    if (!m_connection.requestName(DBUS_AUTH_SERVICE_NAME))
    {
        tqWarning(i18n("Requesting name %1 failed. "
                "The object will only be addressable through unique name '%2'").arg(
                DBUS_AUTH_SERVICE_NAME).arg(m_connection.uniqueName()));
        exit(-2);
    }

    manager = new TDEBluetooth::ObjectManagerImpl("org.bluez", "/", this, "ObjectManager");
    if (!manager->isConnectedToDBUS())
    {
        tqDebug("ObjectManager is not connected to DBus");
        exit(-3);
    }

    rootService = new RootNodeService(m_connection);
    orgService = new OrgNodeService(m_connection);
    tdeNodeService = new TrinityDekstopNodeService(m_connection);
    authService = new AuthService(m_connection);
    agentManager = 0;
    if (!configureAgent())
    {
        tqDebug("Failed to configure the auth agent");
    }
    disableSessionManagement();

// connect to manager signals
//    connect(manager, SIGNAL(adapterAdded(const TQString&)), SLOT(slotAdapterAdded(const TQString&)));
//    connect(manager, SIGNAL(adapterRemoved(const TQString&)), SLOT(slotAdapterRemoved(const TQString&)));
    connect(manager, SIGNAL(adapterPowerOnChanged(const TQString&, bool)),
            this, SLOT(slotPowerOnChanged(const TQString&, bool)));
}

TDEBluezAuth::~TDEBluezAuth()
{
    disconnect(manager, SIGNAL(adapterPowerOnChanged(const TQString&, bool)),
            this, SLOT(slotPowerOnChanged(const TQString&, bool)));
    // close D-Bus connection
    unconfigureAgent();

    m_connection.closeConnection(DBUS_AUTH_SERVICE);

    delete authService;
    delete tdeNodeService;
    delete orgService;
    delete rootService;

    delete manager;
    agentManager = 0;
}

bool TDEBluezAuth::isConnectedToDBUS()
{
    return m_connection.isConnected();
}

bool TDEBluezAuth::configureAgent()
{
    if (!agentManager)
        agentManager = manager->getAgentManager();

    if (manager->isAgentRegistered() && manager->isAgentDefaultAgent())
    {
        return true;
    }

    if (!manager->isAgentRegistered())
    {
        if (!manager->registerAgent())
        {
            tqWarning("org.bluez.Agent1 registering FAILED");
            return false;
        }
    }
    if (!manager->isAgentDefaultAgent())
    {
        if (!manager->requestDefaultAgent())
        {
            tqWarning(i18n("org.bluez.Agent1 registering FAILED"));
            return false;
        }
    }

    kdDebug() << "org.bluez.Agent1 registering OK" << endl;

    return true;
}

bool TDEBluezAuth::unconfigureAgent()
{
    if (manager->isAgentRegistered())
    {
        if (manager->unregisterAgent())
            kdDebug() << "Agent unregistered OK" << endl;
        else
            kdDebug() << "Agent unregistered FAILED" << endl;
    }
    return true;
}

void TDEBluezAuth::slotPowerOnChanged(const TQString& adapter, bool state)
{
    if (state)
        configureAgent();
    else
        unconfigureAgent();
}

#include "application.moc"