1 /*
2  *
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10  * for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with this program; if not, see <http://www.gnu.org/licenses/>.
14  *
15  *
16  * Authors:
17  *		Chris Lahey <clahey@ximian.com>
18  *
19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
20  *
21  */
22 
23 #include "evolution-config.h"
24 
25 #include "e-table-utils.h"
26 
27 #include <libintl.h>		/* This file uses dgettext() but no _() */
28 #include <string.h>
29 
30 #include "e-table-header-utils.h"
31 #include "e-unicode.h"
32 
33 ETableHeader *
e_table_state_to_header(GtkWidget * widget,ETableHeader * full_header,ETableState * state)34 e_table_state_to_header (GtkWidget *widget,
35                          ETableHeader *full_header,
36                          ETableState *state)
37 {
38 	ETableHeader *nh;
39 	gint ii;
40 	GValue *val = g_new0 (GValue, 1);
41 
42 	g_return_val_if_fail (widget, NULL);
43 	g_return_val_if_fail (full_header, NULL);
44 	g_return_val_if_fail (state, NULL);
45 
46 	nh = e_table_header_new ();
47 	g_value_init (val, G_TYPE_DOUBLE);
48 	g_value_set_double (val, e_table_header_width_extras (widget));
49 	g_object_set_property (G_OBJECT (nh), "width_extras", val);
50 	g_free (val);
51 
52 	for (ii = 0; ii < state->col_count; ii++) {
53 		ETableCol *table_col;
54 
55 		table_col = e_table_header_get_column_by_spec (
56 			full_header, state->column_specs[ii]);
57 
58 		if (table_col == NULL)
59 			continue;
60 
61 		if (state->expansions[ii] >= -1)
62 			table_col->expansion = state->expansions[ii];
63 
64 		e_table_header_add_column (nh, table_col, -1);
65 	}
66 
67 	return nh;
68 }
69 
70 static ETableCol *
et_col_spec_to_col(ETableColumnSpecification * col_spec,ETableExtras * ete,const gchar * domain)71 et_col_spec_to_col (ETableColumnSpecification *col_spec,
72                     ETableExtras *ete,
73                     const gchar *domain)
74 {
75 	ETableCol *col = NULL;
76 	ECell *cell = NULL;
77 	GCompareDataFunc compare = NULL;
78 	ETableSearchFunc search = NULL;
79 
80 	if (col_spec->cell)
81 		cell = e_table_extras_get_cell (ete, col_spec->cell);
82 	if (col_spec->compare)
83 		compare = e_table_extras_get_compare (ete, col_spec->compare);
84 	if (col_spec->search)
85 		search = e_table_extras_get_search (ete, col_spec->search);
86 
87 	if (cell && compare) {
88 		gchar *title = dgettext (domain, col_spec->title);
89 
90 		title = g_strdup (title);
91 
92 		if (col_spec->pixbuf && *col_spec->pixbuf) {
93 			const gchar *icon_name;
94 
95 			icon_name = e_table_extras_get_icon_name (
96 				ete, col_spec->pixbuf);
97 			if (icon_name != NULL) {
98 				col = e_table_col_new (
99 					col_spec,
100 					title, icon_name,
101 					cell, compare);
102 			}
103 		}
104 
105 		if (col == NULL && col_spec->title && *col_spec->title) {
106 			col = e_table_col_new (
107 				col_spec,
108 				title, NULL,
109 				cell, compare);
110 		}
111 
112 		if (col != NULL)
113 			col->search = search;
114 
115 		g_free (title);
116 	}
117 
118 	return col;
119 }
120 
121 ETableHeader *
e_table_spec_to_full_header(ETableSpecification * spec,ETableExtras * ete)122 e_table_spec_to_full_header (ETableSpecification *spec,
123                              ETableExtras *ete)
124 {
125 	ETableHeader *nh;
126 	GPtrArray *columns;
127 	guint ii;
128 
129 	g_return_val_if_fail (spec, NULL);
130 	g_return_val_if_fail (ete, NULL);
131 
132 	nh = e_table_header_new ();
133 
134 	columns = e_table_specification_ref_columns (spec);
135 
136 	for (ii = 0; ii < columns->len; ii++) {
137 		ETableColumnSpecification *col_spec;
138 		ETableCol *col;
139 
140 		col_spec = g_ptr_array_index (columns, ii);
141 		col = et_col_spec_to_col (col_spec, ete, spec->domain);
142 
143 		if (col != NULL) {
144 			e_table_header_add_column (nh, col, -1);
145 			g_object_unref (col);
146 		}
147 	}
148 
149 	g_ptr_array_unref (columns);
150 
151 	return nh;
152 }
153 
154 static gboolean
check_col(ETableCol * col,gpointer user_data)155 check_col (ETableCol *col,
156            gpointer user_data)
157 {
158 	return col->search ? TRUE : FALSE;
159 }
160 
161 ETableCol *
e_table_util_calculate_current_search_col(ETableHeader * header,ETableHeader * full_header,ETableSortInfo * sort_info,gboolean always_search)162 e_table_util_calculate_current_search_col (ETableHeader *header,
163                                            ETableHeader *full_header,
164                                            ETableSortInfo *sort_info,
165                                            gboolean always_search)
166 {
167 	gint i;
168 	gint count;
169 	ETableCol *col = NULL;
170 
171 	count = e_table_sort_info_grouping_get_count (sort_info);
172 
173 	for (i = 0; i < count; i++) {
174 		ETableColumnSpecification *spec;
175 
176 		spec = e_table_sort_info_grouping_get_nth (sort_info, i, NULL);
177 		col = e_table_header_get_column_by_spec (full_header, spec);
178 
179 		if (col != NULL && col->search)
180 			return col;
181 
182 		col = NULL;
183 	}
184 
185 	count = e_table_sort_info_sorting_get_count (sort_info);
186 	for (i = 0; i < count; i++) {
187 		ETableColumnSpecification *spec;
188 
189 		spec = e_table_sort_info_sorting_get_nth (sort_info, i, NULL);
190 		col = e_table_header_get_column_by_spec (full_header, spec);
191 
192 		if (col != NULL && col->search)
193 			return col;
194 
195 		col = NULL;
196 	}
197 
198 	if (always_search)
199 		col = e_table_header_prioritized_column_selected (
200 			header, check_col, NULL);
201 
202 	return col;
203 }
204