summaryrefslogtreecommitdiff
path: root/src/plugins/tracker/totem-tracker-widget.c
blob: 45941a7b15bced8184f14ffaeeab1c16da9d6830 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
 *
 * The _get_result_count method taken from the tracker-client.h file from libtracker
 * Copyright (C) 2006, Mr Jamie McCracken (jamiemcc@gnome.org)
 * Copyright (C) 2007 Javier Goday <jgoday@gmail.com>
 *
 * 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.
 *
 * Author : Javier Goday <jgoday@gmail.com>
 */

#include "config.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libintl.h>
#include <tracker.h>
#include <tracker-client.h>
#include <glib/gi18n-lib.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libgnomeui/gnome-thumbnail.h>

#include "totem-tracker-widget.h"
#include "totem-cell-renderer-video.h"
#include "totem-playlist.h"
#include "totem-video-list.h"
#include "totem-interface.h"

#define TRACKER_SERVICE			"org.freedesktop.Tracker"
#define TRACKER_OBJECT			"/org/freedesktop/tracker"
#define TRACKER_INTERFACE		"org.freedesktop.Tracker.Search"
#define TOTEM_TRACKER_MAX_RESULTS_SIZE	20

G_DEFINE_TYPE (TotemTrackerWidget, totem_tracker_widget, GTK_TYPE_EVENT_BOX)

struct TotemTrackerWidgetPrivate {
	GtkWidget *search_entry;
	GtkWidget *search_button;
	GtkWidget *status_label;

	GtkWidget *next_button;
	GtkWidget *previous_button;
	GtkWidget *page_selector;

	int total_result_count;
	int current_result_page;

	GtkListStore *result_store;
	TotemVideoList *result_list;
};

enum {
	IMAGE_COLUMN,
	FILE_COLUMN,
	NAME_COLUMN,
	N_COLUMNS
};

enum {
	PROP_0,
	PROP_TOTEM
};

static void totem_tracker_widget_class_init	(TotemTrackerWidgetClass *klass);
static void totem_tracker_widget_init		(TotemTrackerWidget	 *widget);
static void totem_tracker_widget_dispose	(GObject *object);
static void totem_tracker_widget_set_property	(GObject *object,
						 guint property_id,
						 const GValue *value,
						 GParamSpec *pspec);
static void page_selector_value_changed_cb (GtkSpinButton *self, TotemTrackerWidget *widget);

static void
totem_tracker_widget_class_init (TotemTrackerWidgetClass *klass)
{
	GObjectClass *object_class;

	g_type_class_add_private (klass, sizeof (TotemTrackerWidgetPrivate));

	object_class = G_OBJECT_CLASS (klass);
	object_class->dispose = totem_tracker_widget_dispose;
	object_class->set_property = totem_tracker_widget_set_property;

	g_object_class_install_property (object_class, PROP_TOTEM,
					 g_param_spec_object ("totem", NULL, NULL,
							      TOTEM_TYPE_OBJECT, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
}

static void
totem_tracker_widget_dispose (GObject *object)
{
	TotemTrackerWidget *self = TOTEM_TRACKER_WIDGET (object);

	if (self->priv->result_store != NULL) {
		g_object_unref (self->priv->result_store);
		self->priv->result_store = NULL;
	}

	G_OBJECT_CLASS (totem_tracker_widget_parent_class)->dispose (object);
}

static void
totem_tracker_widget_set_property (GObject *object,
				   guint property_id,
				   const GValue *value,
				   GParamSpec *pspec)
{
	TotemTrackerWidget *widget;

	widget = TOTEM_TRACKER_WIDGET (object);

	switch (property_id)
	{
	case PROP_TOTEM:
		widget->totem = g_object_ref (g_value_get_object (value));
		g_object_set (G_OBJECT (widget->priv->result_list), "totem", widget->totem, NULL);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
populate_result (TotemTrackerWidget *widget, char *result)
{
	GtkTreeIter iter;
	GnomeVFSFileInfo *info;
	GnomeVFSResult vfs_result;
	GdkPixbuf *thumbnail = NULL;
	char *thumbnail_path, *file_uri;

	info = gnome_vfs_file_info_new ();
	vfs_result = gnome_vfs_get_file_info (result, info, GNOME_VFS_FILE_INFO_NAME_ONLY);

	if (vfs_result == GNOME_VFS_OK) {
		gtk_list_store_append (GTK_LIST_STORE (widget->priv->result_store), &iter);  /* Acquire an iterator */
		file_uri = gnome_vfs_get_uri_from_local_path (result);
		thumbnail_path = gnome_thumbnail_path_for_uri (file_uri, GNOME_THUMBNAIL_SIZE_NORMAL);

		if (thumbnail_path != NULL)
			thumbnail = gdk_pixbuf_new_from_file (thumbnail_path, NULL);

		gtk_list_store_set (GTK_LIST_STORE (widget->priv->result_store), &iter,
				    IMAGE_COLUMN, thumbnail,
				    FILE_COLUMN, file_uri,
				    NAME_COLUMN, info->name,
				    -1);

		g_free (thumbnail_path);
		g_free (file_uri);
		if (thumbnail != NULL)
			g_object_unref (thumbnail);
	} else {
		/* Display an error */
		char *message = g_strdup_printf (_("Could not get metadata for file %s."), result);
		totem_interface_error_blocking	(_("File Error"), message, NULL);
		g_free (message);
	}

	g_free (info);
}

static int
get_search_count (TrackerClient *client, const char *search)
{
	GError *error = NULL;
	int count = 0;

	dbus_g_proxy_call (client->proxy_search, "GetHitCount", &error, G_TYPE_STRING, "Videos", 
			   G_TYPE_STRING, search,
			   G_TYPE_INVALID, G_TYPE_INT, &count, G_TYPE_INVALID);

	if (error) {
		g_warning (error->message);
		g_error_free (error);
		return -1;
	}

	return count;
}

struct SearchResultsData {
	TotemTrackerWidget *widget;
	char *search_text;
	TrackerClient *client;
};

static void
search_results_cb (char **result, GError *error, gpointer userdata)
{
	struct SearchResultsData *data = (struct SearchResultsData*) userdata;
	char *label;
	int i, next_page;
	TotemTrackerWidgetPrivate *priv = data->widget->priv;

	if (!error && result) {
		for (i = 0; result [i] != NULL; i++)
			populate_result (data->widget, result [i]);
	
		next_page = (priv->current_result_page + 1) * TOTEM_TRACKER_MAX_RESULTS_SIZE;

		/* Set the new range on the page selector's adjustment and ensure the current page is correct */
		gtk_spin_button_set_range (GTK_SPIN_BUTTON (priv->page_selector), 1,
					   priv->total_result_count / TOTEM_TRACKER_MAX_RESULTS_SIZE + 1);
		priv->current_result_page = gtk_spin_button_get_value (GTK_SPIN_BUTTON (priv->page_selector)) - 1;

		if (priv->total_result_count == 0) {
			gtk_label_set_text (GTK_LABEL (priv->status_label), _("No results"));
		} else {
			/* Translators:
			 * This is used to show which items are listed in the list view, for example:
			 * Showing 10-20 of 128 matches
			 * This is similar to what web searches use, eg. Google on the top-right of their search results page show:
			 * Personalized Results 1 - 10 of about 4,130,000 for foobar */
			label = g_strdup_printf (ngettext ("Showing %i - %i of %i match", "Showing %i - %i of %i matches", priv->total_result_count),
						 priv->current_result_page * TOTEM_TRACKER_MAX_RESULTS_SIZE, 
						 next_page > priv->total_result_count ? priv->total_result_count : next_page,
						 priv->total_result_count);
			gtk_label_set_text (GTK_LABEL (priv->status_label), label);
			g_free (label);
		}

		/* Enable or disable the pager buttons */
		if (priv->current_result_page < priv->total_result_count / TOTEM_TRACKER_MAX_RESULTS_SIZE) {
			gtk_widget_set_sensitive (GTK_WIDGET (priv->page_selector), TRUE);
			gtk_widget_set_sensitive (GTK_WIDGET (priv->next_button), TRUE);
		}

		if (priv->current_result_page > 0) {
			gtk_widget_set_sensitive (GTK_WIDGET (priv->page_selector), TRUE);
			gtk_widget_set_sensitive (GTK_WIDGET (priv->previous_button), TRUE);
		}

		g_signal_handlers_unblock_by_func (priv->page_selector, page_selector_value_changed_cb, data->widget);
	} else {
		g_warning ("Error getting the search results for '%s': %s", data->search_text, error->message ? error->message : "No reason");
	}

	g_free (data->search_text);
	tracker_disconnect (data->client);
	g_free (data);
}

static void
do_search (TotemTrackerWidget *widget)
{
	TrackerClient *client;
	struct SearchResultsData *data;

	/* Clear the list store */
	gtk_list_store_clear (GTK_LIST_STORE (widget->priv->result_store));

	/* Stop pagination temporarily */
	gtk_widget_set_sensitive (GTK_WIDGET (widget->priv->previous_button), FALSE);
	gtk_widget_set_sensitive (GTK_WIDGET (widget->priv->page_selector), FALSE);
	gtk_widget_set_sensitive (GTK_WIDGET (widget->priv->next_button), FALSE);

	/* Stop after clearing the list store if they're just emptying the search entry box */
	if (strcmp (gtk_entry_get_text (GTK_ENTRY (widget->priv->search_entry)), "") == 0) {
		gtk_label_set_text (GTK_LABEL (widget->priv->status_label), _("No results"));
		return;
	}

	g_signal_handlers_block_by_func (widget->priv->page_selector, page_selector_value_changed_cb, widget);

	/* Get the tracker client */
	client = tracker_connect (TRUE);
	if (!client) {
		g_warning ("Error trying to get the Tracker client.");
		return;
	}

	data = g_new0 (struct SearchResultsData, 1);
	data->widget = widget;
	data->client = client;

	/* Search text */
	data->search_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (widget->priv->search_entry)));

	widget->priv->total_result_count = get_search_count (client, data->search_text);

	tracker_search_text_async (data->client, -1, SERVICE_VIDEOS, data->search_text, 
				   widget->priv->current_result_page * TOTEM_TRACKER_MAX_RESULTS_SIZE, 
				   TOTEM_TRACKER_MAX_RESULTS_SIZE, 
				   search_results_cb, data);
}

static void
go_next (GtkWidget *button, TotemTrackerWidget *widget)
{
	if (widget->priv->current_result_page < widget->priv->total_result_count / TOTEM_TRACKER_MAX_RESULTS_SIZE)
		gtk_spin_button_spin (GTK_SPIN_BUTTON (widget->priv->page_selector), GTK_SPIN_STEP_FORWARD, 1);

	/* do_search is called via page_selector_value_changed_cb */
}

static void
go_previous (GtkWidget *button, TotemTrackerWidget *widget)
{
	if (widget->priv->current_result_page > 0)
		gtk_spin_button_spin (GTK_SPIN_BUTTON (widget->priv->page_selector), GTK_SPIN_STEP_BACKWARD, 1);

	/* do_search is called via page_selector_value_changed_cb */
}

static void
page_selector_value_changed_cb (GtkSpinButton *self, TotemTrackerWidget *widget)
{
	widget->priv->current_result_page = gtk_spin_button_get_value (self) - 1;
	do_search (widget);
}

static void
new_search (GtkButton *button, TotemTrackerWidget *widget)
{
	/* Reset from the last search */
	g_signal_handlers_block_by_func (widget->priv->page_selector, page_selector_value_changed_cb, widget);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget->priv->page_selector), 1);
	g_signal_handlers_unblock_by_func (widget->priv->page_selector, page_selector_value_changed_cb, widget);

	do_search (widget);
}

static void
init_result_list (TotemTrackerWidget *widget)
{
	/* Initialize the store result list */
	widget->priv->result_store = gtk_list_store_new (N_COLUMNS, 
							 GDK_TYPE_PIXBUF,
							 G_TYPE_STRING,
							 G_TYPE_STRING);

	/* Create the gtktreewidget to show the results */
	widget->priv->result_list = g_object_new (TOTEM_TYPE_VIDEO_LIST,
						  "mrl-column", FILE_COLUMN,
						  "tooltip-column", NAME_COLUMN,
						  NULL);

	gtk_tree_view_set_model (GTK_TREE_VIEW (widget->priv->result_list), 
				 GTK_TREE_MODEL (widget->priv->result_store));
}

static void
initialize_list_store (TotemTrackerWidget *widget) 
{
	TotemCellRendererVideo *renderer;
	GtkTreeViewColumn *column;

	/* Initialise the columns of the result list */
	renderer = totem_cell_renderer_video_new (TRUE);
	column = gtk_tree_view_column_new_with_attributes (_("Search Results"), GTK_CELL_RENDERER (renderer),
							   "thumbnail", IMAGE_COLUMN, 
							   "title", NAME_COLUMN, 
							   NULL);

	gtk_tree_view_append_column (GTK_TREE_VIEW (widget->priv->result_list), column);
}

static void
totem_tracker_widget_init (TotemTrackerWidget *widget)
{
	GtkWidget *v_box;		/* the main vertical box of the widget */
	GtkWidget *pager_box;		/* box that holds the next and previous buttons */
	GtkWidget *search_box;		/* the search box contains the search entry and the search button */
	GtkScrolledWindow *scroll;	/* make the result list scrollable */
	GtkAdjustment *adjust;		/* adjustment for the page selector spin button */

	widget->priv = G_TYPE_INSTANCE_GET_PRIVATE (widget, TOTEM_TYPE_TRACKER_WIDGET, TotemTrackerWidgetPrivate);

	init_result_list (widget);

	v_box = gtk_vbox_new (FALSE, 2);

	/* Search entry */
	widget->priv->search_entry = gtk_entry_new ();

	/* Search button */
	widget->priv->search_button = gtk_button_new_from_stock (GTK_STOCK_FIND);

	/* Add the search entry and button to the search box,
	   and add the search box to the main vertical box */
	search_box = gtk_hbox_new (FALSE, 2);	
	gtk_container_add (GTK_CONTAINER (search_box), widget->priv->search_entry);
	gtk_container_add (GTK_CONTAINER (search_box), widget->priv->search_button);	
	gtk_box_pack_start (GTK_BOX (v_box), search_box, FALSE, FALSE, 2);

	/* Insert the result list and initialize the viewport */
	scroll = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
	gtk_scrolled_window_set_policy (scroll, GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_add_with_viewport (scroll, GTK_WIDGET (widget->priv->result_list));
	gtk_container_add (GTK_CONTAINER (v_box), GTK_WIDGET (scroll));

	/* Initialise the pager box */
	pager_box = gtk_hbox_new (FALSE, 2);
	widget->priv->next_button = gtk_button_new_from_stock (GTK_STOCK_GO_FORWARD);
	widget->priv->previous_button = gtk_button_new_from_stock (GTK_STOCK_GO_BACK);
	adjust = GTK_ADJUSTMENT (gtk_adjustment_new (1, 1, 1, 1, 5, 0));
	widget->priv->page_selector = gtk_spin_button_new (adjust, 1, 0);
	gtk_box_pack_start (GTK_BOX (pager_box), widget->priv->previous_button, FALSE, FALSE, 2);
	gtk_box_pack_start (GTK_BOX (pager_box), gtk_label_new (_("Page")), FALSE, FALSE, 2);
	gtk_box_pack_start (GTK_BOX (pager_box), widget->priv->page_selector, TRUE, TRUE, 2);
	gtk_box_pack_start (GTK_BOX (pager_box), widget->priv->next_button, FALSE, FALSE, 2);
	gtk_box_pack_start (GTK_BOX (v_box), pager_box, FALSE, FALSE, 2);

	gtk_widget_set_sensitive (GTK_WIDGET (widget->priv->previous_button), FALSE);
	gtk_widget_set_sensitive (GTK_WIDGET (widget->priv->page_selector), FALSE);
	gtk_widget_set_sensitive (GTK_WIDGET (widget->priv->next_button), FALSE);

	/* Status label */
	widget->priv->status_label = gtk_label_new ("");
	gtk_box_pack_start (GTK_BOX (v_box), widget->priv->status_label, FALSE, FALSE, 2);

	/* Add the main container to the widget */
	gtk_container_add (GTK_CONTAINER (widget), v_box);

	gtk_widget_show_all (GTK_WIDGET (widget));

	/* Connect the search button clicked signal and the search entry  */
	g_signal_connect (widget->priv->search_button, "clicked",
			  G_CALLBACK (new_search), widget);
	g_signal_connect (widget->priv->search_entry, "activate",
			  G_CALLBACK (new_search), widget);
	/* Connect the pager buttons */
	g_signal_connect (widget->priv->next_button, "clicked",
			  G_CALLBACK (go_next), widget);
	g_signal_connect (widget->priv->previous_button, "clicked",
			  G_CALLBACK (go_previous), widget);
	g_signal_connect (widget->priv->page_selector, "value-changed",
			  G_CALLBACK (page_selector_value_changed_cb), widget);
}

GtkWidget *
totem_tracker_widget_new (TotemObject *totem)
{
	GtkWidget *widget;

	widget = g_object_new (TOTEM_TYPE_TRACKER_WIDGET,
			       "totem", totem, NULL);

	/* Reset the info about the search */
	TOTEM_TRACKER_WIDGET (widget)->priv->total_result_count = 0;
	TOTEM_TRACKER_WIDGET (widget)->priv->current_result_page = 0;

	initialize_list_store (TOTEM_TRACKER_WIDGET (widget));

	return widget;
}