1 |
|
---|
2 | /* -------------------------------------------------------------------------
|
---|
3 | * Bernese 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
23 | bncTableDlg::bncTableDlg(QWidget* parent, const QString& proxyHost,
|
---|
24 | int proxyPort) : QDialog(parent) {
|
---|
25 |
|
---|
26 | _proxyHost = proxyHost;
|
---|
27 | _proxyPort = proxyPort;
|
---|
28 |
|
---|
29 | setMinimumSize(600,400);
|
---|
30 |
|
---|
31 | QVBoxLayout* mainLayout = new QVBoxLayout(this);
|
---|
32 |
|
---|
33 | _casterHostLabel = new QLabel(tr("caster host"));
|
---|
34 | _casterPortLabel = new QLabel(tr("caster port"));
|
---|
35 | QSettings settings;
|
---|
36 | _casterHostLineEdit = new QLineEdit(settings.value("casterHost").toString());
|
---|
37 | _casterPortLineEdit = new QLineEdit(settings.value("casterPort").toString());
|
---|
38 |
|
---|
39 | QHBoxLayout* editLayout = new QHBoxLayout;
|
---|
40 | editLayout->addWidget(_casterHostLabel);
|
---|
41 | editLayout->addWidget(_casterHostLineEdit);
|
---|
42 | editLayout->addWidget(_casterPortLabel);
|
---|
43 | editLayout->addWidget(_casterPortLineEdit);
|
---|
44 | mainLayout->addLayout(editLayout);
|
---|
45 |
|
---|
46 | _table = new QTableWidget(this);
|
---|
47 | mainLayout->addWidget(_table);
|
---|
48 |
|
---|
49 | _buttonGet = new QPushButton(tr("Get Table"), this);
|
---|
50 | connect(_buttonGet, SIGNAL(clicked()), this, SLOT(slotGetTable()));
|
---|
51 |
|
---|
52 | _buttonCancel = new QPushButton(tr("Cancel"), this);
|
---|
53 | connect(_buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
|
---|
54 |
|
---|
55 | _buttonOK = new QPushButton(tr("OK"), this);
|
---|
56 | connect(_buttonOK, SIGNAL(clicked()), this, SLOT(accept()));
|
---|
57 |
|
---|
58 | QHBoxLayout* buttonLayout = new QHBoxLayout;
|
---|
59 | buttonLayout->addStretch(1);
|
---|
60 | buttonLayout->addWidget(_buttonGet);
|
---|
61 | buttonLayout->addWidget(_buttonCancel);
|
---|
62 | buttonLayout->addWidget(_buttonOK);
|
---|
63 |
|
---|
64 | mainLayout->addLayout(buttonLayout);
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Destructor
|
---|
68 | ////////////////////////////////////////////////////////////////////////////
|
---|
69 | bncTableDlg::~bncTableDlg() {
|
---|
70 | if (_table) {
|
---|
71 | for (int ir = 0; ir < _table->rowCount(); ir++) {
|
---|
72 | for (int ic = 0; ic < _table->columnCount(); ic++) {
|
---|
73 | delete _table->item(ir,ic);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | // Read Table from Caster
|
---|
80 | ////////////////////////////////////////////////////////////////////////////
|
---|
81 | void bncTableDlg::slotGetTable() {
|
---|
82 |
|
---|
83 | QString host = _casterHostLineEdit->text();
|
---|
84 | int port = _casterPortLineEdit->text().toInt();
|
---|
85 | QByteArray mountPoint = "/";
|
---|
86 | QByteArray user;
|
---|
87 | QByteArray password;
|
---|
88 |
|
---|
89 | // Send the Request
|
---|
90 | // ----------------
|
---|
91 | QTcpSocket* socket = bncGetThread::request(host, port, _proxyHost,
|
---|
92 | _proxyPort, mountPoint,
|
---|
93 | user, password);
|
---|
94 | if (!socket) {
|
---|
95 | return;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // Read Caster Response
|
---|
99 | // --------------------
|
---|
100 | QStringList allLines;
|
---|
101 | bool first = true;
|
---|
102 | while (true) {
|
---|
103 | if (socket->canReadLine()) {
|
---|
104 | QString line = socket->readLine();
|
---|
105 | if (first) {
|
---|
106 | first = false;
|
---|
107 | if (line.indexOf("SOURCETABLE 200 OK") != 0) {
|
---|
108 | QMessageBox::warning(0, "BNC", "Wrong Caster Response:\n" + line);
|
---|
109 | break;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | else {
|
---|
113 | if (line.indexOf("ENDSOURCETABLE") == 0) {
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | if (line.indexOf("STR") == 0) {
|
---|
117 | allLines.push_back(line);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | else {
|
---|
122 | const int timeOut = 10*1000;
|
---|
123 | socket->waitForReadyRead(timeOut);
|
---|
124 | if (socket->bytesAvailable() > 0) {
|
---|
125 | continue;
|
---|
126 | }
|
---|
127 | else {
|
---|
128 | QMessageBox::warning(0, "BNC", "Data Timeout");
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | delete socket;
|
---|
134 |
|
---|
135 | if (allLines.size() > 0) {
|
---|
136 | _table->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
---|
137 | _table->setSelectionBehavior(QAbstractItemView::SelectRows);
|
---|
138 |
|
---|
139 | QStringList hlp = allLines[0].split(";");
|
---|
140 | _table->setColumnCount(hlp.size()-1);
|
---|
141 | _table->setRowCount(allLines.size());
|
---|
142 |
|
---|
143 | QListIterator<QString> it(allLines);
|
---|
144 | int nRow = -1;
|
---|
145 | while (it.hasNext()) {
|
---|
146 | QStringList columns = it.next().split(";");
|
---|
147 | ++nRow;
|
---|
148 | for (int ic = 0; ic < columns.size()-1; ic++) {
|
---|
149 | _table->setItem(nRow, ic, new QTableWidgetItem(columns[ic+1]));
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | // Accept slot
|
---|
156 | ////////////////////////////////////////////////////////////////////////////
|
---|
157 | void bncTableDlg::accept() {
|
---|
158 |
|
---|
159 | QSettings settings;
|
---|
160 | settings.setValue("casterHost", _casterHostLineEdit->text());
|
---|
161 | settings.setValue("casterPort", _casterPortLineEdit->text());
|
---|
162 |
|
---|
163 | QStringList* mountPoints = new QStringList;
|
---|
164 |
|
---|
165 | if (_table) {
|
---|
166 | for (int ir = 0; ir < _table->rowCount(); ir++) {
|
---|
167 | QTableWidgetItem* item = _table->item(ir,0);
|
---|
168 | if (_table->isItemSelected(item)) {
|
---|
169 | QUrl url;
|
---|
170 | url.setHost(_casterHostLineEdit->text());
|
---|
171 | url.setPort(_casterPortLineEdit->text().toInt());
|
---|
172 | url.setPath(item->text());
|
---|
173 | mountPoints->push_back(url.toString());
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | emit newMountPoints(mountPoints);
|
---|
178 |
|
---|
179 | QDialog::accept();
|
---|
180 | }
|
---|