1 // ----------------------------------------------------------------------------
2 // record_loader.cxx
3 //
4 // Copyright (C) 2013
5 //		Remi Chateauneu, F4ECW
6 //
7 // This file is part of fldigi.
8 //
9 // Fldigi is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // Fldigi is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with fldigi.  If not, see <http://www.gnu.org/licenses/>.
21 // ----------------------------------------------------------------------------
22 
23 #include <config.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <errno.h>
27 
28 #include <sstream>
29 
30 #ifdef __MINGW32__
31 #  include "compat.h"
32 #endif
33 
34 #include "record_loader.h"
35 #include "record_browse.h"
36 #include "debug.h"
37 #include "main.h"
38 #include "icons.h"
39 #include "fl_digi.h"
40 #include "strutil.h"
41 #include "gettext.h"
42 #include "network.h"
43 
44 #include <stdexcept>
45 #include <fstream>
46 #include <iostream>
47 
48 #include <sys/stat.h>
49 
50 #include "FL/Fl_Double_Window.H"
51 #include "FL/Fl_Output.H"
52 #include "FL/fl_ask.H"
53 #include "FL/Fl_Check_Button.H"
54 
55 LOG_FILE_SOURCE(debug::LOG_DATASOURCES);
56 
57 Fl_Double_Window *dlgRecordLoader = (Fl_Double_Window *)0;
58 
59 /// Loads a file and stores it for later lookup.
LoadAndRegister()60 int RecordLoaderInterface::LoadAndRegister()
61 {
62 	Clear();
63 
64 	std::string filnam = storage_filename().first;
65 
66 	time_t cntTim = time(NULL);
67 	LOG_INFO("Opening:%s", filnam.c_str());
68 
69 	std::ifstream ifs( filnam.c_str() );
70 
71 	/// Reuse the same string for each new record.
72 	std::string input_str ;
73 	size_t nbRec = 0 ;
74 	while( ! ifs.eof() )
75 	{
76 		if( ! std::getline( ifs, input_str ) ) break;
77 
78 		/// Comments are legal with # as first character.
79 		if( input_str[0] == '#' ) continue;
80 
81 		imemstream str_strm( input_str );
82 		try
83 		{
84 			if( ReadRecord( str_strm ) ) {
85 				++nbRec;
86 			} else {
87 				LOG_WARN( "Cannot process '%s'", input_str.c_str() );
88 			}
89 		}
90 		catch(const std::exception & exc)
91 		{
92 			LOG_WARN( "%s: Caught <%s> when reading '%s'",
93 				base_filename().c_str(),
94 				exc.what(),
95 				input_str.c_str() );
96 			return -1 ;
97 		}
98 	}
99 	ifs.close();
100 	LOG_INFO( "Read:%s with %d records in %d seconds",
101 		filnam.c_str(), static_cast<int>(nbRec),
102 		static_cast<int>( time(NULL) - cntTim ) );
103 	return nbRec ;
104 }
105 
106 // ----------------------------------------------------------------------------
107 
108 struct Row
109 {
110 	Fl_Output             * m_timestamp ;
111 	Fl_Check_Button       * m_select ;
112 	Fl_Output             * m_content_size ;
113 	Fl_Output             * m_nb_rows ;
114 	Fl_Button             * m_url ;
115 	RecordLoaderInterface * m_itf ;
116 
UpdateRowRow117 	bool UpdateRow()
118 	{
119 		const std::string str = m_itf->Timestamp();
120 		m_timestamp->value(str.c_str());
121 
122 		const std::string & strSz = m_itf->ContentSize();
123 		m_content_size->value(strSz.c_str());
124 		int nb_recs = m_itf->LoadAndRegister();
125 		char nb_recs_str[64];
126 		bool isGood = ( nb_recs >= 0 );
127 
128 		if( isGood )
129 			snprintf( nb_recs_str, sizeof(nb_recs_str), "%6d", nb_recs );
130 		else
131 			strcpy( nb_recs_str, "   N/A" );
132 		m_nb_rows->value(nb_recs_str);
133 		const char * strurl = m_itf->Url();
134 
135 		if( strurl != NULL ) {
136 			const std::string strnam = m_itf->base_filename();
137 			m_url->tooltip( strurl );
138 		}
139 		if (dlgRecordLoader)
140 			dlgRecordLoader->damage();
141 		return isGood ;
142 	}
143 };
144 
145 /// Array all data loaders. It is setup at start time.
146 static Row * all_recs = NULL ;
147 
148 /// Number of data loaders, it is a very small integer.
149 static int dataloader_nb = 0 ;
150 
151 static const int nb_cols = 5 ;
152 
153 // ----------------------------------------------------------------------------
154 
155 /// This is a virtual class, therefore it must have a default constructor.
RecordLoaderInterface()156 RecordLoaderInterface::RecordLoaderInterface()
157 {
158 	++dataloader_nb ;
159 	/// We prefer tp use realloc because it is ready before main() is called.
160 	all_recs = (Row *)realloc( all_recs, dataloader_nb * sizeof( Row ) );
161 
162 	all_recs[ dataloader_nb - 1 ].m_itf = this ;
163 }
164 
165 /// This happens very rarely, so performance is not an issue.
~RecordLoaderInterface()166 RecordLoaderInterface::~RecordLoaderInterface()
167 {
168 	for( int i = 0; i < dataloader_nb; ++i )
169 	{
170 		if( all_recs[i].m_itf == this ) {
171 			memmove( all_recs + i, all_recs + i + 1, sizeof( Row ) * ( dataloader_nb - i - 1 ) );
172 			--dataloader_nb ;
173 			return ;
174 		}
175 	}
176 	LOG_ERROR("Inconsistent %d", dataloader_nb );
177 
178 }
179 
180 /// This takes only the filename from the complete HTTP or FTP URL, or file path.
base_filename() const181 std::string RecordLoaderInterface::base_filename() const
182 {
183 	const char * pFil = strrchr( Url(), '/' );
184 	if( pFil == NULL )
185 		pFil =  Url();
186 	else
187 		++pFil ;
188 
189 	/// This might be an URL so we take only the beginning.
190 	const char * quest = strchr( pFil, '?' );
191 	if( quest == NULL ) quest = pFil + strlen(pFil);
192 	return std::string( pFil, quest );
193 }
194 
storage_filename(bool create_dir) const195 std::pair< std::string, bool > RecordLoaderInterface::storage_filename(bool create_dir) const
196 {
197 	/// We check if it is changed, it is not performance-critical.
198 	if( create_dir ) {
199 		if( ask_dir_creation( DATA_dir ) ) {
200 			const char * err = create_directory( DATA_dir.c_str() );
201 			if( err ) {
202 				fl_alert("Error:%s",err);
203 			}
204 		}
205 	}
206 
207 	std::string filnam_data = DATA_dir;
208 	filnam_data.append(base_filename());
209 	if( create_dir ) {
210 		return std::make_pair( filnam_data, false );
211 	}
212 
213 	/// This is for a read access.
214 	std::ifstream ifs( filnam_data.c_str() );
215 	if( ifs )
216 	{
217 		ifs.close();
218 		return std::make_pair( filnam_data, false );
219 	}
220 
221 	if( errno != ENOENT ) {
222 		LOG_WARN( "Cannot read '%s': %s", filnam_data.c_str(), strerror(errno) );
223 	}
224 
225 	// Second try with a file maybe installed by "make install".
226 	std::string filnam_inst = PKGDATADIR "/" + base_filename();
227 	LOG_INFO("Errno=%s with %s. Trying %s",
228 		strerror(errno), filnam_data.c_str(), filnam_inst.c_str() );
229 	ifs.open( filnam_inst.c_str() );
230 	if( ifs )
231 	{
232 		ifs.close();
233 		return std::make_pair( filnam_inst, true );
234 	}
235 	/// But the file is not there.
236 	return std::make_pair( filnam_data, false );
237 }
238 
Timestamp() const239 std::string RecordLoaderInterface::Timestamp() const
240 {
241 	std::string filnam = storage_filename().first;
242 
243 	struct stat st;
244 	if (stat(filnam.c_str(), &st) == -1 ) return "N/A";
245 
246 	struct tm tmLastMod = *localtime( & st.st_mtime );
247 
248 	char buf[64];
249 	snprintf(buf, sizeof(buf), "%d/%d/%d %02d:%02d",
250 			tmLastMod.tm_year + 1900,
251 			tmLastMod.tm_mon + 1,
252 			tmLastMod.tm_mday,
253 			tmLastMod.tm_hour,
254 			tmLastMod.tm_min );
255 
256 	return buf ;
257 }
258 
ContentSize() const259 std::string RecordLoaderInterface::ContentSize() const
260 {
261 	/// It would be faster to cache this result in the object.
262 	std::string filnam = storage_filename().first;
263 
264 	struct stat st;
265 	if (stat(filnam.c_str(), &st) == -1 ) return "      N/A";
266 
267 	std::stringstream buf;
268 	buf.width(9); buf.fill(' ');
269 	buf <<  st.st_size;
270 	return buf.str();
271 }
272 
273 // ----------------------------------------------------------------------------
274 
cb_record_url(Fl_Widget * w,void * ptr)275 static void cb_record_url(Fl_Widget *w, void* ptr)
276 {
277 	const RecordLoaderInterface * it = static_cast< const RecordLoaderInterface * >(ptr);
278 	cb_mnuVisitURL( NULL, const_cast< char * >( it->Url() ) );
279 }
280 
AddRow(int row)281 void DerivedRecordLst::AddRow( int row )
282 {
283 	Row * ptRow = all_recs + row ;
284 
285 	int X,Y,W,H;
286 	int col=0;
287 	{
288 		col_width( col, 110 );
289 		find_cell(CONTEXT_TABLE, row, col, X, Y, W, H);
290 
291 		ptRow->m_timestamp = new Fl_Output(X,Y,W,H);
292 		ptRow->m_timestamp->tooltip( _("Data file creation date") );
293 	}
294 	++col;
295 	{
296 		col_width( col, 16 );
297 		find_cell(CONTEXT_TABLE, row, col, X, Y, W, H);
298 
299 		ptRow->m_select = new Fl_Check_Button(X,Y,W,H);
300 		ptRow->m_select->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE);
301 		ptRow->m_select->value(1);
302 	}
303 	++col;
304 	{
305 		col_width( col, 80 );
306 		find_cell(CONTEXT_TABLE, row, col, X, Y, W, H);
307 
308 		ptRow->m_content_size = new Fl_Output(X,Y,W,H);
309 		ptRow->m_content_size->tooltip( _("Size in bytes") );
310 	}
311 	++col;
312 	{
313 		col_width( col, 50 );
314 		find_cell(CONTEXT_TABLE, row, col, X, Y, W, H);
315 
316 		ptRow->m_nb_rows = new Fl_Output(X,Y,W,H);
317 		ptRow->m_nb_rows->tooltip( _("Number of lines in data file") );
318 	}
319 	++col;
320 	{
321 		col_width( col, 166 );
322 		find_cell(CONTEXT_TABLE, row, col, X, Y, W, H);
323 		const char * strurl = ptRow->m_itf->Url();
324 
325 		ptRow->m_url = NULL ;
326 		if( strurl != NULL ) {
327 			const std::string strnam = ptRow->m_itf->base_filename();
328 
329 			ptRow->m_url = new Fl_Button(X,Y,W,H, strdup(strnam.c_str()) );
330 			ptRow->m_url->tooltip( strurl );
331 			ptRow->m_url->callback(cb_record_url, ptRow->m_itf);
332 		} else {
333 			ptRow->m_url = new Fl_Button(X, Y, W, H, "N/A");
334 		}
335 	}
336 	ptRow->UpdateRow();
337 }
338 
DerivedRecordLst(int x,int y,int w,int h,const char * title)339 DerivedRecordLst::DerivedRecordLst(int x, int y, int w, int h, const char *title)
340 : Fl_Table(x,y,w,h,title)
341 {
342 	col_header(1);
343 	col_resize(1);
344 	col_header_height(25);
345 	row_header(1);
346 	row_resize(0);
347 	row_header_width(105);
348 
349 	rows(dataloader_nb);
350 	cols(nb_cols);
351 
352 	begin();		// start adding widgets to group
353 	{
354 		for( int row = 0; row < dataloader_nb; ++row )
355 		{
356 			AddRow( row );
357 		}
358 	}
359 	end();
360 }
361 
~DerivedRecordLst()362 DerivedRecordLst::~DerivedRecordLst()
363 {
364 }
365 
366 // Handle drawing all cells in table
draw_cell(TableContext context,int R,int C,int X,int Y,int W,int H)367 void DerivedRecordLst::draw_cell(TableContext context,
368 			  int R, int C, int X, int Y, int W, int H)
369 {
370 	switch ( context )
371 	{
372 	case CONTEXT_STARTPAGE:
373 		fl_font(FL_HELVETICA, 12);		// font used by all headers
374 		break;
375 
376 	case CONTEXT_RC_RESIZE:
377 	{
378 		int X, Y, W, H;
379 		int index = 0;
380 		for ( int r = 0; r<rows(); r++ )
381 		{
382 			for ( int c = 0; c<cols(); c++ )
383 			{
384 				if ( index >= children() ) break;
385 				find_cell(CONTEXT_TABLE, r, c, X, Y, W, H);
386 				child(index++)->resize(X,Y,W,H);
387 			}
388 		}
389 		init_sizes();			// tell group children resized
390 		return;
391 	}
392 
393 	case CONTEXT_ROW_HEADER:
394 		fl_push_clip(X, Y, W, H);
395 		{
396 			RecordLoaderInterface * it = ( (R >= 0) && ( R < dataloader_nb ) ) ? all_recs[ R ].m_itf : NULL;
397 			if( it == NULL ) {
398 				LOG_ERROR("R=%d",R);
399 				return;
400 			}
401 
402 			const char * str = it ? it->Description() : "Unknown" ;
403 
404 			fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, row_header_color());
405 			fl_color(FL_BLACK);
406 			fl_draw(str, X, Y, W, H, FL_ALIGN_CENTER);
407 		}
408 		fl_pop_clip();
409 		return;
410 
411 	case CONTEXT_COL_HEADER:
412 		fl_push_clip(X, Y, W, H);
413 		{
414 			static const char * col_names[nb_cols] = {
415 				_("Timestamp"),
416 				_(" "),
417 				_("Size"),
418 				_("# recs"),
419 				_("WWW"),
420 			};
421 
422 			const char * title = ( ( C >= 0 ) && ( C < nb_cols ) ) ? col_names[C] : "?" ;
423 
424 			fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, col_header_color());
425 			fl_color(FL_BLACK);
426 			fl_draw(title, X, Y, W, H, FL_ALIGN_CENTER);
427 		}
428 		fl_pop_clip();
429 		return;
430 	case CONTEXT_CELL:
431 		// fl_push_clip(X, Y, W, H);
432 		return;	// fltk handles drawing the widgets
433 
434 	default:
435 		return;
436 	}
437 }
438 
cbGuiUpdate()439 void DerivedRecordLst::cbGuiUpdate()
440 {
441 	std::string server = inpDataSources->value();
442 	if( server.empty() ) {
443 		fl_alert(_("No server selected"));
444 		return ;
445 	}
446 
447 	if( server[server.size()-1] != '/' ) server += '/' ;
448 
449 	for( int row = 0; row < dataloader_nb; ++row ) {
450 		Row * ptrRow = all_recs + row;
451 		if( ! ptrRow->m_select->value() ) continue ;
452 		RecordLoaderInterface * it = ptrRow->m_itf;
453 
454 		std::string url = server + it->base_filename();
455 		std::string reply ;
456 
457 //		double timeout=5.0;
458 		// Consider truncating the HTTP header.
459 //		int res = get_http_gui(url, reply, timeout );
460 		int res = get_http(url, reply, 5.0);
461 		LOG_INFO("Loaded %s : %d chars. res=%d",
462 			url.c_str(), (int)reply.size(), res );
463 		if (reply.empty()) {
464 			int ok = fl_choice2(
465 					_("Could not download %s"),
466 					_("Continue"),
467 					_("Stop"),
468 					NULL,
469 					url.c_str() );
470 			if( ok == 1 ) break ;
471 			continue ;
472 		}
473 
474 		static const char *notFound404 = "HTTP/1.1 404 Not Found";
475 		if( 0 == strncmp( reply.c_str(), notFound404, strlen(notFound404) ) )
476 		{
477 			int ok = fl_choice2(
478 					_("Non-existent URL: %s"),
479 					_("Continue"),
480 					_("Stop"),
481 					NULL,
482 					url.c_str() );
483 			if( ok == 1 ) break ;
484 			continue ;
485 		}
486 
487 		/// This creates the directory if necessary;
488 		std::string filnam = it->storage_filename(true).first;
489 		std::ofstream ofstrm( filnam.c_str() );
490 		if( ofstrm )
491 			ofstrm.write( &reply[0], reply.size() );
492 		if( ! ofstrm ) {
493 			int ok = fl_choice2(
494 					_("Error saving %s to %s:%s"),
495 					_("Continue"),
496 					_("Stop"),
497 					NULL,
498 					url.c_str(), filnam.c_str(), strerror(errno) );
499 			if( ok == 1 ) break ;
500 			continue ;
501 		}
502 		ofstrm.close();
503 
504 		bool isGood = all_recs[row].UpdateRow();
505 		if( ! isGood ) {
506 			int ok = fl_choice2(
507 					_("Error loading %s to %s: %s."),
508 					_("Continue"),
509 					_("Stop"),
510 					NULL,
511 					url.c_str(), filnam.c_str(), strerror(errno) );
512 			if( ok == 0 ) break ;
513 			continue ;
514 		}
515 	}
516 	btnDataSourceUpdate->value(0);
517 }
518 
cbGuiReset()519 void DerivedRecordLst::cbGuiReset()
520 {
521 	fprintf(stderr, "%s\n", __FUNCTION__ );
522 
523 	for( int row = 0; row < dataloader_nb; ++row )
524 	{
525 		Row * ptrRow = all_recs + row;
526 		if( ! ptrRow->m_select->value() ) continue ;
527 		RecordLoaderInterface * it = ptrRow->m_itf;
528 
529 		std::pair< std::string, bool > stofil_pair = it->storage_filename(true);
530 
531 		it->Clear();
532 
533 		const char * stofil = stofil_pair.first.c_str() ;
534 		if( stofil_pair.second ) {
535 			fl_alert("Cannot erase installed data file %s", stofil );
536 			continue ;
537 		} else {
538 			LOG_INFO("Erasing %s", stofil );
539 			int res = ::remove( stofil );
540 			if( ( res != 0 ) && ( res != ENOENT ) ) {
541 				fl_alert("Error erasing data file %s:%s", stofil, strerror(errno) );
542 				continue ;
543 			}
544 		all_recs[row].UpdateRow();
545 		}
546 	}
547 }
548 
549 // ----------------------------------------------------------------------------
550 
551 /// Necessary because in a Fl_Menu, a slash has a special meaning.
fl_escape(const char * str)552 static std::string fl_escape( const char * str )
553 {
554 	std::string res ;
555 	for( char ch ; ( ch = *str ) != '\0' ; ++str )
556 	{
557 		if( ch == '/' ) res += '\\';
558 		res += ch  ;
559 	}
560 	return res ;
561 }
562 
fl_input_add(const char * str)563 static void fl_input_add( const char * str )
564 {
565 	inpDataSources->add( fl_escape( str ).c_str() );
566 }
567 
createRecordLoader()568 void createRecordLoader()
569 {
570 	if (dlgRecordLoader) return;
571 	dlgRecordLoader = make_record_loader_window();
572 	fl_input_add("http://www.w1hkj.com/support_files/");
573 
574 	inpDataSources->value(0);
575 }
576 // ----------------------------------------------------------------------------
577