--- main.cxx.orig 2009-03-01 04:00:35.000000000 +0700 +++ main.cxx 2009-08-09 05:12:41.526564463 +0800 @@ -25,6 +25,8 @@ using namespace ePDFView; +static gboolean displayVersion = FALSE; + struct LoadFileInfo { gchar *fileName; @@ -32,6 +34,13 @@ PDFDocument *document; }; +static GOptionEntry optEntries[] = +{ + { "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &displayVersion, + N_("Print version information and exit"), 0 }, + { 0 } +}; + static int loadFileFromCommandLine (gpointer data) { @@ -57,19 +66,28 @@ // Create the command line options context. GOptionContext *optionContext = g_option_context_new (_("[FILE] - view PDF documents")); + g_option_context_add_main_entries (optionContext, optEntries, PACKAGE); + g_option_context_set_help_enabled (optionContext, TRUE); g_option_context_add_group (optionContext, gtk_get_option_group (TRUE)); GError *error = NULL; if ( !g_option_context_parse (optionContext, &argc, &argv, &error) ) { g_critical ("Error parsing command line options: %s\n", error->message); + // g_option_group_set_error_hook () seems to be a more elegant solution + g_print ("Usage: [-h, --help]/[-V, --version] \n"); g_error_free (error); exit (EXIT_FAILURE); } + if ( displayVersion ) + { + g_print ("ePDFView %s\n", VERSION); + return EXIT_SUCCESS; + } + // Initialise the working thread. IJob::init (); // Initialise the GTK library. gtk_init (&argc, &argv); - g_set_application_name (_("PDF Viewer")); // Create the main presenter. PDFDocument *document = new PDFDocument; MainPter *mainPter = new MainPter (document); @@ -78,14 +96,28 @@ // Let know to the presenter which is its view. mainPter->setView (mainView); // Now check if we have additional parameters. Any additional parameter - // will be a file name to open. + // can be only a file name to open, because all other options that could be + // parsed were removed from argv[] by g_option_context_parse (). if ( argc > 1 ) { + gchar * errmsg = g_strdup ("testpattern"); LoadFileInfo *info = new LoadFileInfo; info->mainPter = mainPter; info->document = document; info->fileName = g_strdup (argv[1]); - g_idle_add (loadFileFromCommandLine, info); + // Open file only if it exist and is a regular file but not a directory. + if ( !g_file_test (info->fileName, G_FILE_TEST_EXISTS ) + && g_ascii_strcasecmp (info->fileName, "-") ) + g_strlcpy (errmsg, "it does not exist", 60); + else if ( g_file_test (info->fileName, G_FILE_TEST_IS_DIR )) + g_strlcpy (errmsg, "it is a directory", 60); + if ( g_ascii_strcasecmp (errmsg, "testpattern") ) + { + g_critical ("Error opening file \"%s\": %s. Exiting...\n", + info->fileName, errmsg); + return EXIT_FAILURE; + } + g_idle_add (loadFileFromCommandLine, info); } gtk_main(); @@ -99,6 +131,6 @@ g_option_context_free (optionContext); - // All done!. + // All done! return EXIT_SUCCESS; }