forked from Eivs/gascop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwin.cpp
executable file
·2239 lines (2128 loc) · 87 KB
/
mainwin.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Gascop
*
* Copyright (C) 2011-2014 Clive Cooper.
*
* Gascop is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* Gascop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Author: Clive Cooper,
*
*/
#include <QtGui>
#include <QApplication>
#include <QDebug>
#include <QTcpSocket>
#include "mainwin.h"
#define MainWin_DEBUG
const bool DEBUG = true;
const QString VERSION = "2013.11.08-1";
#define RICPOS(c) (c % 8)
#define QSn QString::number
using namespace std;
Mix_Chunk * SC_sound;
bool isPlayingWav;
void channelDone(int channel);
/* There is no limit to transmission length but bear in mind a 3 minute transmission requires about 13MB of memory to store the wave data
*
*
*/
MainWin::MainWin( QWidget * parent, Qt::WFlags f)
: QMainWindow(parent, f)
{
setupUi(this);
readSettings(); // Get any user settings at start.
running = false; // This is used in the show event to make sure we only run the code in the show event just once.
formleft = x(); // Used to keep the main window where the user wants it.
formtop = y();
userPath = QDir::homePath(); // userPath is the path to the users home directory.
txLength = new QTimer(this);
watchdog = new QTimer(this);
watchdog->setInterval(2000);
verLabel->setText("Ver: " + VERSION);
userDir = QDir::homePath() + "/.gascop";
gascopdb = new QStringList();
pagerIDdb = new QStringList();
QDir md;
md.mkdir(userDir);
if(!QFile::exists(userDir + "/prev-pages.txt"))
{
QFile::copy(":/files/prev-pages.txt", userDir + "/prev-pages.txt");
QFile::setPermissions(userDir + "/prev-pages.txt", QFile::ReadUser | QFile::WriteUser | QFile::ReadGroup | QFile::ReadOther);
}
if(!QFile::exists(userDir + "/gascop.db"))
{
QFile::copy(":/files/gascop.db", userDir + "/gascop.db");
QFile::setPermissions(userDir + "/gascop.db", QFile::ReadUser | QFile::WriteUser | QFile::ReadGroup | QFile::ReadOther);
}
if(!QFile::exists(userDir + "/pagerid.db"))
{
QFile::copy(":/files/pagerid.db", userDir + "/pagerid.db");
QFile::setPermissions(userDir + "/pagerid.db", QFile::ReadUser | QFile::WriteUser | QFile::ReadGroup | QFile::ReadOther);
}
pocsagBuffer = NULL;
connect(txLength, SIGNAL(timeout()), this, SLOT(txLengthTimeout()));
connect(watchdog, SIGNAL(timeout()), this, SLOT(watchdogTimeout()));
connect(pbSend, SIGNAL(clicked()), this, SLOT(sendSinglePage()));
connect(twMain, SIGNAL(currentChanged(int)), this, SLOT(currentTabChanged(int)));
connect(lwPrevPages, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(prevPagesDoubleClicked(QListWidgetItem *)));
connect(tbTune, SIGNAL(clicked()), this, SLOT(tuneupTransmit()));
connect(tbTuneStop, SIGNAL(clicked()), this, SLOT(tuneupStop()));
connect(pbStart, SIGNAL(clicked()), this, SLOT(ricSearch()));
connect(pbAbort, SIGNAL(clicked()), this, SLOT(ricSearchAbort()));
connect(pbSave, SIGNAL(clicked()), this, SLOT(logSave()));
connect(pbReload, SIGNAL(clicked()), this, SLOT(logReload()));
connect(tbNumeric, SIGNAL(clicked()), this, SLOT(numericClicked()));
connect(tbResetTime, SIGNAL(clicked()), this, SLOT(resetTimeClicked()));
connect(pocsagLevel, SIGNAL(valueChanged(int)), this, SLOT(dataLevelChanged(int)));
connect(tbGetPagerID, SIGNAL(clicked()), this, SLOT(getPagerIDClicked()));
connect(teMessage, SIGNAL(textChanged()), this, SLOT(msgTextChanged()));
connect(cbPeriod, SIGNAL(currentIndexChanged(QString)), this, SLOT(periodIndexChanged(QString)));
connect(lwPagerIDs, SIGNAL(currentRowChanged(int)), this, SLOT(pagerIDsRowChanged(int)));
connect(lwPages, SIGNAL(currentRowChanged(int)), this, SLOT(pagesRowChanged(int)));
connect(pbApply, SIGNAL(clicked()), this, SLOT(applySettings()));
connect(pbKeyTx, SIGNAL(clicked()), this, SLOT(keyTxClicked()));
// Below are all to do with detecting a change in the PagerIDs info
connect(leRic_2, SIGNAL(textChanged(QString)), this, SLOT(pagerIDstextChanged(QString)));
connect(leName_2, SIGNAL(textChanged(QString)), this, SLOT(pagerIDstextChanged(QString)));
connect(leFrequency, SIGNAL(textChanged(QString)), this, SLOT(pagerIDstextChanged(QString)));
connect(sbTx_2, SIGNAL(valueChanged(int)), this, SLOT(pagerIDsvalChanged(int)));
connect(sbMaxMsgLen, SIGNAL(valueChanged(int)), this, SLOT(pagerIDsvalChanged(int)));
connect(tbFuncTone1_2, SIGNAL(clicked()), this, SLOT(pagerIDsChange()));
connect(tbFuncTone2_2, SIGNAL(clicked()), this, SLOT(pagerIDsChange()));
connect(tbFuncNumeric_2, SIGNAL(clicked()), this, SLOT(pagerIDsChange()));
connect(tbFuncAlpha_2, SIGNAL(clicked()), this, SLOT(pagerIDsChange()));
connect(tbAlpha_2, SIGNAL(clicked()), this, SLOT(pagerIDsChange()));
connect(tbNumeric_2, SIGNAL(clicked()), this, SLOT(pagerIDsChange()));
connect(tb512_2, SIGNAL(clicked()), this, SLOT(pagerIDsChange()));
connect(tb1200_2, SIGNAL(clicked()), this, SLOT(pagerIDsChange()));
connect(tb2400_2, SIGNAL(clicked()), this, SLOT(pagerIDsChange()));
//=================================================================
connect(pbAddEntry, SIGNAL(clicked()), this, SLOT(pagerIDsAddEntry()));
connect(pbUpdateEntry, SIGNAL(clicked()), this, SLOT(pagerIDsUpdateEntry()));
connect(pbClearAll, SIGNAL(clicked()), this, SLOT(pagerIDsClearAll()));
connect(pbDeleteEntry, SIGNAL(clicked()), this, SLOT(pagerIDsDeleteEntry()));
connect(pbDeleteEntry_3, SIGNAL(clicked()), this, SLOT(pagesDeleteEntry()));
teMessage->installEventFilter(this);
lwPrevPages->installEventFilter(this);
leRic->installEventFilter(this);
tbGetPagerID->installEventFilter(this);
pbSend->installEventFilter(this);
}
// This allows me to do seconds, milliseconds and microseconds sleeping : gotsleep::msleep(100); for 100 milliseconds
class gotoSleep : public QThread
{
public:
static void sleep(unsigned long secs)
{
QThread::sleep(secs);
}
static void msleep(unsigned long msecs)
{
QThread::msleep(msecs);
}
static void usleep(unsigned long usecs)
{
QThread::usleep(usecs);
}
};
void MainWin::showEvent(QShowEvent *) // The main window is drawn
{
if(!running)
{
running = true; // All show events after the first one will do nothing because this will be true.
formleft = x(); // Get the position of the main window, if the window is never moved then these values do not get updated.
formtop = y();
readSettings();
preambleCW = 0xaaaaaaaa; // Fixed value preamble
dataLevelChanged(pocsagLevel->value());
dtSendTime->setDateTime(QDateTime::currentDateTime());
applySettings();
currentBaudRate = 1200;
currentTx = 1;
tbTuneStop->setEnabled(false);
singlePage = false;
isPlayingWav = false;
isRicSearching =false;
testMode = false; // For testing only, set to true and sent pages do not get deleted from the database
logReload(); // Initially load the log file using the reload function
openDB();
loadPrevPages();
tbGetPagerID->setFocus(); // set the focus on the RIC entry edit
pagesDbLastModified = getLastModified(userDir + "/gascop.db");
isAutoPagingActive = true;
txComplete = true;
displayMessages("Gascop has started.");
watchdogTimeout();
}
}
void MainWin::moveEvent(QMoveEvent *) // The main window is being moved.
{
formleft = x(); // The position of the main window is kept so the next time the application is run it can be put back to where the user left it.
formtop = y();
}
void MainWin::closeEvent(QCloseEvent *) // The main window is closing.
{
watchdog->stop();
displayMessages("Gascop has closed.");
writeSettings(); // Save the user settings before closing.
}
// Open the databases
void MainWin::openDB()
{
if(QFile::exists(userDir + "/gascop.db"))
{
readTextFile(userDir + "/gascop.db", gascopdb);
readTextFile(userDir + "/pagerid.db", pagerIDdb);
populatePagerIDsEditor();
if(lwPagerIDs->count() > 0)
{
lwPagerIDs->setCurrentRow(0);
pagerIDsListToEditor(0);
pbDeleteEntry->setEnabled(true);
pbClearAll->setEnabled(true);
pbUpdateEntry->setEnabled(false);
pbAddEntry->setEnabled(true);
}
populatePagerMsgsEditor();
if(lwPages->count() > 0)
{
lwPages->setCurrentRow(0);
pagesListToEditor(0);
}
}
}
// Add a new entry to the Pager IDs database
void MainWin::pagerIDsAddEntry()
{
int cr = lwPagerIDs->currentRow();
pagerIDsEditorToStruct();
writeToPagerIDDatabase(-1);
populatePagerIDsEditor();
if((cr >= 0) && (cr < lwPagerIDs->count()))
{
lwPagerIDs->setCurrentRow(cr);
lwPagerIDs->setFocus();
}
}
// Update an existing entry in the Pager IDs database
void MainWin::pagerIDsUpdateEntry()
{
int cr = lwPagerIDs->currentRow();
pagerIDsEditorToStruct();
writeToPagerIDDatabase(lwPagerIDs->currentRow());
populatePagerIDsEditor();
if((cr >= 0) && (cr < lwPagerIDs->count()))
{
lwPagerIDs->setCurrentRow(cr);
lwPagerIDs->setFocus();
}
}
// Delete an entry from the pager ID database file
void MainWin::pagerIDsDeleteEntry()
{
int cr = lwPagerIDs->currentRow();
if((cr >= 0) && (cr < lwPagerIDs->count()))
{
pagerIDdb->takeAt(lwPagerIDs->currentRow());
writeTextFile(userDir + "/pagerid.db", pagerIDdb);
populatePagerIDsEditor();
if((cr >= 0) && (cr < lwPagerIDs->count()))
lwPagerIDs->setCurrentRow(cr);
else if(lwPagerIDs->count())
lwPagerIDs->setCurrentRow(lwPagerIDs->count() - 1);
lwPagerIDs->setFocus();
}
}
void MainWin::pagesDeleteEntry()
{
int cr = lwPages->currentRow();
if((cr >= 0) && (cr < lwPages->count()))
{
leRic_3->clear();
teMessage_3->clear();
gascopdb->takeAt(lwPages->currentRow());
writeTextFile(userDir + "/gascop.db", gascopdb);
populatePagerMsgsEditor();
if((cr >= 0) && (cr < lwPages->count()))
lwPages->setCurrentRow(cr);
else if(lwPages->count())
lwPages->setCurrentRow(lwPages->count() - 1);
lwPages->setFocus();
}
}
// Change made to an item in the Pager IDs editor
void MainWin::pagerIDstextChanged(QString)
{
pbUpdateEntry->setEnabled(true);
}
// Change made to an item in the Pager IDs editor
void MainWin::pagerIDsvalChanged(int)
{
pbUpdateEntry->setEnabled(true);
}
// Change made to an item in the Pager IDs editor
void MainWin::pagerIDsChange()
{
pbUpdateEntry->setEnabled(true);
}
// This watchdog timer is called every 5 seconds and checks if the pages database modified time has changed.
void MainWin::watchdogTimeout()
{
watchdog->stop(); // Stop the timer
if(dtSendTime->dateTime().toTime_t() < (timeToInt() + 50))
dtSendTime->setDateTime(QDateTime::currentDateTime()); // Keep the date/time on pager entry correct if it is within 8 seconds
checkForPagesToSend();
}
// Check for pages to send
void MainWin::checkForPagesToSend()
{
uint timeNow = timeToInt();
bool lm = (pagesDbLastModified != getLastModified(userDir + "/gascop.db")); // get a flag showing if the database has been modified
int index = 0;
if(lm)
setFileLock("gascop.db"); // If the database has been modified since last use then open it again now
while(index < dbPages.count())
{
if((dbPages.at(index).flag == 0) && (dbPages.at(index).sendTime <= timeNow) &&
(dbPages.at(index).tx == txOrder[txRotate]) && (dbPages.at(index).baud == baudOrder[baudRotate]))
{
dbPages[index].flag = 1; // Set the flag to 1 to show we are processing this page
gascopdb->replace(index, "1" + gascopdb->at(index).mid(1)); // Set the flag to 1 directly in the database file
txSendPage.funcBits = dbPages.at(index).funcBits; // Get the data we will need to be able to create a pager message
txSendPage.numeric = dbPages.at(index).numeric;
txSendPage.ric = QSn(dbPages.at(index).ric);
txSendPage.msg = dbPages.at(index).msg;
txSendPage.ricPos = RICPOS(dbPages.at(index).ric);
txSendPages.append(txSendPage);
}
index++;
}
currentBaudRate = baudOrder[baudRotate];
currentTx = txOrder[txRotate];
baudRotate++;
if(baudRotate > 5) // Go through all the baud rates
{
baudRotate = 0; // Once all the baud rates are checked then check the next tx
txRotate++;
if(txRotate > 7) //
txRotate = 0;
}
if(txSendPages.count()) // If there are pages to send...
{
displayMessages("Sending " + QSn(txSendPages.count()) + " pages @ " + QSn(currentBaudRate) + " baud");
writeTextFile(userDir + "/gascop.db", gascopdb); // Write the updated db file back out
QFile::remove(userDir + "/gascop.db.lck"); // Remove the lock file
txComplete = false;
isPlayingWav = true;
stackPages(); // Go stack and send the pages
return;
}
if(lm)
QFile::remove(userDir + "/gascop.db.lck"); // Remove the lock file if we opened it at the start of this function
watchdog->start();
}
// Retruns the time a file was last modified as a unix time value
uint MainWin::getLastModified(QString file)
{
QFileInfo dbInfo(file);
return(dbInfo.lastModified().toTime_t());
}
// Supplied with just the filename (like... gascop.db) this will create a lock file, if there is a lock file it waits 3 seconds and deletes it
void MainWin::setFileLock(QString filename)
{
uint t = timeToInt() + 2; // Wait for up to 2 seconds for the lock to expire
while(QFile::exists(userDir + "/" + filename + ".lck")) // Loop while we wait
{
gotoSleep::msleep(200); // This while loop can use a lot of cpu so we sleep for a good part of the time
QCoreApplication::processEvents(QEventLoop::AllEvents); // Keep the GUI responsive
if(timeToInt() >= t) // Must be a stale lock to get into here: the 2 seconds is up
{
QFile::remove(userDir + "/" + filename + ".lck"); // Delete the lock
break; // Break out of the loop
}
}
QFile::copy(":/files/" + filename + ".lck", userDir + "/" + filename + ".lck"); // write a new lock file (Remember it expires in 2 seconds!)
if(filename == "gascop.db")
{
readTextFile(userDir + "/gascop.db", gascopdb); // The file is read and MUST be written within the next 2 seconds
pagesDbLastModified = getLastModified(userDir + "/gascop.db");
getPagesElements();
populatePagerMsgsEditor();
}
else
{
readTextFile(userDir + "/pagerid.db", pagerIDdb); // The file is read and MUST be written withing 2 seconds
getPagerIDElements();
}
}
// takes the info in a dbPage and turns it into a line entry for the pages database
void MainWin::writeToPagesDatabase()
{
int cr = lwPages->currentRow();
QString dbEntry;
QString spare = "0000";
QString schedule = QSn(dbPage.schedule);
while(schedule.length() < 3)
schedule = "0" + schedule;
QString howMany = QSn(dbPage.howMany);
while(howMany.length() < 2)
howMany = "0" + howMany;
QString baud = QSn(dbPage.baud);
while(baud.length() < 4)
baud = "0" + baud;
QString ric = QSn(dbPage.ric);
while(ric.length() < 7)
ric = "0" + ric;
dbEntry = QSn(dbPage.flag) + QSn(dbPage.sendTime) + spare + schedule + howMany + QSn(dbPage.tx) + baud + QSn(dbPage.numeric) +
QSn(dbPage.funcBits) + ric + dbPage.msg;
watchdog->stop();
while(watchdog->isActive())
{
gotoSleep::msleep(100);
QCoreApplication::processEvents(QEventLoop::AllEvents); // Keep the GUI responsive
}
setFileLock("gascop.db"); // We now have 2 seconds before we must give up the lock
gascopdb->append(dbEntry);
writeTextFile(userDir + "/gascop.db", gascopdb);
QFile::remove(userDir + "/gascop.db.lck"); // Remove the lock file
populatePagerMsgsEditor();
if((cr >= 0) && (cr < lwPages->count()))
lwPages->setCurrentRow(cr);
watchdog->start();
}
void MainWin::pagerIDsClearAll()
{
int cr = lwPagerIDs->currentRow();
leRic_2->clear();
leFrequency->clear();
leName_2->clear();
sbTx_2->setValue(1);
if(cr >= 0)
lwPagerIDs->setCurrentRow(cr);
}
// takes the info in a dbPage and turns it into a line entry for the pages database
void MainWin::writeToPagerIDDatabase(int index)
{
QString dbEntry;
QString fx = dbPagerID.fx;
while(fx.length() < 9)
fx = fx + "0";
QString baud = QSn(dbPagerID.baud);
while(baud.length() < 4)
baud = "0" + baud;
QString ric = QSn(dbPagerID.ric);
while(ric.length() < 7)
ric = "0" + ric;
QString msgLen = QSn(dbPagerID.msgLen);
while(msgLen.length() < 3)
msgLen = "0" + msgLen;
// 15450893012001153.17500255BT EasyReach 77C : This is a typical entry line in the database file
dbEntry = ric + QSn(dbPagerID.funcBits) + QSn(dbPagerID.numeric) + baud + QSn(dbPagerID.tx) + fx + msgLen + dbPagerID.name;
if(index < 0)
pagerIDdb->append(dbEntry);
else
pagerIDdb->replace(index, dbEntry);
writeTextFile(userDir + "/pagerid.db", pagerIDdb);
}
// takes pagerID info from the Editor and places it in the QVector structure
void MainWin::pagerIDsEditorToStruct()
{
dbPagerID.ric = leRic_2->text().toInt();
dbPagerID.funcBits = getFunctionBits2();
dbPagerID.numeric = 0;
if(tbNumeric_2->isChecked())
dbPagerID.numeric = 1;
dbPagerID.baud = getBaudRate2();
dbPagerID.tx = sbTx_2->value();
dbPagerID.fx = leFrequency->text();
dbPagerID.msgLen = sbMaxMsgLen->value();
dbPagerID.name = leName_2->text();
}
// Returns the elements of the entries in the pager ID database
void MainWin::getPagerIDElements()
{
dbPagerIDs.clear();
int index = 0;
while(index < pagerIDdb->count())
{
dbPagerID.ric = pagerIDdb->at(index).left(7).toInt();
dbPagerID.funcBits = pagerIDdb->at(index).mid(7, 1).toInt();
dbPagerID.numeric = pagerIDdb->at(index).mid(8, 1).toInt();
dbPagerID.baud = pagerIDdb->at(index).mid(9, 4).toInt();
dbPagerID.tx = pagerIDdb->at(index).mid(13, 1).toInt();
dbPagerID.fx = pagerIDdb->at(index).mid(14, 9);
dbPagerID.msgLen = pagerIDdb->at(index).mid(23, 3).toInt();
dbPagerID.name = pagerIDdb->at(index++).mid(26);
dbPagerIDs.append(dbPagerID);
}
}
// Place the pager ID info into the pager IDs list
void MainWin::populatePagerIDsEditor()
{
lwPagerIDs->clear();
readTextFile(userDir + "/pagerid.db", pagerIDdb);
getPagerIDElements();
int index = 0;
while(index < dbPagerIDs.count())
{
dbPagerID = dbPagerIDs.at(index++);
QString name = dbPagerID.name;
while(name.length() < 20)
name += " ";
QString baud = QSn(dbPagerID.baud);
while(baud.length() < 4)
baud = " " + baud;
QString fx = dbPagerID.fx;
while(fx.length() < 9)
fx = " " + fx;
QString ric = QSn(dbPagerID.ric);
while(ric.length() < 7)
ric = " " + ric;
QString msgLen = QSn(dbPagerID.msgLen);
while(msgLen.length() < 3)
msgLen = " " + msgLen;
QString numeric = "Alpha ";
if(dbPagerID.numeric)
numeric = "Numeric";
QString line = name + " " + ric + " " + baud + " " + QSn(dbPagerID.tx) + " " + fx + " " +
QSn(dbPagerID.funcBits) + " " + numeric + " " + msgLen;
lwPagerIDs->addItem(line);
}
}
// takes pagerID info from the QVector and places it in the editor
void MainWin::pagerIDsListToEditor(int index)
{
sspFuncBits2 = dbPagerIDs.at(index).funcBits;
setFunctionBits2();
sspBaud2 = dbPagerIDs.at(index).baud;
setBaudRate2();
if(dbPagerIDs.at(index).numeric == 1)
tbNumeric_2->setChecked(true);
else
tbAlpha_2->setChecked(true);
sbTx_2->setValue(dbPagerIDs.at(index).tx);
leRic_2->setText(QSn(dbPagerIDs.at(index).ric));
leName_2->setText(dbPagerIDs.at(index).name);
leFrequency->setText(dbPagerIDs.at(index).fx);
sbMaxMsgLen->setValue(dbPagerIDs.at(index).msgLen);
}
// return the current function bits value
int MainWin::getFunctionBits2()
{
if(tbFuncAlpha_2->isChecked())
return(3);
else if(tbFuncTone2_2->isChecked())
return(2);
else if(tbFuncTone1_2->isChecked())
return(1);
else
return(0);
}
// Return the current baud rate
int MainWin::getBaudRate2()
{
if(tb512_2->isChecked())
return(512);
else if(tb1200_2->isChecked())
return(1200);
else
return(2400);
}
// A new row is selected in the pagerIDs list so we get the info of that entry into the editor
void MainWin::pagerIDsRowChanged(int index)
{
if(index >= 0)
pagerIDsListToEditor(index);
}
// Return the elements from an entry in the gascop database plain text file
void MainWin::getPagesElements()
{
dbPages.clear();
int index = 0;
while(index < gascopdb->count())
{
dbPage.flag = gascopdb->at(index).left(1).toInt();
dbPage.sendTime = gascopdb->at(index).mid(1, 10).toUInt();
dbPage.spare = gascopdb->at(index).mid(11, 4).toInt();
dbPage.schedule = gascopdb->at(index).mid(15, 3).toInt();
dbPage.howMany = gascopdb->at(index).mid(18, 2).toInt();
dbPage.tx = gascopdb->at(index).mid(20, 1).toInt();
dbPage.baud = gascopdb->at(index).mid(21, 4).toInt();
dbPage.numeric = gascopdb->at(index).mid(25, 1).toInt();
dbPage.funcBits = gascopdb->at(index).mid(26, 1).toInt();
dbPage.ric = gascopdb->at(index).mid(27, 7).toInt();
dbPage.msg = gascopdb->at(index++).mid(34);
dbPages.append(dbPage);
}
}
// place the pager messages into the pager messages list
void MainWin::populatePagerMsgsEditor()
{
lwPages->clear();
readTextFile(userDir + "/gascop.db", gascopdb);
getPagesElements();
int index = 0;
while(index < dbPages.count())
{
dbPage = dbPages.at(index++);
QString msg = dbPage.msg;
// if(msg.length() > 39)
// msg = msg.left(36) + "...";
QString baud = QSn(dbPage.baud);
while(baud.length() < 4)
baud = " " + baud;
QString schedule = QSn(dbPage.schedule);
while(schedule.length() < 3)
schedule = "0" + schedule;
QString howMany = QSn(dbPage.howMany);
while(howMany.length() < 2)
howMany = "0" + howMany;
QString ric = QSn(dbPage.ric);
while(ric.length() < 7)
ric = " " + ric;
QString numeric = "Alpha ";
if(dbPage.numeric)
numeric = "Numeric";
QString line = QSn(dbPage.flag) + " " + QSn(dbPage.sendTime) + " " + ric + " " + baud + " " +
QSn(dbPagerID.tx) + " " + schedule + " " + howMany + " " + QSn(dbPagerID.funcBits) + " " + numeric + " " + msg;
lwPages->addItem(line);
}
if(lwPages->count() > 0)
lwPages->setCurrentRow(0);
}
// takes pages info from the QVector and places it in the editor
void MainWin::pagesListToEditor(int index)
{
if(index >= 0)
{
sspFuncBits3 = dbPages.at(index).funcBits;
setFunctionBits3();
sspBaud3 = dbPages.at(index).baud;
setBaudRate3();
if(dbPages.at(index).numeric == 1)
tbNumeric_3->setChecked(true);
else
tbAlpha_3->setChecked(true);
sbTx_3->setValue(dbPages.at(index).tx);
leRic_3->setText(QSn(dbPages.at(index).ric));
teMessage_3->setPlainText(dbPages.at(index).msg);
dtSendTime_3->setDateTime(QDateTime::currentDateTime().fromTime_t(dbPages.at(index).sendTime));
cbFlags_3->setCurrentIndex(dbPages.at(index).flag);
sbPeriodQty_3->setValue(dbPages.at(index).schedule >> 4);
cbPeriod_3->setCurrentIndex(dbPages.at(index).schedule & 0x0f);
sbHowMany_3->setValue(dbPages.at(index).howMany);
}
}
// A new row is selected in the pages list so we get the info of that entry into the editor
void MainWin::pagesRowChanged(int index)
{
if(index >= 0)
pagesListToEditor(index);
}
// Sets the baud rate buttons on the pagerIDs editor tab
void MainWin::setBaudRate2()
{
if(sspBaud2 == 512)
tb512_2->setChecked(true);
else if(sspBaud2 == 1200)
tb1200_2->setChecked(true);
else
tb2400_2->setChecked(true);
}
// Sets the function bits buttons on the pagerIDs editor tab
void MainWin::setFunctionBits2()
{
if(sspFuncBits2 == 0)
tbFuncNumeric_2->setChecked(true);
else if(sspFuncBits2 == 1)
tbFuncTone1_2->setChecked(true);
else if(sspFuncBits2 == 2)
tbFuncTone2_2->setChecked(true);
else
tbFuncAlpha_2->setChecked(true);
}
// Sets the baud rate buttons on the pages editor tab
void MainWin::setBaudRate3()
{
if(sspBaud3 == 512)
tb512_3->setChecked(true);
else if(sspBaud3 == 1200)
tb1200_3->setChecked(true);
else
tb2400_3->setChecked(true);
}
// Sets the function bits buttons on the pages editor tab
void MainWin::setFunctionBits3()
{
if(sspFuncBits3 == 0)
tbFuncNumeric_3->setChecked(true);
else if(sspFuncBits3 == 1)
tbFuncTone1_3->setChecked(true);
else if(sspFuncBits3 == 2)
tbFuncTone2_3->setChecked(true);
else
tbFuncAlpha_3->setChecked(true);
}
// PagerID button on the single page tab is clicked: show a list of pagers in the database
void MainWin::getPagerIDClicked()
{
pagerid = new Pagerid(this);
int index = 0;
getPagerIDElements();
while(index < dbPagerIDs.count())
pagerid->lwPagerIDs->addItem(dbPagerIDs.at(index++).name);
pagerid->lwPagerIDs->setCurrentRow(0);
if(pagerid->exec())
{
index = pagerid->lwPagerIDs->currentRow();
if(index >= 0)
{
lePagerName->setText(dbPagerIDs.at(index).name);
leRic->setText(QSn(dbPagerIDs.at(index).ric));
sbTx->setValue(dbPagerIDs.at(index).tx);
maxMsgLen = dbPagerIDs.at(index).msgLen;
sspFuncBits = dbPagerIDs.at(index).funcBits;
setFunctionBits();
sspBaud = dbPagerIDs.at(index).baud;
setBaudRate();
if(dbPagerIDs.at(index).numeric)
tbNumeric->setChecked(true);
else
tbAlpha->setChecked(true);
teMessage->setFocus();
}
}
delete pagerid;
pagerid = NULL;
}
// Called when the user selects a different tab
void MainWin::currentTabChanged(int tabIndex)
{
if((isAutoPagingActive == false) && (twMain->tabText(tabIndex) != "Pager Msgs"))
{
while(isPlayingWav) // Need to wait just incase some pages are being transmitted
{
gotoSleep::msleep(200);
QCoreApplication::processEvents(QEventLoop::AllEvents); // Keep the GUI responsive
}
watchdog->start();
isAutoPagingActive = true;
}
if(twMain->tabText(tabIndex) == "Page Entry") // The page entry tab is selected
{
tbGetPagerID->setFocus();
}
else if(twMain->tabText(tabIndex) == "Pager Msgs")
{
QMessageBox msgBox;
msgBox.setText("Access to the paging messages database file requires disabling the automatic paging system.");
msgBox.setInformativeText("If you select 'Ok' then automatic paging will be disabled.\n"
"If a transmission is in progress you will have to wait until it finishes.\n"
"Automatic paging will be enabled when you leave this tab.\n\n"
"If you select 'Cancel' then you will not be able to edit the database.");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Cancel);
msgBox.setGeometry(x() + 80, y() + 100, msgBox.width(), msgBox.height());
int ret = msgBox.exec();
if(ret == QMessageBox::Ok)
{
isAutoPagingActive = false;
watchdog->stop();
while(!txComplete) // Need to wait until the database is not being used
{
gotoSleep::msleep(200);
QCoreApplication::processEvents(QEventLoop::AllEvents); // Keep the GUI responsive
}
watchdog->stop();
lwPages->clear();
leRic_3->clear();
teMessage_3->clear();
readTextFile(userDir + "/gascop.db", gascopdb);
populatePagerMsgsEditor();
pbDeleteEntry_3->setEnabled(true);
}
else
{
pbDeleteEntry_3->setEnabled(false);
}
lwPages->setFocus();
}
else if(twMain->tabText(tabIndex) == "Pager IDs")
{
lwPagerIDs->setFocus();
}
}
// Cgange the message font colour when the max msg length for the pager is exceded
void MainWin::msgTextChanged()
{
if((teMessage->document()->characterCount() > maxMsgLen) && (teMessage->textColor() != Qt::red))
teMessage->setTextColor(Qt::red);
else if((teMessage->document()->characterCount() < maxMsgLen) && (teMessage->textColor() != Qt::black))
teMessage->setTextColor(Qt::black);
}
void MainWin::periodIndexChanged(QString text)
{
if(text == "Minutes") sbPeriodQty->setMaximum(59);
if(text == "Hours") sbPeriodQty->setMaximum(23);
if(text == "Days") sbPeriodQty->setMaximum(6);
if(text == "Weeks") sbPeriodQty->setMaximum(51);
if(text == "Months") sbPeriodQty->setMaximum(11);
if(text == "Years") sbPeriodQty->setMaximum(20);
}
// Load the log file into the plain text editor
void MainWin::logReload()
{
QStringList lf;
readTextFile(userDir + "/gascop.log", &lf);
logFile->document()->setPlainText(lf.join("\n"));
}
void MainWin::loadPrevPages()
{
pPages.clear();
readTextFile(userDir + "/prev-pages.txt", &pPages); // Read the previous pages file into the prev list widget
lwPrevPages->clear();
for(int x = 0; x < pPages.count(); x++) // Populate the previous pages list
{
QStringList ln = pPages.at(x).split("%|%");
if(ln.count() == 3)
{
lwPrevPages->addItem(ln.at(0) + "-" + ln.at(1) + " " + ln.at(2));
}
}
lwPrevPages->setCurrentRow(0); // Select the first row in the previous pages list
}
// Save the currently loaded log file
void MainWin::logSave()
{
QStringList lf;
lf = logFile->document()->toPlainText().split("\n", QString::KeepEmptyParts);
writeTextFile(userDir + "/gascop.log", &lf);
}
// Writes various information and messages to the loaded log file and also writes it to the log file on disk
void MainWin::displayMessages(QString msg)
{
QStringList sl;
sl << intToTime(timeToInt()) + " : " + msg;
logFile->appendPlainText(intToTime(timeToInt()) + " : " + msg);
appendTextFile(userDir + "/gascop.log", &sl);
}
void MainWin::dataLevelChanged(int value)
{
levelLabel->setText(QSn(value));
}
// Sent constant preamble as a tuning up signal
void MainWin::tuneupTransmit()
{
tbTune->setEnabled(false);
pbSend->setEnabled(false);
tbTuneStop->setEnabled(true);
for(int x = 0; x < 3700; x++)
{
msgcw.append(preambleCW);
}
singlePage = true;
buildPocsagWaveData(&msgcw, getBaudRate());
}
void MainWin::tuneupStop()
{
tbTuneStop->setEnabled(false);
if(isPlayingWav)
{
isPlayingWav = false;
Mix_HaltChannel(pocsagChannel); // Halt the channel
}
Mix_FreeChunk(SC_sound); // Free the chunck
SC_sound = NULL; // Null the pointer
txLengthTimeout(); // Run the normal function for when a transmission is finished
}
// If an alpha msg is in the message window and numeric is selected this will clear the msg window unless the text is valid for numeric messages
void MainWin::numericClicked()
{
QString txt = teMessage->toPlainText();
for(int x = 0; x < txt.length(); x++)
{
if((txt[x] < '0') || (txt[x] > '9'))
{
if((txt[x] != 'U') && (txt[x] != '[') && (txt[x] != ']') && (txt[x] != '-'))
teMessage->clear();
}
}
}
// Fast search for a RIC code
void MainWin::ricSearch()
{
if((leStartRic->text() == "") || (leStartRic->text().toUInt() > 2097151))
{
QMessageBox::critical(0, "Input Data Error", "You must enter a start RIC smaller than 2097151");
return;
}
txComplete = false;
isRicSearching = true;
isPlayingWav = true;
pbStart->setEnabled(false);
int startRIC;
leStartRic->setText(QSn((leStartRic->text().toInt() / 8) * 8));
startRIC = leStartRic->text().toInt();
lwRicSearch->clear();
msgcw.clear();
msgcw.append(syncCW);
while(startRIC < leStartRic->text().toInt() + sbAmtRics->value())
{
if(cbSendRic->isChecked()) // This section sends the RIC code (last 5 digits only) as a numeric message to the RIC being transmitted.
{
QList<quint32> ncw;
for(int x = 0; x < 8; x++)
{
msgcw.append(getRicCode(startRIC, getFunctionBits()));
ncw.clear();
encodeNumericCW(QSn(startRIC + x).right(5), &ncw); // Just encode the last 5 digits of the RIC as it fits in a single codeword
msgcw.append(ncw.at(0));
}
msgcw.append(syncCW);
startRIC += 8;
}
else // This section just transmits RICs only to set a pager off, every position contains a RIC code hence this is fast
{
for(int x = 0; x < 8; x++)
{
msgcw.append(getRicCode(startRIC, getFunctionBits()));
startRIC += 8;
msgcw.append(getRicCode(startRIC, getFunctionBits()));
startRIC -= 8;
}
msgcw.append(syncCW);
startRIC += 16;
}
}
for(int y = 0; y < 16; y++)
{
msgcw.append(idleCW);
}
searchInfo->setText("Sending RIC " + leStartRic->text() + " to " + QSn(startRIC - 1));
displayMessages("# RIC SEARCH # " + searchInfo->text() + " # RIC SEARCH #");
leStartRic->setText(QSn(startRIC));
displayPocsagData(lwRicSearch);
pbAbort->setEnabled(true);
buildPocsagWaveData(&msgcw, getBaudRate());
}
// Abort the RIC search
void MainWin::ricSearchAbort()
{
sbAmtTxs->setValue(1);
if(isPlayingWav)
{
isPlayingWav = false;
Mix_HaltChannel(pocsagChannel); // Halt the channel
}
Mix_FreeChunk(SC_sound); // Free the chunck
SC_sound = NULL; // Null the pointer
txLengthTimeout(); // Run the normal function for when a transmission is finished
pbAbort->setEnabled(false); // Disbale the abort button
}
void MainWin::resetTimeClicked()
{
dtSendTime->setDateTime(QDateTime::currentDateTime());
}
// Send pager messages one at a time by hand
void MainWin::sendSinglePage()
{