~ubuntu-branches/ubuntu/oneiric/ubuntuone-client/oneiric

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
/*
 * Ubuntu One Nautilus plugin
 *
 * Authors: Alejandro J. Cura <alecu@canonical.com>
 *
 * Copyright 2010 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <string.h>
#include <glib.h>

static gint
compare_glongs (gconstpointer a,
	        gconstpointer b,
		gpointer user_data)
{
	return (glong) a - (glong) b;
}


static void
tree_of_arrays_insert (GTree *tree,
		       gpointer key,
		       gpointer value)
{
	GPtrArray *array = g_tree_lookup (tree, key);
	if (array == NULL) {
		array = g_ptr_array_new ();
		g_tree_insert (tree, key, array);
	}
	g_ptr_array_add (array, value);
}


static void
destroy_tree_array (gpointer data)
{
	g_ptr_array_free (data, TRUE);
}

typedef struct {
	GString *built_string;
	gchar *source_string;
	gchar *source_cursor;
} BuildResultsData;


static void
append_array_strings (gpointer data,
		      gpointer user_data)
{
	g_string_append (user_data, data);
}


static gboolean
build_results (gpointer key,
	       gpointer value,
	       gpointer data)
{
	BuildResultsData* results_data = data;
	glong tag_start = (glong)key;
	gchar *tag_start_ptr = g_utf8_offset_to_pointer (results_data->source_string,
							 tag_start);
	glong len = tag_start_ptr - results_data->source_cursor;

	gchar *escaped_str = g_markup_escape_text (results_data->source_cursor,
						   len);
	
	g_string_append (results_data->built_string,
			 escaped_str);
	g_free (escaped_str);
	results_data->source_cursor += len;

	g_ptr_array_foreach (value,
			     append_array_strings,
			     results_data->built_string);
	return FALSE;
}


gchar *
highlight_result(gchar *needles, gchar *haystack)
{
	gchar **split_needles;
	GTree *result_parts;
	gchar *folded_needles;
	gchar *folded_haystack;
	gchar **needle;
	gchar *escaped_str;
	BuildResultsData results_data;

	folded_needles = g_utf8_casefold (needles, -1);
	folded_haystack = g_utf8_casefold (haystack, -1);

	results_data.built_string = g_string_new ("");
	results_data.source_string = haystack;
	results_data.source_cursor = haystack;

	result_parts = g_tree_new_full (compare_glongs,
					NULL,
					NULL,
					destroy_tree_array);
	
	split_needles = g_strsplit (folded_needles, " ", 0);
	needle = split_needles;
	while (*needle != NULL) {
		gchar *search_start;
		gchar *start_ptr;
		glong needle_len = g_utf8_strlen (*needle, -1);
		if (needle_len < 1) {
			needle++;
			continue;
		}
		search_start = folded_haystack;
		start_ptr = g_strstr_len (search_start, -1, *needle);
		while (start_ptr != NULL) {
			glong start = g_utf8_pointer_to_offset (folded_haystack,
								start_ptr);
			glong end = start + g_utf8_strlen (*needle, -1);
			tree_of_arrays_insert (result_parts,
					       (gpointer) start,
					       "<b>");
			tree_of_arrays_insert (result_parts,
					       (gpointer) end,
					       "</b>");
			search_start = g_utf8_next_char (start_ptr);
			start_ptr = g_strstr_len (search_start,
			                          -1,
						  *needle);
		}
		needle++;
	}
	g_free (folded_needles);
	g_free (folded_haystack);


	g_tree_foreach (result_parts, build_results, &results_data);

	escaped_str = g_markup_escape_text (results_data.source_cursor, -1);
	g_string_append (results_data.built_string,
			 escaped_str);
	g_free (escaped_str);

	g_tree_destroy (result_parts);
	g_strfreev (split_needles);
	return g_string_free (results_data.built_string,
			      FALSE);
}