tracker-0.16.2/src/libtracker-extract/tracker-guarantee.c

No issues found

  1 /*
  2  * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
  3  *
  4  * This library is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2.1 of the License, or (at your option) any later version.
  8  *
  9  * This library 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 GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with this library; if not, write to the
 16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 17  * Boston, MA  02110-1301, USA.
 18  */
 19 
 20 #include "config.h"
 21 
 22 #include <string.h>
 23 
 24 #include <glib.h>
 25 
 26 #include <libtracker-common/tracker-file-utils.h>
 27 #include <libtracker-common/tracker-date-time.h>
 28 
 29 #include "tracker-guarantee.h"
 30 
 31 #ifdef GUARANTEE_METADATA
 32 
 33 static gchar *
 34 get_title_from_file (const gchar *uri)
 35 {
 36 	gchar *filename;
 37 	gchar *basename;
 38 	gchar *p;
 39 
 40 	filename = g_filename_from_uri (uri, NULL, NULL);
 41 	basename = g_filename_display_basename (filename);
 42 	g_free (filename);
 43 
 44 	p = strrchr (basename, '.');
 45 	if (p) {
 46                 if (p == basename) {
 47                         p = g_strdup (&basename[1]);
 48                         g_free (basename);
 49                         basename = p;
 50                 } else {
 51                         *p = '\0';
 52                 }
 53 	}
 54 
 55 	return g_strdelimit (basename, "_", ' ');
 56 }
 57 
 58 static gchar *
 59 get_date_from_file_mtime (const gchar *uri)
 60 {
 61 	gchar *date;
 62 	guint64 mtime;
 63 
 64 	mtime = tracker_file_get_mtime_uri (uri);
 65 
 66 	date = tracker_date_to_string ((time_t) mtime);
 67 
 68 	return date;
 69 }
 70 
 71 #endif /* GUARANTEE_METADATA */
 72 
 73 /**
 74  * tracker_guarantee_title_from_file:
 75  * @metadata: the metadata object to insert the data into
 76  * @key: the key to insert into @metadata
 77  * @current_value: the current data to check before looking at @uri.
 78  * @uri: a string representing a URI to use
 79  * @p_new_value: pointer to a string which receives the new title, or
 80  *             %NULL
 81  *
 82  * Checks @current_value to make sure it is sane (i.e. not %NULL or an
 83  * empty string). If it is, then @uri is parsed to guarantee a
 84  * metadata value for @key.
 85  *
 86  * Parses the file pointed to by @uri and uses the basename
 87  * (before the "." and extension of the file) as the title. If the
 88  * title has any "_" characters, they are also converted into spaces.
 89  *
 90  * Returns: %TRUE on success, otherwise %FALSE.
 91  *
 92  * Since: 0.10
 93  **/
 94 gboolean
 95 tracker_guarantee_title_from_file (TrackerSparqlBuilder  *metadata,
 96                                    const gchar           *key,
 97                                    const gchar           *current_value,
 98                                    const gchar           *uri,
 99                                    gchar                **p_new_value)
100 {
101 #ifdef GUARANTEE_METADATA
102 	g_return_val_if_fail (metadata != NULL, FALSE);
103 	g_return_val_if_fail (key != NULL, FALSE);
104 	g_return_val_if_fail (uri != NULL, FALSE);
105 
106 	tracker_sparql_builder_predicate (metadata, key);
107 
108 	if (current_value && *current_value != '\0') {
109 		tracker_sparql_builder_object_unvalidated (metadata, current_value);
110 
111 		if (p_new_value != NULL) {
112 			*p_new_value = g_strdup (current_value);
113 		}
114 	} else {
115 		gchar *value;
116 
117 		value = get_title_from_file (uri);
118 		tracker_sparql_builder_object_unvalidated (metadata, value);
119 
120 		if (p_new_value != NULL) {
121 			*p_new_value = value;
122 		} else {
123 			g_free (value);
124 		}
125 	}
126 #else  /* GUARANTEE_METADATA */
127 	if (current_value && *current_value != '\0') {
128 		tracker_sparql_builder_predicate (metadata, key);
129 		tracker_sparql_builder_object_unvalidated (metadata, current_value);
130 
131 		if (p_new_value != NULL) {
132 			*p_new_value = g_strdup (current_value);
133 		}
134 	}
135 #endif /* GUARANTEE_METADATA */
136 
137 	return TRUE;
138 }
139 
140 /**
141  * tracker_guarantee_date_from_file_mtime:
142  * @metadata: the metadata object to insert the data into
143  * @key: the key to insert into @metadata
144  * @current_value: the current data to check before looking at @uri
145  * @uri: a string representing a URI to use
146  *
147  * Checks @current_value to make sure it is sane (i.e. not %NULL or an
148  * empty string). If it is, then @uri is parsed to guarantee a
149  * metadata value for @key.
150  *
151  * When parsing @uri, stat() is called on the file to create a
152  * date based on the file's mtime.
153  *
154  * Returns: %TRUE on success, otherwise %FALSE.
155  *
156  * Since: 0.10
157  **/
158 gboolean
159 tracker_guarantee_date_from_file_mtime (TrackerSparqlBuilder *metadata,
160                                         const gchar          *key,
161                                         const gchar          *current_value,
162                                         const gchar          *uri)
163 {
164 #ifdef GUARANTEE_METADATA
165 	g_return_val_if_fail (metadata != NULL, FALSE);
166 	g_return_val_if_fail (key != NULL, FALSE);
167 	g_return_val_if_fail (uri != NULL, FALSE);
168 
169 	tracker_sparql_builder_predicate (metadata, key);
170 
171 	if (current_value && *current_value != '\0') {
172 		tracker_sparql_builder_object_unvalidated (metadata, current_value);
173 	} else {
174 		gchar *value;
175 
176 		value = get_date_from_file_mtime (uri);
177 		tracker_sparql_builder_object_unvalidated (metadata, value);
178 		g_free (value);
179 	}
180 #else  /* GUARANTEE_METADATA */
181 	if (current_value && *current_value != '\0') {
182 		tracker_sparql_builder_predicate (metadata, key);
183 		tracker_sparql_builder_object_unvalidated (metadata, current_value);
184 	}
185 #endif /* GUARANTEE_METADATA */
186 
187 	return TRUE;
188 }