740e2a8c11b99300589b93ff5e13c1f3891b0abe
[jenirok] / src / gui / connectionselector.cpp
1 /*
2  * This file is part of Jenirok.
3  *
4  * Jenirok is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Jenirok is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Jenirok.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QList>
20 #include <QtCore/QVariant>
21 #include <QDebug>
22 #include "connectionselector.h"
23 #include "connectionmanager.h"
24
25 ConnectionSelector::ConnectionSelector(QString const& text, QWidget* parent):
26 ButtonSelector(text, parent), loaded_(false), gprsIndex_(0)
27 {
28     addItems();
29 }
30
31 void ConnectionSelector::updateConnections()
32 {
33     QVariant currentValue = value();
34
35     addItems();
36
37     ConnectionManager cm;
38
39     QList<ConnectionManager::Connection> connections;
40
41     if(cm.scanConnections(connections, ConnectionManager::GPRS) &&
42        connections.size() > 0)
43     {
44         changeItem(gprsIndex_, connections.at(0).name, "gprs");
45     }
46     else
47     {
48         qDebug() << "Unable to scan network connections";
49     }
50
51     selectByValue(currentValue);
52     loaded_ = true;
53 }
54
55 void ConnectionSelector::setVisible(bool visible)
56 {
57     if(visible && !loaded_)
58     {
59         updateConnections();
60     }
61
62     ButtonSelector::setVisible(visible);
63 }
64
65 void ConnectionSelector::addItems()
66 {
67     clear();
68
69     addItem(tr("Use global setting"), "global");
70     addItem(tr("WLAN connection"), "wlan");
71     addItem(tr("GPRS connection"), "gprs");
72     addItem(tr("Any connection"), "any");
73
74     gprsIndex_ = 2;
75 }