summaryrefslogtreecommitdiff
path: root/src/libtracker/tracker-tag.c
blob: b70f6a2150ad97cee09dcaadd3276472281e22d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Tracker - indexer and metadata database engine
 * Copyright (C) 2006, Mr Jamie McCracken (jamiemcc@gnome.org)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <config.h>

#include <locale.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gi18n.h>

#include "../libtracker/tracker.h" 

#ifdef OS_WIN32
#include "../trackerd/mingw-compat.h"
#endif

static gchar **add = NULL;
static gchar **delete = NULL;
static gchar **search = NULL;
static gchar **files = NULL;
static gboolean remove_all = FALSE;
static gboolean list = FALSE;

static GOptionEntry entries[] = {
	{"add", 'a', 0, G_OPTION_ARG_STRING_ARRAY, &add, N_("Add specified tag to a file"), N_("TAG")},
	{"remove", 'r', 0, G_OPTION_ARG_STRING_ARRAY, &delete, N_("Remove specified tag from a file"), N_("TAG")},
	{"remove-all", 'R', 0, G_OPTION_ARG_NONE, &remove_all, N_("Remove all tags from a file"), NULL},
	{"list", 'l', 0, G_OPTION_ARG_NONE, &list, N_("List all defined tags"), NULL},
	{"search", 's', 0, G_OPTION_ARG_STRING_ARRAY, &search, N_("Search for files with specified tag"), N_("TAG")},
	{G_OPTION_REMAINING, 0, G_OPTION_FLAG_FILENAME, G_OPTION_ARG_STRING_ARRAY, &files, N_("FILE..."), NULL},
	{NULL}
};


static void
get_meta_table_data (gpointer value)
		    
{
	char **meta, **meta_p;

	meta = (char **)value;

	int i = 0;
	for (meta_p = meta; *meta_p; meta_p++) {

		if (i == 0) {
			g_print ("%s : ", *meta_p);

		} else {
			g_print ("%s ", *meta_p);
		}
		i++;
	}
	g_print ("\n");
}

int 
main (int argc, char **argv) 
{
	TrackerClient *client = NULL;
	GOptionContext *context = NULL;
	GError *error = NULL;
	gchar *example;
	int i = 0, k;

	setlocale (LC_ALL, "");

	bindtextdomain (GETTEXT_PACKAGE, TRACKER_LOCALEDIR);
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        textdomain (GETTEXT_PACKAGE);

	/* Translators: this messagge will apper immediately after the  */
	/* usage string - Usage: COMMAND [OPTION]... <THIS_MESSAGE>     */
	context = g_option_context_new (_("FILE... - manipulate tags on files"));

	example = g_strconcat ("-a ", _("TAG"), " -a ", _("TAG"), " -a ", _("TAG"), NULL);


#ifdef HAVE_RECENT_GLIB
	/* Translators: this message will appear after the usage string */
	/* and before the list of options, showing an usage example.    */
	g_option_context_set_summary (context,
				      g_strconcat(_("To add, remove, or search for multiple tags "
						    "at the same time, join multiple options like:"), 
						  "\n\n\t", 
						  example, NULL));

#endif /* HAVE_RECENT_GLIB */

	g_option_context_add_main_entries (context, entries, NULL);
	g_option_context_parse (context, &argc, &argv, &error);

	g_option_context_free (context);
	g_free (example);
	
	if (error) {
		g_printerr ("%s: %s", argv[0], error->message);
		g_printerr ("\n");
		g_printerr (_("Try \"%s --help\" for more information."), argv[0]);
		g_printerr ("\n");
		return 1;
	}

	if (((add || delete || remove_all) && !files) || (remove_all && (add || delete)) || (search && files)) {
		g_printerr (_("%s: invalid arguments"), argv[0]);
		g_printerr ("\n");
		g_printerr (_("Try \"%s --help\" for more information."), argv[0]);
		g_printerr ("\n");
		return 1;
	}


	client = tracker_connect (FALSE);

	if (!client) {
		g_printerr (_("%s: no connection to tracker daemon"), argv[0]);
		g_printerr ("\n");
		g_printerr (_("Ensure \"trackerd\" is running before launch this command."));
		g_printerr ("\n");
		return 1;
	}


	if (files) {
		for (i = 0; files[i] != NULL; i++) {
                        char resolved_path[1024];
			gchar *tmp = realpath (files[i], resolved_path);
			if (tmp) {
				g_free (files[i]);
				files[i] = g_strdup (tmp);
			} else {
				g_printerr (_("%s: file %s not found"), argv[0], files[i]);
				g_printerr ("\n");
				for (k = i; files[k] != NULL; k++)
					files[k] = files[k+1];
				i--; // Make sure we run over this file again
			}
		}
	}

	if (add || delete || remove_all) {

		if (add)
			for (i = 0; add[i] != NULL; i++) {
				gchar *tmp = g_locale_to_utf8 (add[i], -1, NULL, NULL, NULL);
				g_free (add[i]);
				add[i] = tmp;
			}

		if (delete)
			for (i = 0; delete[i] != NULL; i++) {
				gchar *tmp = g_locale_to_utf8 (delete[i], -1, NULL, NULL, NULL);
				g_free (delete[i]);
				delete[i] = tmp;
			}


		for (i = 0; files[i] != NULL; i++) {

			if (remove_all) {
				tracker_keywords_remove_all (client, SERVICE_FILES, files[i], &error);

				if (error) {
					g_printerr (_("%s: internal tracker error: %s"), argv[0], error->message);
					g_printerr ("\n");
				}
			}

			if (add) {
				tracker_keywords_add (client, SERVICE_FILES, files[i], add, &error);

				if (error) {
					g_printerr (_("%s: internal tracker error: %s"), argv[0], error->message);
					g_printerr ("\n");
				}
			}

			if (delete) {
				tracker_keywords_remove (client, SERVICE_FILES, files[i], delete, &error);

				if (error) {
					g_printerr (_("%s: internal tracker error: %s"), argv[0], error->message);
					g_printerr ("\n");
				} 
			}

		}

	}

	if (((list && !files) || (!files && (!remove_all && !delete && !add))) && !search) {

		GPtrArray *out_array = NULL;

		out_array = tracker_keywords_get_list (client, SERVICE_FILES, &error);

		if (error)
			goto error;

		if (out_array) {
			g_ptr_array_foreach (out_array, (GFunc)get_meta_table_data, NULL);
			g_ptr_array_free (out_array, TRUE);
		}

	}

	if ((list && files) || (files && (!remove_all && !delete && !add))) {
			
		int i = 0;

		for (i = 0; files[i] != NULL; i++) {

			int j = 0;
			gchar **tags = tracker_keywords_get (client, SERVICE_FILES, files[i], &error);

			if (error) {
				g_printerr (_("%s: internal tracker error: %s"), argv[0], error->message);
				g_printerr ("\n");
			}

			if (!tags)
				continue;

			g_print ("%s:", files[i]);
			for (j = 0; tags[j] != NULL; j++)
				g_print (" %s", tags[j]);
			g_print ("\n");

			g_strfreev (tags);

		}

	}

	if (search) {

		int i = 0;

		for (i = 0; search[i] != NULL; i++) {
			gchar *tmp = g_locale_to_utf8 (search[i], -1, NULL, NULL, NULL);
			g_free (search[i]);
			search[i] = tmp;
		}

		gchar **results = tracker_keywords_search (client, -1, SERVICE_FILES, search, 0, 512, &error);

		if (error)
			goto error;

		if (!results) {
			/* FIXME!! coreutilus don't print anything on no-results */
			g_print (_("No results found matching your query"));
			g_print ("\n");
		}
		else
			for (i = 0; results[i] != NULL; i++)
				g_print ("%s\n", results[i]);

		g_strfreev (results);

	}

	tracker_disconnect (client);
	return 0;

error:
	g_printerr (_("%s: internal tracker error: %s"), argv[0], error->message);
	g_printerr ("\n");
	tracker_disconnect (client);
	return 1;	
}