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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Engrampa
*
* Copyright (C) 2001, 2003 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 <string.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <glib/gi18n.h>
#include <glib.h>
#include <gio/gio.h>
#include "eggsmclient.h"
#include "fr-init.h"
static char **remaining_args;
static char *add_to = NULL;
static int add;
static char *extract_to = NULL;
static int extract;
static int extract_here;
static char *default_url = NULL;
static void
_print_version_and_exit (const gchar *option_name, const gchar *value, gpointer data, GError *error)
{
g_print("%s %s\n", _("Engrampa Archive Manager"), VERSION);
exit (EXIT_SUCCESS);
}
/* argv[0] from main(); used as the command to restart the program */
static const char *program_argv0 = NULL;
static const GOptionEntry options[] = {
{ "add-to", 'a', 0, G_OPTION_ARG_STRING, &add_to,
N_("Add files to the specified archive and quit the program"),
N_("ARCHIVE") },
{ "add", 'd', 0, G_OPTION_ARG_NONE, &add,
N_("Add files asking the name of the archive and quit the program"),
NULL },
{ "extract-to", 'e', 0, G_OPTION_ARG_STRING, &extract_to,
N_("Extract archives to the specified folder and quit the program"),
N_("FOLDER") },
{ "extract", 'f', 0, G_OPTION_ARG_NONE, &extract,
N_("Extract archives asking the destination folder and quit the program"),
NULL },
{ "extract-here", 'h', 0, G_OPTION_ARG_NONE, &extract_here,
N_("Extract the contents of the archives in the archive folder and quit the program"),
NULL },
{ "default-dir", '\0', 0, G_OPTION_ARG_STRING, &default_url,
N_("Default folder to use for the '--add' and '--extract' commands"),
N_("FOLDER") },
{ "force", '\0', 0, G_OPTION_ARG_NONE, &ForceDirectoryCreation,
N_("Create destination folder without asking confirmation"),
NULL },
{ "version", 0, G_OPTION_FLAG_NO_ARG,
G_OPTION_ARG_CALLBACK, _print_version_and_exit,
N_("Show the application's version"),
NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &remaining_args,
NULL,
NULL },
{ NULL }
};
static void
fr_restore_session (EggSMClient *client)
{
GKeyFile *state = NULL;
guint i;
state = egg_sm_client_get_state_file (client);
i = g_key_file_get_integer (state, "Session", "archives", NULL);
for (; i > 0; i--) {
GtkWidget *window;
gchar *key, *archive;
key = g_strdup_printf ("archive%d", i);
archive = g_key_file_get_string (state, "Session", key, NULL);
g_free (key);
window = fr_window_new ();
gtk_widget_show (window);
if (strlen (archive))
fr_window_archive_open (FR_WINDOW (window), archive, GTK_WINDOW (window));
g_free (archive);
}
}
static char *
get_uri_from_command_line (const char *path)
{
GFile *file;
char *uri;
file = g_file_new_for_commandline_arg (path);
uri = g_file_get_uri (file);
g_object_unref (file);
return uri;
}
static void
prepare_app (void)
{
char *extract_to_uri = NULL;
char *add_to_uri = NULL;
EggSMClient *client = NULL;
client = egg_sm_client_get ();
if (egg_sm_client_is_resumed (client)) {
fr_restore_session (client);
return;
}
/**/
if (remaining_args == NULL) { /* No archive specified. */
fr_window_new ();
return;
}
if (extract_to != NULL)
extract_to_uri = get_uri_from_command_line (extract_to);
if (add_to != NULL)
add_to_uri = get_uri_from_command_line (add_to);
if ((add_to != NULL) || (add == 1)) { /* Add files to an archive */
GtkWidget *window;
GList *file_list = NULL;
const char *filename;
int i = 0;
window = fr_window_new ();
if (default_url != NULL)
fr_window_set_default_dir (FR_WINDOW (window), default_url, TRUE);
while ((filename = remaining_args[i++]) != NULL)
file_list = g_list_prepend (file_list, get_uri_from_command_line (filename));
file_list = g_list_reverse (file_list);
fr_window_new_batch (FR_WINDOW (window), _("Compress"));
fr_window_set_batch__add (FR_WINDOW (window), add_to_uri, file_list);
fr_window_append_batch_action (FR_WINDOW (window),
FR_BATCH_ACTION_QUIT,
NULL,
NULL);
fr_window_start_batch (FR_WINDOW (window));
}
else if ((extract_to != NULL) || (extract == 1) || (extract_here == 1)) {
/* Extract all archives. */
GtkWidget *window;
const char *archive;
int i = 0;
window = fr_window_new ();
if (default_url != NULL)
fr_window_set_default_dir (FR_WINDOW (window), default_url, TRUE);
fr_window_new_batch (FR_WINDOW (window), _("Extract archive"));
while ((archive = remaining_args[i++]) != NULL) {
char *archive_uri;
archive_uri = get_uri_from_command_line (archive);
if (extract_here == 1)
fr_window_set_batch__extract_here (FR_WINDOW (window),
archive_uri);
else
fr_window_set_batch__extract (FR_WINDOW (window),
archive_uri,
extract_to_uri);
g_free (archive_uri);
}
fr_window_append_batch_action (FR_WINDOW (window),
FR_BATCH_ACTION_QUIT,
NULL,
NULL);
fr_window_start_batch (FR_WINDOW (window));
}
else { /* Open each archive in a window */
const char *filename = NULL;
int i = 0;
while ((filename = remaining_args[i++]) != NULL) {
GtkWidget *window;
GFile *file;
char *uri;
window = fr_window_new ();
file = g_file_new_for_commandline_arg (filename);
uri = g_file_get_uri (file);
fr_window_archive_open (FR_WINDOW (window), uri, GTK_WINDOW (window));
g_free (uri);
g_object_unref (file);
}
}
g_free (add_to_uri);
g_free (extract_to_uri);
}
static void
startup_cb (GApplication *application)
{
initialize_data ();
prepare_app ();
}
static void
activate_cb (GApplication *application)
{
GList *link;
for (link = gtk_application_get_windows (GTK_APPLICATION (application));
link != NULL;
link = link->next)
{
if (! fr_window_is_batch_mode (FR_WINDOW (link->data)))
gtk_widget_show (GTK_WIDGET (link->data));
}
}
static void
fr_save_state (EggSMClient *client, GKeyFile *state, gpointer user_data)
{
/* discard command is automatically set by EggSMClient */
const char *argv[2] = { NULL };
GApplication *application;
guint i = 0;
/* restart command */
argv[0] = program_argv0;
argv[1] = NULL;
egg_sm_client_set_restart_command (client, 1, argv);
/* state */
application = g_application_get_default ();
if (application != NULL) {
GList *window;
for (window = gtk_application_get_windows (GTK_APPLICATION (application)), i = 0;
window != NULL;
window = window->next, i++)
{
FrWindow *session = window->data;
gchar *key;
key = g_strdup_printf ("archive%d", i);
if ((session->archive == NULL) || (session->archive->file == NULL)) {
g_key_file_set_string (state, "Session", key, "");
}
else {
gchar *uri;
uri = g_file_get_uri (session->archive->file);
g_key_file_set_string (state, "Session", key, uri);
g_free (uri);
}
g_free (key);
}
}
g_key_file_set_integer (state, "Session", "archives", i);
}
int
main (int argc, char **argv)
{
GOptionContext *context = NULL;
GError *error = NULL;
GtkApplication *app = NULL;
EggSMClient *client = NULL;
int status;
program_argv0 = argv[0];
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
context = g_option_context_new (N_("- Create and modify an archive"));
g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
g_option_context_add_group (context, gtk_get_option_group (TRUE));
g_option_context_add_group (context, egg_sm_client_get_option_group ());
if (! g_option_context_parse (context, &argc, &argv, &error)) {
g_critical ("Failed to parse arguments: %s", error->message);
g_error_free (error);
g_option_context_free (context);
return EXIT_FAILURE;
}
g_option_context_free (context);
g_set_application_name (_("Engrampa"));
gtk_window_set_default_icon_name ("engrampa");
client = egg_sm_client_get ();
g_signal_connect (client, "save-state", G_CALLBACK (fr_save_state), NULL);
gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
PKG_DATA_DIR G_DIR_SEPARATOR_S "icons");
app = gtk_application_new (NULL, G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "startup", G_CALLBACK (startup_cb), NULL);
g_signal_connect (app, "activate", G_CALLBACK (activate_cb), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
release_data ();
return status;
}
|