| 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 |
// plaintext fields |
| 76 |
|
| 77 |
// compatibility fields required to read older data files |
| 78 |
|
| 79 |
string apt_title = "<tt:"; |
| 80 |
string apt_to = "<to:"; |
| 81 |
string apt_fm = "<fm:"; |
| 82 |
string apt_subj = "<sb:"; |
| 83 |
string apt_dt = "<dt:"; |
| 84 |
string apt_tm = "<tm:"; |
| 85 |
string apt_msg = "<mg:"; |
| 86 |
|
| 87 |
FIELD aptfields[] = { |
| 88 |
{ apt_title, "", (void **)&txt_pt_title, 't' }, |
| 89 |
{ apt_to, "", (void **)&txt_pt_to, 't' }, |
| 90 |
{ apt_fm, "", (void **)&txt_pt_fm, 't' }, |
| 91 |
{ apt_dt, "", (void **)&txt_pt_date, 'd' }, |
| 92 |
{ apt_tm, "", (void **)&txt_pt_time, 't' }, |
| 93 |
{ apt_subj, "", (void **)&txt_pt_subj, 't' }, |
| 94 |
{ apt_msg, "", (void **)&txt_pt_msg, 'e' } }; |
| 95 |
|
| 96 |
// new string tags & fields |
| 97 |
string pt_title = ":tt:"; |
| 98 |
string pt_to = ":to:"; |
| 99 |
string pt_fm = ":fm:"; |
| 100 |
string pt_subj = ":sb:"; |
| 101 |
string pt_dt = ":dt:"; |
| 102 |
string pt_tm = ":tm:"; |
| 103 |
string pt_msg = ":mg:"; |
| 104 |
|
| 105 |
FIELD ptfields[] = { |
| 106 |
{ pt_title, "", (void **)&txt_pt_title, 't' }, |
| 107 |
{ pt_to, "", (void **)&txt_pt_to, 't' }, |
| 108 |
{ pt_fm, "", (void **)&txt_pt_fm, 't' }, |
| 109 |
{ pt_dt, "", (void **)&txt_pt_date, 'd' }, |
| 110 |
{ pt_tm, "", (void **)&txt_pt_time, 't' }, |
| 111 |
{ pt_subj, "", (void **)&txt_pt_subj, 't' }, |
| 112 |
{ pt_msg, "", (void **)&txt_pt_msg, 'e' } }; |
| 113 |
|
| 114 |
string ptbuffer; |
| 115 |
string def_pt_filename = ""; |
| 116 |
string base_pt_filename = ""; |
| 117 |
string def_pt_TemplateName = ""; |
| 118 |
|
| 119 |
bool using_pt_template = false; |
| 120 |
|
| 121 |
int num_ptfields = sizeof(ptfields) / sizeof(FIELD); |
| 122 |
|
| 123 |
void cb_set_pt_date() |
| 124 |
{ |
| 125 |
txt_pt_date->value(szDate(progStatus.dtformat)); |
| 126 |
} |
| 127 |
|
| 128 |
void cb_set_pt_time() |
| 129 |
{ |
| 130 |
txt_pt_time->value(szTime(progStatus.UTC)); |
| 131 |
} |
| 132 |
|
| 133 |
void clear_ptfields() |
| 134 |
{ |
| 135 |
for (int i = 0; i < num_ptfields; i++) |
| 136 |
ptfields[i].f_data.clear(); |
| 137 |
} |
| 138 |
|
| 139 |
bool check_ptfields() |
| 140 |
{ |
| 141 |
for (int i = 0; i < num_ptfields; i++) { |
| 142 |
if (ptfields[i].w_type == 'd') { |
| 143 |
if (ptfields[i].f_data != ((Fl_DateInput *)(*ptfields[i].w))->value()) return true; |
| 144 |
} else if (ptfields[i].w_type == 't') { |
| 145 |
if (ptfields[i].f_data != ((Fl_Input2 *)(*ptfields[i].w))->value()) return true; |
| 146 |
} else if (ptfields[i].w_type == 'e') { |
| 147 |
if (ptfields[i].f_data != ((FTextEdit *)(*ptfields[i].w))->buffer()->text()) return true; |
| 148 |
} |
| 149 |
} |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
void update_ptfields() |
| 154 |
{ |
| 155 |
for (int i = 0; i < num_ptfields; i++) { |
| 156 |
if (ptfields[i].w_type == 'd') |
| 157 |
ptfields[i].f_data = ((Fl_DateInput *)(*ptfields[i].w))->value(); |
| 158 |
else if (ptfields[i].w_type == 't') |
| 159 |
ptfields[i].f_data = ((Fl_Input2 *)(*ptfields[i].w))->value(); |
| 160 |
else if (ptfields[i].w_type == 'e') { |
| 161 |
ptfields[i].f_data = ((FTextEdit *)(*ptfields[i].w))->buffer()->text(); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void clear_pt_form() |
| 167 |
{ |
| 168 |
clear_ptfields(); |
| 169 |
ptfields[0].f_data = "General Message"; |
| 170 |
txt_pt_title->value(ptfields[0].f_data.c_str()); |
| 171 |
txt_pt_to->value(""); |
| 172 |
txt_pt_fm->value(""); |
| 173 |
txt_pt_subj->value(""); |
| 174 |
txt_pt_date->value(""); |
| 175 |
txt_pt_time->value(""); |
| 176 |
txt_pt_msg->clear(); |
| 177 |
update_ptfields(); |
| 178 |
} |
| 179 |
|
| 180 |
void update_pt_form() |
| 181 |
{ |
| 182 |
for (int i = 0; i < num_ptfields; i++) { |
| 183 |
if (ptfields[i].w_type == 'd') |
| 184 |
((Fl_DateInput *)(*ptfields[i].w))->value(ptfields[i].f_data.c_str()); |
| 185 |
else if (ptfields[i].w_type == 't') |
| 186 |
((Fl_Input2 *)(*ptfields[i].w))->value(ptfields[i].f_data.c_str()); |
| 187 |
else { |
| 188 |
((FTextEdit *)(*ptfields[i].w))->clear(); |
| 189 |
((FTextEdit *)(*ptfields[i].w))->add(ptfields[i].f_data.c_str()); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
void make_ptbuffer(bool compress = false) |
| 195 |
{ |
| 196 |
string mbuff; |
| 197 |
mbuff.clear(); |
| 198 |
for (int i = 0; i < num_ptfields; i++) |
| 199 |
mbuff.append( lineout( ptfields[i].f_type, ptfields[i].f_data ) ); |
| 200 |
if (compress) compress_maybe(mbuff); |
| 201 |
ptbuffer.append(mbuff); |
| 202 |
} |
| 203 |
|
| 204 |
void read_ptbuffer(string data) |
| 205 |
{ |
| 206 |
bool data_ok = false; |
| 207 |
clear_ptfields(); |
| 208 |
read_header(data); |
| 209 |
|
| 210 |
for (int i = 0; i < num_ptfields; i++) { |
| 211 |
ptfields[i].f_data = findstr(data, ptfields[i].f_type); |
| 212 |
if (!ptfields[i].f_data.empty()) data_ok = true; |
| 213 |
} |
| 214 |
if (!data_ok) |
| 215 |
for (int i = 0; i < num_ptfields; i++) |
| 216 |
ptfields[i].f_data = findstr(data, aptfields[i].f_type); |
| 217 |
update_pt_form(); |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
void cb_pt_new() |
| 222 |
{ |
| 223 |
if (check_ptfields()) { |
| 224 |
if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) { |
| 225 |
update_header(CHANGED); |
| 226 |
cb_pt_save(); |
| 227 |
} |
| 228 |
} |
| 229 |
clear_pt_form(); |
| 230 |
clear_header(); |
| 231 |
def_pt_filename = ICS_msg_dir; |
| 232 |
def_pt_filename.append("new").append(PTFILE_EXT); |
| 233 |
show_filename(def_pt_filename); |
| 234 |
using_pt_template = false; |
| 235 |
} |
| 236 |
|
| 237 |
void cb_pt_import() |
| 238 |
{ |
| 239 |
fl_alert2("Not implemented"); |
| 240 |
} |
| 241 |
|
| 242 |
void cb_pt_export() |
| 243 |
{ |
| 244 |
fl_alert2("Not implemented"); |
| 245 |
} |
| 246 |
|
| 247 |
void cb_pt_wrap_import(string wrapfilename, string inpbuffer) |
| 248 |
{ |
| 249 |
clear_pt_form(); |
| 250 |
read_ptbuffer(inpbuffer); |
| 251 |
def_pt_filename = ICS_msg_dir; |
| 252 |
def_pt_filename.append(wrapfilename); |
| 253 |
show_filename(def_pt_filename); |
| 254 |
using_pt_template = false; |
| 255 |
} |
| 256 |
|
| 257 |
int eval_pt_fsize() |
| 258 |
{ |
| 259 |
Ccrc16 chksum; |
| 260 |
evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn "); |
| 261 |
evalstr.append(base_pt_filename).append("]"); |
| 262 |
update_ptfields(); |
| 263 |
update_header(FROM); |
| 264 |
evalstr.append(header("<plaintext>")); |
| 265 |
ptbuffer.clear(); |
| 266 |
make_ptbuffer(true); |
| 267 |
if (ptbuffer.empty()) return 0; |
| 268 |
compress_maybe( ptbuffer ); |
| 269 |
evalstr.append( ptbuffer ); |
| 270 |
evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]"); |
| 271 |
return evalstr.length(); |
| 272 |
} |
| 273 |
|
| 274 |
void cb_pt_wrap_export() |
| 275 |
{ |
| 276 |
if (check_ptfields()) { |
| 277 |
if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0) |
| 278 |
return; |
| 279 |
update_header(CHANGED); |
| 280 |
} |
| 281 |
update_ptfields(); |
| 282 |
|
| 283 |
if (base_pt_filename == string("new").append(PTFILE_EXT) || |
| 284 |
base_pt_filename == string("default").append(PTFILE_EXT) ) |
| 285 |
if (!cb_pt_save_as()) return; |
| 286 |
|
| 287 |
string wrapfilename = WRAP_send_dir; |
| 288 |
wrapfilename.append(base_pt_filename); |
| 289 |
wrapfilename.append(WRAP_EXT); |
| 290 |
const char *p = FSEL::saveas( |
| 291 |
"Save as wrap file", |
| 292 |
"Wrap file\t*.{wrap,WRAP}", |
| 293 |
wrapfilename.c_str()); |
| 294 |
if (p) { |
| 295 |
string pext = fl_filename_ext(p); |
| 296 |
wrapfilename = p; |
| 297 |
update_header(FROM); |
| 298 |
ptbuffer.assign(header("<plaintext>")); |
| 299 |
make_ptbuffer(true); |
| 300 |
export_wrapfile(base_pt_filename, wrapfilename, ptbuffer, pext != WRAP_EXT); |
| 301 |
|
| 302 |
ptbuffer.assign(header("<plaintext>")); |
| 303 |
make_ptbuffer(false); |
| 304 |
write_pt(def_pt_filename); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
void cb_pt_wrap_autosend() |
| 309 |
{ |
| 310 |
if (check_ptfields()) { |
| 311 |
if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0) |
| 312 |
return; |
| 313 |
update_header(CHANGED); |
| 314 |
} |
| 315 |
update_ptfields(); |
| 316 |
|
| 317 |
if (base_pt_filename == string("new").append(PTFILE_EXT) || |
| 318 |
base_pt_filename == string("default").append(PTFILE_EXT) ) |
| 319 |
if (!cb_pt_save_as()) return; |
| 320 |
|
| 321 |
update_header(FROM); |
| 322 |
ptbuffer.assign(header("<plaintext>")); |
| 323 |
make_ptbuffer(true); |
| 324 |
xfr_via_socket(base_pt_filename, ptbuffer); |
| 325 |
|
| 326 |
ptbuffer.assign(header("<plaintext>")); |
| 327 |
make_ptbuffer(false); |
| 328 |
write_pt(def_pt_filename); |
| 329 |
} |
| 330 |
|
| 331 |
void cb_pt_load_template() |
| 332 |
{ |
| 333 |
string def_pt_filename = def_pt_TemplateName; |
| 334 |
const char *p = FSEL::select( |
| 335 |
"Open template file", |
| 336 |
string("Template file\t*").append(PTTEMP_EXT).c_str(), |
| 337 |
def_pt_filename.c_str()); |
| 338 |
if (p) { |
| 339 |
clear_pt_form(); |
| 340 |
read_data_file(p); |
| 341 |
def_pt_TemplateName = p; |
| 342 |
show_filename(def_pt_TemplateName); |
| 343 |
using_pt_template = true; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
void cb_pt_save_template() |
| 348 |
{ |
| 349 |
if (!using_pt_template) { |
| 350 |
cb_pt_save_as_template(); |
| 351 |
return; |
| 352 |
} |
| 353 |
string def_pt_filename = def_pt_TemplateName; |
| 354 |
const char *p = FSEL::saveas( |
| 355 |
"Save template file", |
| 356 |
string("Template file\t*").append(PTTEMP_EXT).c_str(), |
| 357 |
def_pt_filename.c_str()); |
| 358 |
if (p) { |
| 359 |
update_header(CHANGED); |
| 360 |
update_ptfields(); |
| 361 |
ptbuffer.assign(header("<plaintext>")); |
| 362 |
make_ptbuffer(); |
| 363 |
write_pt(p); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
void cb_pt_save_as_template() |
| 368 |
{ |
| 369 |
string def_pt_filename = def_pt_TemplateName; |
| 370 |
const char *p = FSEL::saveas( |
| 371 |
"Save as template file", |
| 372 |
string("Template file\t*").append(PTTEMP_EXT).c_str(), |
| 373 |
def_pt_filename.c_str()); |
| 374 |
if (p) { |
| 375 |
const char *pext = fl_filename_ext(p); |
| 376 |
def_pt_TemplateName = p; |
| 377 |
if (strlen(pext) == 0) def_pt_TemplateName.append(PTTEMP_EXT); |
| 378 |
remove_spaces_from_filename(def_pt_TemplateName); |
| 379 |
|
| 380 |
clear_header(); |
| 381 |
update_header(CHANGED); |
| 382 |
ptbuffer.assign(header("<plaintext>")); |
| 383 |
make_ptbuffer(); |
| 384 |
write_pt(def_pt_TemplateName); |
| 385 |
|
| 386 |
show_filename(def_pt_TemplateName); |
| 387 |
using_pt_template = true; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
void cb_pt_open() |
| 392 |
{ |
| 393 |
const char *p = FSEL::select(_("Open data file"), "plain_text\t*.p2s", |
| 394 |
def_pt_filename.c_str()); |
| 395 |
if (!p) return; |
| 396 |
if (strlen(p) == 0) return; |
| 397 |
clear_pt_form(); |
| 398 |
read_data_file(p); |
| 399 |
using_pt_template = false; |
| 400 |
def_pt_filename = p; |
| 401 |
show_filename(def_pt_filename); |
| 402 |
} |
| 403 |
|
| 404 |
void write_pt(string s) |
| 405 |
{ |
| 406 |
FILE *ptfile = fopen(s.c_str(), "w"); |
| 407 |
if (!ptfile) return; |
| 408 |
|
| 409 |
fwrite(ptbuffer.c_str(), ptbuffer.length(), 1, ptfile); |
| 410 |
fclose(ptfile); |
| 411 |
} |
| 412 |
|
| 413 |
bool cb_pt_save_as() |
| 414 |
{ |
| 415 |
const char *p; |
| 416 |
string newfilename; |
| 417 |
|
| 418 |
string name = named_file(); |
| 419 |
if (!name.empty()) { |
| 420 |
name.append(".p2s"); |
| 421 |
newfilename = ICS_msg_dir; |
| 422 |
newfilename.append(name); |
| 423 |
} else |
| 424 |
newfilename = def_pt_filename; |
| 425 |
|
| 426 |
p = FSEL::saveas(_("Save data file"), "plain_text\t*.p2s", |
| 427 |
newfilename.c_str()); |
| 428 |
|
| 429 |
if (!p) return false; |
| 430 |
if (strlen(p) == 0) return false; |
| 431 |
|
| 432 |
if (progStatus.sernbr_fname) update_sernbr(); |
| 433 |
|
| 434 |
const char *pext = fl_filename_ext(p); |
| 435 |
def_pt_filename = p; |
| 436 |
if (strlen(pext) == 0) def_pt_filename.append(".p2s"); |
| 437 |
|
| 438 |
remove_spaces_from_filename(def_pt_filename); |
| 439 |
|
| 440 |
update_header(NEW); |
| 441 |
update_ptfields(); |
| 442 |
ptbuffer.assign(header("<plaintext>")); |
| 443 |
make_ptbuffer(); |
| 444 |
write_pt(def_pt_filename); |
| 445 |
|
| 446 |
using_pt_template = false; |
| 447 |
show_filename(def_pt_filename); |
| 448 |
return true; |
| 449 |
} |
| 450 |
|
| 451 |
void cb_pt_save() |
| 452 |
{ |
| 453 |
if (base_pt_filename == "new.p2s" || |
| 454 |
base_pt_filename == "default.p2s" || |
| 455 |
using_pt_template == true) { |
| 456 |
cb_pt_save_as(); |
| 457 |
return; |
| 458 |
} |
| 459 |
|
| 460 |
if (check_ptfields()) update_header(CHANGED); |
| 461 |
update_ptfields(); |
| 462 |
ptbuffer.assign(header("<plaintext>")); |
| 463 |
make_ptbuffer(); |
| 464 |
write_pt(def_pt_filename); |
| 465 |
|
| 466 |
using_pt_template = false; |
| 467 |
} |
| 468 |
|
| 469 |
void cb_pt_html() |
| 470 |
{ |
| 471 |
string pt_name = ICS_dir; |
| 472 |
string html_text = ""; |
| 473 |
pt_name.append("plaintext_doc.html"); |
| 474 |
|
| 475 |
update_ptfields(); |
| 476 |
string ptform = pt_html_template; |
| 477 |
|
| 478 |
for (int i = 0; i < num_ptfields; i++) { |
| 479 |
if (ptfields[i].w_type != 'e') |
| 480 |
replacestr( ptform, ptfields[i].f_type, ptfields[i].f_data ); |
| 481 |
else { |
| 482 |
html_text = "<pre><big style= font-family: \"Consolas\", \"Monospace\", \"Courier\";\">"; |
| 483 |
if (progStatus.autowordwrap) |
| 484 |
html_text.append(wordwrap(ptfields[i].f_data, progStatus.charcount)); |
| 485 |
else |
| 486 |
html_text.append(ptfields[i].f_data); |
| 487 |
html_text.append("</big></pre>"); |
| 488 |
replacestr( ptform, ptfields[i].f_type, html_text ); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
FILE *ptfile = fopen(pt_name.c_str(), "w"); |
| 493 |
fprintf(ptfile,"%s", ptform.c_str()); |
| 494 |
fclose(ptfile); |
| 495 |
|
| 496 |
open_url(pt_name.c_str()); |
| 497 |
} |
| 498 |
|
| 499 |
void cb_pt_msg_type() |
| 500 |
{ |
| 501 |
if (tabs_msg_type->value() == tab_plaintext ) { |
| 502 |
show_filename(def_pt_filename); |
| 503 |
} else { |
| 504 |
show_filename(def_rg_filename); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
void cb_pt_textout() |
| 509 |
{ |
| 510 |
string pt_name = ICS_dir; |
| 511 |
pt_name.append("plaintext.txt"); |
| 512 |
|
| 513 |
update_ptfields(); |
| 514 |
string ptform = pt_txt_template; |
| 515 |
|
| 516 |
for (int i = 0; i < num_ptfields; i++) |
| 517 |
replacestr( ptform, ptfields[i].f_type, ptfields[i].f_data); |
| 518 |
|
| 519 |
FILE *ptfile = fopen(pt_name.c_str(), "w"); |
| 520 |
fprintf(ptfile,"%s", ptform.c_str()); |
| 521 |
fclose(ptfile); |
| 522 |
|
| 523 |
open_url(pt_name.c_str()); |
| 524 |
} |