summaryrefslogtreecommitdiffstats
path: root/src/tdebluez/adapterconfigdialog.cpp
blob: fc3bd5e84e876bb643ff1f10e1bc4a8a7d5a18a5 (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
/*
 *
 *  Adapter Manager Gui for tdebluez
 *
 *  Copyright (C) 2018  Emanoil Kotsev <deloptes@gmail.com>
 *
 *
 *  This file is part of tdebluez.
 *
 *  tdebluez 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.
 *
 *  tdebluez 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 "adapterconfig.h"
#include "adapterconfigdialog.h"

AdapterConfigDialog::AdapterConfigDialog(TDEBluetoothApp *_app) :
        KDialogBase(NULL, "AdapterConfigDialog", true, "Adapter Configuration", (Ok)), app(_app), manager(_app->manager)
{
    nodevice = NULL;
    tabWidget = new TQTabWidget(this);

    ObjectManagerImpl::AdapterList list = manager->getAdapters();
    ObjectManagerImpl::AdapterList::iterator it;
    for (it = list.begin(); it != list.end(); ++it)
        addAdapter((*it));

    if (list.count() == 0)
    {
        nodevice = new TQLabel(i18n("No Bluetooth adapter found!"), this);
        tabWidget->addTab(nodevice, i18n("no adapter"));
        tabWidget->setMinimumSize(250, 300);
    }

    tabWidget->show();
    setMainWidget(tabWidget);
    setModal(false);

    connect(this, TQ_SIGNAL(okClicked()), this, TQ_SLOT(hide()));
    connect(manager, TQ_SIGNAL(adapterAdded(const TQString&)), TQ_SLOT(addAdapter(const TQString&)));
    connect(manager, TQ_SIGNAL(adapterRemoved(const TQString&)), TQ_SLOT(removeAdapter(const TQString&)));
    connect(tabWidget, TQ_SIGNAL(currentChanged(TQWidget *)), this, TQ_SLOT(slotCurrentChanged(TQWidget *)));
}

AdapterConfigDialog::~AdapterConfigDialog()
{
    close();
    if (nodevice)
        delete nodevice;
    delete tabWidget;
//    tabWidget = 0;
}

void AdapterConfigDialog::addAdapter(const TQString &path)
{
    AdapterConfig *aconfig = new AdapterConfig(app->manager, app->adapters[path]);
    tabWidget->addTab(aconfig->dialog(), aconfig->getName());

    TQT_DBusError dbuserr;
    bool powered = app->adapters[path]->getPowered(dbuserr);
    if (dbuserr.isValid())
        tqDebug(i18n("Adapter getPowered failed: %1").arg(dbuserr.message()));

    aconfig->dialog()->setEnabled(powered);
//
//    if (tabWidget->isTabEnabled(aconfig->dialog()))
//    {
        tabWidget->showPage(aconfig->dialog());
//    }
    adapterList.insert(path, aconfig);

    connect(aconfig->dialog()->adapterName, TQ_SIGNAL(textChanged(const TQString &)),
            this, TQ_SLOT(slotChangeName(const TQString &)));
    connect(aconfig->dialog()->adapterName, TQ_SIGNAL(textChanged(const TQString &)),
            app->adapters[path], TQ_SLOT(slotSetAlias(const TQString &)));

    if (nodevice)
    {
        tabWidget->removePage(nodevice);
        nodevice = 0;
    }
}

void AdapterConfigDialog::removeAdapter(const TQString &path)
{
    AdapterConfig *aconfig = adapterList[path];
    if (!aconfig)
        return;

    tabWidget->removePage(aconfig->dialog());
    delete adapterList[path];
    adapterList.remove(path);

    if (adapterList.count() == 0)
    {
        nodevice = new TQLabel(i18n("No Bluetooth device found!"), tabWidget);
        tabWidget->addTab(nodevice, i18n("no device"));
        if (tabWidget->isTabEnabled(nodevice))
        {
            tabWidget->showPage(nodevice);
        }
    }
}

void AdapterConfigDialog::slotChangeName(const TQString &name)
{
    tabWidget->changeTab(tabWidget->currentPage(), name);
}

void AdapterConfigDialog::slotCurrentChanged(TQWidget *widget)
{
    TQMap<TQString,AdapterConfig *>::iterator it;
    for (it = adapterList.begin(); it != adapterList.end(); ++it)
    {
        if (it.data()->dialog() == widget)
        {
            TQString path = it.key();
            TQT_DBusError dbuserr;
            TQString name = app->adapters[path]->getAlias(dbuserr);
            if (dbuserr.isValid())
                tqDebug(i18n("Adapter getAlias failed: %1").arg(dbuserr.message()));
//            kdDebug() << "Adapter changed: " << it.data()->dialog()->macLabel->text() << endl;
            emit signalAdapterSelected(path,name);
            break;
        }
    }
}

#include "adapterconfigdialog.moc"