1 /*
2  * Photos - access, organize and share your photos on GNOME
3  * Copyright © 2018 – 2019 Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20 
21 
22 #include "config.h"
23 
24 #include <gio/gio.h>
25 
26 #include "photos-base-manager.h"
27 #include "photos-filterable.h"
28 #include "photos-model-button.h"
29 #include "photos-search-context.h"
30 #include "photos-removable-device-widget.h"
31 #include "photos-removable-devices-button.h"
32 
33 
34 struct _PhotosRemovableDevicesButton
35 {
36   GtkBin parent_instance;
37   GtkWidget *device_button;
38   GtkWidget *device_widget;
39   GtkWidget *devices_button;
40   GtkWidget *devices_popover;
41   GtkWidget *devices_popover_grid;
42   GtkWidget *stack;
43   PhotosBaseManager *src_mngr;
44 };
45 
46 
47 G_DEFINE_TYPE (PhotosRemovableDevicesButton, photos_removable_devices_button, GTK_TYPE_BIN);
48 
49 
50 static void
photos_removable_devices_button_refresh_devices(PhotosRemovableDevicesButton * self)51 photos_removable_devices_button_refresh_devices (PhotosRemovableDevicesButton *self)
52 {
53   GList *sources = NULL;
54   gboolean visible = FALSE;
55   const gchar *action_id;
56   const gchar *action_namespace = "app";
57   guint i;
58   guint n_items;
59 
60   photos_removable_device_widget_set_source (PHOTOS_REMOVABLE_DEVICE_WIDGET (self->device_widget), NULL);
61   gtk_container_foreach (GTK_CONTAINER (self->devices_popover_grid), (GtkCallback) gtk_widget_destroy, NULL);
62 
63   n_items = g_list_model_get_n_items (G_LIST_MODEL (self->src_mngr));
64   for (i = 0; i < n_items; i++)
65     {
66       GMount *mount;
67       g_autoptr (PhotosSource) source = NULL;
68 
69       source = PHOTOS_SOURCE (g_list_model_get_object (G_LIST_MODEL (self->src_mngr), i));
70       mount = photos_source_get_mount (source);
71       if (mount == NULL)
72         continue;
73 
74       sources = g_list_prepend (sources, g_object_ref (source));
75     }
76 
77   if (sources == NULL)
78     goto out;
79 
80   action_id = photos_base_manager_get_action_id (self->src_mngr);
81 
82   if (sources->next == NULL) /* length == 1 */
83     {
84       PhotosSource *source = PHOTOS_SOURCE (sources->data);
85       const gchar *id;
86       g_autofree gchar *action_name = NULL;
87 
88       id = photos_filterable_get_id (PHOTOS_FILTERABLE (source));
89       gtk_actionable_set_action_target (GTK_ACTIONABLE (self->device_button), "s", id);
90       action_name = g_strconcat (action_namespace, ".", action_id, NULL);
91       gtk_actionable_set_action_name (GTK_ACTIONABLE (self->device_button), action_name);
92 
93       photos_removable_device_widget_set_source (PHOTOS_REMOVABLE_DEVICE_WIDGET (self->device_widget), source);
94       gtk_widget_show_all (self->device_widget);
95 
96       gtk_stack_set_visible_child (GTK_STACK (self->stack), self->device_button);
97     }
98   else
99     {
100       GList *l;
101 
102       for (l = sources; l != NULL; l = l->next)
103         {
104           GtkWidget *device_button;
105           GtkWidget *device_widget;
106           PhotosSource *source = PHOTOS_SOURCE (l->data);
107           const gchar *id;
108           g_autofree gchar *action_name = NULL;
109 
110           device_button = photos_model_button_new ();
111           action_name = g_strconcat (action_namespace, ".", action_id, NULL);
112           gtk_actionable_set_action_name (GTK_ACTIONABLE (device_button), action_name);
113           id = photos_filterable_get_id (PHOTOS_FILTERABLE (source));
114           gtk_actionable_set_action_target (GTK_ACTIONABLE (device_button), "s", id);
115           gtk_container_add (GTK_CONTAINER (self->devices_popover_grid), device_button);
116 
117           device_widget = photos_removable_device_widget_new (source);
118           gtk_container_add (GTK_CONTAINER (device_button), device_widget);
119 
120           gtk_widget_show_all (device_button);
121         }
122 
123       gtk_stack_set_visible_child (GTK_STACK (self->stack), self->devices_button);
124     }
125 
126   visible = TRUE;
127 
128  out:
129   gtk_widget_set_visible (self->stack, visible);
130   g_list_free_full (sources, g_object_unref);
131 }
132 
133 
134 static void
photos_removable_devices_button_dispose(GObject * object)135 photos_removable_devices_button_dispose (GObject *object)
136 {
137   PhotosRemovableDevicesButton *self = PHOTOS_REMOVABLE_DEVICES_BUTTON (object);
138 
139   g_clear_object (&self->src_mngr);
140 
141   G_OBJECT_CLASS (photos_removable_devices_button_parent_class)->dispose (object);
142 }
143 
144 
145 static void
photos_removable_devices_button_init(PhotosRemovableDevicesButton * self)146 photos_removable_devices_button_init (PhotosRemovableDevicesButton *self)
147 {
148   GApplication *app;
149   PhotosSearchContextState *state;
150 
151   gtk_widget_init_template (GTK_WIDGET (self));
152 
153   app = g_application_get_default ();
154   state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
155 
156   self->src_mngr = g_object_ref (state->src_mngr);
157   g_signal_connect_object (self->src_mngr,
158                            "object-added",
159                            G_CALLBACK (photos_removable_devices_button_refresh_devices),
160                            self,
161                            G_CONNECT_SWAPPED);
162   g_signal_connect_object (self->src_mngr,
163                            "object-removed",
164                            G_CALLBACK (photos_removable_devices_button_refresh_devices),
165                            self,
166                            G_CONNECT_SWAPPED);
167 
168   photos_removable_devices_button_refresh_devices (self);
169 }
170 
171 
172 static void
photos_removable_devices_button_class_init(PhotosRemovableDevicesButtonClass * class)173 photos_removable_devices_button_class_init (PhotosRemovableDevicesButtonClass *class)
174 {
175   GObjectClass *object_class = G_OBJECT_CLASS (class);
176   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
177 
178   object_class->dispose = photos_removable_devices_button_dispose;
179 
180   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Photos/removable-devices-button.ui");
181   gtk_widget_class_bind_template_child (widget_class, PhotosRemovableDevicesButton, device_button);
182   gtk_widget_class_bind_template_child (widget_class, PhotosRemovableDevicesButton, device_widget);
183   gtk_widget_class_bind_template_child (widget_class, PhotosRemovableDevicesButton, devices_button);
184   gtk_widget_class_bind_template_child (widget_class, PhotosRemovableDevicesButton, devices_popover);
185   gtk_widget_class_bind_template_child (widget_class, PhotosRemovableDevicesButton, devices_popover_grid);
186   gtk_widget_class_bind_template_child (widget_class, PhotosRemovableDevicesButton, stack);
187 }
188 
189 
190 GtkWidget *
photos_removable_devices_button_new(void)191 photos_removable_devices_button_new (void)
192 {
193   return g_object_new (PHOTOS_TYPE_REMOVABLE_DEVICES_BUTTON, NULL);
194 }
195