Debian Codegraph @

src/ics/ics309.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 309 field variables and template variables
76 // ---------------------------------------------------------------------
77
78 std::string ics309_incident = ":inc:";
79 std::string ics309_date_fm = ":dfm:";
80 std::string ics309_time_fm = ":tfm:";
81 std::string ics309_date_to = ":dto:";
82 std::string ics309_time_to = ":tto:";
83 std::string ics309_prepared_by = ":pre:";
84 std::string ics309_preparer_date_time = ":dtm:";
85 std::string ics309_radio_net = ":net:";
86 std::string ics309_radio_operator = ":opr:";
87
88 std::string ics309_comm_msg = ":msg[n]:"; // 34
89 std::string ics309_comm_to = ":to[n]:"; // 34
90 std::string ics309_comm_from = ":fm[n]:"; // 34
91 std::string ics309_comm_time = ":tm[n]:"; // 34
92
93 std::string s309_incident;
94 std::string s309_date_fm;
95 std::string s309_time_fm;
96 std::string s309_date_to;
97 std::string s309_time_to;
98 std::string s309_prepared_by;
99 std::string s309_preparer_date_time;
100 std::string s309_radio_net;
101 std::string s309_radio_operator;
102
103 std::string s309_comm_time[34];
104 std::string s309_comm_from[34];
105 std::string s309_comm_msg[34];
106 std::string s309_comm_to[34];
107
108 // =====================================================================
109
110 std::string buff309;
111 std::string def_309_filename = "";
112 std::string base_309_filename = "";
113 std::string def_309_TemplateName = "";
114 bool using_ics309_template = false;
115
116 void cb_309_set_date_fm()
117 {
118 txt_309_date_fm->value(szDate(progStatus.dtformat));
119 }
120
121 void cb_309_set_time_fm()
122 {
123 txt_309_time_fm->value(szTime(progStatus.UTC));
124 }
125
126 void cb_309_set_date_to()
127 {
128 txt_309_date_to->value(szDate(progStatus.dtformat));
129 }
130
131 void cb_309_set_time_to()
132 {
133 txt_309_time_to->value(szTime(progStatus.UTC));
134 }
135
136 void cb_309_set_date_time()
137 {
138 std::string dt = szDate(progStatus.dtformat);
139 dt.append(", ").append(szTime(progStatus.UTC));
140 txt_309_preparer_date_time->value(dt.c_str());
141 }
142
143 void clear_309fields()
144 {
145 s309_incident.clear();
146 s309_date_fm.clear();
147 s309_time_fm.clear();
148 s309_date_to.clear();
149 s309_time_to.clear();
150 s309_prepared_by.clear();
151 s309_preparer_date_time.clear();
152 s309_radio_net.clear();
153 s309_radio_operator.clear();
154
155 for (int i = 0; i < 34; i++) {
156 s309_comm_msg[i].clear();
157 s309_comm_to[i].clear();
158 s309_comm_from[i].clear();
159 }
160 }
161
162 bool check_309fields()
163 {
164 if (s309_incident != txt_309_incident->value())
165 return true;
166 if (s309_date_fm != txt_309_date_fm->value())
167 return true;
168 if (s309_time_fm != txt_309_time_fm->value())
169 return true;
170 if (s309_date_to != txt_309_date_to->value())
171 return true;
172 if (s309_time_to != txt_309_time_to->value())
173 return true;
174 if (s309_prepared_by != txt_309_prepared_by->value())
175 return true;
176 if (s309_radio_net != txt_309_radio_net->value())
177 return true;
178 if (s309_radio_operator != txt_309_radio_operator->value())
179 return true;
180 if (s309_preparer_date_time != txt_309_preparer_date_time->value())
181 return true;
182 for (int i = 0; i < 34; i++) {
183 if (s309_comm_time[i] != txt_309_comm_time[i]->value())
184 return true;
185 if (s309_comm_msg[i] != txt_309_comm_msg[i]->value())
186 return true;
187 if (s309_comm_to[i] != txt_309_comm_to[i]->value())
188 return true;
189 if (s309_comm_from[i] != txt_309_comm_from[i]->value())
190 return true;
191 }
192 return false;
193 }
194
195 void update_309fields()
196 {
197 s309_incident = txt_309_incident->value();
198 s309_date_fm = txt_309_date_fm->value();
199 s309_time_fm = txt_309_time_fm->value();
200 s309_date_to = txt_309_date_to->value();
201 s309_time_to = txt_309_time_to->value();
202 s309_prepared_by = txt_309_prepared_by->value();
203 s309_radio_net = txt_309_radio_net->value();
204 s309_radio_operator = txt_309_radio_operator->value();
205 s309_preparer_date_time = txt_309_preparer_date_time->value();
206 for (int i = 0; i < 34; i++) {
207 s309_comm_time[i] = txt_309_comm_time[i]->value();
208 s309_comm_from[i] = txt_309_comm_from[i]->value();
209 s309_comm_to[i] = txt_309_comm_to[i]->value();
210 s309_comm_msg[i] = txt_309_comm_msg[i]->value();
211 }
212 }
213
214 void update_309form()
215 {
216 txt_309_incident->value(s309_incident.c_str());
217 txt_309_date_fm->value(s309_date_fm.c_str());
218 txt_309_time_fm->value(s309_time_fm.c_str());
219 txt_309_date_to->value(s309_date_to.c_str());
220 txt_309_time_to->value(s309_time_to.c_str());
221 txt_309_prepared_by->value(s309_prepared_by.c_str());
222 txt_309_radio_net->value(s309_radio_net.c_str());
223 txt_309_radio_operator->value(s309_radio_operator.c_str());
224 txt_309_preparer_date_time->value(s309_preparer_date_time.c_str());
225
226 for (int i = 0; i < 34; i++) {
227 txt_309_comm_time[i]->value(s309_comm_time[i].c_str());
228 txt_309_comm_to[i]->value(s309_comm_to[i].c_str());
229 txt_309_comm_from[i]->value(s309_comm_from[i].c_str());
230 txt_309_comm_msg[i]->value(s309_comm_msg[i].c_str());
231 }
232 }
233
234 void clear_309_form()
235 {
236 clear_309fields();
237 update_309form();
238 }
239
240 std::string &ics309_nn(std::string & subst, int n)
241 {
242 static std::string garbage = "#$^*!";
243 static std::string ics;
244 ics.clear();
245 ics = subst;
246 size_t pos = ics.find("[");
247 if (pos == std::string::npos) return garbage;
248 pos++;
249 if (n < 10)
250 ics[pos] = '0' + n;
251 else {
252 ics[pos] = n > 29 ? '3' : n > 19 ? '2' : '1';
253 ics[pos+1] = '0' + n % 10;
254 ics[pos+2] = ']';
255 ics += ':';
256 }
257 return ics;
258 }
259
260 void make_buff309(bool compress = false)
261 {
262 std::string mbuff;
263 mbuff.clear();
264 mbuff.append( lineout( ics309_incident, s309_incident ) );
265 mbuff.append( lineout( ics309_date_fm, s309_date_fm ) );
266 mbuff.append( lineout( ics309_time_fm, s309_time_fm ) );
267 mbuff.append( lineout( ics309_date_to, s309_date_to ) );
268 mbuff.append( lineout( ics309_time_to, s309_time_to ) );
269 mbuff.append( lineout( ics309_prepared_by, s309_prepared_by ) );
270 mbuff.append( lineout( ics309_preparer_date_time, s309_preparer_date_time ) );
271 mbuff.append( lineout( ics309_radio_net, s309_radio_net ) );
272 mbuff.append( lineout( ics309_radio_operator, s309_radio_operator ) );
273
274 for (int i = 0; i < 34; i++) {
275 mbuff.append( lineout( ics309_nn( ics309_comm_time, i ), s309_comm_time[i] ) );
276 mbuff.append( lineout( ics309_nn( ics309_comm_to, i ), s309_comm_to[i] ) );
277 mbuff.append( lineout( ics309_nn( ics309_comm_from, i ), s309_comm_from[i] ) );
278 mbuff.append( lineout( ics309_nn( ics309_comm_msg, i ), s309_comm_msg[i] ) );
279 }
280 if (compress) compress_maybe(mbuff);
281 buff309.append(mbuff);
282 }
283
284 void read_309_buffer(std::string data)
285 {
286 clear_309fields();
287 read_header(data);
288
289 s309_incident = findstr( data, ics309_incident );
290 s309_date_fm = findstr( data, ics309_date_fm );
291 s309_time_fm = findstr( data, ics309_time_fm );
292 s309_date_to = findstr( data, ics309_date_to );
293 s309_time_to = findstr( data, ics309_time_to );
294 s309_prepared_by = findstr( data, ics309_prepared_by );
295 s309_preparer_date_time = findstr( data, ics309_preparer_date_time );
296 s309_radio_net = findstr( data, ics309_radio_net );
297 s309_radio_operator = findstr( data, ics309_radio_operator );
298
299 for (int i = 0; i < 34; i++) {
300 s309_comm_time[i] = findstr( data, ics309_nn( ics309_comm_time, i ) );
301 s309_comm_to[i] = findstr( data, ics309_nn( ics309_comm_to, i ) );
302 s309_comm_from[i] = findstr( data, ics309_nn( ics309_comm_from, i ) );
303 s309_comm_msg[i] = findstr( data, ics309_nn( ics309_comm_msg, i ) );
304 }
305
306 update_309form();
307 }
308
309 void cb_309_new()
310 {
311 if (check_309fields()) {
312 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
313 update_header(CHANGED);
314 cb_309_save();
315 }
316 }
317 clear_309_form();
318 clear_header();
319 def_309_filename = ICS_msg_dir;
320 def_309_filename.append("new").append(F309_EXT);
321 show_filename(def_309_filename);
322 using_ics309_template = false;
323 }
324
325 void cb_309_import()
326 {
327 fl_alert2("Not implemented");
328 }
329
330 void cb_309_export()
331 {
332 fl_alert2("Not implemented");
333 }
334
335 void cb_309_wrap_import(std::string wrapfilename, std::string inpbuffer)
336 {
337 clear_309_form();
338 read_309_buffer(inpbuffer);
339 def_309_filename = ICS_msg_dir;
340 def_309_filename.append(wrapfilename);
341 show_filename(def_309_filename);
342 using_ics309_template = false;
343 }
344
345 int eval_309_fsize()
346 {
347 Ccrc16 chksum;
348 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
349 evalstr.append(base_309_filename).append("]");
350 update_309fields();
351 update_header(FROM);
352 evalstr.append(header("<ics309>"));
353 buff309.clear();
354 make_buff309(true);
355 if (buff309.empty()) return 0;
356 compress_maybe( buff309 );
357 evalstr.append( buff309 );
358 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
359 return evalstr.length();
360 }
361
362 void cb_309_wrap_export()
363 {
364 if (check_309fields()) {
365 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
366 return;
367 update_header(CHANGED);
368 }
369 update_309fields();
370
371 if (base_309_filename == std::string("new").append(F309_EXT) ||
372 base_309_filename == std::string("default").append(F309_EXT) )
373 if (!cb_309_save_as()) return;
374
375 std::string wrapfilename = WRAP_send_dir;
376 wrapfilename.append(base_309_filename);
377 wrapfilename.append(".wrap");
378 const char *p = FSEL::saveas(
379 "Save as wrap file",
380 "Wrap file\t*.{wrap,WRAP}",
381 wrapfilename.c_str());
382 if (p) {
383 std::string pext = fl_filename_ext(p);
384 wrapfilename = p;
385 update_header(FROM);
386 buff309.assign(header("<ics309>"));
387 make_buff309(true);
388 export_wrapfile(base_309_filename, wrapfilename, buff309, pext != ".wrap");
389 write_309(def_309_filename);
390 }
391 }
392
393 void cb_309_wrap_autosend()
394 {
395 if (check_309fields()) {
396 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
397 return;
398 update_header(CHANGED);
399 }
400 update_309fields();
401
402 if (base_309_filename == std::string("new").append(F309_EXT) ||
403 base_309_filename == std::string("default").append(F309_EXT) )
404 cb_309_save_as();
405
406 update_header(FROM);
407 buff309.assign(header("<ics309>"));
408 make_buff309(true);
409
410 xfr_via_socket(base_309_filename, buff309);
411 write_309(def_309_filename);
412 }
413
414 void cb_309_load_template()
415 {
416 std::string def_309_filename = def_309_TemplateName;
417 const char *p = FSEL::select(
418 "Open template file",
419 std::string("Template file\t*").append(T309_EXT).c_str(),
420 def_309_filename.c_str());
421 if (p) {
422 clear_309_form();
423 read_data_file(p);
424 def_309_TemplateName = p;
425 show_filename(def_309_TemplateName);
426 using_ics309_template = true;
427 }
428 }
429
430 void cb_309_save_template()
431 {
432 if (!using_ics309_template) {
433 cb_309_save_as_template();
434 return;
435 }
436 std::string def_309_filename = def_309_TemplateName;
437 const char *p = FSEL::saveas(
438 "Save template file",
439 std::string("Template file\t*").append(T309_EXT).c_str(),
440 def_309_filename.c_str());
441 if (p) {
442 update_header(CHANGED);
443 buff309.assign(header("<ics309>"));
444 make_buff309();
445 write_309(p);
446 }
447 }
448
449 void cb_309_save_as_template()
450 {
451 std::string def_309_filename = def_309_TemplateName;
452 const char *p = FSEL::saveas(
453 "Save as template file",
454 std::string("Template file\t*").append(T309_EXT).c_str(),
455 def_309_filename.c_str());
456 if (p) {
457 const char *pext = fl_filename_ext(p);
458 def_309_TemplateName = p;
459 if (strlen(pext) == 0) def_309_TemplateName.append(T309_EXT);
460 remove_spaces_from_filename(def_309_TemplateName);
461 clear_header();
462 update_header(CHANGED);
463 buff309.assign(header("<ics309>"));
464 make_buff309();
465 write_309(def_309_TemplateName);
466 show_filename(def_309_TemplateName);
467 using_ics309_template = true;
468 }
469 }
470
471 void cb_309_open()
472 {
473 const char *p = FSEL::select(
474 _("Open data file"),
475 std::string("ICS-309\t*").append(F309_EXT).c_str(),
476 def_309_filename.c_str());
477 if (!p) return;
478 if (strlen(p) == 0) return;
479 clear_309_form();
480 read_data_file(p);
481 using_ics309_template = false;
482 def_309_filename = p;
483 show_filename(def_309_filename);
484 }
485
486 void write_309(std::string s)
487 {
488 FILE *file309 = fopen(s.c_str(), "w");
489 if (!file309) return;
490
491 fwrite(buff309.c_str(), buff309.length(), 1, file309);
492 fclose(file309);
493 }
494
495 bool cb_309_save_as()
496 {
497 const char *p;
498 std::string newfilename;
499
500 std::string name = named_file();
501 if (!name.empty()) {
502 name.append(F309_EXT);
503 newfilename = ICS_msg_dir;
504 newfilename.append(name);
505 } else
506 newfilename = def_309_filename;
507
508 p = FSEL::saveas(
509 _("Save data file"),
510 std::string("ICS-309\t*").append(F309_EXT).c_str(),
511 newfilename.c_str());
512
513 if (!p) return false;
514 if (strlen(p) == 0) return false;
515
516 if (progStatus.sernbr_fname) update_sernbr();
517
518 const char *pext = fl_filename_ext(p);
519 def_309_filename = p;
520 if (strlen(pext) == 0) def_309_filename.append(F309_EXT);
521
522 remove_spaces_from_filename(def_309_filename);
523 update_309fields();
524 update_header(NEW);
525 buff309.assign(header("<ics309>"));
526 make_buff309();
527 write_309(def_309_filename);
528
529 using_ics309_template = false;
530 show_filename(def_309_filename);
531 return true;
532 }
533
534 void cb_309_save()
535 {
536 if (base_309_filename == std::string("new").append(F309_EXT) ||
537 base_309_filename == std::string("default").append(F309_EXT) ||
538 using_ics309_template == true) {
539 cb_309_save_as();
540 return;
541 }
542 if (check_309fields()) update_header(CHANGED);
543 update_309fields();
544 buff309.assign(header("<ics309>"));
545 make_buff309();
546 write_309(def_309_filename);
547 using_ics309_template = false;
548 }
549
550 void cb_309_html()
551 {
552 std::string fname_name = fl_filename_name(def_309_filename.c_str());
553 size_t p = fname_name.rfind('.');
554 if (p != std::string::npos) fname_name.erase(p);
555
556 std::string ics309_fname = ICS_dir;
557 ics309_fname.append(fname_name);
558 ics309_fname.append(".html");
559
560 std::string html_text = "";
561 std::string empty = "<br>";
562
563 update_309fields();
564 std::string form309 = ics309_html_template;
565
566 replacestr(form309, TITLE, fname_name);
567 replacestr(form309, ics309_incident, s309_incident );
568 replacestr(form309, ics309_date_fm, s309_date_fm );
569 replacestr(form309, ics309_time_fm, s309_time_fm );
570 replacestr(form309, ics309_date_to, s309_date_to );
571 replacestr(form309, ics309_time_to, s309_time_to );
572 replacestr(form309, ics309_prepared_by, s309_prepared_by );
573 replacestr(form309, ics309_preparer_date_time, s309_preparer_date_time );
574 replacestr(form309, ics309_radio_net, s309_radio_net );
575 replacestr(form309, ics309_radio_operator, s309_radio_operator );
576
577 for (int i = 0; i < 34; i++) {
578 replacestr(form309, ics309_nn( ics309_comm_time, i ),
579 s309_comm_time[i].empty() ? empty : s309_comm_time[i] );
580 replacestr(form309, ics309_nn( ics309_comm_to, i ),
581 s309_comm_to[i].empty() ? empty : s309_comm_to[i] );
582 replacestr(form309, ics309_nn( ics309_comm_from, i ),
583 s309_comm_from[i].empty() ? empty : s309_comm_from[i] );
584 replacestr(form309, ics309_nn( ics309_comm_msg, i ),
585 s309_comm_msg[i].empty() ? empty : s309_comm_msg[i] );
586 }
587
588 FILE *file309 = fopen(ics309_fname.c_str(), "w");
589 fprintf(file309,"%s", form309.c_str());
590 fclose(file309);
591
592 open_url(ics309_fname.c_str());
593 }
594
595 void cb_309_msg_type()
596 {
597 if (tabs_msg_type->value() == tab_ics309 ) {
598 tab_ics309_type->value(tab_309_1);
599 show_filename(def_309_filename);
600 }
601 }
602
603 void cb_309_textout()
604 {
605 std::string ics309_fname = ICS_dir;
606 ics309_fname.append("ics309.txt");
607
608 update_309fields();
609 std::string form309 = ics309_text_template;
610
611 replacestr(form309, ics309_incident, s309_incident );
612 replacestr(form309, ics309_date_fm, s309_date_fm );
613 replacestr(form309, ics309_time_fm, s309_time_fm );
614 replacestr(form309, ics309_date_to, s309_date_to );
615 replacestr(form309, ics309_time_to, s309_time_to );
616 replacestr(form309, ics309_prepared_by, s309_prepared_by );
617 replacestr(form309, ics309_preparer_date_time, s309_preparer_date_time );
618 replacestr(form309, ics309_radio_net, s309_radio_net );
619 replacestr(form309, ics309_radio_operator, s309_radio_operator );
620
621 for (int i = 0; i < 34; i++) {
622 replacestr(form309, ics309_nn( ics309_comm_time, i ), s309_comm_time[i] );
623 replacestr(form309, ics309_nn( ics309_comm_msg, i ), s309_comm_msg[i] );
624 replacestr(form309, ics309_nn( ics309_comm_to, i ), s309_comm_to[i] );
625 replacestr(form309, ics309_nn( ics309_comm_from, i ), s309_comm_from[i] );
626 }
627
628 FILE *file309 = fopen(ics309_fname.c_str(), "w");
629 fprintf(file309,"%s", form309.c_str());
630 fclose(file309);
631
632 open_url(ics309_fname.c_str());
633 }
634
635 void ics309_csv(Fl_Widget *w, void *d)
636 {
637 std::string csv_fname = ICS_dir;
638 csv_fname.append("ics309.csv");
639
640 const char *p;
641
642 p = FSEL::saveas(_("Save csv file"), "ICS-309\t*.csv",
643 csv_fname.c_str());
644
645 if (!p) return;
646 if (strlen(p) == 0) return;
647
648 csv_fname = p;
649
650 update_309fields();
651
652 std::string cvs_text;
653 cvs_text.assign("Incident,Start_Date,Start_Time,End_Date,End_Time,Preparer,Prepared_Date,Radio_Net,Radio_operator\n");
654 cvs_text.append("\"").append(s309_incident).append("\",");
655 cvs_text.append("\"").append(s309_date_fm).append("\",");
656 cvs_text.append("\"").append(s309_time_fm).append("\",");
657 cvs_text.append("\"").append(s309_date_to).append("\",");
658 cvs_text.append("\"").append(s309_time_to).append("\",");
659 cvs_text.append("\"").append(s309_prepared_by).append("\",");
660 cvs_text.append("\"").append(s309_preparer_date_time).append("\",");
661 cvs_text.append("\"").append(s309_radio_net).append("\",");
662 cvs_text.append("\"").append(s309_radio_operator).append("\"\n");
663 cvs_text.append("\n");
664 cvs_text.append("COMM_TIME,COMM_TO,COMM_FM,COMM_MSG\n");
665
666 for (int i = 0; i < 34; i++) {
667 cvs_text.append("\"").append(s309_comm_time[i]).append("\",");
668 cvs_text.append("\"").append(s309_comm_to[i]).append("\",");
669 cvs_text.append("\"").append(s309_comm_from[i]).append("\",");
670 cvs_text.append("\"").append(s309_comm_msg[i]).append("\"\n");
671 }
672
673 FILE *csv_file = fopen(csv_fname.c_str(), "w");
674 if (!csv_file) return;
675 fprintf(csv_file,"%s", cvs_text.c_str());
676 fclose(csv_file);
677 }