forked from faizalzakaria/wireless_manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
147 lines (116 loc) · 3.45 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
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
/**
@file mainwindow.cpp
@brief main window
@author Faizal Zakaria
@date 2012-07-20
*/
#include "mainwindow.h"
MainWindow::MainWindow(void) : QMainWindow()
{
wireless = new Wireless();
currentIndex = -1;
currentSSID = "";
QFrame *frame = new QFrame;
QVBoxLayout *frameLayout = new QVBoxLayout(frame);
QPushButton *scanWIFI;
scanWIFI = new QPushButton("Scan Network");
connect(scanWIFI, SIGNAL(clicked()), this, SLOT(scanCompleted()));
QPushButton *joinNetwork;
joinNetwork = new QPushButton("Join");
connect(joinNetwork, SIGNAL(clicked()), this, SLOT(joinNetwork()));
networkList = new QListView();
networkList->setViewMode(QListView::ListMode);
listModel = new QStringListModel();
networkList->setModel(listModel);
frameLayout->addWidget(scanWIFI);
frameLayout->addWidget(networkList);
frameLayout->addWidget(joinNetwork);
setFixedSize( 640, 480 );
//frame->setFrameRect(QRect( 10, 10, 640, 480 ));
setCentralWidget(frame);
QRect screenGeometry = QApplication::desktop()->screenGeometry();
int x = (screenGeometry.width() - width()) / 2;
int y = (screenGeometry.height() - height()) / 2;
move( x, y );
createStatusBar();
passwordQDialog = new PasswordQDialog( this );
setWindowTitle( tr("Wireless Manager") );
// SelectionModel
/*
QItemSelectionModel *selectionModel = networkList->selectionModel();
connect( selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)) );
*/
}
MainWindow::~MainWindow()
{
delete wireless;
delete passwordQDialog;
delete listModel;
delete networkList;
}
void MainWindow::scanCompleted()
{
QString text = "Scanning network ... ";
int status = 0;
statusBar()->showMessage(text);
status = wireless->scanNetworks( (const QString) "wlan0" );
if (status != 0)
{
text = "Failed to scan network ... ";
statusBar()->showMessage(text);
}
else
{
text = "Scan network done! ";
statusBar()->showMessage(text);
}
listModel->setStringList( (const QStringList)*wireless->getNetworkList() );
}
void MainWindow::joinNetwork()
{
const QModelIndex index = networkList->selectionModel()->currentIndex();
currentSSID = index.data(Qt::DisplayRole).toString();
QString text;
text = "Connecting to " + currentSSID + " ...";
statusBar()->showMessage(text);
int status = wireless->joinOpenNetwork((const QString) currentSSID);
if ( status < 0 )
{
text = "Failed to join Network : " + currentSSID + ", try again";
statusBar()->showMessage(text);
//passwordQDialog->setSSID( ¤tSSID );
passwordQDialog->show();
}
else
{
text = "Connected to " + currentSSID;
statusBar()->showMessage(text);
}
}
void MainWindow::createStatusBar()
{
statusBar()->showMessage(tr("Ready"));
}
void MainWindow::hidePasswordQDialog()
{
passwordQDialog->hide();
}
void MainWindow::joinWPANetwork()
{
QString text;
text = "Connecting to " + currentSSID + " ..." + passwordQDialog->getPassword();
statusBar()->showMessage(text);
int status = wireless->joinWPANetwork((const QString) currentSSID, (const QString) passwordQDialog->getPassword() );
if ( status < 0 )
{
text = "Failed to join Network : " + currentSSID + ", try again";
statusBar()->showMessage(text);
}
else
{
text = "Connected to " + currentSSID;
statusBar()->showMessage(text);
}
passwordQDialog->hide();
}