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_strings.h"
46 
47 #include "flmsg.h"
48 #include "templates.h"
49 #include "debug.h"
50 #include "util.h"
51 #include "gettext.h"
52 #include "flmsg_dialog.h"
53 #include "flinput2.h"
54 #include "date.h"
55 #include "calendar.h"
56 #include "icons.h"
57 #include "fileselect.h"
58 #include "wrap.h"
59 #include "status.h"
60 #include "parse_xml.h"
61 #include "icons.h"
62 
63 #ifdef WIN32
64 #  include "flmsgrc.h"
65 #  include "compat.h"
66 #  define dirent fl_dirent_no_thanks
67 #endif
68 
69 #include <FL/filename.H>
70 #include "dirent-check.h"
71 
72 #include <FL/x.H>
73 #include <FL/Fl_Pixmap.H>
74 #include <FL/Fl_Image.H>
75 
76 using namespace std;
77 
78 // blankform fields
79 
80 string blankbuffer;
81 string def_blank_filename = "";
82 string base_blank_filename = "";
83 string def_blank_TemplateName = "";
84 
85 string blank_msg = ":mg:";
86 string ablank_msg = "<mg:";
87 string blank_field;
88 
89 bool using_blank_template = false;
90 
clear_blankfields()91 void clear_blankfields()
92 {
93 	blank_field.clear();
94 }
95 
check_blankfields()96 bool check_blankfields()
97 {
98 	return (blank_field != txt_blank_msg->buffer()->text());
99 }
100 
update_blankfields()101 void update_blankfields()
102 {
103 	blank_field = txt_blank_msg->buffer()->text();
104 }
105 
clear_blank_form()106 void clear_blank_form()
107 {
108 	clear_blankfields();
109 	txt_blank_msg->clear();
110 }
111 
update_blankform()112 void update_blankform()
113 {
114 	txt_blank_msg->clear();
115 	txt_blank_msg->add(blank_field.c_str());
116 }
117 
read_blankbuffer(string data)118 void read_blankbuffer(string data)
119 {
120 	clear_blankfields();
121 	read_header(data);
122 	blank_field = findstr(data, blank_msg);
123 	if (blank_field.empty())
124 		blank_field = findstr(data, ablank_msg); // test for old style data file
125 	update_blankform();
126 }
127 
cb_blank_new()128 void cb_blank_new()
129 {
130 	if (check_blankfields()) {
131 		if (fl_choice2(xMODIFIED, xNO, xYES, NULL) == 1) {
132 			update_header(CHANGED);
133 			cb_blank_save();
134 		}
135 	}
136 	clear_blank_form();
137 	clear_header();
138 	def_blank_filename = ICS_msg_dir;
139 	def_blank_filename.append(xNEW).append(BLANKFILE_EXT);
140 	show_filename(def_blank_filename);
141 	using_blank_template = false;
142 }
143 
cb_blank_import()144 void cb_blank_import()
145 {
146 	fl_alert2("Not implemented");
147 }
148 
cb_blank_export()149 void cb_blank_export()
150 {
151 	fl_alert2("Not implemented");
152 }
153 
cb_blank_wrap_import(string wrapfilename,string inpbuffer)154 void cb_blank_wrap_import(string wrapfilename, string inpbuffer)
155 {
156 	clear_blank_form();
157 	read_blankbuffer(inpbuffer);
158 	def_blank_filename = ICS_msg_dir;
159 	def_blank_filename.append(wrapfilename);
160 	show_filename(def_blank_filename);
161 	using_blank_template = false;
162 }
163 
eval_blank_fsize()164 int eval_blank_fsize()
165 {
166 	Ccrc16 chksum;
167 	evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
168 	evalstr.append(base_blank_filename).append("]");
169 	update_blankfields();
170 	update_header(FROM);
171 	evalstr.append(header("<blankform>"));
172 	string outbuf = lineout( blank_msg, blank_field );
173 	if (outbuf.empty()) return 0;
174 	compress_maybe( outbuf );
175 	evalstr.append( outbuf );
176 	evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
177 	return evalstr.length();
178 }
179 
cb_blank_wrap_export()180 void cb_blank_wrap_export()
181 {
182 	if (check_blankfields()) {
183 		if (fl_choice2(xMODIFIED, xNO, xYES, NULL) == 0)
184 			return;
185 		update_header(CHANGED);
186 	}
187 	update_blankfields();
188 	if (blank_field.empty()) return;
189 
190 	if (base_blank_filename == string(xNEW).append(BLANKFILE_EXT) ||
191 		base_blank_filename == string(xDEFAULT).append(BLANKFILE_EXT) )
192 		if (!cb_blank_save_as()) return;
193 
194 	string wrapfilename = WRAP_send_dir;
195 	wrapfilename.append(base_blank_filename);
196 	wrapfilename.append(WRAP_EXT);
197 	const char *p = FSEL::saveas(
198 			xSAVEASWRAP,
199 			xWRAPFILE,
200 			wrapfilename.c_str());
201 	if (p) {
202 		string pext = fl_filename_ext(p);
203 		wrapfilename = p;
204 		update_header(FROM);
205 		blankbuffer.assign(header("<blankform>"));
206 		string outbuf = lineout( blank_msg, blank_field );
207 		compress_maybe( outbuf );
208 		blankbuffer.append( outbuf );
209 		export_wrapfile(base_blank_filename, wrapfilename, blankbuffer, pext != WRAP_EXT);
210 
211 		blankbuffer.assign(header("<blankform>")).append(lineout( blank_msg, blank_field ));
212 		write_blank(def_blank_filename);
213 	}
214 }
215 
cb_blank_wrap_autosend()216 void cb_blank_wrap_autosend()
217 {
218 	if (check_blankfields()) {
219 		if (fl_choice2(xMODIFIED, xNO, xYES, NULL) == 0)
220 			return;
221 		update_header(CHANGED);
222 	}
223 	update_blankfields();
224 	if (blank_field.empty()) return;
225 
226 	if (base_blank_filename == string(xNEW).append(BLANKFILE_EXT) ||
227 		base_blank_filename == string(xDEFAULT).append(BLANKFILE_EXT) )
228 		if (!cb_blank_save_as()) return;
229 
230 	update_header(FROM);
231 	blankbuffer.assign(header("<blankform>"));
232 	string outbuf = lineout( blank_msg, blank_field );
233 	compress_maybe( outbuf );
234 	blankbuffer.append( outbuf );
235 	xfr_via_socket(base_blank_filename, blankbuffer);
236 
237 	blankbuffer.assign(header("<blankform>")).append(lineout( blank_msg, blank_field ));
238 	write_blank(def_blank_filename);
239 }
240 
cb_blank_load_template()241 void cb_blank_load_template()
242 {
243 	string def_blank_filename = def_blank_TemplateName;
244 	const char *p = FSEL::select(
245 			xOPENTEMPLATE,
246 			string(xTEMPLATEFILE).append(BLANKTEMP_EXT).c_str(),
247 			def_blank_filename.c_str());
248 	if (p) {
249 		clear_blank_form();
250 		read_data_file(p);
251 		def_blank_TemplateName = p;
252 		show_filename(def_blank_TemplateName);
253 		using_blank_template = true;
254 	}
255 }
256 
cb_blank_save_template()257 void cb_blank_save_template()
258 {
259 	if (!using_blank_template) {
260 		cb_blank_save_as_template();
261 		return;
262 	}
263 	string def_blank_filename = def_blank_TemplateName;
264 	const char *p = FSEL::saveas(
265 			xSAVEASTEMPLATE,
266 			string(xTEMPLATEFILE).append(BLANKTEMP_EXT).c_str(),
267 			def_blank_filename.c_str());
268 	if (p) {
269 		update_header(CHANGED);
270 		write_blank(p);
271 	}
272 }
273 
cb_blank_save_as_template()274 void cb_blank_save_as_template()
275 {
276 	string def_blank_filename = def_blank_TemplateName;
277 	const char *p = FSEL::saveas(
278 			xSAVEASTEMPLATE,
279 			string(xTEMPLATEFILE).append(BLANKTEMP_EXT).c_str(),
280 			def_blank_filename.c_str());
281 	if (p) {
282 		const char *pext = fl_filename_ext(p);
283 		def_blank_TemplateName = p;
284 		if (strlen(pext) == 0) def_blank_TemplateName.append(BLANKTEMP_EXT);
285 		remove_spaces_from_filename(def_blank_TemplateName);
286 		clear_header();
287 		update_header(CHANGED);
288 		write_blank(def_blank_TemplateName);
289 		show_filename(def_blank_TemplateName);
290 		using_blank_template = true;
291 	}
292 }
293 
cb_blank_open()294 void cb_blank_open()
295 {
296 	const char *p = FSEL::select(xOPENDATAFILE, "blank form\t*.b2s",
297 					def_blank_filename.c_str());
298 	if (!p) return;
299 	if (strlen(p) == 0) return;
300 	clear_blank_form();
301 	read_data_file(p);
302 	using_blank_template = false;
303 	def_blank_filename = p;
304 	show_filename(def_blank_filename);
305 }
306 
write_blank(string s)307 void write_blank(string s)
308 {
309 	FILE *blankfile = fopen(s.c_str(), "w");
310 	if (!blankfile) return;
311 	fwrite(blankbuffer.c_str(), blankbuffer.length(), 1, blankfile);
312 	fclose(blankfile);
313 }
314 
cb_blank_save_as()315 bool cb_blank_save_as()
316 {
317 	const char *p;
318 	string newfilename;
319 
320 	string name = named_file();
321 	if (!name.empty()) {
322 		name.append(".b2s");
323 		newfilename = ICS_msg_dir;
324 		newfilename.append(name);
325 	} else
326 		newfilename = def_blank_filename;
327 
328 	p = FSEL::saveas(xSAVEDATAFILE, "blank form\t*.b2s",
329 					newfilename.c_str());
330 
331 	if (!p) return false;
332 	if (strlen(p) == 0) return false;
333 
334 	if (progStatus.sernbr_fname) update_sernbr();
335 
336 	const char *pext = fl_filename_ext(p);
337 	def_blank_filename = p;
338 	if (strlen(pext) == 0) def_blank_filename.append(".b2s");
339 
340 	remove_spaces_from_filename(def_blank_filename);
341 	update_header(NEW);
342 	update_blankfields();
343 	blankbuffer.assign(header("<blankform>"));
344 	blankbuffer.append( lineout( blank_msg, blank_field ) );
345 	write_blank(def_blank_filename);
346 
347 	using_blank_template = false;
348 	show_filename(def_blank_filename);
349 	return true;
350 }
351 
cb_blank_save()352 void cb_blank_save()
353 {
354 	if (base_blank_filename == string(xNEW).append(BLANKFILE_EXT) ||
355 		base_blank_filename == string(xDEFAULT).append(BLANKFILE_EXT) ||
356 		using_blank_template == true) {
357 		cb_blank_save_as();
358 		return;
359 	}
360 	if (check_blankfields()) update_header(CHANGED);
361 	update_blankfields();
362 	blankbuffer.assign(header("<blankform>"));
363 	blankbuffer.append( lineout( blank_msg, blank_field ) );
364 	write_blank(def_blank_filename);
365 	using_blank_template = false;
366 }
367 
cb_blank_msg_type()368 void cb_blank_msg_type()
369 {
370 	if (tabs_msg_type->value() == tab_blank ) {
371 		show_filename(def_blank_filename);
372 	} else {
373 		show_filename(def_rg_filename);
374 	}
375 }
376 
cb_blank_html()377 void cb_blank_html()
378 {
379 	update_blankfields();
380 //	if (blank_field.empty()) return;
381 
382 	string fname_name = fl_filename_name(def_blank_filename.c_str());
383 	size_t p = fname_name.rfind('.');
384 	if (p != string::npos) fname_name.erase(p);
385 	string blank_name = ICS_dir;
386 	string html_text = "";
387 	blank_name.append(fname_name);
388 	blank_name.append(".html");
389 
390 	string blankform = blank_html_template;
391 
392 	html_text = "<pre><big style= font-family: \"Consolas\", \"Monospace\", \"Courier\";\">";
393 	if (progStatus.autowordwrap)
394 		html_text.append(wordwrap(blank_field, progStatus.charcount));
395 	else
396 		html_text.append(blank_field);
397 	html_text.append("</big></pre>");
398 	replacestr(blankform, blank_msg, html_text);
399 
400 	FILE *blankfile = fopen(blank_name.c_str(), "w");
401 	fprintf(blankfile,"%s", blankform.c_str());
402 	fclose(blankfile);
403 
404 	open_url(blank_name.c_str());
405 }
406 
cb_blank_textout()407 void cb_blank_textout()
408 {
409 	string blank_name = ICS_dir;
410 	blank_name.append("blankform.txt");
411 
412 	update_blankfields();
413 	string blankform = blank_txt_template;
414 
415 	replacestr(blankform, blank_msg, blank_field);
416 
417 	FILE *blankfile = fopen(blank_name.c_str(), "w");
418 	fprintf(blankfile,"%s", blankform.c_str());
419 	fclose(blankfile);
420 
421 	open_url(blank_name.c_str());
422 }
423