*
*/
+#include <config.h>
+
#include "ev-utils.h"
+#include "ev-file-helpers.h"
+
+#include <string.h>
#include <math.h>
+#define PRINT_CONFIG_FILENAME "ev-print-config.xml"
+
typedef struct
{
int size;
g_free (rectangles);
}
+#ifdef WITH_GNOME_PRINT
+gboolean
+using_pdf_printer (GnomePrintConfig *config)
+{
+ const guchar *driver;
-#ifndef HAVE_G_FILE_SET_CONTENTS
+ driver = gnome_print_config_get (
+ config, (const guchar *)"Settings.Engine.Backend.Driver");
-#include <errno.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <string.h>
+ if (driver) {
+ if (!strcmp ((const gchar *)driver, "gnome-print-pdf"))
+ return TRUE;
+ else
+ return FALSE;
+ }
+
+ return FALSE;
+}
-static gboolean
-rename_file (const char *old_name,
- const char *new_name,
- GError **err)
+gboolean
+using_postscript_printer (GnomePrintConfig *config)
{
- errno = 0;
- if (g_rename (old_name, new_name) == -1)
- {
- return FALSE;
- }
-
- return TRUE;
+ const guchar *driver;
+ const guchar *transport;
+
+ driver = gnome_print_config_get (
+ config, (const guchar *)"Settings.Engine.Backend.Driver");
+
+ transport = gnome_print_config_get (
+ config, (const guchar *)"Settings.Transport.Backend");
+
+ if (driver) {
+ if (!strcmp ((const gchar *)driver, "gnome-print-ps"))
+ return TRUE;
+ else
+ return FALSE;
+ } else if (transport) { /* these transports default to PostScript */
+ if (!strcmp ((const gchar *)transport, "CUPS"))
+ return TRUE;
+ else if (!strcmp ((const gchar *)transport, "LPD"))
+ return TRUE;
+ else if (!strcmp ((const gchar *)transport, "PAPI"))
+ return TRUE;
+ }
+
+ return FALSE;
}
-static gboolean
-set_umask_permissions (int fd,
- GError **err)
+GnomePrintConfig *
+load_print_config_from_file (void)
{
- /* All of this function is just to work around the fact that
- * there is no way to get the umask without changing it.
- *
- * We can't just change-and-reset the umask because that would
- * lead to a race condition if another thread tried to change
- * the umask in between the getting and the setting of the umask.
- * So we have to do the whole thing in a child process.
- */
-
- int save_errno;
- pid_t pid;
-
- pid = fork ();
-
- if (pid == -1)
- {
- return FALSE;
- }
- else if (pid == 0)
- {
- /* child */
- mode_t mask = umask (0666);
+ GnomePrintConfig *print_config = NULL;
+ char *file_name, *contents = NULL;
- errno = 0;
- if (fchmod (fd, 0666 & ~mask) == -1)
- _exit (errno);
- else
- _exit (0);
+ file_name = g_build_filename (ev_dot_dir (), PRINT_CONFIG_FILENAME,
+ NULL);
- return TRUE; /* To quiet gcc */
- }
- else
- {
- /* parent */
- int status;
+ if (g_file_get_contents (file_name, &contents, NULL, NULL)) {
+ print_config = gnome_print_config_from_string (contents, 0);
+ g_free (contents);
+ }
- errno = 0;
- if (waitpid (pid, &status, 0) == -1)
- {
- return FALSE;
+ if (print_config == NULL) {
+ print_config = gnome_print_config_default ();
}
- if (WIFEXITED (status))
- {
- save_errno = WEXITSTATUS (status);
+ g_free (file_name);
- if (save_errno == 0)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- else if (WIFSIGNALED (status))
- {
- return FALSE;
- }
- else
- {
- return FALSE;
- }
- }
+ return print_config;
}
-static gchar *
-write_to_temp_file (const gchar *contents,
- gssize length,
- const gchar *template,
- GError **err)
+void
+save_print_config_to_file (GnomePrintConfig *config)
{
- gchar *tmp_name;
- gchar *display_name;
- gchar *retval;
- FILE *file;
- gint fd;
- int save_errno;
-
- retval = NULL;
-
- tmp_name = g_strdup_printf ("%s.XXXXXX", template);
+ char *file_name, *str;
- errno = 0;
- fd = g_mkstemp (tmp_name);
- display_name = g_filename_display_name (tmp_name);
-
- if (fd == -1)
- {
- goto out;
- }
+ g_return_if_fail (config != NULL);
- if (!set_umask_permissions (fd, err))
- {
- close (fd);
- g_unlink (tmp_name);
+ str = gnome_print_config_to_string (config, 0);
+ if (str == NULL) return;
- goto out;
- }
-
- errno = 0;
- file = fdopen (fd, "wb");
- if (!file)
- {
- close (fd);
- g_unlink (tmp_name);
-
- goto out;
- }
+ file_name = g_build_filename (ev_dot_dir (),
+ PRINT_CONFIG_FILENAME,
+ NULL);
- if (length > 0)
- {
- size_t n_written;
-
- errno = 0;
+ g_file_set_contents (file_name, str, -1, NULL);
- n_written = fwrite (contents, 1, length, file);
+ g_free (file_name);
+ g_free (str);
+}
+#endif /* WITH_GNOME_PRINT */
- if (n_written < length)
- {
- fclose (file);
- g_unlink (tmp_name);
-
- goto out;
- }
- }
-
- errno = 0;
- if (fclose (file) == EOF)
- {
- g_unlink (tmp_name);
-
- goto out;
- }
+static void
+ev_gui_sanitise_popup_position (GtkMenu *menu,
+ GtkWidget *widget,
+ gint *x,
+ gint *y)
+{
+ GdkScreen *screen = gtk_widget_get_screen (widget);
+ gint monitor_num;
+ GdkRectangle monitor;
+ GtkRequisition req;
- retval = g_strdup (tmp_name);
-
- out:
- g_free (tmp_name);
- g_free (display_name);
-
- return retval;
+ g_return_if_fail (widget != NULL);
+
+ gtk_widget_size_request (GTK_WIDGET (menu), &req);
+
+ monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
+ gtk_menu_set_monitor (menu, monitor_num);
+ gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
+
+ *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
+ *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
}
-gboolean
-ev_file_set_contents (const gchar *filename,
- const gchar *contents,
- gssize length,
- GError **error)
+void
+ev_gui_menu_position_tree_selection (GtkMenu *menu,
+ gint *x,
+ gint *y,
+ gboolean *push_in,
+ gpointer user_data)
{
- gchar *tmp_filename;
- gboolean retval;
- GError *rename_error = NULL;
-
- g_return_val_if_fail (filename != NULL, FALSE);
- g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
- g_return_val_if_fail (contents != NULL || length == 0, FALSE);
- g_return_val_if_fail (length >= -1, FALSE);
-
- if (length == -1)
- length = strlen (contents);
+ GtkTreeSelection *selection;
+ GList *selected_rows;
+ GtkTreeModel *model;
+ GtkTreeView *tree_view = GTK_TREE_VIEW (user_data);
+ GtkWidget *widget = GTK_WIDGET (user_data);
+ GtkRequisition req;
+ GdkRectangle visible;
+
+ gtk_widget_size_request (GTK_WIDGET (menu), &req);
+ gdk_window_get_origin (widget->window, x, y);
+
+ *x += (widget->allocation.width - req.width) / 2;
+
+ /* Add on height for the treeview title */
+ gtk_tree_view_get_visible_rect (tree_view, &visible);
+ *y += widget->allocation.height - visible.height;
+
+ selection = gtk_tree_view_get_selection (tree_view);
+ selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
+ if (selected_rows)
+ {
+ GdkRectangle cell_rect;
- tmp_filename = write_to_temp_file (contents, length, filename, error);
-
- if (!tmp_filename)
- {
- retval = FALSE;
- goto out;
- }
+ gtk_tree_view_get_cell_area (tree_view, selected_rows->data,
+ NULL, &cell_rect);
- if (!rename_file (tmp_filename, filename, &rename_error))
- {
- g_unlink (tmp_filename);
- g_propagate_error (error, rename_error);
- retval = FALSE;
- goto out;
- }
+ *y += CLAMP (cell_rect.y + cell_rect.height, 0, visible.height);
- retval = TRUE;
-
- out:
- g_free (tmp_filename);
- return retval;
-}
+ g_list_foreach (selected_rows, (GFunc)gtk_tree_path_free, NULL);
+ g_list_free (selected_rows);
+ }
-#endif /* HAVE_G_FILE_SET_CONTENTS */
+ ev_gui_sanitise_popup_position (menu, widget, x, y);
+}