1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2  *
3  * Copyright 2020 Canonical Ltd.
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 3 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  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #include <config.h>
22 #include <glib/gi18n.h>
23 
24 #include "pp-job-row.h"
25 #include "cc-printers-resources.h"
26 
27 struct _PpJobRow
28 {
29   GtkListBoxRow parent;
30 
31   GtkButton *pause_button;
32   GtkImage  *pause_image;
33   GtkLabel  *state_label;
34   GtkLabel  *title_label;
35 
36   PpJob *job;
37 };
38 
39 G_DEFINE_TYPE (PpJobRow, pp_job_row, GTK_TYPE_LIST_BOX_ROW)
40 
41 static void
42 pause_cb (PpJobRow *self)
43 {
44   pp_job_set_hold_until_async (self->job, pp_job_get_state (self->job) == IPP_JOB_HELD ? "no-hold" : "indefinite");
45   gtk_image_set_from_icon_name (self->pause_image,
46                                 pp_job_get_state (self->job) == IPP_JOB_HELD ?
47                                                   "media-playback-pause-symbolic" : "media-playback-start-symbolic",
48                                 GTK_ICON_SIZE_SMALL_TOOLBAR);
49 }
50 
51 static void
52 stop_cb (PpJobRow *self)
53 {
54   pp_job_cancel_purge_async (self->job, FALSE);
55 }
56 
57 static void
58 pp_job_row_dispose (GObject *object)
59 {
60   PpJobRow *self = PP_JOB_ROW (object);
61 
62   g_clear_object (&self->job);
63 
64   G_OBJECT_CLASS (pp_job_row_parent_class)->dispose (object);
65 }
66 
67 static void
68 pp_job_row_class_init (PpJobRowClass *klass)
69 {
70   GObjectClass *object_class = G_OBJECT_CLASS (klass);
71   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
72 
73   object_class->dispose = pp_job_row_dispose;
74 
75   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/printers/pp-job-row.ui");
76 
77   gtk_widget_class_bind_template_child (widget_class, PpJobRow, pause_button);
78   gtk_widget_class_bind_template_child (widget_class, PpJobRow, pause_image);
79   gtk_widget_class_bind_template_child (widget_class, PpJobRow, state_label);
80   gtk_widget_class_bind_template_child (widget_class, PpJobRow, title_label);
81 
82   gtk_widget_class_bind_template_callback (widget_class, pause_cb);
83   gtk_widget_class_bind_template_callback (widget_class, stop_cb);
84 }
85 
86 static void
87 pp_job_row_init (PpJobRow *self)
88 {
89   gtk_widget_init_template (GTK_WIDGET (self));
90 }
91 
92 PpJobRow *
93 pp_job_row_new (PpJob *job)
94 {
95   PpJobRow *self;
96   g_autofree gchar *state_string = NULL;
97 
98   self = g_object_new (PP_TYPE_JOB_ROW, NULL);
99 
100   self->job = g_object_ref (job);
101 
102   switch (pp_job_get_state (job))
103     {
104       case IPP_JOB_PENDING:
105         /* Translators: Job's state (job is waiting to be printed) */
106         state_string = g_strdup (C_("print job", "Pending"));
107         break;
108       case IPP_JOB_HELD:
109         if (pp_job_get_auth_info_required (job) == NULL)
110           {
111             /* Translators: Job's state (job is held for printing) */
112             state_string = g_strdup (C_("print job", "Paused"));
113           }
114         else
115           {
116             /* Translators: Job's state (job needs authentication to proceed further) */
117             state_string = g_strdup_printf ("<span foreground=\"#ff0000\">%s</span>", C_("print job", "Authentication required"));
118           }
119         break;
120       case IPP_JOB_PROCESSING:
121         /* Translators: Job's state (job is currently printing) */
122         state_string = g_strdup (C_("print job", "Processing"));
123         break;
124       case IPP_JOB_STOPPED:
125         /* Translators: Job's state (job has been stopped) */
126         state_string = g_strdup (C_("print job", "Stopped"));
127         break;
128       case IPP_JOB_CANCELED:
129         /* Translators: Job's state (job has been canceled) */
130         state_string = g_strdup (C_("print job", "Canceled"));
131         break;
132       case IPP_JOB_ABORTED:
133         /* Translators: Job's state (job has aborted due to error) */
134         state_string = g_strdup (C_("print job", "Aborted"));
135         break;
136       case IPP_JOB_COMPLETED:
137         /* Translators: Job's state (job has completed successfully) */
138         state_string = g_strdup (C_("print job", "Completed"));
139         break;
140     }
141 
142   gtk_label_set_text (self->title_label, pp_job_get_title (job));
143   gtk_label_set_markup (self->state_label, state_string);
144   gtk_widget_set_sensitive (GTK_WIDGET (self->pause_button), pp_job_get_auth_info_required (job) == NULL);
145   gtk_image_set_from_icon_name (self->pause_image,
146                                 pp_job_get_state (self->job) == IPP_JOB_HELD ?
147                                                   "media-playback-start-symbolic" : "media-playback-pause-symbolic",
148                                 GTK_ICON_SIZE_SMALL_TOOLBAR);
149 
150   return self;
151 }
152