1 /*
2  * Photos - access, organize and share your photos on GNOME
3  * Copyright © 2013 – 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 <tracker-sparql.h>
28 
29 #include "photos-search-controller.h"
30 
31 
32 struct _PhotosSearchController
33 {
34   GObject parent_instance;
35   gchar *str;
36 };
37 
38 enum
39 {
40   SEARCH_STRING_CHANGED,
41   LAST_SIGNAL
42 };
43 
44 static guint signals[LAST_SIGNAL] = { 0 };
45 
46 
47 G_DEFINE_TYPE (PhotosSearchController, photos_search_controller, G_TYPE_OBJECT);
48 
49 
50 static void
photos_search_controller_finalize(GObject * object)51 photos_search_controller_finalize (GObject *object)
52 {
53   PhotosSearchController *self = PHOTOS_SEARCH_CONTROLLER (object);
54 
55   g_free (self->str);
56 
57   G_OBJECT_CLASS (photos_search_controller_parent_class)->finalize (object);
58 }
59 
60 
61 static void
photos_search_controller_init(PhotosSearchController * self)62 photos_search_controller_init (PhotosSearchController *self)
63 {
64   self->str = g_strdup ("");
65 }
66 
67 
68 static void
photos_search_controller_class_init(PhotosSearchControllerClass * class)69 photos_search_controller_class_init (PhotosSearchControllerClass *class)
70 {
71   GObjectClass *object_class = G_OBJECT_CLASS (class);
72 
73   object_class->finalize = photos_search_controller_finalize;
74 
75   signals[SEARCH_STRING_CHANGED] = g_signal_new ("search-string-changed",
76                                                   G_TYPE_FROM_CLASS (class),
77                                                   G_SIGNAL_RUN_LAST,
78                                                   0,
79                                                   NULL, /*accumulator */
80                                                   NULL, /*accu_data */
81                                                   g_cclosure_marshal_VOID__STRING,
82                                                   G_TYPE_NONE,
83                                                   1,
84                                                   G_TYPE_STRING);
85 }
86 
87 
88 PhotosSearchController *
photos_search_controller_new(void)89 photos_search_controller_new (void)
90 {
91   return g_object_new (PHOTOS_TYPE_SEARCH_CONTROLLER, NULL);
92 }
93 
94 
95 const gchar *
photos_search_controller_get_string(PhotosSearchController * self)96 photos_search_controller_get_string (PhotosSearchController *self)
97 {
98   return self->str;
99 }
100 
101 
102 gchar **
photos_search_controller_get_terms(PhotosSearchController * self)103 photos_search_controller_get_terms (PhotosSearchController *self)
104 {
105   gchar *escaped_str;
106   gchar *str;
107   gchar **terms;
108 
109   escaped_str = tracker_sparql_escape_string (self->str);
110   str = g_utf8_casefold (escaped_str, -1);
111   /* TODO: find out what str.replace(/ + /g, ' ') does */
112   terms = g_strsplit (str, " ", -1);
113   g_free (str);
114   g_free (escaped_str);
115   return terms;
116 }
117 
118 
119 void
photos_search_controller_set_string(PhotosSearchController * self,const gchar * str)120 photos_search_controller_set_string (PhotosSearchController *self, const gchar *str)
121 {
122   if (g_strcmp0 (self->str, str) == 0)
123     return;
124 
125   g_free (self->str);
126   self->str = g_strdup (str);
127   g_signal_emit (self, signals[SEARCH_STRING_CHANGED], 0, self->str);
128 }
129