1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/* pps-previewer.c:
* this file is part of papers, a gnome document viewer
*
* Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
* Copyright © 2012, 2018, 2021, 2022 Christian Persch
*
* Papers is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Papers is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <adwaita.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <papers-document.h>
#include <papers-view.h>
#include "pps-previewer-window.h"
static gboolean unlink_temp_file = FALSE;
static int input_fd = -1;
static char *input_file = NULL;
static char *input_mime_type = NULL;
static int print_settings_fd = -1;
static gchar *print_settings_file = NULL;
static PpsPreviewerWindow *window = NULL;
static const GOptionEntry goption_options[] = {
{ "unlink-tempfile", 'u', 0, G_OPTION_ARG_NONE, &unlink_temp_file,
N_ ("Delete the temporary file"), NULL },
{ "print-settings", 'p', 0, G_OPTION_ARG_FILENAME, &print_settings_file,
N_ ("File specifying print settings"), N_ ("FILE") },
{ "fd", 0, 0, G_OPTION_ARG_INT, &input_fd,
N_ ("File descriptor of input file"), N_ ("FD") },
{ "mime-type", 0, 0, G_OPTION_ARG_STRING, &input_mime_type,
N_ ("MIME type of input file"), N_ ("TYPE") },
{ "print-settings-fd", 0, 0, G_OPTION_ARG_INT, &print_settings_fd,
N_ ("File descriptor of print settings file"), N_ ("FD") },
{ NULL }
};
static void
pps_previewer_unlink_tempfile (const gchar *filename)
{
GFile *file, *tempdir;
file = g_file_new_for_path (filename);
tempdir = g_file_new_for_path (g_get_tmp_dir ());
if (g_file_has_prefix (file, tempdir)) {
g_file_delete (file, NULL, NULL);
}
g_object_unref (file);
g_object_unref (tempdir);
}
static void
activate_cb (GApplication *application,
gpointer user_data)
{
if (window) {
gtk_window_present (GTK_WINDOW (window));
}
}
static void
startup_cb (GApplication *application,
gpointer data)
{
PpsJobLoad *job;
GError *error = NULL;
gboolean ps_ok = TRUE;
g_assert (input_fd != -1 || input_file != NULL);
window = pps_previewer_window_new ();
if (print_settings_fd != -1) {
ps_ok = pps_previewer_window_set_print_settings_fd (PPS_PREVIEWER_WINDOW (window), print_settings_fd, &error);
print_settings_fd = -1;
} else if (print_settings_file != NULL) {
ps_ok = pps_previewer_window_set_print_settings (PPS_PREVIEWER_WINDOW (window), print_settings_file, &error);
g_clear_pointer (&print_settings_file, g_free);
}
if (!ps_ok) {
g_printerr ("Failed to load print settings: %s\n", error->message);
g_clear_error (&error);
}
job = PPS_JOB_LOAD (pps_job_load_new ());
if (input_fd != -1) {
if (!pps_previewer_window_set_source_fd (PPS_PREVIEWER_WINDOW (window), input_fd, &error)) {
g_printerr ("Failed to set source FD: %s\n", error->message);
g_clear_error (&error);
g_application_quit (application);
return;
}
pps_job_load_take_fd (job, input_fd, input_mime_type);
pps_job_load_set_load_flags (job, PPS_DOCUMENT_LOAD_FLAG_NO_CACHE);
input_fd = -1;
} else {
GFile *file;
char *uri;
char *path;
file = g_file_new_for_commandline_arg (input_file);
uri = g_file_get_uri (file);
path = g_file_get_path (file);
pps_previewer_window_set_source_file (PPS_PREVIEWER_WINDOW (window), path);
pps_job_load_set_uri (job, uri);
g_free (uri);
g_free (path);
g_object_unref (file);
g_clear_pointer (&input_file, g_free);
}
pps_previewer_window_set_job (window, PPS_JOB (job));
g_object_unref (job);
/* Window will be presented by 'activate' signal */
}
static gboolean
check_arguments (int argc,
char **argv,
GError **error)
{
if (input_fd != -1) {
struct stat statbuf;
int flags;
if (fstat (input_fd, &statbuf) == -1 ||
(flags = fcntl (input_fd, F_GETFL, &flags)) == -1) {
int errsv = errno;
g_set_error_literal (error, G_FILE_ERROR,
g_file_error_from_errno (errsv),
g_strerror (errsv));
return FALSE;
}
if (!S_ISREG (statbuf.st_mode)) {
g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_BADF,
"Not a regular file.");
return FALSE;
}
switch (flags & O_ACCMODE) {
case O_RDONLY:
case O_RDWR:
break;
case O_WRONLY:
default:
g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_BADF,
"Not a readable file descriptor.");
return FALSE;
}
if (argc > 1) {
g_set_error_literal (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Too many arguments");
return FALSE;
}
if (input_mime_type == NULL) {
input_mime_type = pps_file_get_mime_type_from_fd (input_fd, error);
if (input_mime_type == NULL) {
g_prefix_error (error, "Must specify --mime-type: ");
return FALSE;
}
}
if (unlink_temp_file) {
g_set_error_literal (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Must not specify --unlink-tempfile");
return FALSE;
}
} else {
char *path;
if (argc != 2) {
g_set_error_literal (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Need exactly one argument");
return FALSE;
}
if (input_mime_type != NULL) {
g_set_error_literal (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Must not specify --mime-type");
return FALSE;
}
path = g_filename_from_uri (argv[1], NULL, NULL);
if (!g_file_test (argv[1], G_FILE_TEST_IS_REGULAR) && !g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT,
"File \"%s\" does not exist or is not a regular file\n", argv[1]);
g_free (path);
return FALSE;
}
g_free (path);
input_file = g_strdup (argv[1]);
}
return TRUE;
}
gint
main (gint argc, gchar **argv)
{
AdwApplication *application;
GOptionContext *context;
GError *error = NULL;
int status = 1;
const gchar *action_accels[] = {
"win.select-page",
"<Ctrl>L",
NULL,
"win.go-previous-page",
"p",
"<Ctrl>Page_Up",
NULL,
"win.go-next-page",
"n",
"<Ctrl>Page_Down",
NULL,
"win.print",
"<Ctrl>P",
NULL,
"win.zoom-in",
"plus",
"<Ctrl>plus",
"KP_Add",
"<Ctrl>KP_Add",
"equal",
"<Ctrl>equal",
NULL,
"win.zoom-out",
"minus",
"<Ctrl>minus",
"KP_Subtract",
"<Ctrl>KP_Subtract",
NULL,
"win.zoom-default",
"a",
NULL,
NULL,
};
const char **it;
/* Initialize the i18n stuff */
bindtextdomain (GETTEXT_PACKAGE, PPS_LOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
g_set_prgname ("papers-previewer");
context = g_option_context_new (_ ("GNOME Document Previewer"));
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
if (!g_option_context_parse (context, &argc, &argv, &error) ||
!check_arguments (argc, argv, &error)) {
g_printerr ("Error parsing command line arguments: %s\n", error->message);
g_error_free (error);
g_option_context_free (context);
return 1;
}
g_option_context_free (context);
if (!pps_init ())
return 1;
g_set_application_name (_ ("GNOME Document Previewer"));
gtk_window_set_default_icon_name (APP_ID);
application = adw_application_new (APP_ID "-previewer", G_APPLICATION_NON_UNIQUE);
g_application_set_resource_base_path (G_APPLICATION (application),
"/org/gnome/papers/previewer");
g_signal_connect (application, "startup", G_CALLBACK (startup_cb), NULL);
g_signal_connect (application, "activate", G_CALLBACK (activate_cb), NULL);
for (it = action_accels; it[0]; it += g_strv_length ((gchar **) it) + 1)
gtk_application_set_accels_for_action (GTK_APPLICATION (application), it[0], &it[1]);
status = g_application_run (G_APPLICATION (application), 0, NULL);
if (unlink_temp_file)
pps_previewer_unlink_tempfile (argv[1]);
if (print_settings_file)
pps_previewer_unlink_tempfile (print_settings_file);
pps_job_scheduler_wait ();
pps_shutdown ();
return status;
}
|