Debian Codegraph @

src/ics/ics216.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
61 #ifdef WIN32
62 # include "flmsgrc.h"
63 # include "compat.h"
64 # define dirent fl_dirent_no_thanks
65 #endif
66
67 #include <FL/filename.H>
68 #include "dirent-check.h"
69
70 #include <FL/x.H>
71 #include <FL/Fl_Pixmap.H>
72 #include <FL/Fl_Image.H>
73
74 // ---------------------------------------------------------------------
75 // ics 216 field variables and template variables
76 // ---------------------------------------------------------------------
77
78 std::string ics216_incident = ":inc:";
79 std::string ics216_date = ":dat:";
80 std::string ics216_time = ":tim:";
81 std::string ics216_branch = ":br:";
82 std::string ics216_agc = ":agc:";
83 std::string ics216_op_period = ":opp:";
84 std::string ics216_tac_freq = ":tfr:";
85
86 std::string ics216_div_grp = ":div[n]:";
87 std::string ics216_agency = ":agy[n]:";
88
89 std::string ics216_ag = ":ag[n]:";
90 std::string ics216_id = ":id[n]:";
91 std::string ics216_rr = ":rr[n]:";
92
93 std::string ics216_prepared_by = ":pre:";
94
95 std::string s216_incident;
96 std::string s216_date;
97 std::string s216_time;
98 std::string s216_branch;
99 std::string s216_agc;
100 std::string s216_op_period;
101 std::string s216_tac_freq;
102
103 std::string s216_div_grp[4];
104 std::string s216_agency[4];
105 std::string s216_ag[36];
106 std::string s216_id[36];
107 std::string s216_rr[36];
108
109 std::string s216_prepared_by;
110
111 // =====================================================================
112
113 std::string buff216;
114 std::string def_216_filename = "";
115 std::string base_216_filename = "";
116 std::string def_216_TemplateName = "";
117 bool using_ics216_template = false;
118
119 void cb_216_set_date()
120 {
121 txt_216_date->value(szDate(progStatus.dtformat));
122 }
123
124 void cb_216_set_time()
125 {
126 txt_216_time->value(szTime(progStatus.UTC));
127 }
128
129 void clear_216fields()
130 {
131 s216_incident.clear();
132 s216_date.clear();
133 s216_time.clear();
134 s216_branch.clear();
135 s216_agc.clear();
136 s216_op_period.clear();
137 s216_tac_freq.clear();
138 s216_prepared_by.clear();
139
140 for (int i = 0; i < 4; i++) {
141 s216_div_grp[i].clear();
142 s216_agency[i].clear();
143 }
144 for (int i = 0; i < 36; i++) {
145 s216_ag[i].clear();
146 s216_id[i].clear();
147 s216_rr[i].clear();
148 }
149 }
150
151 bool check_216fields()
152 {
153 if (s216_incident != txt_216_incident->value())
154 return true;
155 if (s216_date != txt_216_date->value())
156 return true;
157 if (s216_time != txt_216_time->value())
158 return true;
159 if (s216_branch != txt_216_branch->value())
160 return true;
161 if (s216_agc != txt_216_agc->value())
162 return true;
163 if (s216_op_period != txt_216_op_period->value())
164 return true;
165 if (s216_tac_freq != txt_216_tac_freq->value())
166 return true;
167 if (s216_prepared_by != txt_216_prepared_by->value())
168 return true;
169
170 for (int i = 0; i < 4; i++) {
171 if (s216_div_grp[i] != txt_216_div_grp[i]->value())
172 return true;
173 if (s216_agency[i] != txt_216_agency[i]->value())
174 return true;
175 }
176
177 for (int i = 0; i < 36; i++) {
178 if (s216_ag[i] != txt_216_ag[i]->value())
179 return true;
180 if (s216_id[i] != txt_216_id[i]->value())
181 return true;
182 if (s216_rr[i] != txt_216_rr[i]->value())
183 return true;
184 }
185 return false;
186 }
187
188 void update_216fields()
189 {
190 s216_incident = txt_216_incident->value();
191 s216_date = txt_216_date->value();
192 s216_time = txt_216_time->value();
193 s216_branch = txt_216_branch->value();
194 s216_agc = txt_216_agc->value();
195 s216_op_period = txt_216_op_period->value();
196 s216_tac_freq = txt_216_tac_freq->value();
197 s216_prepared_by = txt_216_prepared_by->value();
198
199 for (int i = 0; i < 4; i++) {
200 s216_div_grp[i] = txt_216_div_grp[i]->value();
201 s216_agency[i] = txt_216_agency[i]->value();
202 }
203
204 for (int i = 0; i < 36; i++) {
205 s216_ag[i] = txt_216_ag[i]->value();
206 s216_id[i] = txt_216_id[i]->value();
207 s216_rr[i] = txt_216_rr[i]->value();
208 }
209 }
210
211 void update_216form()
212 {
213 txt_216_incident->value(s216_incident.c_str());
214 txt_216_date->value(s216_date.c_str());
215 txt_216_time->value(s216_time.c_str());
216 txt_216_branch->value(s216_branch.c_str());
217 txt_216_agc->value(s216_agc.c_str());
218 txt_216_op_period->value(s216_op_period.c_str());
219 txt_216_tac_freq->value(s216_tac_freq.c_str());
220 txt_216_prepared_by->value(s216_prepared_by.c_str());
221
222 for (int i = 0; i < 4; i++) {
223 txt_216_div_grp[i]->value(s216_div_grp[i].c_str());
224 txt_216_agency[i]->value(s216_agency[i].c_str());
225 }
226 for (int i = 0; i < 36; i++) {
227 txt_216_ag[i]->value(s216_ag[i].c_str());
228 txt_216_id[i]->value(s216_id[i].c_str());
229 txt_216_rr[i]->value(s216_rr[i].c_str());
230 }
231 }
232
233 void clear_216_form()
234 {
235 clear_216fields();
236 update_216form();
237 }
238
239 std::string &ics_216_nn(std::string & subst, int n)
240 {
241 static std::string garbage = "#$^*!";
242 static std::string ics;
243 ics.clear();
244 ics = subst;
245 size_t pos = ics.find("[");
246 if (pos == std::string::npos) return garbage;
247 pos++;
248 if (n < 10)
249 ics[pos] = '0' + n;
250 else {
251 if (n < 20)
252 ics[pos] = '1';
253 else if ( n < 30)
254 ics[pos] = '2';
255 else if (n < 40)
256 ics[pos] = '3';
257 ics[pos+1] = '0' + n % 10;
258 ics[pos+2] = ']';
259 ics += ':';
260 }
261 return ics;
262 }
263
264 void make_buff216(bool compress = false)
265 {
266 std::string mbuff;
267 mbuff.clear();
268 mbuff.append( lineout( ics216_incident, s216_incident ) );
269 mbuff.append( lineout( ics216_date, s216_date ) );
270 mbuff.append( lineout( ics216_time, s216_time ) );
271 mbuff.append( lineout( ics216_branch, s216_branch ) );
272 mbuff.append( lineout( ics216_agc, s216_agc ) );
273 mbuff.append( lineout( ics216_op_period, s216_op_period ) );
274 mbuff.append( lineout( ics216_tac_freq, s216_tac_freq ) );
275 mbuff.append( lineout( ics216_prepared_by, s216_prepared_by ) );
276
277 for (int i = 0; i < 4; i++) {
278 mbuff.append( lineout( ics_216_nn( ics216_div_grp, i), s216_div_grp[i] ) );
279 mbuff.append( lineout( ics_216_nn( ics216_agency, i), s216_agency[i] ) );
280 }
281 for (int i = 0; i < 36; i++) {
282 mbuff.append( lineout( ics_216_nn( ics216_ag, i), s216_ag[i] ) );
283 mbuff.append( lineout( ics_216_nn( ics216_id, i), s216_id[i] ) );
284 mbuff.append( lineout( ics_216_nn( ics216_rr, i), s216_rr[i] ) );
285 }
286 if (compress) compress_maybe(mbuff);
287 buff216.append(mbuff);
288 }
289
290 void read_216_buffer(std::string data)
291 {
292 clear_216fields();
293 read_header(data);
294
295 s216_incident = findstr( data, ics216_incident );
296 s216_date = findstr( data, ics216_date );
297 s216_time = findstr( data, ics216_time );
298 s216_branch = findstr( data, ics216_branch );
299 s216_agc = findstr( data, ics216_agc );
300 s216_op_period = findstr( data, ics216_op_period );
301 s216_tac_freq = findstr( data, ics216_tac_freq );
302 s216_prepared_by = findstr( data, ics216_prepared_by );
303
304 for (int i = 0; i < 4; i++) {
305 s216_div_grp[i] = findstr( data, ics_216_nn( ics216_div_grp, i ) );
306 s216_agency[i] = findstr( data, ics_216_nn( ics216_agency, i ) );
307 }
308 for (int i = 0; i < 36; i++) {
309 s216_ag[i] = findstr( data, ics_216_nn( ics216_ag, i ) );
310 s216_id[i] = findstr( data, ics_216_nn( ics216_id, i ) );
311 s216_rr[i] = findstr( data, ics_216_nn( ics216_rr, i ) );
312 }
313
314 update_216form();
315 }
316
317 void cb_216_new()
318 {
319 if (check_216fields()) {
320 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
321 update_header(CHANGED);
322 cb_216_save();
323 }
324 }
325 clear_216_form();
326 clear_header();
327 def_216_filename = ICS_msg_dir;
328 def_216_filename.append("new").append(F216_EXT);
329 show_filename(def_216_filename);
330 using_ics216_template = false;
331 }
332
333 void cb_216_import()
334 {
335 fl_alert2("Not implemented");
336 }
337
338 void cb_216_export()
339 {
340 fl_alert2("Not implemented");
341 }
342
343 void cb_216_wrap_import(std::string wrapfilename, std::string inpbuffer)
344 {
345 clear_216_form();
346 read_216_buffer(inpbuffer);
347 def_216_filename = ICS_msg_dir;
348 def_216_filename.append(wrapfilename);
349 show_filename(def_216_filename);
350 using_ics216_template = false;
351 }
352
353 int eval_216_fsize()
354 {
355 Ccrc16 chksum;
356 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
357 evalstr.append(base_216_filename).append("]");
358 update_216fields();
359 update_header(FROM);
360 evalstr.append(header("<ics216>"));
361 buff216.clear();
362 make_buff216(true);
363 if (buff216.empty()) return 0;
364 compress_maybe( buff216 );
365 evalstr.append( buff216 );
366 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
367 return evalstr.length();
368 }
369
370 void cb_216_wrap_export()
371 {
372 if (check_216fields()) {
373 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
374 return;
375 update_header(CHANGED);
376 }
377 update_216fields();
378
379 if (base_216_filename == std::string("new").append(F216_EXT) ||
380 base_216_filename == std::string("default").append(F216_EXT) )
381 if (!cb_216_save_as()) return;
382
383 std::string wrapfilename = WRAP_send_dir;
384 wrapfilename.append(base_216_filename);
385 wrapfilename.append(".wrap");
386 const char *p = FSEL::saveas(
387 "Save as wrap file",
388 "Wrap file\t*.{wrap,WRAP}",
389 wrapfilename.c_str());
390 if (p) {
391 std::string pext = fl_filename_ext(p);
392 wrapfilename = p;
393 update_header(FROM);
394 buff216.assign(header("<ics216>"));
395 make_buff216(true);
396 export_wrapfile(base_216_filename, wrapfilename, buff216, pext != ".wrap");
397
398 buff216.assign(header("<ics216>"));
399 make_buff216(false);
400 write_216(def_216_filename);
401 }
402 }
403
404 void cb_216_wrap_autosend()
405 {
406 if (check_216fields()) {
407 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
408 return;
409 update_header(CHANGED);
410 }
411 update_216fields();
412
413 if (base_216_filename == std::string("new").append(F216_EXT) ||
414 base_216_filename == std::string("default").append(F216_EXT) )
415 if (!cb_216_save_as()) return;
416
417 update_header(FROM);
418 buff216.assign(header("<ics216>"));
419 make_buff216(true);
420 xfr_via_socket(base_216_filename, buff216);
421
422 buff216.assign(header("<ics216>"));
423 make_buff216(false);
424 write_216(def_216_filename);
425 }
426
427 void cb_216_load_template()
428 {
429 std::string def_216_filename = def_216_TemplateName;
430 const char *p = FSEL::select(
431 "Open template file",
432 std::string("Template file\t*").append(T216_EXT).c_str(),
433 def_216_filename.c_str());
434 if (p) {
435 clear_216_form();
436 read_data_file(p);
437 def_216_TemplateName = p;
438 show_filename(def_216_TemplateName);
439 using_ics216_template = true;
440 }
441 }
442
443 void cb_216_save_template()
444 {
445 if (!using_ics216_template) {
446 cb_216_save_as_template();
447 return;
448 }
449 std::string def_216_filename = def_216_TemplateName;
450 const char *p = FSEL::saveas(
451 "Save template file",
452 std::string("Template file\t*").append(T216_EXT).c_str(),
453 def_216_filename.c_str());
454 if (p) {
455 update_header(CHANGED);
456 buff216.assign(header("<ics216>"));
457 make_buff216();
458 write_216(p);
459 }
460 }
461
462 void cb_216_save_as_template()
463 {
464 std::string def_216_filename = def_216_TemplateName;
465 const char *p = FSEL::saveas(
466 "Save as template file",
467 std::string("Template file\t*").append(T216_EXT).c_str(),
468 def_216_filename.c_str());
469 if (p) {
470 const char *pext = fl_filename_ext(p);
471 def_216_TemplateName = p;
472 if (strlen(pext) == 0) def_216_TemplateName.append(T216_EXT);
473 remove_spaces_from_filename(def_216_TemplateName);
474 clear_header();
475 update_header(CHANGED);
476 buff216.assign(header("<ics216>"));
477 make_buff216();
478 write_216(def_216_TemplateName);
479 show_filename(def_216_TemplateName);
480 using_ics216_template = true;
481 }
482 }
483
484 void cb_216_open()
485 {
486 const char *p = FSEL::select(
487 _("Open data file"),
488 std::string("ICS-216\t*").append(F216_EXT).c_str(),
489 def_216_filename.c_str());
490 if (!p) return;
491 if (strlen(p) == 0) return;
492 clear_216_form();
493 read_data_file(p);
494 using_ics216_template = false;
495 def_216_filename = p;
496 show_filename(def_216_filename);
497 }
498
499 void write_216(std::string s)
500 {
501 FILE *file216 = fopen(s.c_str(), "w");
502 if (!file216) return;
503
504 fwrite(buff216.c_str(), buff216.length(), 1, file216);
505 fclose(file216);
506 }
507
508 bool cb_216_save_as()
509 {
510 const char *p;
511 std::string newfilename;
512
513 std::string name = named_file();
514 if (!name.empty()) {
515 name.append(F216_EXT);
516 newfilename = ICS_msg_dir;
517 newfilename.append(name);
518 } else
519 newfilename = def_216_filename;
520
521 p = FSEL::saveas(
522 _("Save data file"),
523 std::string("ICS-216\t*").append(F216_EXT).c_str(),
524 newfilename.c_str());
525
526 if (!p) return false;
527 if (strlen(p) == 0) return false;
528
529 if (progStatus.sernbr_fname) update_sernbr();
530
531 const char *pext = fl_filename_ext(p);
532 def_216_filename = p;
533 if (strlen(pext) == 0) def_216_filename.append(F216_EXT);
534
535 remove_spaces_from_filename(def_216_filename);
536 update_216fields();
537 update_header(NEW);
538 buff216.assign(header("<ics216>"));
539 make_buff216();
540 write_216(def_216_filename);
541
542 using_ics216_template = false;
543 show_filename(def_216_filename);
544 return true;
545 }
546
547 void cb_216_save()
548 {
549 if (base_216_filename == std::string("new").append(F216_EXT) ||
550 base_216_filename == std::string("default").append(F216_EXT) ||
551 using_ics216_template == true) {
552 cb_216_save_as();
553 return;
554 }
555 if (check_216fields()) update_header(CHANGED);
556 update_216fields();
557 buff216.assign(header("<ics216>"));
558 make_buff216();
559 write_216(def_216_filename);
560 using_ics216_template = false;
561 }
562
563 void cb_216_html()
564 {
565 std::string fname_name = fl_filename_name(def_216_filename.c_str());
566 size_t p = fname_name.rfind('.');
567 if (p != std::string::npos) fname_name.erase(p);
568
569 std::string ics216_fname = ICS_dir;
570 ics216_fname.append(fname_name);
571 ics216_fname.append(".html");
572
573 std::string html_text = "";
574
575 update_216fields();
576 std::string form216 = ics216_html_template;
577
578 replacestr(form216, TITLE, fname_name);
579 replacestr(form216, ics216_incident, s216_incident );
580 replacestr(form216, ics216_date, s216_date );
581 replacestr(form216, ics216_time, s216_time );
582 replacestr(form216, ics216_branch, s216_branch );
583 replacestr(form216, ics216_agc, s216_agc );
584 replacestr(form216, ics216_op_period, s216_op_period );
585 replacestr(form216, ics216_tac_freq, s216_tac_freq );
586 replacestr(form216, ics216_prepared_by, s216_prepared_by );
587
588 for (int i = 0; i < 4; i++) {
589 replacestr(form216, ics_216_nn( ics216_div_grp, i), s216_div_grp[i] );
590 replacestr(form216, ics_216_nn( ics216_agency, i), s216_agency[i] );
591 }
592 for (int i = 0; i < 36; i++) {
593 replacestr(form216, ics_216_nn( ics216_ag, i), s216_ag[i] );
594 replacestr(form216, ics_216_nn( ics216_id, i), s216_id[i] );
595 replacestr(form216, ics_216_nn( ics216_rr, i), s216_rr[i] );
596 }
597
598 FILE *file216 = fopen(ics216_fname.c_str(), "w");
599 fprintf(file216,"%s", form216.c_str());
600 fclose(file216);
601
602 open_url(ics216_fname.c_str());
603 }
604
605 void cb_216_msg_type()
606 {
607 if (tabs_msg_type->value() == tab_ics216 ) {
608 tab_ics216_type->value(tab_216_1);
609 show_filename(def_216_filename);
610 }
611 }
612
613 void cb_216_textout()
614 {
615 std::string ics216_fname = ICS_dir;
616 ics216_fname.append("ics216.txt");
617
618 update_216fields();
619 std::string form216 = ics216_text_template;
620
621 replacestr(form216, ics216_incident, s216_incident );
622 replacestr(form216, ics216_date, s216_date );
623 replacestr(form216, ics216_time, s216_time );
624 replacestr(form216, ics216_branch, s216_branch );
625 replacestr(form216, ics216_agc, s216_agc );
626 replacestr(form216, ics216_op_period, s216_op_period );
627 replacestr(form216, ics216_tac_freq, s216_tac_freq );
628 replacestr(form216, ics216_prepared_by, s216_prepared_by );
629
630 for (int i = 0; i < 4; i++) {
631 replacestr(form216, ics_216_nn( ics216_div_grp, i), s216_div_grp[i] );
632 replacestr(form216, ics_216_nn( ics216_agency, i), s216_agency[i] );
633 }
634 for (int i = 0; i < 36; i++) {
635 replacestr(form216, ics_216_nn( ics216_ag, i), s216_ag[i] );
636 replacestr(form216, ics_216_nn( ics216_id, i), s216_id[i] );
637 replacestr(form216, ics_216_nn( ics216_rr, i), s216_rr[i] );
638 }
639
640 FILE *file216 = fopen(ics216_fname.c_str(), "w");
641 fprintf(file216,"%s", form216.c_str());
642 fclose(file216);
643
644 open_url(ics216_fname.c_str());
645 }