Rev 711 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
674 | KeyOz | 1 | /*************************************************************************** |
2 | * Copyright (C) 2009 by Manuel Schrape * |
||
3 | * manuel.schrape@gmx.de * |
||
4 | * * |
||
5 | * This program is free software; you can redistribute it and/or modify * |
||
6 | * it under the terms of the GNU General Public License as published by * |
||
7 | * the Free Software Foundation; either version 2 of the License. * |
||
8 | * * |
||
9 | * This program 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 this program; if not, write to the * |
||
16 | * Free Software Foundation, Inc., * |
||
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
18 | ***************************************************************************/ |
||
19 | |||
20 | #include <QMessageBox> |
||
21 | |||
22 | #include "dlg_Main.h" |
||
23 | |||
24 | // Konstruktor Main-Form |
||
25 | //////////////////////// |
||
26 | dlg_Main::dlg_Main() |
||
27 | { |
||
28 | setupUi(this); |
||
29 | |||
30 | o_Settings = new cSettings(); |
||
31 | |||
32 | o_Input = new Input(); |
||
33 | |||
34 | f_Terminal = new dlg_Terminal(this); |
||
35 | |||
36 | init_GUI(); |
||
37 | init_Connections(); |
||
38 | |||
39 | init_TCP(); |
||
40 | } |
||
41 | |||
42 | void dlg_Main::set_ARGV(char *Programm) |
||
43 | { |
||
44 | QString tmp = QString(Programm); |
||
45 | QMK_Dir = tmp.left(tmp.lastIndexOf("/")); |
||
46 | } |
||
47 | |||
48 | // Grafische Oberfläche initialisieren |
||
49 | ////////////////////////////////////// |
||
50 | void dlg_Main::init_GUI() |
||
51 | { |
||
52 | setWindowTitle(QA_NAME + " " + QA_VERSION); |
||
53 | |||
54 | resize(o_Settings->GUI.Size); |
||
55 | move(o_Settings->GUI.Point); |
||
56 | |||
57 | if (o_Settings->GUI.isMax) |
||
58 | { |
||
59 | showMaximized(); |
||
60 | } |
||
61 | |||
62 | // todo: In abhängigkeit von Settings |
||
63 | wg_IP->setVisible(false); |
||
64 | rb_Device->setChecked(true); |
||
65 | |||
66 | for (int z = 0; z < DEV_IP_MAX; z++) |
||
67 | { |
||
68 | Item[z][0] = new QListWidgetItem(); |
||
69 | Item[z][1] = new QListWidgetItem(); |
||
70 | } |
||
71 | |||
72 | for(int z = 0; z < o_Settings->CLIENT.TTY_MAX; z++) |
||
73 | { |
||
74 | if (cb_Device->findText(o_Settings->CLIENT.TTY_DEVICES[z]) == -1) |
||
75 | { |
||
76 | cb_Device->addItem(o_Settings->CLIENT.TTY_DEVICES[z]); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | cb_Device->setCurrentIndex(o_Settings->CLIENT.TTY_ID); |
||
81 | } |
||
82 | |||
83 | // Signale mit Slots verbinden |
||
84 | ////////////////////////////// |
||
85 | void dlg_Main::init_Connections() |
||
86 | { |
||
87 | // Connect-Button |
||
88 | connect(btn_Connect, SIGNAL(clicked()), this, SLOT(slot_btn_Connect())); |
||
89 | |||
90 | connect(btn_cScope, SIGNAL(clicked()), this, SLOT(slot_btn_cScope())); |
||
91 | connect(btn_cSettings, SIGNAL(clicked()), this, SLOT(slot_btn_cSettings())); |
||
92 | connect(btn_cMaps, SIGNAL(clicked()), this, SLOT(slot_btn_cMaps())); |
||
93 | connect(btn_cVoice, SIGNAL(clicked()), this, SLOT(slot_btn_cVoice())); |
||
94 | connect(btn_cLogger, SIGNAL(clicked()), this, SLOT(slot_btn_cLogger())); |
||
95 | |||
96 | connect(btn_Terminal, SIGNAL(clicked()), this, SLOT(slot_btn_Terminal())); |
||
97 | |||
98 | // About QMK-Kernel & About-QT Dialog einfügen |
||
99 | connect(ac_About, SIGNAL(triggered()), this, SLOT(slot_ac_About())); |
||
100 | connect(ac_Server, SIGNAL(triggered()), this, SLOT(slot_ac_Server())); |
||
101 | menu_Help->addAction(trUtf8("Über &Qt"), qApp, SLOT(aboutQt())); |
||
102 | } |
||
103 | |||
104 | // IP-Ports initialisiern |
||
105 | ///////////////////////// |
||
106 | // todo: Port und Anzahl aus Settings |
||
107 | bool dlg_Main::init_TCP() |
||
108 | { |
||
109 | Connect_Count = 0; |
||
110 | Connection[0].TCP = false; |
||
111 | Connection[0].UDP = false; |
||
112 | |||
113 | for (int a = 0; a < DEV_IP_MAX; a++) |
||
114 | { |
||
115 | Connection[a].Open = false; |
||
116 | } |
||
117 | |||
118 | TCP_Server = new QTcpServer(this); |
||
119 | |||
120 | if (TCP_Server->listen(QHostAddress::Any, qint16(o_Settings->SERVER.TCP_PORT))) |
||
121 | { |
||
122 | connect(TCP_Server, SIGNAL(newConnection()), this, SLOT(slot_TCP_NewConnection())); |
||
123 | } |
||
124 | |||
125 | return true; |
||
126 | } |
||
127 | |||
128 | // Eingangsdaten verarbeiten |
||
129 | //////////////////////////// |
||
130 | void dlg_Main::parse_Input_Data(QString t_Data) |
||
131 | { |
||
132 | unsigned char OutData[150]; |
||
133 | char *InData = t_Data.toLatin1().data(); |
||
134 | |||
135 | if (HandlerMK::Decode_64(InData, t_Data.length(), OutData) != 0) |
||
136 | { |
||
137 | switch(InData[2]) |
||
138 | { |
||
139 | case 'V' : // Versions-Info |
||
140 | { |
||
141 | o_Input->stop_Resend(DATA_VERSION); |
||
142 | VersionInfo = HandlerMK::parse_Version(OutData, InData[1] - 'a'); |
||
143 | lb_Info->setText(tr("Verbunden mit ") + VersionInfo.Hardware + " " + VersionInfo.Version + trUtf8(" über ") + s_Input.Main + "."); |
||
144 | |||
145 | if (VersionInfo.ID == ADDRESS_FC) |
||
146 | { |
||
147 | rb_FC->setChecked(true); |
||
148 | } |
||
149 | if (VersionInfo.ID == ADDRESS_NC) |
||
150 | { |
||
151 | rb_NC->setChecked(true); |
||
152 | } |
||
153 | if (VersionInfo.ID == ADDRESS_MK3MAG) |
||
154 | { |
||
155 | rb_MK3MAG->setChecked(true); |
||
156 | } |
||
157 | } |
||
158 | break; |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | |||
163 | void dlg_Main::route_Input_Data(QString t_Data) |
||
164 | { |
||
165 | // An alle offenen TCP-Sockets senden. |
||
166 | for (int a = 1; a < DEV_IP_MAX; a++) |
||
167 | { |
||
168 | if (Connection[a].Open == true) |
||
169 | { |
||
170 | if (Connection[a].TCP == true) |
||
171 | { |
||
172 | send_TCP(o_TCP[a], t_Data + "\r"); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | void dlg_Main::parse_TCP_Data(QString t_Data, int t_ID) |
||
179 | { |
||
180 | QStringList Data; |
||
181 | Data = t_Data.split(":"); |
||
182 | |||
183 | if (Data.count() > 1) |
||
184 | { |
||
185 | int CMD = Data[2].toInt(); |
||
186 | |||
187 | switch(CMD) |
||
188 | { |
||
189 | case 101 : |
||
190 | { |
||
191 | Item[t_ID][1]->setText(Data[3]); |
||
192 | if (Connection[t_ID].Auth == true) |
||
193 | Item[t_ID][1]->setText("* " + Data[3]); |
||
194 | } |
||
195 | break; |
||
196 | case 105 : |
||
197 | { |
||
198 | if (Connection[t_ID].Auth == false) |
||
199 | { |
||
200 | if ((o_Settings->SERVER.Password != "") && (Data[3] == o_Settings->SERVER.Password)) |
||
201 | { |
||
202 | send_TCP(o_TCP[t_ID],HandlerIP::make_Frame(ID_COMMUNICATOR, 505, "OK")); |
||
203 | |||
204 | Connection[t_ID].Auth = true; |
||
205 | Item[t_ID][1]->setText("* " + Item[t_ID][1]->text()); |
||
206 | } |
||
207 | else |
||
208 | { |
||
209 | send_TCP(o_TCP[t_ID],HandlerIP::make_Frame(ID_COMMUNICATOR, 505, "NO")); |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | break; |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | // Freies IP-Socket ermitteln |
||
219 | int dlg_Main::get_FreeSocket() |
||
220 | { |
||
221 | for (int a = 1; a < DEV_IP_MAX; a++) |
||
222 | { |
||
223 | if (Connection[a].Open == false) |
||
224 | return a; |
||
225 | } |
||
226 | return 0; |
||
227 | } |
||
228 | |||
229 | // Daten auf TCP Senden |
||
230 | void dlg_Main::send_TCP(QTcpSocket *Socket, QString Data) |
||
231 | { |
||
232 | QByteArray SendText = Data.toAscii(); |
||
233 | |||
234 | Socket->write(SendText + "\n"); |
||
235 | } |
||
236 | |||
237 | ///////////ettings |
||
238 | // Slots // |
||
239 | /////////// |
||
240 | |||
241 | void dlg_Main::slot_btn_cScope() |
||
242 | { |
||
243 | QString Programm = QMK_Dir + "/QMK-Scope"; |
||
244 | |||
245 | QStringList Argumente; |
||
246 | |||
247 | o_cScope = new QProcess(); |
||
248 | |||
249 | Argumente << ""; |
||
250 | |||
251 | o_cScope->start(Programm, Argumente); // Programmaufruf |
||
252 | } |
||
253 | |||
254 | void dlg_Main::slot_btn_cSettings() |
||
255 | { |
||
256 | QString Programm = QMK_Dir + "/QMK-Settings"; |
||
257 | |||
258 | QStringList Argumente; |
||
259 | |||
260 | o_cSettings = new QProcess(); |
||
261 | |||
262 | Argumente << ""; |
||
263 | |||
264 | o_cSettings->start(Programm, Argumente); // Programmaufruf |
||
265 | } |
||
266 | |||
267 | void dlg_Main::slot_btn_cMaps() |
||
268 | { |
||
269 | QString Programm = QMK_Dir + "/QMK-Maps"; |
||
270 | |||
271 | QStringList Argumente; |
||
272 | |||
273 | o_cMaps = new QProcess(); |
||
274 | |||
275 | Argumente << ""; |
||
276 | |||
277 | o_cMaps->start(Programm, Argumente); // Programmaufruf |
||
278 | } |
||
279 | |||
280 | void dlg_Main::slot_btn_cVoice() |
||
281 | { |
||
282 | QString Programm = QMK_Dir + "/QMK-Voice"; |
||
283 | |||
284 | QStringList Argumente; |
||
285 | |||
286 | o_cVoice = new QProcess(); |
||
287 | |||
288 | Argumente << ""; |
||
289 | |||
290 | o_cVoice->start(Programm, Argumente); // Programmaufruf |
||
291 | } |
||
292 | |||
293 | void dlg_Main::slot_btn_cLogger() |
||
294 | { |
||
295 | QString Programm = QMK_Dir + "/QMK-Logger"; |
||
296 | |||
297 | QStringList Argumente; |
||
298 | |||
299 | o_cLogger = new QProcess(); |
||
300 | |||
301 | Argumente << ""; |
||
302 | |||
303 | o_cLogger->start(Programm, Argumente); // Programmaufruf |
||
304 | } |
||
305 | |||
306 | void dlg_Main::slot_btn_Terminal() |
||
307 | { |
||
308 | if (!f_Terminal->isVisible()) |
||
309 | { |
||
310 | f_Terminal->show(); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | // Verbindung herstellen |
||
315 | void dlg_Main::slot_btn_Connect() |
||
316 | { |
||
317 | if (!o_Input->IsOpen()) |
||
318 | { |
||
319 | if (rb_Device->isChecked()) |
||
320 | { |
||
321 | if (cb_Device->findText(cb_Device->currentText()) == -1) |
||
322 | { |
||
323 | cb_Device->addItem(cb_Device->currentText()); |
||
324 | cb_Device->setCurrentIndex(cb_Device->findText(cb_Device->currentText())); |
||
325 | } |
||
326 | |||
327 | s_Input.Main = cb_Device->currentText(); |
||
328 | |||
329 | o_Input = new Input_TTY(); |
||
330 | o_Input->Init(); |
||
331 | |||
332 | if (o_Input->Open(s_Input) == true) |
||
333 | { |
||
334 | connect(o_Input, SIGNAL(sig_NewData(QString)), this, SLOT(slot_Input_Data(QString))); |
||
335 | |||
336 | o_Input->send_Data(HandlerMK::make_Frame('v', 0, c_Data, 0).toLatin1().data(), DATA_VERSION); |
||
337 | |||
338 | btn_Connect->setText(tr("Trennen")); |
||
339 | cb_Device->setEnabled(false); |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 | else |
||
344 | { |
||
345 | if (rb_Device->isChecked()) |
||
346 | { |
||
347 | o_Input->Close(); |
||
348 | btn_Connect->setText(tr("Verbinden")); |
||
349 | cb_Device->setEnabled(true); |
||
350 | } |
||
351 | } |
||
352 | } |
||
353 | |||
354 | // About-Dialog |
||
355 | void dlg_Main::slot_ac_About() |
||
356 | { |
||
357 | QMessageBox::about(this, trUtf8(("Über ")) + QA_NAME, QA_ABOUT); |
||
358 | } |
||
359 | |||
360 | void dlg_Main::slot_ac_Server() |
||
361 | { |
||
362 | dlg_Preferences *f_Preferences = new dlg_Preferences(this); |
||
363 | |||
364 | f_Preferences->set_Settings(o_Settings); |
||
365 | |||
366 | if (f_Preferences->exec()==QDialog::Accepted) |
||
367 | { |
||
368 | o_Settings = f_Preferences->get_Settings(); |
||
369 | o_Settings->write_Settings(); |
||
370 | } |
||
371 | } |
||
372 | |||
373 | // Neue Daten empfangen. |
||
374 | void dlg_Main::show_Terminal(int t_Typ, QString t_Data) |
||
375 | { |
||
376 | if (f_Terminal->isVisible()) |
||
377 | { |
||
378 | f_Terminal->show_Data(t_Typ, t_Data); |
||
379 | } |
||
380 | } |
||
381 | |||
382 | void dlg_Main::slot_Input_Data(QString t_Data) |
||
383 | { |
||
384 | if (HandlerMK::Check_CRC(t_Data.toLatin1().data(), t_Data.length())) |
||
385 | { |
||
386 | show_Terminal(1, t_Data); |
||
387 | parse_Input_Data(t_Data); |
||
388 | route_Input_Data(t_Data); |
||
389 | } |
||
390 | else |
||
391 | { |
||
392 | show_Terminal(2, t_Data); |
||
393 | } |
||
394 | } |
||
395 | |||
396 | ////////////// |
||
397 | // IP-Slots // |
||
398 | ////////////// |
||
399 | |||
400 | void dlg_Main::slot_TCP_NewConnection() |
||
401 | { |
||
402 | if (!o_Input->IsOpen()) |
||
403 | slot_btn_Connect(); |
||
404 | |||
405 | int ID = get_FreeSocket(); |
||
406 | |||
407 | if (ID != 0) |
||
408 | { |
||
409 | Connect_Count++; |
||
410 | // Leeres Connection erzeugen |
||
411 | Connection[ID] = Connection[0]; |
||
412 | Connection[ID].Open = true; |
||
413 | Connection[ID].TCP = true; |
||
414 | Connection[ID].Auth = false; |
||
415 | |||
416 | o_TCP[ID] = TCP_Server->nextPendingConnection(); |
||
417 | o_TCP[ID]->setProperty("ID", ID); |
||
418 | |||
419 | send_TCP(o_TCP[ID],HandlerIP::make_Frame(ID_COMMUNICATOR, 501, QA_NAME + " " + QA_VERSION)); |
||
420 | |||
421 | connect(o_TCP[ID], SIGNAL(disconnected()),o_TCP[ID], SLOT(deleteLater())); |
||
422 | connect(o_TCP[ID], SIGNAL(disconnected()),this, SLOT(slot_TCP_Disconnect())); |
||
423 | connect(o_TCP[ID], SIGNAL(readyRead()),this, SLOT(slot_TCP_Read())); |
||
424 | |||
425 | Item[ID][0]->setText(o_TCP[ID]->peerAddress().toString()); |
||
426 | lw_ClientsIP->addItem(Item[ID][0]); |
||
427 | Item[ID][1]->setText("n/a"); |
||
428 | lw_Clients->addItem(Item[ID][1]); |
||
429 | |||
430 | if (Item[ID][0]->text() == "127.0.0.1") |
||
431 | Connection[ID].Auth = true; |
||
432 | |||
433 | if (Connection[ID].Auth) |
||
434 | Item[ID][1]->setText("* n/a"); |
||
435 | |||
436 | } |
||
437 | else |
||
438 | { // Server voll |
||
439 | o_TCP[ID] = TCP_Server->nextPendingConnection(); |
||
440 | o_TCP[ID]->setProperty("ID", ID); |
||
441 | |||
442 | // send_TCP(o_TCP[ID], QString("SERVER FULL\n")); |
||
443 | o_TCP[ID]->disconnectFromHost(); |
||
444 | } |
||
445 | } |
||
446 | |||
447 | void dlg_Main::slot_TCP_Read() |
||
448 | { |
||
449 | QTcpSocket *Socket = (QTcpSocket*)sender(); |
||
450 | |||
451 | // if (Socket->canReadLine()) |
||
452 | { |
||
453 | int ID = Socket->property("ID").toUInt(); |
||
454 | ID = ID; |
||
455 | |||
456 | QString t_Data = QString(Socket->readLine((Socket->bytesAvailable()))); |
||
457 | |||
458 | QStringList s_Data = t_Data.split('\r'); |
||
459 | |||
460 | for (int z = 0; z < s_Data.length() - 1; z++) |
||
461 | { |
||
462 | if (s_Data[z][0] == '$') |
||
463 | { |
||
464 | // qDebug(QString("TCP: " + s_Data[z]).toLatin1().data()); |
||
465 | parse_TCP_Data(s_Data[z], ID); |
||
466 | } |
||
467 | else |
||
468 | { // Daten vom Client an den MK. todo: Nur erlauben wenn autemtifiziert oder Localhost |
||
469 | if ((Connection[ID].Auth == true)) |
||
470 | { |
||
471 | show_Terminal(3, s_Data[z]); |
||
472 | o_Input->send_Data(s_Data[z]); |
||
473 | } |
||
474 | } |
||
475 | } |
||
476 | } |
||
477 | } |
||
478 | |||
479 | void dlg_Main::slot_TCP_Disconnect() |
||
480 | { |
||
481 | QTcpSocket *Socket = (QTcpSocket*)sender(); |
||
482 | |||
483 | int ID = Socket->property("ID").toUInt(); |
||
484 | |||
485 | Item[ID][1]->setText("Disconected"); |
||
486 | |||
487 | Connection[ID] = Connection[0]; |
||
488 | Connection[ID].Open = false; |
||
489 | |||
490 | Connect_Count--; |
||
491 | |||
492 | Socket->setProperty("ID", 0); |
||
493 | |||
494 | if (Connect_Count == 0) |
||
495 | slot_btn_Connect(); |
||
496 | } |
||
497 | |||
498 | |||
499 | // Programm Ende |
||
500 | //////////////// |
||
501 | dlg_Main::~dlg_Main() |
||
502 | { |
||
503 | o_Settings->GUI.isMax = isMaximized(); |
||
504 | o_Settings->GUI.Size = size(); |
||
505 | o_Settings->GUI.Point = pos(); |
||
506 | |||
507 | o_Settings->CLIENT.TTY_MAX = cb_Device->count(); |
||
508 | o_Settings->CLIENT.TTY_ID = cb_Device->currentIndex(); |
||
509 | |||
510 | for (int z = 0; z < cb_Device->count(); z++) |
||
511 | { |
||
512 | if (z < 10) |
||
513 | { |
||
514 | o_Settings->CLIENT.TTY_DEVICES[z] = cb_Device->itemText(z); |
||
515 | } |
||
516 | } |
||
517 | |||
518 | |||
519 | o_Settings->write_Settings(); |
||
520 | |||
521 | // qDebug("Ende."); |
||
522 | } |