nautilus-3.6.3/libnautilus-private/nautilus-column-utilities.c

No issues found

  1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
  2 
  3 /* nautilus-column-utilities.h - Utilities related to column specifications
  4 
  5    Copyright (C) 2004 Novell, Inc.
  6 
  7    The Gnome Library is free software; you can redistribute it and/or
  8    modify it under the terms of the GNU Library General Public License as
  9    published by the Free Software Foundation; either version 2 of the
 10    License, or (at your option) any later version.
 11 
 12    The Gnome Library is distributed in the hope that it will be useful,
 13    but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15    Library General Public License for more details.
 16 
 17    You should have received a copy of the GNU Library General Public
 18    License along with the Gnome Library; see the column COPYING.LIB.  If not,
 19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 20    Boston, MA 02111-1307, USA.
 21 
 22    Authors: Dave Camp <dave@ximian.com>
 23 */
 24 
 25 #include <config.h>
 26 #include "nautilus-column-utilities.h"
 27 
 28 #include <string.h>
 29 #include <eel/eel-glib-extensions.h>
 30 #include <glib/gi18n.h>
 31 #include <libnautilus-extension/nautilus-column-provider.h>
 32 #include <libnautilus-private/nautilus-module.h>
 33 
 34 static const char *default_column_order[] = {
 35 	"name",
 36 	"size",
 37 	"type",
 38 	"date_modified",
 39 	"owner",
 40 	"group",
 41 	"permissions",
 42 	"mime_type",
 43 	"where",
 44 	NULL
 45 };
 46 
 47 static GList *
 48 get_builtin_columns (void)
 49 {
 50 	GList *columns;
 51 
 52 	columns = g_list_append (NULL,
 53 				 g_object_new (NAUTILUS_TYPE_COLUMN,
 54 					       "name", "name",
 55 					       "attribute", "name",
 56 					       "label", _("Name"),
 57 					       "description", _("The name and icon of the file."),
 58 					       NULL));
 59 	columns = g_list_append (columns,
 60 				 g_object_new (NAUTILUS_TYPE_COLUMN,
 61 					       "name", "size",
 62 					       "attribute", "size",
 63 					       "label", _("Size"),
 64 					       "description", _("The size of the file."),
 65 					       "xalign", 1.0,
 66 					       NULL));
 67 	columns = g_list_append (columns,
 68 				 g_object_new (NAUTILUS_TYPE_COLUMN,
 69 					       "name", "type",
 70 					       "attribute", "type",
 71 					       "label", _("Type"),
 72 					       "description", _("The type of the file."),
 73 					       NULL));
 74 	columns = g_list_append (columns,
 75 				 g_object_new (NAUTILUS_TYPE_COLUMN,
 76 					       "name", "date_modified",
 77 					       "attribute", "date_modified",
 78 					       "label", _("Modified"),
 79 					       "description", _("The date the file was modified."),
 80 					       "default-sort-order", GTK_SORT_DESCENDING,
 81 					       NULL));
 82 
 83 	columns = g_list_append (columns,
 84 				 g_object_new (NAUTILUS_TYPE_COLUMN,
 85 					       "name", "owner",
 86 					       "attribute", "owner",
 87 					       "label", _("Owner"),
 88 					       "description", _("The owner of the file."),
 89 					       NULL));
 90 
 91 	columns = g_list_append (columns,
 92 				 g_object_new (NAUTILUS_TYPE_COLUMN,
 93 					       "name", "group",
 94 					       "attribute", "group",
 95 					       "label", _("Group"),
 96 					       "description", _("The group of the file."),
 97 					       NULL));
 98 
 99 	columns = g_list_append (columns,
100 				 g_object_new (NAUTILUS_TYPE_COLUMN,
101 					       "name", "permissions",
102 					       "attribute", "permissions",
103 					       "label", _("Permissions"),
104 					       "description", _("The permissions of the file."),
105 					       NULL));
106 
107 	columns = g_list_append (columns,
108 				 g_object_new (NAUTILUS_TYPE_COLUMN,
109 					       "name", "mime_type",
110 					       "attribute", "mime_type",
111 					       "label", _("MIME Type"),
112 					       "description", _("The mime type of the file."),
113 					       NULL));
114 
115 	columns = g_list_append (columns,
116 				 g_object_new (NAUTILUS_TYPE_COLUMN,
117 					       "name", "where",
118 					       "attribute", "where",
119 					       "label", _("Location"),
120 					       "description", _("The location of the file."),
121 					       NULL));
122 
123 	return columns;
124 }
125 
126 static GList *
127 get_extension_columns (void)
128 {
129 	GList *columns;
130 	GList *providers;
131 	GList *l;
132 	
133 	providers = nautilus_module_get_extensions_for_type (NAUTILUS_TYPE_COLUMN_PROVIDER);
134 	
135 	columns = NULL;
136 	
137 	for (l = providers; l != NULL; l = l->next) {
138 		NautilusColumnProvider *provider;
139 		GList *provider_columns;
140 		
141 		provider = NAUTILUS_COLUMN_PROVIDER (l->data);
142 		provider_columns = nautilus_column_provider_get_columns (provider);
143 		columns = g_list_concat (columns, provider_columns);
144 	}
145 
146 	nautilus_module_extension_list_free (providers);
147 
148 	return columns;
149 }
150 
151 static GList *
152 get_trash_columns (void)
153 {
154 	static GList *columns = NULL;
155 
156 	if (columns == NULL) {
157 		columns = g_list_append (columns,
158 					 g_object_new (NAUTILUS_TYPE_COLUMN,
159 						       "name", "trashed_on",
160 						       "attribute", "trashed_on",
161 						       "label", _("Trashed On"),
162 						       "description", _("Date when file was moved to the Trash"),
163 						       NULL));
164 		columns = g_list_append (columns,
165 			                 g_object_new (NAUTILUS_TYPE_COLUMN,
166 			                               "name", "trash_orig_path",
167 			                               "attribute", "trash_orig_path",
168 			                               "label", _("Original Location"),
169 			                               "description", _("Original location of file before moved to the Trash"),
170 			                               NULL));
171 	}
172 
173 	return nautilus_column_list_copy (columns);
174 }
175 
176 static GList *
177 get_search_columns (void)
178 {
179 	static GList *columns = NULL;
180 
181 	if (columns == NULL) {
182 		columns = g_list_append (columns,
183 					 g_object_new (NAUTILUS_TYPE_COLUMN,
184 						       "name", "search_relevance",
185 						       "attribute", "search_relevance",
186 						       "label", _("Relevance"),
187 						       "description", _("Relevance rank for search"),
188 						       NULL));
189 	}
190 
191 	return nautilus_column_list_copy (columns);
192 }
193 
194 GList *
195 nautilus_get_common_columns (void)
196 {
197 	static GList *columns = NULL;
198 
199 	if (!columns) {
200 		columns = g_list_concat (get_builtin_columns (),
201 		                         get_extension_columns ());
202 	}
203 
204 	return nautilus_column_list_copy (columns);
205 }
206 
207 GList *
208 nautilus_get_all_columns (void)
209 {
210 	GList *columns = NULL;
211 
212 	columns = g_list_concat (nautilus_get_common_columns (),
213 	                         get_trash_columns ());
214 	columns = g_list_concat (columns,
215 				 get_search_columns ());
216 
217 	return columns;
218 }
219 
220 GList *
221 nautilus_get_columns_for_file (NautilusFile *file)
222 {
223 	GList *columns;
224 
225 	columns = nautilus_get_common_columns ();
226 
227 	if (file != NULL && nautilus_file_is_in_trash (file)) {
228 		columns = g_list_concat (columns,
229 		                         get_trash_columns ());
230 	}
231 
232 	return columns;
233 }
234 
235 GList *
236 nautilus_column_list_copy (GList *columns) 
237 {
238 	GList *ret;
239 	GList *l;
240 	
241 	ret = g_list_copy (columns);
242 	
243 	for (l = ret; l != NULL; l = l->next) {
244 		g_object_ref (l->data);
245 	}
246 
247 	return ret;
248 }
249 
250 void
251 nautilus_column_list_free (GList *columns)
252 {
253 	GList *l;
254 	
255 	for (l = columns; l != NULL; l = l->next) {
256 		g_object_unref (l->data);
257 	}
258 	
259 	g_list_free (columns);
260 }
261 
262 static int
263 strv_index (char **strv, const char *str)
264 {
265 	int i;
266 
267 	for (i = 0; strv[i] != NULL; ++i) {
268 		if (strcmp (strv[i], str) == 0)
269 			return i;
270 	}
271 
272 	return -1;
273 }
274 
275 static int
276 column_compare (NautilusColumn *a, NautilusColumn *b, char **column_order)
277 {
278 	int index_a;
279 	int index_b;
280 	char *name_a;
281 	char *name_b;
282 	int ret;
283 
284 	g_object_get (G_OBJECT (a), "name", &name_a, NULL);
285 	index_a = strv_index (column_order, name_a);
286 
287 	g_object_get (G_OBJECT (b), "name", &name_b, NULL);
288 	index_b = strv_index (column_order, name_b);
289 
290 	if (index_a == index_b) {
291 		int pos_a;
292 		int pos_b;
293 
294 		pos_a = strv_index ((char **)default_column_order, name_a);
295 		pos_b = strv_index ((char **)default_column_order, name_b);
296 
297 		if (pos_a == pos_b) {
298 			char *label_a;
299 			char *label_b;
300 		
301 			g_object_get (G_OBJECT (a), "label", &label_a, NULL);
302 			g_object_get (G_OBJECT (b), "label", &label_b, NULL);
303 			ret = strcmp (label_a, label_b);
304 			g_free (label_a);
305 			g_free (label_b);
306 		} else if (pos_a == -1) {
307 			ret = 1;
308 		} else if (pos_b == -1) {
309 			ret = -1;
310 		} else {
311 			ret = index_a - index_b;
312 		}
313 	} else if (index_a == -1) {
314 		ret = 1;
315 	} else if (index_b == -1) {
316 		ret = -1;
317 	} else {
318 		ret = index_a - index_b;
319 	}
320 
321 	g_free (name_a);
322 	g_free (name_b);
323 
324 	return ret;
325 }
326 
327 GList *
328 nautilus_sort_columns (GList  *columns, 
329 		       char  **column_order)
330 {
331 	if (column_order == NULL) {
332 		return columns;
333 	}
334 
335 	return g_list_sort_with_data (columns,
336 				      (GCompareDataFunc)column_compare,
337 				      column_order);
338 }