New CPDB print backend for GTK Print Dialog

The Common Print Dialog Backends (CPDB) concept has GUI-toolkit-independent
backends for each print technology (CUPS, Print to File, cloud printing
services, ...) and each print dialog (GTK, Qt, Chromium, ...) is supposed
to use this backend, so that changes in print technologies can be centrally
and quickly covered by changing the backends and everything new gets available
in all print dialogs.

This commit provides a GTK print dialog backend to add support for the CPDB
concept. It communicates with all installed CPDB backends and so gives support
for all these print technologies to the GTK print dialog.

To make use of CPDB the GTK print dialog is supposed to be installed with this
backend and the 'Print To File' backend, and not any others to prevent printer
duplication.
This commit is contained in:
tinytrebuchet 2023-02-09 23:47:02 +05:30
parent 45e6e9a7d1
commit 41b60bbd6c
9 changed files with 2100 additions and 82 deletions

View File

@ -41,6 +41,11 @@ option('media-gstreamer',
# Print backends
option('print-cpdb',
type: 'feature',
value: 'disabled',
description : 'Build the cpdb print backend')
option('print-cups',
type: 'feature',
value: 'auto',

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,43 @@
/* GTK - The GIMP Toolkit
* gtkprintbackendcpdb.h: Default implementation of GtkPrintBackend
* for the Common Print Dialog Backends (CPDB)
* Copyright (C) 2022, 2023 TinyTrebuchet <tinytrebuchet@protonmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GTK_PRINT_BACKEND_CPDB_H__
#define __GTK_PRINT_BACKEND_CPDB_H__
#include <glib-object.h>
#include <gtk/gtk.h>
#include "gtkprintbackendprivate.h"
G_BEGIN_DECLS
#define GTK_TYPE_PRINT_BACKEND_CPDB (gtk_print_backend_cpdb_get_type ())
#define GTK_PRINT_BACKEND_CPDB(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_PRINT_BACKEND_CPDB, GtkPrintBackendCpdb))
#define GTK_IS_PRINT_BACKEND_CPDB(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_PRINT_BACKEND_CPDB))
typedef struct _GtkPrintBackendCpdbClass GtkPrintBackendCpdbClass;
typedef struct _GtkPrintBackendCpdb GtkPrintBackendCpdb;
GtkPrintBackend *gtk_print_backend_cpdb_new (void);
GType gtk_print_backend_cpdb_get_type (void) G_GNUC_CONST;
G_END_DECLS
#endif /* __GTK_PRINT_BACKEND_CPDB_H__ */

View File

@ -55,6 +55,7 @@
#include "gtkcupsutils.h"
#include "gtkcupssecretsutils.h"
#include "gtkprintbackendutils.h"
#include <gtkprintutils.h>
#include "gtkprivate.h"
@ -6483,87 +6484,6 @@ foreach_option_get_settings (GtkPrinterOption *option,
gtk_print_settings_set (settings, option->name, value);
}
static gboolean
supports_am_pm (void)
{
struct tm tmp_tm = { 0 };
char time[8];
int length;
length = strftime (time, sizeof (time), "%p", &tmp_tm);
return length != 0;
}
/* Converts local time to UTC time. Local time has to be in one of these
* formats: HH:MM:SS, HH:MM, HH:MM:SS {am, pm}, HH:MM {am, pm}, HH {am, pm},
* {am, pm} HH:MM:SS, {am, pm} HH:MM, {am, pm} HH.
* Returns a newly allocated string holding UTC time in HH:MM:SS format
* or NULL.
*/
static char *
localtime_to_utctime (const char *local_time)
{
const char *formats_0[] = {" %I : %M : %S %p ", " %p %I : %M : %S ",
" %H : %M : %S ",
" %I : %M %p ", " %p %I : %M ",
" %H : %M ",
" %I %p ", " %p %I "};
const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
const char *end = NULL;
struct tm *actual_local_time;
struct tm *actual_utc_time;
struct tm local_print_time;
struct tm utc_print_time;
struct tm diff_time;
char *utc_time = NULL;
int i, n;
if (local_time == NULL || local_time[0] == '\0')
return NULL;
n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
for (i = 0; i < n; i++)
{
local_print_time.tm_hour = 0;
local_print_time.tm_min = 0;
local_print_time.tm_sec = 0;
if (supports_am_pm ())
end = strptime (local_time, formats_0[i], &local_print_time);
else
end = strptime (local_time, formats_1[i], &local_print_time);
if (end != NULL && end[0] == '\0')
break;
}
if (end != NULL && end[0] == '\0')
{
time_t rawtime;
time (&rawtime);
actual_utc_time = g_memdup2 (gmtime (&rawtime), sizeof (struct tm));
actual_local_time = g_memdup2 (localtime (&rawtime), sizeof (struct tm));
diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
diff_time.tm_min = actual_utc_time->tm_min - actual_local_time->tm_min;
diff_time.tm_sec = actual_utc_time->tm_sec - actual_local_time->tm_sec;
utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
utc_print_time.tm_min = ((local_print_time.tm_min + diff_time.tm_min) + 60) % 60;
utc_print_time.tm_sec = ((local_print_time.tm_sec + diff_time.tm_sec) + 60) % 60;
utc_time = g_strdup_printf ("%02d:%02d:%02d",
utc_print_time.tm_hour,
utc_print_time.tm_min,
utc_print_time.tm_sec);
}
return utc_time;
}
static void
cups_printer_get_settings_from_options (GtkPrinter *printer,
GtkPrinterOptionSet *options,

View File

@ -0,0 +1,128 @@
/* GTK - The GIMP Toolkit
* gtkprintbackendcups.h: Default implementation of GtkPrintBackend
* for the Common Unix Print System (CUPS)
* Copyright (C) 2006, 2007 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <sys/random.h>
#include "gtkprintbackendutils.h"
/* Converts local time to UTC time. Local time has to be in one of these
* formats: HH:MM:SS, HH:MM, HH:MM:SS {am, pm}, HH:MM {am, pm}, HH {am, pm},
* {am, pm} HH:MM:SS, {am, pm} HH:MM, {am, pm} HH.
* Returns a newly allocated string holding UTC time in HH:MM:SS format
* or NULL.
*/
char *
localtime_to_utctime (const char *local_time)
{
const char *formats_0[] = {" %I : %M : %S %p ", " %p %I : %M : %S ",
" %H : %M : %S ",
" %I : %M %p ", " %p %I : %M ",
" %H : %M ",
" %I %p ", " %p %I "};
const char *formats_1[] = {" %H : %M : %S ", " %H : %M "};
const char *end = NULL;
struct tm *actual_local_time;
struct tm *actual_utc_time;
struct tm local_print_time;
struct tm utc_print_time;
struct tm diff_time;
char *utc_time = NULL;
int i, n;
if (local_time == NULL || local_time[0] == '\0')
return NULL;
n = supports_am_pm () ? G_N_ELEMENTS (formats_0) : G_N_ELEMENTS (formats_1);
for (i = 0; i < n; i++)
{
local_print_time.tm_hour = 0;
local_print_time.tm_min = 0;
local_print_time.tm_sec = 0;
if (supports_am_pm ())
end = strptime (local_time, formats_0[i], &local_print_time);
else
end = strptime (local_time, formats_1[i], &local_print_time);
if (end != NULL && end[0] == '\0')
break;
}
if (end != NULL && end[0] == '\0')
{
time_t rawtime;
time (&rawtime);
actual_utc_time = g_memdup2 (gmtime (&rawtime), sizeof (struct tm));
actual_local_time = g_memdup2 (localtime (&rawtime), sizeof (struct tm));
diff_time.tm_hour = actual_utc_time->tm_hour - actual_local_time->tm_hour;
diff_time.tm_min = actual_utc_time->tm_min - actual_local_time->tm_min;
diff_time.tm_sec = actual_utc_time->tm_sec - actual_local_time->tm_sec;
utc_print_time.tm_hour = ((local_print_time.tm_hour + diff_time.tm_hour) + 24) % 24;
utc_print_time.tm_min = ((local_print_time.tm_min + diff_time.tm_min) + 60) % 60;
utc_print_time.tm_sec = ((local_print_time.tm_sec + diff_time.tm_sec) + 60) % 60;
utc_time = g_strdup_printf ("%02d:%02d:%02d",
utc_print_time.tm_hour,
utc_print_time.tm_min,
utc_print_time.tm_sec);
}
return utc_time;
}
gboolean
supports_am_pm (void)
{
struct tm tmp_tm = { 0 };
char time[8];
int length;
length = strftime (time, sizeof (time), "%p", &tmp_tm);
return length != 0;
}
/*
* Generate a random string of "n" length
*/
char *
random_string (int n)
{
const char charset[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
char *str = g_new0 (char, n+1);
getrandom (str, n, 0);
for (int i=0; i<n; i++)
{
int rand = str[i] + 128;
int idx = rand % ((int) sizeof charset);
str[i] = charset[idx];
}
str[n] = '\0';
return str;
}

View File

@ -0,0 +1,27 @@
/* GTK - The GIMP Toolkit
* gtkprintbackendcups.h: Default implementation of GtkPrintBackend
* for the Common Unix Print System (CUPS)
* Copyright (C) 2006, 2007 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GTK_PRINT_BACKEND_UTILS_H__
#define __GTK_PRINT_BACKEND_UTILS_H__
char *random_string (int size);
char *localtime_to_utctime (const char *local_time);
gboolean supports_am_pm (void);
#endif /* __GTK_PRINT_BACKEND_UTILS_H__ */

View File

@ -0,0 +1,137 @@
/* GtkPrinterCpdb
* Copyright (C) 2022, 2023 TinyTrebuchet <tinytrebuchet@protonmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtkprintercpdb.h>
enum {
PROP_0,
PROP_PRINTER_OBJ
};
static void gtk_printer_cpdb_init (GtkPrinterCpdb *printer);
static void gtk_printer_cpdb_class_init (GtkPrinterCpdbClass *class);
static GType gtk_printer_cpdb_type = 0;
static void gtk_printer_cpdb_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_printer_cpdb_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
void
gtk_printer_cpdb_register_type (GTypeModule *module)
{
const GTypeInfo object_info =
{
sizeof (GtkPrinterCpdbClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gtk_printer_cpdb_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GtkPrinterCpdb),
0, /* n_preallocs */
(GInstanceInitFunc) gtk_printer_cpdb_init,
};
gtk_printer_cpdb_type = g_type_module_register_type (module,
GTK_TYPE_PRINTER,
"GtkPrinterCpdb",
&object_info, 0);
}
GType
gtk_printer_cpdb_get_type (void)
{
return gtk_printer_cpdb_type;
}
static void
gtk_printer_cpdb_class_init (GtkPrinterCpdbClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = gtk_printer_cpdb_set_property;
object_class->get_property = gtk_printer_cpdb_get_property;
g_object_class_install_property (G_OBJECT_CLASS (klass),
PROP_PRINTER_OBJ,
g_param_spec_pointer ("printer-obj",
NULL,
NULL,
G_PARAM_READWRITE));
}
static void
gtk_printer_cpdb_init (GtkPrinterCpdb *self)
{
}
static void
gtk_printer_cpdb_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (object);
switch (prop_id)
{
case PROP_PRINTER_OBJ:
printer_cpdb->printer_obj = g_value_get_pointer (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_printer_cpdb_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkPrinterCpdb *printer_cpdb = GTK_PRINTER_CPDB (object);
switch (prop_id)
{
case PROP_PRINTER_OBJ:
g_value_set_pointer (value, printer_cpdb->printer_obj);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
cpdb_printer_obj_t *
gtk_printer_cpdb_get_printer_obj (GtkPrinterCpdb *cpdb_printer)
{
return cpdb_printer->printer_obj;
}
void
gtk_printer_cpdb_set_printer_obj (GtkPrinterCpdb *cpdb_printer,
cpdb_printer_obj_t *printer_obj)
{
cpdb_printer->printer_obj = printer_obj;
}

View File

@ -0,0 +1,46 @@
/* GtkPrinterCpdb
* Copyright (C) 2022, 2023 TinyTrebuchet <tinytrebuchet@protonmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GTK_PRINTER_CPDB_H__
#define __GTK_PRINTER_CPDB_H__
#include <glib-object.h>
#include <cpdb/cpdb-frontend.h>
#include <gtk/gtkprinterprivate.h>
G_BEGIN_DECLS
#define GTK_TYPE_PRINTER_CPDB (gtk_printer_cpdb_get_type ())
G_DECLARE_FINAL_TYPE (GtkPrinterCpdb, gtk_printer_cpdb, GTK, PRINTER_CPDB, GtkPrinter)
struct _GtkPrinterCpdb
{
GtkPrinter parent_instance;
cpdb_printer_obj_t *printer_obj;
};
void gtk_printer_cpdb_register_type (GTypeModule *module);
cpdb_printer_obj_t *gtk_printer_cpdb_get_printer_obj (GtkPrinterCpdb *cpdb_printer);
void gtk_printer_cpdb_set_printer_obj (GtkPrinterCpdb *cpdb_printer,
cpdb_printer_obj_t *printer_obj);
G_END_DECLS
#endif /* __GTK_PRINTER_CPDB_H__ */

View File

@ -10,8 +10,27 @@ printbackends_args = [
'-DGTK_PRINT_BACKEND_ENABLE_UNSUPPORTED',
] + common_cflags
cpdb_dep = dependency('cpdb-frontend', version : '>=1.0', required: get_option('print-cpdb'))
cups_dep = dependency('cups', version : '>=2.0', required: get_option('print-cups'))
if cups_dep.found()
# Use cpdb backend if present and enabled.
# If not, use cups if present.
# If not, use lpr.
if get_option('print-cpdb').enabled() and cpdb_dep.found()
print_backends += 'cpdb'
shared_module('printbackend-cpdb',
sources: [
'gtkprintbackendcpdb.c',
'gtkprintercpdb.c',
'gtkprintbackendutils.c',
],
c_args: printbackends_args,
dependencies: [libgtk_dep, cpdb_dep],
install_dir: printbackends_install_dir,
install: true,
)
elif cups_dep.found()
print_backends += 'cups'
shared_module('printbackend-cups',
sources: [
@ -19,6 +38,7 @@ if cups_dep.found()
'gtkprintercups.c',
'gtkcupsutils.c',
'gtkcupssecretsutils.c',
'gtkprintbackendutils.c',
],
c_args: printbackends_args,
dependencies: [libgtk_dep, cups_dep, colord_dep],