1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /*
4 * Engrampa
5 *
6 * Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02110-1301, USA.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26
27 #include <glib.h>
28
29 #include "file-data.h"
30 #include "file-utils.h"
31 #include "glib-utils.h"
32 #include "fr-command.h"
33 #include "fr-command-ace.h"
34
35 static void fr_command_ace_class_init (FrCommandAceClass *class);
36 static void fr_command_ace_init (FrCommand *afile);
37 static void fr_command_ace_finalize (GObject *object);
38
39 /* Parent Class */
40
41 static FrCommandClass *parent_class = NULL;
42
43
44 /* -- list -- */
45
46
47 static time_t
mktime_from_string(char * date,char * time)48 mktime_from_string (char *date,
49 char *time)
50 {
51 struct tm tm = {0, };
52 char **fields;
53
54 tm.tm_isdst = -1;
55
56 /* date */
57
58 fields = g_strsplit (date, ".", 3);
59 if (fields[0] != NULL) {
60 tm.tm_mday = atoi (fields[0]);
61 if (fields[1] != NULL) {
62 tm.tm_mon = atoi (fields[1]) - 1;
63 if (fields[2] != NULL) {
64 int y = atoi (fields[2]);
65 if (y > 75)
66 tm.tm_year = y;
67 else
68 tm.tm_year = 100 + y;
69 }
70 }
71 }
72 g_strfreev (fields);
73
74 /* time */
75
76 fields = g_strsplit (time, ":", 2);
77 if (fields[0] != NULL) {
78 tm.tm_hour = atoi (fields[0]);
79 if (fields[1] != NULL)
80 tm.tm_min = atoi (fields[1]);
81 }
82 tm.tm_sec = 0;
83 g_strfreev (fields);
84
85 return mktime (&tm);
86 }
87
88
89 static void
process_line(char * line,gpointer data)90 process_line (char *line,
91 gpointer data)
92 {
93 FileData *fdata;
94 FrCommandAce *ace_comm = FR_COMMAND_ACE (data);
95 FrCommand *comm = FR_COMMAND (data);
96 char **fields = NULL;
97 const char *field_name = NULL;
98
99 g_return_if_fail (line != NULL);
100
101 if (ace_comm->command_type == FR_ACE_COMMAND_UNKNOWN) {
102 if (g_str_has_prefix (line, "UNACE")) {
103 if (strstr (line, "public version") != NULL)
104 ace_comm->command_type = FR_ACE_COMMAND_PUBLIC;
105 else
106 ace_comm->command_type = FR_ACE_COMMAND_NONFREE;
107 }
108 return;
109 }
110
111 if (! ace_comm->list_started) {
112 if (ace_comm->command_type == FR_ACE_COMMAND_PUBLIC) {
113 if (g_str_has_prefix (line, "Date"))
114 ace_comm->list_started = TRUE;
115 }
116 else if (ace_comm->command_type == FR_ACE_COMMAND_NONFREE) {
117 if (g_str_has_prefix (line, " Date"))
118 ace_comm->list_started = TRUE;
119 }
120 return;
121 }
122
123 fdata = file_data_new ();
124
125 if (ace_comm->command_type == FR_ACE_COMMAND_PUBLIC)
126 fields = g_strsplit (line, "|", 6);
127 else if (ace_comm->command_type == FR_ACE_COMMAND_NONFREE)
128 fields = split_line (line, 5);
129
130 if ((fields == NULL) || (fields[0] == NULL) || (n_fields (fields) < 5))
131 return;
132
133 fdata->size = g_ascii_strtoull (fields[3], NULL, 10);
134 fdata->modified = mktime_from_string (fields[0], fields[1]);
135
136 if (ace_comm->command_type == FR_ACE_COMMAND_PUBLIC) {
137 field_name = fields[5];
138 field_name = field_name + 1;
139 }
140 else if (ace_comm->command_type == FR_ACE_COMMAND_NONFREE)
141 field_name = get_last_field (line, 6);
142
143 g_assert (field_name != NULL);
144 if (field_name[0] != '/') {
145 fdata->full_path = g_strconcat ("/", field_name, NULL);
146 fdata->original_path = fdata->full_path + 1;
147 }
148 else {
149 fdata->full_path = g_strdup (field_name);
150 fdata->original_path = fdata->full_path;
151 }
152
153 g_strfreev (fields);
154
155 fdata->name = g_strdup (file_name_from_path (fdata->full_path));
156 fdata->path = remove_level_from_path (fdata->full_path);
157
158 if (*fdata->name == 0)
159 file_data_free (fdata);
160 else
161 fr_command_add_file (comm, fdata);
162 }
163
164
165 static void
list__begin(gpointer data)166 list__begin (gpointer data)
167 {
168 FrCommandAce *comm = data;
169
170 comm->list_started = FALSE;
171 comm->command_type = FR_ACE_COMMAND_UNKNOWN;
172 }
173
174
175 static void
fr_command_ace_list(FrCommand * comm)176 fr_command_ace_list (FrCommand *comm)
177 {
178 fr_process_set_out_line_func (comm->process, process_line, comm);
179
180 fr_process_begin_command (comm->process, "unace");
181 fr_process_set_begin_func (comm->process, list__begin, comm);
182 fr_process_add_arg (comm->process, "v");
183 fr_process_add_arg (comm->process, "-y");
184 fr_process_add_arg (comm->process, comm->filename);
185 fr_process_end_command (comm->process);
186 fr_process_start (comm->process);
187 }
188
189
190 static void
fr_command_ace_extract(FrCommand * comm,const char * from_file,GList * file_list,const char * dest_dir,gboolean overwrite,gboolean skip_older,gboolean junk_paths)191 fr_command_ace_extract (FrCommand *comm,
192 const char *from_file,
193 GList *file_list,
194 const char *dest_dir,
195 gboolean overwrite,
196 gboolean skip_older,
197 gboolean junk_paths)
198 {
199 GList *scan;
200
201 fr_process_begin_command (comm->process, "unace");
202
203 if (dest_dir != NULL)
204 fr_process_set_working_dir (comm->process, dest_dir);
205
206 if (junk_paths)
207 fr_process_add_arg (comm->process, "e");
208 else
209 fr_process_add_arg (comm->process, "x");
210 fr_process_add_arg (comm->process, "-y");
211 fr_process_add_arg (comm->process, comm->filename);
212
213 for (scan = file_list; scan; scan = scan->next)
214 fr_process_add_arg (comm->process, scan->data);
215
216 fr_process_end_command (comm->process);
217 }
218
219
220 static void
fr_command_ace_test(FrCommand * comm)221 fr_command_ace_test (FrCommand *comm)
222 {
223 fr_process_begin_command (comm->process, "unace");
224 fr_process_add_arg (comm->process, "t");
225 fr_process_add_arg (comm->process, "-y");
226 fr_process_add_arg (comm->process, comm->filename);
227 fr_process_end_command (comm->process);
228 }
229
230
231 static void
fr_command_ace_handle_error(FrCommand * comm,FrProcError * error)232 fr_command_ace_handle_error (FrCommand *comm,
233 FrProcError *error)
234 {
235 /* FIXME */
236 }
237
238
239 const char *ace_mime_type[] = { "application/x-ace", NULL };
240
241
242 static const char **
fr_command_ace_get_mime_types(FrCommand * comm)243 fr_command_ace_get_mime_types (FrCommand *comm)
244 {
245 return ace_mime_type;
246 }
247
248
249 static FrCommandCap
fr_command_ace_get_capabilities(FrCommand * comm,const char * mime_type,gboolean check_command)250 fr_command_ace_get_capabilities (FrCommand *comm,
251 const char *mime_type,
252 gboolean check_command)
253 {
254 FrCommandCap capabilities;
255
256 capabilities = FR_COMMAND_CAN_ARCHIVE_MANY_FILES;
257 if (is_program_available ("unace", check_command))
258 capabilities |= FR_COMMAND_CAN_READ;
259
260 return capabilities;
261 }
262
263
264 static const char *
fr_command_ace_get_packages(FrCommand * comm,const char * mime_type)265 fr_command_ace_get_packages (FrCommand *comm,
266 const char *mime_type)
267 {
268 return PACKAGES ("unace");
269 }
270
271
272 static void
fr_command_ace_class_init(FrCommandAceClass * class)273 fr_command_ace_class_init (FrCommandAceClass *class)
274 {
275 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
276 FrCommandClass *afc;
277
278 parent_class = g_type_class_peek_parent (class);
279 afc = (FrCommandClass*) class;
280
281 gobject_class->finalize = fr_command_ace_finalize;
282
283 afc->list = fr_command_ace_list;
284 afc->extract = fr_command_ace_extract;
285 afc->test = fr_command_ace_test;
286 afc->handle_error = fr_command_ace_handle_error;
287 afc->get_mime_types = fr_command_ace_get_mime_types;
288 afc->get_capabilities = fr_command_ace_get_capabilities;
289 afc->get_packages = fr_command_ace_get_packages;
290 }
291
292
293 static void
fr_command_ace_init(FrCommand * comm)294 fr_command_ace_init (FrCommand *comm)
295 {
296 comm->propAddCanUpdate = TRUE;
297 comm->propAddCanReplace = TRUE;
298 comm->propExtractCanAvoidOverwrite = FALSE;
299 comm->propExtractCanSkipOlder = FALSE;
300 comm->propExtractCanJunkPaths = TRUE;
301 comm->propPassword = FALSE;
302 comm->propTest = TRUE;
303 }
304
305
306 static void
fr_command_ace_finalize(GObject * object)307 fr_command_ace_finalize (GObject *object)
308 {
309 g_return_if_fail (object != NULL);
310 g_return_if_fail (FR_IS_COMMAND_ACE (object));
311
312 /* Chain up */
313 if (G_OBJECT_CLASS (parent_class)->finalize)
314 G_OBJECT_CLASS (parent_class)->finalize (object);
315 }
316
317
318 GType
fr_command_ace_get_type()319 fr_command_ace_get_type ()
320 {
321 static GType type = 0;
322
323 if (! type) {
324 GTypeInfo type_info = {
325 sizeof (FrCommandAceClass),
326 NULL,
327 NULL,
328 (GClassInitFunc) fr_command_ace_class_init,
329 NULL,
330 NULL,
331 sizeof (FrCommandAce),
332 0,
333 (GInstanceInitFunc) fr_command_ace_init
334 };
335
336 type = g_type_register_static (FR_TYPE_COMMAND,
337 "FRCommandAce",
338 &type_info,
339 0);
340 }
341
342 return type;
343 }
344