Debian Codegraph @

src/hics/hics214.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 <cstring>
25 #include <ctime>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <errno.h>
30
31 #include <FL/Fl.H>
32 #include <FL/Enumerations.H>
33 #include <FL/Fl_Window.H>
34 #include <FL/Fl_Button.H>
35 #include <FL/Fl_Group.H>
36 #include <FL/Fl_Sys_Menu_Bar.H>
37 #include <FL/x.H>
38 #include <FL/Fl_Help_Dialog.H>
39 #include <FL/Fl_Menu_Item.H>
40 #include <FL/Fl_File_Icon.H>
41
42 #include "config.h"
43 #include "flmsg_config.h"
44
45 #include "flmsg.h"
46 #include "templates.h"
47 #include "debug.h"
48 #include "util.h"
49 #include "gettext.h"
50 #include "flmsg_dialog.h"
51 #include "flinput2.h"
52 #include "date.h"
53 #include "calendar.h"
54 #include "icons.h"
55 #include "fileselect.h"
56 #include "wrap.h"
57 #include "status.h"
58 #include "parse_xml.h"
59
60 #ifdef WIN32
61 # include "flmsgrc.h"
62 # include "compat.h"
63 # define dirent fl_dirent_no_thanks
64 #endif
65
66 #include <FL/filename.H>
67 #include "dirent-check.h"
68
69 #include <FL/x.H>
70 #include <FL/Fl_Pixmap.H>
71 #include <FL/Fl_Image.H>
72
73 // ---------------------------------------------------------------------
74 // hics 214 field variables and template variables
75 // ---------------------------------------------------------------------
76
77 std::string hics214_tag_incident = ":1:";
78 std::string hics214_tag_date = ":2:";
79 std::string hics214_tag_time = ":2a:";
80 std::string hics214_tag_op_period = ":3:";
81 std::string hics214_tag_sec_brch = ":4:";
82 std::string hics214_tag_position = ":5:";
83 std::string hics214_tag_activity_time = ":T[n]:"; // 30
84 std::string hics214_tag_activity_event = ":E[n]:";
85 std::string hics214_tag_prepared_by = ":7:";
86 std::string hics214_tag_facility = ":8:"; // new field
87
88 std::string hics214_incident;
89 std::string hics214_date;
90 std::string hics214_time;
91 std::string hics214_op_period;
92 std::string hics214_sec_brch;
93 std::string hics214_position;
94 std::string hics214_activity_time[30];
95 std::string hics214_activity_event[30];
96 std::string hics214_prepared_by;
97 std::string hics214_facility;
98
99 // =====================================================================
100
101 std::string hics214_buff;
102 std::string hics214_def_filename = "";
103 std::string hics214_base_filename = "";
104 std::string hics214_template_name = "";
105 bool hics214_using_template = false;
106
107 void hics214_cb_set_date()
108 {
109 hics214_txt_date->value(szDate(progStatus.dtformat));
110 }
111
112 void hics214_cb_set_time()
113 {
114 hics214_txt_time->value(szTime(progStatus.UTC));
115 }
116
117 void hics214_clear_fields()
118 {
119 hics214_incident.clear();
120 hics214_date.clear();
121 hics214_time.clear();
122 hics214_op_period.clear();
123 hics214_sec_brch.clear();
124 hics214_position.clear();
125 hics214_prepared_by.clear();
126 hics214_facility.clear();
127
128 for (int i = 0; i < 30; i++) {
129 hics214_activity_time[i].clear();
130 hics214_activity_event[i].clear();
131 }
132 }
133
134 bool check_hics214fields()
135 {
136 if (hics214_incident != hics214_txt_incident->value())
137 return true;
138 if (hics214_date != hics214_txt_date->value())
139 return true;
140 if (hics214_time != hics214_txt_time->value())
141 return true;
142 if (hics214_op_period != hics214_txt_op_period->value())
143 return true;
144 if (hics214_sec_brch != hics214_txt_sec_brch->value())
145 return true;
146 if (hics214_position != hics214_txt_position->value())
147 return true;
148 if (hics214_prepared_by != hics214_txt_prepared_by->value())
149 return true;
150 if (hics214_facility != hics214_txt_facility->value())
151 return true;
152
153 for (int i = 0; i < 30; i++) {
154 if (hics214_activity_time[i] != hics214_txt_activity_time[i]->value())
155 return true;
156 if (hics214_activity_event[i] != hics214_txt_activity_event[i]->value())
157 return true;
158 }
159 return false;
160 }
161
162 void hics214_update_fields()
163 {
164 hics214_incident = hics214_txt_incident->value();
165 hics214_date = hics214_txt_date->value();
166 hics214_time = hics214_txt_time->value();
167 hics214_op_period = hics214_txt_op_period->value();
168 hics214_sec_brch = hics214_txt_sec_brch->value();
169 hics214_position = hics214_txt_position->value();
170 hics214_prepared_by = hics214_txt_prepared_by->value();
171 hics214_facility = hics214_txt_facility->value();
172
173 for (int i = 0; i < 30; i++) {
174 hics214_activity_time[i] = hics214_txt_activity_time[i]->value();
175 hics214_activity_event[i] = hics214_txt_activity_event[i]->value();
176 }
177 }
178
179 void hics214_update_form()
180 {
181 hics214_txt_incident->value(hics214_incident.c_str());
182 hics214_txt_date->value(hics214_date.c_str());
183 hics214_txt_time->value(hics214_time.c_str());
184 hics214_txt_op_period->value(hics214_op_period.c_str());
185 hics214_txt_sec_brch->value(hics214_sec_brch.c_str());
186 hics214_txt_position->value(hics214_position.c_str());
187 hics214_txt_prepared_by->value(hics214_prepared_by.c_str());
188 hics214_txt_facility->value(hics214_facility.c_str());
189
190 for (int i = 0; i < 30; i++) {
191 hics214_txt_activity_time[i]->value(hics214_activity_time[i].c_str());
192 hics214_txt_activity_event[i]->value(hics214_activity_event[i].c_str());
193 }
194 }
195
196 void hics214_clear_form()
197 {
198 hics214_clear_fields();
199 hics214_update_form();
200 }
201
202 static std::string &hics_nn(std::string & subst, int n)
203 {
204 static std::string garbage = "#$^*!";
205 static std::string hics;
206 hics.clear();
207 hics = subst;
208 size_t pos = hics.find("[");
209 if (pos == std::string::npos) return garbage;
210 pos++;
211 hics[pos] = '0' + (n/10 % 10);
212 hics[pos+1] = '0' + n % 10;
213 hics[pos+2] = ']';
214 hics += ':';
215 return hics;
216 }
217
218 void hics214_make_buff(bool compress = false)
219 {
220 std::string mbuff;
221 mbuff.clear();
222 mbuff.append( lineout( hics214_tag_incident, hics214_incident ) );
223 mbuff.append( lineout( hics214_tag_date, hics214_date ) );
224 mbuff.append( lineout( hics214_tag_time, hics214_time ) );
225 mbuff.append( lineout( hics214_tag_op_period, hics214_op_period ) );
226 mbuff.append( lineout( hics214_tag_sec_brch, hics214_sec_brch) );
227 mbuff.append( lineout( hics214_tag_position, hics214_position) );
228 mbuff.append( lineout( hics214_tag_prepared_by, hics214_prepared_by ) );
229 mbuff.append( lineout( hics214_tag_facility, hics214_facility ) );
230
231 for (int i = 0; i < 30; i++) {
232 mbuff.append( lineout( hics_nn( hics214_tag_activity_time, i ), hics214_activity_time[i] ) );
233 mbuff.append( lineout( hics_nn( hics214_tag_activity_event, i ), hics214_activity_event[i] ) );
234 }
235 if (compress) compress_maybe(mbuff);
236 hics214_buff.append(mbuff);
237 }
238
239 void hics214_read_buffer(std::string data)
240 {
241 hics214_clear_fields();
242 read_header(data);
243
244 hics214_incident = findstr(data, hics214_tag_incident);
245 hics214_date = findstr(data, hics214_tag_date);
246 hics214_time = findstr(data, hics214_tag_time);
247 hics214_op_period = findstr(data, hics214_tag_op_period);
248 hics214_sec_brch = findstr(data, hics214_tag_sec_brch);
249 hics214_position = findstr(data, hics214_tag_position);
250 hics214_prepared_by = findstr(data, hics214_tag_prepared_by);
251 hics214_facility = findstr(data, hics214_tag_facility);
252
253 for (int i = 0; i < 30; i++) {
254 hics214_activity_time[i] = findstr(data, hics_nn( hics214_tag_activity_time, i ) );
255 hics214_activity_event[i] = findstr(data, hics_nn( hics214_tag_activity_event, i ) );
256 }
257
258 hics214_update_form();
259 }
260
261 void hics214_cb_new()
262 {
263 if (check_hics214fields()) {
264 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
265 update_header(CHANGED);
266 hics214_cb_save();
267 }
268 }
269 hics214_clear_form();
270 clear_header();
271 hics214_def_filename = ICS_msg_dir;
272 hics214_def_filename.append("new").append(HF214_EXT);
273 show_filename(hics214_def_filename);
274 hics214_using_template = false;
275 }
276
277 void hics214_cb_import()
278 {
279 fl_alert2("Not implemented");
280 }
281
282 void hics214_cb_export()
283 {
284 fl_alert2("Not implemented");
285 }
286
287 void hics214_cb_wrap_import(std::string wrapfilename, std::string inpbuffer)
288 {
289 hics214_clear_form();
290 hics214_read_buffer(inpbuffer);
291 hics214_def_filename = ICS_msg_dir;
292 hics214_def_filename.append(wrapfilename);
293 show_filename(hics214_def_filename);
294 hics214_using_template = false;
295 }
296
297 int eval_hics214_fsize()
298 {
299 Ccrc16 chksum;
300 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
301 evalstr.append(hics214_base_filename).append("]");
302 hics214_update_fields();
303 update_header(FROM);
304 evalstr.append(header("<hics214>"));
305 hics214_buff.clear();
306 hics214_make_buff(true);
307 if (hics214_buff.empty()) return 0;
308 compress_maybe( hics214_buff );
309 evalstr.append( hics214_buff );
310 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
311 return evalstr.length();
312 }
313
314 void hics214_cb_wrap_export()
315 {
316 if (check_hics214fields()) {
317 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
318 return;
319 update_header(CHANGED);
320 }
321 hics214_update_fields();
322
323 if (hics214_base_filename == std::string("new").append(HF214_EXT) ||\
324 hics214_base_filename == std::string("default").append(HF214_EXT) )
325 hics214_cb_save_as();
326
327 std::string wrapfilename = WRAP_send_dir;
328 wrapfilename.append(hics214_base_filename);
329 wrapfilename.append(".wrap");
330 const char *p = FSEL::saveas(
331 "Save as wrap file",
332 "Wrap file\t*.{wrap,WRAP}",
333 wrapfilename.c_str());
334 if (p) {
335 std::string pext = fl_filename_ext(p);
336 wrapfilename = p;
337 update_header(FROM);
338 hics214_buff.assign(header("<hics214>"));
339 hics214_make_buff(true);
340 export_wrapfile(hics214_base_filename, wrapfilename, hics214_buff, pext != ".wrap");
341
342 hics214_buff.assign(header("<hics214>"));
343 hics214_make_buff(false);
344 hics214_write(hics214_def_filename);
345 }
346 }
347
348 void hics214_cb_wrap_autosend()
349 {
350 if (check_hics214fields()) {
351 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
352 return;
353 update_header(CHANGED);
354 }
355 hics214_update_fields();
356
357 if (hics214_base_filename == std::string("new").append(HF214_EXT) ||
358 hics214_base_filename == std::string("default").append(HF214_EXT) )
359 hics214_cb_save_as();
360
361 update_header(FROM);
362 hics214_buff.assign(header("<hics214>"));
363 hics214_make_buff(true);
364 xfr_via_socket(hics214_base_filename,hics214_buff);
365
366 hics214_buff.assign(header("<hics214>"));
367 hics214_make_buff(false);
368 hics214_write(hics214_def_filename);
369 }
370
371 void hics214_cb_load_template()
372 {
373 std::string hics214_def_filename = hics214_template_name;
374 const char *p = FSEL::select(
375 "Open template file",
376 std::string("Template file\t*").append(HT214_EXT).c_str(),
377 hics214_def_filename.c_str());
378 if (p) {
379 hics214_clear_form();
380 read_data_file(p);
381 hics214_template_name = p;
382 show_filename(hics214_template_name);
383 hics214_using_template = true;
384 }
385 }
386
387 void hics214_cb_save_template()
388 {
389 if (!hics214_using_template) {
390 hics214_cb_save_as_template();
391 return;
392 }
393 std::string hics214_def_filename = hics214_template_name;
394 const char *p = FSEL::saveas(
395 "Save template file",
396 std::string("Template file\t*").append(HT214_EXT).c_str(),
397 hics214_def_filename.c_str());
398 if (p) {
399 update_header(CHANGED);
400 hics214_update_fields();
401 hics214_buff.assign(header("<hics214>"));
402 hics214_make_buff();
403 hics214_write(p);
404 }
405 }
406
407 void hics214_cb_save_as_template()
408 {
409 std::string hics214_def_filename = hics214_template_name;
410 const char *p = FSEL::saveas(
411 "Save as template file",
412 std::string("Template file\t*").append(HT214_EXT).c_str(),
413 hics214_def_filename.c_str());
414 if (p) {
415 const char *pext = fl_filename_ext(p);
416 hics214_template_name = p;
417 if (strlen(pext) == 0) hics214_template_name.append(HT214_EXT);
418 remove_spaces_from_filename(hics214_template_name);
419 clear_header();
420 update_header(CHANGED);
421 hics214_update_fields();
422 hics214_buff.assign(header("<hics214>"));
423 hics214_make_buff();
424 hics214_write(hics214_template_name);
425 show_filename(hics214_template_name);
426 hics214_using_template = true;
427 }
428 }
429
430 void hics214_cb_open()
431 {
432 const char *p = FSEL::select(
433 _("Open data file"),
434 std::string("HICS-214\t*").append(HF214_EXT).c_str(),
435 hics214_def_filename.c_str());
436 if (!p) return;
437 if (strlen(p) == 0) return;
438 hics214_clear_form();
439 read_data_file(p);
440 hics214_using_template = false;
441 hics214_def_filename = p;
442 show_filename(hics214_def_filename);
443 }
444
445 void hics214_write(std::string s)
446 {
447 FILE *file214 = fopen(s.c_str(), "w");
448 if (!file214) return;
449
450 fwrite(hics214_buff.c_str(), hics214_buff.length(), 1, file214);
451 fclose(file214);
452 }
453
454 bool hics214_cb_save_as()
455 {
456 const char *p;
457 std::string newfilename;
458
459 std::string name = named_file();
460 if (!name.empty()) {
461 name.append(HF214_EXT);
462 newfilename = ICS_msg_dir;
463 newfilename.append(name);
464 } else
465 newfilename = hics214_def_filename;
466
467 p = FSEL::saveas(
468 _("Save data file"),
469 std::string("HICS-214\t*").append(HF214_EXT).c_str(),
470 newfilename.c_str());
471 if (!p) return false;
472 if (strlen(p) == 0) return false;
473
474 if (progStatus.sernbr_fname) update_sernbr();
475
476 const char *pext = fl_filename_ext(p);
477 hics214_def_filename = p;
478 if (strlen(pext) == 0) hics214_def_filename.append(HF214_EXT);
479
480 remove_spaces_from_filename(hics214_def_filename);
481 hics214_update_fields();
482 update_header(NEW);
483 hics214_buff.assign(header("<hics214>"));
484 hics214_make_buff();
485 hics214_write(hics214_def_filename);
486
487 hics214_using_template = false;
488 show_filename(hics214_def_filename);
489
490 return true;
491 }
492
493 void hics214_cb_save()
494 {
495 if (hics214_base_filename == std::string("new").append(HF214_EXT) ||
496 hics214_base_filename == std::string("default").append(HF214_EXT) ||
497 hics214_using_template == true) {
498 hics214_cb_save_as();
499 return;
500 }
501 if (check_hics214fields()) update_header(CHANGED);
502 hics214_buff.assign(header("<hics214>"));
503 hics214_update_fields();
504 hics214_make_buff();
505 hics214_write(hics214_def_filename);
506 hics214_using_template = false;
507 }
508
509 void hics214_cb_html()
510 {
511 std::string fname_name = fl_filename_name(hics214_def_filename.c_str());
512 size_t p = fname_name.rfind('.');
513 if (p != std::string::npos) fname_name.erase(p);
514
515 std::string hics214_fname = ICS_dir;
516 hics214_fname.append(fname_name);
517 hics214_fname.append(".html");
518
519 std::string html_text = "";
520
521 hics214_update_fields();
522 std::string form214 = hics214_html_template;
523
524 replacestr(form214, TITLE, fname_name);
525 replacestr(form214, hics214_tag_incident, hics214_incident );
526 replacestr(form214, hics214_tag_date, hics214_date );
527 replacestr(form214, hics214_tag_time, hics214_time );
528 replacestr(form214, hics214_tag_op_period, hics214_op_period );
529 replacestr(form214, hics214_tag_sec_brch, hics214_sec_brch );
530 replacestr(form214, hics214_tag_position, hics214_position );
531 replacestr(form214, hics214_tag_prepared_by, hics214_prepared_by );
532 replacestr(form214, hics214_tag_facility, hics214_facility );
533
534 for (int i = 0; i < 30; i++) {
535 replacestr(form214, hics_nn( hics214_tag_activity_time, i ), hics214_activity_time[i] );
536 replacestr(form214, hics_nn( hics214_tag_activity_event, i ), hics214_activity_event[i] );
537 }
538
539 FILE *file214 = fopen(hics214_fname.c_str(), "w");
540 fprintf(file214,"%s", form214.c_str());
541 fclose(file214);
542
543 open_url(hics214_fname.c_str());
544 }
545
546 void hics214_cb_textout()
547 {
548 std::string hics214_fname = ICS_dir;
549 hics214_fname.append("hics214.txt");
550
551 hics214_update_fields();
552 std::string form214 = hics214_text_template;
553
554 replacestr(form214, hics214_tag_incident, hics214_incident );
555 replacestr(form214, hics214_tag_date, hics214_date );
556 replacestr(form214, hics214_tag_time, hics214_time );
557 replacestr(form214, hics214_tag_op_period, hics214_op_period );
558 replacestr(form214, hics214_tag_sec_brch, hics214_sec_brch );
559 replacestr(form214, hics214_tag_position, hics214_position );
560 replacestr(form214, hics214_tag_prepared_by, hics214_prepared_by );
561 replacestr(form214, hics214_tag_facility, hics214_facility );
562
563 for (int i = 0; i < 30; i++) {
564 replacestr(form214, hics_nn( hics214_tag_activity_time, i ), hics214_activity_time[i] );
565 replacestr(form214, hics_nn( hics214_tag_activity_event, i ), hics214_activity_event[i] );
566 }
567
568 FILE *file214 = fopen(hics214_fname.c_str(), "w");
569 fprintf(file214,"%s", form214.c_str());
570 fclose(file214);
571
572 open_url(hics214_fname.c_str());
573 }