1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 
3 /* caja-column-utilities.h - Utilities related to column specifications
4 
5    Copyright (C) 2004 Novell, Inc.
6 
7    The Mate 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 Mate 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 Mate Library; see the column COPYING.LIB.  If not,
19    write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20    Boston, MA 02110-1301, USA.
21 
22    Authors: Dave Camp <dave@ximian.com>
23 */
24 
25 #include <config.h>
26 #include <string.h>
27 #include <glib/gi18n.h>
28 
29 #include <eel/eel-glib-extensions.h>
30 
31 #include <libcaja-extension/caja-column-provider.h>
32 
33 #include "caja-column-utilities.h"
34 #include "caja-extensions.h"
35 #include "caja-module.h"
36 
37 static GList *
get_builtin_columns(void)38 get_builtin_columns (void)
39 {
40     GList *columns;
41 
42     columns = g_list_append (NULL,
43                              g_object_new (CAJA_TYPE_COLUMN,
44                                            "name", "name",
45                                            "attribute", "name",
46                                            "label", _("Name"),
47                                            "description", _("The name and icon of the file."),
48                                            NULL));
49     columns = g_list_append (columns,
50                              g_object_new (CAJA_TYPE_COLUMN,
51                                            "name", "size",
52                                            "attribute", "size",
53                                            "label", _("Size"),
54                                            "description", _("The size of the file."),
55                                            "xalign", 1.0,
56                                            NULL));
57     columns = g_list_append (columns,
58                              g_object_new (CAJA_TYPE_COLUMN,
59                                            "name", "size_on_disk",
60                                            "attribute", "size_on_disk",
61                                            "label", _("Size on Disk"),
62                                            "description", _("The size of the file on disk."),
63                                            "xalign", 1.0,
64                                            NULL));
65     columns = g_list_append (columns,
66                              g_object_new (CAJA_TYPE_COLUMN,
67                                            "name", "type",
68                                            "attribute", "type",
69                                            "label", _("Type"),
70                                            "description", _("The type of the file."),
71                                            NULL));
72     columns = g_list_append (columns,
73                              g_object_new (CAJA_TYPE_COLUMN,
74                                            "name", "date_modified",
75                                            "attribute", "date_modified",
76                                            "label", _("Date Modified"),
77                                            "description", _("The date the file was modified."),
78                                            NULL));
79 
80     columns = g_list_append (columns,
81                              g_object_new (CAJA_TYPE_COLUMN,
82                                            "name", "date_created",
83                                            "attribute", "date_created",
84                                            "label", _("Date Created"),
85                                            "description", _("The date the file was created."),
86                                            NULL));
87 
88     columns = g_list_append (columns,
89                              g_object_new (CAJA_TYPE_COLUMN,
90                                            "name", "date_accessed",
91                                            "attribute", "date_accessed",
92                                            "label", _("Date Accessed"),
93                                            "description", _("The date the file was accessed."),
94                                            NULL));
95 
96     columns = g_list_append (columns,
97                              g_object_new (CAJA_TYPE_COLUMN,
98                                            "name", "owner",
99                                            "attribute", "owner",
100                                            "label", _("Owner"),
101                                            "description", _("The owner of the file."),
102                                            NULL));
103 
104     columns = g_list_append (columns,
105                              g_object_new (CAJA_TYPE_COLUMN,
106                                            "name", "group",
107                                            "attribute", "group",
108                                            "label", _("Group"),
109                                            "description", _("The group of the file."),
110                                            NULL));
111 
112     columns = g_list_append (columns,
113                              g_object_new (CAJA_TYPE_COLUMN,
114                                            "name", "permissions",
115                                            "attribute", "permissions",
116                                            "label", _("Permissions"),
117                                            "description", _("The permissions of the file."),
118                                            NULL));
119 
120     columns = g_list_append (columns,
121                              g_object_new (CAJA_TYPE_COLUMN,
122                                            "name", "octal_permissions",
123                                            "attribute", "octal_permissions",
124                                            "label", _("Octal Permissions"),
125                                            "description", _("The permissions of the file, in octal notation."),
126                                            NULL));
127 
128     columns = g_list_append (columns,
129                              g_object_new (CAJA_TYPE_COLUMN,
130                                            "name", "mime_type",
131                                            "attribute", "mime_type",
132                                            "label", _("MIME Type"),
133                                            "description", _("The mime type of the file."),
134                                            NULL));
135 #ifdef HAVE_SELINUX
136     columns = g_list_append (columns,
137                              g_object_new (CAJA_TYPE_COLUMN,
138                                            "name", "selinux_context",
139                                            "attribute", "selinux_context",
140                                            "label", _("SELinux Context"),
141                                            "description", _("The SELinux security context of the file."),
142                                            NULL));
143 #endif
144     columns = g_list_append (columns,
145                              g_object_new (CAJA_TYPE_COLUMN,
146                                            "name", "where",
147                                            "attribute", "where",
148                                            "label", _("Location"),
149                                            "description", _("The location of the file."),
150                                            NULL));
151 
152     columns = g_list_append (columns,
153                              g_object_new (CAJA_TYPE_COLUMN,
154                                            "name", "extension",
155                                            "attribute", "extension",
156                                            "label", _("Extension"),
157                                            "description", _("The extension of the file."),
158                                            NULL));
159     return columns;
160 }
161 
162 static GList *
get_extension_columns(void)163 get_extension_columns (void)
164 {
165     GList *columns;
166     GList *providers;
167     GList *l;
168 
169     providers = caja_extensions_get_for_type (CAJA_TYPE_COLUMN_PROVIDER);
170 
171     columns = NULL;
172 
173     for (l = providers; l != NULL; l = l->next)
174     {
175         CajaColumnProvider *provider;
176         GList *provider_columns;
177 
178         provider = CAJA_COLUMN_PROVIDER (l->data);
179         provider_columns = caja_column_provider_get_columns (provider);
180         columns = g_list_concat (columns, provider_columns);
181     }
182 
183     caja_module_extension_list_free (providers);
184 
185     return columns;
186 }
187 
188 static GList *
get_trash_columns(void)189 get_trash_columns (void)
190 {
191     static GList *columns = NULL;
192 
193     if (columns == NULL)
194     {
195         columns = g_list_append (columns,
196                                  g_object_new (CAJA_TYPE_COLUMN,
197                                                "name", "trashed_on",
198                                                "attribute", "trashed_on",
199                                                "label", _("Trashed On"),
200                                                "description", _("Date when file was moved to the Trash"),
201                                                NULL));
202         columns = g_list_append (columns,
203                                  g_object_new (CAJA_TYPE_COLUMN,
204                                                "name", "trash_orig_path",
205                                                "attribute", "trash_orig_path",
206                                                "label", _("Original Location"),
207                                                "description", _("Original location of file before moved to the Trash"),
208                                                NULL));
209     }
210 
211     return caja_column_list_copy (columns);
212 }
213 
214 GList *
caja_get_common_columns(void)215 caja_get_common_columns (void)
216 {
217     static GList *columns = NULL;
218 
219     if (!columns)
220     {
221         columns = g_list_concat (get_builtin_columns (),
222                                  get_extension_columns ());
223     }
224 
225     return caja_column_list_copy (columns);
226 }
227 
228 GList *
caja_get_all_columns(void)229 caja_get_all_columns (void)
230 {
231     GList *columns = NULL;
232 
233     columns = g_list_concat (caja_get_common_columns (),
234                              get_trash_columns ());
235 
236     return columns;
237 }
238 
239 GList *
caja_get_columns_for_file(CajaFile * file)240 caja_get_columns_for_file (CajaFile *file)
241 {
242     GList *columns;
243 
244     columns = caja_get_common_columns ();
245 
246     if (file != NULL && caja_file_is_in_trash (file))
247     {
248         columns = g_list_concat (columns,
249                                  get_trash_columns ());
250     }
251 
252     return columns;
253 }
254 
255 GList *
caja_column_list_copy(GList * columns)256 caja_column_list_copy (GList *columns)
257 {
258     GList *ret;
259     GList *l;
260 
261     ret = g_list_copy (columns);
262 
263     for (l = ret; l != NULL; l = l->next)
264     {
265         g_object_ref (l->data);
266     }
267 
268     return ret;
269 }
270 
271 void
caja_column_list_free(GList * columns)272 caja_column_list_free (GList *columns)
273 {
274     GList *l;
275 
276     for (l = columns; l != NULL; l = l->next)
277     {
278         g_object_unref (l->data);
279     }
280 
281     g_list_free (columns);
282 }
283 
284 static int
strv_index(char ** strv,const char * str)285 strv_index (char **strv, const char *str)
286 {
287     int i;
288 
289     for (i = 0; strv[i] != NULL; ++i)
290     {
291         if (strcmp (strv[i], str) == 0)
292             return i;
293     }
294 
295     return -1;
296 }
297 
298 static int
column_compare(CajaColumn * a,CajaColumn * b,char ** column_order)299 column_compare (CajaColumn *a, CajaColumn *b, char **column_order)
300 {
301     int index_a;
302     int index_b;
303     char *name;
304 
305     g_object_get (G_OBJECT (a), "name", &name, NULL);
306     index_a = strv_index (column_order, name);
307     g_free (name);
308 
309     g_object_get (G_OBJECT (b), "name", &name, NULL);
310     index_b = strv_index (column_order, name);
311     g_free (name);
312 
313     if (index_a == index_b)
314     {
315         int ret;
316         char *label_a;
317         char *label_b;
318 
319         g_object_get (G_OBJECT (a), "label", &label_a, NULL);
320         g_object_get (G_OBJECT (b), "label", &label_b, NULL);
321         ret = strcmp (label_a, label_b);
322         g_free (label_a);
323         g_free (label_b);
324 
325         return ret;
326     }
327     else if (index_a == -1)
328     {
329         return 1;
330     }
331     else if (index_b == -1)
332     {
333         return -1;
334     }
335     else
336     {
337         return index_a - index_b;
338     }
339 }
340 
341 GList *
caja_sort_columns(GList * columns,char ** column_order)342 caja_sort_columns (GList  *columns,
343                    char  **column_order)
344 {
345     if (!column_order)
346     {
347         return NULL;
348     }
349 
350     return g_list_sort_with_data (columns,
351                                   (GCompareDataFunc)column_compare,
352                                   column_order);
353 }
354 
355