Make PpNewPrinterDialog a GtkDialog instead of GObject

This is accomplished by moving the calls to pp_printer_add_async
directly to CcPrintersPanel. pp_printer_delete_async calls are already
done directly in the CcPrintersPanel so there is consistency gained by
this implementation in addition to PpNewPrinterDialog actually being a
GtkDialog.

A pp_new_printer_dialog_get_new_print_device method has been added to
PpNewPrinterDialog to allow getting the PpPrintDevice selected by the
user to add. This can be called anytime after a response callback
with a GTK_RESPONSE_OK reponse_id.

PpNewPrinterDialog still does asynchronous operations to populate the
dialog, but the create dialog -> receive signal -> destroy dialog flow
can all be handled like a traditional GtkDialog without additional
callbacks or signalling.
This commit is contained in:
Brandon Nielsen 2021-06-21 18:57:48 -05:00 committed by Marek Kašík
parent aceaa40432
commit 179bda1b64
5 changed files with 381 additions and 495 deletions

View file

@ -40,10 +40,13 @@
#include "pp-cups.h" #include "pp-cups.h"
#include "pp-printer-entry.h" #include "pp-printer-entry.h"
#include "pp-job.h" #include "pp-job.h"
#include "pp-new-printer.h"
#include "cc-permission-infobar.h" #include "cc-permission-infobar.h"
#include "cc-util.h" #include "cc-util.h"
#include <gdk/gdkx.h>
#define RENEW_INTERVAL 500 #define RENEW_INTERVAL 500
#define SUBSCRIPTION_DURATION 600 #define SUBSCRIPTION_DURATION 600
@ -296,7 +299,6 @@ cc_printers_panel_dispose (GObject *object)
} }
g_clear_object (&self->cups); g_clear_object (&self->cups);
g_clear_object (&self->pp_new_printer_dialog);
g_clear_pointer (&self->new_printer_name, g_free); g_clear_pointer (&self->new_printer_name, g_free);
g_clear_pointer (&self->renamed_printer_name, g_free); g_clear_pointer (&self->renamed_printer_name, g_free);
g_clear_pointer (&self->old_printer_name, g_free); g_clear_pointer (&self->old_printer_name, g_free);
@ -900,42 +902,84 @@ actualize_printers_list (CcPrintersPanel *self)
} }
static void static void
new_printer_dialog_pre_response_cb (CcPrintersPanel *self, printer_add_async_cb (GObject *source_object,
const gchar *device_name, GAsyncResult *res,
const gchar *device_location, gpointer user_data)
const gchar *device_make_and_model,
gboolean is_network_device)
{ {
self->new_printer_name = g_strdup (device_name); CcPrintersPanel *self = (CcPrintersPanel*) user_data;
gboolean success;
g_autoptr(GError) error = NULL;
success = pp_new_printer_add_finish (PP_NEW_PRINTER (source_object), res, &error);
if (!success)
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
{
g_warning ("%s", error->message);
GtkWidget *message_dialog;
message_dialog = gtk_message_dialog_new (NULL,
0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
/* Translators: Addition of the new printer failed. */
_("Failed to add new printer."));
g_signal_connect (message_dialog,
"response",
G_CALLBACK (gtk_widget_destroy),
NULL);
gtk_widget_show (message_dialog);
}
}
actualize_printers_list (self); actualize_printers_list (self);
} }
static void static void
new_printer_dialog_response_cb (CcPrintersPanel *self, new_printer_dialog_response_cb (GtkDialog *_dialog,
gint response_id) gint response_id,
gpointer user_data)
{ {
if (self->pp_new_printer_dialog) CcPrintersPanel *self = (CcPrintersPanel*) user_data;
g_clear_object (&self->pp_new_printer_dialog); PpNewPrinterDialog *pp_new_printer_dialog = PP_NEW_PRINTER_DIALOG (_dialog);
g_autoptr(PpPrintDevice) new_device = NULL;
g_autoptr(PpNewPrinter) new_printer = NULL;
guint window_id = 0;
if (response_id == GTK_RESPONSE_REJECT) if (response_id == GTK_RESPONSE_OK) {
{ new_device = pp_new_printer_dialog_get_new_print_device (pp_new_printer_dialog);
GtkWidget *message_dialog; self->new_printer_name = g_strdup (pp_print_device_get_device_name (new_device));
actualize_printers_list (self);
message_dialog = gtk_message_dialog_new (NULL, window_id = (guint) GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (gtk_window_get_transient_for (GTK_WINDOW (pp_new_printer_dialog)))));
0,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
/* Translators: Addition of the new printer failed. */
_("Failed to add new printer."));
g_signal_connect (message_dialog,
"response",
G_CALLBACK (gtk_widget_destroy),
NULL);
gtk_widget_show (message_dialog);
}
actualize_printers_list (self); new_printer = pp_new_printer_new ();
g_object_set (new_printer,
"name", pp_print_device_get_device_name (new_device),
"original-name", pp_print_device_get_device_original_name (new_device),
"device-uri", pp_print_device_get_device_uri (new_device),
"device-id", pp_print_device_get_device_id (new_device),
"ppd-name", pp_print_device_get_device_ppd (new_device),
"ppd-file-name", pp_print_device_get_device_ppd (new_device),
"info", pp_print_device_get_device_info (new_device),
"location", pp_print_device_get_device_location (new_device),
"make-and-model", pp_print_device_get_device_make_and_model (new_device),
"host-name", pp_print_device_get_host_name (new_device),
"host-port", pp_print_device_get_host_port (new_device),
"is-network-device", pp_print_device_is_network_device (new_device),
"window-id", window_id,
NULL);
pp_new_printer_add_async (new_printer,
cc_panel_get_cancellable (CC_PANEL (self)),
printer_add_async_cb,
self);
}
gtk_widget_destroy (GTK_WIDGET (pp_new_printer_dialog));
self->pp_new_printer_dialog = NULL;
} }
static void static void
@ -944,21 +988,14 @@ printer_add_cb (CcPrintersPanel *self)
GtkWidget *toplevel; GtkWidget *toplevel;
toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self)); toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
self->pp_new_printer_dialog = PP_NEW_PRINTER_DIALOG ( self->pp_new_printer_dialog = pp_new_printer_dialog_new (self->all_ppds_list,
pp_new_printer_dialog_new (GTK_WINDOW (toplevel), new_printer_dialog_response_cb,
self->all_ppds_list)); self);
g_signal_connect_object (self->pp_new_printer_dialog, gtk_window_set_transient_for (GTK_WINDOW (self->pp_new_printer_dialog),
"pre-response", GTK_WINDOW (toplevel));
G_CALLBACK (new_printer_dialog_pre_response_cb),
self,
G_CONNECT_SWAPPED);
g_signal_connect_object (self->pp_new_printer_dialog, gtk_dialog_run (GTK_DIALOG (self->pp_new_printer_dialog));
"response",
G_CALLBACK (new_printer_dialog_response_cb),
self,
G_CONNECT_SWAPPED);
} }
static void static void

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<requires lib="gtk+" version="3.14"/> <requires lib="gtk+" version="3.14"/>
<object class="GtkListStore" id="devices-liststore"> <object class="GtkListStore" id="devices_liststore">
<columns> <columns>
<!-- column-name device_gicon --> <!-- column-name device_gicon -->
<column type="GIcon"/> <column type="GIcon"/>
@ -19,10 +19,10 @@
<column type="PpPrintDevice"/> <column type="PpPrintDevice"/>
</columns> </columns>
</object> </object>
<object class="GtkTreeModelFilter" id="devices-model-filter"> <object class="GtkTreeModelFilter" id="devices_model_filter">
<property name="child_model">devices-liststore</property> <property name="child_model">devices_liststore</property>
</object> </object>
<object class="GtkDialog" id="dialog"> <template class="PpNewPrinterDialog" parent="GtkDialog">
<property name="width_request">480</property> <property name="width_request">480</property>
<property name="height_request">490</property> <property name="height_request">490</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
@ -37,11 +37,11 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="show-close-button">False</property> <property name="show-close-button">False</property>
<child> <child>
<object class="GtkStack" id="headerbar-topleft-buttons"> <object class="GtkStack" id="headerbar_topleft_buttons">
<property name="visible">True</property> <property name="visible">True</property>
<property name="valign">center</property> <property name="valign">center</property>
<child> <child>
<object class="GtkButton" id="new-printer-cancel-button"> <object class="GtkButton" id="new_printer_cancel_button">
<property name="label" translatable="yes">_Cancel</property> <property name="label" translatable="yes">_Cancel</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
@ -53,7 +53,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkButton" id="go-back-button"> <object class="GtkButton" id="go_back_button">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
@ -72,11 +72,11 @@
</object> </object>
</child> </child>
<child> <child>
<object class="GtkStack" id="headerbar-topright-buttons"> <object class="GtkStack" id="headerbar_topright_buttons">
<property name="visible">True</property> <property name="visible">True</property>
<property name="valign">center</property> <property name="valign">center</property>
<child> <child>
<object class="GtkButton" id="new-printer-add-button"> <object class="GtkButton" id="new_printer_add_button">
<property name="label" translatable="yes">_Add</property> <property name="label" translatable="yes">_Add</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="sensitive">False</property> <property name="sensitive">False</property>
@ -92,7 +92,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkButton" id="unlock-button"> <object class="GtkButton" id="unlock_button">
<property name="label" translatable="yes" comments="Translators: This button opens authentication dialog for selected server.">_Unlock</property> <property name="label" translatable="yes" comments="Translators: This button opens authentication dialog for selected server.">_Unlock</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
@ -103,11 +103,11 @@
</style> </style>
</object> </object>
<packing> <packing>
<property name="name">unlock-button</property> <property name="name">unlock_button</property>
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkButton" id="authenticate-button"> <object class="GtkButton" id="authenticate_button">
<property name="label" translatable="yes" comments="Translators: This buttons submits the credentials for the selected server.">_Unlock</property> <property name="label" translatable="yes" comments="Translators: This buttons submits the credentials for the selected server.">_Unlock</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
@ -138,7 +138,7 @@
<property name="spacing">10</property> <property name="spacing">10</property>
<property name="border_width">0</property> <property name="border_width">0</property>
<child> <child>
<object class="GtkStack" id="dialog-stack"> <object class="GtkStack" id="dialog_stack">
<property name="visible">True</property> <property name="visible">True</property>
<property name="border_width">0</property> <property name="border_width">0</property>
<child> <child>
@ -162,10 +162,10 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<child> <child>
<object class="GtkTreeView" id="devices-treeview"> <object class="GtkTreeView" id="devices_treeview">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="model">devices-model-filter</property> <property name="model">devices_model_filter</property>
<property name="headers_visible">False</property> <property name="headers_visible">False</property>
<property name="enable-grid-lines">GTK_TREE_VIEW_GRID_LINES_HORIZONTAL</property> <property name="enable-grid-lines">GTK_TREE_VIEW_GRID_LINES_HORIZONTAL</property>
<child internal-child="selection"> <child internal-child="selection">
@ -274,7 +274,7 @@
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="border_width">4</property> <property name="border_width">4</property>
<child> <child>
<object class="GtkSearchEntry" id="search-entry"> <object class="GtkSearchEntry" id="search_entry">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="has_tooltip">True</property> <property name="has_tooltip">True</property>
@ -334,7 +334,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkLabel" id="authentication-title"> <object class="GtkLabel" id="authentication_title">
<property name="visible">True</property> <property name="visible">True</property>
<property name="label" translatable="yes">Authentication Required</property> <property name="label" translatable="yes">Authentication Required</property>
<property name="xalign">0</property> <property name="xalign">0</property>
@ -348,7 +348,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkLabel" id="authentication-text"> <object class="GtkLabel" id="authentication_text">
<property name="visible">True</property> <property name="visible">True</property>
<property name="wrap">True</property> <property name="wrap">True</property>
<property name="max_width_chars">36</property> <property name="max_width_chars">36</property>
@ -371,7 +371,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkEntry" id="username-entry"> <object class="GtkEntry" id="username_entry">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="invisible_char">●</property> <property name="invisible_char">●</property>
@ -394,7 +394,7 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkEntry" id="password-entry"> <object class="GtkEntry" id="password_entry">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="visibility">False</property> <property name="visibility">False</property>
@ -417,14 +417,14 @@
</object> </object>
</child> </child>
<action-widgets> <action-widgets>
<action-widget response="-6">new-printer-cancel-button</action-widget> <action-widget response="-6">new_printer_cancel_button</action-widget>
<action-widget response="-5">new-printer-add-button</action-widget> <action-widget response="-5">new_printer_add_button</action-widget>
</action-widgets> </action-widgets>
</object> </template>
<object class="GtkSizeGroup"> <object class="GtkSizeGroup">
<widgets> <widgets>
<widget name="new-printer-cancel-button"/> <widget name="new_printer_cancel_button"/>
<widget name="headerbar-topright-buttons"/> <widget name="headerbar_topright_buttons"/>
</widgets> </widgets>
</object> </object>
</interface> </interface>

File diff suppressed because it is too large Load diff

View file

@ -19,18 +19,19 @@
#pragma once #pragma once
#include "pp-utils.h"
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include "pp-utils.h"
G_BEGIN_DECLS G_BEGIN_DECLS
#define PP_TYPE_NEW_PRINTER_DIALOG (pp_new_printer_dialog_get_type ()) #define PP_TYPE_NEW_PRINTER_DIALOG (pp_new_printer_dialog_get_type ())
G_DECLARE_FINAL_TYPE (PpNewPrinterDialog, pp_new_printer_dialog, PP, NEW_PRINTER_DIALOG, GObject) G_DECLARE_FINAL_TYPE (PpNewPrinterDialog, pp_new_printer_dialog, PP, NEW_PRINTER_DIALOG, GtkDialog)
PpNewPrinterDialog *pp_new_printer_dialog_new (GtkWindow *parent, PpNewPrinterDialog *pp_new_printer_dialog_new (PPDList *ppd_list,
PPDList *ppd_list); UserResponseCallback user_callback,
void pp_new_printer_dialog_set_ppd_list (PpNewPrinterDialog *dialog, gpointer user_data);
PPDList *list); void pp_new_printer_dialog_set_ppd_list (PpNewPrinterDialog *dialog,
PPDList *list);
PpPrintDevice *pp_new_printer_dialog_get_new_print_device (PpNewPrinterDialog *dialog);
G_END_DECLS G_END_DECLS

View file

@ -30,9 +30,6 @@
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <cups/cups.h>
#include <cups/ppd.h>
#include "pp-ppd-selection-dialog.h" #include "pp-ppd-selection-dialog.h"
enum enum
@ -334,6 +331,7 @@ pp_ppd_selection_dialog_dispose (GObject *object)
g_clear_pointer (&self->ppd_name, g_free); g_clear_pointer (&self->ppd_name, g_free);
g_clear_pointer (&self->ppd_display_name, g_free); g_clear_pointer (&self->ppd_display_name, g_free);
g_clear_pointer (&self->manufacturer, g_free); g_clear_pointer (&self->manufacturer, g_free);
g_clear_pointer (&self->list, ppd_list_free);
G_OBJECT_CLASS (pp_ppd_selection_dialog_parent_class)->dispose (object); G_OBJECT_CLASS (pp_ppd_selection_dialog_parent_class)->dispose (object);
} }