1 /*
2  * Photos - access, organize and share your photos on GNOME
3  * Copyright © 2014 – 2019 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 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 
19 /* Based on code from:
20  *   + Documents
21  */
22 
23 
24 #include "config.h"
25 
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 
29 #include "photos-print-notification.h"
30 #include "photos-notification-manager.h"
31 
32 
33 struct _PhotosPrintNotification
34 {
35   GtkGrid parent_instance;
36   GtkPrintOperation *print_op;
37   GtkWidget *ntfctn_mngr;
38   GtkWidget *spinner;
39   GtkWidget *status_label;
40   GtkWidget *stop_button;
41 };
42 
43 enum
44 {
45   PROP_0,
46   PROP_PRINT_OP
47 };
48 
49 
50 G_DEFINE_TYPE (PhotosPrintNotification, photos_print_notification, GTK_TYPE_GRID);
51 
52 
53 static void
photos_print_notification_begin_print(PhotosPrintNotification * self)54 photos_print_notification_begin_print (PhotosPrintNotification *self)
55 {
56   photos_notification_manager_add_notification (PHOTOS_NOTIFICATION_MANAGER (self->ntfctn_mngr),
57                                                 GTK_WIDGET (self));
58   gtk_spinner_start (GTK_SPINNER (self->spinner));
59 }
60 
61 
62 static void
photos_print_notification_status_changed(PhotosPrintNotification * self)63 photos_print_notification_status_changed (PhotosPrintNotification *self)
64 {
65   const gchar *status_str;
66   g_autofree gchar *job_name = NULL;
67   g_autofree gchar *status = NULL;
68 
69   status_str = gtk_print_operation_get_status_string (self->print_op);
70   g_object_get (self->print_op, "job-name", &job_name, NULL);
71   status = g_strdup_printf (_("Printing “%s”: %s"), job_name, status_str);
72   gtk_label_set_text (GTK_LABEL (self->status_label), status);
73 
74   if (gtk_print_operation_is_finished (self->print_op))
75     gtk_widget_destroy (GTK_WIDGET (self));
76 }
77 
78 
79 static void
photos_print_notification_stop_clicked(PhotosPrintNotification * self)80 photos_print_notification_stop_clicked (PhotosPrintNotification *self)
81 {
82   gtk_print_operation_cancel (self->print_op);
83   gtk_widget_destroy (GTK_WIDGET (self));
84 }
85 
86 
87 static void
photos_print_notification_constructed(GObject * object)88 photos_print_notification_constructed (GObject *object)
89 {
90   PhotosPrintNotification *self = PHOTOS_PRINT_NOTIFICATION (object);
91 
92   G_OBJECT_CLASS (photos_print_notification_parent_class)->constructed (object);
93 
94   g_signal_connect_object (self->print_op,
95                            "begin-print",
96                            G_CALLBACK (photos_print_notification_begin_print),
97                            self,
98                            G_CONNECT_SWAPPED);
99   g_signal_connect_object (self->print_op,
100                            "status-changed",
101                            G_CALLBACK (photos_print_notification_status_changed),
102                            self,
103                            G_CONNECT_SWAPPED);
104 }
105 
106 
107 static void
photos_print_notification_dispose(GObject * object)108 photos_print_notification_dispose (GObject *object)
109 {
110   PhotosPrintNotification *self = PHOTOS_PRINT_NOTIFICATION (object);
111 
112   g_clear_object (&self->print_op);
113   g_clear_object (&self->ntfctn_mngr);
114 
115   G_OBJECT_CLASS (photos_print_notification_parent_class)->dispose (object);
116 }
117 
118 
119 static void
photos_print_notification_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)120 photos_print_notification_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
121 {
122   PhotosPrintNotification *self = PHOTOS_PRINT_NOTIFICATION (object);
123 
124   switch (prop_id)
125     {
126     case PROP_PRINT_OP:
127       self->print_op = GTK_PRINT_OPERATION (g_value_dup_object (value));
128       break;
129 
130     default:
131       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132       break;
133     }
134 }
135 
136 
137 static void
photos_print_notification_init(PhotosPrintNotification * self)138 photos_print_notification_init (PhotosPrintNotification *self)
139 {
140   GtkWidget *image;
141 
142   self->ntfctn_mngr = photos_notification_manager_dup_singleton ();
143 
144   self->spinner = gtk_spinner_new ();
145   gtk_widget_set_size_request (self->spinner, 16, 16);
146   gtk_container_add (GTK_CONTAINER (self), self->spinner);
147 
148   self->status_label = gtk_label_new (NULL);
149   gtk_widget_set_halign (self->status_label, GTK_ALIGN_START);
150   gtk_widget_set_hexpand (self->status_label, TRUE);
151   gtk_container_add (GTK_CONTAINER (self), self->status_label);
152 
153   image = gtk_image_new_from_icon_name ("process-stop-symbolic", GTK_ICON_SIZE_INVALID);
154   gtk_widget_set_margin_bottom (image, 2);
155   gtk_widget_set_margin_top (image, 2);
156   gtk_image_set_pixel_size (GTK_IMAGE (image), 16);
157 
158   self->stop_button = gtk_button_new ();
159   gtk_widget_set_valign (self->stop_button, GTK_ALIGN_CENTER);
160   gtk_button_set_image (GTK_BUTTON (self->stop_button), image);
161   gtk_container_add (GTK_CONTAINER (self), self->stop_button);
162   g_signal_connect_swapped (self->stop_button,
163                             "clicked",
164                             G_CALLBACK (photos_print_notification_stop_clicked),
165                             self);
166 }
167 
168 
169 static void
photos_print_notification_class_init(PhotosPrintNotificationClass * class)170 photos_print_notification_class_init (PhotosPrintNotificationClass *class)
171 {
172   GObjectClass *object_class = G_OBJECT_CLASS (class);
173 
174   object_class->constructed = photos_print_notification_constructed;
175   object_class->dispose = photos_print_notification_dispose;
176   object_class->set_property = photos_print_notification_set_property;
177 
178   g_object_class_install_property (object_class,
179                                    PROP_PRINT_OP,
180                                    g_param_spec_object ("print-op",
181                                                         "GtkPrintOperation object",
182                                                         "The print operation in progress",
183                                                         GTK_TYPE_PRINT_OPERATION,
184                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
185 }
186 
187 
188 void
photos_print_notification_new(GtkPrintOperation * print_op)189 photos_print_notification_new (GtkPrintOperation *print_op)
190 {
191   g_object_new (PHOTOS_TYPE_PRINT_NOTIFICATION,
192                 "column-spacing", 12,
193                 "orientation", GTK_ORIENTATION_HORIZONTAL,
194                 "print-op", print_op,
195                 NULL);
196 }
197