tracker-0.16.2/src/plugins/nautilus/tracker-tags-utils.c

No issues found

  1 /*
  2  * Copyright (C) 2009, Debarshi Ray <debarshir@src.gnome.org>
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public License
  6  * as published by the Free Software Foundation; either version 2
  7  * of the License, or (at your option) any later version.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU General Public License
 15  * along with this program; if not, write to the Free Software
 16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 17  * 02110-1301, USA.
 18  */
 19 
 20 #include "config.h"
 21 
 22 #include <string.h>
 23 
 24 #include <libnautilus-extension/nautilus-file-info.h>
 25 
 26 #include <libtracker-sparql/tracker-sparql.h>
 27 
 28 #include "tracker-tags-utils.h"
 29 
 30 /* Copied from src/libtracker-common/tracker-utils.c */
 31 inline gboolean
 32 tracker_is_empty_string (const char *str)
 33 {
 34 	return str == NULL || str[0] == '\0';
 35 }
 36 
 37 /* Copied from src/libtracker-common/tracker-type-utils.c */
 38 gchar **
 39 tracker_glist_to_string_list_for_nautilus_files (GList *list)
 40 {
 41 	GList  *l;
 42 	gchar **strv;
 43 	gint    i;
 44 
 45 	strv = g_new0 (gchar *, g_list_length (list) + 1);
 46 
 47 	for (l = list, i = 0; l; l = l->next) {
 48 		if (!l->data) {
 49 			continue;
 50 		}
 51 
 52 		strv[i++] = nautilus_file_info_get_uri (NAUTILUS_FILE_INFO (l->data));
 53 	}
 54 
 55 	strv[i] = NULL;
 56 
 57 	return strv;
 58 }
 59 
 60 /* Copied from src/tracker-utils/tracker-tags.c */
 61 gchar *
 62 tracker_tags_get_filter_string (GStrv        files,
 63                                 const gchar *tag)
 64 {
 65 	GString *filter;
 66 	gint i, len;
 67 
 68 	if (!files) {
 69 		return NULL;
 70 	}
 71 
 72 	len = g_strv_length (files);
 73 
 74 	if (len < 1) {
 75 		return NULL;
 76 	}
 77 
 78 	filter = g_string_new ("");
 79 
 80 	g_string_append_printf (filter, "FILTER (");
 81 
 82 	if (tag) {
 83 		g_string_append (filter, "(");
 84 	}
 85 
 86 	for (i = 0; i < len; i++) {
 87 		g_string_append_printf (filter, "?f = \"%s\"", files[i]);
 88 
 89 		if (i < len - 1) {
 90 			g_string_append (filter, " || ");
 91 		}
 92 	}
 93 
 94 	if (tag) {
 95 		g_string_append_printf (filter, ") && ?t = <%s>", tag);
 96 	}
 97 
 98 	g_string_append (filter, ")");
 99 
100 	return g_string_free (filter, FALSE);
101 }
102 
103 gchar *
104 tracker_tags_escape_sparql_string (const gchar *str)
105 {
106 	gchar *escaped, *retval;
107 
108 	escaped = tracker_sparql_escape_string (str);
109 	retval = g_strdup_printf ("\"%s\"", escaped);
110 	g_free (escaped);
111 
112 	return retval;
113 }
114 
115 gchar *
116 tracker_tags_add_query (const gchar *tag_label)
117 {
118 	gchar *query;
119 	gchar *tag_label_escaped;
120 
121 	tag_label_escaped = tracker_tags_escape_sparql_string (tag_label);
122 	query = g_strdup_printf ("INSERT { "
123 	                         "  _:tag a nao:Tag ;"
124 	                         "  nao:prefLabel %s ."
125 	                         "} "
126 	                         "WHERE {"
127 	                         "  OPTIONAL {"
128 	                         "     ?tag a nao:Tag ;"
129 	                         "     nao:prefLabel %s"
130 	                         "  } ."
131 	                         "  FILTER (!bound(?tag)) "
132 	                         "}",
133 	                         tag_label_escaped,
134 	                         tag_label_escaped);
135 	g_free (tag_label_escaped);
136 
137 	return query;
138 }
139 
140 gchar *
141 tracker_tags_remove_query (const gchar *tag_label)
142 {
143 	gchar *query;
144 	gchar *tag_label_escaped;
145 
146 	tag_label_escaped = tracker_tags_escape_sparql_string (tag_label);
147 	query = g_strdup_printf ("DELETE { "
148 	                         "  ?tag a rdfs:Resource "
149 	                         "} "
150 	                         "WHERE {"
151 	                         "  ?tag a nao:Tag ;"
152 	                         "  nao:prefLabel %s "
153 	                         "}",
154 	                         tag_label_escaped);
155 	g_free (tag_label_escaped);
156 
157 	return query;
158 }