1 // fgadmin_funcs.cxx -- FG Admin UI functions.
3 // Written by Curtis Olson, started February 2004.
5 // Copyright (c) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <FL/Fl_File_Chooser.H>
36 #include <simgear/misc/sg_path.hxx>
46 extern string def_install_source;
47 extern string def_scenery_dest;
49 static const float min_progress = 0.0;
50 static const float max_progress = 5000.0;
53 FGAdminUI::~FGAdminUI() {
58 // initialize additional class elements
59 void FGAdminUI::init() {
60 prefs = new Fl_Preferences( Fl_Preferences::USER,
63 char buf[FL_PATH_MAX];
64 prefs->get( "install-source", buf, def_install_source.c_str(), FL_PATH_MAX );
65 source_text->value( buf );
68 prefs->get( "scenery-dest", buf, def_scenery_dest.c_str(), FL_PATH_MAX );
69 dest_text->value( buf );
74 progress->minimum( min_progress );
75 progress->maximum( max_progress );
77 main_window->size_range( 465, 435 );
81 void FGAdminUI::show() {
86 // refresh the check box lists
87 void FGAdminUI::refresh_lists() {
93 // scan the source directory and update the install_box contents
95 void FGAdminUI::quit() {
100 // select the install source
101 void FGAdminUI::select_install_source() {
102 char* p = fl_dir_chooser( "Select Scenery Source Directory",
107 source_text->value( p );
108 prefs->set( "install-source", p );
114 // select the install source
115 void FGAdminUI::select_install_dest() {
116 char* p = fl_dir_chooser( "Select Scenery Install Directory",
121 dest_text->value( p );
122 prefs->set( "scenery-dest", p );
128 // Like strcmp, but for strings
129 static int stringCompare(const void *string1, const void *string2)
131 const string *s1 = (const string *)string1;
132 const string *s2 = (const string *)string2;
134 // Compare name first, and then index.
135 return strcmp(s1->c_str(), s2->c_str());
139 void FGAdminUI::update_install_box() {
141 vector<string> file_list;
144 install_box->clear();
146 if ( source.length() ) {
147 ulDir *dir = ulOpenDir( source.c_str() ) ;
149 while ( dir != 0 && ( ent = ulReadDir( dir ) ) ) {
150 // find base name of archive file
151 char base[FL_PATH_MAX];
152 strncpy( base, ent->d_name, FL_PATH_MAX );
153 const char *p = fl_filename_ext( base );
155 if ( strcmp( p, ".gz" ) == 0 ) {
158 p = fl_filename_ext( base );
159 if ( strcmp( p, ".tar" ) == 0 ) {
163 } else if ( strcmp( p, ".zip" ) == 0 ) {
168 if ( strlen(ent->d_name) != 14 ) {
169 // simple heuristic to ignore non-scenery files
170 } else if ( ent->d_name[0] != 'e' && ent->d_name[0] != 'w' ) {
171 // further sanity checks on name
172 } else if ( ent->d_name[4] != 'n' && ent->d_name[4] != 's' ) {
173 // further sanity checks on name
175 // add to list if not installed
176 SGPath install( dest );
177 install.append( base );
178 if ( ! fl_filename_isdir( install.c_str() ) ) {
179 // cout << install.str() << " install candidate." << endl;
180 file_list.push_back( ent->d_name );
182 // cout << install.str() << " exists." << endl;
188 // convert to a qsort()'able array
189 string *sort_list = new string[file_list.size()];
190 for ( i = 0; i < file_list.size(); ++i ) {
191 sort_list[i] = fl_filename_name( file_list[i].c_str() );
194 // Sort the file list into display order
195 qsort(sort_list, file_list.size(), sizeof(string), stringCompare);
197 for ( int i = 0; i < file_list.size(); ++i ) {
198 install_box->add( sort_list[i].c_str() );
203 install_box->redraw();
208 // scan the source directory and update the install_box contents
209 void FGAdminUI::update_remove_box() {
211 vector<string> dir_list;
216 if ( dest.length() ) {
217 ulDir *dir = ulOpenDir( dest.c_str() ) ;
219 while ( dir != 0 && ( ent = ulReadDir( dir ) ) ) {
220 if ( strlen(ent->d_name) != 7 ) {
221 // simple heuristic to ignore non-scenery directories
222 } else if ( ent->d_name[0] != 'e' && ent->d_name[0] != 'w' ) {
223 // further sanity checks on name
224 } else if ( ent->d_name[4] != 'n' && ent->d_name[4] != 's' ) {
225 // further sanity checks on name
227 dir_list.push_back( ent->d_name );
232 // convert to a qsort()'able array
233 string *sort_list = new string[dir_list.size()];
234 for ( i = 0; i < dir_list.size(); ++i ) {
235 sort_list[i] = fl_filename_name( dir_list[i].c_str() );
238 // Sort the file list into display order
239 qsort(sort_list, dir_list.size(), sizeof(string), stringCompare);
241 for ( int i = 0; i < dir_list.size(); ++i ) {
242 remove_box->add( sort_list[i].c_str() );
247 remove_box->redraw();
252 // install selected files
253 void FGAdminUI::install_selected() {
256 install_b->deactivate();
257 remove_b->deactivate();
258 quit_b->deactivate();
260 // traverse install box and install each item
261 for ( int i = 0; i <= install_box->nitems(); ++i ) {
262 if ( install_box->checked( i ) ) {
263 f = install_box->text( i );
264 SGPath file( source );
267 stat( file.str().c_str(), &info );
268 float old_max = progress->maximum();
269 progress->maximum( info.st_size );
270 progress_label = "Installing ";
272 progress->label( progress_label.c_str() );
273 progress->value( min_progress );
274 main_window->cursor( FL_CURSOR_WAIT );
275 tarextract( (char *)file.c_str(), (char *)dest.c_str(), true, &FGAdminUI::step, this );
276 progress->value( min_progress );
277 main_window->cursor( FL_CURSOR_DEFAULT );
278 progress->label( "" );
279 progress->maximum( old_max );
283 install_b->activate();
284 remove_b->activate();
290 static unsigned long count_dir( const char *dir_name ) {
291 ulDir *dir = ulOpenDir( dir_name ) ;
293 unsigned long cnt = 0L;
294 while ( ent = ulReadDir( dir ) ) {
295 if ( strcmp( ent->d_name, "." ) == 0 ) {
297 } else if ( strcmp( ent->d_name, ".." ) == 0 ) {
299 } else if ( ent->d_isdir ) {
300 SGPath child( dir_name );
301 child.append( ent->d_name );
302 cnt += count_dir( child.c_str() );
311 static void remove_dir( const char *dir_name, void (*step)(void*,int), void *data ) {
312 ulDir *dir = ulOpenDir( dir_name ) ;
314 while ( ent = ulReadDir( dir ) ) {
315 if ( strcmp( ent->d_name, "." ) == 0 ) {
317 } else if ( strcmp( ent->d_name, ".." ) == 0 ) {
319 } else if ( ent->d_isdir ) {
320 SGPath child( dir_name );
321 child.append( ent->d_name );
322 remove_dir( child.c_str(), step, data );
324 SGPath child( dir_name );
325 child.append( ent->d_name );
326 unlink( child.c_str() );
327 if (step) step( data, 1 );
335 // remove selected files
336 void FGAdminUI::remove_selected() {
339 install_b->deactivate();
340 remove_b->deactivate();
341 quit_b->deactivate();
342 // traverse remove box and recursively remove each item
343 for ( int i = 0; i <= remove_box->nitems(); ++i ) {
344 if ( remove_box->checked( i ) ) {
345 f = remove_box->text( i );
348 float old_max = progress->maximum();
349 progress_label = "Removing ";
351 progress->label( progress_label.c_str() );
352 progress->value( min_progress );
353 main_window->cursor( FL_CURSOR_WAIT );
354 progress->maximum( count_dir( dir.c_str() ) );
355 remove_dir( dir.c_str(), &FGAdminUI::step, this );
356 progress->value( min_progress );
357 main_window->cursor( FL_CURSOR_DEFAULT );
358 progress->label( "" );
359 progress->maximum( old_max );
363 install_b->activate();
364 remove_b->activate();
370 void FGAdminUI::step(void *data)
372 Fl_Progress *p = ((FGAdminUI*)data)->progress;
374 // we don't actually know the total work in advanced due to the
375 // nature of tar archives, and it would be inefficient to prescan
376 // the files, so just cycle the progress bar until we are done.
377 float tmp = p->value() + 1;
378 if ( tmp > max_progress ) {
386 void FGAdminUI::step(void *data, int n)
388 Fl_Progress *p = ((FGAdminUI*)data)->progress;
390 float tmp = p->value() + n;