-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbard.cpp
More file actions
392 lines (348 loc) · 9.63 KB
/
bard.cpp
File metadata and controls
392 lines (348 loc) · 9.63 KB
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
// Author: Benjamin Johnson (AB3NJ)
// Date: July 30, 2017
// Purpose: Ben's Logger for logging contacts on amateur radio
#include <iostream>
#include <sstream>
#include <cstring>
#include "bard_utils.h"
#include "ncurses_utils.h"
using namespace std;
// prototypes
void setupWindows();
void commandLoop();
void command(bool cb);
void command_input();
void esc_menu();
void mode(Mode m);
void search();
void casual(Person *p);
void printInfo(Person *p);
void cprintPerson(Person *p, Contact *c);
// some global crap (blah blah bad practice blah blah)
Bard b;
NcursesUtils ncu;
Mode prevMode;
bool beginb = true; // kind of stupid, but for top and bottom bars
int main() {
string s;
// setup
b.init("bard_data");
ncu.init();
// setup windows
setupWindows();
curs_set(0);
commandLoop();
endwin();
}
void setupWindows() {
string cancelmess = "Are you sure you want to cancel? (y/n)";
// winder creationism
ncu.addWin("top", 3, COLS, 0, 0, BOX);
ncu.addWin("bottom", 3, COLS, LINES - 3, 0, BOX);
ncu.addWin("searchtext", 3, 10, 3, 0, NO_BOX);
ncu.addWin("searchbox", 3, COLS / 2 - 10, 3, 10, INPUT_BOX);
ncu.addWin("call_right", LINES - 6, COLS / 2, 3, COLS - COLS / 2, BOX);
ncu.addWin("searchresults", LINES - 9, COLS / 2, 6, 0, NO_BOX);
ncu.addWin("casualselected", 3, COLS / 2, 3, 0, BOX);
ncu.addWin("casualinput", 3, COLS / 2, 6, 0, INPUT_BOX);
ncu.addWin("casualoverviewtop", /*LINES - 6 - 7*/11, COLS / 2, 3, COLS - COLS / 2, BOX);
ncu.addWin("casualoverviewbottom", 7, COLS / 2, /*LINES - 10*/14, COLS - COLS / 2, BOX);
ncu.addWin("casualmessage", LINES - 12, COLS / 2, 9, 0, NO_BOX);
ncu.addWin("cancel", 3, COLS, LINES / 2, 0, NO_BOX);
ncu.addWin("esc", LINES, 30, 0, 0, NO_BOX);
// build escape menu
wbkgd(ncu.getWin("esc"), COLOR_PAIR(1));
mvwhline(ncu.getWin("esc"), 1, 0, 0, 30);
mvwhline(ncu.getWin("esc"), 7, 0, 0, 30);
// other stuff
wbkgd(ncu.getWin("cancel"), COLOR_PAIR(1));
mvwprintw(ncu.getWin("searchtext"), 1, 2, "Search");
mvwprintw(ncu.getWin("esc"), 1, 2, " Modes ");
mvwprintw(ncu.getWin("esc"), 3, 3, "c: Casual");
mvwprintw(ncu.getWin("esc"), 4, 3, "C: Contest");
mvwprintw(ncu.getWin("esc"), 5, 3, "n: Net");
mvwprintw(ncu.getWin("esc"), 9, 3, "u: Set User Info");
mvwprintw(ncu.getWin("esc"), 10, 3, "s: Search");
mvwprintw(ncu.getWin("esc"), 11, 3, "q: Quit");
mvwprintw(ncu.getWin("cancel"), 1, (COLS - cancelmess.size()) / 2, cancelmess.c_str());
// layout creationism
vector<string> v = {"top", "bottom"};
ncu.addLayout("always", v, false, false);
v = {"esc"};
ncu.addLayout("esc", v, true, false);
v = {"searchtext", "searchbox", "searchresults"};
ncu.addLayout("search", v, true, true);
v = {"casualselected", "casualinput", "casualoverviewtop", "casualoverviewbottom", "casualmessage"};
ncu.addLayout("casual", v, true, true);
v = {"cancel"};
ncu.addLayout("cancel", v, true, false);
}
void commandLoop() {
char c;
string tmp;
// start in escape mode
mode(ESC);
while (1) {
switch (b.getMode()) {
case ESC:
switch (getch()) {
case 'q':
return;
break;
case 's':
mode(SEARCH);
break;
case 'c':
mode(CASUAL);
break;
case 27:
mode(prevMode);
break;
default: break;
}
break;
case SEARCH:
search();
mode(ESC);
break;
break;
case CASUAL:
casual(NULL);
mode(ESC);
break;
default: break;
}
}
}
// function functions --------------------------------------------------------
void mode(Mode m) {
prevMode = b.getCurrMode();
// set the mode
b.setMode(m);
int result;
if (m != ESC && beginb == true) {
ncu.showLayout("always");
beginb = false;
}
else if (m != ESC && beginb == false) {
ncu.clearWin("top");
ncu.clearWin("bottom");
}
switch(m) {
case SEARCH:
ncu.printWin("top", 1, 2, "Search Callsign Database");
ncu.printWin("bottom", 1, 2, "s: New Search n: Create Log e: Edit Contact");
ncu.showLayout("search");
break;
case ESC:
ncu.showLayout("esc");
break;
case CASUAL:
ncu.printWin("top", 1, 2, "Casual Mode");
ncu.printWin("bottom", 1, 2, "c: Call Sign n: Name l: Location s: Signal Sent r: Signal Received t: Time T: Custom Time f: Finish");
ncu.showLayout("casual");
break;
default:
break;
}
}
void search() {
Person *p = NULL;
string s;
while (1) {
ncu.clearWin("searchbox");
switch(getch()) {
case 's':
ncu.clearWin("searchresults");
ncu.clearWin("searchbox");
s = ncu.getInput("searchbox");
if (s != "") {
b.cap(s);
p = b.search(s);
if (p != NULL) printInfo(p);
else ncu.printWin("searchresults", 1, 2, "No results.");
}
break;
case 'n':
if (p == NULL) ncu.printWin("searchresults", 1, 2, "Enter a valid callsign to start a log");
else {
mode(CASUAL);
casual(p);
mode(SEARCH);
}
break;
case 27:
return;
default: break;
}
}
}
void casual(Person *passp) {
int i, choice;
char ch;
bool timeb = false;
string s;
stringstream ss;
Person *p, *ptmp;
Contact *c;
// setup
p = new Person;
c = new Contact;
c->time = new Time;
if (passp == NULL) {
p->call.resize(1);
}
else {
*p = *passp;
}
if (p->location.size() != 0) c->location = 0;
while (1) {
// update screen
ncu.clearWin("casualselected");
ncu.clearWin("casualoverviewtop");
cprintPerson(p, c);
switch (getch()) {
case 'c':
ncu.printWin("casualselected", 1, (COLS/2-4)/2, "Callsign");
s = ncu.getInput("casualinput");
if (s != "") {
b.cap(s);
// search for call sign and add info if found
ptmp = b.search(s);
if (ptmp == NULL) p->call[0] = s;
else {
*p = *ptmp;
if (p->location.size() != 0) c->location = 0;
}
}
break;
case 'n':
ncu.printWin("casualselected", 1, (COLS/2-4)/2, "Name");
s = ncu.getInput("casualinput");
if (s != "") p->name = s;
break;
case 't':
if (timeb) {
ncu.printWin("casualmessage", 1, 2, "Time already recorded. Overwrite? (y/n)");
if (getch() == 'y') c->time = b.getTime();
ncu.clearWin("casualmessage");
}
else {
c->time = b.getTime();
timeb = true;
}
break;
case 'l':
ncu.printWin("casualselected", 1, ((COLS/2) - 8) / 2, "Location");
if (p->location.size() == 0) {
s = ncu.getInput("casualinput");
if (s != "") {
p->location.push_back(s);
c->location = 0;
}
}
else {
ss.str(""); // for cleanliness sake
ncu.printWin("casualmessage", 1, 2, "Choose a location (or esc to cancel):");
wmove(ncu.getWin("casualmessage"), 2, 1);
whline(ncu.getWin("casualmessage"), 0, COLS/2 - 2);
for (i = 0; i < p->location.size(); i++) {
ss << i + 1 << ": " << p->location[i];
ncu.printWin("casualmessage", i + 3, 2, ss.str());
ss.str("");
}
ss << i+1 << ": + New Location";
ncu.printWin("casualmessage", p->location.size() + 3, 2, ss.str());
while (1) {
ch = getch();
if (ch == 27) break; // esc the madness
choice = ch - '0';
cout << choice << endl;
if (choice > 0 && choice < p->location.size() + 1) {
c->location = choice - 1;
break;
}
else if (choice == p->location.size() + 1) {
s = ncu.getInput("casualinput");
p->location.push_back(s);
c->location = p->location.size() - 1;
break;
}
}
}
ncu.clearWin("casualmessage");
break;
case 27:
ncu.showLayout("cancel");
if (getch() == 'y') {
ncu.hideLayout("cancel");
return;
}
ncu.hideLayout("cancel");
break;
default: break;
}
}
}
void cprintPerson(Person *p, Contact *c) {
string cw = "casualoverviewtop";
string you = "casualoverviewbottom";
int cl = 0;
stringstream ss;
ncu.printWin(cw, cl++, 1, " Contact ");
cl++;
ncu.printWin(cw, cl, 2, "Callsign:");
if (p->call.size() != 0) ncu.printWin(cw, cl, 19, p->call[0]);
cl++;
ncu.printWin(cw, cl++, 2, "Name: " + p->name);
ncu.printWin(cw, cl, 2, "Location:");
if (p->location.size() != 0 && c->location != -1)
ncu.printWin(cw, cl, 19, p->location[c->location]);
cl++;
ss << c->sigs;
ncu.printWin(cw, cl++, 2, "Report Sent: " + ss.str());
ss.clear(); ss.str(""); ss << c->sigr;
ncu.printWin(cw, cl++, 2, "Report Received: " + ss.str());
cl++;
ncu.printWin(cw, cl, 2, "Time: ");
ncu.printWin(cw, cl+1, 2, "Date: ");
if (c->time->hour != -1) {
// time
ss.clear(); ss.str("");
if (c->time->hour < 10) ss << 0; ss << c->time->hour << ":";
if (c->time->minute < 10) ss << 0; ss << c->time->minute;
ncu.printWin(cw, cl++, 19, ss.str());
// date
ss.str("");
if (c->time->day < 10) ss << 0; ss << c->time->day << ".";
if (c->time->month < 10) ss << 0; ss << c->time->month << ".";
ss << c->time->year;
ncu.printWin(cw, cl++, 19, ss.str());
}
// you part
cl = 0;
ncu.printWin(you, cl++, 1, " You ");
cl++;
ncu.printWin(you, cl++, 2, "Location: " + b.userInfo->location[b.userInfo->cur[0]]);
ncu.printWin(you, cl++, 2, "Radio: " + b.userInfo->radio[b.userInfo->cur[1]]);
ss.str("");
ss << "Power: " << b.userInfo->cur[2];
ncu.printWin(you, cl++, 2, ss.str());
ncu.printWin(you, cl++, 2, "Mode: " + b.getRadioMode(b.userInfo->cur[3]));
}
void printInfo(Person *p) {
string cw = "searchresults"; // for my sake
stringstream ss;
int cl = 1;
ncu.printWin(cw, cl++, 2, "CALL: " + p->call[0]);
if (p->call.size() > 1) {
ncu.printWin(cw, cl, 2, "PREVIOUS CALL(S): ");
for (int i = 1; i < p->call.size(); i++) {
ncu.printWin(cw, cl++, 20, p->call[i]);
}
}
ncu.printWin(cw, cl++, 2, "Location: " + p->location[0]);
cl++;
ss << p->contact.size() << " previous contacts";
ncu.printWin(cw, cl++, 2, ss.str());
}