Debian Codegraph @

src/radiogram/radiogram.cxx

raw
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2014
3 // David Freese, W1HKJ
4 //
5 // This file is part of flmsg
6 //
7 // flrig is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // flrig is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // ----------------------------------------------------------------------------
20
21 #include <stdlib.h>
22 #include <iostream>
23 #include <fstream>
24 #include <string>
25 #include <cstring>
26 #include <ctime>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <stdio.h>
30 #include <errno.h>
31
32 #include <FL/Fl.H>
33 #include <FL/Enumerations.H>
34 #include <FL/Fl_Window.H>
35 #include <FL/Fl_Button.H>
36 #include <FL/Fl_Group.H>
37 #include <FL/Fl_Sys_Menu_Bar.H>
38 #include <FL/x.H>
39 #include <FL/Fl_Help_Dialog.H>
40 #include <FL/Fl_Menu_Item.H>
41 #include <FL/Fl_File_Icon.H>
42
43 #include "config.h"
44 #include "flmsg_config.h"
45
46 #include "flmsg.h"
47 #include "templates.h"
48 #include "debug.h"
49 #include "util.h"
50 #include "gettext.h"
51 #include "flmsg_dialog.h"
52 #include "flinput2.h"
53 #include "date.h"
54 #include "calendar.h"
55 #include "icons.h"
56 #include "fileselect.h"
57 #include "wrap.h"
58 #include "status.h"
59 #include "parse_xml.h"
60 #include "arl_msgs.h"
61
62 #ifdef WIN32
63 # include "flmsgrc.h"
64 # include "compat.h"
65 # define dirent fl_dirent_no_thanks
66 #endif
67
68 #include <FL/filename.H>
69 #include "dirent-check.h"
70
71 #include <FL/x.H>
72 #include <FL/Fl_Pixmap.H>
73 #include <FL/Fl_Image.H>
74
75 std::string base_rg_filename = "";
76 std::string def_rg_filename = "";
77 std::string def_rg_TemplateName = "";
78
79 // Radiogram rgfields
80
81 const char *s_prec[] = {
82 "ROUTINE", "WELFARE", "PRIORITY", "EMERGENCY",
83 "TEST ROUTINE", "TEST WELFARE", "TEST PRIORITY", "TEST EMERGENCY"};
84 const char precitems[] =
85 "ROUTINE|WELFARE|PRIORITY|EMERGENCY|\
86 TEST ROUTINE|TEST WELFARE|TEST PRIORITY|TEST EMERGENCY";
87
88 const char *s_hx[] = {"", "HXA", "HXB", "HXC", "HXD", "HXE", "HXF", "HXG"};
89 const char hxitems[] = " |HXA|HXB|HXC|HXD|HXE|HXF|HXG";
90
91 // compatibility fields required to read older data files
92
93 std::string a_rg_nbr = "<nbr:"; // 0.
94 std::string a_rg_prec = "<prec:"; // 1.
95 std::string a_rg_hx = "<hx:"; // 2.
96 std::string a_rg_d1 = "<d1:"; // 3.
97 std::string a_rg_t1 = "<t1:"; // 4.
98 std::string a_rg_dt2 = "<dt2:"; // 5.
99 std::string a_rg_dt3 = "<dt3:"; // 6.
100 std::string a_rg_to = "<to:"; // 7.
101 std::string a_rg_phone = "<tel:"; // 8.
102 std::string a_rg_opnote = "<opn:"; // 9
103 std::string a_rg_msg = "<msg:"; // 10.
104 std::string a_rg_sig = "<sig:"; // 11.
105 std::string a_rg_opnote2 = "<op2:"; // 12
106 std::string a_rg_check = "<ck:"; // 13.
107 std::string a_rg_station = "<sta:"; // 14.
108 std::string a_rg_place = "<org:"; // 15
109 std::string a_rg_orig = "<ori:"; // 16
110 std::string a_rg_dlvd_to = "<dlv:"; // 17
111 std::string a_rg_sent_to = "<sto:"; // 18
112 std::string a_rg_snt_net = "<snt:"; // 19
113 std::string a_rg_dt4 = "<dt4:"; // 20
114 std::string a_rg_rcv_fm = "<rfm:"; // 21
115 std::string a_rg_rcv_net = "<rnt:"; // 22
116 std::string a_rg_dt5 = "<dt5:"; // 23
117 std::string a_rg_svc = "<svc:"; // 24
118
119 FIELD argfields[] = {
120 { a_rg_nbr, "", (void **)&txt_rg_nbr, 't' }, // 0
121 { a_rg_prec, "0", (void **)&sel_rg_prec, 's' }, // 1
122 { a_rg_hx, "", (void **)&txt_rg_hx, 't' }, // 2
123 { a_rg_d1, "", (void **)&txt_rg_d1, 't' }, // 3
124 { a_rg_t1, "", (void **)&txt_rg_t1, 't' }, // 4
125 { a_rg_dt2, "", (void **)&txt_rg_dt2, 't' }, // 5
126 { a_rg_dt3, "", (void **)&txt_rg_dt3, 't' }, // 6
127 { a_rg_to, "", (void **)&txt_rg_to, 't' }, // 7
128 { a_rg_phone, "", (void **)&txt_rg_phone, 't' }, // 8
129 { a_rg_opnote, "", (void **)&txt_rg_opnote, 't' }, // 9
130 { a_rg_msg, "", (void **)&txt_rg_msg, 'e' }, // 10
131 { a_rg_sig, "", (void **)&txt_rg_sig, 't' }, // 11
132 { a_rg_opnote2, "", (void **)&txt_rg_opnote2,'t' }, // 12
133 { a_rg_check, "", (void **)&txt_rg_check, 't' }, // 13
134 { a_rg_station, "", (void **)&txt_rg_station,'t' }, // 14
135 { a_rg_place, "", (void **)&txt_rg_place, 't' }, // 15
136 { a_rg_orig, "", (void **)&txt_rg_orig, 't' }, // 16
137 { a_rg_dlvd_to, "", (void **)&txt_rg_dlvd_to,'t' }, // 17
138 { a_rg_sent_to, "", (void **)&txt_rg_sent_to,'t' }, // 18
139 { a_rg_snt_net, "", (void **)&txt_rg_snt_net,'t' }, // 19
140 { a_rg_dt4, "", (void **)&txt_rg_dt4, 't' }, // 20
141 { a_rg_rcv_fm, "", (void **)&txt_rg_rcv_fm, 't' }, // 21
142 { a_rg_rcv_net, "", (void **)&txt_rg_rcv_net,'t' }, // 22
143 { a_rg_dt5, "", (void **)&txt_rg_dt5, 't' }, // 23
144 { a_rg_svc, "", (void **)&btn_rg_svc, 'b' } // 24
145 };
146
147 // new tag std::strings
148
149 std::string _rg_nbr = ":nbr:"; // 0.
150 std::string _rg_prec = ":prec:"; // 1.
151 std::string _rg_hx = ":hx:"; // 2.
152 std::string _rg_d1 = ":d1:"; // 3.
153 std::string _rg_t1 = ":t1:"; // 4.
154 std::string _rg_dt2 = ":dt2:"; // 5.
155 std::string _rg_dt3 = ":dt3:"; // 6.
156 std::string _rg_to = ":to:"; // 7.
157 std::string _rg_phone = ":tel:"; // 8.
158 std::string _rg_opnote = ":opn:"; // 9
159 std::string _rg_msg = ":msg:"; // 10.
160 std::string _rg_sig = ":sig:"; // 11.
161 std::string _rg_opnote2 = ":op2:"; // 12
162 std::string _rg_check = ":ck:"; // 13.
163 std::string _rg_station = ":sta:"; // 14.
164 std::string _rg_place = ":org:"; // 15
165 std::string _rg_orig = ":ori:"; // 16
166 std::string _rg_dlvd_to = ":dlv:"; // 17
167 std::string _rg_sent_to = ":sto:"; // 18
168 std::string _rg_snt_net = ":snt:"; // 19
169 std::string _rg_dt4 = ":dt4:"; // 20
170 std::string _rg_rcv_fm = ":rfm:"; // 21
171 std::string _rg_rcv_net = ":rnt:"; // 22
172 std::string _rg_dt5 = ":dt5:"; // 23
173 std::string _rg_svc = ":svc:"; // 24
174 std::string _rg_standard = ":std:"; // 25
175
176 FIELD rgfields[] = {
177 { _rg_nbr, "", (void **)&txt_rg_nbr, 't' }, // 0
178 { _rg_prec, "0", (void **)&sel_rg_prec, 's' }, // 1
179 { _rg_hx, "", (void **)&txt_rg_hx, 't' }, // 2
180 { _rg_d1, "", (void **)&txt_rg_d1, 't' }, // 3
181 { _rg_t1, "", (void **)&txt_rg_t1, 't' }, // 4
182 { _rg_dt2, "", (void **)&txt_rg_dt2, 't' }, // 5
183 { _rg_dt3, "", (void **)&txt_rg_dt3, 't' }, // 6
184 { _rg_to, "", (void **)&txt_rg_to, 't' }, // 7
185 { _rg_phone, "", (void **)&txt_rg_phone, 't' }, // 8
186 { _rg_opnote, "", (void **)&txt_rg_opnote, 't' }, // 9
187 { _rg_msg, "", (void **)&txt_rg_msg, 'e' }, // 10
188 { _rg_sig, "", (void **)&txt_rg_sig, 't' }, // 11
189 { _rg_opnote2, "", (void **)&txt_rg_opnote2, 't' }, // 12
190 { _rg_check, "", (void **)&txt_rg_check, 't' }, // 13
191 { _rg_station, "", (void **)&txt_rg_station, 't' }, // 14
192 { _rg_place, "", (void **)&txt_rg_place, 't' }, // 15
193 { _rg_orig, "", (void **)&txt_rg_orig, 't' }, // 16
194 { _rg_dlvd_to, "", (void **)&txt_rg_dlvd_to, 't' }, // 17
195 { _rg_sent_to, "", (void **)&txt_rg_sent_to, 't' }, // 18
196 { _rg_snt_net, "", (void **)&txt_rg_snt_net, 't' }, // 19
197 { _rg_dt4, "", (void **)&txt_rg_dt4, 't' }, // 20
198 { _rg_rcv_fm, "", (void **)&txt_rg_rcv_fm, 't' }, // 21
199 { _rg_rcv_net, "", (void **)&txt_rg_rcv_net, 't' }, // 22
200 { _rg_dt5, "", (void **)&txt_rg_dt5, 't' }, // 23
201 { _rg_svc, "F", (void **)&btn_rg_svc, 'b' }, // 24
202 { _rg_standard, "T", (void **)&btn_rg_standard, 'B' } // 25
203 };
204
205 bool using_rg_template = false;
206
207 int num_rgfields = sizeof(rgfields) / sizeof(FIELD);
208
209 int fld_nbr(std::string &fld)
210 {
211 for (int i = 0; i < num_rgfields; i++)
212 if (fld == rgfields[i].f_type)
213 return i;
214 printf("err %s\n", fld.c_str());
215 exit(1);
216 }
217
218 //======================================================================
219
220 void cb_rgSetDate1()
221 {
222 txt_rg_d1->value(szAbbrevDate());
223 }
224
225 void cb_rgSetTime1()
226 {
227 txt_rg_t1->value(szTime(progStatus.UTC <= 1 ? 0 : 2));
228 }
229
230 void cb_rgSetDateTime2()
231 {
232 txt_rg_dt2->value(szDateTime());
233 }
234
235 void cb_rgSetDateTime3()
236 {
237 txt_rg_dt3->value(szDateTime());
238 }
239
240 void cb_rgSetDateTime4()
241 {
242 txt_rg_dt4->value(szDateTime());
243 }
244
245 void cb_rgSetDateTime5()
246 {
247 txt_rg_dt5->value(szDateTime());
248 }
249
250 void cb_rg_nbr(Fl_Widget *wdg)
251 {
252 Fl_Input2 *inp = (Fl_Input2 *)wdg;
253 std::string s = inp->value();
254 for (size_t n = 0; n < s.length(); n++)
255 if (!isdigit(s[n])) s.erase(n,1);
256 strip_leading_zeros(s);
257 inp->value(s.c_str());
258 }
259
260 static char valid_input[] = "0123456789/ ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
261
262 void cb_rg_filter_input(Fl_Widget *wdg)
263 {
264 Fl_Input2 *inp = (Fl_Input2 *)wdg;
265 int p = inp->position();
266 std::string s = inp->value();
267 ucase(s);
268 for (size_t n = 0; n < s.length(); n++)
269 if (strchr(valid_input, s[n]) == NULL)
270 s.erase(n,1);
271 inp->value(s.c_str());
272 inp->position(p);
273 }
274
275 void clear_rgfields()
276 {
277 for (int i = 0; i < num_rgfields; i++) {
278 switch (rgfields[i].w_type) {
279 case 's':
280 rgfields[i].f_data = "0";
281 break;
282 case 'b':
283 rgfields[i].f_data = "F";
284 break;
285 case 'B':
286 rgfields[i].f_data = "T";
287 break;
288 default:
289 rgfields[i].f_data.clear();
290 }
291 }
292 }
293
294 std::string numeric(int n)
295 {
296 static char snum[12];
297 snprintf(snum, sizeof(snum), "%d", n);
298 return snum;
299 }
300
301 void set_rg_choices() {
302 sel_rg_prec->clear();
303 sel_rg_prec->add(precitems);
304 sel_rg_prec->index(0);
305 }
306
307 bool check_rgfields()
308 {
309 std::string temp;
310 for (int i = 0; i < num_rgfields; i++) {
311 if (rgfields[i].w == NULL) return false;
312 if (rgfields[i].w_type == 'd') {
313 if (rgfields[i].f_data != ((Fl_DateInput *)(*rgfields[i].w))->value())
314 return true;
315 } else if (rgfields[i].w_type == 't') {
316 if (rgfields[i].f_data != ((Fl_Input2 *)(*rgfields[i].w))->value())
317 return true;
318 } else if (rgfields[i].w_type == 's') {
319 int choice = ((Fl_ListBox *)(*rgfields[i].w))->index();
320 if (rgfields[i].f_data != numeric(choice))
321 return true;
322 } else if (rgfields[i].w_type == 'e') {
323 if (rgfields[i].f_data != ((FTextEdit *)(*rgfields[i].w))->buffer()->text())
324 return true;
325 } else if (rgfields[i].w_type == 'b') {
326 temp = ((Fl_Button *)(*rgfields[i].w))->value() ? "T" : "F";
327 if (rgfields[i].f_data != temp)
328 return true;
329 } else if (rgfields[i].w_type == 'B'){
330 temp = ((Fl_Button *)(*rgfields[i].w))->value() ? "T" : "F";
331 if (rgfields[i].f_data != temp)
332 return true;
333 }
334 }
335 return false;
336 }
337
338 void update_rgfields()
339 {
340 for (int i = 0; i < num_rgfields; i++) {
341 if (rgfields[i].w_type == 'd')
342 rgfields[i].f_data = ((Fl_DateInput *)(*rgfields[i].w))->value();
343 else if (rgfields[i].w_type == 't')
344 rgfields[i].f_data = ((Fl_Input2 *)(*rgfields[i].w))->value();
345 else if (rgfields[i].w_type == 's') {
346 int choice = ((Fl_ListBox *)(*rgfields[i].w))->index();
347 if (choice >= 0)
348 rgfields[i].f_data = numeric(choice);
349 } else if (rgfields[i].w_type == 'e')
350 rgfields[i].f_data = ((FTextEdit *)(*rgfields[i].w))->buffer()->text();
351 else if (rgfields[i].w_type == 'b')
352 rgfields[i].f_data = ((Fl_Button *)(*rgfields[i].w))->value() ? "T" : "F";
353 else if (rgfields[i].w_type == 'B')
354 rgfields[i].f_data = ((Fl_Button *)(*rgfields[i].w))->value() ? "T" : "F";
355 }
356 }
357
358 void clear_rg_form()
359 {
360 clear_rgfields();
361
362 if (progStatus.rgnbr_fname)
363 txt_rg_nbr->value(progStatus.rgnbr.c_str());
364 else
365 txt_rg_nbr->value("");
366
367 for (int i = 1; i < num_rgfields; i++)
368 if (rgfields[i].w_type == 'd')
369 ((Fl_DateInput *)(*rgfields[i].w))->value("");
370 else if (rgfields[i].w_type == 't')
371 ((Fl_Input2 *)(*rgfields[i].w))->value("");
372 else if (rgfields[i].w_type == 's')
373 ((Fl_ListBox *)(*rgfields[i].w))->index(0);
374 else if (rgfields[i].w_type == 'e')
375 ((FTextEdit *)(*rgfields[i].w))->clear();
376 else if (rgfields[i].w_type == 'b')
377 ((Fl_Button *)(*rgfields[i].w))->value(0);
378 else if (rgfields[i].w_type == 'B')
379 ((Fl_Button *)(*rgfields[i].w))->value(1);
380 update_rgfields();
381 }
382
383 void update_rg_form()
384 {
385 for (int i = 0; i < num_rgfields; i++) {
386 if (rgfields[i].w_type == 'd')
387 ((Fl_DateInput *)(*rgfields[i].w))->value(rgfields[i].f_data.c_str());
388 else if (rgfields[i].w_type == 't')
389 ((Fl_Input2 *)(*rgfields[i].w))->value(rgfields[i].f_data.c_str());
390 else if (rgfields[i].w_type == 's')
391 ((Fl_ListBox *)(*rgfields[i].w))->index(atoi(rgfields[i].f_data.c_str()));
392 else if (rgfields[i].w_type == 'e') {
393 ((FTextEdit *)(*rgfields[i].w))->clear();
394 ((FTextEdit *)(*rgfields[i].w))->add(rgfields[i].f_data.c_str());
395 } else if (rgfields[i].w_type == 'b')
396 ((Fl_Button *)(*rgfields[i].w))->value(rgfields[i].f_data == "T" ? 1 : 0);
397 else if (rgfields[i].w_type == 'B')
398 ((Fl_Button *)(*rgfields[i].w))->value(rgfields[i].f_data == "T" ? 1 : 0);
399 }
400 }
401
402 void make_rg_buffer(bool compress = false)
403 {
404 std::string mbuff;
405 mbuff.clear();
406 for (int i = 0; i < num_rgfields; i++)
407 mbuff.append( lineout( rgfields[i].f_type, rgfields[i].f_data ) );
408 if (compress) compress_maybe(mbuff);
409 buffer.append(mbuff);
410 }
411
412 void read_rg_buffer(std::string data)
413 {
414 bool data_ok = false;
415 clear_fields();
416 read_header(data);
417
418 std::string temp;
419 for (int i = 0; i < num_rgfields; i++) {
420 temp = findstr(data, rgfields[i].f_type);
421 if (!temp.empty()) rgfields[i].f_data = temp;
422 if (!rgfields[i].f_data.empty()) data_ok = true;
423 }
424 if (!data_ok)
425 for (int i = 0; i < num_rgfields; i++)
426 rgfields[i].f_data = findstr(data, argfields[i].f_type);
427
428 update_rg_form();
429 }
430
431 void cb_rg_new()
432 {
433 if (check_rgfields()) {
434 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
435 update_header(CHANGED);
436 cb_rg_save();
437 }
438 }
439 clear_rg_form();
440 clear_header();
441 def_rg_filename = ICS_msg_dir;
442 def_rg_filename.append("new").append(RGFILE_EXT);
443 using_rg_template = false;
444 show_filename(def_rg_filename);
445 }
446
447 void cb_rg_import()
448 {
449 std::string def_rg_filename = ICS_dir;
450 def_rg_filename.append("DEFAULT.XML");
451 const char *p = FSEL::select(
452 "Open Qforms xml file",
453 "Qforms xml\t*.{xml,XML}",
454 def_rg_filename.c_str());
455 if (p){
456 clear_rg_form();
457 qform_rg_import(p);
458 using_rg_template = false;
459 }
460 }
461
462 void cb_rg_export()
463 {
464 std::string exp_rgFileName = ICS_dir;
465 exp_rgFileName.append(base_rg_filename);
466 exp_rgFileName.append(".XML");
467 const char *p = FSEL::saveas(
468 "Open Qforms xml file",
469 "Qforms xml\t*.{xml,XML}",
470 exp_rgFileName.c_str());
471 if (p) {
472 const char *pext = fl_filename_ext(p);
473 exp_rgFileName = p;
474 if (strlen(pext) == 0) exp_rgFileName.append(".XML");
475 qform_rg_export(exp_rgFileName);
476 }
477 }
478
479 void cb_rg_wrap_import(std::string wrapfilename, std::string inpbuffer)
480 {
481 clear_rg_form();
482 read_rg_buffer(inpbuffer);
483 def_rg_filename = ICS_msg_dir;
484 def_rg_filename.append(wrapfilename);
485 show_filename(def_rg_filename);
486 using_rg_template = false;
487 }
488
489 int eval_rg_fsize()
490 {
491 Ccrc16 chksum;
492 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
493 evalstr.append(base_rg_filename).append("]");
494 update_rgfields();
495 update_header(FROM);
496 evalstr.append(header("<radiogram>"));
497 buffer.clear();
498 make_rg_buffer(true);
499 if (buffer.empty()) return 0;
500 evalstr.append( buffer );
501 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
502 return evalstr.length();
503 }
504
505 void cb_rg_wrap_export()
506 {
507 if (btn_rg_check->labelcolor() == FL_RED)
508 cb_rg_check();
509
510 if (check_rgfields()) {
511 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
512 return;
513 update_header(CHANGED);
514 }
515 update_rgfields();
516
517 if (base_rg_filename == std::string("new").append(RGFILE_EXT) ||
518 base_rg_filename == std::string("default").append(RGFILE_EXT) )
519 if (!cb_rg_save_as()) return;
520
521 std::string wrapfilename = WRAP_send_dir;
522 wrapfilename.append(base_rg_filename);
523 wrapfilename.append(WRAP_EXT);
524 const char *p = FSEL::saveas(
525 "Save as wrapped radiogram file",
526 "Wrap file\t*.{wrap,WRAP}",
527 wrapfilename.c_str());
528 if (p) {
529 if (btn_rg_check->labelcolor() == FL_RED)
530 cb_rg_check();
531 std::string pext = fl_filename_ext(p);
532 wrapfilename = p;
533
534 update_header(FROM);
535 buffer.assign(header("<radiogram>"));
536 make_rg_buffer(true);
537 export_wrapfile(base_rg_filename, wrapfilename, buffer, pext != WRAP_EXT);
538
539 buffer.assign(header("<radiogram>"));
540 make_rg_buffer(false);
541 write_rg(def_rg_filename);
542 }
543 }
544
545 void cb_rg_wrap_autosend()
546 {
547 if (btn_rg_check->labelcolor() == FL_RED)
548 cb_rg_check();
549
550 if (check_rgfields()) {
551 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
552 return;
553 update_header(CHANGED);
554 }
555 update_rgfields();
556
557 if (base_rg_filename == std::string("new").append(RGFILE_EXT) ||
558 base_rg_filename == std::string("default").append(RGFILE_EXT) )
559 if (!cb_rg_save_as()) return;
560
561 update_header(FROM);
562 buffer.assign(header("<radiogram>"));
563 make_rg_buffer(true);
564 xfr_via_socket(base_rg_filename, buffer);
565
566 buffer.assign(header("<radiogram>"));
567 make_rg_buffer(false);
568 write_rg(def_rg_filename);
569 }
570
571 void cb_rg_load_template()
572 {
573 std::string def_rg_filename = def_rg_TemplateName;
574 const char *p = FSEL::select(
575 "Open template file",
576 std::string("Template file\t*").append(RGTEMP_EXT).c_str(),
577 def_rg_filename.c_str());
578 if (p) {
579 clear_rg_form();
580 read_data_file(p);
581 def_rg_TemplateName = p;
582 show_filename(def_rg_TemplateName);
583 using_rg_template = true;
584 }
585 }
586
587 void cb_rg_save_template()
588 {
589 if (!using_rg_template) {
590 cb_rg_save_as_template();
591 return;
592 }
593 std::string def_rg_filename = def_rg_TemplateName;
594 const char *p = FSEL::saveas(
595 "Save template file",
596 std::string("Template file\t*").append(RGTEMP_EXT).c_str(),
597 def_rg_filename.c_str());
598 if (p) {
599 update_header(CHANGED);
600 update_rgfields();
601 buffer.assign(header("<radiogram>"));
602 make_rg_buffer();
603 write_rg(p);
604 }
605 }
606
607 void cb_rg_save_as_template()
608 {
609 std::string def_rg_filename = def_rg_TemplateName;
610 const char *p = FSEL::saveas(
611 "Save as template file",
612 std::string("Template file\t*").append(RGTEMP_EXT).c_str(),
613 def_rg_filename.c_str());
614 if (p) {
615 const char *pext = fl_filename_ext(p);
616 def_rg_TemplateName = p;
617 if (strlen(pext) == 0) def_rg_TemplateName.append(RGTEMP_EXT);
618 remove_spaces_from_filename(def_rg_TemplateName);
619
620 clear_header();
621 update_header(CHANGED);
622 buffer.assign(header("<radiogram>"));
623 make_rg_buffer();
624 write_rg(def_rg_TemplateName);
625
626 show_filename(def_rg_TemplateName);
627 using_rg_template = true;
628 }
629 }
630
631 void cb_rg_open()
632 {
633 const char *p = FSEL::select(
634 _("Open data file"),
635 std::string("radiogram\t*").append(RGFILE_EXT).c_str(),
636 def_rg_filename.c_str());
637 if (!p) return;
638 if (strlen(p) == 0) return;
639 clear_rg_form();
640 read_data_file(p);
641 using_rg_template = false;
642 def_rg_filename = p;
643 show_filename(def_rg_filename);
644 }
645
646 void write_rg(std::string s)
647 {
648 FILE *rgfile = fopen(s.c_str(), "w");
649 if (!rgfile) return;
650
651 fwrite(buffer.c_str(), buffer.length(), 1, rgfile);
652 fclose(rgfile);
653 }
654
655 bool cb_rg_save_as()
656 {
657 const char *p;
658 std::string newfilename;
659 std::string name = named_file();
660
661 if (!name.empty()) {
662 name.append(RGFILE_EXT);
663 newfilename = ICS_msg_dir;
664 newfilename.append(name);
665 } else
666 newfilename = def_rg_filename;
667 p = FSEL::saveas(
668 _("Save data file"),
669 std::string("radiogram\t*").append(RGFILE_EXT).c_str(),
670 newfilename.c_str());
671
672 if (!p) return false;
673 if (strlen(p) == 0) return false;
674
675 if (progStatus.rgnbr_fname) {
676 int n = atoi(progStatus.rgnbr.c_str());
677 n++;
678 char szn[12];
679 snprintf(szn, sizeof(szn), "%d", n);
680 progStatus.rgnbr = szn;
681 txt_rgnbr->value(szn);
682 txt_rgnbr->redraw();
683 } else if (progStatus.sernbr_fname)
684 update_sernbr();
685
686 const char *pext = fl_filename_ext(p);
687 def_rg_filename = p;
688 if (strlen(pext) == 0) def_rg_filename.append(RGFILE_EXT);
689
690 remove_spaces_from_filename(def_rg_filename);
691
692 update_header(NEW);
693 update_rgfields();
694 buffer.assign(header("<radiogram>"));
695 make_rg_buffer();
696 write_rg(def_rg_filename);
697
698 using_rg_template = false;
699 show_filename(def_rg_filename);
700 return true;
701 }
702
703 void cb_rg_save()
704 {
705 if (base_rg_filename == std::string("new").append(RGFILE_EXT) ||
706 base_rg_filename == std::string("default").append(RGFILE_EXT) ||
707 using_rg_template == true) {
708 cb_rg_save_as();
709 return;
710 }
711
712 if (check_rgfields()) update_header(CHANGED);
713 update_rgfields();
714 buffer.assign(header("<radiogram>"));
715 make_rg_buffer();
716 write_rg(def_rg_filename);
717 using_rg_template = false;
718 }
719
720 const char *punctuation[] = {
721 ". ", " X ",
722 ",", " COMMA ",
723 "?", " QUERY ",
724 "\\", " BACKSLASH ",
725 "://", " COLON SLASH SLASH ",
726 "~", " TILDE ",
727 "_", " UNDERSCORE ",
728 "@", " AT ",
729 "#", " POUNDSIGN ",
730 "\"", " QUOTE ",
731 "\'", "",
732 0, 0 };
733
734 void cb_rg_check()
735 {
736 std::string temp = txt_rg_msg->buffer()->text();
737 if (temp.empty()) {
738 txt_rg_check->value("");
739 btn_rg_check->labelcolor(FL_BLACK);
740 btn_rg_check->redraw_label();
741 return;
742 }
743
744 size_t pos = std::string::npos;
745
746 if (btn_rg_standard->value()) {
747 // convert to uppercase
748 for (size_t n = 0; n < temp.length(); n++)
749 temp[n] = toupper(temp[n]);
750
751 strip_lfs(temp);
752 // remove trailing period
753 if (temp[temp.length()-1] == '.') temp.erase(temp.length()-1,1);
754 // convert punctuation
755 for (int n = 0; punctuation[n]; n += 2)
756 while ((pos = temp.find(punctuation[n])) != std::string::npos)
757 temp.replace(pos, strlen(punctuation[n]), punctuation[n+1]);
758 //convert embedded periods
759 while ((pos = temp.find(".")) != std::string::npos)
760 if (isdigit(temp[pos-1]) || isdigit(temp[pos+1]))
761 temp[pos] = 'R';
762 else
763 temp.replace(pos, 1, " DOT ");
764 }
765
766 // remove any user inserted end-of-lines
767 while ((pos = temp.find('\n')) != std::string::npos) temp[pos] = ' ';
768
769 // only single spaces no trailing spaces, no leading spaces
770 while ((pos = temp.find(" ")) != std::string::npos) temp.erase(pos,1);
771 while (temp[temp.length() -1] == ' ') temp.erase(temp.length()-1, 1);
772 if (temp[0] == ' ') temp.erase(0,1);
773
774 // count number of words in textdef_rg_filename
775 int numwords = 1;
776 if (temp.length()) {
777 pos = 0;
778 while ((pos = temp.find(" ", pos + 1)) != std::string::npos) numwords++;
779 }
780
781 // no more than specified # words to a line
782 if (numwords > progStatus.wpl) {
783 int wc = numwords;
784 size_t pos = 0;
785 while (wc > progStatus.wpl) {
786 for (int i = 0; i < progStatus.wpl; i++) pos = temp.find(' ', pos + 1);
787 temp[pos] = '\n';
788 wc -= progStatus.wpl;
789 }
790 }
791 // insert trailing end-of-line
792 temp += '\n';
793
794 // return converted text to editor
795 txt_rg_msg->clear();
796 txt_rg_msg->addstr(temp.c_str());
797
798 char snum[15];
799 snprintf(snum, sizeof(snum), "%s%d",
800 temp.find("ARL") != std::string::npos ? "ARL " : "",
801 numwords);
802 txt_rg_check->value(snum);
803 update_rgfields();
804 btn_rg_check->labelcolor(FL_BLACK);
805 btn_rg_check->redraw_label();
806 }
807
808 void cb_rg_html()
809 {
810 std::string rgname;
811 std::string html_text;
812 unsigned int nbr;
813 rgname = ICS_dir;
814 rgname.append("radiogram.html");
815
816 update_rgfields();
817 cb_rg_check();
818 std::string form;
819 if (progStatus.rri) form = rri_html_template;
820 else form = rg_html_template;
821
822 for (int i = 0; i < num_rgfields; i++) {
823 if (rgfields[i].f_type == _rg_prec) {
824 sscanf(rgfields[i].f_data.c_str(), "%u", &nbr);
825 if (nbr >= 0 && nbr < (sizeof(s_prec) / sizeof(*s_prec)))
826 html_text = s_prec[nbr];
827 else
828 html_text = s_prec[0];
829 replacestr( form, rgfields[i].f_type, html_text );
830 } else if (rgfields[i].w_type == 'b') {
831 replacestr( form, rgfields[i].f_type, rgfields[i].f_data == "T" ? yes : no);
832 } else
833 replacestr( form, rgfields[i].f_type, rgfields[i].f_data );
834 }
835
836 std::string rxstr = "";
837 rxstr.append(progStatus.my_call).append(" ").append(progStatus.my_tel);
838 rxstr.append("\n").append(progStatus.my_name);
839 rxstr.append("\n").append(progStatus.my_addr);
840 rxstr.append("\n").append(progStatus.my_city);
841 html_text = ":rx:";
842 replacestr( form, html_text, rxstr);
843
844 html_text = ":exp:";
845 std::string arlmsgs = "";
846 if (progStatus.arl_desc)
847 arlmsgs = expand_arl(rgfields[10].f_data);
848 replacestr( form, html_text, arlmsgs);
849
850 FILE *rgfile = fopen(rgname.c_str(), "w");
851 fprintf(rgfile,"%s", form.c_str());
852 fclose(rgfile);
853
854 open_url(rgname.c_str());
855 }
856
857 void cb_rg_html_fcopy()
858 {
859 std::string rgname;
860 std::string MSG = "";
861 std::string html_text;
862 unsigned int nbr;
863 rgname = ICS_dir;
864 rgname.append("rg_file_copy.html");
865
866 update_rgfields();
867 cb_rg_check();
868 std::string form;
869 if (progStatus.rri) form = rri_html_fcopy_template;
870 else form = rg_html_fcopy_template;
871
872 for (int i = 0; i < num_rgfields; i++) {
873 if (rgfields[i].f_type == _rg_prec) {
874 sscanf(rgfields[i].f_data.c_str(), "%u", &nbr);
875 if (nbr >= 0 && nbr < (sizeof(s_prec) / sizeof(*s_prec)))
876 html_text = s_prec[nbr];
877 else
878 html_text = s_prec[0];
879 replacestr( form, rgfields[i].f_type, html_text);
880 } else if (rgfields[i].w_type == 'b') {
881 replacestr( form, rgfields[i].f_type, rgfields[i].f_data == "T" ? yes : no);
882 } else
883 replacestr( form, rgfields[i].f_type, rgfields[i].f_data );
884 }
885
886 std::string rxstr = "";
887 rxstr.append(progStatus.my_call).append(" ").append(progStatus.my_tel);
888 rxstr.append("\n").append(progStatus.my_name);
889 rxstr.append("\n").append(progStatus.my_addr);
890 rxstr.append("\n").append(progStatus.my_city);
891 html_text = ":rx:";
892 replacestr( form, html_text, rxstr);
893
894 FILE *rgfile = fopen(rgname.c_str(), "w");
895 fprintf(rgfile,"%s", form.c_str());
896 fclose(rgfile);
897
898 open_url(rgname.c_str());
899 }
900
901 void cb_rg_textout()
902 {
903 std::string rgname;
904 std::string lines;
905 std::string str;
906 unsigned int nbr = 0;
907 rgname = ICS_dir;
908 rgname.append("radiogram.txt");
909
910 update_rgfields();
911 cb_rg_check();
912
913 std::string form;
914 if (progStatus.rri) form = rri_txt_template;
915 else form = rg_txt_template;
916
917 for (int i = 0; i < num_rgfields; i++) {
918 str.clear();
919 if (rgfields[i].f_type == _rg_prec) {
920 sscanf(rgfields[i].f_data.c_str(), "%u", &nbr);
921 if (nbr < 0) nbr = 0;
922 if (nbr >= sizeof(s_prec)/sizeof(*s_prec)) nbr = 0;
923 str = s_prec[nbr];
924 if (str.find("TEST") != std::string::npos) { // test message
925 if (str.find("EMERGENCY") == std::string::npos)
926 str = str.substr(0, 6);
927 } else {
928 if (str.find("EMERGENCY") == std::string::npos)
929 str = str[0];
930 }
931 replacestr( form, rgfields[i].f_type, str);
932 } else if (rgfields[i].w_type == 'e' || rgfields[i].w_type == 't') {
933 if (rgfields[i].f_type == _rg_opnote || rgfields[i].f_type == _rg_opnote2) {
934 if (!rgfields[i].f_data.empty())
935 str.append("\nOPNOTE ").append(rgfields[i].f_data);
936 } else if (rgfields[i].f_type == _rg_hx && !rgfields[i].f_data.empty()) {
937 str = " ";
938 str.append(rgfields[i].f_data);
939 } else {
940 str = rgfields[i].f_data;
941 strip_lfs(str);
942 }
943 replacestr( form, rgfields[i].f_type, str );
944 } else
945 replacestr( form, rgfields[i].f_type, rgfields[i].f_data );
946 }
947
948 FILE *rgfile = fopen(rgname.c_str(), "w");
949 fprintf(rgfile,"%s", form.c_str());
950 fclose(rgfile);
951 open_url(rgname.c_str());
952 }
953