source: ntrip/trunk/BNC/bnctabledlg.cpp@ 191

Last change on this file since 191 was 191, checked in by mervart, 18 years ago

* empty log message *

File size: 6.7 KB
Line 
1
2/* -------------------------------------------------------------------------
3 * BKG NTRIP Client
4 * -------------------------------------------------------------------------
5 *
6 * Class: bncTableDlg
7 *
8 * Purpose: Displays the source table, allows mountpoints selection
9 *
10 * Author: L. Mervart
11 *
12 * Created: 24-Dec-2005
13 *
14 * Changes:
15 *
16 * -----------------------------------------------------------------------*/
17
18#include "bnctabledlg.h"
19#include "bncgetthread.h"
20
21// Constructor
22////////////////////////////////////////////////////////////////////////////
23bncTableDlg::bncTableDlg(QWidget* parent) : QDialog(parent) {
24
25 setMinimumSize(600,400);
26
27 QVBoxLayout* mainLayout = new QVBoxLayout(this);
28
29 QSettings settings;
30 _casterHostLineEdit = new QLineEdit(settings.value("casterHost").toString());
31 int ww = QFontMetrics(_casterHostLineEdit->font()).width('w');
32 _casterHostLineEdit->setMaximumWidth(18*ww);
33 _casterPortLineEdit = new QLineEdit(settings.value("casterPort").toString());
34 _casterPortLineEdit->setMaximumWidth(9*ww);
35 _casterUserLineEdit = new QLineEdit(settings.value("casterUser").toString());
36 _casterUserLineEdit->setMaximumWidth(9*ww);
37 _casterPasswordLineEdit = new QLineEdit(settings.value("casterPassword").toString());
38 _casterPasswordLineEdit->setMaximumWidth(9*ww);
39 _casterPasswordLineEdit->setEchoMode(QLineEdit::Password);
40
41 QGridLayout* editLayout = new QGridLayout;
42 editLayout->addWidget(new QLabel(tr("Caster host")), 0, 0);
43 editLayout->addWidget(_casterHostLineEdit, 0, 1);
44 editLayout->addWidget(new QLabel(tr("Caster port")), 0, 2);
45 editLayout->addWidget(_casterPortLineEdit, 0, 3);
46 editLayout->addWidget(new QLabel(tr("User")), 1, 0);
47 editLayout->addWidget(_casterUserLineEdit, 1, 1);
48 editLayout->addWidget(new QLabel(tr("Password")), 1, 2);
49 editLayout->addWidget(_casterPasswordLineEdit, 1, 3);
50
51 mainLayout->addLayout(editLayout);
52
53 _table = new QTableWidget(this);
54 mainLayout->addWidget(_table);
55
56 _buttonGet = new QPushButton(tr("Get Table"), this);
57 connect(_buttonGet, SIGNAL(clicked()), this, SLOT(slotGetTable()));
58
59 _buttonCancel = new QPushButton(tr("Cancel"), this);
60 connect(_buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
61
62 _buttonOK = new QPushButton(tr("OK"), this);
63 connect(_buttonOK, SIGNAL(clicked()), this, SLOT(accept()));
64
65 QHBoxLayout* buttonLayout = new QHBoxLayout;
66 buttonLayout->addStretch(1);
67 buttonLayout->addWidget(_buttonGet);
68 buttonLayout->addWidget(_buttonCancel);
69 buttonLayout->addWidget(_buttonOK);
70
71 mainLayout->addLayout(buttonLayout);
72}
73
74// Destructor
75////////////////////////////////////////////////////////////////////////////
76bncTableDlg::~bncTableDlg() {
77 if (_table) {
78 for (int ir = 0; ir < _table->rowCount(); ir++) {
79 for (int ic = 0; ic < _table->columnCount(); ic++) {
80 delete _table->item(ir,ic);
81 }
82 }
83 }
84}
85
86// Read Table the caster (static)
87////////////////////////////////////////////////////////////////////////////
88t_irc bncTableDlg::getFullTable(const QString& casterHost,
89 int casterPort, QStringList& allLines) {
90
91 allLines.clear();
92
93 QUrl url;
94 url.setHost(casterHost);
95 url.setPort(casterPort);
96
97 // Send the Request
98 // ----------------
99 const int timeOut = 10*1000;
100 QString msg;
101 QTcpSocket* socket = bncGetThread::request(url, timeOut, msg);
102
103 if (!socket) {
104 return failure;
105 }
106
107 // Read Caster Response
108 // --------------------
109 bool first = true;
110 while (true) {
111 if (socket->canReadLine()) {
112 QString line = socket->readLine();
113 allLines.push_back(line);
114 if (first) {
115 first = false;
116 if (line.indexOf("SOURCETABLE 200 OK") != 0) {
117 break;
118 }
119 }
120 else {
121 if (line.indexOf("ENDSOURCETABLE") == 0) {
122 break;
123 }
124 }
125 }
126 else {
127 socket->waitForReadyRead(timeOut);
128 if (socket->bytesAvailable() > 0) {
129 continue;
130 }
131 else {
132 break;
133 }
134 }
135 }
136 delete socket;
137
138 return success;
139}
140
141// Read Table from Caster
142////////////////////////////////////////////////////////////////////////////
143void bncTableDlg::slotGetTable() {
144
145 QStringList allLines;
146
147 if ( getFullTable(_casterHostLineEdit->text(),
148 _casterPortLineEdit->text().toInt(),
149 allLines) != success ) {
150 QMessageBox::warning(0, "BNC", "Cannot retrieve table of data");
151 return;
152 }
153
154 QStringList lines;
155 QStringListIterator it(allLines);
156 while (it.hasNext()) {
157 QString line = it.next();
158 if (line.indexOf("STR") == 0) {
159 lines.push_back(line);
160 }
161 }
162
163 static const QStringList labels = QString("mountpoint,identifier,format,"
164 "format-details,carrier,nav-system,network,country,latitude,longitude,"
165 "nmea,solution,generator,compression,authentication,fee,bitrate,"
166 "misc").split(",");
167
168 if (lines.size() > 0) {
169 _table->setSelectionMode(QAbstractItemView::ExtendedSelection);
170 _table->setSelectionBehavior(QAbstractItemView::SelectRows);
171
172 QStringList hlp = lines[0].split(";");
173 _table->setColumnCount(hlp.size()-1);
174 _table->setRowCount(lines.size());
175
176 QListIterator<QString> it(lines);
177 int nRow = -1;
178 while (it.hasNext()) {
179 QStringList columns = it.next().split(";");
180 ++nRow;
181 for (int ic = 0; ic < columns.size()-1; ic++) {
182 QTableWidgetItem* it = new QTableWidgetItem(columns[ic+1]);
183 it->setFlags(it->flags() & ~Qt::ItemIsEditable);
184 _table->setItem(nRow, ic, it);
185 }
186 }
187 _table->sortItems(0);
188 _table->setHorizontalHeaderLabels(labels);
189 _table->setSortingEnabled(true);
190 }
191}
192
193// Accept slot
194////////////////////////////////////////////////////////////////////////////
195void bncTableDlg::accept() {
196
197 QSettings settings;
198 settings.setValue("casterHost", _casterHostLineEdit->text());
199 settings.setValue("casterPort", _casterPortLineEdit->text());
200 settings.setValue("casterUser", _casterUserLineEdit->text());
201 settings.setValue("casterPassword", _casterPasswordLineEdit->text());
202
203 QStringList* mountPoints = new QStringList;
204
205 if (_table) {
206 for (int ir = 0; ir < _table->rowCount(); ir++) {
207 QTableWidgetItem* item = _table->item(ir,0);
208 QString format = _table->item(ir,2)->text();
209 format.replace(" ", "_");
210 if (_table->isItemSelected(item)) {
211 QUrl url;
212 url.setUserName(_casterUserLineEdit->text());
213 url.setPassword(_casterPasswordLineEdit->text());
214 url.setHost(_casterHostLineEdit->text());
215 url.setPort(_casterPortLineEdit->text().toInt());
216 url.setPath(item->text());
217
218 mountPoints->push_back(url.toString() + " " + format);
219 }
220 }
221 }
222 emit newMountPoints(mountPoints);
223
224 QDialog::accept();
225}
Note: See TracBrowser for help on using the repository browser.