]> www.fi.muni.cz Git - evince.git/blobdiff - shell/main.c
[windows] Fix localization on Windows
[evince.git] / shell / main.c
index 1de87f6d6be12237f121d6f8ebb65502fb29fd75..4ed0b05ebd42067d934d64b144599ba637c43df7 100644 (file)
@@ -13,7 +13,7 @@
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  */
 
 #endif
 #include <io.h>
 #include <conio.h>
-#define _WIN32_WINNT 0x0500
+#if !(_WIN32_WINNT >= 0x0500)
+#error "_WIN32_WINNT must be defined >= 0x0500"
+#endif
 #include <windows.h>
 #endif
 
 static gchar   *ev_page_label;
 static gchar   *ev_find_string;
+static gint     ev_page_index = 0;
 static gboolean preview_mode = FALSE;
 static gboolean fullscreen_mode = FALSE;
 static gboolean presentation_mode = FALSE;
@@ -59,6 +62,7 @@ static gboolean unlink_temp_file = FALSE;
 static gchar   *print_settings;
 static const char **file_arguments = NULL;
 
+
 static gboolean
 option_version_cb (const gchar *option_name,
                    const gchar *value,
@@ -73,7 +77,8 @@ option_version_cb (const gchar *option_name,
 
 static const GOptionEntry goption_options[] =
 {
-       { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
+       { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page label of the document to display."), N_("PAGE")},
+       { "page-index", 'i', 0, G_OPTION_ARG_INT, &ev_page_index, N_("The page number of the document to display."), N_("NUMBER")},
        { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
        { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
        { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
@@ -81,7 +86,7 @@ static const GOptionEntry goption_options[] =
        { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
        { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
        { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
-       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
+       { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE]") },
        { NULL }
 };
 
@@ -141,11 +146,26 @@ launch_previewer (void)
        return retval;
 }
 
-static gint
-find_window_list (EvWindow    *window,
-                 const gchar *uri)
+static gchar *
+get_label_from_filename (const gchar *filename)
 {
-       return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
+       GFile   *file;
+       gchar   *label;
+       gboolean exists;
+
+       label = g_strrstr (filename, "#");
+       if (!label)
+               return NULL;
+
+       /* Filename contains a #, check
+        * whether it's part of the path
+        * or a label
+        */
+       file = g_file_new_for_commandline_arg (filename);
+       exists = g_file_query_exists (file, NULL);
+       g_object_unref (file);
+
+       return exists ? NULL : label;
 }
 
 static void
@@ -153,22 +173,19 @@ load_files (const char **files)
 {
        GdkScreen       *screen = gdk_screen_get_default ();
        EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
-       GList           *windows;
        gint             i;
        EvLinkDest      *global_dest = NULL;
 
-       windows = ev_application_get_windows (EV_APP);
-
        if (!files) {
-               if (!windows)
+               if (!ev_application_has_window (EV_APP))
                        ev_application_open_window (EV_APP, screen, GDK_CURRENT_TIME);
-               else
-                       g_list_free (windows);
                return;
        }
 
        if (ev_page_label)
                global_dest = ev_link_dest_new_page_label (ev_page_label);
+       else if (ev_page_index)
+               global_dest = ev_link_dest_new_page (MAX (0, ev_page_index - 1));
 
        if (fullscreen_mode)
                mode = EV_WINDOW_MODE_FULLSCREEN;
@@ -176,28 +193,34 @@ load_files (const char **files)
                mode = EV_WINDOW_MODE_PRESENTATION;
 
        for (i = 0; files[i]; i++) {
-               gchar      *uri;
-               gchar      *label;
-               GFile      *file;
-               EvLinkDest *dest = NULL;
+               const gchar *filename;
+               gchar       *uri;
+               gchar       *label;
+               GFile       *file;
+               EvLinkDest  *dest = NULL;
+               const gchar *app_uri;
+
+               filename = files[i];
+               label = get_label_from_filename (filename);
+               if (label) {
+                       *label = 0;
+                       label++;
+                       dest = ev_link_dest_new_page_label (label);
+               } else if (global_dest) {
+                       dest = g_object_ref (global_dest);
+               }
 
-               file = g_file_new_for_commandline_arg (files[i]);
+               file = g_file_new_for_commandline_arg (filename);
                uri = g_file_get_uri (file);
                g_object_unref (file);
 
-               if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
+               app_uri = ev_application_get_uri (EV_APP);
+               if (app_uri && strcmp (app_uri, uri) == 0) {
                        g_free (uri);
                        continue;
                }
 
-               label = strchr (uri, '#');
-               if (label) {
-                       *label = 0;
-                       label++;
-                       dest = ev_link_dest_new_page_label (label);
-               } else if (global_dest) {
-                       dest = g_object_ref (global_dest);
-               }
+
 
                ev_application_open_uri_at_dest (EV_APP, uri, screen, dest,
                                                 mode, ev_find_string,
@@ -207,16 +230,40 @@ load_files (const char **files)
                        g_object_unref (dest);
                g_free (uri);
         }
+}
+
+static gchar*
+ev_get_locale_dir ()
+{
+#ifdef G_OS_WIN32
+       gchar *install_dir = NULL, *locale_dir;
+       gchar *retval = NULL;
+
+       install_dir =
+           g_win32_get_package_installation_directory_of_module (NULL);
+
+       if (install_dir) {
+               locale_dir = g_build_filename (install_dir,
+                                       "share", "locale", NULL);
+               retval = g_win32_locale_filename_from_utf8 (locale_dir);
+               g_free (install_dir);
+               g_free (locale_dir);
+       }
 
-       g_list_free (windows);
+       if (retval)
+               return retval;
+       else
+               return g_strdup ("");
+#else
+       return g_strdup (GNOMELOCALEDIR);
+#endif
 }
 
 int
 main (int argc, char *argv[])
 {
        GOptionContext *context;
-       GList *toplevels;
-       GError *error = NULL;
+       GError         *error = NULL;
 
 #ifdef G_OS_WIN32
 
@@ -249,7 +296,9 @@ main (int argc, char *argv[])
 
 #ifdef ENABLE_NLS
        /* Initialize the i18n stuff */
-       bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
+       gchar *tmp = ev_get_locale_dir ();
+       bindtextdomain (GETTEXT_PACKAGE, tmp);
+       g_free (tmp);
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        textdomain (GETTEXT_PACKAGE);
 #endif
@@ -265,10 +314,10 @@ main (int argc, char *argv[])
        g_option_context_add_group (context, gtk_get_option_group (TRUE));
 
        if (!g_option_context_parse (context, &argc, &argv, &error)) {
-               g_printerr ("Cannot parse arguments: %s", error->message);
+               g_printerr ("Cannot parse arguments: %s\n", error->message);
                g_error_free (error);
                g_option_context_free (context);
-               
+
                return 1;
        }
        g_option_context_free (context);
@@ -294,18 +343,15 @@ main (int argc, char *argv[])
        gtk_window_set_default_icon_name ("evince");
 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
 
-       ev_application_load_session (EV_APP, file_arguments);
+       ev_application_load_session (EV_APP);
        load_files (file_arguments);
-       toplevels = gtk_window_list_toplevels ();
-       if (toplevels) {
-               g_list_free (toplevels);
-               /* Change directory so we don't prevent unmounting in case the initial cwd
-                * is on an external device (see bug #575436)
-                */
-               g_chdir (g_get_home_dir ());
-
-               gtk_main ();
-       }
+
+       /* Change directory so we don't prevent unmounting in case the initial cwd
+        * is on an external device (see bug #575436)
+        */
+       g_chdir (g_get_home_dir ());
+
+       gtk_main ();
 
        ev_shutdown ();
        ev_stock_icons_shutdown ();