Debian Codegraph @

src/ics/ics205a.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 205a field variables and template variables
76 // ---------------------------------------------------------------------
77
78 std::string ics205a_incident = ":inc:";
79 std::string ics205a_date_fm = ":dfm:";
80 std::string ics205a_time_fm = ":tfm:";
81 std::string ics205a_date_to = ":dto:";
82 std::string ics205a_time_to = ":tto:";
83 std::string ics205a_prepared_by = ":pre:";
84 std::string ics205a_preparer_position = ":pos:";
85 std::string ics205a_preparer_date_time = ":dtm:";
86
87 std::string ics205a_comm_position = ":asg[n]:"; // 32
88 std::string ics205a_comm_name = ":nam[n]:"; // 32
89 std::string ics205a_comm_info = ":inf[n]:"; // 32
90
91 std::string s205a_incident;
92 std::string s205a_date_fm;
93 std::string s205a_time_fm;
94 std::string s205a_date_to;
95 std::string s205a_time_to;
96 std::string s205a_prepared_by;
97 std::string s205a_preparer_position;
98 std::string s205a_preparer_date_time;
99
100 std::string s205a_comm_position[32];
101 std::string s205a_comm_name[32];
102 std::string s205a_comm_info[32];
103
104 // =====================================================================
105
106 std::string buff205a;
107 std::string def_205a_filename = "";
108 std::string base_205a_filename = "";
109 std::string def_205a_TemplateName = "";
110 bool using_ics205a_template = false;
111
112 void cb_205a_set_date_fm()
113 {
114 txt_205a_date_fm->value(szDate(progStatus.dtformat));
115 }
116
117 void cb_205a_set_time_fm()
118 {
119 txt_205a_time_fm->value(szTime(progStatus.UTC));
120 }
121
122 void cb_205a_set_date_to()
123 {
124 txt_205a_date_to->value(szDate(progStatus.dtformat));
125 }
126
127 void cb_205a_set_time_to()
128 {
129 txt_205a_time_to->value(szTime(progStatus.UTC));
130 }
131
132 void clear_205afields()
133 {
134 s205a_incident.clear();
135 s205a_date_fm.clear();
136 s205a_time_fm.clear();
137 s205a_date_to.clear();
138 s205a_time_to.clear();
139 s205a_prepared_by.clear();
140 s205a_preparer_position.clear();
141 s205a_preparer_date_time.clear();
142
143 for (int i = 0; i < 32; i++) {
144 s205a_comm_position[i].clear();
145 s205a_comm_name[i].clear();
146 s205a_comm_info[i].clear();
147 }
148 }
149
150 bool check_205afields()
151 {
152 if (s205a_incident != txt_205a_incident->value())
153 return true;
154 if (s205a_date_fm != txt_205a_date_fm->value())
155 return true;
156 if (s205a_time_fm != txt_205a_time_fm->value())
157 return true;
158 if (s205a_date_to != txt_205a_date_to->value())
159 return true;
160 if (s205a_time_to != txt_205a_time_to->value())
161 return true;
162 if (s205a_prepared_by != txt_205a_prepared_by->value())
163 return true;
164 if (s205a_preparer_position != txt_205a_preparer_position->value())
165 return true;
166 if (s205a_preparer_date_time != txt_205a_preparer_date_time->value())
167 return true;
168 for (int i = 0; i < 32; i++) {
169 if (s205a_comm_position[i] != txt_205a_comm_position[i]->value())
170 return true;
171 if (s205a_comm_name[i] != txt_205a_comm_name[i]->value())
172 return true;
173 if (s205a_comm_info[i] != txt_205a_comm_info[i]->value())
174 return true;
175 }
176 return false;
177 }
178
179 void update_205afields()
180 {
181 s205a_incident = txt_205a_incident->value();
182 s205a_date_fm = txt_205a_date_fm->value();
183 s205a_time_fm = txt_205a_time_fm->value();
184 s205a_date_to = txt_205a_date_to->value();
185 s205a_time_to = txt_205a_time_to->value();
186 s205a_prepared_by = txt_205a_prepared_by->value();
187 s205a_preparer_position = txt_205a_preparer_position->value();
188 s205a_preparer_date_time = txt_205a_preparer_date_time->value();
189 for (int i = 0; i < 32; i++) {
190 s205a_comm_position[i] = txt_205a_comm_position[i]->value();
191 s205a_comm_name[i] = txt_205a_comm_name[i]->value();
192 s205a_comm_info[i] = txt_205a_comm_info[i]->value();
193 }
194 }
195
196 void update_205aform()
197 {
198 txt_205a_incident->value(s205a_incident.c_str());
199 txt_205a_date_fm->value(s205a_date_fm.c_str());
200 txt_205a_time_fm->value(s205a_time_fm.c_str());
201 txt_205a_date_to->value(s205a_date_to.c_str());
202 txt_205a_time_to->value(s205a_time_to.c_str());
203 txt_205a_prepared_by->value(s205a_prepared_by.c_str());
204 txt_205a_preparer_position->value(s205a_preparer_position.c_str());
205 txt_205a_preparer_date_time->value(s205a_preparer_date_time.c_str());
206
207 for (int i = 0; i < 32; i++) {
208 txt_205a_comm_position[i]->value(s205a_comm_position[i].c_str());
209 txt_205a_comm_name[i]->value(s205a_comm_name[i].c_str());
210 txt_205a_comm_info[i]->value(s205a_comm_info[i].c_str());
211 }
212 }
213
214 void clear_205a_form()
215 {
216 clear_205afields();
217 update_205aform();
218 }
219
220 std::string &ics205a_nn(std::string & subst, int n)
221 {
222 static std::string garbage = "#$^*!";
223 static std::string ics;
224 ics.clear();
225 ics = subst;
226 size_t pos = ics.find("[");
227 if (pos == std::string::npos) return garbage;
228 pos++;
229 if (n < 10)
230 ics[pos] = '0' + n;
231 else {
232 ics[pos] = n > 29 ? '3' : n > 19 ? '2' : '1';
233 ics[pos+1] = '0' + n % 10;
234 ics[pos+2] = ']';
235 ics += ':';
236 }
237 return ics;
238 }
239
240 void make_buff205a(bool compress = false)
241 {
242 std::string mbuff;
243 mbuff.clear();
244 mbuff.append( lineout( ics205a_incident, s205a_incident ) );
245 mbuff.append( lineout( ics205a_date_fm, s205a_date_fm ) );
246 mbuff.append( lineout( ics205a_time_fm, s205a_time_fm ) );
247 mbuff.append( lineout( ics205a_date_to, s205a_date_to ) );
248 mbuff.append( lineout( ics205a_time_to, s205a_time_to ) );
249 mbuff.append( lineout( ics205a_prepared_by, s205a_prepared_by ) );
250 mbuff.append( lineout( ics205a_preparer_position, s205a_preparer_position ) );
251 mbuff.append( lineout( ics205a_preparer_date_time, s205a_preparer_date_time ) );
252
253 for (int i = 0; i < 32; i++) {
254 mbuff.append( lineout( ics205a_nn( ics205a_comm_position, i ), s205a_comm_position[i] ) );
255 mbuff.append( lineout( ics205a_nn( ics205a_comm_name, i ), s205a_comm_name[i] ) );
256 mbuff.append( lineout( ics205a_nn( ics205a_comm_info, i ), s205a_comm_info[i] ) );
257 }
258 if (compress) compress_maybe(mbuff);
259 buff205a.append(mbuff);
260 }
261
262 void read_205a_buffer(std::string data)
263 {
264 clear_205afields();
265 read_header(data);
266
267 s205a_incident = findstr( data, ics205a_incident );
268 s205a_date_fm = findstr( data, ics205a_date_fm );
269 s205a_time_fm = findstr( data, ics205a_time_fm );
270 s205a_date_to = findstr( data, ics205a_date_to );
271 s205a_time_to = findstr( data, ics205a_time_to );
272 s205a_prepared_by = findstr( data, ics205a_prepared_by );
273 s205a_preparer_position = findstr( data, ics205a_preparer_position );
274 s205a_preparer_date_time = findstr( data, ics205a_preparer_date_time );
275
276 for (int i = 0; i < 32; i++) {
277 s205a_comm_position[i] = findstr( data, ics205a_nn( ics205a_comm_position, i ) );
278 s205a_comm_name[i] = findstr( data, ics205a_nn( ics205a_comm_name, i ) );
279 s205a_comm_info[i] = findstr( data, ics205a_nn( ics205a_comm_info, i ) );
280 }
281
282 update_205aform();
283 }
284
285 void cb_205a_new()
286 {
287 if (check_205afields()) {
288 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
289 update_header(CHANGED);
290 cb_205a_save();
291 }
292 }
293 clear_205a_form();
294 clear_header();
295 def_205a_filename = ICS_msg_dir;
296 def_205a_filename.append("new").append(F205A_EXT);
297 show_filename(def_205a_filename);
298 using_ics205a_template = false;
299 }
300
301 void cb_205a_import()
302 {
303 fl_alert2("Not implemented");
304 }
305
306 void cb_205a_export()
307 {
308 fl_alert2("Not implemented");
309 }
310
311 void cb_205a_wrap_import(std::string wrapfilename, std::string inpbuffer)
312 {
313 clear_205a_form();
314 read_205a_buffer(inpbuffer);
315 def_205a_filename = ICS_msg_dir;
316 def_205a_filename.append(wrapfilename);
317 show_filename(def_205a_filename);
318 using_ics205a_template = false;
319 }
320
321 int eval_205a_fsize()
322 {
323 Ccrc16 chksum;
324 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
325 evalstr.append(base_205a_filename).append("]");
326 update_205afields();
327 update_header(FROM);
328 evalstr.append(header("<ics205a>"));
329 buff205a.clear();
330 make_buff205a(true);
331 if (buff205a.empty()) return 0;
332 compress_maybe( buff205a );
333 evalstr.append( buff205a );
334 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
335 return evalstr.length();
336 }
337
338 void cb_205a_wrap_export()
339 {
340 if (check_205afields()) {
341 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
342 return;
343 update_header(CHANGED);
344 }
345 update_205afields();
346
347 if (base_205a_filename == std::string("new").append(F205A_EXT) ||
348 base_205a_filename == std::string("default").append(F205A_EXT) )
349 if (!cb_205a_save_as()) return;
350
351 std::string wrapfilename = WRAP_send_dir;
352 wrapfilename.append(base_205a_filename);
353 wrapfilename.append(".wrap");
354 const char *p = FSEL::saveas(
355 "Save as wrap file",
356 "Wrap file\t*.{wrap,WRAP}",
357 wrapfilename.c_str());
358 if (p) {
359 std::string pext = fl_filename_ext(p);
360 wrapfilename = p;
361 update_header(FROM);
362 buff205a.assign(header("<ics205a>"));
363 make_buff205a(true);
364 export_wrapfile(base_205a_filename, wrapfilename, buff205a, pext != ".wrap");
365 write_205a(def_205a_filename);
366 }
367 }
368
369 void cb_205a_wrap_autosend()
370 {
371 if (check_205afields()) {
372 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
373 return;
374 update_header(CHANGED);
375 }
376 update_205afields();
377
378 if (base_205a_filename == std::string("new").append(F205A_EXT) ||
379 base_205a_filename == std::string("default").append(F205A_EXT) )
380 cb_205a_save_as();
381
382 update_header(FROM);
383 buff205a.assign(header("<ics205a>"));
384 make_buff205a(true);
385
386 xfr_via_socket(base_205a_filename, buff205a);
387 write_205a(def_205a_filename);
388 }
389
390 void cb_205a_load_template()
391 {
392 std::string def_205a_filename = def_205a_TemplateName;
393 const char *p = FSEL::select(
394 "Open template file",
395 std::string("Template file\t*").append(F205A_EXT).c_str(),
396 def_205a_filename.c_str());
397 if (p) {
398 clear_205a_form();
399 read_data_file(p);
400 def_205a_TemplateName = p;
401 show_filename(def_205a_TemplateName);
402 using_ics205a_template = true;
403 }
404 }
405
406 void cb_205a_save_template()
407 {
408 if (!using_ics205a_template) {
409 cb_205a_save_as_template();
410 return;
411 }
412 std::string def_205a_filename = def_205a_TemplateName;
413 const char *p = FSEL::saveas(
414 "Save template file",
415 std::string("Template file\t*").append(T205A_EXT).c_str(),
416 def_205a_filename.c_str());
417 if (p) {
418 clear_header();
419 make_buff205a();
420 write_205a(p);
421 }
422 }
423
424 void cb_205a_save_as_template()
425 {
426 std::string def_205a_filename = def_205a_TemplateName;
427 const char *p = FSEL::saveas(
428 "Save as template file",
429 std::string("Template file\t*").append(T205A_EXT).c_str(),
430 def_205a_filename.c_str());
431 if (p) {
432 const char *pext = fl_filename_ext(p);
433 def_205a_TemplateName = p;
434 if (strlen(pext) == 0) def_205a_TemplateName.append(T205A_EXT);
435 remove_spaces_from_filename(def_205a_TemplateName);
436 clear_header();
437 make_buff205a();
438 write_205a(def_205a_TemplateName);
439 show_filename(def_205a_TemplateName);
440 using_ics205a_template = true;
441 }
442 }
443
444 void cb_205a_open()
445 {
446 const char *p = FSEL::select(
447 _("Open data file"),
448 std::string("ICS-205a\t*").append(F205A_EXT).c_str(),
449 def_205a_filename.c_str());
450 if (!p) return;
451 if (strlen(p) == 0) return;
452 clear_205a_form();
453 read_data_file(p);
454 using_ics205a_template = false;
455 def_205a_filename = p;
456 show_filename(def_205a_filename);
457 }
458
459 void write_205a(std::string s)
460 {
461 FILE *file205a = fopen(s.c_str(), "w");
462 if (!file205a) return;
463
464 fwrite(buff205a.c_str(), buff205a.length(), 1, file205a);
465 fclose(file205a);
466 }
467
468 bool cb_205a_save_as()
469 {
470 const char *p;
471 std::string newfilename;
472
473 std::string name = named_file();
474 if (!name.empty()) {
475 name.append(F205A_EXT);
476 newfilename = ICS_msg_dir;
477 newfilename.append(name);
478 } else
479 newfilename = def_205a_filename;
480
481 p = FSEL::saveas(
482 _("Save data file"),
483 std::string("ICS-205a\t*").append(F205A_EXT).c_str(),
484 newfilename.c_str());
485
486 if (!p) return false;
487 if (strlen(p) == 0) return false;
488
489 if (progStatus.sernbr_fname) update_sernbr();
490
491 const char *pext = fl_filename_ext(p);
492 def_205a_filename = p;
493 if (strlen(pext) == 0) def_205a_filename.append(F205A_EXT);
494
495 remove_spaces_from_filename(def_205a_filename);
496 update_205afields();
497 update_header(NEW);
498 buff205a.assign(header("<ics205a>"));
499 make_buff205a();
500 write_205a(def_205a_filename);
501
502 using_ics205a_template = false;
503 show_filename(def_205a_filename);
504 return true;
505 }
506
507 void cb_205a_save()
508 {
509 if (base_205a_filename == std::string("new").append(F205A_EXT) ||
510 base_205a_filename == std::string("default").append(F205A_EXT) ||
511 using_ics205a_template == true) {
512 cb_205a_save_as();
513 return;
514 }
515 if (check_205afields()) update_header(CHANGED);
516 update_205afields();
517 buff205a.assign(header("<ics205a>"));
518 make_buff205a();
519 write_205a(def_205a_filename);
520 using_ics205a_template = false;
521 }
522
523 void cb_205a_html()
524 {
525 std::string fname_name = fl_filename_name(def_205a_filename.c_str());
526 size_t p = fname_name.rfind('.');
527 if (p != std::string::npos) fname_name.erase(p);
528
529 std::string ics205a_fname = ICS_dir;
530 ics205a_fname.append(fname_name);
531 ics205a_fname.append(".html");
532
533 std::string html_text = "";
534 std::string empty = "<br>";
535
536 update_205afields();
537 std::string form205a = ics205a_html_template;
538
539 replacestr(form205a, TITLE, fname_name);
540 replacestr(form205a, ics205a_incident, s205a_incident );
541 replacestr(form205a, ics205a_date_fm, s205a_date_fm );
542 replacestr(form205a, ics205a_time_fm, s205a_time_fm );
543 replacestr(form205a, ics205a_date_to, s205a_date_to );
544 replacestr(form205a, ics205a_time_to, s205a_time_to );
545 replacestr(form205a, ics205a_prepared_by, s205a_prepared_by );
546 replacestr(form205a, ics205a_preparer_position, s205a_preparer_position );
547 replacestr(form205a, ics205a_preparer_date_time, s205a_preparer_date_time );
548
549 for (int i = 0; i < 32; i++) {
550 replacestr(form205a, ics205a_nn( ics205a_comm_position, i ),
551 s205a_comm_position[i].empty() ? empty : s205a_comm_position[i] );
552 replacestr(form205a, ics205a_nn( ics205a_comm_name, i ),
553 s205a_comm_name[i].empty() ? empty : s205a_comm_name[i] );
554 replacestr(form205a, ics205a_nn( ics205a_comm_info, i ),
555 s205a_comm_info[i].empty() ? empty : s205a_comm_info[i] );
556 }
557
558 FILE *file205a = fopen(ics205a_fname.c_str(), "w");
559 fprintf(file205a,"%s", form205a.c_str());
560 fclose(file205a);
561
562 open_url(ics205a_fname.c_str());
563 }
564
565 void cb_205a_msg_type()
566 {
567 if (tabs_msg_type->value() == tab_ics205a ) {
568 tab_ics205a_type->value(tab_205a_1);
569 show_filename(def_205a_filename);
570 }
571 }
572
573 void cb_205a_textout()
574 {
575 std::string ics205a_fname = ICS_dir;
576 ics205a_fname.append("ics205a.txt");
577
578 update_205afields();
579 std::string form205a = ics205a_text_template;
580
581 replacestr(form205a, ics205a_incident, s205a_incident );
582 replacestr(form205a, ics205a_date_fm, s205a_date_fm );
583 replacestr(form205a, ics205a_time_fm, s205a_time_fm );
584 replacestr(form205a, ics205a_date_to, s205a_date_to );
585 replacestr(form205a, ics205a_time_to, s205a_time_to );
586 replacestr(form205a, ics205a_prepared_by, s205a_prepared_by );
587 replacestr(form205a, ics205a_preparer_position, s205a_preparer_position );
588 replacestr(form205a, ics205a_preparer_date_time, s205a_preparer_date_time );
589
590 for (int i = 0; i < 32; i++) {
591 replacestr(form205a, ics205a_nn( ics205a_comm_position, i ), s205a_comm_position[i] );
592 replacestr(form205a, ics205a_nn( ics205a_comm_name, i ), s205a_comm_name[i] );
593 replacestr(form205a, ics205a_nn( ics205a_comm_info, i ), s205a_comm_info[i] );
594 }
595
596 FILE *file205a = fopen(ics205a_fname.c_str(), "w");
597 fprintf(file205a,"%s", form205a.c_str());
598 fclose(file205a);
599
600 open_url(ics205a_fname.c_str());
601 }