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 using namespace std;
74 
75 const char mars_net_precedent[] = "RPOZ";
76 
77 string buffnet;
78 string def_mars_net_filename = "";
79 string base_mars_net_filename = "";
80 string def_mars_net_TemplateName = "";
81 bool using_mars_net_template = false;
82 
83 // mars_net fields
84 
85 string mars_net_DE			= ":de:";
86 string mars_net_NBR			= ":nbr:";
87 string mars_net_PREC		= ":prec:";
88 string mars_net_DTG			= ":dtg:";
89 string mars_net_FMNAME		= ":fmname:";
90 string mars_net_FMCALL		= ":fmcall:";
91 string mars_net_FMSTATE		= ":fmstate:";
92 string mars_net_TOPOS		= ":topos:";
93 string mars_net_TOCALL		= ":tocall:";
94 string mars_net_TOSTATE		= ":tostate:";
95 string mars_net_INFOPOS	= ":INFOpos:";
96 string mars_net_INFOCALL	= ":INFOcall:";
97 string mars_net_INFOSTATE	= ":INFOstate:";
98 string mars_net_INCIDENT	= ":incident:";
99 string mars_net_DND			= ":dnd:";
100 string mars_net_NETCALL		= ":netcall:";
101 string mars_net_DTGSTART	= ":dtgstart:";
102 string mars_net_DTGEND		= ":dtgend:";
103 string mars_net_NETSB		= ":netsb:";
104 string mars_net_NCSCALL		= ":ncscall:";
105 string mars_net_NBRSTAS		= ":nbrstas:";
106 string mars_net_CALLS		= ":calls:";
107 string mars_net_NBRMSGS		= ":nbrmsgs:";
108 string mars_net_COMMENTS	= ":comments:";
109 
110 string s_mars_net_DE;
111 string s_mars_net_NBR;
112 string s_mars_net_PREC = "R";
113 string s_mars_net_DTG;
114 string s_mars_net_FMNAME;
115 string s_mars_net_FMCALL;
116 string s_mars_net_FMSTATE;
117 string s_mars_net_TOPOS;
118 string s_mars_net_TOCALL;
119 string s_mars_net_TOSTATE;
120 string s_mars_net_INFOPOS;
121 string s_mars_net_INFOCALL;
122 string s_mars_net_INFOSTATE;
123 string s_mars_net_INCIDENT;
124 string s_mars_net_DND;
125 string s_mars_net_NETCALL;
126 string s_mars_net_DTGSTART;
127 string s_mars_net_DTGEND;
128 string s_mars_net_NETSB;
129 string s_mars_net_NCSCALL;
130 string s_mars_net_NBRSTAS;
131 string s_mars_net_CALLS;
132 string s_mars_net_NBRMSGS;
133 string s_mars_net_COMMENTS;
134 
cb_mars_net_SetDTG()135 void cb_mars_net_SetDTG()
136 {
137 	txt_mars_net_DTG->value(szDateTime());
138 }
139 
cb_mars_net_SetDTGSTART()140 void cb_mars_net_SetDTGSTART()
141 {
142 	txt_mars_net_DTGSTART->value(szDateTime());
143 }
144 
cb_mars_net_SetDTGEND()145 void cb_mars_net_SetDTGEND()
146 {
147 	txt_mars_net_DTGEND->value(szDateTime());
148 }
149 
clear_mars_net_fields()150 void clear_mars_net_fields()
151 {
152 	s_mars_net_DE.clear();
153 	s_mars_net_NBR.clear();
154 	s_mars_net_PREC = "R";
155 	s_mars_net_DTG.clear();
156 	s_mars_net_FMNAME.clear();
157 	s_mars_net_FMCALL.clear();
158 	s_mars_net_FMSTATE.clear();
159 	s_mars_net_TOPOS.clear();
160 	s_mars_net_TOCALL.clear();
161 	s_mars_net_TOSTATE.clear();
162 	s_mars_net_INFOPOS.clear();
163 	s_mars_net_INFOCALL.clear();
164 	s_mars_net_INFOSTATE.clear();
165 	s_mars_net_INCIDENT.clear();
166 	s_mars_net_DND.clear();
167 	s_mars_net_NETCALL.clear();
168 	s_mars_net_DTGSTART.clear();
169 	s_mars_net_DTGEND.clear();
170 	s_mars_net_NETSB.clear();
171 	s_mars_net_NCSCALL.clear();
172 	s_mars_net_NBRSTAS.clear();
173 	s_mars_net_CALLS.clear();
174 	s_mars_net_NBRMSGS.clear();
175 	s_mars_net_COMMENTS.clear();
176 
177 }
178 
check_mars_net_fields()179 bool check_mars_net_fields()
180 {
181 	string temp;
182 	if (s_mars_net_DE != txt_mars_net_DE->value()) return true;
183 	if (s_mars_net_NBR != txt_mars_net_NBR->value()) return true;
184 	temp = mars_net_precedent[sel_mars_net_PREC->index()];
185 	if (s_mars_net_PREC != temp) return true;
186 	if (s_mars_net_DTG != txt_mars_net_DTG->value()) return true;
187 	if (s_mars_net_FMNAME != txt_mars_net_FMNAME->value()) return true;
188 	if (s_mars_net_FMCALL != txt_mars_net_FMCALL->value()) return true;
189 	if (s_mars_net_FMSTATE != txt_mars_net_FMSTATE->value()) return true;
190 	if (s_mars_net_TOPOS != txt_mars_net_TOPOS->value()) return true;
191 	if (s_mars_net_TOCALL != txt_mars_net_TOCALL->value()) return true;
192 	if (s_mars_net_TOSTATE != txt_mars_net_TOSTATE->value()) return true;
193 	if (s_mars_net_INFOPOS != txt_mars_net_INFOPOS->value()) return true;
194 	if (s_mars_net_INFOCALL != txt_mars_net_INFOCALL->value()) return true;
195 	if (s_mars_net_INFOSTATE != txt_mars_net_INFOSTATE->value()) return true;
196 	if (s_mars_net_INCIDENT != txt_mars_net_INCIDENT->value()) return true;
197 	if (s_mars_net_DND != txt_mars_net_DND->value()) return true;
198 	if (s_mars_net_NETCALL != txt_mars_net_NETCALL->value()) return true;
199 	if (s_mars_net_DTGSTART != txt_mars_net_DTGSTART->value()) return true;
200 	if (s_mars_net_DTGEND != txt_mars_net_DTGEND->value()) return true;
201 	if (s_mars_net_NETSB != txt_mars_net_NETSB->value()) return true;
202 	if (s_mars_net_NCSCALL != txt_mars_net_NCSCALL->value()) return true;
203 	if (s_mars_net_NBRSTAS != txt_mars_net_NBRSTAS->value()) return true;
204 	if (s_mars_net_CALLS != txt_mars_net_CALLS->value()) return true;
205 	if (s_mars_net_NBRMSGS != txt_mars_net_NBRMSGS->value()) return true;
206 	if (s_mars_net_COMMENTS != txt_mars_net_COMMENTS->value()) return true;
207 	return false;
208 }
209 
update_mars_net_fields()210 void update_mars_net_fields()
211 {
212 	s_mars_net_DE = txt_mars_net_DE->value();
213 	s_mars_net_NBR = txt_mars_net_NBR->value();
214 	s_mars_net_PREC.clear();
215 	s_mars_net_PREC = mars_net_precedent[sel_mars_net_PREC->index()];
216 	s_mars_net_DTG = txt_mars_net_DTG->value();
217 	s_mars_net_FMNAME = txt_mars_net_FMNAME->value();
218 	s_mars_net_FMCALL = txt_mars_net_FMCALL->value();
219 	s_mars_net_FMSTATE = txt_mars_net_FMSTATE->value();
220 	s_mars_net_TOPOS = txt_mars_net_TOPOS->value();
221 	s_mars_net_TOCALL = txt_mars_net_TOCALL->value();
222 	s_mars_net_TOSTATE = txt_mars_net_TOSTATE->value();
223 	s_mars_net_INFOPOS = txt_mars_net_INFOPOS->value();
224 	s_mars_net_INFOCALL = txt_mars_net_INFOCALL->value();
225 	s_mars_net_INFOSTATE = txt_mars_net_INFOSTATE->value();
226 	s_mars_net_INCIDENT = txt_mars_net_INCIDENT->value();
227 	s_mars_net_DND = txt_mars_net_DND->value();
228 	s_mars_net_NETCALL = txt_mars_net_NETCALL->value();
229 	s_mars_net_DTGSTART = txt_mars_net_DTGSTART->value();
230 	s_mars_net_DTGEND = txt_mars_net_DTGEND->value();
231 	s_mars_net_NETSB = txt_mars_net_NETSB->value();
232 	s_mars_net_NCSCALL = txt_mars_net_NCSCALL->value();
233 	s_mars_net_NBRSTAS = txt_mars_net_NBRSTAS->value();
234 	s_mars_net_CALLS = txt_mars_net_CALLS->value();
235 	s_mars_net_NBRMSGS = txt_mars_net_NBRMSGS->value();
236 	s_mars_net_COMMENTS = txt_mars_net_COMMENTS->value();
237 }
238 
update_mars_netform()239 void update_mars_netform()
240 {
241 	txt_mars_net_DE->value(s_mars_net_DE.c_str());
242 	txt_mars_net_NBR->value(s_mars_net_NBR.c_str());
243 
244 	size_t n = strchr(mars_net_precedent, s_mars_net_PREC[0]) - mars_net_precedent;
245 	if (n < 0) n = 0;
246 	if (n > 4) n = 4;
247 	sel_mars_net_PREC->index(n);
248 
249 	txt_mars_net_DTG->value(s_mars_net_DTG.c_str());
250 	txt_mars_net_FMNAME->value(s_mars_net_FMNAME.c_str());
251 	txt_mars_net_FMCALL->value(s_mars_net_FMCALL.c_str());
252 	txt_mars_net_FMSTATE->value(s_mars_net_FMSTATE.c_str());
253 	txt_mars_net_TOPOS->value(s_mars_net_TOPOS.c_str());
254 	txt_mars_net_TOCALL->value(s_mars_net_TOCALL.c_str());
255 	txt_mars_net_TOSTATE->value(s_mars_net_TOSTATE.c_str());
256 	txt_mars_net_INFOPOS->value(s_mars_net_INFOPOS.c_str());
257 	txt_mars_net_INFOCALL->value(s_mars_net_INFOCALL.c_str());
258 	txt_mars_net_INFOSTATE->value(s_mars_net_INFOSTATE.c_str());
259 	txt_mars_net_INCIDENT->value(s_mars_net_INCIDENT.c_str());
260 	txt_mars_net_DND->value(s_mars_net_DND.c_str());
261 	txt_mars_net_NETCALL->value(s_mars_net_NETCALL.c_str());
262 	txt_mars_net_DTGSTART->value(s_mars_net_DTGSTART.c_str());
263 	txt_mars_net_DTGEND->value(s_mars_net_DTGEND.c_str());
264 	txt_mars_net_NETSB->value(s_mars_net_NETSB.c_str());
265 	txt_mars_net_NCSCALL->value(s_mars_net_NCSCALL.c_str());
266 	txt_mars_net_NBRSTAS->value(s_mars_net_NBRSTAS.c_str());
267 	txt_mars_net_CALLS->value(s_mars_net_CALLS.c_str());
268 	txt_mars_net_NBRMSGS->value(s_mars_net_NBRMSGS.c_str());
269 	txt_mars_net_COMMENTS->value(s_mars_net_COMMENTS.c_str());
270 }
271 
clear_mars_net_form()272 void clear_mars_net_form()
273 {
274 	clear_mars_net_fields();
275 	txt_mars_net_DE->value("");
276 	txt_mars_net_NBR->value("");
277 	sel_mars_net_PREC->index(4);
278 	txt_mars_net_DTG->value("");
279 	txt_mars_net_FMNAME->value("");
280 	txt_mars_net_FMCALL->value("");
281 	txt_mars_net_FMSTATE->value("");
282 	txt_mars_net_TOPOS->value("");
283 	txt_mars_net_TOCALL->value("");
284 	txt_mars_net_TOSTATE->value("");
285 	txt_mars_net_INFOPOS->value("");
286 	txt_mars_net_INFOCALL->value("");
287 	txt_mars_net_INFOSTATE->value("");
288 	txt_mars_net_INCIDENT->value("");
289 	txt_mars_net_DND->value("");
290 	txt_mars_net_NETCALL->value("");
291 	txt_mars_net_DTGSTART->value("");
292 	txt_mars_net_DTGEND->value("");
293 	txt_mars_net_NETSB->value("");
294 	txt_mars_net_NCSCALL->value("");
295 	txt_mars_net_NBRSTAS->value("");
296 	txt_mars_net_CALLS->value("");
297 	txt_mars_net_NBRMSGS->value("");
298 	txt_mars_net_COMMENTS->value("");
299 }
300 
make_mars_buffnet(bool compress=false)301 void make_mars_buffnet(bool compress = false)
302 {
303 	string mbuff;
304 	mbuff.clear();
305 	mbuff.append( lineout( mars_net_DE, s_mars_net_DE ) );
306 	mbuff.append( lineout( mars_net_NBR, s_mars_net_NBR ) );
307 	mbuff.append( lineout( mars_net_PREC, s_mars_net_PREC ) );
308 	mbuff.append( lineout( mars_net_DTG, s_mars_net_DTG ) );
309 	mbuff.append( lineout( mars_net_FMNAME, s_mars_net_FMNAME ) );
310 	mbuff.append( lineout( mars_net_FMCALL, s_mars_net_FMCALL ) );
311 	mbuff.append( lineout( mars_net_FMSTATE, s_mars_net_FMSTATE ) );
312 	mbuff.append( lineout( mars_net_TOPOS, s_mars_net_TOPOS ) );
313 	mbuff.append( lineout( mars_net_TOCALL, s_mars_net_TOCALL ) );
314 	mbuff.append( lineout( mars_net_TOSTATE, s_mars_net_TOSTATE ) );
315 	mbuff.append( lineout( mars_net_INFOPOS, s_mars_net_INFOPOS ) );
316 	mbuff.append( lineout( mars_net_INFOCALL, s_mars_net_INFOCALL ) );
317 	mbuff.append( lineout( mars_net_INFOSTATE, s_mars_net_INFOSTATE ) );
318 	mbuff.append( lineout( mars_net_INCIDENT, s_mars_net_INCIDENT ) );
319 	mbuff.append( lineout( mars_net_DND, s_mars_net_DND ) );
320 	mbuff.append( lineout( mars_net_NETCALL, s_mars_net_NETCALL ) );
321 	mbuff.append( lineout( mars_net_DTGSTART, s_mars_net_DTGSTART ) );
322 	mbuff.append( lineout( mars_net_DTGEND, s_mars_net_DTGEND ) );
323 	mbuff.append( lineout( mars_net_NETSB, s_mars_net_NETSB ) );
324 	mbuff.append( lineout( mars_net_NCSCALL, s_mars_net_NCSCALL ) );
325 	mbuff.append( lineout( mars_net_NBRSTAS, s_mars_net_NBRSTAS ) );
326 	mbuff.append( lineout( mars_net_CALLS, s_mars_net_CALLS ) );
327 	mbuff.append( lineout( mars_net_NBRMSGS, s_mars_net_NBRMSGS ) );
328 	mbuff.append( lineout( mars_net_COMMENTS, s_mars_net_COMMENTS ) );
329 	if (compress) compress_maybe(mbuff);
330 	buffnet.append(mbuff);
331 }
332 
read_mars_net_buffer(string data)333 void read_mars_net_buffer(string data)
334 {
335 	clear_mars_net_fields();
336 // search the file buffer for each of the mars_net fields
337 
338 	s_mars_net_DE = findstr(data, mars_net_DE);
339 	s_mars_net_NBR = findstr(data, mars_net_NBR);
340 	s_mars_net_PREC = findstr(data, mars_net_PREC);
341 	s_mars_net_DTG = findstr(data, mars_net_DTG);
342 	s_mars_net_FMNAME = findstr(data, mars_net_FMNAME);
343 	s_mars_net_FMCALL = findstr(data, mars_net_FMCALL);
344 	s_mars_net_FMSTATE = findstr(data, mars_net_FMSTATE);
345 	s_mars_net_TOPOS = findstr(data, mars_net_TOPOS);
346 	s_mars_net_TOCALL = findstr(data, mars_net_TOCALL);
347 	s_mars_net_TOSTATE = findstr(data, mars_net_TOSTATE);
348 	s_mars_net_INFOPOS = findstr(data, mars_net_INFOPOS);
349 	s_mars_net_INFOCALL = findstr(data, mars_net_INFOCALL);
350 	s_mars_net_INFOSTATE = findstr(data, mars_net_INFOSTATE);
351 	s_mars_net_INCIDENT = findstr(data, mars_net_INCIDENT);
352 	s_mars_net_DND = findstr(data, mars_net_DND);
353 	s_mars_net_NETCALL = findstr(data, mars_net_NETCALL);
354 	s_mars_net_DTGSTART = findstr(data, mars_net_DTGSTART);
355 	s_mars_net_DTGEND = findstr(data, mars_net_DTGEND);
356 	s_mars_net_NETSB = findstr(data, mars_net_NETSB);
357 	s_mars_net_NCSCALL = findstr(data, mars_net_NCSCALL);
358 	s_mars_net_NBRSTAS = findstr(data, mars_net_NBRSTAS);
359 	s_mars_net_CALLS = findstr(data, mars_net_CALLS);
360 	s_mars_net_NBRMSGS = findstr(data, mars_net_NBRMSGS);
361 	s_mars_net_COMMENTS = findstr(data, mars_net_COMMENTS);
362 
363 	update_mars_netform();
364 }
365 
cb_mars_net_new()366 void cb_mars_net_new()
367 {
368 	if (check_mars_net_fields()) {
369 		if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
370 			update_header(CHANGED);
371 			cb_mars_net_save();
372 		}
373 	}
374 	clear_mars_net_form();
375 	def_mars_net_filename = ICS_msg_dir;
376 	def_mars_net_filename.append("new").append(FMARSNET_EXT);
377 	show_filename(def_mars_net_filename);
378 	using_mars_net_template = false;
379 }
380 
cb_mars_net_import()381 void cb_mars_net_import()
382 {
383 	fl_alert2("Not implemented");
384 }
385 
cb_mars_net_export()386 void cb_mars_net_export()
387 {
388 	fl_alert2("Not implemented");
389 }
390 
cb_mars_net_wrap_import(string wrapfilename,string inpbuffer)391 void cb_mars_net_wrap_import(string wrapfilename, string inpbuffer)
392 {
393 	clear_mars_net_form();
394 	read_mars_net_buffer(inpbuffer);
395 	def_mars_net_filename = ICS_msg_dir;
396 	def_mars_net_filename.append(wrapfilename);
397 	show_filename(def_mars_net_filename);
398 	using_mars_net_template = false;
399 }
400 
eval_mars_net_fsize()401 int eval_mars_net_fsize()
402 {
403 	Ccrc16 chksum;
404 	evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
405 	evalstr.append(base_mars_net_filename).append("]");
406 	update_mars_net_fields();
407 	update_header(FROM);
408 	evalstr.append(header("<mars_net>"));
409 	buffnet.clear();
410 	make_mars_buffnet(true);
411 	if (buffnet.empty()) return 0;
412 	compress_maybe( buffnet );
413 	evalstr.append( buffnet );
414 	evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
415 	return evalstr.length();
416 }
417 
cb_mars_net_wrap_export()418 void cb_mars_net_wrap_export()
419 {
420 	if (check_mars_net_fields()) {
421 		if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
422 			return;
423 		update_header(CHANGED);
424 	}
425 	update_mars_net_fields();
426 
427 	if (base_mars_net_filename == string("new").append(FMARSNET_EXT) ||
428 		base_mars_net_filename == string("default").append(FMARSNET_EXT) )
429 		if (!cb_mars_net_save_as()) return;
430 
431 	string wrapfilename = WRAP_send_dir;
432 	wrapfilename.append(base_mars_net_filename);
433 	wrapfilename.append(".wrap");
434 	const char *p = FSEL::saveas(
435 			"Save as wrap file",
436 			"Wrap file\t*.{wrap,WRAP}",
437 			wrapfilename.c_str());
438 	if (p) {
439 		string pext = fl_filename_ext(p);
440 		wrapfilename = p;
441 
442 		update_header(FROM);
443 		buffnet.assign(header("<mars_net>"));
444 		make_mars_buffnet(true);
445 		export_wrapfile(base_mars_net_filename, wrapfilename, buffnet, pext != ".wrap");
446 
447 		buffnet.assign(header("<mars_net>"));
448 		make_mars_buffnet(false);
449 		write_mars_net(def_mars_net_filename);
450 	}
451 }
452 
cb_mars_net_wrap_autosend()453 void cb_mars_net_wrap_autosend()
454 {
455 	if (check_mars_net_fields()) {
456 		if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
457 			return;
458 		update_header(CHANGED);
459 	}
460 	update_mars_net_fields();
461 
462 	if (base_mars_net_filename == string("new").append(FMARSNET_EXT) ||
463 		base_mars_net_filename == string("default").append(FMARSNET_EXT) )
464 		if (!cb_mars_net_save_as()) return;
465 
466 	update_header(FROM);
467 	buffnet.assign(header("<mars_net>"));
468 	make_mars_buffnet(true);
469 	xfr_via_socket(base_mars_net_filename, buffnet);
470 
471 	buffnet.assign(header("<mars_net>"));
472 	make_mars_buffnet(false);
473 	write_mars_net(def_mars_net_filename);
474 }
475 
cb_mars_net_load_template()476 void cb_mars_net_load_template()
477 {
478 	string def_mars_net_filename = def_mars_net_TemplateName;
479 	const char *p = FSEL::select(
480 			"Open template file",
481 			std::string("Template file\t*").append(TMARSNET_EXT).c_str(),
482 			def_mars_net_filename.c_str());
483 	if (p) {
484 		clear_mars_net_form();
485 		read_data_file(p);
486 		def_mars_net_TemplateName = p;
487 		show_filename(def_mars_net_TemplateName);
488 		using_mars_net_template = true;
489 	}
490 }
491 
cb_mars_net_save_template()492 void cb_mars_net_save_template()
493 {
494 	if (!using_mars_net_template) {
495 		cb_mars_net_save_as_template();
496 		return;
497 	}
498 	string def_mars_net_filename = def_mars_net_TemplateName;
499 	const char *p = FSEL::saveas(
500 			"Save template file",
501 			std::string("Template file\t*").append(TMARSNET_EXT).c_str(),
502 			def_mars_net_filename.c_str());
503 	if (p) {
504 		update_header(CHANGED);
505 		update_mars_net_fields();
506 		buffnet.assign(header("<mars_net>"));
507 		make_mars_buffnet();
508 		write_mars_net(p);
509 	}
510 }
511 
cb_mars_net_save_as_template()512 void cb_mars_net_save_as_template()
513 {
514 	string def_mars_net_filename = def_mars_net_TemplateName;
515 	const char *p = FSEL::saveas(
516 			"Save as template file",
517 			std::string("Template file\t*").append(TMARSNET_EXT).c_str(),
518 			def_mars_net_filename.c_str());
519 	if (p) {
520 		const char *pext = fl_filename_ext(p);
521 		def_mars_net_TemplateName = p;
522 		if (strlen(pext) == 0) def_mars_net_TemplateName.append(TMARSNET_EXT);
523 		remove_spaces_from_filename(def_mars_net_TemplateName);
524 
525 		clear_header();
526 		update_header(CHANGED);
527 		update_mars_net_fields();
528 		buffnet.assign(header("<mars_net>"));
529 		make_mars_buffnet();
530 		write_mars_net(def_mars_net_TemplateName);
531 
532 		show_filename(def_mars_net_TemplateName);
533 		using_mars_net_template = true;
534 	}
535 }
536 
cb_mars_net_open()537 void cb_mars_net_open()
538 {
539 	const char *p = FSEL::select(
540 			_("Open data file"),
541 			string("ICS-net\t*").append(FMARSNET_EXT).c_str(),
542 			def_mars_net_filename.c_str());
543 	if (!p) return;
544 	if (strlen(p) == 0) return;
545 	clear_mars_net_form();
546 	read_data_file(p);
547 	using_mars_net_template = false;
548 	def_mars_net_filename = p;
549 	show_filename(def_mars_net_filename);
550 }
551 
write_mars_net(string s)552 void write_mars_net(string s)
553 {
554 	FILE *filenet = fopen(s.c_str(), "w");
555 	if (!filenet) return;
556 	make_mars_buffnet();
557 	fwrite(buffnet.c_str(), buffnet.length(), 1, filenet);
558 	fclose(filenet);
559 }
560 
cb_mars_net_save_as()561 bool cb_mars_net_save_as()
562 {
563 	const char *p;
564 	string newfilename;
565 
566 	string name = named_file();
567 	if (!name.empty()) {
568 		name.append(FMARSNET_EXT);
569 		newfilename = ICS_msg_dir;
570 		newfilename.append(name);
571 	} else
572 		newfilename = def_mars_net_filename;
573 
574 	p = FSEL::saveas(
575 			_("Save data file"),
576 			string("ICS-net\t*").append(FMARSNET_EXT).c_str(),
577 			newfilename.c_str());
578 
579 	if (!p) return false;
580 	if (strlen(p) == 0) return false;
581 
582 	if (progStatus.sernbr_fname) update_sernbr();
583 
584 	const char *pext = fl_filename_ext(p);
585 	def_mars_net_filename = p;
586 	if (strlen(pext) == 0) def_mars_net_filename.append(FMARSNET_EXT);
587 
588 	remove_spaces_from_filename(def_mars_net_filename);
589 
590 	update_header(NEW);
591 	update_mars_net_fields();
592 	buffnet.assign(header("<mars_net>"));
593 	make_mars_buffnet();
594 	write_mars_net(def_mars_net_filename);
595 
596 	using_mars_net_template = false;
597 	show_filename(def_mars_net_filename);
598 	return true;
599 }
600 
cb_mars_net_save()601 void cb_mars_net_save()
602 {
603 	if (base_mars_net_filename == string("new").append(FMARSNET_EXT) ||
604 		base_mars_net_filename == string("default").append(FMARSNET_EXT) ||
605 		using_mars_net_template == true) {
606 		cb_mars_net_save_as();
607 		return;
608 	}
609 	if (check_mars_net_fields()) update_header(CHANGED);
610 	update_mars_net_fields();
611 	buffnet.assign(header("<mars_net>"));
612 	make_mars_buffnet();
613 	write_mars_net(def_mars_net_filename);
614 
615 	using_mars_net_template = false;
616 }
617 
cb_mars_net_html()618 void cb_mars_net_html()
619 {
620 	string fname_name = fl_filename_name(def_mars_net_filename.c_str());
621 	size_t p = fname_name.rfind('.');
622 	if (p != string::npos) fname_name.erase(p);
623 
624 	string mars_net_fname = ICS_dir;
625 	mars_net_fname.append(fname_name);
626 	mars_net_fname.append(".html");
627 
628 	string formnet = mars_net_html_template;
629 
630 	replacestr(formnet, mars_net_DE, s_mars_net_DE);
631 	replacestr(formnet, mars_net_NBR, s_mars_net_NBR);
632 	replacestr(formnet, mars_net_PREC, s_mars_net_PREC);
633 	replacestr(formnet, mars_net_DTG, s_mars_net_DTG);
634 	replacestr(formnet, mars_net_FMNAME, s_mars_net_FMNAME);
635 	replacestr(formnet, mars_net_FMCALL, s_mars_net_FMCALL);
636 	replacestr(formnet, mars_net_FMSTATE, s_mars_net_FMSTATE);
637 	replacestr(formnet, mars_net_TOPOS, s_mars_net_TOPOS);
638 	replacestr(formnet, mars_net_TOCALL, s_mars_net_TOCALL);
639 	replacestr(formnet, mars_net_TOSTATE, s_mars_net_TOSTATE);
640 	replacestr(formnet, mars_net_INFOPOS, s_mars_net_INFOPOS);
641 	replacestr(formnet, mars_net_INFOCALL, s_mars_net_INFOCALL);
642 	replacestr(formnet, mars_net_INFOSTATE, s_mars_net_INFOSTATE);
643 	replacestr(formnet, mars_net_INCIDENT, s_mars_net_INCIDENT);
644 	replacestr(formnet, mars_net_INCIDENT, s_mars_net_INCIDENT); // 2 in form
645 	replacestr(formnet, mars_net_DND, s_mars_net_DND);
646 	replacestr(formnet, mars_net_NETCALL, s_mars_net_NETCALL);
647 	replacestr(formnet, mars_net_DTGSTART, s_mars_net_DTGSTART);
648 	replacestr(formnet, mars_net_DTGEND, s_mars_net_DTGEND);
649 	replacestr(formnet, mars_net_NETSB, s_mars_net_NETSB);
650 	replacestr(formnet, mars_net_NCSCALL, s_mars_net_NCSCALL);
651 	replacestr(formnet, mars_net_NBRSTAS, s_mars_net_NBRSTAS);
652 	replacestr(formnet, mars_net_CALLS, s_mars_net_CALLS);
653 	replacestr(formnet, mars_net_NBRMSGS, s_mars_net_NBRMSGS);
654 	replacestr(formnet, mars_net_COMMENTS, s_mars_net_COMMENTS);
655 
656 	FILE *filenet = fopen(mars_net_fname.c_str(), "w");
657 	fprintf(filenet,"%s", formnet.c_str());
658 	fclose(filenet);
659 
660 	open_url(mars_net_fname.c_str());
661 }
662 
cb_mars_net_textout()663 void cb_mars_net_textout()
664 {
665 	string temp = "";
666 	string mars_net_fname = ICS_dir;
667 	mars_net_fname.append("mars_net.txt");
668 
669 	string formnet = mars_net_text_template;
670 
671 	replacestr(formnet, mars_net_DE, s_mars_net_DE);
672 	replacestr(formnet, mars_net_NBR, s_mars_net_NBR);
673 	replacestr(formnet, mars_net_PREC, s_mars_net_PREC);
674 	replacestr(formnet, mars_net_DTG, s_mars_net_DTG);
675 	replacestr(formnet, mars_net_FMNAME, s_mars_net_FMNAME);
676 	replacestr(formnet, mars_net_FMCALL, s_mars_net_FMCALL);
677 	replacestr(formnet, mars_net_FMSTATE, s_mars_net_FMSTATE);
678 	replacestr(formnet, mars_net_TOPOS, s_mars_net_TOPOS);
679 	replacestr(formnet, mars_net_TOCALL, s_mars_net_TOCALL);
680 	replacestr(formnet, mars_net_TOSTATE, s_mars_net_TOSTATE);
681 	replacestr(formnet, mars_net_INFOPOS, s_mars_net_INFOPOS);
682 	replacestr(formnet, mars_net_INFOCALL, s_mars_net_INFOCALL);
683 	replacestr(formnet, mars_net_INFOSTATE, s_mars_net_INFOSTATE);
684 	replacestr(formnet, mars_net_INCIDENT, s_mars_net_INCIDENT);
685 	replacestr(formnet, mars_net_INCIDENT, s_mars_net_INCIDENT); // 2 in form
686 	replacestr(formnet, mars_net_DND, s_mars_net_DND);
687 	replacestr(formnet, mars_net_NETCALL, s_mars_net_NETCALL);
688 	replacestr(formnet, mars_net_DTGSTART, s_mars_net_DTGSTART);
689 	replacestr(formnet, mars_net_DTGEND, s_mars_net_DTGEND);
690 	replacestr(formnet, mars_net_NETSB, s_mars_net_NETSB);
691 	replacestr(formnet, mars_net_NCSCALL, s_mars_net_NCSCALL);
692 	replacestr(formnet, mars_net_NBRSTAS, s_mars_net_NBRSTAS);
693 	replacestr(formnet, mars_net_CALLS, s_mars_net_CALLS);
694 	replacestr(formnet, mars_net_NBRMSGS, s_mars_net_NBRMSGS);
695 	replacestr(formnet, mars_net_COMMENTS, s_mars_net_COMMENTS);
696 
697 	FILE *filenet = fopen(mars_net_fname.c_str(), "w");
698 	fprintf(filenet,"%s", formnet.c_str());
699 	fclose(filenet);
700 
701 	open_url(mars_net_fname.c_str());
702 }
703 
704