1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2  *
3  * Copyright 2012  Red Hat, Inc,
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Marek Kasik <mkasik@redhat.com>
19  */
20 
21 #include "config.h"
22 
23 #include <unistd.h>
24 #include <stdlib.h>
25 
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <glib/gstdio.h>
29 #include <gtk/gtk.h>
30 #include <gdesktop-enums.h>
31 
32 #include <cups/cups.h>
33 
34 #include "list-box-helper.h"
35 #include "pp-jobs-dialog.h"
36 #include "pp-utils.h"
37 #include "pp-job.h"
38 #include "pp-job-row.h"
39 #include "pp-cups.h"
40 #include "pp-printer.h"
41 
42 #define EMPTY_TEXT "\xe2\x80\x94"
43 
44 #define CLOCK_SCHEMA "org.gnome.desktop.interface"
45 #define CLOCK_FORMAT_KEY "clock-format"
46 
47 struct _PpJobsDialog {
48   GtkDialog   parent_instance;
49 
50   GtkButton         *authenticate_button;
51   GtkMenuButton     *authenticate_jobs_button;
52   GtkLabel          *authenticate_jobs_label;
53   GtkInfoBar        *authentication_infobar;
54   GtkLabel          *authentication_label;
55   GtkEntry          *domain_entry;
56   GtkLabel          *domain_label;
57   GtkButton         *jobs_clear_all_button;
58   GtkListBox        *jobs_listbox;
59   GtkScrolledWindow *list_jobs_page;
60   GtkBox            *no_jobs_page;
61   GtkEntry          *password_entry;
62   GtkLabel          *password_label;
63   GtkStack          *stack;
64   GListStore        *store;
65   GtkEntry          *username_entry;
66   GtkLabel          *username_label;
67 
68   gchar *printer_name;
69 
70   gchar    **actual_auth_info_required;
71   gboolean   jobs_filled;
72   gboolean   pop_up_authentication_popup;
73 
74   GCancellable *get_jobs_cancellable;
75 };
76 
77 G_DEFINE_TYPE (PpJobsDialog, pp_jobs_dialog, GTK_TYPE_DIALOG)
78 
79 static gboolean
80 is_info_required (PpJobsDialog *self,
81                   const gchar  *info)
82 {
83   gint i;
84 
85   if (self->actual_auth_info_required == NULL)
86     return FALSE;
87 
88   for (i = 0; self->actual_auth_info_required[i] != NULL; i++)
89     if (g_strcmp0 (self->actual_auth_info_required[i], info) == 0)
90       return TRUE;
91 
92   return FALSE;
93 }
94 
95 static gboolean
96 is_domain_required (PpJobsDialog *self)
97 {
98   return is_info_required (self, "domain");
99 }
100 
101 static gboolean
102 is_username_required (PpJobsDialog *self)
103 {
104   return is_info_required (self, "username");
105 }
106 
107 static gboolean
108 is_password_required (PpJobsDialog *self)
109 {
110   return is_info_required (self, "password");
111 }
112 
113 static gboolean
114 auth_popup_filled (PpJobsDialog *self)
115 {
116   gboolean domain_required;
117   gboolean username_required;
118   gboolean password_required;
119   guint16  domain_length;
120   guint16  username_length;
121   guint16  password_length;
122 
123   domain_required = is_domain_required (self);
124   username_required = is_username_required (self);
125   password_required = is_password_required (self);
126 
127   domain_length = gtk_entry_get_text_length (self->domain_entry);
128   username_length = gtk_entry_get_text_length (self->username_entry);
129   password_length = gtk_entry_get_text_length (self->password_entry);
130 
131   return (!domain_required || domain_length > 0) &&
132          (!username_required || username_length > 0) &&
133          (!password_required || password_length > 0);
134 }
135 
136 static void
137 auth_entries_changed (PpJobsDialog *self)
138 {
139   gtk_widget_set_sensitive (GTK_WIDGET (self->authenticate_button), auth_popup_filled (self));
140 }
141 
142 static void
143 auth_entries_activated (PpJobsDialog *self)
144 {
145   if (auth_popup_filled (self))
146     gtk_button_clicked (self->authenticate_button);
147 }
148 
149 static void
150 authenticate_popover_update (PpJobsDialog *self)
151 {
152   gboolean   domain_required;
153   gboolean   username_required;
154   gboolean   password_required;
155 
156   domain_required = is_domain_required (self);
157   username_required = is_username_required (self);
158   password_required = is_password_required (self);
159 
160   gtk_widget_set_visible (GTK_WIDGET (self->domain_label), domain_required);
161   gtk_widget_set_visible (GTK_WIDGET (self->domain_entry), domain_required);
162   if (domain_required)
163     gtk_entry_set_text (self->domain_entry, "");
164 
165   gtk_widget_set_visible (GTK_WIDGET (self->username_label), username_required);
166   gtk_widget_set_visible (GTK_WIDGET (self->username_entry), username_required);
167   if (username_required)
168     gtk_entry_set_text (self->username_entry, cupsUser ());
169 
170   gtk_widget_set_visible (GTK_WIDGET (self->password_label), password_required);
171   gtk_widget_set_visible (GTK_WIDGET (self->password_entry), password_required);
172   if (password_required)
173     gtk_entry_set_text (self->password_entry, "");
174 
175   gtk_widget_set_sensitive (GTK_WIDGET (self->authenticate_button), FALSE);
176 }
177 
178 static GtkWidget *
179 create_listbox_row (gpointer item,
180                     gpointer user_data)
181 {
182   return GTK_WIDGET (pp_job_row_new (PP_JOB (item)));
183 }
184 
185 static void
186 pop_up_authentication_popup (PpJobsDialog *self)
187 {
188   if (self->actual_auth_info_required != NULL)
189     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->authenticate_jobs_button), TRUE);
190 }
191 
192 static void
193 update_jobs_list_cb (GObject      *source_object,
194                      GAsyncResult *result,
195                      gpointer      user_data)
196 {
197   PpJobsDialog        *self = user_data;
198   PpPrinter           *printer = PP_PRINTER (source_object);
199   g_autoptr(GError)    error = NULL;
200   g_autoptr(GPtrArray) jobs;
201   PpJob               *job;
202   gint                 num_of_auth_jobs = 0;
203   guint                i;
204 
205   g_list_store_remove_all (self->store);
206 
207   jobs = pp_printer_get_jobs_finish (printer, result, &error);
208   if (error != NULL)
209     {
210       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
211         {
212           g_warning ("Could not get jobs: %s", error->message);
213         }
214 
215       return;
216     }
217 
218   if (jobs->len > 0)
219     {
220       gtk_widget_set_sensitive (GTK_WIDGET (self->jobs_clear_all_button), TRUE);
221       gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->list_jobs_page));
222     }
223   else
224     {
225       gtk_widget_set_sensitive (GTK_WIDGET (self->jobs_clear_all_button), FALSE);
226       gtk_stack_set_visible_child (self->stack, GTK_WIDGET (self->no_jobs_page));
227     }
228 
229   for (i = 0; i < jobs->len; i++)
230     {
231       job = PP_JOB (g_ptr_array_index (jobs, i));
232 
233       g_list_store_append (self->store, g_object_ref (job));
234 
235       if (pp_job_get_auth_info_required (job) != NULL)
236         {
237           num_of_auth_jobs++;
238 
239           if (self->actual_auth_info_required == NULL)
240             self->actual_auth_info_required = g_strdupv (pp_job_get_auth_info_required (job));
241         }
242     }
243 
244   if (num_of_auth_jobs > 0)
245     {
246       g_autofree gchar *text = NULL;
247 
248       /* Translators: This label shows how many jobs of this printer needs to be authenticated to be printed. */
249       text = g_strdup_printf (ngettext ("%u Job Requires Authentication", "%u Jobs Require Authentication", num_of_auth_jobs), num_of_auth_jobs);
250       gtk_label_set_text (self->authenticate_jobs_label, text);
251 
252       gtk_widget_show (GTK_WIDGET (self->authentication_infobar));
253     }
254   else
255     {
256       gtk_widget_hide (GTK_WIDGET (self->authentication_infobar));
257     }
258 
259   authenticate_popover_update (self);
260 
261   g_clear_object (&self->get_jobs_cancellable);
262 
263   if (!self->jobs_filled)
264     {
265       if (self->pop_up_authentication_popup)
266         {
267           pop_up_authentication_popup (self);
268           self->pop_up_authentication_popup = FALSE;
269         }
270 
271       self->jobs_filled = TRUE;
272     }
273 }
274 
275 static void
276 update_jobs_list (PpJobsDialog *self)
277 {
278   g_autoptr(PpPrinter) printer = NULL;
279 
280   if (self->printer_name != NULL)
281     {
282       g_cancellable_cancel (self->get_jobs_cancellable);
283       g_clear_object (&self->get_jobs_cancellable);
284 
285       self->get_jobs_cancellable = g_cancellable_new ();
286 
287       printer = pp_printer_new (self->printer_name);
288       pp_printer_get_jobs_async (printer,
289                                  TRUE,
290                                  CUPS_WHICHJOBS_ACTIVE,
291                                  self->get_jobs_cancellable,
292                                  update_jobs_list_cb,
293                                  self);
294     }
295 }
296 
297 static void
298 on_clear_all_button_clicked (PpJobsDialog *self)
299 {
300   guint num_items;
301   guint i;
302 
303   num_items = g_list_model_get_n_items (G_LIST_MODEL (self->store));
304 
305   for (i = 0; i < num_items; i++)
306     {
307       PpJob *job = PP_JOB (g_list_model_get_item (G_LIST_MODEL (self->store), i));
308 
309       pp_job_cancel_purge_async (job, FALSE);
310     }
311 }
312 
313 static void
314 pp_job_authenticate_cb (GObject      *source_object,
315                         GAsyncResult *res,
316                         gpointer      user_data)
317 {
318   PpJobsDialog     *self = user_data;
319   gboolean          result;
320   g_autoptr(GError) error = NULL;
321   PpJob            *job = PP_JOB (source_object);
322 
323   result = pp_job_authenticate_finish (job, res, &error);
324   if (result)
325     {
326       pp_jobs_dialog_update (self);
327     }
328   else if (error != NULL)
329     {
330       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
331         {
332           g_warning ("Could not authenticate job: %s", error->message);
333         }
334     }
335 }
336 
337 static void
338 authenticate_button_clicked (PpJobsDialog *self)
339 {
340   PpJob        *job;
341   gchar       **auth_info;
342   guint         num_items;
343   gint          i;
344 
345   auth_info = g_new0 (gchar *, g_strv_length (self->actual_auth_info_required) + 1);
346   for (i = 0; self->actual_auth_info_required[i] != NULL; i++)
347     {
348       if (g_strcmp0 (self->actual_auth_info_required[i], "domain") == 0)
349         auth_info[i] = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->domain_entry)));
350       else if (g_strcmp0 (self->actual_auth_info_required[i], "username") == 0)
351         auth_info[i] = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->username_entry)));
352       else if (g_strcmp0 (self->actual_auth_info_required[i], "password") == 0)
353         auth_info[i] = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->password_entry)));
354     }
355 
356   num_items = g_list_model_get_n_items (G_LIST_MODEL (self->store));
357   for (i = 0; i < num_items; i++)
358     {
359       job = PP_JOB (g_list_model_get_item (G_LIST_MODEL (self->store), i));
360 
361       if (pp_job_get_auth_info_required (job) != NULL)
362         {
363           pp_job_authenticate_async (job, auth_info, NULL, pp_job_authenticate_cb, self);
364         }
365     }
366 
367   g_strfreev (auth_info);
368 }
369 
370 static gboolean
371 key_press_event_cb (GtkWidget   *widget,
372                     GdkEventKey *event,
373                     gpointer     user_data)
374 {
375   if (event->keyval == GDK_KEY_Escape)
376     gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_CLOSE);
377 
378   return FALSE;
379 }
380 
381 PpJobsDialog *
382 pp_jobs_dialog_new (const gchar *printer_name)
383 {
384   PpJobsDialog *self;
385   g_autofree gchar *text = NULL;
386   g_autofree gchar *title = NULL;
387 
388   self = g_object_new (PP_TYPE_JOBS_DIALOG,
389                        "use-header-bar", 1,
390                        NULL);
391 
392   self->printer_name = g_strdup (printer_name);
393   self->actual_auth_info_required = NULL;
394   self->jobs_filled = FALSE;
395   self->pop_up_authentication_popup = FALSE;
396 
397   /* connect signals */
398   g_signal_connect (self, "key-press-event", G_CALLBACK (key_press_event_cb), NULL);
399 
400   /* Translators: This is the printer name for which we are showing the active jobs */
401   title = g_strdup_printf (C_("Printer jobs dialog title", "%s — Active Jobs"), printer_name);
402   gtk_window_set_title (GTK_WINDOW (self), title);
403 
404   /* Translators: The printer needs authentication info to print. */
405   text = g_strdup_printf (_("Enter credentials to print from %s."), printer_name);
406   gtk_label_set_text (self->authentication_label, text);
407 
408   gtk_list_box_set_header_func (self->jobs_listbox,
409                                 cc_list_box_update_header_func, NULL, NULL);
410   self->store = g_list_store_new (pp_job_get_type ());
411   gtk_list_box_bind_model (self->jobs_listbox, G_LIST_MODEL (self->store),
412                            create_listbox_row, NULL, NULL);
413 
414   update_jobs_list (self);
415 
416   return self;
417 }
418 
419 void
420 pp_jobs_dialog_update (PpJobsDialog *self)
421 {
422   update_jobs_list (self);
423 }
424 
425 void
426 pp_jobs_dialog_authenticate_jobs (PpJobsDialog *self)
427 {
428   if (self->jobs_filled)
429     pop_up_authentication_popup (self);
430   else
431     self->pop_up_authentication_popup = TRUE;
432 }
433 
434 static void
435 pp_jobs_dialog_init (PpJobsDialog *dialog)
436 {
437   gtk_widget_init_template (GTK_WIDGET (dialog));
438 }
439 
440 static void
441 pp_jobs_dialog_dispose (GObject *object)
442 {
443   PpJobsDialog *self = PP_JOBS_DIALOG (object);
444 
445   g_cancellable_cancel (self->get_jobs_cancellable);
446   g_clear_object (&self->get_jobs_cancellable);
447   g_clear_pointer (&self->actual_auth_info_required, g_strfreev);
448   g_clear_pointer (&self->printer_name, g_free);
449 
450   G_OBJECT_CLASS (pp_jobs_dialog_parent_class)->dispose (object);
451 }
452 
453 static void
454 pp_jobs_dialog_class_init (PpJobsDialogClass *klass)
455 {
456   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
457   GObjectClass   *object_class = G_OBJECT_CLASS (klass);
458 
459   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/printers/pp-jobs-dialog.ui");
460 
461   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, authenticate_button);
462   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, authenticate_jobs_button);
463   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, authenticate_jobs_label);
464   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, authentication_infobar);
465   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, authentication_label);
466   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, domain_entry);
467   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, domain_label);
468   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, jobs_clear_all_button);
469   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, jobs_listbox);
470   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, list_jobs_page);
471   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, no_jobs_page);
472   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, password_entry);
473   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, password_label);
474   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, stack);
475   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, username_entry);
476   gtk_widget_class_bind_template_child (widget_class, PpJobsDialog, username_label);
477 
478   gtk_widget_class_bind_template_callback (widget_class, authenticate_button_clicked);
479   gtk_widget_class_bind_template_callback (widget_class, on_clear_all_button_clicked);
480   gtk_widget_class_bind_template_callback (widget_class, auth_entries_activated);
481   gtk_widget_class_bind_template_callback (widget_class, auth_entries_changed);
482 
483   object_class->dispose = pp_jobs_dialog_dispose;
484 }
485