cups-filters/0001-Add-BrowseOptionsUpdate.patch
2025-04-14 14:40:51 +02:00

195 lines
7.7 KiB
Diff

diff --git a/utils/cups-browsed.c b/utils/cups-browsed.c
index b190829..c3ffce5 100644
--- a/utils/cups-browsed.c
+++ b/utils/cups-browsed.c
@@ -342,6 +342,17 @@ typedef enum autoshutdown_inactivity_type_e {
NO_JOBS
} autoshutdown_inactivity_type_t;
+// How and when to take the options when recreating queue found by browsing:
+// NONE - from file at CacheDir
+// STATIC - from destination's IPP Get-Printer-Attributes response once the service is found
+// DYNAMIC - from destination's IPP Get-Printer-Attributes response at every browsing event defined by BrowseInterval
+typedef enum browse_options_update
+{
+ NONE = 0,
+ STATIC,
+ DYNAMIC
+} browse_options_update_t;
+
typedef struct media_size_s{
int x;
int y;
@@ -490,6 +501,7 @@ static autoshutdown_inactivity_type_t autoshutdown_on = NO_QUEUES;
static guint autoshutdown_exec_id = 0;
static const char *default_printer = NULL;
static unsigned int notify_lease_duration = 86400;
+static browse_options_update_t method = NONE;
static int debug_stderr = 0;
static int debug_logfile = 0;
@@ -6996,14 +7008,16 @@ on_printer_modified (CupsNotifier *object,
return;
}
/* The user has changed settings of a printer which we have generated,
- backup the changes for the case of a crash or unclean shutdown of
- cups-browsed. */
- if (!p->no_autosave) {
- debug_printf("Settings of printer %s got modified, doing backup.\n",
+ backup the changes for the case of a crash or unclean shutdown of
+ cups-browsed if we don't want to get defaults from destination. */
+ if (!p->no_autosave && method == NONE) {
+ debug_printf("Settings of printer %s got modified, doing backup.\n",
p->queue_name);
- p->no_autosave = 1; /* Avoid infinite recursion */
- record_printer_options(p->queue_name);
- p->no_autosave = 0;
+ p->no_autosave = 1; /* Avoid infinite recursion */
+
+ record_printer_options(p->queue_name);
+
+ p->no_autosave = 0;
}
}
}
@@ -7816,8 +7830,11 @@ gboolean update_cups_queues(gpointer unused) {
p->no_autosave = 1;
/* Record the option settings to retrieve them when the remote
- queue re-appears later or when cups-browsed gets started again */
- record_printer_options(p->queue_name);
+ queue re-appears later or when cups-browsed gets started again
+ if we want to use local settings */
+ if (method == NONE)
+ record_printer_options(p->queue_name);
+
if (p->status != STATUS_TO_BE_RELEASED &&
!queue_overwritten(p)) {
@@ -8486,9 +8503,10 @@ gboolean update_cups_queues(gpointer unused) {
&p->options);
}
- /* Loading saved option settings from last session */
- p->num_options = load_printer_options(p->queue_name, p->num_options,
- &p->options);
+ // Loading saved option settings from last session if we want them
+ if (method == NONE)
+ p->num_options = load_printer_options(p->queue_name, p->num_options,
+ &p->options);
/* Determine whether we have an IPP network printer. If not we
have remote CUPS queue(s) and so we use an implicit class for
@@ -9902,7 +9920,27 @@ examine_discovered_printer_record(const char *host,
p->type = strdup(type);
p->domain = strdup(domain);
debug_printf("Switched over to newly discovered entry for this printer.\n");
- } else
+ }
+ else if (method == DYNAMIC)
+ {
+ // in the end we can skip most free+strdup and use the same pointers for
+ // option update, but we need to free:
+ // - prattrs
+ // - options
+ // - nickname
+ free(p->prattrs);
+ cupsFreeOptions(p->num_options, p->options);
+ free(p->nickname);
+
+ p->prattrs = NULL;
+ p->nickname = NULL;
+ p->options = NULL;
+ p->num_options = 0;
+ p->status = STATUS_TO_BE_CREATED;
+ p->timeout = time(NULL) + TIMEOUT_IMMEDIATELY;
+ debug_printf("Updating printer capabilities for printer %s.\n", p->queue_name);
+ }
+ else
debug_printf("Staying with previously discovered entry for this printer.\n");
/* Mark queue entry as confirmed if the entry
@@ -9923,9 +9961,11 @@ examine_discovered_printer_record(const char *host,
queue_creation_handle_default(p->queue_name);
/* If this queue is disabled, re-enable it. */
enable_printer(p->queue_name);
- /* Record the options, to record any changes which happened
- while cups-browsed was not running */
- record_printer_options(p->queue_name);
+ // If we prefer options from local machine, record them,
+ // to record any changes which happened while cups-browsed
+ // was not running
+ if (method == NONE)
+ record_printer_options(p->queue_name);
}
/* Gather extra info from our new discovery */
@@ -12189,6 +12229,15 @@ read_configuration (const char *filename)
BrowseLDAPFilter = strdup(value);
}
#endif /* HAVE_LDAP */
+ else if (!strcasecmp(line, "BrowseOptionsUpdate") && value)
+ {
+ if (!strcasecmp(value, "None"))
+ method = NONE;
+ else if (!strcasecmp(value, "Static"))
+ method = STATIC;
+ else if (!strcasecmp(value, "Dynamic"))
+ method = DYNAMIC;
+ }
}
if (browse_line_found == 0) {
diff --git a/utils/cups-browsed.conf.5 b/utils/cups-browsed.conf.5
index 7e6ee3b..3f0588e 100644
--- a/utils/cups-browsed.conf.5
+++ b/utils/cups-browsed.conf.5
@@ -1003,6 +1003,25 @@ and doing specific actions when a D-BUS notification comes.
.nf
.fam C
NotifLeaseDuration 86400
+
+.fam T
+.fi
+BrowseOptionsUpdate directive defines how default printing options are updated when
+cups-browsed is running. The value "None" uses default values from destination
+at discovery and overrides them by values from the file "/var/cache/cups/cups-browsed-options-<queue-name>"
+if it is present from the previous cups-browsed run. The value "Static" does not save default
+options between runs and do not read them from file if the cached file exists. The default options
+are statically set at the first discovery and cups-browsed restart is required to synchronize
+new default options from destinations. The value "Dynamic" causes cups-browsed to update default
+options at every "BrowseInterval" event to synchronize with destination's default options automatically.
+With "Static" and "Dynamic" user are able to change default options values locally via "lpoptions".
+.PP
+.nf
+.fam C
+ BrowseOptionsUpdate None
+ BrowseOptionsUpdate Static
+ BrowseOptionsUpdate Dynamic
+
.fam T
.fi
.SH SEE ALSO
diff --git a/utils/cups-browsed.conf.in b/utils/cups-browsed.conf.in
index ee2f5bf..b1d9bb1 100644
--- a/utils/cups-browsed.conf.in
+++ b/utils/cups-browsed.conf.in
@@ -774,3 +774,17 @@ BrowseRemoteProtocols @BROWSEREMOTEPROTOCOLS@
# and doing specific actions when a D-BUS notification comes.
# NotifLeaseDuration 86400
+
+# BrowseOptionsUpdate directive defines how default printing options are updated when
+# cups-browsed is running. The value "None" uses default values from destination
+# at discovery and overrides them by values from the file "/var/cache/cups/cups-browsed-options-<queue-name>"
+# if it is present from the previous cups-browsed run. The value "Static" does not save default
+# options between runs and do not read them from file if the cached file exists. The default options
+# are statically set at the first discovery and cups-browsed restart is required to synchronize
+# new default options from destinations. The value "Dynamic" causes cups-browsed to update default
+# options at every BrowseInterval event to synchronize with destination's default options automatically.
+# With "Static" and "Dynamic" user are able to change default options values locally via "lpoptions".
+#
+# BrowseOptionsUpdate None
+# BrowseOptionsUpdate Static
+# BrowseOptionsUpdate Dynamic