1 /*
2  * Photos - access, organize and share your photos on GNOME
3  * Copyright © 2017 – 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 <gio/gio.h>
27 
28 #include "photos-base-manager.h"
29 #include "photos-filterable.h"
30 #include "photos-offset-import-controller.h"
31 #include "photos-query.h"
32 #include "photos-query-builder.h"
33 #include "photos-search-context.h"
34 #include "photos-source.h"
35 
36 
37 struct _PhotosOffsetImportController
38 {
39   PhotosOffsetController parent_instance;
40   PhotosBaseManager *src_mngr;
41 };
42 
43 
44 G_DEFINE_TYPE (PhotosOffsetImportController, photos_offset_import_controller, PHOTOS_TYPE_OFFSET_CONTROLLER);
45 
46 
47 static PhotosQuery *
photos_offset_import_controller_get_query(PhotosOffsetController * offset_cntrlr)48 photos_offset_import_controller_get_query (PhotosOffsetController *offset_cntrlr)
49 {
50   PhotosOffsetImportController *self = PHOTOS_OFFSET_IMPORT_CONTROLLER (offset_cntrlr);
51   GApplication *app;
52   GMount *mount;
53   g_autoptr (PhotosQuery) query = NULL;
54   PhotosQuery *ret_val = NULL;
55   PhotosSearchContextState *state;
56   PhotosSource *source;
57   const gchar *id;
58 
59   source = PHOTOS_SOURCE (photos_base_manager_get_active_object (self->src_mngr));
60   g_return_val_if_fail (PHOTOS_IS_SOURCE (source), NULL);
61 
62   id = photos_filterable_get_id (PHOTOS_FILTERABLE (source));
63   mount = photos_source_get_mount (source);
64   g_return_val_if_fail (g_strcmp0 (id, PHOTOS_SOURCE_STOCK_ALL) == 0 || G_IS_MOUNT (mount), NULL);
65 
66   app = g_application_get_default ();
67   state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
68 
69   if (mount != NULL)
70     query = photos_query_builder_count_query (state, PHOTOS_QUERY_FLAGS_IMPORT);
71   else
72     query = photos_query_new (state, "SELECT DISTINCT COUNT (?urn) WHERE { ?urn a rdfs:Resource FILTER (false) }");
73 
74   ret_val = g_steal_pointer (&query);
75   return ret_val;
76 }
77 
78 
79 static GObject *
photos_offset_import_controller_constructor(GType type,guint n_construct_params,GObjectConstructParam * construct_params)80 photos_offset_import_controller_constructor (GType type,
81                                              guint n_construct_params,
82                                              GObjectConstructParam *construct_params)
83 {
84   static GObject *self = NULL;
85 
86   if (self == NULL)
87     {
88       self = G_OBJECT_CLASS (photos_offset_import_controller_parent_class)->constructor (type,
89                                                                                          n_construct_params,
90                                                                                          construct_params);
91       g_object_add_weak_pointer (self, (gpointer) &self);
92       return self;
93     }
94 
95   return g_object_ref (self);
96 }
97 
98 
99 static void
photos_offset_import_controller_dispose(GObject * object)100 photos_offset_import_controller_dispose (GObject *object)
101 {
102   PhotosOffsetImportController *self = PHOTOS_OFFSET_IMPORT_CONTROLLER (object);
103 
104   g_clear_object (&self->src_mngr);
105 
106   G_OBJECT_CLASS (photos_offset_import_controller_parent_class)->dispose (object);
107 }
108 
109 
110 static void
photos_offset_import_controller_init(PhotosOffsetImportController * self)111 photos_offset_import_controller_init (PhotosOffsetImportController *self)
112 {
113   GApplication *app;
114   PhotosSearchContextState *state;
115 
116   app = g_application_get_default ();
117   state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
118 
119   self->src_mngr = g_object_ref (state->src_mngr);
120 }
121 
122 
123 static void
photos_offset_import_controller_class_init(PhotosOffsetImportControllerClass * class)124 photos_offset_import_controller_class_init (PhotosOffsetImportControllerClass *class)
125 {
126   GObjectClass *object_class = G_OBJECT_CLASS (class);
127   PhotosOffsetControllerClass *offset_controller_class = PHOTOS_OFFSET_CONTROLLER_CLASS (class);
128 
129   object_class->constructor = photos_offset_import_controller_constructor;
130   object_class->dispose = photos_offset_import_controller_dispose;
131   offset_controller_class->get_query = photos_offset_import_controller_get_query;
132 }
133 
134 
135 PhotosOffsetController *
photos_offset_import_controller_dup_singleton(void)136 photos_offset_import_controller_dup_singleton (void)
137 {
138   return g_object_new (PHOTOS_TYPE_OFFSET_IMPORT_CONTROLLER, NULL);
139 }
140