-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
102 lines (83 loc) · 2.93 KB
/
mainwindow.cpp
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
/*
* Qt BlueZ Manager
*
* Copyright (C) 2010 Jose Antonio Santos-Cadenas <santoscadenas at gmail.com>
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "mainwindow.h"
#include "adapterview.h"
#include <QtGui>
#include <kicon.h>
#include <kstatusnotifieritem.h>
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
manager("org.bluez", "/", QDBusConnection::systemBus())
{
setWindowIcon(KIcon("preferences-system-bluetooth"));
setWindowTitle(tr("Bluetooth Manager"));
QVBoxLayout *vLayout = new QVBoxLayout(this);
QDBusPendingReply<QVariantMap> dprops = manager.GetProperties();
dprops.waitForFinished();
if (!dprops.isValid()) {
qCritical() << "Received unvalid reply";
return;
}
QStringList paths = qdbus_cast<QStringList>(dprops.value()["Adapters"]);
foreach (QString path, paths) {
AdapterView *adapterView = new AdapterView(path, this);
vLayout->addWidget(adapterView);
adapters.append(adapterView);
}
spacer = new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding,
QSizePolicy::MinimumExpanding);
vLayout->addItem(spacer);
KStatusNotifierItem *tray = new KStatusNotifierItem(this);
tray->setCategory(KStatusNotifierItem::Hardware);
tray->setIconByName("preferences-system-bluetooth");
tray->setToolTipIconByName("preferences-system-bluetooth");
tray->setToolTipTitle("BlueZ Manager");
tray->setToolTipSubTitle("Bluetooth Manager");
tray->setStatus(KStatusNotifierItem::Active);
connect(&manager, SIGNAL(AdapterAdded(QDBusObjectPath)), this,
SLOT(adapterAdded(QDBusObjectPath)));
connect(&manager, SIGNAL(AdapterRemoved(QDBusObjectPath)), this,
SLOT(adapterRemoved(QDBusObjectPath)));
}
AdapterView *MainWindow::getAdapterView(const QString path)
{
foreach (AdapterView *view, adapters) {
if (view->adapterPath() == path)
return view;
}
return NULL;
}
void MainWindow::adapterRemoved(QDBusObjectPath path)
{
AdapterView *view = getAdapterView(path.path());
adapters.removeAll(view);
layout()->removeWidget(view);
view->notifyDestruction();
delete view;
}
void MainWindow::adapterAdded(QDBusObjectPath path)
{
AdapterView *adapterView = new AdapterView(path.path(), this);
layout()->removeItem(spacer);
layout()->addWidget(adapterView);
layout()->addItem(spacer);
adapters.append(adapterView);
adapterView->notifyCreation();
}