Debian Codegraph @

src/ics/ics213.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 // ICS213 fields
75 // compatibility fields required to read older data files
76
77 std::string a_213_inc = "<inc:";
78 std::string a_213_to = "<to:";
79 std::string a_213_fm = "<fm:";
80 std::string a_213_p1 = "<p1:";
81 std::string a_213_p2 = "<p2:";
82 std::string a_213_subj = "<sb:";
83 std::string a_213_d1 = "<d1:";
84 std::string a_213_t1 = "<t1:";
85 std::string a_213_msg = "<mg:";
86 std::string a_213_s1 = "<s1:";
87 std::string a_213_p3 = "<p3:";
88 std::string a_213_reply = "<rp:";
89 std::string a_213_d2 = "<d2:";
90 std::string a_213_t2 = "<t2:";
91 std::string a_213_s2 = "<s2:";
92 std::string a_213_p4 = "<p4:";
93
94 FIELD afields[] = {
95 { a_213_inc, "", (void **)&txt_213_inc, 't' },
96 { a_213_to, "", (void **)&txt_213_to, 't' },
97 { a_213_p1, "", (void **)&txt_213_p1, 't' },
98 { a_213_fm, "", (void **)&txt_213_fm, 't' },
99 { a_213_p2, "", (void **)&txt_213_p2, 't' },
100 { a_213_d1, "", (void **)&txt_213_d1, 'd' },
101 { a_213_t1, "", (void **)&txt_213_t1, 't' },
102 { a_213_subj, "", (void **)&txt_213_subj, 't' },
103 { a_213_s1, "", (void **)&txt_213_s1, 't' },
104 { a_213_p3, "", (void **)&txt_213_p3, 't' },
105 { a_213_s2, "", (void **)&txt_213_s2, 't' },
106 { a_213_p4, "", (void **)&txt_213_p4, 't' },
107 { a_213_d2, "", (void **)&txt_213_d2, 'd' },
108 { a_213_t2, "", (void **)&txt_213_t2, 't' },
109 { a_213_msg, "", (void **)&txt_213_msg, 'e' },
110 { a_213_reply, "", (void **)&txt_213_reply, 'e' } };
111
112 std::string _213_inc = ":inc:";
113 std::string _213_to = ":to:";
114 std::string _213_fm = ":fm:";
115 std::string _213_p1 = ":p1:";
116 std::string _213_p2 = ":p2:";
117 std::string _213_subj = ":sb:";
118 std::string _213_d1 = ":d1:";
119 std::string _213_t1 = ":t1:";
120 std::string _213_msg = ":mg:";
121 std::string _213_s1 = ":s1:";
122 std::string _213_p3 = ":p3:";
123 std::string _213_reply = ":rp:";
124 std::string _213_d2 = ":d2:";
125 std::string _213_t2 = ":t2:";
126 std::string _213_s2 = ":s2:";
127 std::string _213_p4 = ":p4:";
128
129 std::string ICS_213_msg = "";
130 std::string ICS_213_reply = "";
131 std::string buffer;
132
133 FIELD fields[] = {
134 { _213_inc, "", (void **)&txt_213_inc, 't' },
135 { _213_to, "", (void **)&txt_213_to, 't' },
136 { _213_p1, "", (void **)&txt_213_p1, 't' },
137 { _213_fm, "", (void **)&txt_213_fm, 't' },
138 { _213_p2, "", (void **)&txt_213_p2, 't' },
139 { _213_d1, "", (void **)&txt_213_d1, 'd' },
140 { _213_t1, "", (void **)&txt_213_t1, 't' },
141 { _213_subj, "", (void **)&txt_213_subj, 't' },
142 { _213_s1, "", (void **)&txt_213_s1, 't' },
143 { _213_p3, "", (void **)&txt_213_p3, 't' },
144 { _213_s2, "", (void **)&txt_213_s2, 't' },
145 { _213_p4, "", (void **)&txt_213_p4, 't' },
146 { _213_d2, "", (void **)&txt_213_d2, 'd' },
147 { _213_t2, "", (void **)&txt_213_t2, 't' },
148 { _213_msg, "", (void **)&txt_213_msg, 'e' },
149 { _213_reply, "", (void **)&txt_213_reply, 'e' } };
150
151 bool using_213Template = false;
152
153 std::string base_213_filename = "";
154 std::string def_213_filename = "";
155 std::string def_213_TemplateName = "";
156
157 int numfields = sizeof(fields) / sizeof(FIELD);
158
159 void cb_SetDate1()
160 {
161 txt_213_d1->value(szDate(progStatus.dtformat));
162 }
163
164 void cb_SetDate2()
165 {
166 txt_213_d2->value(szDate(progStatus.dtformat));
167 }
168
169 void cb_SetTime1()
170 {
171 txt_213_t1->value(szTime(progStatus.UTC));
172 }
173
174 void cb_SetTime2()
175 {
176 txt_213_t2->value(szTime(progStatus.UTC));
177 }
178
179 void clear_fields()
180 {
181 for (int i = 0; i < numfields; i++)
182 fields[i].f_data.clear();
183 }
184
185 bool check_fields()
186 {
187 for (int i = 0; i < numfields; i++) {
188 if (fields[i].w_type == 'd') {
189 if (fields[i].f_data != ((Fl_DateInput *)(*fields[i].w))->value())
190 return true;
191 } else if (fields[i].w_type == 't') {
192 if (fields[i].f_data != ((Fl_Input2 *)(*fields[i].w))->value())
193 return true;
194 } else if (fields[i].w_type == 'e') {
195 if (fields[i].f_data!= ((FTextEdit *)(*fields[i].w))->buffer()->text())
196 return true;
197 }
198 }
199 return false;
200 }
201
202 void update_fields()
203 {
204 bool changed = false;
205 for (int i = 0; i < numfields; i++) {
206 if (fields[i].w_type == 'd') {
207 if (fields[i].f_data != ((Fl_DateInput *)(*fields[i].w))->value()) {
208 changed = true;;
209 fields[i].f_data = ((Fl_DateInput *)(*fields[i].w))->value();
210 }
211 } else if (fields[i].w_type == 't') {
212 if (fields[i].f_data != ((Fl_Input2 *)(*fields[i].w))->value()) {
213 changed = true;;
214 fields[i].f_data = ((Fl_Input2 *)(*fields[i].w))->value();
215 }
216 } else if (fields[i].w_type == 'e') {
217 if (fields[i].f_data!= ((FTextEdit *)(*fields[i].w))->buffer()->text()) {
218 changed = true;;
219 fields[i].f_data = ((FTextEdit *)(*fields[i].w))->buffer()->text();
220 }
221 }
222 }
223 if (changed) update_header(CHANGED);
224 }
225
226 void clear_213_form()
227 {
228 clear_fields();
229 txt_213_inc->value("");
230 txt_213_to->value("");
231 txt_213_p1->value("");
232 txt_213_fm->value("");
233 txt_213_p2->value("");
234 txt_213_subj->value("");
235 txt_213_d1->value("");
236 txt_213_t1->value("");
237 txt_213_msg->clear();
238 txt_213_s1->value("");
239 txt_213_p3->value("");
240 txt_213_reply->clear();
241 txt_213_s2->value("");
242 txt_213_d2->value("");
243 txt_213_t2->value("");
244 txt_213_p4->value("");
245 update_fields();
246 }
247
248 void update_form213()
249 {
250 for (int i = 0; i < numfields; i++) {
251 if (fields[i].w_type == 'd')
252 ((Fl_DateInput *)(*fields[i].w))->value(fields[i].f_data.c_str());
253 else if (fields[i].w_type == 't')
254 ((Fl_Input2 *)(*fields[i].w))->value(fields[i].f_data.c_str());
255 else if (fields[i].w_type == 'e') {
256 ((FTextEdit *)(*fields[i].w))->clear();
257 ((FTextEdit *)(*fields[i].w))->add(fields[i].f_data.c_str());
258 }
259 }
260 }
261
262 void make_buffer(bool compress = false)
263 {
264 std::string mbuff;
265 mbuff.clear();
266 for (int i = 0; i < numfields; i++)
267 mbuff.append( lineout( fields[i].f_type, fields[i].f_data ) );
268 if (compress) compress_maybe(mbuff);
269 buffer.append(mbuff);
270 }
271
272 void read_213_buffer(std::string data)
273 {
274 bool data_ok = false;
275 clear_fields();
276 read_header(data);
277 for (int i = 0; i < numfields; i++) {
278 fields[i].f_data = findstr(data, fields[i].f_type);
279 if (!fields[i].f_data.empty()) data_ok = true;
280 }
281 if (!data_ok)
282 for (int i = 0; i < numfields; i++)
283 fields[i].f_data = findstr(data, afields[i].f_type);
284
285 update_form213();
286 }
287
288 void cb_213_new()
289 {
290 if (check_fields()) {
291 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
292 update_header(CHANGED);
293 cb_213_save();
294 }
295 }
296 clear_213_form();
297 clear_header();
298 def_213_filename = ICS_msg_dir;
299 def_213_filename.append("new").append(F213_EXT);
300 using_213Template = false;
301 show_filename(def_213_filename);
302 }
303
304 void cb_213_import()
305 {
306 std::string def_213_filename = ICS_dir;
307 def_213_filename.append("DEFAULT.XML");
308 const char *p = FSEL::select(
309 "Open Qforms xml file",
310 "Qforms xml\t*.{xml,XML}",
311 def_213_filename.c_str());
312 if (p){
313 clear_213_form();
314 clear_header();
315 qform_ics_import(p);
316 using_213Template = false;
317 }
318 }
319
320 void cb_213_export()
321 {
322 std::string def_213_filename = ICS_dir;
323 def_213_filename.append(base_213_filename);
324 def_213_filename.append(".XML");
325 const char *p = FSEL::saveas(
326 "Open Qforms xml file",
327 "Qforms xml\t*.{xml,XML}",
328 def_213_filename.c_str());
329 if (p) {
330 const char *pext = fl_filename_ext(p);
331 def_213_filename = p;
332 update_fields();
333 if (strlen(pext) == 0) def_213_filename.append(".XML");
334 qform_ics_export(def_213_filename);
335 }
336 }
337
338 void cb_213_wrap_import(std::string wrapfilename, std::string inpbuffer)
339 {
340 clear_213_form();
341 read_213_buffer(inpbuffer);
342 def_213_filename = ICS_msg_dir;
343 def_213_filename.append(wrapfilename);
344 using_213Template = false;
345 show_filename(def_213_filename);
346 }
347
348 int eval_213_fsize()
349 {
350 Ccrc16 chksum;
351 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
352 evalstr.append(base_213_filename).append("]");
353 update_fields();
354 update_header(FROM);
355 evalstr.append(header("<ics213>"));
356 buffer.clear();
357 make_buffer(true);
358 if (buffer.empty()) return 0;
359 compress_maybe( buffer );
360 evalstr.append( buffer );
361 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
362 return evalstr.length();
363 }
364
365 void cb_213_wrap_export()
366 {
367 if (check_fields()) {
368 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
369 return;
370 update_header(CHANGED);
371 }
372
373 if (base_213_filename == std::string("new").append(F213_EXT) ||
374 base_213_filename == std::string("default").append(F213_EXT) )
375 if (!cb_213_save_as()) return;
376
377 std::string wrapfilename = WRAP_send_dir;
378 wrapfilename.append(base_213_filename);
379 wrapfilename.append(WRAP_EXT);
380 const char *p = FSEL::saveas(
381 "Save as wrap file",
382 "Wrap file\t*.{wrap,WRAP}",
383 wrapfilename.c_str());
384 if (p) {
385 std::string pext = fl_filename_ext(p);
386 wrapfilename = p;
387 update_header(FROM);
388 update_fields();
389 buffer.assign(header("<ics213>"));
390 make_buffer(true);
391 export_wrapfile(base_213_filename, wrapfilename, buffer, pext != WRAP_EXT);
392
393 buffer.assign(header("<ics213>"));
394 make_buffer(false);
395 write_213(def_213_filename);
396 }
397 }
398
399 void cb_213_wrap_autosend()
400 {
401 if (check_fields()) {
402 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
403 return;
404 update_header(CHANGED);
405 }
406
407 if (base_213_filename == std::string("new").append(F213_EXT) ||
408 base_213_filename == std::string("default").append(F213_EXT) )
409 if (!cb_213_save_as()) return;
410
411 update_header(FROM);
412 update_fields();
413 buffer.assign(header("<ics213>"));
414 make_buffer(true);
415 xfr_via_socket(base_213_filename, buffer);
416
417 buffer.assign(header("<ics213>"));
418 make_buffer(false);
419 write_213(def_213_filename);
420 }
421
422 void cb_213_load_template()
423 {
424 std::string def_213_filename = def_213_TemplateName;
425 const char *p = FSEL::select(
426 "Open template file",
427 std::string("Template file\t*").append(T213_EXT).c_str(),
428 def_213_filename.c_str());
429 if (p) {
430 clear_213_form();
431 read_data_file(p);
432 def_213_TemplateName = p;
433 show_filename(def_213_TemplateName);
434 using_213Template = true;
435 }
436 }
437
438 void cb_213_save_template()
439 {
440 if (!using_213Template) {
441 cb_save_as_template();
442 return;
443 }
444 std::string def_213_filename = def_213_TemplateName;
445 const char *p = FSEL::saveas(
446 "Save template file",
447 std::string("Template file\t*").append(T213_EXT).c_str(),
448 def_213_filename.c_str());
449 if (p) {
450 update_header(CHANGED);
451 update_fields();
452 buffer.assign(header("<ics213>"));
453 make_buffer();
454 write_213(p);
455 }
456 }
457
458 void cb_213_save_as_template()
459 {
460 std::string def_213_filename = def_213_TemplateName;
461 const char *p = FSEL::saveas(
462 "Save as template file",
463 std::string("Template file\t*").append(T213_EXT).c_str(),
464 def_213_filename.c_str());
465 if (p) {
466 const char *pext = fl_filename_ext(p);
467 def_213_TemplateName = p;
468 if (strlen(pext) == 0) def_213_TemplateName.append(T213_EXT);
469 remove_spaces_from_filename(def_213_TemplateName);
470 clear_header();
471 update_header(CHANGED);
472 update_fields();
473 buffer.assign(header("<ics213>"));
474 make_buffer();
475 write_213(def_213_TemplateName);
476 show_filename(def_213_TemplateName);
477 using_213Template = true;
478 }
479 }
480
481 void cb_213_open()
482 {
483 const char *p = FSEL::select(_("Open data file"), "ICS-213\t*.{213,f2s}",
484 def_213_filename.c_str());
485 if (!p) return;
486 if (strlen(p) == 0) return;
487 clear_213_form();
488 read_data_file(p);
489 using_213Template = false;
490 def_213_filename = p;
491 show_filename(def_213_filename);
492 }
493
494 void write_213(std::string s)
495 {
496 FILE *icsfile = fopen(s.c_str(), "w");
497 if (!icsfile) return;
498
499 fwrite(buffer.c_str(), buffer.length(), 1, icsfile);
500 fclose(icsfile);
501 }
502
503 bool cb_213_save_as()
504 {
505 const char *p;
506 std::string newfilename;
507
508 std::string name = named_file();
509 if (!name.empty()) {
510 name.append(F213_EXT);
511 newfilename = ICS_msg_dir;
512 newfilename.append(name);
513 } else
514 newfilename = def_213_filename;
515
516 p = FSEL::saveas(_("Save data file"), "ICS-213\t*.{213T,f2t}",
517 newfilename.c_str());
518
519 if (!p) return false;
520 if (strlen(p) == 0) return false;
521
522 if (progStatus.sernbr_fname) update_sernbr();
523
524 const char *pext = fl_filename_ext(p);
525 def_213_filename = p;
526 if (strlen(pext) == 0) def_213_filename.append(F213_EXT);
527
528 remove_spaces_from_filename(def_213_filename);
529 update_header(NEW);
530 update_fields();
531 buffer.assign(header("<ics213>"));
532 make_buffer();
533 write_213(def_213_filename);
534
535 using_213Template = false;
536 show_filename(def_213_filename);
537 return true;
538 }
539
540 void cb_213_save()
541 {
542 if (base_213_filename == std::string("new").append(F213_EXT) ||
543 base_213_filename == std::string("default").append(F213_EXT) ||
544 using_213Template == true) {
545 cb_213_save_as();
546 return;
547 }
548 if (check_fields()) update_header(CHANGED);
549 update_fields();
550 buffer.assign(header("<ics213>"));
551 make_buffer();
552 write_213(def_213_filename);
553 using_213Template = false;
554 }
555
556 void cb_213_html()
557 {
558 std::string fname_name = fl_filename_name(def_213_filename.c_str());
559 size_t p = fname_name.rfind('.');
560 if (p != std::string::npos) fname_name.erase(p);
561
562 std::string icsname = ICS_dir;
563 icsname.append(fname_name);
564 icsname.append(".html");
565
566 std::string html_text = "";
567
568 update_fields();
569 std::string form = ics213_html_template;
570
571 replacestr(form, TITLE, fname_name);
572 for (int i = 0; i < numfields; i++) {
573 if (fields[i].w_type != 'e')
574 replacestr( form, fields[i].f_type, fields[i].f_data );
575 else {
576 std::string ww = wordwrap(fields[i].f_data, progStatus.charcount);
577 int lfcount = 0;
578 size_t pos = 0;
579 while ((pos = ww.find("\n", pos)) != std::string::npos){
580 lfcount++; pos++;
581 }
582 if (lfcount < 10) for (int i = 0; i < (10 - lfcount); i++) ww.append("\n");
583 html_text.assign("<pre>");
584 html_text.append(ww);
585 html_text.append("</pre>");
586 replacestr( form, fields[i].f_type, html_text );
587 }
588 }
589
590 for (int i = 0; i < numfields; i++)
591 replacestr( form, fields[i].f_type, fields[i].f_data );
592
593 FILE *icsfile = fopen(icsname.c_str(), "w");
594 fprintf(icsfile,"%s", form.c_str());
595 fclose(icsfile);
596
597 open_url(icsname.c_str());
598 }
599
600 void cb_213_textout()
601 {
602 std::string icsname = ICS_dir;
603 icsname.append("ics213.txt");
604
605 update_fields();
606 std::string form = ics213_text_template;
607
608 for (int i = 0; i < numfields; i++)
609 replacestr( form, fields[i].f_type, fields[i].f_data);
610
611 FILE *icsfile = fopen(icsname.c_str(), "w");
612 fprintf(icsfile,"%s", form.c_str());
613 fclose(icsfile);
614
615 open_url(icsname.c_str());
616 }