Debian Codegraph @

src/mars/mars_msg.cxx

raw
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2021
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 using namespace std;
74
75 const char msg_precedent[] = "RPIF";
76
77 std::string mars_msg_de = ":de:";
78 std::string mars_msg_serno = ":serno:";
79 std::string mars_msg_prec = ":pre:";
80 std::string mars_msg_dtg = ":dtg:";
81 std::string mars_msg_fm = ":fm:";
82 std::string mars_msg_to = ":to:";
83 std::string mars_msg_info = ":info:";
84 std::string mars_msg_subj = ":subj:";
85 std::string mars_msg_text = ":text:";
86
87 std::string s_mars_msg_de;
88 std::string s_mars_msg_serno;
89 std::string s_mars_msg_prec = "R";
90 std::string s_mars_msg_dtg;
91 std::string s_mars_msg_fm;
92 std::string s_mars_msg_to;
93 std::string s_mars_msg_subj;
94 std::string s_mars_msg_info;
95 std::string s_mars_msg_text;
96
97 std::string buffmars_msg;
98 std::string def_mars_msg_filename = "";
99 std::string base_mars_msg_filename = "";
100 std::string def_mars_msg_TemplateName = "";
101
102 bool using_mars_msg_template = false;
103
104 void clear_mars_msgfields()
105 {
106 s_mars_msg_de.clear();
107 s_mars_msg_serno.clear();
108
109 s_mars_msg_prec = "R";
110 s_mars_msg_dtg.clear();
111 s_mars_msg_fm.clear();
112 s_mars_msg_to.clear();
113 s_mars_msg_subj.clear();
114 s_mars_msg_info.clear();
115 s_mars_msg_text.clear();
116 }
117
118 bool check_mars_msgfields()
119 {
120 std::string temp;
121 temp = msg_precedent[sel_mars_msg_prec->index()];
122 if (s_mars_msg_prec != temp) return true;
123
124 if (s_mars_msg_de != txt_mars_msg_de->value()) return true;
125 if (s_mars_msg_serno != txt_mars_msg_serno->value()) return true;
126
127 if (s_mars_msg_dtg != txt_mars_msg_dtg->value()) return true;
128 if (s_mars_msg_fm != txt_mars_msg_fm->value()) return true;
129 if (s_mars_msg_serno != txt_mars_msg_serno->value()) return true;
130 if (s_mars_msg_to != txt_mars_msg_to->buffer()->text()) return true;
131 if (s_mars_msg_info != txt_mars_msg_info->buffer()->text()) return true;
132 if (s_mars_msg_subj != txt_mars_msg_subj->value()) return true;
133 if (s_mars_msg_text != txt_mars_msg_text->buffer()->text()) return true;
134 return false;
135 }
136
137 void update_mars_msgfields()
138 {
139 s_mars_msg_de = txt_mars_msg_de->value();
140 s_mars_msg_serno = txt_mars_msg_serno->value();
141
142 s_mars_msg_prec.clear();
143 s_mars_msg_prec = msg_precedent[sel_mars_msg_prec->index()];
144 s_mars_msg_dtg = txt_mars_msg_dtg->value();
145 s_mars_msg_fm = txt_mars_msg_fm->value();
146 s_mars_msg_to = txt_mars_msg_to->buffer()->text();
147 s_mars_msg_info = txt_mars_msg_info->buffer()->text();
148 s_mars_msg_subj = txt_mars_msg_subj->value();
149 s_mars_msg_text = txt_mars_msg_text->buffer()->text();
150 }
151
152 void update_mars_msgform()
153 {
154 size_t n = strchr(msg_precedent, s_mars_msg_prec[0]) - msg_precedent;
155 if (n < 0) n = 0;
156 if (n > 4) n = 4;
157 sel_mars_msg_prec->index(n);
158
159 txt_mars_msg_de->value(s_mars_msg_de.c_str());
160 txt_mars_msg_serno->value(s_mars_msg_serno.c_str());
161
162 txt_mars_msg_dtg->value(s_mars_msg_dtg.c_str());
163 txt_mars_msg_fm->value(s_mars_msg_fm.c_str());
164 txt_mars_msg_to->add(s_mars_msg_to.c_str());
165 txt_mars_msg_info->add(s_mars_msg_info.c_str());
166 txt_mars_msg_subj->value(s_mars_msg_subj.c_str());
167 txt_mars_msg_text->add(s_mars_msg_text.c_str());
168
169 }
170
171 void clear_mars_msg_form()
172 {
173 clear_mars_msgfields();
174
175 txt_mars_msg_de->value("");
176 txt_mars_msg_serno->value("");
177
178 sel_mars_msg_prec->index(0);
179 txt_mars_msg_dtg->value("");
180 txt_mars_msg_fm->value("");
181
182 txt_mars_msg_to->clear();
183 txt_mars_msg_info->clear();
184 txt_mars_msg_subj->value("");
185 txt_mars_msg_text->clear();
186 }
187
188 void make_buffmars_msg(bool compress = false)
189 {
190 std::string mbuff;
191 mbuff.clear();
192 mbuff.append( lineout( mars_msg_de, s_mars_msg_de ) );
193 mbuff.append( lineout( mars_msg_serno, s_mars_msg_serno ) );
194 mbuff.append( lineout( mars_msg_prec, s_mars_msg_prec ) );
195 mbuff.append( lineout( mars_msg_dtg, s_mars_msg_dtg ) );
196 mbuff.append( lineout( mars_msg_fm, s_mars_msg_fm ) );
197 mbuff.append( lineout( mars_msg_to, s_mars_msg_to ) );
198 mbuff.append( lineout( mars_msg_info, s_mars_msg_info ) );
199 mbuff.append( lineout( mars_msg_subj, s_mars_msg_subj ) );
200 mbuff.append( lineout( mars_msg_text, s_mars_msg_text ) );
201 if (compress) compress_maybe(mbuff);
202 buffmars_msg.append(mbuff);
203 }
204
205 void read_mars_msg_buffer(std::string data)
206 {
207 clear_mars_msgfields();
208
209 s_mars_msg_de = findstr( data, mars_msg_de );
210 s_mars_msg_serno = findstr( data, mars_msg_serno );
211 s_mars_msg_prec = findstr( data, mars_msg_prec );
212 s_mars_msg_dtg = findstr( data, mars_msg_dtg );
213 s_mars_msg_fm = findstr( data, mars_msg_fm );
214 s_mars_msg_to = findstr( data, mars_msg_to );
215 s_mars_msg_info = findstr( data, mars_msg_info );
216 s_mars_msg_subj = findstr( data, mars_msg_subj );
217 s_mars_msg_text = findstr( data, mars_msg_text );
218 update_mars_msgform();
219 }
220
221 void cb_mars_msg_new()
222 {
223 if (check_mars_msgfields()) {
224 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
225 update_header(CHANGED);
226 cb_mars_msg_save();
227 }
228 }
229 clear_mars_msg_form();
230 def_mars_msg_filename = ICS_msg_dir;
231 def_mars_msg_filename.append("new").append(FMARSMSG_EXT);
232 show_filename(def_mars_msg_filename);
233 using_mars_msg_template = false;
234 }
235
236 void cb_mars_msg_import()
237 {
238 fl_alert2("Not implemented");
239 }
240
241 void cb_mars_msg_export()
242 {
243 fl_alert2("Not implemented");
244 }
245
246 void cb_mars_msg_wrap_import(std::string wrapfilename, std::string inpbuffer)
247 {
248 clear_mars_msg_form();
249 read_mars_msg_buffer(inpbuffer);
250 def_mars_msg_filename = ICS_msg_dir;
251 def_mars_msg_filename.append(wrapfilename);
252 show_filename(def_mars_msg_filename);
253 using_mars_msg_template = false;
254 }
255
256 int eval_mars_msg_fsize()
257 {
258 Ccrc16 chksum;
259 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
260 evalstr.append(base_mars_msg_filename).append("]");
261 update_mars_msgfields();
262 update_header(FROM);
263 evalstr.append(header("<mars_msg>"));
264 buffmars_msg.clear();
265 make_buffmars_msg(true);
266 if (buffmars_msg.empty()) return 0;
267 compress_maybe( buffmars_msg );
268 evalstr.append( buffmars_msg );
269 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
270 return evalstr.length();
271 }
272
273 void cb_mars_msg_wrap_export()
274 {
275 if (check_mars_msgfields()) {
276 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
277 return;
278 update_header(CHANGED);
279 }
280 update_mars_msgfields();
281
282 if (base_mars_msg_filename == std::string("new").append(FMARSMSG_EXT) ||
283 base_mars_msg_filename == std::string("default").append(FMARSMSG_EXT))
284 if (!cb_mars_msg_save_as()) return;
285
286 std::string wrapfilename = WRAP_send_dir;
287 wrapfilename.append(base_mars_msg_filename);
288 wrapfilename.append(".wrap");
289 const char *p = FSEL::saveas(
290 "Save as wrap file",
291 "Wrap file\t*.{wrap,WRAP}",
292 wrapfilename.c_str());
293 if (p) {
294 std::string pext = fl_filename_ext(p);
295 wrapfilename = p;
296 update_header(FROM);
297 buffmars_msg.assign(header("<mars_msg>"));
298 make_buffmars_msg(true);
299 export_wrapfile(base_mars_msg_filename, wrapfilename, buffmars_msg, pext != ".wrap");
300
301 buffmars_msg.assign(header("<mars_msg>"));
302 make_buffmars_msg(false);
303 write_mars_msg(def_mars_msg_filename);
304 }
305 }
306
307 void cb_mars_msg_wrap_autosend()
308 {
309 if (check_mars_msgfields()) {
310 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
311 return;
312 update_header(CHANGED);
313 }
314 update_mars_msgfields();
315
316 if (base_mars_msg_filename == std::string("new").append(FMARSMSG_EXT) ||
317 base_mars_msg_filename == std::string("default").append(FMARSMSG_EXT) )
318 cb_mars_msg_save_as();
319
320 update_header(FROM);
321 buffmars_msg.assign(header("<mars_msg>"));
322 make_buffmars_msg(true);
323 xfr_via_socket(base_mars_msg_filename, buffmars_msg);
324
325 buffmars_msg.assign(header("<mars_msg>"));
326 make_buffmars_msg(false);
327 write_mars_msg(def_mars_msg_filename);
328 }
329
330 void cb_mars_msg_load_template()
331 {
332 std::string def_mars_msg_filename = def_mars_msg_TemplateName;
333 const char *p = FSEL::select(
334 "Open template file",
335 std::string("Template file\t*").append(TMARSMSG_EXT).c_str(),
336 def_mars_msg_filename.c_str());
337 if (p) {
338 clear_mars_msg_form();
339 read_data_file(p);
340 def_mars_msg_TemplateName = p;
341 show_filename(def_mars_msg_TemplateName);
342 using_mars_msg_template = true;
343 }
344 }
345
346 void cb_mars_msg_save_template()
347 {
348 if (!using_mars_msg_template) {
349 cb_mars_msg_save_as_template();
350 return;
351 }
352 std::string def_mars_msg_filename = def_mars_msg_TemplateName;
353 const char *p = FSEL::saveas(
354 "Save template file",
355 std::string("Template file\t*").append(TMARSMSG_EXT).c_str(),
356 def_mars_msg_filename.c_str());
357 if (p) {
358 update_header(CHANGED);
359 update_mars_msgfields();
360 buffmars_msg.assign(header("<mars_msg>"));
361 make_buffmars_msg();
362 write_mars_msg(p);
363 }
364 }
365
366 void cb_mars_msg_save_as_template()
367 {
368 std::string def_mars_msg_filename = def_mars_msg_TemplateName;
369 const char *p = FSEL::saveas(
370 "Save as template file",
371 std::string("Template file\t*").append(TMARSMSG_EXT).c_str(),
372 def_mars_msg_filename.c_str());
373 if (p) {
374 const char *pext = fl_filename_ext(p);
375 def_mars_msg_TemplateName = p;
376 if (strlen(pext) == 0) def_mars_msg_TemplateName.append(TMARSMSG_EXT);
377 remove_spaces_from_filename(def_mars_msg_TemplateName);
378
379 clear_header();
380 update_header(CHANGED);
381 buffmars_msg.assign(header("<mars_msg>"));
382 make_buffmars_msg();
383 write_mars_msg(def_mars_msg_TemplateName);
384
385 show_filename(def_mars_msg_TemplateName);
386 using_mars_msg_template = true;
387 }
388 }
389
390 void cb_mars_msg_open()
391 {
392 const char *p = FSEL::select(
393 _("Open data file"),
394 std::string("ICS-mars_msg\t*").append(FMARSMSG_EXT).c_str(),
395 def_mars_msg_filename.c_str());
396 if (!p) return;
397 if (strlen(p) == 0) return;
398 clear_mars_msg_form();
399 read_data_file(p);
400 using_mars_msg_template = false;
401 def_mars_msg_filename = p;
402 show_filename(def_mars_msg_filename);
403 }
404
405 void write_mars_msg(std::string s)
406 {
407 FILE *filemars_msg = fopen(s.c_str(), "w");
408 if (!filemars_msg) return;
409
410 fwrite(buffmars_msg.c_str(), buffmars_msg.length(), 1, filemars_msg);
411 fclose(filemars_msg);
412 }
413
414 bool cb_mars_msg_save_as()
415 {
416 const char *p;
417 std::string newfilename;
418
419 std::string name = named_file();
420 if (!name.empty()) {
421 name.append(FMARSMSG_EXT);
422 newfilename = ICS_msg_dir;
423 newfilename.append(name);
424 } else
425 newfilename = def_mars_msg_filename;
426
427 p = FSEL::saveas(_("Save data file"),
428 std::string("ICS-mars_msg\t*").append(FMARSMSG_EXT).c_str(),
429 newfilename.c_str());
430
431 if (!p) return false;
432 if (strlen(p) == 0) return false;
433
434 if (progStatus.sernbr_fname) {
435 std::string haystack = p;
436 if (haystack.find(newfilename) != std::string::npos) {
437 int n = atoi(progStatus.sernbr.c_str());
438 n++;
439 char szn[15];
440 snprintf(szn, sizeof(szn), "%d", n);
441 progStatus.sernbr = szn;
442 txt_sernbr->value(szn);
443 txt_sernbr->redraw();
444 }
445 }
446
447 const char *pext = fl_filename_ext(p);
448 def_mars_msg_filename = p;
449 if (strlen(pext) == 0) def_mars_msg_filename.append(FMARSMSG_EXT);
450
451 remove_spaces_from_filename(def_mars_msg_filename);
452
453 update_header(NEW);
454 update_mars_msgfields();
455 buffmars_msg.assign(header("<mars_msg>"));
456 make_buffmars_msg();
457 write_mars_msg(def_mars_msg_filename);
458
459 using_mars_msg_template = false;
460 show_filename(def_mars_msg_filename);
461 return true;
462 }
463
464 void cb_mars_msg_save()
465 {
466 if (base_mars_msg_filename == std::string("new").append(FMARSMSG_EXT) ||
467 base_mars_msg_filename == std::string("default").append(FMARSMSG_EXT) ||
468 using_mars_msg_template == true) {
469 cb_mars_msg_save_as();
470 return;
471 }
472
473 if (check_mars_msgfields()) update_header(CHANGED);
474 update_mars_msgfields();
475 buffmars_msg.assign(header("<mars_msg>"));
476 make_buffmars_msg();
477 write_mars_msg(def_mars_msg_filename);
478
479 using_mars_msg_template = false;
480 }
481
482 void cb_mars_msg_html()
483 {
484 std::string fname_name = fl_filename_name(def_mars_msg_filename.c_str());
485 size_t p = fname_name.rfind('.');
486 if (p != std::string::npos) fname_name.erase(p);
487
488 std::string mars_msg_fname = ICS_dir;
489 mars_msg_fname.append(fname_name);
490 mars_msg_fname.append(".html");
491
492 update_mars_msgfields();
493 std::string formmars_msg = mars_msg_html_template;
494
495 replacestr(formmars_msg, TITLE, fname_name);
496
497 std::string temp = s_mars_msg_prec;
498 if (!s_mars_msg_de.empty()) {
499 temp.assign("DE ").append(s_mars_msg_de).append(" ");
500 if (!s_mars_msg_serno.empty()) {
501 if (s_mars_msg_serno.length() < 4)
502 temp.append(4-s_mars_msg_serno.length(),'0');
503 temp.append(s_mars_msg_serno);
504 }
505 temp.append("\n");
506 temp.append(s_mars_msg_prec);
507 }
508 replacestr(formmars_msg, mars_msg_prec, temp );
509 replacestr(formmars_msg, mars_msg_dtg, s_mars_msg_dtg );
510 replacestr(formmars_msg, mars_msg_fm, s_mars_msg_fm );
511 temp = s_mars_msg_to;
512 if (!s_mars_msg_info.empty())
513 temp.append("\nINFO ").append(s_mars_msg_info);
514 replacestr(formmars_msg, mars_msg_to, temp );
515
516 std::string text = "";
517 temp = "";
518 if (!s_mars_msg_subj.empty()) {
519 temp = "SUBJ: "; temp.append(s_mars_msg_subj);
520 temp = maxchars(temp, 69, 6);
521 text = temp;
522 text += '\n';
523 }
524 temp = maxchars(s_mars_msg_text, 69);
525 text.append(temp);
526
527 replacestr(formmars_msg, mars_msg_text, text );
528
529 FILE *filemars_msg = fopen(mars_msg_fname.c_str(), "w");
530 fprintf(filemars_msg,"%s", formmars_msg.c_str());
531 fclose(filemars_msg);
532
533 open_url(mars_msg_fname.c_str());
534 }
535
536 void cb_mars_msg_msg_type()
537 {
538 if (tabs_msg_type->value() == tab_mars_msg ) {
539 show_filename(def_mars_msg_filename);
540 } else {
541 show_filename(def_rg_filename);
542 }
543 }
544
545 void cb_mars_msg_textout()
546 {
547 std::string mars_msg_fname = ICS_dir;
548 mars_msg_fname.append("mars_msg.txt");
549
550 update_mars_msgfields();
551 std::string formmars_msg = mars_msg_text_template;
552
553 std::string temp = s_mars_msg_prec;
554 if (!s_mars_msg_de.empty()) {
555 temp.assign("DE ").append(s_mars_msg_de).append(" ");
556 if (!s_mars_msg_serno.empty()) {
557 if (s_mars_msg_serno.length() < 4)
558 temp.append(4-s_mars_msg_serno.length(),'0');
559 temp.append(s_mars_msg_serno);
560 }
561 temp.append("\n");
562 temp.append(s_mars_msg_prec);
563 }
564 replacestr(formmars_msg, mars_msg_prec, temp );
565 replacestr(formmars_msg, mars_msg_dtg, s_mars_msg_dtg );
566 replacestr(formmars_msg, mars_msg_fm, s_mars_msg_fm );
567 temp = s_mars_msg_to;
568 if (!s_mars_msg_info.empty())
569 temp.append("\nINFO ").append(s_mars_msg_info);
570 replacestr(formmars_msg, mars_msg_to, temp );
571
572 std::string text = "";
573 temp = "";
574 if (!s_mars_msg_subj.empty()) {
575 temp = "SUBJ: "; temp.append(s_mars_msg_subj);
576 temp = maxchars(temp, 69, 6);
577 text = temp;
578 text += '\n';
579 }
580 temp = maxchars(s_mars_msg_text, 69);
581 text.append(temp);
582
583 replacestr(formmars_msg, mars_msg_text, text );
584
585 FILE *filemars_msg = fopen(mars_msg_fname.c_str(), "w");
586 fprintf(filemars_msg,"%s", formmars_msg.c_str());
587 fclose(filemars_msg);
588
589 open_url(mars_msg_fname.c_str());
590 }
591