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 #include "icons.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
72 #include "transfer.h"
73
74 using namespace std;
75
76 string transfer_buffer = "";
77 string def_transfer_filename = "";
78 string base_transfer_filename = "";
79
80 const char xfrstr[] = "<transfer>\n";
81
clear_transfer_form()82 void clear_transfer_form()
83 {
84 txt_send_filename->value("");
85 show_filename("");
86 transfer_buffer.clear();
87 }
88
read_transfer_buffer(string data)89 void read_transfer_buffer(string data)
90 {
91 size_t p1 = data.find(xfrstr);
92 if (p1 != string::npos) p1 += strlen(xfrstr);
93 data.erase(0, p1);
94 transfer_buffer = data;
95 read_header(data);
96 }
97
cb_transfer_new()98 void cb_transfer_new()
99 {
100 clear_transfer_form();
101 def_transfer_filename = XFR_dir;
102 show_filename(def_transfer_filename);
103 clear_estimate();
104 }
105
cb_transfer_import()106 void cb_transfer_import()
107 {
108 fl_alert2("Not implemented");
109 }
110
cb_transfer_export()111 void cb_transfer_export()
112 {
113 fl_alert2("Not implemented");
114 }
115
cb_transfer_wrap_import(string fname,string txt)116 void cb_transfer_wrap_import(string fname, string txt)
117 {
118 clear_transfer_form();
119
120 string fnam = fname;
121 string outfname;
122 string tst_name;
123 string tst_ext = "";
124
125 // write to file with name(s) such as
126 // test.b2s, test-01.txt, test-02.txt etc. up to 999
127 size_t pos = fnam.find(".");
128 if (pos != string::npos) {
129 tst_name = fnam.substr(0, pos);
130 tst_ext = fnam.substr(pos);
131 } else
132 tst_name = fnam;
133
134 char fnum[8]="";
135 int num = 0;
136 outfname.assign(XFR_dir).append(tst_name).append(tst_ext);
137
138 ifstream tstfile;
139 tstfile.open(outfname.c_str());
140 while (tstfile) {
141 tstfile.close();
142 snprintf(fnum, sizeof(fnum), "-%03d", (++num % 1000));
143 outfname.assign(XFR_dir).append(tst_name).append(fnum).append(tst_ext);
144 tstfile.open(outfname.c_str());
145 }
146
147 // decompress_maybe returns 0 on failure
148
149 pos = txt.find(xfrstr);
150 if (pos != string::npos) {
151 LOG_INFO("Text length %d", (int)txt.length());
152 pos += strlen(xfrstr);
153 txt.erase(0, pos);
154 }
155
156 pos = txt.find("[b64");
157 if (pos != string::npos) {
158 txt.erase(0, pos);
159 decompress_maybe(txt);
160 LOG_INFO("File length %d", (int)txt.length());
161 }
162
163 ofstream ofile(outfname.c_str(), ios::binary);
164 if (ofile) ofile << txt;
165 ofile.close();
166
167 brws_xfr_filenames->add(outfname.c_str());
168 }
169
eval_transfer_fsize()170 int eval_transfer_fsize()
171 {
172 if (transfer_buffer.empty()) return 0;
173
174 Ccrc16 chksum;
175
176 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
177 evalstr.append(fl_filename_name(def_transfer_filename.c_str())).append("]");
178 evalstr.append(header(xfrstr));
179
180 string outbuf(transfer_buffer);
181 if (outbuf.empty()) return 0;
182
183 if (!progStatus.send_original)
184 compress_maybe( outbuf, true );
185
186 evalstr.append( outbuf );
187 string ck = chksum.scrc16(evalstr);
188
189 evalstr.append("[WRAP:chksum ").append(ck).append("][WRAP:end]");
190 return evalstr.length();
191 }
192
193 // not called in 3.x
194
cb_transfer_wrap_export()195 void cb_transfer_wrap_export()
196 {
197 if (transfer_buffer.empty()) return;
198
199 string wrapfilename = WRAP_send_dir;
200 base_transfer_filename = fl_filename_name(def_transfer_filename.c_str());
201 wrapfilename.append(base_transfer_filename);
202 wrapfilename.append(WRAP_EXT);
203 const char *p = FSEL::saveas(
204 "Save as wrap file",
205 "Wrap file\t*.{wrap,WRAP}",
206 wrapfilename.c_str());
207 if (p) {
208 string pext = fl_filename_ext(p);
209 wrapfilename = p;
210
211 update_header(FROM);
212 string fbuff(header("<transfer>"));
213 string outbuf(transfer_buffer);
214 if (!progStatus.send_original)
215 compress_maybe(outbuf, true);
216 fbuff.append(outbuf);
217
218 export_wrapfile(base_transfer_filename, wrapfilename, fbuff, pext != WRAP_EXT);
219 }
220 }
221
cb_transfer_wrap_autosend()222 void cb_transfer_wrap_autosend()
223 {
224 if (transfer_buffer.empty()) return;
225
226 update_header(FROM);
227 string fbuff(header("<transfer>"));
228 string outbuf(transfer_buffer);
229 if (!progStatus.send_original)
230 compress_maybe(outbuf, true);
231 fbuff.append(outbuf);
232 xfr_via_socket(fl_filename_name(def_transfer_filename.c_str()), fbuff);
233
234 }
235
cb_transfer_load_template()236 void cb_transfer_load_template()
237 {
238 fl_alert2("Not implemented");
239 }
240
cb_transfer_save_template()241 void cb_transfer_save_template()
242 {
243 fl_alert2("Not implemented");
244 }
245
cb_transfer_open_as_template()246 void cb_transfer_open_as_template()
247 {
248 fl_alert2("Not implemented");
249 }
250
cb_transfer_open()251 void cb_transfer_open()
252 {
253 const char *p = FSEL::select(_("Open file"), "transfer\t*",
254 def_transfer_filename.c_str());
255 if (!p) return;
256 if (strlen(p) == 0) return;
257 clear_transfer_form();
258 def_transfer_filename = p;
259 txt_send_filename->value(def_transfer_filename.c_str());
260 transfer_buffer.clear();
261
262 FILE *dfile = fopen(p, "rb");
263 if (!dfile) {
264 txt_send_filename->value("");
265 transfer_buffer.clear();
266 return;
267 }
268 fseek(dfile, 0, SEEK_END);
269 size_t fsize = ftell(dfile);
270 if (fsize <= 0) {
271 fclose (dfile);
272 return;
273 }
274 fseek(dfile, 0, SEEK_SET);
275 transfer_buffer.resize(fsize);
276 size_t r = fread((void *)transfer_buffer.c_str(), 1, fsize, dfile);
277 fclose(dfile);
278 if (r != fsize) {
279 txt_send_filename->value("");
280 transfer_buffer.clear();
281 return;
282 }
283
284 estimate();
285 }
286
write_transfer(string s)287 void write_transfer(string s)
288 {
289 return;
290 }
291
cb_transfer_open_as()292 void cb_transfer_open_as()
293 {
294 int selected = brws_xfr_filenames->value();
295 if (!selected) return;
296 const char *p = brws_xfr_filenames->text(selected);
297
298 open_url(p);
299
300 return;
301 }
302
cb_transfer_msg_type()303 void cb_transfer_msg_type()
304 {
305 if (tabs_msg_type->value() == tab_transfer ) {
306 show_filename(def_transfer_filename);
307 } else {
308 show_filename(def_rg_filename);
309 }
310 }
311
cb_transfer_html()312 void cb_transfer_html()
313 {
314 fl_alert2("Not implemented");
315 }
316
cb_transfer_textout()317 void cb_transfer_textout()
318 {
319 fl_alert2("Not implemented");
320 }
321
cb_transfer_import_data()322 void cb_transfer_import_data()
323 {
324 fl_alert2("Not implemented");
325 }
326
cb_transfer_export_data()327 void cb_transfer_export_data()
328 {
329 fl_alert2("Not implemented");
330 }
331
332