| File: | shell/main.c |
| Warning: | line 259, column 12 This statement is never executed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * Copyright (C) 2004 Marco Pesenti Gritti |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include <glib/gstdio.h> |
| 26 | #include <glib/gi18n.h> |
| 27 | #include <ctk/ctk.h> |
| 28 | |
| 29 | #include "ev-application.h" |
| 30 | #include "ev-debug.h" |
| 31 | #include "ev-init.h" |
| 32 | #include "ev-file-helpers.h" |
| 33 | #include "ev-stock-icons.h" |
| 34 | #include "ev-metadata.h" |
| 35 | |
| 36 | #include "eggsmclient.h" |
| 37 | #include "eggdesktopfile.h" |
| 38 | |
| 39 | |
| 40 | static gchar *ev_page_label; |
| 41 | static gchar *ev_find_string; |
| 42 | static gint ev_page_index = 0; |
| 43 | static gchar *ev_named_dest; |
| 44 | static gboolean preview_mode = FALSE(0); |
| 45 | static gboolean fullscreen_mode = FALSE(0); |
| 46 | static gboolean presentation_mode = FALSE(0); |
| 47 | static gboolean unlink_temp_file = FALSE(0); |
| 48 | static gchar *print_settings; |
| 49 | static const char **file_arguments = NULL((void*)0); |
| 50 | |
| 51 | |
| 52 | static gboolean |
| 53 | option_version_cb (const gchar *option_name, |
| 54 | const gchar *value, |
| 55 | gpointer data, |
| 56 | GError **error) |
| 57 | { |
| 58 | g_print ("%s %s\n", _("CAFE Document Viewer")gettext ("CAFE Document Viewer"), VERSION"1.24.0"); |
| 59 | |
| 60 | exit (0); |
| 61 | return FALSE(0); |
| 62 | } |
| 63 | |
| 64 | static const GOptionEntry goption_options[] = |
| 65 | { |
| 66 | { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page label of the document to display.")("The page label of the document to display."), N_("PAGE")("PAGE")}, |
| 67 | { "page-index", 'i', 0, G_OPTION_ARG_INT, &ev_page_index, N_("The page number of the document to display.")("The page number of the document to display."), N_("NUMBER")("NUMBER")}, |
| 68 | { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run lector in fullscreen mode")("Run lector in fullscreen mode"), NULL((void*)0) }, |
| 69 | { "named-dest", 'n', 0, G_OPTION_ARG_STRING, &ev_named_dest, N_("Named destination to display.")("Named destination to display."), N_("DEST")("DEST")}, |
| 70 | { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run lector in presentation mode")("Run lector in presentation mode"), NULL((void*)0) }, |
| 71 | { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run lector as a previewer")("Run lector as a previewer"), NULL((void*)0) }, |
| 72 | { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document")("The word or phrase to find in the document"), N_("STRING")("STRING")}, |
| 73 | { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL((void*)0), NULL((void*)0) }, |
| 74 | { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL((void*)0), NULL((void*)0) }, |
| 75 | { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL((void*)0), NULL((void*)0) }, |
| 76 | { G_OPTION_REMAINING"", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL((void*)0), N_("[FILE…]")("[FILE…]") }, |
| 77 | { NULL((void*)0) } |
| 78 | }; |
| 79 | |
| 80 | static gboolean |
| 81 | launch_previewer (void) |
| 82 | { |
| 83 | GString *cmd_str; |
| 84 | gchar *cmd; |
| 85 | gboolean retval = FALSE(0); |
| 86 | GError *error = NULL((void*)0); |
| 87 | |
| 88 | /* Rebuild the command line, ignoring options |
| 89 | * not supported by the previewer and taking only |
| 90 | * the first path given |
| 91 | */ |
| 92 | cmd_str = g_string_new ("lector-previewer"); |
| 93 | |
| 94 | if (print_settings) { |
| 95 | gchar *quoted; |
| 96 | |
| 97 | quoted = g_shell_quote (print_settings); |
| 98 | g_string_append_printf (cmd_str, " --print-settings %s", quoted); |
| 99 | g_free (quoted); |
| 100 | } |
| 101 | |
| 102 | if (unlink_temp_file) |
| 103 | g_string_append (cmd_str, " --unlink-tempfile")(__builtin_constant_p (" --unlink-tempfile") ? __extension__ ( { const char * const __val = (" --unlink-tempfile"); g_string_append_len_inline (cmd_str, __val, (__val != ((void*)0)) ? (gssize) strlen ((( __val) + !(__val))) : (gssize) -1); }) : g_string_append_len_inline (cmd_str, " --unlink-tempfile", (gssize) -1)); |
| 104 | |
| 105 | if (file_arguments) { |
| 106 | gchar *quoted; |
| 107 | |
| 108 | quoted = g_shell_quote (file_arguments[0]); |
| 109 | g_string_append_printf (cmd_str, " %s", quoted); |
| 110 | g_free (quoted); |
| 111 | } |
| 112 | |
| 113 | cmd = g_string_free (cmd_str, FALSE)(__builtin_constant_p ((0)) ? (((0)) ? (g_string_free) ((cmd_str ), ((0))) : g_string_free_and_steal (cmd_str)) : (g_string_free ) ((cmd_str), ((0)))); |
| 114 | |
| 115 | if (!error) { |
| 116 | GAppInfo *app; |
| 117 | |
| 118 | app = g_app_info_create_from_commandline (cmd, NULL((void*)0), 0, &error); |
| 119 | |
| 120 | if (app != NULL((void*)0)) { |
| 121 | retval = g_app_info_launch (app, NULL((void*)0), NULL((void*)0), &error); |
| 122 | g_object_unref (app); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (error) { |
| 127 | g_warning ("Error launching previewer: %s\n", error->message); |
| 128 | g_error_free (error); |
| 129 | } |
| 130 | |
| 131 | g_free (cmd); |
| 132 | |
| 133 | return retval; |
| 134 | } |
| 135 | |
| 136 | static gchar * |
| 137 | get_label_from_filename (const gchar *filename) |
| 138 | { |
| 139 | GFile *file; |
| 140 | gchar *label; |
| 141 | gboolean exists; |
| 142 | |
| 143 | label = g_strrstr (filename, "#"); |
| 144 | if (!label) |
| 145 | return NULL((void*)0); |
| 146 | |
| 147 | /* Filename contains a #, check |
| 148 | * whether it's part of the path |
| 149 | * or a label |
| 150 | */ |
| 151 | file = g_file_new_for_commandline_arg (filename); |
| 152 | exists = g_file_query_exists (file, NULL((void*)0)); |
| 153 | g_object_unref (file); |
| 154 | |
| 155 | return exists ? NULL((void*)0) : label; |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | load_files (const char **files) |
| 160 | { |
| 161 | CdkScreen *screen = cdk_screen_get_default (); |
| 162 | EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL; |
| 163 | gint i; |
| 164 | EvLinkDest *global_dest = NULL((void*)0); |
| 165 | |
| 166 | if (!files) { |
| 167 | if (!ev_application_has_window (EV_APP((EvApplication *) g_application_get_default ()))) |
| 168 | ev_application_open_window (EV_APP((EvApplication *) g_application_get_default ()), screen, CDK_CURRENT_TIME0L); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (ev_page_label) |
| 173 | global_dest = ev_link_dest_new_page_label (ev_page_label); |
| 174 | else if (ev_page_index) |
| 175 | global_dest = ev_link_dest_new_page (MAX (0, ev_page_index - 1)(((0) > (ev_page_index - 1)) ? (0) : (ev_page_index - 1))); |
| 176 | else if (ev_named_dest) |
| 177 | global_dest = ev_link_dest_new_named (ev_named_dest); |
| 178 | |
| 179 | if (fullscreen_mode) |
| 180 | mode = EV_WINDOW_MODE_FULLSCREEN; |
| 181 | else if (presentation_mode) |
| 182 | mode = EV_WINDOW_MODE_PRESENTATION; |
| 183 | |
| 184 | for (i = 0; files[i]; i++) { |
| 185 | const gchar *filename; |
| 186 | gchar *uri; |
| 187 | gchar *label; |
| 188 | GFile *file; |
| 189 | EvLinkDest *dest = NULL((void*)0); |
| 190 | const gchar *app_uri; |
| 191 | |
| 192 | filename = files[i]; |
| 193 | label = get_label_from_filename (filename); |
| 194 | if (label) { |
| 195 | *label = 0; |
| 196 | label++; |
| 197 | dest = ev_link_dest_new_page_label (label); |
| 198 | } else if (global_dest) { |
| 199 | dest = g_object_ref (global_dest)((__typeof__ (global_dest)) (g_object_ref) (global_dest)); |
| 200 | } |
| 201 | |
| 202 | file = g_file_new_for_commandline_arg (filename); |
| 203 | uri = g_file_get_uri (file); |
| 204 | g_object_unref (file); |
| 205 | |
| 206 | app_uri = ev_application_get_uri (EV_APP((EvApplication *) g_application_get_default ())); |
| 207 | if (app_uri && strcmp (app_uri, uri) == 0) { |
| 208 | g_free (uri); |
| 209 | continue; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | |
| 214 | ev_application_open_uri_at_dest (EV_APP((EvApplication *) g_application_get_default ()), uri, screen, dest, |
| 215 | mode, ev_find_string, |
| 216 | CDK_CURRENT_TIME0L); |
| 217 | |
| 218 | if (dest) |
| 219 | g_object_unref (dest); |
| 220 | g_free (uri); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | int |
| 225 | main (int argc, char *argv[]) |
| 226 | { |
| 227 | EvApplication *application; |
| 228 | GOptionContext *context; |
| 229 | GError *error = NULL((void*)0); |
| 230 | int status; |
| 231 | |
| 232 | #ifdef ENABLE_NLS1 |
| 233 | /* Initialize the i18n stuff */ |
| 234 | bindtextdomain (GETTEXT_PACKAGE"lector", ev_get_locale_dir()); |
| 235 | bind_textdomain_codeset (GETTEXT_PACKAGE"lector", "UTF-8"); |
| 236 | textdomain (GETTEXT_PACKAGE"lector"); |
| 237 | #endif |
| 238 | |
| 239 | cdk_set_allowed_backends ("x11"); |
| 240 | |
| 241 | context = g_option_context_new (N_("CAFE Document Viewer")("CAFE Document Viewer")); |
| 242 | g_option_context_set_translation_domain(context, GETTEXT_PACKAGE"lector"); |
| 243 | g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE"lector"); |
| 244 | g_option_context_add_group (context, egg_sm_client_get_option_group ()); |
| 245 | g_option_context_add_group (context, ctk_get_option_group (TRUE(!(0)))); |
| 246 | |
| 247 | if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 248 | g_printerr ("Cannot parse arguments: %s\n", error->message); |
| 249 | g_error_free (error); |
| 250 | g_option_context_free (context); |
| 251 | |
| 252 | return 1; |
| 253 | } |
| 254 | g_option_context_free (context); |
| 255 | |
| 256 | if (preview_mode) { |
| 257 | gboolean retval; |
| 258 | |
| 259 | retval = launch_previewer (); |
This statement is never executed | |
| 260 | |
| 261 | return retval ? 0 : 1; |
| 262 | } |
| 263 | |
| 264 | if (!ev_init ()) |
| 265 | return 1; |
| 266 | |
| 267 | ev_stock_icons_init (); |
| 268 | |
| 269 | egg_set_desktop_file (CAFEDATADIR"/usr/share" "/applications/lector.desktop"); |
| 270 | |
| 271 | application = ev_application_new (); |
| 272 | if (!g_application_register (G_APPLICATION (application)((((GApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((application)), ((g_application_get_type ())))))), NULL((void*)0), &error)) { |
| 273 | g_printerr ("Failed to register: %s\n", error->message); |
| 274 | g_error_free (error); |
| 275 | status = 1; |
| 276 | goto done; |
| 277 | } |
| 278 | |
| 279 | ev_application_load_session (application); |
| 280 | load_files (file_arguments); |
| 281 | |
| 282 | /* Change directory so we don't prevent unmounting in case the initial cwd |
| 283 | * is on an external device (see bug #575436) |
| 284 | */ |
| 285 | g_chdir (g_get_home_dir ()); |
| 286 | |
| 287 | status = g_application_run (G_APPLICATION (application)((((GApplication*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((application)), ((g_application_get_type ())))))), 0, NULL((void*)0)); |
| 288 | |
| 289 | done: |
| 290 | ev_shutdown (); |
| 291 | ev_stock_icons_shutdown (); |
| 292 | |
| 293 | g_object_unref (application); |
| 294 | return status; |
| 295 | } |