1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  *  Engrampa
5  *
6  *  Copyright (C) 2003 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-zoo.h"
34 
35 static void fr_command_zoo_class_init  (FrCommandZooClass *class);
36 static void fr_command_zoo_init        (FrCommand         *afile);
37 static void fr_command_zoo_finalize    (GObject           *object);
38 
39 /* Parent Class */
40 
41 static FrCommandClass *parent_class = NULL;
42 
43 
44 /* -- list -- */
45 
46 static time_t
mktime_from_string_zoo(char * mday_s,char * month_s,char * year_s,char * time_s)47 mktime_from_string_zoo (char *mday_s,
48 			char *month_s,
49 			char *year_s,
50 			char *time_s)
51 {
52 	struct tm  tm = {0, };
53 	char **fields;
54 	int year;
55 
56 	tm.tm_isdst = -1;
57 
58 	/* This will break in 2075 */
59 	year = atoi (year_s);
60 	if (year >= 75) {
61 		tm.tm_year = year;
62 	} else {
63 		tm.tm_year = 100 + year;
64 	}
65 
66 	if (g_ascii_strncasecmp(month_s, "Jan", 3) == 0) {
67 		tm.tm_mon = 0;
68 	} else if (g_ascii_strncasecmp(month_s, "Feb", 3) == 0) {
69 		tm.tm_mon = 1;
70 	} else if (g_ascii_strncasecmp(month_s, "Mar", 3) == 0) {
71 		tm.tm_mon = 2;
72 	} else if (g_ascii_strncasecmp(month_s, "Apr", 3) == 0) {
73 		tm.tm_mon = 3;
74 	} else if (g_ascii_strncasecmp(month_s, "May", 3) == 0) {
75 		tm.tm_mon = 4;
76 	} else if (g_ascii_strncasecmp(month_s, "Jun", 3) == 0) {
77 		tm.tm_mon = 5;
78 	} else if (g_ascii_strncasecmp(month_s, "Jul", 3) == 0) {
79 		tm.tm_mon = 6;
80 	} else if (g_ascii_strncasecmp(month_s, "Aug", 3) == 0) {
81 		tm.tm_mon = 7;
82 	} else if (g_ascii_strncasecmp(month_s, "Sep", 3) == 0) {
83 		tm.tm_mon = 8;
84 	} else if (g_ascii_strncasecmp(month_s, "Oct", 3) == 0) {
85 		tm.tm_mon = 9;
86 	} else if (g_ascii_strncasecmp(month_s, "Nov", 3) == 0) {
87 		tm.tm_mon = 10;
88 	} else if (g_ascii_strncasecmp(month_s, "Dec", 3) == 0) {
89 		tm.tm_mon = 11;
90 	}
91 
92 	tm.tm_mday = atoi (mday_s);
93 
94 	fields = g_strsplit (time_s, ":", 3);
95 	if (fields[0] != NULL) {
96 		tm.tm_hour = atoi (fields[0]);
97 		if (fields[1] != NULL) {
98 			tm.tm_min  = atoi (fields[1]);
99 			if (fields[2] != NULL)
100 				tm.tm_sec  = atoi (fields[2]);
101 		}
102 	}
103 
104 	g_strfreev (fields);
105 
106 	return mktime (&tm);
107 }
108 
109 
110 static char **
split_line_zoo(char * line)111 split_line_zoo (char *line)
112 {
113 	char       **fields;
114 	const char  *scan, *field_end;
115 	int          i;
116 
117 	if (line[0] == '-') {
118 		return NULL;
119 	}
120 
121 	fields = g_new0 (char *, 6);
122 	fields[5] = NULL;
123 
124 	/* Get Length */
125 	scan = eat_spaces (line);
126 	field_end = strchr (scan, ' ');
127 	fields[0] = g_strndup (scan, field_end - scan);
128 	scan = eat_spaces (field_end);
129 
130 	/* Toss CF, Size Now */
131 	for (i = 0; i < 2; i++) {
132 		field_end = strchr (scan, ' ');
133 		scan = eat_spaces (field_end);
134 	}
135 
136 	/* Get Day, Month, Year, Time */
137 	for (i = 1; i < 5; i++) {
138 		if (i == 2 && g_ascii_strncasecmp (scan, "file", 4) == 0) {
139 			g_strfreev(fields);
140 			return NULL;
141 		}
142 		field_end = strchr (scan, ' ');
143 		fields[i] = g_strndup (scan, field_end - scan);
144 		scan = eat_spaces (field_end);
145 	}
146 
147 	return fields;
148 }
149 
150 
151 static const char *
get_last_field_zoo(char * line)152 get_last_field_zoo (char *line)
153 {
154 	const char *field;
155 	int         i;
156 	int         n = 6;
157 
158 	field = eat_spaces (line);
159 	for (i = 0; i < n; i++) {
160 		field = strchr (field, ' ');
161 		field = eat_spaces (field);
162 	}
163 	field = strchr (field, ' ');
164 	if (g_ascii_strncasecmp (field, " C ", 3) == 0) {
165 		field = eat_spaces (field);
166 		field = strchr (field, ' ');
167 		field = eat_spaces (field);
168 	} else
169 		field = eat_spaces (field);
170 
171 	return field;
172 }
173 
174 
175 static void
process_zoo_line(char * line,gpointer data)176 process_zoo_line (char     *line,
177 		  gpointer  data)
178 {
179 	FileData    *fdata;
180 	FrCommand   *zoo_comm = FR_COMMAND (data);
181 	char       **fields;
182 	const char  *name_field;
183 
184 	g_return_if_fail (line != NULL);
185 	if (line[0] == '-')
186 		return;
187 
188 	fields = split_line_zoo (line);
189 	if (fields == NULL)
190 		return;
191 
192 	fdata = file_data_new ();
193 
194 	fdata->size = g_ascii_strtoull (fields[0], NULL, 10);
195 	fdata->modified = mktime_from_string_zoo (fields[1],
196 						  fields[2],
197 						  fields[3],
198 						  fields[4]);
199 	g_strfreev (fields);
200 
201 	/* Full path */
202 
203 	name_field = get_last_field_zoo (line);
204 	if (*(name_field) == '/') {
205 		fdata->full_path = g_strdup (name_field);
206 		fdata->original_path = fdata->full_path;
207 	} else {
208 		fdata->full_path = g_strconcat ("/", name_field, NULL);
209 		fdata->original_path = fdata->full_path + 1;
210 	}
211 
212 	fdata->name = g_strdup (file_name_from_path (fdata->full_path));
213 	fdata->path = remove_level_from_path (fdata->full_path);
214 
215 	if (*fdata->name == 0)
216 		file_data_free (fdata);
217 	else
218 		fr_command_add_file (zoo_comm, fdata);
219 }
220 
221 
222 static void
fr_command_zoo_list(FrCommand * zoo_comm)223 fr_command_zoo_list (FrCommand  *zoo_comm)
224 {
225 	fr_process_set_out_line_func (zoo_comm->process, process_zoo_line, zoo_comm);
226 
227 	fr_process_begin_command (zoo_comm->process, "zoo");
228 	fr_process_add_arg (zoo_comm->process, "lq");
229 	fr_process_add_arg (zoo_comm->process, zoo_comm->filename);
230 	fr_process_end_command (zoo_comm->process);
231 	fr_process_start (zoo_comm->process);
232 }
233 
234 
235 static void
fr_command_zoo_add(FrCommand * comm,const char * from_file,GList * file_list,const char * base_dir,gboolean update,gboolean recursive)236 fr_command_zoo_add (FrCommand     *comm,
237 		    const char    *from_file,
238 		    GList         *file_list,
239 		    const char    *base_dir,
240 		    gboolean       update,
241 		    gboolean       recursive)
242 {
243 	GList        *scan;
244 
245 	/* Add files. */
246 
247 	fr_process_begin_command (comm->process, "zoo");
248 
249 	fr_process_set_working_dir (comm->process, base_dir);
250 
251 	if (update)
252 		fr_process_add_arg (comm->process, "auP");
253 	else
254 		fr_process_add_arg (comm->process, "aP");
255 
256 	fr_process_add_arg (comm->process, comm->filename);
257 
258 	for (scan = file_list; scan; scan = scan->next)
259 		fr_process_add_arg (comm->process, scan->data);
260 	fr_process_end_command (comm->process);
261 }
262 
263 
264 static void
fr_command_zoo_delete(FrCommand * comm,const char * from_file,GList * file_list)265 fr_command_zoo_delete (FrCommand *comm,
266 		       const char  *from_file,
267 		       GList     *file_list)
268 {
269 	GList        *scan;
270 
271 	/* Delete files. */
272 
273 	fr_process_begin_command (comm->process, "zoo");
274 	fr_process_add_arg (comm->process, "DP");
275 	fr_process_add_arg (comm->process, comm->filename);
276 
277 	for (scan = file_list; scan; scan = scan->next)
278 		fr_process_add_arg (comm->process, scan->data);
279 	fr_process_end_command (comm->process);
280 }
281 
282 
283 static void
fr_command_zoo_extract(FrCommand * comm,const char * from_file,GList * file_list,const char * dest_dir,gboolean overwrite,gboolean skip_older,gboolean junk_paths)284 fr_command_zoo_extract (FrCommand  *comm,
285 			const char  *from_file,
286 			GList      *file_list,
287 			const char *dest_dir,
288 			gboolean    overwrite,
289 			gboolean    skip_older,
290 			gboolean    junk_paths)
291 {
292 	GList *scan;
293 
294 	fr_process_begin_command (comm->process, "zoo");
295 
296 	if (overwrite)
297 		fr_process_add_arg (comm->process, "xO");
298 	else
299 		fr_process_add_arg (comm->process, "x");
300 
301 	fr_process_add_arg (comm->process, comm->filename);
302 
303 	if (dest_dir != NULL)
304 		fr_process_set_working_dir (comm->process, dest_dir);
305 
306 	for (scan = file_list; scan; scan = scan->next)
307 		fr_process_add_arg (comm->process, scan->data);
308 
309 	fr_process_end_command (comm->process);
310 }
311 
312 
313 static void
fr_command_zoo_test(FrCommand * comm)314 fr_command_zoo_test (FrCommand   *comm)
315 {
316 	fr_process_begin_command (comm->process, "zoo");
317 	fr_process_add_arg (comm->process, "-test");
318 	fr_process_add_arg (comm->process, comm->filename);
319 	fr_process_end_command (comm->process);
320 }
321 
322 
323 const char *zoo_mime_type[] = { "application/x-zoo", NULL };
324 
325 
326 static const char **
fr_command_zoo_get_mime_types(FrCommand * comm)327 fr_command_zoo_get_mime_types (FrCommand *comm)
328 {
329 	return zoo_mime_type;
330 }
331 
332 
333 static FrCommandCap
fr_command_zoo_get_capabilities(FrCommand * comm,const char * mime_type,gboolean check_command)334 fr_command_zoo_get_capabilities (FrCommand  *comm,
335 			         const char *mime_type,
336 				 gboolean    check_command)
337 {
338 	FrCommandCap capabilities;
339 
340 	capabilities = FR_COMMAND_CAN_ARCHIVE_MANY_FILES;
341 	if (is_program_available ("zoo", check_command))
342 		capabilities |= FR_COMMAND_CAN_READ_WRITE;
343 
344 	return capabilities;
345 }
346 
347 
348 static const char *
fr_command_zoo_get_packages(FrCommand * comm,const char * mime_type)349 fr_command_zoo_get_packages (FrCommand  *comm,
350 			     const char *mime_type)
351 {
352 	return PACKAGES ("zoo");
353 }
354 
355 
356 static void
fr_command_zoo_class_init(FrCommandZooClass * class)357 fr_command_zoo_class_init (FrCommandZooClass *class)
358 {
359         GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
360         FrCommandClass *afc;
361 
362         parent_class = g_type_class_peek_parent (class);
363 	afc = (FrCommandClass*) class;
364 
365 	gobject_class->finalize = fr_command_zoo_finalize;
366 
367         afc->list             = fr_command_zoo_list;
368 	afc->add              = fr_command_zoo_add;
369 	afc->delete_           = fr_command_zoo_delete;
370 	afc->extract          = fr_command_zoo_extract;
371 	afc->test             = fr_command_zoo_test;
372 	afc->get_mime_types   = fr_command_zoo_get_mime_types;
373 	afc->get_capabilities = fr_command_zoo_get_capabilities;
374 	afc->get_packages     = fr_command_zoo_get_packages;
375 }
376 
377 
378 static void
fr_command_zoo_init(FrCommand * comm)379 fr_command_zoo_init (FrCommand *comm)
380 {
381 	comm->propAddCanUpdate             = TRUE;
382 	comm->propAddCanReplace            = FALSE;
383 	comm->propExtractCanAvoidOverwrite = FALSE;
384 	comm->propExtractCanSkipOlder      = FALSE;
385 	comm->propExtractCanJunkPaths      = FALSE;
386 	comm->propPassword                 = FALSE;
387 	comm->propTest                     = TRUE;
388 }
389 
390 
391 static void
fr_command_zoo_finalize(GObject * object)392 fr_command_zoo_finalize (GObject *object)
393 {
394         g_return_if_fail (object != NULL);
395         g_return_if_fail (FR_IS_COMMAND_ZOO (object));
396 
397 	/* Chain up */
398         if (G_OBJECT_CLASS (parent_class)->finalize)
399 		G_OBJECT_CLASS (parent_class)->finalize (object);
400 }
401 
402 
403 GType
fr_command_zoo_get_type()404 fr_command_zoo_get_type ()
405 {
406         static GType type = 0;
407 
408         if (! type) {
409                 GTypeInfo type_info = {
410 			sizeof (FrCommandZooClass),
411 			NULL,
412 			NULL,
413 			(GClassInitFunc) fr_command_zoo_class_init,
414 			NULL,
415 			NULL,
416 			sizeof (FrCommandZoo),
417 			0,
418 			(GInstanceInitFunc) fr_command_zoo_init
419 		};
420 
421 		type = g_type_register_static (FR_TYPE_COMMAND,
422 					       "FRCommandZoo",
423 					       &type_info,
424 					       0);
425         }
426 
427         return type;
428 }
429