Debian Codegraph @

src/ics/ics214.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 214 field variables and template variables
76 // ---------------------------------------------------------------------
77
78 std::string ics214_incident = ":inc:";
79 std::string ics214_date = ":dat:";
80 std::string ics214_time = ":tim:";
81 std::string ics214_op_period = ":opp:";
82 std::string ics214_unit_name = ":und:";
83 std::string ics214_unit_leader = ":unl:";
84
85 std::string ics214_roster_name = ":nam[n]:"; // 14
86 std::string ics214_roster_position = ":pos[n]:";
87 std::string ics214_roster_home_base = ":hom[n]:";
88
89 std::string ics214_activity_time = ":at[n]:"; // 16
90 std::string ics214_activity_event = ":ev[n]:";
91
92 std::string ics214_prepared_by = ":pre:";
93
94 std::string s214_incident;
95 std::string s214_date;
96 std::string s214_time;
97 std::string s214_op_period;
98 std::string s214_unit_name;
99 std::string s214_unit_leader;
100
101 std::string s214_roster_name[16];
102 std::string s214_roster_position[16];
103 std::string s214_roster_home_base[16];
104 std::string s214_activity_time[16];
105 std::string s214_activity_event[16];
106
107 std::string s214_prepared_by;
108
109 // =====================================================================
110
111 std::string buff214;
112 std::string def_214_filename = "";
113 std::string base_214_filename = "";
114 std::string def_214_TemplateName = "";
115
116 bool using_ics214_template = false;
117
118 void cb_214_set_date()
119 {
120 txt_214_date->value(szDate(progStatus.dtformat));
121 }
122
123 void cb_214_set_time()
124 {
125 txt_214_time->value(szTime(progStatus.UTC));
126 }
127
128 void clear_214fields()
129 {
130 s214_incident.clear();
131 s214_date.clear();
132 s214_time.clear();
133 s214_op_period.clear();
134 s214_unit_name.clear();
135 s214_unit_leader.clear();
136 s214_prepared_by.clear();
137
138 for (int i = 0; i < 16; i++) {
139 s214_roster_name[i].clear();
140 s214_roster_position[i].clear();
141 s214_roster_home_base[i].clear();
142 s214_activity_time[i].clear();
143 s214_activity_event[i].clear();
144 }
145 }
146
147 bool check_214fields()
148 {
149 if (s214_incident != txt_214_incident->value())
150 return true;
151 if (s214_date != txt_214_date->value())
152 return true;
153 if (s214_time != txt_214_time->value())
154 return true;
155 if (s214_op_period != txt_214_op_period->value())
156 return true;
157 if (s214_unit_name != txt_214_unit_name->value())
158 return true;
159 if (s214_unit_leader != txt_214_unit_leader->value())
160 return true;
161 if (s214_prepared_by != txt_214_prepared_by->value())
162 return true;
163
164 for (int i = 0; i < 16; i++) {
165 if (s214_roster_name[i] != txt_214_roster_name[i]->value())
166 return true;
167 if (s214_roster_position[i] != txt_214_roster_position[i]->value())
168 return true;
169 if (s214_roster_home_base[i] != txt_214_roster_home_base[i]->value())
170 return true;
171 if (s214_activity_time[i] != txt_214_activity_time[i]->value())
172 return true;
173 if (s214_activity_event[i] != txt_214_activity_event[i]->value())
174 return true;
175 }
176 return false;
177 }
178
179 void update_214fields()
180 {
181 s214_incident = txt_214_incident->value();
182 s214_date = txt_214_date->value();
183 s214_time = txt_214_time->value();
184 s214_op_period = txt_214_op_period->value();
185 s214_unit_name = txt_214_unit_name->value();
186 s214_unit_leader = txt_214_unit_leader->value();
187 s214_prepared_by = txt_214_prepared_by->value();
188
189 for (int i = 0; i < 16; i++) {
190 s214_roster_name[i] = txt_214_roster_name[i]->value();
191 s214_roster_position[i] = txt_214_roster_position[i]->value();
192 s214_roster_home_base[i] = txt_214_roster_home_base[i]->value();
193 s214_activity_time[i] = txt_214_activity_time[i]->value();
194 s214_activity_event[i] = txt_214_activity_event[i]->value();
195 }
196 }
197
198 void update_214form()
199 {
200 txt_214_incident->value(s214_incident.c_str());
201 txt_214_date->value(s214_date.c_str());
202 txt_214_time->value(s214_time.c_str());
203 txt_214_op_period->value(s214_op_period.c_str());
204 txt_214_unit_name->value(s214_unit_name.c_str());
205 txt_214_unit_leader->value(s214_unit_leader.c_str());
206 txt_214_prepared_by->value(s214_prepared_by.c_str());
207
208 for (int i = 0; i < 16; i++) {
209 txt_214_roster_name[i]->value(s214_roster_name[i].c_str());
210 txt_214_roster_position[i]->value(s214_roster_position[i].c_str());
211 txt_214_roster_home_base[i]->value(s214_roster_home_base[i].c_str());
212 txt_214_activity_time[i]->value(s214_activity_time[i].c_str());
213 txt_214_activity_event[i]->value(s214_activity_event[i].c_str());
214 }
215 }
216
217 void clear_214_form()
218 {
219 clear_214fields();
220 update_214form();
221 }
222
223 std::string &ics_nn(std::string & subst, int n)
224 {
225 static std::string garbage = "#$^*!";
226 static std::string ics;
227 ics.clear();
228 ics = subst;
229 size_t pos = ics.find("[");
230 if (pos == std::string::npos) return garbage;
231 pos++;
232 if (n < 10)
233 ics[pos] = '0' + n;
234 else {
235 ics[pos] = '1';
236 ics[pos+1] = '0' + n % 10;
237 ics[pos+2] = ']';
238 ics += ':';
239 }
240 return ics;
241 }
242
243 void make_buff214(bool compress = false)
244 {
245 std::string mbuff;
246 mbuff.clear();
247 mbuff.append( lineout( ics214_incident, s214_incident ) );
248 mbuff.append( lineout( ics214_date, s214_date ) );
249 mbuff.append( lineout( ics214_time, s214_time ) );
250 mbuff.append( lineout( ics214_op_period, s214_op_period ) );
251 mbuff.append( lineout( ics214_unit_name, s214_unit_name) );
252 mbuff.append( lineout( ics214_unit_leader, s214_unit_leader) );
253
254 mbuff.append( lineout( ics214_prepared_by, s214_prepared_by ) );
255
256 for (int i = 0; i < 16; i++) {
257 mbuff.append( lineout( ics_nn( ics214_roster_name, i ), s214_roster_name[i] ) );
258 mbuff.append( lineout( ics_nn( ics214_roster_position, i ), s214_roster_position[i] ) );
259 mbuff.append( lineout( ics_nn( ics214_roster_home_base, i ), s214_roster_home_base[i] ) );
260 mbuff.append( lineout( ics_nn( ics214_activity_time, i ), s214_activity_time[i] ) );
261 mbuff.append( lineout( ics_nn( ics214_activity_event, i ), s214_activity_event[i] ) );
262 }
263 if (compress) compress_maybe(mbuff);
264 buff214.append(mbuff);
265 }
266
267 void read_214_buffer(std::string data)
268 {
269 clear_214fields();
270 read_header(data);
271
272 s214_incident = findstr(data, ics214_incident);
273 s214_date = findstr(data, ics214_date);
274 s214_time = findstr(data, ics214_time);
275 s214_op_period = findstr(data, ics214_op_period);
276 s214_unit_name = findstr(data, ics214_unit_name);
277 s214_unit_leader = findstr(data, ics214_unit_leader);
278 s214_prepared_by = findstr(data, ics214_prepared_by);
279
280 for (int i = 0; i < 16; i++) {
281 s214_roster_name[i] = findstr(data, ics_nn( ics214_roster_name, i ) );
282 s214_roster_position[i] = findstr(data, ics_nn( ics214_roster_position, i ) );
283 s214_roster_home_base[i] = findstr(data, ics_nn( ics214_roster_home_base, i ) );
284 s214_activity_time[i] = findstr(data, ics_nn( ics214_activity_time, i ) );
285 s214_activity_event[i] = findstr(data, ics_nn( ics214_activity_event, i ) );
286 }
287
288 update_214form();
289 }
290
291 void cb_214_new()
292 {
293 if (check_214fields()) {
294 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
295 update_header(CHANGED);
296 cb_214_save();
297 }
298 }
299 clear_214_form();
300 clear_header();
301 def_214_filename = ICS_msg_dir;
302 def_214_filename.append("new").append(F214_EXT);
303 show_filename(def_214_filename);
304 using_ics214_template = false;
305 }
306
307 void cb_214_import()
308 {
309 fl_alert2("Not implemented");
310 }
311
312 void cb_214_export()
313 {
314 fl_alert2("Not implemented");
315 }
316
317 void cb_214_wrap_import(std::string wrapfilename, std::string inpbuffer)
318 {
319 clear_214_form();
320 read_214_buffer(inpbuffer);
321 def_214_filename = ICS_msg_dir;
322 def_214_filename.append(wrapfilename);
323 show_filename(def_214_filename);
324 using_ics214_template = false;
325 }
326
327 int eval_214_fsize()
328 {
329 Ccrc16 chksum;
330 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
331 evalstr.append(base_214_filename).append("]");
332 update_214fields();
333 update_header(FROM);
334 evalstr.append(header("<ics214>"));
335 buff214.clear();
336 make_buff214(true);
337 if (buff214.empty()) return 0;
338 compress_maybe( buff214 );
339 evalstr.append( buff214 );
340 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
341 return evalstr.length();
342 }
343
344 void cb_214_wrap_export()
345 {
346 if (check_214fields()) {
347 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
348 return;
349 update_header(CHANGED);
350 }
351 update_214fields();
352
353 if (base_214_filename == std::string("new").append(F214_EXT) ||
354 base_214_filename == std::string("default").append(F214_EXT) )
355 if (!cb_214_save_as()) return;
356
357 std::string wrapfilename = WRAP_send_dir;
358 wrapfilename.append(base_214_filename);
359 wrapfilename.append(".wrap");
360 const char *p = FSEL::saveas(
361 "Save as wrap file",
362 "Wrap file\t*.{wrap,WRAP}",
363 wrapfilename.c_str());
364 if (p) {
365 std::string pext = fl_filename_ext(p);
366 wrapfilename = p;
367 update_header(FROM);
368 buff214.assign(header("<ics214>"));
369 make_buff214(true);
370 export_wrapfile(base_214_filename, wrapfilename, buff214, pext != ".wrap");
371
372 buff214.assign(header("<ics214>"));
373 make_buff214(false);
374 write_214(def_214_filename);
375 }
376 }
377
378 void cb_214_wrap_autosend()
379 {
380 if (check_214fields()) {
381 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
382 return;
383 update_header(CHANGED);
384 }
385 update_214fields();
386
387 if (base_214_filename == std::string("new").append(F214_EXT) ||
388 base_214_filename == std::string("default").append(F214_EXT) )
389 if (!cb_214_save_as()) return;
390
391 update_header(FROM);
392 buff214.assign(header("<ics214>"));
393 make_buff214(true);
394 xfr_via_socket(base_214_filename, buff214);
395
396 buff214.assign(header("<ics214>"));
397 make_buff214(false);
398 write_214(def_214_filename);
399 }
400
401 void cb_214_load_template()
402 {
403 std::string def_214_filename = def_214_TemplateName;
404 const char *p = FSEL::select(
405 "Open template file",
406 std::string("Template file\t*").append(T214_EXT).c_str(),
407 def_214_filename.c_str());
408 if (p) {
409 clear_214_form();
410 read_data_file(p);
411 def_214_TemplateName = p;
412 show_filename(def_214_TemplateName);
413 using_ics214_template = true;
414 }
415 }
416
417 void cb_214_save_template()
418 {
419 if (!using_ics214_template) {
420 cb_214_save_as_template();
421 return;
422 }
423 std::string def_214_filename = def_214_TemplateName;
424 const char *p = FSEL::saveas(
425 "Save template file",
426 std::string("Template file\t*").append(T214_EXT).c_str(),
427 def_214_filename.c_str());
428 if (p) {
429 update_header(CHANGED);
430 buff214.assign(header("<ics214>"));
431 make_buff214();
432 write_214(p);
433 }
434 }
435
436 void cb_214_save_as_template()
437 {
438 std::string def_214_filename = def_214_TemplateName;
439 const char *p = FSEL::saveas(
440 "Save as template file",
441 std::string("Template file\t*").append(T214_EXT).c_str(),
442 def_214_filename.c_str());
443 if (p) {
444 const char *pext = fl_filename_ext(p);
445 def_214_TemplateName = p;
446 if (strlen(pext) == 0) def_214_TemplateName.append(T214_EXT);
447 remove_spaces_from_filename(def_214_TemplateName);
448 clear_header();
449 update_header(CHANGED);
450 buff214.assign(header("<ics214>"));
451 make_buff214();
452 write_214(def_214_TemplateName);
453 show_filename(def_214_TemplateName);
454 using_ics214_template = true;
455 }
456 }
457
458 void cb_214_open()
459 {
460 const char *p = FSEL::select(
461 _("Open data file"),
462 std::string("ICS-214\t*").append(F214_EXT).c_str(),
463 def_214_filename.c_str());
464 if (!p) return;
465 if (strlen(p) == 0) return;
466 clear_214_form();
467 read_data_file(p);
468 using_ics214_template = false;
469 def_214_filename = p;
470 show_filename(def_214_filename);
471 }
472
473 void write_214(std::string s)
474 {
475 FILE *file214 = fopen(s.c_str(), "w");
476 if (!file214) return;
477
478 fwrite(buff214.c_str(), buff214.length(), 1, file214);
479 fclose(file214);
480 }
481
482 bool cb_214_save_as()
483 {
484 const char *p;
485 std::string newfilename;
486
487 std::string name = named_file();
488 if (!name.empty()) {
489 name.append(F214_EXT);
490 newfilename = ICS_msg_dir;
491 newfilename.append(name);
492 } else
493 newfilename = def_214_filename;
494
495 p = FSEL::saveas(
496 _("Save data file"),
497 std::string("ICS-214\t*").append(F214_EXT).c_str(),
498 newfilename.c_str());
499
500 if (!p) return false;
501 if (strlen(p) == 0) return false;
502
503 if (progStatus.sernbr_fname) update_sernbr();
504
505 const char *pext = fl_filename_ext(p);
506 def_214_filename = p;
507 if (strlen(pext) == 0) def_214_filename.append(F214_EXT);
508
509 remove_spaces_from_filename(def_214_filename);
510 update_214fields();
511 update_header(NEW);
512 buff214.assign(header("<ics214>"));
513 make_buff214();
514 write_214(def_214_filename);
515
516 using_ics214_template = false;
517 show_filename(def_214_filename);
518 return true;
519 }
520
521 void cb_214_save()
522 {
523 if (base_214_filename == std::string("new").append(F214_EXT) ||
524 base_214_filename == std::string("default").append(F214_EXT) ||
525 using_ics214_template == true) {
526 cb_214_save_as();
527 return;
528 }
529 if (check_214fields()) update_header(CHANGED);
530 update_214fields();
531 buff214.assign(header("<ics214>"));
532 make_buff214();
533 write_214(def_214_filename);
534 using_ics214_template = false;
535 }
536
537 void cb_214_html()
538 {
539 std::string fname_name = fl_filename_name(def_214_filename.c_str());
540 size_t p = fname_name.rfind('.');
541 if (p != std::string::npos) fname_name.erase(p);
542
543 std::string ics214_fname = ICS_dir;
544 ics214_fname.append(fname_name);
545 ics214_fname.append(".html");
546
547 std::string html_text = "";
548
549 update_214fields();
550 std::string form214 = ics214_html_template;
551
552 replacestr(form214, TITLE, fname_name);
553 replacestr(form214, ics214_incident, s214_incident );
554 replacestr(form214, ics214_date, s214_date );
555 replacestr(form214, ics214_time, s214_time );
556 replacestr(form214, ics214_op_period, s214_op_period );
557 replacestr(form214, ics214_unit_name, s214_unit_name );
558 replacestr(form214, ics214_unit_leader, s214_unit_leader );
559 replacestr(form214, ics214_prepared_by, s214_prepared_by );
560
561 for (int i = 0; i < 16; i++) {
562 replacestr(form214, ics_nn( ics214_roster_name, i ), s214_roster_name[i] );
563 replacestr(form214, ics_nn( ics214_roster_position, i ), s214_roster_position[i] );
564 replacestr(form214, ics_nn( ics214_roster_home_base, i ), s214_roster_home_base[i] );
565 replacestr(form214, ics_nn( ics214_activity_time, i ), s214_activity_time[i] );
566 replacestr(form214, ics_nn( ics214_activity_event, i ), s214_activity_event[i] );
567 }
568
569 FILE *file214 = fopen(ics214_fname.c_str(), "w");
570 fprintf(file214,"%s", form214.c_str());
571 fclose(file214);
572
573 open_url(ics214_fname.c_str());
574 }
575
576 void cb_214_msg_type()
577 {
578 if (tabs_msg_type->value() == tab_ics214 ) {
579 tab_ics214_type->value(tab_214_1);
580 show_filename(def_214_filename);
581 }
582 }
583
584 void cb_214_textout()
585 {
586 std::string ics214_fname = ICS_dir;
587 ics214_fname.append("ics214.txt");
588
589 update_214fields();
590 std::string form214 = ics214_text_template;
591
592 replacestr(form214, ics214_incident, s214_incident );
593 replacestr(form214, ics214_date, s214_date );
594 replacestr(form214, ics214_time, s214_time );
595 replacestr(form214, ics214_op_period, s214_op_period );
596 replacestr(form214, ics214_unit_name, s214_unit_name );
597 replacestr(form214, ics214_unit_leader, s214_unit_leader );
598 replacestr(form214, ics214_prepared_by, s214_prepared_by );
599
600 for (int i = 0; i < 16; i++) {
601 replacestr(form214, ics_nn( ics214_roster_name, i ), s214_roster_name[i] );
602 replacestr(form214, ics_nn( ics214_roster_position, i ), s214_roster_position[i] );
603 replacestr(form214, ics_nn( ics214_roster_home_base, i ), s214_roster_home_base[i] );
604 replacestr(form214, ics_nn( ics214_activity_time, i ), s214_activity_time[i] );
605 replacestr(form214, ics_nn( ics214_activity_event, i ), s214_activity_event[i] );
606 }
607
608 FILE *file214 = fopen(ics214_fname.c_str(), "w");
609 fprintf(file214,"%s", form214.c_str());
610 fclose(file214);
611
612 open_url(ics214_fname.c_str());
613 }