Debian Codegraph @

src/hics/hics213.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 std::string h213_buffer;
74 std::string h213_base_filename = "";
75 std::string h213_def_filename = "";
76 std::string h213_def_template_name = "";
77
78 bool h213_using_template = false;
79
80 std::string h213_tag_fm = ":1:";
81 std::string h213_tag_to = ":2:";
82 std::string h213_tag_date = ":3:";
83 std::string h213_tag_time = ":4:";
84 std::string h213_tag_via_phone = ":5a:";
85 std::string h213_tag_via_radio = ":5b:";
86 std::string h213_tag_via_other = ":5c:";
87 std::string h213_tag_reply_yes = ":6a:";
88 std::string h213_tag_reply_no = ":6b:";
89 std::string h213_tag_reply_to = ":6c:";
90 std::string h213_tag_priority = ":7:";
91 std::string h213_tag_high = ":7a:";
92 std::string h213_tag_medium = ":7b:";
93 std::string h213_tag_low = ":7c:";
94 std::string h213_tag_msg = ":8:";
95 std::string h213_tag_action = ":9:";
96 std::string h213_tag_rcvd_by = ":10:";
97 std::string h213_tag_time_rcvd = ":11:";
98 std::string h213_tag_fwd_to = ":12:";
99 std::string h213_tag_comments = ":13:";
100 std::string h213_tag_rcvd_by2 = ":14:";
101 std::string h213_tag_time_rcvd2 = ":15:";
102 std::string h213_tag_fwd_to2 = ":16:";
103 std::string h213_tag_comments2 = ":17:";
104 std::string h213_tag_facility = ":18:";
105
106 FIELD h213_fields[] = {
107 { h213_tag_to, "", (void **)&h213_txt_to, 't' },
108 { h213_tag_fm, "", (void **)&h213_txt_fm, 't' },
109 { h213_tag_date, "", (void **)&h213_txt_date, 'd' },
110 { h213_tag_time, "", (void **)&h213_txt_time, 't' },
111 { h213_tag_via_phone, "", (void **)&h213_btn_phone, 'b' },
112 { h213_tag_via_radio, "", (void **)&h213_btn_radio, 'b' },
113 { h213_tag_via_other, "", (void **)&h213_btn_other, 'b' },
114 { h213_tag_reply_yes, "", (void **)&h213_btn_yes, 'b' },
115 { h213_tag_reply_no, "", (void **)&h213_btn_no, 'b' },
116 { h213_tag_reply_to, "", (void **)&h213_txt_reply_to, 't' },
117 { h213_tag_high, "", (void **)&h213_btn_high, 'b' },
118 { h213_tag_medium, "", (void **)&h213_btn_medium, 'b' },
119 { h213_tag_low, "", (void **)&h213_btn_low, 'b' },
120 { h213_tag_msg, "", (void **)&h213_txt_msg, 'e' },
121 { h213_tag_action, "", (void **)&h213_txt_action, 'e' },
122 { h213_tag_rcvd_by, "", (void **)&h213_txt_rcvd_by, 't' },
123 { h213_tag_time_rcvd, "", (void **)&h213_txt_time_rcvd, 't' },
124 { h213_tag_fwd_to, "", (void **)&h213_txt_fwd_to, 't' },
125 { h213_tag_comments, "", (void **)&h213_txt_comments, 'e' },
126 { h213_tag_rcvd_by2, "", (void **)&h213_txt_rcvd_by2, 't' },
127 { h213_tag_time_rcvd2, "", (void **)&h213_txt_time_rcvd2, 't' },
128 { h213_tag_fwd_to2, "", (void **)&h213_txt_fwd_to2, 't' },
129 { h213_tag_comments2, "", (void **)&h213_txt_comments2, 'e' },
130 { h213_tag_facility, "", (void **)&h213_txt_facility, 't' } };
131
132 int h213_numfields = sizeof(h213_fields) / sizeof(FIELD);
133
134 void h213_cb_set_date()
135 {
136 h213_txt_date->value(szDate(progStatus.dtformat));
137 }
138
139 void h213_cb_set_time0()
140 {
141 h213_txt_time->value(szTime(progStatus.UTC));
142 }
143
144 void h213_cb_set_time1()
145 {
146 h213_txt_time_rcvd->value(szTime(progStatus.UTC));
147 }
148
149 void h213_cb_set_time2()
150 {
151 h213_txt_time_rcvd2->value(szTime(progStatus.UTC));
152 }
153
154 void h213_clear_fields()
155 {
156 for (int i = 0; i < h213_numfields; i++)
157 h213_fields[i].f_data.clear();
158 }
159
160 bool check_hics213fields()
161 {
162 for (int i = 0; i < h213_numfields; i++) {
163 if (h213_fields[i].w_type == 'd') {
164 if (h213_fields[i].f_data != ((Fl_DateInput *)(*h213_fields[i].w))->value())
165 return true;
166 } else if (h213_fields[i].w_type == 't') {
167 if (h213_fields[i].f_data != ((Fl_Input2 *)(*h213_fields[i].w))->value())
168 return true;
169 } else if (h213_fields[i].w_type == 'e') {
170 if (h213_fields[i].f_data != ((FTextEdit *)(*h213_fields[i].w))->buffer()->text())
171 return true;
172 } else if (h213_fields[i].w_type == 'b') {
173 std::string val = ((Fl_Check_Button *)(*h213_fields[i].w))->value() ? "X" : "";
174 if (h213_fields[i].f_data != val)
175 return true;
176 }
177 }
178 return false;
179 }
180
181 void h213_update_fields()
182 {
183 for (int i = 0; i < h213_numfields; i++) {
184 if (h213_fields[i].w_type == 'd') {
185 h213_fields[i].f_data = ((Fl_DateInput *)(*h213_fields[i].w))->value();
186 } else if (h213_fields[i].w_type == 't') {
187 h213_fields[i].f_data = ((Fl_Input2 *)(*h213_fields[i].w))->value();
188 } else if (h213_fields[i].w_type == 'e') {
189 h213_fields[i].f_data = ((FTextEdit *)(*h213_fields[i].w))->buffer()->text();
190 } else if (h213_fields[i].w_type == 'b') {
191 std::string val = ((Fl_Check_Button *)(*h213_fields[i].w))->value() ? "X" : "";
192 h213_fields[i].f_data = val;
193 }
194 }
195 }
196
197 void h213_clear_form()
198 {
199 h213_clear_fields();
200 for (int i = 0; i < h213_numfields; i++) {
201 if (h213_fields[i].w_type == 'd')
202 ((Fl_DateInput *)(*h213_fields[i].w))->value("");
203 else if (h213_fields[i].w_type == 'b')
204 ((Fl_Check_Button *)(*h213_fields[i].w))->value(0);
205 else if (h213_fields[i].w_type == 't')
206 ((Fl_Input2 *)(*h213_fields[i].w))->value("");
207 else if (h213_fields[i].w_type == 'e')
208 ((FTextEdit *)(*h213_fields[i].w))->clear();
209 }
210 h213_update_fields();
211 }
212
213 void h213_update_form()
214 {
215 for (int i = 0; i < h213_numfields; i++) {
216 if (h213_fields[i].w_type == 'd')
217 ((Fl_DateInput *)(*h213_fields[i].w))->value(h213_fields[i].f_data.c_str());
218 else if (h213_fields[i].w_type == 'b')
219 ((Fl_Check_Button *)(*h213_fields[i].w))->value(h213_fields[i].f_data == "X");
220 else if (h213_fields[i].w_type == 't')
221 ((Fl_Input2 *)(*h213_fields[i].w))->value(h213_fields[i].f_data.c_str());
222 else if (h213_fields[i].w_type == 'e') {
223 ((FTextEdit *)(*h213_fields[i].w))->clear();
224 ((FTextEdit *)(*h213_fields[i].w))->add(h213_fields[i].f_data.c_str());
225 }
226 }
227 }
228
229 void h213_make_buffer(bool compress = false)
230 {
231 std::string mbuff;
232 mbuff.clear();
233 for (int i = 0; i < h213_numfields; i++)
234 mbuff.append( lineout( h213_fields[i].f_type, h213_fields[i].f_data ) );
235 if (compress) compress_maybe(mbuff);
236 h213_buffer.append(mbuff);
237 }
238
239 void h213_read_buffer(std::string data)
240 {
241 h213_clear_fields();
242 read_header(data);
243 for (int i = 0; i < h213_numfields; i++)
244 h213_fields[i].f_data = findstr(data, h213_fields[i].f_type);
245 h213_update_form();
246 }
247
248 void h213_cb_new()
249 {
250 if (check_hics213fields()) {
251 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
252 update_header(CHANGED);
253 h213_cb_save();
254 }
255 }
256 h213_clear_form();
257 clear_header();
258 h213_def_filename = ICS_msg_dir;
259 h213_def_filename.append("new").append(HF213_EXT);
260 h213_using_template = false;
261 show_filename(h213_def_filename);
262 }
263
264 void h213_cb_import()
265 {
266 fl_alert2("Not implemented");
267 }
268
269 void h213_cb_export()
270 {
271 fl_alert2("Not implemented");
272 }
273
274 void h213_cb_wrap_import(std::string wrapfilename, std::string inpbuffer)
275 {
276 h213_clear_form();
277 h213_read_buffer(inpbuffer);
278 h213_def_filename = ICS_msg_dir;
279 h213_def_filename.append(wrapfilename);
280 show_filename(h213_def_filename);
281 h213_using_template = false;
282 }
283
284 int eval_h213_fsize()
285 {
286 Ccrc16 chksum;
287 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
288 evalstr.append(h213_base_filename).append("]");
289 h213_update_fields();
290 update_header(FROM);
291 evalstr.append(header("<hics213>"));
292 h213_buffer.clear();
293 h213_make_buffer(true);
294 if (h213_buffer.empty()) return 0;
295 compress_maybe( h213_buffer );
296 evalstr.append( h213_buffer );
297 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
298 return evalstr.length();
299 }
300
301 void h213_cb_wrap_export()
302 {
303 if (check_hics213fields()) {
304 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
305 return;
306 update_header(CHANGED);
307 }
308 h213_update_fields();
309
310 if (h213_base_filename == std::string("new").append(HF213_EXT) ||
311 h213_base_filename == std::string("default").append(HF213_EXT) )
312 if (!h213_cb_save_as()) return;
313
314 std::string wrapfilename = WRAP_send_dir;
315 wrapfilename.append(h213_base_filename);
316 wrapfilename.append(WRAP_EXT);
317 const char *p = FSEL::saveas(
318 "Save as wrap file",
319 "Wrap file\t*.{wrap,WRAP}",
320 wrapfilename.c_str());
321 if (p) {
322 std::string pext = fl_filename_ext(p);
323 wrapfilename = p;
324 update_header(FROM);
325 h213_buffer.assign(header("<hics213>"));
326 h213_make_buffer(true);
327 export_wrapfile(h213_base_filename, wrapfilename, h213_buffer, pext != WRAP_EXT);
328
329 h213_buffer.assign(header("<hics213>"));
330 h213_make_buffer(false);
331 h213_write(h213_def_filename);
332 }
333 }
334
335 void h213_cb_wrap_autosend()
336 {
337 if (check_hics213fields()) {
338 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
339 return;
340 update_header(CHANGED);
341 }
342 h213_update_fields();
343
344 if (h213_base_filename == std::string("new").append(HF213_EXT) ||
345 h213_base_filename == std::string("default").append(HF213_EXT) )
346 if (!h213_cb_save_as()) return;
347
348 update_header(FROM);
349 h213_buffer.assign(header("<hics213>"));
350 h213_make_buffer(true);
351 xfr_via_socket(h213_base_filename, h213_buffer);
352
353 h213_buffer.assign(header("<hics213>"));
354 h213_make_buffer(false);
355 h213_write(h213_def_filename);
356 }
357
358 void h213_cb_load_template()
359 {
360 std::string h213_def_filename = h213_def_template_name;
361 const char *p = FSEL::select(
362 "Open template file",
363 std::string("Template file\t*").append(HT213_EXT).c_str(),
364 h213_def_filename.c_str());
365 if (p) {
366 h213_clear_form();
367 read_data_file(p);
368 h213_def_template_name = p;
369 show_filename(h213_def_template_name);
370 h213_using_template = true;
371 }
372 }
373
374 void h213_cb_save_template()
375 {
376 if (!h213_using_template) {
377 cb_save_as_template();
378 return;
379 }
380 std::string h213_def_filename = h213_def_template_name;
381 const char *p = FSEL::saveas(
382 "Save template file",
383 std::string("Template file\t*").append(HT213_EXT).c_str(),
384 h213_def_filename.c_str());
385 if (p) {
386 update_header(CHANGED);
387 h213_update_fields();
388 h213_buffer.assign(header("<hics213>"));
389 h213_make_buffer();
390 h213_write(p);
391 }
392 }
393
394 void h213_cb_save_as_template()
395 {
396 std::string h213_def_filename = h213_def_template_name;
397 const char *p = FSEL::saveas(
398 "Save as template file",
399 std::string("Template file\t*").append(HT213_EXT).c_str(),
400 h213_def_filename.c_str());
401 if (p) {
402 const char *pext = fl_filename_ext(p);
403 h213_def_template_name = p;
404 if (strlen(pext) == 0) h213_def_template_name.append(HT213_EXT);
405 remove_spaces_from_filename(h213_def_template_name);
406 clear_header();
407 update_header(CHANGED);
408 h213_update_fields();
409 h213_buffer.assign(header("<hics213>"));
410 h213_make_buffer();
411 h213_write(h213_def_template_name);
412 show_filename(h213_def_template_name);
413 h213_using_template = true;
414 }
415 }
416
417 void h213_cb_open()
418 {
419 const char *p = FSEL::select(
420 _("Open data file"),
421 "HICS-213\t*.{H213}",
422 h213_def_filename.c_str());
423 if (!p) return;
424 if (strlen(p) == 0) return;
425 h213_clear_form();
426 read_data_file(p);
427 h213_using_template = false;
428 h213_def_filename = p;
429 show_filename(h213_def_filename);
430 }
431
432 void h213_write(std::string s)
433 {
434 FILE *hicsfile = fopen(s.c_str(), "w");
435 if (!hicsfile) return;
436
437 fwrite(h213_buffer.c_str(), h213_buffer.length(), 1, hicsfile);
438 fclose(hicsfile);
439 }
440
441 bool h213_cb_save_as()
442 {
443 const char *p;
444 std::string newfilename;
445
446 std::string name = named_file();
447 if (!name.empty()) {
448 name.append(HF213_EXT);
449 newfilename = ICS_msg_dir;
450 newfilename.append(name);
451 } else
452 newfilename = h213_def_filename;
453
454 p = FSEL::saveas(_("Save data file"), "HICS-213\t*.{H213}",
455 newfilename.c_str());
456
457 if (!p) return false;
458 if (strlen(p) == 0) return false;
459
460 if (progStatus.sernbr_fname) update_sernbr();
461
462 const char *pext = fl_filename_ext(p);
463 h213_def_filename = p;
464 if (strlen(pext) == 0) h213_def_filename.append(HF213_EXT);
465
466 remove_spaces_from_filename(h213_def_filename);
467 h213_update_fields();
468 update_header(NEW);
469 h213_buffer.assign(header("<hics213>"));
470 h213_make_buffer();
471 h213_write(h213_def_filename);
472
473 h213_using_template = false;
474 show_filename(h213_def_filename);
475 return true;
476 }
477
478 void h213_cb_save()
479 {
480 if (h213_base_filename == std::string("new").append(HF213_EXT) ||
481 h213_base_filename == std::string("default").append(HF213_EXT) ||
482 h213_using_template == true) {
483 h213_cb_save_as();
484 return;
485 }
486 if (check_hics213fields()) update_header(CHANGED);
487 h213_buffer.assign(header("<hics213>"));
488 h213_update_fields();
489 h213_make_buffer();
490 h213_write(h213_def_filename);
491 h213_using_template = false;
492 }
493
494 void h213_cb_html()
495 {
496 std::string fname_name = fl_filename_name(h213_def_filename.c_str());
497 size_t p = fname_name.rfind('.');
498 if (p != std::string::npos) fname_name.erase(p);
499
500 std::string hicsname = ICS_dir;
501 hicsname.append(fname_name);
502 hicsname.append(".html");
503
504 std::string html_text = "";
505
506 h213_update_fields();
507 std::string form = hics213_html_template;
508
509 std::string temp, temp2;
510 for (int i = 0; i < h213_numfields; i++) {
511 temp = h213_fields[i].f_type;
512 if (temp == h213_tag_msg || temp == h213_tag_action ||
513 temp == h213_tag_comments || temp == h213_tag_comments2 ) {
514 temp2 = h213_fields[i].f_data;
515 if (progStatus.autowordwrap)
516 temp2 = wordwrap(h213_fields[i].f_data, progStatus.charcount);
517 replacestr( form, h213_fields[i].f_type, temp2 );
518 } else
519 replacestr( form, h213_fields[i].f_type, h213_fields[i].f_data );
520 }
521
522 FILE *hicsfile = fopen(hicsname.c_str(), "w");
523 fprintf(hicsfile,"%s", form.c_str());
524 fclose(hicsfile);
525
526 open_url(hicsname.c_str());
527 }
528
529 void h213_cb_textout()
530 {
531 std::string hicsname = ICS_dir;
532 hicsname.append("hics213.txt");
533
534 h213_update_fields();
535 std::string form = hics213_text_template;
536
537 for (int i = 0; i < h213_numfields; i++)
538 replacestr( form, h213_fields[i].f_type, h213_fields[i].f_data);
539
540 FILE *hicsfile = fopen(hicsname.c_str(), "w");
541 fprintf(hicsfile,"%s", form.c_str());
542 fclose(hicsfile);
543
544 open_url(hicsname.c_str());
545 }