1 /*
2  * Photos - access, organize and share your photos on GNOME
3  * Copyright © 2012 – 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 "photos-base-manager.h"
27 #include "photos-query.h"
28 #include "photos-utils.h"
29 
30 
31 struct _PhotosQuery
32 {
33   GObject parent_instance;
34   PhotosSearchContextState *state;
35   PhotosSource *source;
36   gchar *sparql;
37   gchar *tag;
38 };
39 
40 enum
41 {
42   PROP_0,
43   PROP_SPARQL,
44   PROP_STATE
45 };
46 
47 
48 G_DEFINE_TYPE (PhotosQuery, photos_query, G_TYPE_OBJECT);
49 
50 
51 static void
photos_query_constructed(GObject * object)52 photos_query_constructed (GObject *object)
53 {
54   PhotosQuery *self = PHOTOS_QUERY (object);
55 
56   G_OBJECT_CLASS (photos_query_parent_class)->constructed (object);
57 
58   if (self->state != NULL)
59     {
60       PhotosSource *source;
61 
62       source = PHOTOS_SOURCE (photos_base_manager_get_active_object (self->state->src_mngr));
63       g_set_object (&self->source, source);
64     }
65 
66   self->state = NULL; /* We will not need it any more */
67 }
68 
69 
70 static void
photos_query_dispose(GObject * object)71 photos_query_dispose (GObject *object)
72 {
73   PhotosQuery *self = PHOTOS_QUERY (object);
74 
75   g_clear_object (&self->source);
76 
77   G_OBJECT_CLASS (photos_query_parent_class)->dispose (object);
78 }
79 
80 
81 static void
photos_query_finalize(GObject * object)82 photos_query_finalize (GObject *object)
83 {
84   PhotosQuery *self = PHOTOS_QUERY (object);
85 
86   g_free (self->sparql);
87   g_free (self->tag);
88 
89   G_OBJECT_CLASS (photos_query_parent_class)->finalize (object);
90 }
91 
92 
93 static void
photos_query_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)94 photos_query_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
95 {
96   PhotosQuery *self = PHOTOS_QUERY (object);
97 
98   switch (prop_id)
99     {
100     case PROP_SPARQL:
101       g_value_set_string (value, self->sparql);
102       break;
103 
104     default:
105       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106       break;
107     }
108 }
109 
110 
111 static void
photos_query_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)112 photos_query_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
113 {
114   PhotosQuery *self = PHOTOS_QUERY (object);
115 
116   switch (prop_id)
117     {
118     case PROP_SPARQL:
119       self->sparql = g_value_dup_string (value);
120       break;
121 
122     case PROP_STATE:
123       self->state = (PhotosSearchContextState *) g_value_get_pointer (value);
124       break;
125 
126     default:
127       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128       break;
129     }
130 }
131 
132 
133 static void
photos_query_init(PhotosQuery * self)134 photos_query_init (PhotosQuery *self)
135 {
136 }
137 
138 
139 static void
photos_query_class_init(PhotosQueryClass * class)140 photos_query_class_init (PhotosQueryClass *class)
141 {
142   GObjectClass *object_class = G_OBJECT_CLASS (class);
143 
144   object_class->constructed = photos_query_constructed;
145   object_class->dispose = photos_query_dispose;
146   object_class->finalize = photos_query_finalize;
147   object_class->get_property = photos_query_get_property;
148   object_class->set_property = photos_query_set_property;
149 
150   g_object_class_install_property (object_class,
151                                    PROP_SPARQL,
152                                    g_param_spec_string ("sparql",
153                                                         "SPARQL",
154                                                         "A SPARQL query that's meant to be sent to Tracker",
155                                                         NULL,
156                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
157 
158   g_object_class_install_property (object_class,
159                                    PROP_STATE,
160                                    g_param_spec_pointer ("state",
161                                                          "State",
162                                                          "The PhotosSearchContextState for this query",
163                                                          G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
164 }
165 
166 
167 PhotosQuery *
photos_query_new(PhotosSearchContextState * state,const gchar * sparql)168 photos_query_new (PhotosSearchContextState *state, const gchar *sparql)
169 {
170   g_return_val_if_fail (sparql != NULL && sparql[0] != '\0', NULL);
171   return g_object_new (PHOTOS_TYPE_QUERY, "state", state, "sparql", sparql, NULL);
172 }
173 
174 
175 const gchar *
photos_query_get_sparql(PhotosQuery * self)176 photos_query_get_sparql (PhotosQuery *self)
177 {
178   g_return_val_if_fail (PHOTOS_IS_QUERY (self), NULL);
179   return self->sparql;
180 }
181 
182 
183 PhotosSource *
photos_query_get_source(PhotosQuery * self)184 photos_query_get_source (PhotosQuery *self)
185 {
186   g_return_val_if_fail (PHOTOS_IS_QUERY (self), NULL);
187   return self->source;
188 }
189 
190 
191 const gchar *
photos_query_get_tag(PhotosQuery * self)192 photos_query_get_tag (PhotosQuery *self)
193 {
194   g_return_val_if_fail (PHOTOS_IS_QUERY (self), NULL);
195   return self->tag;
196 }
197 
198 
199 void
photos_query_set_tag(PhotosQuery * self,const gchar * tag)200 photos_query_set_tag (PhotosQuery *self, const gchar *tag)
201 {
202   g_return_if_fail (PHOTOS_IS_QUERY (self));
203   g_return_if_fail (tag != NULL && tag[0] != '\0');
204 
205   photos_utils_set_string (&self->tag, tag);
206 }
207