From 07f560d51ec11b671a7b6b7e2634dc0ca7d8e73d Mon Sep 17 00:00:00 2001 From: pabs Date: Thu, 28 Feb 2008 08:11:50 +0000 Subject: [PATCH] Improve the browser launch functionality: * Respect user preferences (use "xdg-open" on Linux, "open" on MacOS) * Try a list of browsers instead of just one * Show a message dialog if none of the browsers could be loaded git-svn-id: http://svn.voria.com/code@1820 1f10aa63-cdf2-0310-b900-c93c546f37ac --- synfig-studio/trunk/configure.ac | 1 + synfig-studio/trunk/src/gtkmm/app.cpp | 99 ++++++++++++++++++++++++++++------- 2 files changed, 81 insertions(+), 19 deletions(-) diff --git a/synfig-studio/trunk/configure.ac b/synfig-studio/trunk/configure.ac index d29ee5f..947f10b 100755 --- a/synfig-studio/trunk/configure.ac +++ b/synfig-studio/trunk/configure.ac @@ -79,6 +79,7 @@ case "$host" in *darwin*) AM_CONDITIONAL(WIN32_PKG, false) AM_CONDITIONAL(MACOSX_PKG, true) + AC_DEFINE(USE_OPEN_FOR_URLS,[],[On MacOS X "open " works, on Linux it tries to open a virtual console.] ) ;; *) AM_CONDITIONAL(WIN32_PKG, false) diff --git a/synfig-studio/trunk/src/gtkmm/app.cpp b/synfig-studio/trunk/src/gtkmm/app.cpp index 629a1b7..38de4e2 100644 --- a/synfig-studio/trunk/src/gtkmm/app.cpp +++ b/synfig-studio/trunk/src/gtkmm/app.cpp @@ -271,7 +271,11 @@ bool studio::App::use_colorspace_gamma=true; bool studio::App::single_threaded=false; #endif bool studio::App::restrict_radius_ducks=false; -String studio::App::browser_command("firefox"); +#ifdef USE_OPEN_FOR_URLS +String studio::App::browser_command("open"); // MacOS only +#else +String studio::App::browser_command("xdg-open"); // Linux XDG standard +#endif static int max_recent_files_=25; int studio::App::get_max_recent_files() { return max_recent_files_; } @@ -2078,24 +2082,75 @@ try_open_url(const std::string &url) { #ifdef WIN32 return ShellExecute(GetDesktopWindow(), "open", url.c_str(), NULL, NULL, SW_SHOW); -#else // WIN32 - gchar* argv[3] = {strdup(App::browser_command.c_str()), strdup(url.c_str()), NULL}; - - GError* gerror = NULL; - gboolean retval; - retval = g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &gerror); - free(argv[0]); - free(argv[1]); - - if (!retval) - { - error(_("Could not execute specified web browser: %s"), gerror->message); - g_error_free(gerror); - return false; - } +#else // !WIN32 + std::vector command_line; + std::vector browsers; + browsers.reserve(23); + + // Browser wrapper scripts +#ifdef USE_OPEN_FOR_URLS + browsers.push_back("open"); // Apple MacOS X wrapper, on Linux it opens a virtual console +#endif + browsers.push_back("xdg-open"); // XDG wrapper + browsers.push_back("sensible-browser"); // Debian wrapper + browsers.push_back("gnome-open"); // GNOME wrapper + browsers.push_back("kfmclient"); // KDE wrapper + browsers.push_back("exo-open"); // XFCE wrapper + + // Alternatives system + browsers.push_back("gnome-www-browser"); // Debian GNOME alternative + browsers.push_back("x-www-browser"); // Debian GUI alternative + + // Individual browsers + browsers.push_back("firefox"); + browsers.push_back("epiphany-browser"); + browsers.push_back("epiphany"); + browsers.push_back("konqueror"); + browsers.push_back("iceweasel"); + browsers.push_back("mozilla"); + browsers.push_back("netscape"); + browsers.push_back("icecat"); + browsers.push_back("galeon"); + browsers.push_back("midori"); + browsers.push_back("safari"); + browsers.push_back("opera"); + browsers.push_back("amaya"); + browsers.push_back("netsurf"); + browsers.push_back("dillo"); + + // Try the user-specified browser first + command_line.push_back(App::browser_command); + if( command_line[0] == "kfmclient" ) command_line.push_back("openURL"); + command_line.push_back(url); + + try { Glib::spawn_async(".", command_line, Glib::SPAWN_SEARCH_PATH); } + catch( Glib::SpawnError& exception ){ + + while ( !browsers.empty() ) + { + // Skip the browser if we already tried it + if( browsers[0] == App::browser_command ) + continue; + + // Construct the command line + command_line.clear(); + command_line.push_back(browsers[0]); + if( command_line[0] == "kfmclient" ) command_line.push_back("openURL"); + command_line.push_back(url); + + // Remove the browser from the list + browsers.erase(browsers.begin()); + + // Try to spawn the browser + try { Glib::spawn_async(".", command_line, Glib::SPAWN_SEARCH_PATH); } + // Failed, move on to the next one + catch(Glib::SpawnError& exception){ continue; } + return true; // No exception means we succeeded! + } + } - return true; -#endif // WIN32 + return false; +#endif // !WIN32 } void @@ -2113,7 +2168,13 @@ App::dialog_help() void App::open_url(const std::string &url) { - try_open_url(url); + if(!try_open_url(url)) + { + Gtk::MessageDialog dialog(_("No browser was found. Please load this website manually:"), false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE, true); + dialog.set_secondary_text(url); + dialog.set_title(_("No browser found")); + dialog.run(); + } } bool -- 2.7.4