summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Darmochwal2010-10-07 14:53:20 +0200
committerJan Darmochwal2010-10-07 14:53:20 +0200
commit3932297962d7394574346c016c38ef5103dc5a7c (patch)
tree2a1f8d128d1e87965fea6be41943717b3cddce65
parentrecursive globbing in VSession::readXmlDir() (diff)
downloadvmchooser-3932297962d7394574346c016c38ef5103dc5a7c.tar.gz
vmchooser-3932297962d7394574346c016c38ef5103dc5a7c.tar.xz
vmchooser-3932297962d7394574346c016c38ef5103dc5a7c.zip
Support for command line arguments and conf files
* anyoption has been replaced by getopt * replaces huge unmaintainable .cpp and .h files from an external source by standard POSIX library calls * I would suggest using Boost.Program_options if cross-platform support is needed * support user/global configuration file
-rw-r--r--src/anyoption.cpp1174
-rw-r--r--src/anyoption.h270
-rw-r--r--src/globals.h9
-rw-r--r--src/i18n/de.ts37
-rw-r--r--src/main.cpp300
-rw-r--r--src/save_restore_session.cpp13
-rw-r--r--src/vsession.cpp2
7 files changed, 182 insertions, 1623 deletions
diff --git a/src/anyoption.cpp b/src/anyoption.cpp
deleted file mode 100644
index 9b334a6..0000000
--- a/src/anyoption.cpp
+++ /dev/null
@@ -1,1174 +0,0 @@
-/*
- * AnyOption 1.3
- *
- * kishan at hackorama dot com www.hackorama.com JULY 2001
- *
- * + Acts as a common facade class for reading
- * commandline options as well as options from
- * an optionfile with delimited type value pairs
- *
- * + Handles the POSIX style single character options ( -w )
- * as well as the newer GNU long options ( --width )
- *
- * + The option file assumes the traditional format of
- * first character based comment lines and type value
- * pairs with a delimiter , and flags which are not pairs
- *
- * # this is a coment
- * # next line is an option value pair
- * width : 100
- * # next line is a flag
- * noimages
- *
- * + Supports printing out Help and Usage
- *
- * + Why not just use getopt() ?
- *
- * getopt() Its a POSIX standard not part of ANSI-C.
- * So it may not be available on platforms like Windows.
- *
- * + Why it is so long ?
- *
- * The actual code which does command line parsing
- * and option file parsing are done in few methods.
- * Most of the extra code are for providing a flexible
- * common public interface to both a resourcefile and
- * and command line supporting POSIX style and
- * GNU long option as well as mixing of both.
- *
- * + Please see "anyoption.h" for public method descriptions
- *
- */
-
-/* Updated Auguest 2004
- * Fix from Michael D Peters (mpeters at sandia.gov)
- * to remove static local variables, allowing multiple instantiations
- * of the reader (for using multiple configuration files). There is
- * an error in the destructor when using multiple instances, so you
- * cannot delete your objects (it will crash), but not calling the
- * destructor only introduces a small memory leak, so I
- * have not bothered tracking it down.
- *
- * Also updated to use modern C++ style headers, rather than
- * depricated iostream.h (it was causing my compiler problems)
-*/
-
-/*
- * Updated September 2006
- * Fix from Boyan Asenov for a bug in mixing up option indexes
- * leading to exception when mixing different options types
- */
-
-#include "anyoption.h"
-
-AnyOption::AnyOption()
-{
- init();
-}
-
-AnyOption::AnyOption(int maxopt)
-{
- init( maxopt , maxopt );
-}
-
-AnyOption::AnyOption(int maxopt, int maxcharopt)
-{
- init( maxopt , maxcharopt );
-}
-
-AnyOption::~AnyOption()
-{
- if( mem_allocated )
- cleanup();
-}
-
-void
-AnyOption::init()
-{
- init( DEFAULT_MAXOPTS , DEFAULT_MAXOPTS );
-}
-
-void
-AnyOption::init(int maxopt, int maxcharopt )
-{
-
- max_options = maxopt;
- max_char_options = maxcharopt;
- max_usage_lines = DEFAULT_MAXUSAGE;
- usage_lines = 0 ;
- argc = 0;
- argv = NULL;
- posix_style = true;
- verbose = false;
- filename = NULL;
- appname = NULL;
- option_counter = 0;
- optchar_counter = 0;
- new_argv = NULL;
- new_argc = 0 ;
- max_legal_args = 0 ;
- command_set = false;
- file_set = false;
- values = NULL;
- g_value_counter = 0;
- mem_allocated = false;
- command_set = false;
- file_set = false;
- opt_prefix_char = '-';
- file_delimiter_char = ':';
- file_comment_char = '#';
- equalsign = '=';
- comment = '#' ;
- delimiter = ':' ;
- endofline = '\n';
- whitespace = ' ' ;
- nullterminate = '\0';
- set = false;
- once = true;
- hasoptions = false;
- autousage = false;
-
- strcpy( long_opt_prefix , "--" );
-
- if( alloc() == false ){
- cout << endl << "OPTIONS ERROR : Failed allocating memory" ;
- cout << endl ;
- cout << "Exiting." << endl ;
- exit (0);
- }
-}
-
-bool
-AnyOption::alloc()
-{
- int i = 0 ;
- int size = 0 ;
-
- if( mem_allocated )
- return true;
-
- size = (max_options+1) * sizeof(const char*);
- options = (const char**)malloc( size );
- optiontype = (int*) malloc( (max_options+1)*sizeof(int) );
- optionindex = (int*) malloc( (max_options+1)*sizeof(int) );
- if( options == NULL || optiontype == NULL || optionindex == NULL )
- return false;
- else
- mem_allocated = true;
- for( i = 0 ; i < max_options ; i++ ){
- options[i] = NULL;
- optiontype[i] = 0 ;
- optionindex[i] = -1 ;
- }
- optionchars = (char*) malloc( (max_char_options+1)*sizeof(char) );
- optchartype = (int*) malloc( (max_char_options+1)*sizeof(int) );
- optcharindex = (int*) malloc( (max_char_options+1)*sizeof(int) );
- if( optionchars == NULL ||
- optchartype == NULL ||
- optcharindex == NULL )
- {
- mem_allocated = false;
- return false;
- }
- for( i = 0 ; i < max_char_options ; i++ ){
- optionchars[i] = '0';
- optchartype[i] = 0 ;
- optcharindex[i] = -1 ;
- }
-
- size = (max_usage_lines+1) * sizeof(const char*) ;
- usage = (const char**) malloc( size );
-
- if( usage == NULL ){
- mem_allocated = false;
- return false;
- }
- for( i = 0 ; i < max_usage_lines ; i++ )
- usage[i] = NULL;
-
- return true;
-}
-
-bool
-AnyOption::doubleOptStorage()
-{
- options = (const char**)realloc( options,
- ((2*max_options)+1) * sizeof( const char*) );
- optiontype = (int*) realloc( optiontype ,
- ((2 * max_options)+1)* sizeof(int) );
- optionindex = (int*) realloc( optionindex,
- ((2 * max_options)+1) * sizeof(int) );
- if( options == NULL || optiontype == NULL || optionindex == NULL )
- return false;
- /* init new storage */
- for( int i = max_options ; i < 2*max_options ; i++ ){
- options[i] = NULL;
- optiontype[i] = 0 ;
- optionindex[i] = -1 ;
- }
- max_options = 2 * max_options ;
- return true;
-}
-
-bool
-AnyOption::doubleCharStorage()
-{
- optionchars = (char*) realloc( optionchars,
- ((2*max_char_options)+1)*sizeof(char) );
- optchartype = (int*) realloc( optchartype,
- ((2*max_char_options)+1)*sizeof(int) );
- optcharindex = (int*) realloc( optcharindex,
- ((2*max_char_options)+1)*sizeof(int) );
- if( optionchars == NULL ||
- optchartype == NULL ||
- optcharindex == NULL )
- return false;
- /* init new storage */
- for( int i = max_char_options ; i < 2*max_char_options ; i++ ){
- optionchars[i] = '0';
- optchartype[i] = 0 ;
- optcharindex[i] = -1 ;
- }
- max_char_options = 2 * max_char_options;
- return true;
-}
-
-bool
-AnyOption::doubleUsageStorage()
-{
- usage = (const char**)realloc( usage,
- ((2*max_usage_lines)+1) * sizeof( const char*) );
- if ( usage == NULL )
- return false;
- for( int i = max_usage_lines ; i < 2*max_usage_lines ; i++ )
- usage[i] = NULL;
- max_usage_lines = 2 * max_usage_lines ;
- return true;
-
-}
-
-
-void
-AnyOption::cleanup()
-{
- free (options);
- free (optiontype);
- free (optionindex);
- free (optionchars);
- free (optchartype);
- free (optcharindex);
- free (usage);
- if( values != NULL )
- free (values);
- if( new_argv != NULL )
- free (new_argv);
-}
-
-void
-AnyOption::setCommandPrefixChar( char _prefix )
-{
- opt_prefix_char = _prefix;
-}
-
-void
-AnyOption::setCommandLongPrefix( char *_prefix )
-{
- if( strlen( _prefix ) > MAX_LONG_PREFIX_LENGTH ){
- *( _prefix + MAX_LONG_PREFIX_LENGTH ) = '\0';
- }
-
- strcpy (long_opt_prefix, _prefix);
-}
-
-void
-AnyOption::setFileCommentChar( char _comment )
-{
- file_delimiter_char = _comment;
-}
-
-
-void
-AnyOption::setFileDelimiterChar( char _delimiter )
-{
- file_comment_char = _delimiter ;
-}
-
-bool
-AnyOption::CommandSet()
-{
- return( command_set );
-}
-
-bool
-AnyOption::FileSet()
-{
- return( file_set );
-}
-
-void
-AnyOption::noPOSIX()
-{
- posix_style = false;
-}
-
-bool
-AnyOption::POSIX()
-{
- return posix_style;
-}
-
-
-void
-AnyOption::setVerbose()
-{
- verbose = true ;
-}
-
-void
-AnyOption::printVerbose()
-{
- if( verbose )
- cout << endl ;
-}
-void
-AnyOption::printVerbose( const char *msg )
-{
- if( verbose )
- cout << msg ;
-}
-
-void
-AnyOption::printVerbose( char *msg )
-{
- if( verbose )
- cout << msg ;
-}
-
-void
-AnyOption::printVerbose( char ch )
-{
- if( verbose )
- cout << ch ;
-}
-
-bool
-AnyOption::hasOptions()
-{
- return hasoptions;
-}
-
-void
-AnyOption::autoUsagePrint(bool _autousage)
-{
- autousage = _autousage;
-}
-
-void
-AnyOption::useCommandArgs( int _argc, char **_argv )
-{
- argc = _argc;
- argv = _argv;
- command_set = true;
- appname = argv[0];
- if(argc > 1) hasoptions = true;
-}
-
-void
-AnyOption::useFiileName( const char *_filename )
-{
- filename = _filename;
- file_set = true;
-}
-
-/*
- * set methods for options
- */
-
-void
-AnyOption::setCommandOption( const char *opt )
-{
- addOption( opt , COMMAND_OPT );
- g_value_counter++;
-}
-
-void
-AnyOption::setCommandOption( char opt )
-{
- addOption( opt , COMMAND_OPT );
- g_value_counter++;
-}
-
-void
-AnyOption::setCommandOption( const char *opt , char optchar )
-{
- addOption( opt , COMMAND_OPT );
- addOption( optchar , COMMAND_OPT );
- g_value_counter++;
-}
-
-void
-AnyOption::setCommandFlag( const char *opt )
-{
- addOption( opt , COMMAND_FLAG );
- g_value_counter++;
-}
-
-void
-AnyOption::setCommandFlag( char opt )
-{
- addOption( opt , COMMAND_FLAG );
- g_value_counter++;
-}
-
-void
-AnyOption::setCommandFlag( const char *opt , char optchar )
-{
- addOption( opt , COMMAND_FLAG );
- addOption( optchar , COMMAND_FLAG );
- g_value_counter++;
-}
-
-void
-AnyOption::setFileOption( const char *opt )
-{
- addOption( opt , FILE_OPT );
- g_value_counter++;
-}
-
-void
-AnyOption::setFileOption( char opt )
-{
- addOption( opt , FILE_OPT );
- g_value_counter++;
-}
-
-void
-AnyOption::setFileOption( const char *opt , char optchar )
-{
- addOption( opt , FILE_OPT );
- addOption( optchar, FILE_OPT );
- g_value_counter++;
-}
-
-void
-AnyOption::setFileFlag( const char *opt )
-{
- addOption( opt , FILE_FLAG );
- g_value_counter++;
-}
-
-void
-AnyOption::setFileFlag( char opt )
-{
- addOption( opt , FILE_FLAG );
- g_value_counter++;
-}
-
-void
-AnyOption::setFileFlag( const char *opt , char optchar )
-{
- addOption( opt , FILE_FLAG );
- addOption( optchar , FILE_FLAG );
- g_value_counter++;
-}
-
-void
-AnyOption::setOption( const char *opt )
-{
- addOption( opt , COMMON_OPT );
- g_value_counter++;
-}
-
-void
-AnyOption::setOption( char opt )
-{
- addOption( opt , COMMON_OPT );
- g_value_counter++;
-}
-
-void
-AnyOption::setOption( const char *opt , char optchar )
-{
- addOption( opt , COMMON_OPT );
- addOption( optchar , COMMON_OPT );
- g_value_counter++;
-}
-
-void
-AnyOption::setFlag( const char *opt )
-{
- addOption( opt , COMMON_FLAG );
- g_value_counter++;
-}
-
-void
-AnyOption::setFlag( const char opt )
-{
- addOption( opt , COMMON_FLAG );
- g_value_counter++;
-}
-
-void
-AnyOption::setFlag( const char *opt , char optchar )
-{
- addOption( opt , COMMON_FLAG );
- addOption( optchar , COMMON_FLAG );
- g_value_counter++;
-}
-
-void
-AnyOption::addOption( const char *opt, int type )
-{
- if( option_counter >= max_options ){
- if( doubleOptStorage() == false ){
- addOptionError( opt );
- return;
- }
- }
- options[ option_counter ] = opt ;
- optiontype[ option_counter ] = type ;
- optionindex[ option_counter ] = g_value_counter;
- option_counter++;
-}
-
-void
-AnyOption::addOption( char opt, int type )
-{
- if( !POSIX() ){
- printVerbose("Ignoring the option character \"");
- printVerbose( opt );
- printVerbose( "\" ( POSIX options are turned off )" );
- printVerbose();
- return;
- }
-
-
- if( optchar_counter >= max_char_options ){
- if( doubleCharStorage() == false ){
- addOptionError( opt );
- return;
- }
- }
- optionchars[ optchar_counter ] = opt ;
- optchartype[ optchar_counter ] = type ;
- optcharindex[ optchar_counter ] = g_value_counter;
- optchar_counter++;
-}
-
-void
-AnyOption::addOptionError( const char *opt )
-{
- cout << endl ;
- cout << "OPTIONS ERROR : Failed allocating extra memory " << endl ;
- cout << "While adding the option : \""<< opt << "\"" << endl;
- cout << "Exiting." << endl ;
- cout << endl ;
- exit(0);
-}
-
-void
-AnyOption::addOptionError( char opt )
-{
- cout << endl ;
- cout << "OPTIONS ERROR : Failed allocating extra memory " << endl ;
- cout << "While adding the option: \""<< opt << "\"" << endl;
- cout << "Exiting." << endl ;
- cout << endl ;
- exit(0);
-}
-
-void
-AnyOption::processOptions()
-{
- if( ! valueStoreOK() )
- return;
-}
-
-void
-AnyOption::processCommandArgs(int max_args)
-{
- max_legal_args = max_args;
- processCommandArgs();
-}
-
-void
-AnyOption::processCommandArgs( int _argc, char **_argv, int max_args )
-{
- max_legal_args = max_args;
- processCommandArgs( _argc, _argv );
-}
-
-void
-AnyOption::processCommandArgs( int _argc, char **_argv )
-{
- useCommandArgs( _argc, _argv );
- processCommandArgs();
-}
-
-void
-AnyOption::processCommandArgs()
-{
- if( ! ( valueStoreOK() && CommandSet() ) )
- return;
-
- if( max_legal_args == 0 )
- max_legal_args = argc;
- new_argv = (int*) malloc( (max_legal_args+1) * sizeof(int) );
- for( int i = 1 ; i < argc ; i++ ){/* ignore first argv */
- if( argv[i][0] == long_opt_prefix[0] &&
- argv[i][1] == long_opt_prefix[1] ) { /* long GNU option */
- int match_at = parseGNU( argv[i]+2 ); /* skip -- */
- if( match_at >= 0 && i < argc-1 ) /* found match */
- setValue( options[match_at] , argv[++i] );
- }else if( argv[i][0] == opt_prefix_char ) { /* POSIX char */
- if( POSIX() ){
- char ch = parsePOSIX( argv[i]+1 );/* skip - */
- if( ch != '0' && i < argc-1 ) /* matching char */
- setValue( ch , argv[++i] );
- } else { /* treat it as GNU option with a - */
- int match_at = parseGNU( argv[i]+1 ); /* skip - */
- if( match_at >= 0 && i < argc-1 ) /* found match */
- setValue( options[match_at] , argv[++i] );
- }
- }else { /* not option but an argument keep index */
- if( new_argc < max_legal_args ){
- new_argv[ new_argc ] = i ;
- new_argc++;
- }else{ /* ignore extra arguments */
- printVerbose( "Ignoring extra argument: " );
- printVerbose( argv[i] );
- printVerbose( );
- printAutoUsage();
- }
- printVerbose( "Unknown command argument option : " );
- printVerbose( argv[i] );
- printVerbose( );
- printAutoUsage();
- }
- }
-}
-
-char
-AnyOption::parsePOSIX( char* arg )
-{
-
- for( unsigned int i = 0 ; i < strlen(arg) ; i++ ){
- char ch = arg[i] ;
- if( matchChar(ch) ) { /* keep matching flags till an option */
- /*if last char argv[++i] is the value */
- if( i == strlen(arg)-1 ){
- return ch;
- }else{/* else the rest of arg is the value */
- i++; /* skip any '=' and ' ' */
- while( arg[i] == whitespace
- || arg[i] == equalsign )
- i++;
- setValue( ch , arg+i );
- return '0';
- }
- }
- }
- printVerbose( "Unknown command argument option : " );
- printVerbose( arg );
- printVerbose( );
- printAutoUsage();
- return '0';
-}
-
-int
-AnyOption::parseGNU( char *arg )
-{
- int split_at = 0;
- /* if has a '=' sign get value */
- for( unsigned int i = 0 ; i < strlen(arg) ; i++ ){
- if(arg[i] == equalsign ){
- split_at = i ; /* store index */
- i = strlen(arg); /* get out of loop */
- }
- }
- if( split_at > 0 ){ /* it is an option value pair */
- char* tmp = (char*) malloc( (split_at+1)*sizeof(char) );
- for( int i = 0 ; i < split_at ; i++ )
- tmp[i] = arg[i];
- tmp[split_at] = '\0';
-
- if ( matchOpt( tmp ) >= 0 ){
- setValue( options[matchOpt(tmp)] , arg+split_at+1 );
- free (tmp);
- }else{
- printVerbose( "Unknown command argument option : " );
- printVerbose( arg );
- printVerbose( );
- printAutoUsage();
- free (tmp);
- return -1;
- }
- }else{ /* regular options with no '=' sign */
- return matchOpt(arg);
- }
- return -1;
-}
-
-
-int
-AnyOption::matchOpt( char *opt )
-{
- for( int i = 0 ; i < option_counter ; i++ ){
- if( strcmp( options[i], opt ) == 0 ){
- if( optiontype[i] == COMMON_OPT ||
- optiontype[i] == COMMAND_OPT )
- { /* found option return index */
- return i;
- }else if( optiontype[i] == COMMON_FLAG ||
- optiontype[i] == COMMAND_FLAG )
- { /* found flag, set it */
- setFlagOn( opt );
- return -1;
- }
- }
- }
- printVerbose( "Unknown command argument option : " );
- printVerbose( opt ) ;
- printVerbose( );
- printAutoUsage();
- return -1;
-}
-bool
-AnyOption::matchChar( char c )
-{
- for( int i = 0 ; i < optchar_counter ; i++ ){
- if( optionchars[i] == c ) { /* found match */
- if(optchartype[i] == COMMON_OPT ||
- optchartype[i] == COMMAND_OPT )
- { /* an option store and stop scanning */
- return true;
- }else if( optchartype[i] == COMMON_FLAG ||
- optchartype[i] == COMMAND_FLAG ) { /* a flag store and keep scanning */
- setFlagOn( c );
- return false;
- }
- }
- }
- printVerbose( "Unknown command argument option : " );
- printVerbose( c ) ;
- printVerbose( );
- printAutoUsage();
- return false;
-}
-
-bool
-AnyOption::valueStoreOK( )
-{
- int size= 0;
- if( !set ){
- if( g_value_counter > 0 ){
- size = g_value_counter * sizeof(char*);
- values = (char**)malloc( size );
- for( int i = 0 ; i < g_value_counter ; i++)
- values[i] = NULL;
- set = true;
- }
- }
- return set;
-}
-
-/*
- * public get methods
- */
-char*
-AnyOption::getValue( const char *option )
-{
- if( !valueStoreOK() )
- return NULL;
-
- for( int i = 0 ; i < option_counter ; i++ ){
- if( strcmp( options[i], option ) == 0 )
- return values[ optionindex[i] ];
- }
- return NULL;
-}
-
-bool
-AnyOption::getFlag( const char *option )
-{
- if( !valueStoreOK() )
- return false;
- for( int i = 0 ; i < option_counter ; i++ ){
- if( strcmp( options[i], option ) == 0 )
- return findFlag( values[ optionindex[i] ] );
- }
- return false;
-}
-
-char*
-AnyOption::getValue( char option )
-{
- if( !valueStoreOK() )
- return NULL;
- for( int i = 0 ; i < optchar_counter ; i++ ){
- if( optionchars[i] == option )
- return values[ optcharindex[i] ];
- }
- return NULL;
-}
-
-bool
-AnyOption::getFlag( char option )
-{
- if( !valueStoreOK() )
- return false;
- for( int i = 0 ; i < optchar_counter ; i++ ){
- if( optionchars[i] == option )
- return findFlag( values[ optcharindex[i] ] ) ;
- }
- return false;
-}
-
-bool
-AnyOption::findFlag( char* val )
-{
- if( val == NULL )
- return false;
-
- if( strcmp( TRUE_FLAG , val ) == 0 )
- return true;
-
- return false;
-}
-
-/*
- * private set methods
- */
-bool
-AnyOption::setValue( const char *option , char *value )
-{
- if( !valueStoreOK() )
- return false;
- for( int i = 0 ; i < option_counter ; i++ ){
- if( strcmp( options[i], option ) == 0 ){
- values[ optionindex[i] ] = (char*) malloc((strlen(value)+1)*sizeof(char));
- strcpy( values[ optionindex[i] ], value );
- return true;
- }
- }
- return false;
-}
-
-bool
-AnyOption::setFlagOn( const char *option )
-{
- if( !valueStoreOK() )
- return false;
- for( int i = 0 ; i < option_counter ; i++ ){
- if( strcmp( options[i], option ) == 0 ){
- values[ optionindex[i] ] = (char*) malloc((strlen(TRUE_FLAG)+1)*sizeof(char));
- strcpy( values[ optionindex[i] ] , TRUE_FLAG );
- return true;
- }
- }
- return false;
-}
-
-bool
-AnyOption::setValue( char option , char *value )
-{
- if( !valueStoreOK() )
- return false;
- for( int i = 0 ; i < optchar_counter ; i++ ){
- if( optionchars[i] == option ){
- values[ optcharindex[i] ] = (char*) malloc((strlen(value)+1)*sizeof(char));
- strcpy( values[ optcharindex[i] ], value );
- return true;
- }
- }
- return false;
-}
-
-bool
-AnyOption::setFlagOn( char option )
-{
- if( !valueStoreOK() )
- return false;
- for( int i = 0 ; i < optchar_counter ; i++ ){
- if( optionchars[i] == option ){
- values[ optcharindex[i] ] = (char*) malloc((strlen(TRUE_FLAG)+1)*sizeof(char));
- strcpy( values[ optcharindex[i] ] , TRUE_FLAG );
- return true;
- }
- }
- return false;
-}
-
-
-int
-AnyOption::getArgc( )
-{
- return new_argc;
-}
-
-char*
-AnyOption::getArgv( int index )
-{
- if( index < new_argc ){
- return ( argv[ new_argv[ index ] ] );
- }
- return NULL;
-}
-
-/* dotfile sub routines */
-
-bool
-AnyOption::processFile()
-{
- if( ! (valueStoreOK() && FileSet()) )
- return false;
- return ( consumeFile(readFile()) );
-}
-
-bool
-AnyOption::processFile( const char *filename )
-{
- useFiileName(filename );
- return ( processFile() );
-}
-
-char*
-AnyOption::readFile()
-{
- return ( readFile(filename) );
-}
-
-/*
- * read the file contents to a character buffer
- */
-
-char*
-AnyOption::readFile( const char* fname )
-{
- int length;
- char *buffer;
- ifstream is;
- is.open ( fname , ifstream::in );
- if( ! is.good() ){
- is.close();
- return NULL;
- }
- is.seekg (0, ios::end);
- length = is.tellg();
- is.seekg (0, ios::beg);
- buffer = (char*) malloc(length*sizeof(char));
- is.read (buffer,length);
- is.close();
- return buffer;
-}
-
-/*
- * scans a char* buffer for lines that does not
- * start with the specified comment character.
- */
-bool
-AnyOption::consumeFile( char *buffer )
-{
-
- if( buffer == NULL )
- return false;
-
- char *cursor = buffer;/* preserve the ptr */
- char *pline = NULL ;
- int linelength = 0;
- bool newline = true;
- for( unsigned int i = 0 ; i < strlen( buffer ) ; i++ ){
- if( *cursor == endofline ) { /* end of line */
- if( pline != NULL ) /* valid line */
- processLine( pline, linelength );
- pline = NULL;
- newline = true;
- }else if( newline ){ /* start of line */
- newline = false;
- if( (*cursor != comment ) ){ /* not a comment */
- pline = cursor ;
- linelength = 0 ;
- }
- }
- cursor++; /* keep moving */
- linelength++;
- }
- free (buffer);
- return true;
-}
-
-
-/*
- * find a valid type value pair separated by a delimiter
- * character and pass it to valuePairs()
- * any line which is not valid will be considered a value
- * and will get passed on to justValue()
- *
- * assuming delimiter is ':' the behaviour will be,
- *
- * width:10 - valid pair valuePairs( width, 10 );
- * width : 10 - valid pair valuepairs( width, 10 );
- *
- * :::: - not valid
- * width - not valid
- * :10 - not valid
- * width: - not valid
- * :: - not valid
- * : - not valid
- *
- */
-
-void
-AnyOption::processLine( char *theline, int length )
-{
- bool found = false;
- char *pline = (char*) malloc( (length+1)*sizeof(char) );
- for( int i = 0 ; i < length ; i ++ )
- pline[i]= *(theline++);
- pline[length] = nullterminate;
- char *cursor = pline ; /* preserve the ptr */
- if( *cursor == delimiter || *(cursor+length-1) == delimiter ){
- justValue( pline );/* line with start/end delimiter */
- }else{
- for( int i = 1 ; i < length-1 && !found ; i++){/* delimiter */
- if( *cursor == delimiter ){
- *(cursor-1) = nullterminate; /* two strings */
- found = true;
- valuePairs( pline , cursor+1 );
- }
- cursor++;
- }
- cursor++;
- if( !found ) /* not a pair */
- justValue( pline );
- }
- free (pline);
-}
-
-/*
- * removes trailing and preceeding whitespaces from a string
- */
-char*
-AnyOption::chomp( char *str )
-{
- while( *str == whitespace )
- str++;
- char *end = str+strlen(str)-1;
- while( *end == whitespace )
- end--;
- *(end+1) = nullterminate;
- return str;
-}
-
-void
-AnyOption::valuePairs( char *type, char *value )
-{
- if ( strlen(chomp(type)) == 1 ){ /* this is a char option */
- for( int i = 0 ; i < optchar_counter ; i++ ){
- if( optionchars[i] == type[0] ){ /* match */
- if( optchartype[i] == COMMON_OPT ||
- optchartype[i] == FILE_OPT )
- {
- setValue( type[0] , chomp(value) );
- return;
- }
- }
- }
- }
- /* if no char options matched */
- for( int i = 0 ; i < option_counter ; i++ ){
- if( strcmp( options[i], type ) == 0 ){ /* match */
- if( optiontype[i] == COMMON_OPT ||
- optiontype[i] == FILE_OPT )
- {
- setValue( type , chomp(value) );
- return;
- }
- }
- }
- printVerbose( "Unknown option in resourcefile : " );
- printVerbose( type );
- printVerbose( );
-}
-
-void
-AnyOption::justValue( char *type )
-{
-
- if ( strlen(chomp(type)) == 1 ){ /* this is a char option */
- for( int i = 0 ; i < optchar_counter ; i++ ){
- if( optionchars[i] == type[0] ){ /* match */
- if( optchartype[i] == COMMON_FLAG ||
- optchartype[i] == FILE_FLAG )
- {
- setFlagOn( type[0] );
- return;
- }
- }
- }
- }
- /* if no char options matched */
- for( int i = 0 ; i < option_counter ; i++ ){
- if( strcmp( options[i], type ) == 0 ){ /* match */
- if( optiontype[i] == COMMON_FLAG ||
- optiontype[i] == FILE_FLAG )
- {
- setFlagOn( type );
- return;
- }
- }
- }
- printVerbose( "Unknown option in resourcefile : " );
- printVerbose( type );
- printVerbose( );
-}
-
-/*
- * usage and help
- */
-
-
-void
-AnyOption::printAutoUsage()
-{
- if( autousage ) printUsage();
-}
-
-void
-AnyOption::printUsage()
-{
-
- if( once ) {
- once = false ;
- cout << endl ;
- for( int i = 0 ; i < usage_lines ; i++ )
- cout << usage[i] << endl ;
- cout << endl ;
- }
-}
-
-
-void
-AnyOption::addUsage( const char *line )
-{
- if( usage_lines >= max_usage_lines ){
- if( doubleUsageStorage() == false ){
- addUsageError( line );
- exit(1);
- }
- }
- usage[ usage_lines ] = line ;
- usage_lines++;
-}
-
-void
-AnyOption::addUsageError( const char *line )
-{
- cout << endl ;
- cout << "OPTIONS ERROR : Failed allocating extra memory " << endl ;
- cout << "While adding the usage/help : \""<< line << "\"" << endl;
- cout << "Exiting." << endl ;
- cout << endl ;
- exit(0);
-
-}
diff --git a/src/anyoption.h b/src/anyoption.h
deleted file mode 100644
index 13a4a18..0000000
--- a/src/anyoption.h
+++ /dev/null
@@ -1,270 +0,0 @@
-#ifndef _ANYOPTION_H
-#define _ANYOPTION_H
-
-#include <iostream>
-#include <fstream>
-#include <stdlib.h>
-#include <cstring>
-
-#define COMMON_OPT 1
-#define COMMAND_OPT 2
-#define FILE_OPT 3
-#define COMMON_FLAG 4
-#define COMMAND_FLAG 5
-#define FILE_FLAG 6
-
-#define COMMAND_OPTION_TYPE 1
-#define COMMAND_FLAG_TYPE 2
-#define FILE_OPTION_TYPE 3
-#define FILE_FLAG_TYPE 4
-#define UNKNOWN_TYPE 5
-
-#define DEFAULT_MAXOPTS 10
-#define MAX_LONG_PREFIX_LENGTH 2
-
-#define DEFAULT_MAXUSAGE 3
-#define DEFAULT_MAXHELP 10
-
-#define TRUE_FLAG "true"
-
-using namespace std;
-
-class AnyOption
-{
-
-public: /* the public interface */
- AnyOption();
- AnyOption(int maxoptions );
- AnyOption(int maxoptions , int maxcharoptions);
- ~AnyOption();
-
- /*
- * following set methods specifies the
- * special characters and delimiters
- * if not set traditional defaults will be used
- */
-
- void setCommandPrefixChar( char _prefix ); /* '-' in "-w" */
- void setCommandLongPrefix( char *_prefix ); /* '--' in "--width" */
- void setFileCommentChar( char _comment ); /* '#' in shellscripts */
- void setFileDelimiterChar( char _delimiter );/* ':' in "width : 100" */
-
- /*
- * provide the input for the options
- * like argv[] for commndline and the
- * option file name to use;
- */
-
- void useCommandArgs( int _argc, char **_argv );
- void useFiileName( const char *_filename );
-
- /*
- * turn off the POSIX style options
- * this means anything starting with a '-' or "--"
- * will be considered a valid option
- * which alo means you cannot add a bunch of
- * POIX options chars together like "-lr" for "-l -r"
- *
- */
-
- void noPOSIX();
-
- /*
- * prints warning verbose if you set anything wrong
- */
- void setVerbose();
-
-
- /*
- * there are two types of options
- *
- * Option - has an associated value ( -w 100 )
- * Flag - no value, just a boolean flag ( -nogui )
- *
- * the options can be either a string ( GNU style )
- * or a character ( traditional POSIX style )
- * or both ( --width, -w )
- *
- * the options can be common to the commandline and
- * the optionfile, or can belong only to either of
- * commandline and optionfile
- *
- * following set methods, handle all the aboove
- * cases of options.
- */
-
- /* options comman to command line and option file */
- void setOption( const char *opt_string );
- void setOption( char opt_char );
- void setOption( const char *opt_string , char opt_char );
- void setFlag( const char *opt_string );
- void setFlag( char opt_char );
- void setFlag( const char *opt_string , char opt_char );
-
- /* options read from commandline only */
- void setCommandOption( const char *opt_string );
- void setCommandOption( char opt_char );
- void setCommandOption( const char *opt_string , char opt_char );
- void setCommandFlag( const char *opt_string );
- void setCommandFlag( char opt_char );
- void setCommandFlag( const char *opt_string , char opt_char );
-
- /* options read from an option file only */
- void setFileOption( const char *opt_string );
- void setFileOption( char opt_char );
- void setFileOption( const char *opt_string , char opt_char );
- void setFileFlag( const char *opt_string );
- void setFileFlag( char opt_char );
- void setFileFlag( const char *opt_string , char opt_char );
-
- /*
- * process the options, registerd using
- * useCommandArgs() and useFileName();
- */
- void processOptions();
- void processCommandArgs();
- void processCommandArgs( int max_args );
- bool processFile();
-
- /*
- * process the specified options
- */
- void processCommandArgs( int _argc, char **_argv );
- void processCommandArgs( int _argc, char **_argv, int max_args );
- bool processFile( const char *_filename );
-
- /*
- * get the value of the options
- * will return NULL if no value is set
- */
- char *getValue( const char *_option );
- bool getFlag( const char *_option );
- char *getValue( char _optchar );
- bool getFlag( char _optchar );
-
- /*
- * Print Usage
- */
- void printUsage();
- void printAutoUsage();
- void addUsage( const char *line );
- void printHelp();
- /* print auto usage printing for unknown options or flag */
- void autoUsagePrint(bool flag);
-
- /*
- * get the argument count and arguments sans the options
- */
- int getArgc();
- char* getArgv( int index );
- bool hasOptions();
-
-private: /* the hidden data structure */
- int argc; /* commandline arg count */
- char **argv; /* commndline args */
- const char* filename; /* the option file */
- char* appname; /* the application name from argv[0] */
-
- int *new_argv; /* arguments sans options (index to argv) */
- int new_argc; /* argument count sans the options */
- int max_legal_args; /* ignore extra arguments */
-
-
- /* option strings storage + indexing */
- int max_options; /* maximum number of options */
- const char **options; /* storage */
- int *optiontype; /* type - common, command, file */
- int *optionindex; /* index into value storage */
- int option_counter; /* counter for added options */
-
- /* option chars storage + indexing */
- int max_char_options; /* maximum number options */
- char *optionchars; /* storage */
- int *optchartype; /* type - common, command, file */
- int *optcharindex; /* index into value storage */
- int optchar_counter; /* counter for added options */
-
- /* values */
- char **values; /* common value storage */
- int g_value_counter; /* globally updated value index LAME! */
-
- /* help and usage */
- const char **usage; /* usage */
- int max_usage_lines; /* max usage lines reseverd */
- int usage_lines; /* number of usage lines */
-
- bool command_set; /* if argc/argv were provided */
- bool file_set; /* if a filename was provided */
- bool mem_allocated; /* if memory allocated in init() */
- bool posix_style; /* enables to turn off POSIX style options */
- bool verbose; /* silent|verbose */
- bool print_usage; /* usage verbose */
- bool print_help; /* help verbose */
-
- char opt_prefix_char; /* '-' in "-w" */
- char long_opt_prefix[MAX_LONG_PREFIX_LENGTH]; /* '--' in "--width" */
- char file_delimiter_char; /* ':' in width : 100 */
- char file_comment_char; /* '#' in "#this is a comment" */
- char equalsign;
- char comment;
- char delimiter;
- char endofline;
- char whitespace;
- char nullterminate;
-
- bool set; //was static member
- bool once; //was static member
-
- bool hasoptions;
- bool autousage;
-
-private: /* the hidden utils */
- void init();
- void init(int maxopt, int maxcharopt );
- bool alloc();
- void cleanup();
- bool valueStoreOK();
-
- /* grow storage arrays as required */
- bool doubleOptStorage();
- bool doubleCharStorage();
- bool doubleUsageStorage();
-
- bool setValue( const char *option , char *value );
- bool setFlagOn( const char *option );
- bool setValue( char optchar , char *value);
- bool setFlagOn( char optchar );
-
- void addOption( const char* option , int type );
- void addOption( char optchar , int type );
- void addOptionError( const char *opt);
- void addOptionError( char opt);
- bool findFlag( char* value );
- void addUsageError( const char *line );
- bool CommandSet();
- bool FileSet();
- bool POSIX();
-
- char parsePOSIX( char* arg );
- int parseGNU( char *arg );
- bool matchChar( char c );
- int matchOpt( char *opt );
-
- /* dot file methods */
- char *readFile();
- char *readFile( const char* fname );
- bool consumeFile( char *buffer );
- void processLine( char *theline, int length );
- char *chomp( char *str );
- void valuePairs( char *type, char *value );
- void justValue( char *value );
-
- void printVerbose( const char *msg );
- void printVerbose( char *msg );
- void printVerbose( char ch );
- void printVerbose( );
-
-
-};
-
-#endif /* ! _ANYOPTION_H */
diff --git a/src/globals.h b/src/globals.h
index 96f95e4..b616741 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -10,6 +10,15 @@
#define VMCHOOSER_BIN_PATH "/home/zwerg/vmchooser/bin/"
#define VMCHOOSER_ETC_BASE_PATH "/home/zwerg/vmchooser/etc/"
#define VMCHOOSER_VMPATH "/home/zwerg/vmchooser/vm/"
+#define VMCHOOSER_USER_PATH ".openslx"
+
+#define VMCHOOSER_X_SESSIONS_PATH "/usr/share/xsessions/"
+
+#define VMCHOOSER_DEFAULT_WIDTH 500
+#define VMCHOOSER_DEFAULT_HEIGHT 580
+
+#define VMCHOOSER_CONF_FILE "vmchooser.conf"
+#define VMCHOOSER_PREVIOUS_SESSION_FILE "vmchooser.conf"
class QString;
extern const QString filterscript;
diff --git a/src/i18n/de.ts b/src/i18n/de.ts
index b8063d6..6887b07 100644
--- a/src/i18n/de.ts
+++ b/src/i18n/de.ts
@@ -2,6 +2,39 @@
<!DOCTYPE TS>
<TS version="2.0" language="de">
<context>
+ <name>Console</name>
+ <message>
+ <location filename="../main.cpp" line="31"/>
+ <source>Usage: vmchooser [ OPTIONS | FILE ]
+
+ -d, --default name of default session
+ -p, --path path to vmware .xml files
+ -x, --xpath path of X Session .desktop files
+ -s, --size window size &lt;width&gt;x&lt;height&gt;
+ -v, --version print version and exit
+ -h, --help print usage information and exit
+
+FILE can be a vmware .xml or an X .desktop file
+</source>
+ <translation>Aufruf: vmchooser [ OPTIONEN | DATEI ]
+
+ -d, --default Name der Standardsitzung
+ -p, --path Pfad zu .xml-Dateien für virtuelle Sitzungen
+ -x, --xpath Pfad zu .desktop-Dateien für X Sitzungen
+ -s, --size Fenstergröße &lt;Breite&gt;x&lt;Höhe&gt;
+ -v, --version Version anzeigen und beenden
+ -h, --help Hilfetext anzeigen und beenden
+
+DATEI kann eine .xml- oder .desktop-Datei sein
+</translation>
+ </message>
+ <message>
+ <location filename="../main.cpp" line="136"/>
+ <source>invlid size argument</source>
+ <translation>ungültige Fenstergröße</translation>
+ </message>
+</context>
+<context>
<name>Dialog</name>
<message>
<location filename="../ui/dialog.ui" line="14"/>
@@ -59,12 +92,12 @@
<translation>Vollzugriff</translation>
</message>
<message>
- <location filename="../main.cpp" line="196"/>
+ <location filename="../main.cpp" line="150"/>
<source>X Sessions</source>
<translation>X Sitzungen</translation>
</message>
<message>
- <location filename="../main.cpp" line="197"/>
+ <location filename="../main.cpp" line="153"/>
<source>Virtual Sessions</source>
<translation>Virtuelle Sitzungen</translation>
</message>
diff --git a/src/main.cpp b/src/main.cpp
index 87bb84f..90a9fbc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,18 +4,19 @@
#include <iostream>
#include <stdlib.h>
#include <QTranslator>
+#include <QMap>
+#include <QString>
+#include <QSettings>
#include "save_restore_session.h"
#include "xsession.h"
#include "vsession.h"
-#include "anyoption.h"
#include "globals.h"
+#include "command_line_options.h"
#include <boost/filesystem.hpp>
namespace bfs=boost::filesystem;
-using namespace std;
-
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
@@ -23,176 +24,135 @@ int main(int argc, char *argv[]) {
translator.load(":" + QLocale::system().name());
a.installTranslator(&translator);
- string version = "0.0.13";
- AnyOption* opt = new AnyOption();
- char* xmlpath = NULL;
- char* lsesspath = NULL;
- char* dsession = NULL;
- int width=0, height=0;
-
- //opt->setVerbose();
- opt->autoUsagePrint(false);
-
- opt->addUsage("");
- opt->addUsage("SessionChooser Usage: vmchooser [OPTS|image.xml]");
- opt->addUsage("\t{-d |--default=} name of session to select (part of)");
- opt->addUsage("\t{-p |--path=} path to vmware (.xml) files");
- opt->addUsage("\t{-l |--lpath=} path to linux session (.desktop) files");
- opt->addUsage("\t{-s |--size=} [widthxheight]");
- opt->addUsage("\t{-v |--version} print out version");
- opt->addUsage("\t{-h |--help} prints help");
- opt->addUsage("");
- opt->addUsage("Run with xml-file as additional argument to start image at once.");
-
- opt->setFlag("help",'h');
- opt->setFlag("version",'v');
- opt->setOption("default", 'd');
- opt->setOption("path", 'p');
- opt->setOption("lpath", 'l');
- opt->setOption("size",'s');
-
- opt->processCommandArgs(argc, argv);
-
- /** HELP */
- if(opt->getFlag("help") || opt->getFlag('h')) {
- opt->printUsage();
- return 0;
- }
-
- /**
- * XML - PATH
- *
- * 1. read from stage3.conf
- * 2. option -p
- * 3. option --path
- * 4. default value "/var/lib/virt/vmware/"
- *
- **/
-
- ifstream ifs (
- string(VMCHOOSER_ETC_BASE_PATH).append("vmchooser.conf").c_str(),
- ifstream::in
- );
- if(ifs) {
- int n = 255;
- char buf[n];
- string s = "";
- while(!ifs.eof()) {
- ifs.getline(buf, n);
- s = buf;
- if(s.substr(0,17) == "vmchooser_xmlpath") {
- xmlpath = (char*)strdup(s.substr(19,s.length()-20).append("/").c_str());
- }
- }
-
- }
-
- if(opt->getValue('d')!=NULL) {
- dsession = opt->getValue('d');
- }
-
- if(opt->getValue("default")!= NULL) {
- dsession = opt->getValue("default");
- }
-
- if(opt->getValue('p')!=NULL) {
- xmlpath = opt->getValue('p');
- }
-
- if(opt->getValue("path")!= NULL) {
- xmlpath = opt->getValue("path");
- }
-
- if (xmlpath == NULL) {
- // Default Path comes here
- xmlpath = (char *) VMCHOOSER_VMPATH;
- }
-
- /* VERSION */
- if(opt->getFlag('v') || opt->getFlag("version")) {
- // just print out version information - helps testing
- cout << "virtual machine chooser " << version << endl;
- delete opt;
- return 0;
-
- }
-
- /** LINUX SESSION PATH */
- if(opt->getValue('l')!=NULL) {
- lsesspath = opt->getValue('l');
- }
- if(opt->getValue("lpath")!= NULL) {
- lsesspath = opt->getValue("lpath");
- }
- if (lsesspath == NULL) {
- // TODO: absolute paths as constants
- lsesspath = (char *) "/usr/share/xsessions/";
- }
-
- /** Size of Window */
- string size;
- unsigned int i;
-
- if(opt->getValue('s')!=NULL) {
- size = opt->getValue('s');
- }
- if(opt->getValue("size")!= NULL) {
- size = opt->getValue("size");
- }
-
- if (size.empty()) {
- width = 500;
- height = 550;
- }
- else {
- i = size.find_first_of("x");
- if (i == string::npos) {
- cerr << "Please write <width>x<height> as argument for -s|--size." << endl;
- return 1;
- }
- height = atoi(size.substr(i+1).c_str());
- width = atoi(size.substr(0, size.size()-i).c_str());
- }
-
-
- // additional xml argument -> start image directly
-#if 0
- if(opt->getArgc() > 0) {
- string single_arg = opt->getArgv(0);
- if(bfs::is_directory(single_arg)) {
- fprintf(stderr, "Only argument is a folder, should be a valid xml file!\n");
- return 1;
- }
- // read xml image
- xmlDoc* doc = xmlReadFile(single_arg.c_str(), NULL, XML_PARSE_RECOVER);
- if (doc == NULL) {
- fprintf(stderr, "Error: could not parse file %s\n", single_arg.c_str());
- return 1;
- }
-
- DataEntry* result = get_entry(doc);
- if(result) {
- runImage(*result, single_arg );
- }
- else {
- fprintf(stderr, "Error: can not start image from xml\n\tcheck your <active> setting!\n");
- return 1;
- }
- }
-#endif
-
- delete opt;
-
- /* read xml files */
- QList<Session*> xsessions(XSession::readSessions(lsesspath));
- QList<Session*> vsessions(VSession::readXmlDir(xmlpath));
+ std::string version = "0.0.13";
+
+ CommandLineOptions cmdOptions(argc, argv);
+
+ std::string usage(a.translate(
+ "Console",
+ "Usage: vmchooser [ OPTIONS | FILE ]\n\n"
+ " -d, --default name of default session\n"
+ " -p, --path path to vmware .xml files\n"
+ " -x, --xpath path of X Session .desktop files\n"
+ " -s, --size window size <width>x<height>\n"
+ " -v, --version print version and exit\n"
+ " -h, --help print usage information and exit\n"
+ "\nFILE can be a vmware .xml or an X .desktop file\n"
+ ).toUtf8().data());
+
+ if (cmdOptions.contains("error")) {
+ std::cerr << usage;
+ return 1;
+ }
+
+ if (cmdOptions.contains("usage")) {
+ std::cout << usage;
+ return 0;
+ }
+
+ if (cmdOptions.contains("version")) {
+ std::cout << version;
+ return 0;
+ }
+
+ if (cmdOptions.contains("file")) {
+ QString file(cmdOptions.value("file"));
+
+ if (file.endsWith(".desktop")) {
+ XSession s;
+ return s.init(file) && s.run();
+ } else if (file.endsWith(".xml")) {
+ // our XML-files can contain multiple sessions
+ // let's just take the first one
+ Session* s(VSession::readXmlFile(file).value(0));
+ return s && s->run();
+ } else {
+ std::cerr << "not a valid session file" << std::endl;
+ return 1;
+ }
+ }
+
+ // read configuration file:
+ // file supplied as command line option or
+ // user vmchooser.conf or
+ // globel vmchooser.conf
+ QString confFile;
+ QString userConfFile(QDir::homePath() + "/" +
+ VMCHOOSER_USER_PATH + "/" +
+ VMCHOOSER_CONF_FILE);
+ QString globalConfFile(QString(VMCHOOSER_ETC_BASE_PATH) + "/" +
+ VMCHOOSER_CONF_FILE);
+ if (cmdOptions.contains("config")) {
+ confFile = cmdOptions.value("config");
+ } else if (QFileInfo(userConfFile).exists()) {
+ confFile = userConfFile;
+ } else {
+ confFile = globalConfFile;
+ }
+ QSettings settings(confFile, QSettings::IniFormat);
+ settings.setIniCodec("UTF-8");
+
+ QString defaultSession;
+ if (cmdOptions.contains("default")) {
+ defaultSession = cmdOptions.value("default");
+ } else if (settings.contains("default")) {
+ defaultSession = settings.value("default").toString();
+ } else {
+ defaultSession = readSessionName();
+ }
+
+ QString vSessionXmlPath;
+ if (cmdOptions.contains("path")) {
+ vSessionXmlPath = cmdOptions.value("path");
+ } else if (settings.contains("path")) {
+ vSessionXmlPath = settings.value("path").toString();
+ } else {
+ vSessionXmlPath = VMCHOOSER_VMPATH;
+ }
+
+ QString xSessionPath;
+ if (cmdOptions.contains("xpath")) {
+ xSessionPath = cmdOptions.value("xpath");
+ } else if (settings.contains("xpath")) {
+ xSessionPath = settings.value("xpath").toString();
+ } else {
+ xSessionPath = VMCHOOSER_X_SESSIONS_PATH;
+ }
+
+ QString size;
+ if (cmdOptions.contains("size")) {
+ size = cmdOptions.value("size");
+ } else if (settings.contains("size")) {
+ size = settings.value("size").toString();
+ }
+
+ int width, height;
+ QRegExp rx("^(\\d+)x(\\d+)$");
+ if (rx.indexIn(size) != -1) {
+ QStringList list = rx.capturedTexts();
+ width = list.value(1).toInt();
+ height = list.value(2).toInt();
+ } else if (!size.isEmpty()) {
+ std::cerr << a.translate("Console", "invlid size argument").toUtf8().data() << std::endl;
+ exit(1);
+ } else {
+ width = VMCHOOSER_DEFAULT_WIDTH;
+ height = VMCHOOSER_DEFAULT_HEIGHT;
+ }
+
+ /* read session files */
+ QList<Session*> xsessions(XSession::readSessions(xSessionPath));
+ QList<Session*> vsessions(VSession::readXmlDir(vSessionXmlPath));
Dialog w;
w.resize(width, height);
-
- w.addItems(xsessions, a.translate("Dialog", "X Sessions"));
- w.addItems(vsessions, a.translate("Dialog", "Virtual Sessions"));
- w.selectPreviousSession();
+ if (xsessions.size()) {
+ w.addItems(xsessions, a.translate("Dialog", "X Sessions"));
+ }
+ if (vsessions.size()) {
+ w.addItems(vsessions, a.translate("Dialog", "Virtual Sessions"));
+ }
+ w.selectSession(defaultSession);
w.show();
return a.exec();
}
diff --git a/src/save_restore_session.cpp b/src/save_restore_session.cpp
index ec292a2..3b0e8ab 100644
--- a/src/save_restore_session.cpp
+++ b/src/save_restore_session.cpp
@@ -1,11 +1,12 @@
+#include "save_restore_session.h"
#include <QDir>
#include <QIODevice>
#include <QString>
+#include "globals.h"
void writeSessionName(QString name) {
- // TODO: use constants
- QDir saveFileDir(QDir::homePath() + "/.openslx");
- QString saveFileName("vmchooser_prev_session");
+ QDir saveFileDir(QDir::homePath() + "/" + VMCHOOSER_USER_PATH);
+ QString saveFileName(VMCHOOSER_PREVIOUS_SESSION_FILE);
if (! saveFileDir.exists()) {
if (! saveFileDir.mkpath(saveFileDir.path())) {
@@ -22,10 +23,10 @@ void writeSessionName(QString name) {
}
QString readSessionName() {
- QString saveFileDir(QDir::homePath() + "/.openslx");
- QString saveFileName("vmchooser_prev_session");
+ QDir saveFileDir(QDir::homePath() + "/" + VMCHOOSER_USER_PATH);
+ QString saveFileName(VMCHOOSER_PREVIOUS_SESSION_FILE);
- QFile saveFile(saveFileDir + "/" + saveFileName);
+ QFile saveFile(saveFileDir.path() + "/" + saveFileName);
if (saveFile.open(QIODevice::ReadOnly)) {
return QString(saveFile.readAll());
diff --git a/src/vsession.cpp b/src/vsession.cpp
index 249594a..f3d2604 100644
--- a/src/vsession.cpp
+++ b/src/vsession.cpp
@@ -186,7 +186,6 @@ bool VSession::run() const {
// TODO: save session
return true;
}
-
return false;
}
@@ -212,6 +211,7 @@ bool VSession::run() const {
tmpfile.setAutoRemove(false);
// TODO: put script name in constant
+ // TODO: check if run-virt.sh exists
QString runVmScript(QString(VMCHOOSER_BIN_PATH) + "run-virt.sh");
if (QProcess::startDetached(runVmScript, QStringList(tmpfile.fileName()))) {
// TODO: save session