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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Engrampa
*
* Copyright (C) 2001-2006 The Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include "glib-utils.h"
#include "file-utils.h"
#include "file-data.h"
FileData *
file_data_new (void)
{
FileData *fdata;
fdata = g_new0 (FileData, 1);
fdata->content_type = NULL;
fdata->free_original_path = FALSE;
fdata->dir_size = 0;
return fdata;
}
void
file_data_free (FileData *fdata)
{
if (fdata == NULL)
return;
if (fdata->free_original_path)
g_free (fdata->original_path);
g_free (fdata->full_path);
g_free (fdata->name);
g_free (fdata->path);
g_free (fdata->link);
g_free (fdata->list_name);
g_free (fdata->sort_key);
g_free (fdata);
}
FileData *
file_data_copy (FileData *src)
{
FileData *fdata;
fdata = g_new0 (FileData, 1);
fdata->original_path = g_strdup (src->original_path);
fdata->free_original_path = TRUE;
fdata->full_path = g_strdup (src->full_path);
fdata->link = g_strdup (src->link);
fdata->size = src->size;
fdata->modified = src->modified;
fdata->name = g_strdup (src->name);
fdata->path = g_strdup (src->path);
fdata->content_type = src->content_type;
fdata->encrypted = src->encrypted;
fdata->dir = src->dir;
fdata->dir_size = src->dir_size;
fdata->list_dir = src->list_dir;
fdata->list_name = g_strdup (src->list_name);
fdata->sort_key = g_strdup (src->sort_key);
return fdata;
}
GType
file_data_get_type (void)
{
static GType type = 0;
if (type == 0)
type = g_boxed_type_register_static ("FRFileData", (GBoxedCopyFunc) file_data_copy, (GBoxedFreeFunc) file_data_free);
return type;
}
void
file_data_update_content_type (FileData *fdata)
{
if (fdata->dir)
fdata->content_type = MIME_TYPE_DIRECTORY;
else
fdata->content_type = get_static_string (g_content_type_guess (fdata->full_path, NULL, 0, NULL));
}
gboolean
file_data_is_dir (FileData *fdata)
{
return fdata->dir || fdata->list_dir;
}
void
file_data_set_list_name (FileData *fdata,
const char *value)
{
g_free (fdata->list_name);
fdata->list_name = g_strdup (value);
g_free (fdata->sort_key);
if (fdata->list_name != NULL)
fdata->sort_key = g_utf8_collate_key_for_filename (fdata->list_name, -1);
else
fdata->sort_key = NULL;
}
int
file_data_compare_by_path (gconstpointer a,
gconstpointer b)
{
FileData *data_a = *((FileData **) a);
FileData *data_b = *((FileData **) b);
return strcmp (data_a->full_path, data_b->full_path);
}
int
find_path_in_file_data_array (GPtrArray *array,
const char *path)
{
int path_l;
int left;
int right;
if (path == NULL)
return -1;
path_l = strlen (path);
left = 0;
right = array->len;
while (left < right) {
FileData *fd;
int p;
int cmp;
p = left + ((right - left) / 2);
fd = (FileData *) g_ptr_array_index (array, p);
cmp = strcmp (path, fd->original_path);
if (cmp != 0) {
/* consider '/path/to/dir' and '/path/to/dir/' the same path */
int original_path_l;
original_path_l = strlen (fd->original_path);
if ((path_l == original_path_l - 1) && (fd->original_path[original_path_l - 1] == '/')) {
int cmp2;
cmp2 = strncmp (path, fd->original_path, original_path_l - 1);
if (cmp2 == 0)
cmp = cmp2;
}
}
if (cmp == 0)
return p;
else if (cmp < 0)
right = p;
else
left = p + 1;
}
return -1;
}
|