printers: Don't show the same device multiple times

Pass all discovered devices to 'GroupPhysicalDevices' instead of just
the devices already present in the list and the newly discovered ones.
Replace an existing device in the list if there is a better device
(e.g. replace 'usb:' by 'hp:' device).

https://bugzilla.gnome.org/show_bug.cgi?id=693186
This commit is contained in:
Marek Kasik 2015-05-25 14:57:04 +02:00
parent b02f7722dd
commit 880cc01550
2 changed files with 49 additions and 101 deletions

View file

@ -64,8 +64,7 @@ static void new_printer_dialog_response_cb (GtkDialog *_dialog,
gpointer user_data); gpointer user_data);
static void update_spinner_state (PpNewPrinterDialog *dialog); static void update_spinner_state (PpNewPrinterDialog *dialog);
static void add_devices_to_list (PpNewPrinterDialog *dialog, static void add_devices_to_list (PpNewPrinterDialog *dialog,
GList *devices, GList *devices);
gboolean new_device);
static void remove_device_from_list (PpNewPrinterDialog *dialog, static void remove_device_from_list (PpNewPrinterDialog *dialog,
const gchar *device_name); const gchar *device_name);
@ -84,7 +83,7 @@ struct _PpNewPrinterDialogPrivate
GtkBuilder *builder; GtkBuilder *builder;
GList *devices; GList *devices;
GList *new_devices; GList *local_cups_devices;
cups_dest_t *dests; cups_dest_t *dests;
gint num_of_dests; gint num_of_dests;
@ -283,9 +282,7 @@ get_authenticated_samba_devices_cb (GObject *source_object,
if (result->devices != NULL) if (result->devices != NULL)
{ {
add_devices_to_list (dialog, add_devices_to_list (dialog, result->devices);
result->devices,
FALSE);
device = (PpPrintDevice *) result->devices->data; device = (PpPrintDevice *) result->devices->data;
if (device != NULL) if (device != NULL)
@ -460,8 +457,8 @@ pp_new_printer_dialog_finalize (GObject *object)
g_list_free_full (priv->devices, (GDestroyNotify) pp_print_device_free); g_list_free_full (priv->devices, (GDestroyNotify) pp_print_device_free);
priv->devices = NULL; priv->devices = NULL;
g_list_free_full (priv->new_devices, (GDestroyNotify) pp_print_device_free); g_list_free_full (priv->local_cups_devices, (GDestroyNotify) pp_print_device_free);
priv->new_devices = NULL; priv->local_cups_devices = NULL;
if (priv->num_of_dests > 0) if (priv->num_of_dests > 0)
{ {
@ -548,9 +545,8 @@ remove_device_from_list (PpNewPrinterDialog *dialog,
} }
static void static void
add_device_to_list (PpNewPrinterDialog *dialog, add_device_to_list (PpNewPrinterDialog *dialog,
PpPrintDevice *device, PpPrintDevice *device)
gboolean new_device)
{ {
PpNewPrinterDialogPrivate *priv = dialog->priv; PpNewPrinterDialogPrivate *priv = dialog->priv;
PpPrintDevice *store_device; PpPrintDevice *store_device;
@ -578,7 +574,7 @@ add_device_to_list (PpNewPrinterDialog *dialog,
store_device->show = TRUE; store_device->show = TRUE;
canonicalized_name = canonicalize_device_name (priv->devices, canonicalized_name = canonicalize_device_name (priv->devices,
priv->new_devices, priv->local_cups_devices,
priv->dests, priv->dests,
priv->num_of_dests, priv->num_of_dests,
store_device); store_device);
@ -588,13 +584,13 @@ add_device_to_list (PpNewPrinterDialog *dialog,
g_free (store_device->device_name); g_free (store_device->device_name);
store_device->device_name = canonicalized_name; store_device->device_name = canonicalized_name;
if (new_device) if (device->acquisition_method == ACQUISITION_METHOD_DEFAULT_CUPS_SERVER)
priv->new_devices = g_list_append (priv->new_devices, store_device); priv->local_cups_devices = g_list_append (priv->local_cups_devices, store_device);
else else
priv->devices = g_list_append (priv->devices, store_device); priv->devices = g_list_append (priv->devices, store_device);
} }
else if (device->is_authenticated_server && else if (device->is_authenticated_server &&
device->host_name != NULL) device->host_name != NULL)
{ {
store_device = g_new0 (PpPrintDevice, 1); store_device = g_new0 (PpPrintDevice, 1);
store_device->device_name = g_strdup (device->host_name); store_device->device_name = g_strdup (device->host_name);
@ -609,14 +605,13 @@ add_device_to_list (PpNewPrinterDialog *dialog,
static void static void
add_devices_to_list (PpNewPrinterDialog *dialog, add_devices_to_list (PpNewPrinterDialog *dialog,
GList *devices, GList *devices)
gboolean new_device)
{ {
GList *iter; GList *iter;
for (iter = devices; iter; iter = iter->next) for (iter = devices; iter; iter = iter->next)
{ {
add_device_to_list (dialog, (PpPrintDevice *) iter->data, new_device); add_device_to_list (dialog, (PpPrintDevice *) iter->data);
} }
} }
@ -674,72 +669,59 @@ group_physical_devices_cb (gchar ***device_uris,
{ {
PpNewPrinterDialog *dialog = (PpNewPrinterDialog *) user_data; PpNewPrinterDialog *dialog = (PpNewPrinterDialog *) user_data;
PpNewPrinterDialogPrivate *priv = dialog->priv; PpNewPrinterDialogPrivate *priv = dialog->priv;
PpPrintDevice *device, *tmp; PpPrintDevice *device, *better_device;
GList *iter;
gint i, j; gint i, j;
if (device_uris) if (device_uris != NULL)
{ {
for (i = 0; device_uris[i]; i++) for (i = 0; device_uris[i] != NULL; i++)
{ {
if (device_uris[i]) /* Is there any device in this sublist? */
if (device_uris[i][0] != NULL)
{ {
for (j = 0; device_uris[i][j]; j++) for (j = 0; device_uris[i][j] != NULL; j++)
{ {
device = device_in_list (device_uris[i][j], priv->devices); device = device_in_list (device_uris[i][j], priv->devices);
if (device) if (device != NULL)
break; break;
} }
if (device) /* Is this sublist represented in the current list of devices? */
if (device != NULL)
{ {
for (j = 0; device_uris[i][j]; j++) /* Is there better device in the sublist? */
if (j != 0)
{ {
tmp = device_in_list (device_uris[i][j], priv->new_devices); better_device = device_in_list (device_uris[i][0], priv->local_cups_devices);
if (tmp) iter = g_list_find (priv->devices, device);
if (iter != NULL && better_device != NULL)
{ {
priv->new_devices = g_list_remove (priv->new_devices, tmp); iter->data = pp_print_device_copy (better_device);
pp_print_device_free (tmp); pp_print_device_free (device);
} }
} }
} }
else else
{ {
for (j = 0; device_uris[i][j]; j++) device = device_in_list (device_uris[i][0], priv->local_cups_devices);
if (device != NULL)
{ {
tmp = device_in_list (device_uris[i][j], priv->new_devices); priv->devices = g_list_append (priv->devices, pp_print_device_copy (device));
if (tmp)
{
priv->new_devices = g_list_remove (priv->new_devices, tmp);
if (j == 0)
{
priv->devices = g_list_append (priv->devices, tmp);
}
else
{
pp_print_device_free (tmp);
}
}
} }
} }
} }
} }
for (i = 0; device_uris[i]; i++) for (i = 0; device_uris[i] != NULL; i++)
{ g_strfreev (device_uris[i]);
for (j = 0; device_uris[i][j]; j++)
{
g_free (device_uris[i][j]);
}
g_free (device_uris[i]);
}
g_free (device_uris); g_free (device_uris);
} }
else else
{ {
priv->devices = g_list_concat (priv->devices, priv->new_devices); priv->devices = g_list_concat (priv->devices, priv->local_cups_devices);
priv->new_devices = NULL; priv->local_cups_devices = NULL;
} }
actualize_devices_list (dialog); actualize_devices_list (dialog);
@ -856,11 +838,9 @@ get_cups_devices_cb (GList *devices,
if (devices) if (devices)
{ {
add_devices_to_list (dialog, add_devices_to_list (dialog, devices);
devices,
TRUE);
length = g_list_length (priv->devices) + g_list_length (devices); length = g_list_length (priv->devices) + g_list_length (priv->local_cups_devices);
if (length > 0) if (length > 0)
{ {
all_devices = g_new0 (PpPrintDevice *, length); all_devices = g_new0 (PpPrintDevice *, length);
@ -880,7 +860,7 @@ get_cups_devices_cb (GList *devices,
} }
} }
for (iter = devices; iter != NULL; iter = iter->next) for (iter = priv->local_cups_devices; iter != NULL; iter = iter->next)
{ {
pp_device = (PpPrintDevice *) iter->data; pp_device = (PpPrintDevice *) iter->data;
if (pp_device != NULL) if (pp_device != NULL)
@ -994,9 +974,7 @@ get_snmp_devices_cb (GObject *source_object,
if (result->devices) if (result->devices)
{ {
add_devices_to_list (dialog, add_devices_to_list (dialog, result->devices);
result->devices,
FALSE);
} }
actualize_devices_list (dialog); actualize_devices_list (dialog);
@ -1049,9 +1027,7 @@ get_remote_cups_devices_cb (GObject *source_object,
if (result->devices) if (result->devices)
{ {
add_devices_to_list (dialog, add_devices_to_list (dialog, result->devices);
result->devices,
FALSE);
} }
actualize_devices_list (dialog); actualize_devices_list (dialog);
@ -1104,9 +1080,7 @@ get_samba_host_devices_cb (GObject *source_object,
if (result->devices) if (result->devices)
{ {
add_devices_to_list (dialog, add_devices_to_list (dialog, result->devices);
result->devices,
FALSE);
} }
actualize_devices_list (dialog); actualize_devices_list (dialog);
@ -1157,9 +1131,7 @@ get_samba_devices_cb (GObject *source_object,
if (result->devices) if (result->devices)
{ {
add_devices_to_list (dialog, add_devices_to_list (dialog, result->devices);
result->devices,
FALSE);
} }
actualize_devices_list (dialog); actualize_devices_list (dialog);
@ -1210,9 +1182,7 @@ get_jetdirect_devices_cb (GObject *source_object,
if (result->devices != NULL) if (result->devices != NULL)
{ {
add_devices_to_list (dialog, add_devices_to_list (dialog, result->devices);
result->devices,
FALSE);
} }
actualize_devices_list (dialog); actualize_devices_list (dialog);
@ -1264,9 +1234,7 @@ get_lpd_devices_cb (GObject *source_object,
if (result->devices != NULL) if (result->devices != NULL)
{ {
add_devices_to_list (dialog, add_devices_to_list (dialog, result->devices);
result->devices,
FALSE);
} }
actualize_devices_list (dialog); actualize_devices_list (dialog);
@ -1523,26 +1491,6 @@ search_address (const gchar *text,
iter = iter->next; iter = iter->next;
} }
iter = priv->new_devices;
while (iter)
{
device = iter->data;
if (device->acquisition_method == ACQUISITION_METHOD_REMOTE_CUPS_SERVER ||
device->acquisition_method == ACQUISITION_METHOD_SNMP ||
device->acquisition_method == ACQUISITION_METHOD_JETDIRECT ||
device->acquisition_method == ACQUISITION_METHOD_LPD ||
device->acquisition_method == ACQUISITION_METHOD_SAMBA_HOST)
{
tmp = iter;
iter = iter->next;
priv->new_devices = g_list_remove_link (priv->new_devices, tmp);
g_list_free_full (tmp, (GDestroyNotify) pp_print_device_free);
}
else
iter = iter->next;
}
if (text && text[0] != '\0') if (text && text[0] != '\0')
{ {
gchar *scheme = NULL; gchar *scheme = NULL;
@ -1986,7 +1934,7 @@ ppd_selection_cb (GtkDialog *_dialog,
priv->new_device->device_original_name = g_strdup (ppd_display_name); priv->new_device->device_original_name = g_strdup (ppd_display_name);
printer_name = canonicalize_device_name (priv->devices, printer_name = canonicalize_device_name (priv->devices,
priv->new_devices, priv->local_cups_devices,
priv->dests, priv->dests,
priv->num_of_dests, priv->num_of_dests,
priv->new_device); priv->new_device);

View file

@ -4101,7 +4101,7 @@ guess_device_hostname (PpPrintDevice *device)
gchar * gchar *
canonicalize_device_name (GList *devices, canonicalize_device_name (GList *devices,
GList *new_devices, GList *local_cups_devices,
cups_dest_t *dests, cups_dest_t *dests,
gint num_of_dests, gint num_of_dests,
PpPrintDevice *device) PpPrintDevice *device)
@ -4225,7 +4225,7 @@ canonicalize_device_name (GList *devices,
already_present = TRUE; already_present = TRUE;
} }
for (iter = new_devices; iter; iter = iter->next) for (iter = local_cups_devices; iter; iter = iter->next)
{ {
item = (PpPrintDevice *) iter->data; item = (PpPrintDevice *) iter->data;
if (g_strcmp0 (item->device_original_name, new_name) == 0) if (g_strcmp0 (item->device_original_name, new_name) == 0)