~ubuntu-branches/ubuntu/jaunty/xarchiver/jaunty

« back to all changes in this revision

Viewing changes to src/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-11-07 14:54:00 UTC
  • mfrom: (1.1.7 upstream) (2.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20081107145400-z0j3jmgads8coae2
Tags: 0.5.1-1
MergingĀ upstreamĀ versionĀ 0.5.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2006 Giuseppe Torelli - <colossus73@gmail.com>
 
2
 *  Copyright (C) 2008 Giuseppe Torelli - <colossus73@gmail.com>
3
3
 *
4
4
 *  This program is free software; you can redistribute it and/or modify
5
5
 *  it under the terms of the GNU General Public License as published by
18
18
 
19
19
#include "config.h"
20
20
#include "main.h"
 
21
#include "archive.h"
21
22
#include "string_utils.h"
 
23
#include "mime.h"
22
24
 
23
25
#ifdef HAVE_SOCKET
24
26
#include "socket.h"
25
27
#endif
26
28
 
27
 
gint exit_status;
28
 
gchar *cli_command = NULL;
29
29
gchar *absolute_path = NULL;
30
 
gchar *archive_name = NULL;
 
30
//gchar *archive_name = NULL;
31
31
gchar *_current_dir = NULL;
32
32
gchar *extract_path = NULL;
33
33
GError *cli_error = NULL;
34
 
gboolean error_output, file_to_open, ask_and_extract, ask_and_add;
35
 
gboolean cli = FALSE;
 
34
gboolean error_output, file_to_open, ask_and_extract, add_files,ask_and_add, multi_extract;
 
35
gboolean batch_mode = FALSE;
36
36
gboolean unrar = FALSE;
 
37
gboolean sevenzr = FALSE, sevenza = FALSE, xdg_open = FALSE;
 
38
int response;
37
39
extern gchar *current_open_directory;
 
40
extern int status;
 
41
 
 
42
extern Progress_bar_data *pb;
 
43
Prefs_dialog_data   *prefs_window   = NULL;
 
44
Extract_dialog_data *extract_window = NULL;
 
45
Add_dialog_data     *add_window         = NULL;
 
46
Multi_extract_data      *multi_extract_window   = NULL;
 
47
 
 
48
delete_func             delete[XARCHIVETYPE_COUNT]      = {NULL};
 
49
add_func                add[XARCHIVETYPE_COUNT]         = {NULL};
 
50
extract_func    extract[XARCHIVETYPE_COUNT]     = {NULL};
 
51
test_func               test[XARCHIVETYPE_COUNT]        = {NULL};
 
52
open_func               open_archive[XARCHIVETYPE_COUNT]        = {NULL};
38
53
 
39
54
static GOptionEntry entries[] =
40
55
{
41
56
        {       "extract-to", 'x', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_FILENAME, &extract_path,
42
 
                N_("Extract archive to the directory specified by destination_path and quits."),
43
 
                N_("destination_path archive")
 
57
                N_("Extract archive to the destination directory and quits."),
 
58
                N_("destination archive")
44
59
        },
45
60
        {       "extract", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &ask_and_extract,
46
 
                N_("Extract archive by asking the destination directory and quits."),
 
61
                N_("Extract archive by asking the extraction directory and quits."),
47
62
                N_("archive")
48
63
        },
49
 
        {       "add-to", 'd', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_FILENAME, &archive_name,
 
64
        {       "multi-extract", 'm', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &multi_extract,
 
65
                N_("Multi-extract archives"),
 
66
                N_("filenames")
 
67
        },
 
68
        {       "add-to", 'd', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &add_files,
50
69
                N_("Add the given files by asking the name of the archive and quits."),
51
70
                N_("file1 file2 file3 ... fileN")
52
71
        },
59
78
 
60
79
int main (int argc, char **argv)
61
80
{
 
81
        XArchive *archive = NULL;
 
82
        gboolean no_bzip2_gzip;
 
83
        unsigned short int x;
62
84
        #ifdef ENABLE_NLS
63
85
        bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
64
86
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
72
94
 
73
95
        if (socket_info.lock_socket < 0)
74
96
        {
75
 
                // Socket exists
76
 
                if (argc > 1)   // filenames were sent to first instance, so quit
 
97
                /* Socket exists; filenames were sent to first instance, so quit */
 
98
                if (argc > 1)
77
99
                        return 0;
78
100
        }
79
101
#endif
85
107
                g_error_free (cli_error);
86
108
                return 0;
87
109
        }
88
 
        if (ask_and_extract || ask_and_add || archive_name != NULL || extract_path != NULL)
89
 
                cli = TRUE;
90
 
 
91
 
        if (cli == TRUE)
 
110
        if (multi_extract || add_files || ask_and_extract || ask_and_add || extract_path != NULL)
 
111
                batch_mode = TRUE;
 
112
 
 
113
        xa_set_available_archivers();
 
114
        prefs_window   = xa_create_prefs_dialog();
 
115
        extract_window = xa_create_extract_dialog();
 
116
        add_window     = xa_create_add_dialog();
 
117
        multi_extract_window = xa_create_multi_extract_dialog();
 
118
        xa_prefs_load_options(prefs_window);
 
119
 
 
120
        if (batch_mode == TRUE)
92
121
        {
93
 
                GetAvailableCompressors();
94
 
                ArchiveSuffix = g_list_reverse (ArchiveSuffix);
95
 
                ArchiveType = g_list_reverse (ArchiveType);
96
 
                MainWindow = create_MainWindow ();
97
 
                gtk_main_iteration_do (FALSE);
98
 
                g_print ("Xarchiver " VERSION " (\xC2\xA9)2005-2006 Giuseppe Torelli (colossus73)\n\n");
 
122
                xa_main_window = NULL;
 
123
                archive = xa_init_structure_from_cmd_line (argv[1]);
 
124
                g_print ("Xarchiver " VERSION " (\xC2\xA9)2005-2008 Giuseppe Torelli\n");
99
125
 
100
126
                /* Switch -x */
101
127
                if (extract_path != NULL)
102
128
                {
103
129
                        if (argv[1] == NULL)
104
130
                        {
105
 
                                response = ShowGtkMessageDialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_("Can't extract files from the archive:"),_("You missed the archive name!\n"));
106
 
                                return 0;
 
131
                                response = xa_show_message_dialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_("Can't extract files from the archive:"),_("You missed the archive name!\n"));
 
132
                                return -1;
107
133
                        }
108
 
                        for ( x = 1; x < argc; x++)
 
134
                        if (xa_detect_encrypted_archive(archive))
109
135
                        {
110
 
                                archive_cmd = xa_init_structure_from_cmd_line ( argv[x] );
111
 
                                if (archive_cmd != NULL)
112
 
                                {
113
 
                                        if (archive_cmd->has_passwd)
114
 
                                        {
115
 
                                                archive_cmd->passwd = password_dialog (archive_cmd);
116
 
                                                if (archive_cmd->passwd == NULL)
117
 
                                                        goto done;
118
 
                                        }
119
 
                                        GString *string = g_string_new ( "" );
120
 
                                        archive_cmd->full_path = 1;
121
 
                                        archive_cmd->overwrite = 1;
122
 
                                        gchar *escaped_path = EscapeBadChars (extract_path , "$\'`\"\\!?* ()[]&|@#:;");
123
 
                                        archive_cmd->extraction_path = g_strdup (extract_path);
124
 
                                        cli_command = xa_extract_single_files ( archive_cmd , string, escaped_path );
125
 
                                        g_free (escaped_path);
126
 
                                        if ( cli_command != NULL )
127
 
                                                error_output = SpawnSyncCommand ( cli_command );
128
 
                                        g_string_free (string, TRUE);
129
 
                                }
 
136
                                archive->has_passwd = TRUE;
 
137
                                archive->passwd = xa_create_password_dialog(archive);
 
138
                                if (archive->passwd == NULL)
 
139
                                        goto done;
130
140
                        }
 
141
                        GSList *string = NULL;
 
142
                        archive->full_path = 1;
 
143
                        archive->overwrite = 1;
 
144
                        gchar *escaped_path = xa_escape_bad_chars (extract_path,"$\'`\"\\!?* ()[]&|@#:;");
 
145
                        archive->extraction_path = escaped_path;
 
146
                        archive->status = XA_ARCHIVESTATUS_EXTRACT;
 
147
                        (*archive->extract) (archive,string);
131
148
                }
132
 
 
133
149
                /* Switch -e */
134
 
                else if (ask_and_extract)
 
150
                else if (ask_and_extract && archive != NULL)
135
151
                {
136
152
                        if (argv[1] == NULL)
137
153
                        {
138
 
                                response = ShowGtkMessageDialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_("Can't extract files from the archive:"),_("You missed the archive name!\n"));
139
 
                                return 0;
140
 
                        }
141
 
                        archive_cmd = xa_init_structure_from_cmd_line ( argv[1] );
142
 
                        if (archive_cmd != NULL)
143
 
                        {
144
 
                                extract_window = xa_create_extract_dialog ( 0 , archive_cmd);
145
 
                                cli_command = xa_parse_extract_dialog_options ( archive_cmd , extract_window, NULL );
146
 
                                gtk_widget_destroy ( extract_window->dialog1 );
147
 
                                if ( cli_command != NULL )
148
 
                                        error_output = SpawnSyncCommand ( cli_command );
149
 
                                g_free (extract_window);
150
 
                        }
 
154
                                response = xa_show_message_dialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_("Can't extract files from the archive:"),_("You missed the archive name!\n"));
 
155
                                return -1;
 
156
                        }
 
157
                        if (xa_detect_encrypted_archive(archive))
 
158
                                archive->has_passwd = TRUE;
 
159
 
 
160
                        xa_set_extract_dialog_options(extract_window,0,archive);
 
161
                        xa_parse_extract_dialog_options (archive,extract_window,NULL);
 
162
                        gtk_widget_destroy (extract_window->dialog1);
 
163
                        g_free (extract_window);
 
164
                }
 
165
                /* Switch -m */
 
166
                else if (multi_extract)
 
167
                {
 
168
                        Multi_extract_data *multi_extract = NULL;
 
169
                        multi_extract = xa_create_multi_extract_dialog();
 
170
                        for (x = 1; x< argc; x++)
 
171
                                xa_add_files_liststore(argv[x],multi_extract);
 
172
                        xa_parse_multi_extract_archive(multi_extract);
 
173
                        gtk_widget_destroy (multi_extract->multi_extract);
 
174
                        g_free(multi_extract);
151
175
                }
152
176
                /* Switch -d */
153
 
                else if (archive_name != NULL)
 
177
                else if (add_files)
154
178
                {
155
 
                        XArchive *archive_cmd = NULL;
156
 
                        archive_cmd = xa_new_archive_dialog (archive_name );
157
 
                        if (archive_cmd == NULL)
158
 
                                return 0;
 
179
                        if (argc > 1 || g_file_test (argv[1],G_FILE_TEST_IS_DIR))
 
180
                                no_bzip2_gzip = TRUE;
 
181
                        else
 
182
                                no_bzip2_gzip = FALSE;
 
183
                        archive = xa_new_archive_dialog (argv[1],NULL,no_bzip2_gzip);
 
184
                        if (archive == NULL)
 
185
                                return -1;
159
186
 
160
 
                        if (archive_cmd->path != NULL)
 
187
                        if (archive->path != NULL)
161
188
                        {
162
 
                                _current_dir = g_path_get_dirname(archive_name);
 
189
                                xa_create_temp_directory(archive);
 
190
                                archive->add_recurse = TRUE;
 
191
                                _current_dir = g_path_get_dirname(argv[1]);
163
192
                                chdir (_current_dir);
164
 
                                g_free (_current_dir);
165
 
                                GString *string = g_string_new ( "" );
166
 
 
167
 
                                if ( g_file_test ( archive_name,G_FILE_TEST_EXISTS) )
168
 
                                {
169
 
                                        _current_dir = g_path_get_basename ( archive_name );
170
 
                                        ConcatenateFileNames2 ( _current_dir, string );
171
 
                                        g_free (_current_dir);
172
 
                                }
173
 
 
 
193
                                g_free(_current_dir);
 
194
                                GSList *files = NULL;
174
195
                                for (x = 1; x< argc; x++)
175
196
                                {
176
 
                                        _current_dir = g_path_get_basename ( argv[x] );
177
 
                                        ConcatenateFileNames2 ( _current_dir, string );
 
197
                                        _current_dir = g_path_get_basename(argv[x]);
 
198
                                        files = g_slist_append(files,xa_escape_filename(_current_dir,"$'`\"\\!?* ()[]&|:;<>#"));
178
199
                                        g_free (_current_dir);
179
200
                                }
180
 
                                if ( archive_cmd->type == XARCHIVETYPE_7ZIP)
181
 
                                        archive_cmd->add_recurse = FALSE;
182
 
                                else
183
 
                                        archive_cmd->add_recurse = TRUE;
184
 
                                cli_command = xa_add_single_files ( archive_cmd , string, NULL);
185
 
                                if (cli_command != NULL)
186
 
                                        error_output = SpawnSyncCommand ( cli_command );
187
 
                                g_string_free (string, TRUE);
 
201
                                xa_execute_add_commands(archive,files,NULL);
188
202
                        }
189
 
                        if (cli_command != NULL)
190
 
                                g_free (cli_command);
191
203
                }
192
204
                /* Switch -a */
193
205
                else if (ask_and_add)
194
206
                {
195
207
                        if (argv[1] == NULL)
196
208
                        {
197
 
                                response = ShowGtkMessageDialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_("Can't add files to the archive:"),_("You missed the archive name!\n"));
198
 
                                return 0;
 
209
                                response = xa_show_message_dialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_("Can't add files to the archive:"),_("You missed the archive name!\n"));
 
210
                                return -1;
199
211
                        }
200
 
                        archive_cmd = xa_init_structure_from_cmd_line ( argv[1] );
201
 
                        if (archive_cmd != NULL)
 
212
                        if (archive != NULL)
202
213
                        {
203
 
                                add_window = xa_create_add_dialog (archive_cmd);
204
 
                                cli_command = xa_parse_add_dialog_options ( archive_cmd, add_window );
205
 
                                gtk_widget_destroy ( add_window->dialog1 );
206
 
                                if (cli_command != NULL)
207
 
                                        error_output = SpawnSyncCommand ( cli_command );
 
214
                                xa_set_add_dialog_options(add_window,archive);
 
215
                                xa_parse_add_dialog_options (archive,add_window);
 
216
                                gtk_widget_destroy (add_window->dialog1);
208
217
                                g_free (add_window);
209
218
                        }
210
219
                }
211
 
done:   g_list_free ( ArchiveSuffix);
212
 
                g_list_free ( ArchiveType);
213
 
                if (archive_cmd != NULL)
214
 
                        xa_clean_archive_structure ( archive_cmd );
215
 
                return exit_status;
 
220
done:   g_list_free (ArchiveSuffix);
 
221
                g_list_free (ArchiveType);
 
222
 
 
223
                if (pb != NULL)
 
224
                {
 
225
                        gtk_widget_destroy(pb->progress_window);
 
226
                        g_free(pb);
 
227
                }
 
228
                if (archive != NULL)
 
229
                        xa_clean_archive_structure (archive);
 
230
                #ifdef HAVE_SOCKET
 
231
                        socket_finalize();
 
232
                #endif
216
233
        }
217
234
        else
218
235
        {
219
 
                GetAvailableCompressors();
220
 
                ArchiveSuffix = g_list_reverse (ArchiveSuffix);
221
 
                ArchiveType = g_list_reverse (ArchiveType);
222
 
 
223
 
                MainWindow = create_MainWindow ();
224
 
                xa_show_cmd_line_output (NULL);
225
 
                gtk_window_set_position ( GTK_WINDOW (MainWindow),GTK_WIN_POS_CENTER);
226
 
                gtk_window_set_default_size (GTK_WINDOW(MainWindow), 600, 400);
227
 
                Update_StatusBar ( _("Ready."));
228
 
                gtk_widget_show (MainWindow);
229
 
 
230
 
                /* This to open the archive_cmd from the command line */
 
236
                xa_main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
237
                xa_create_main_window (xa_main_window,  gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (prefs_window->show_location_bar)),
 
238
                                                                                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (prefs_window->store_output)),
 
239
                                                                                                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (prefs_window->show_sidebar)));
 
240
 
 
241
                gtk_window_set_transient_for (GTK_WINDOW (extract_window->dialog1),GTK_WINDOW (xa_main_window));
 
242
                gtk_window_set_transient_for (GTK_WINDOW (add_window->dialog1),GTK_WINDOW (xa_main_window));
 
243
                gtk_window_set_transient_for (GTK_WINDOW (prefs_window->dialog1),GTK_WINDOW (xa_main_window));
 
244
                gtk_window_set_transient_for (GTK_WINDOW (multi_extract_window->multi_extract),GTK_WINDOW (xa_main_window));
 
245
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(prefs_window->check_save_geometry)) && prefs_window->geometry[0] != -1)
 
246
                {
 
247
                        gtk_window_move (GTK_WINDOW(xa_main_window), prefs_window->geometry[0], prefs_window->geometry[1]);
 
248
                        gtk_window_set_default_size (GTK_WINDOW(xa_main_window), prefs_window->geometry[XARCHIVETYPE_7ZIP], prefs_window->geometry[XARCHIVETYPE_ARJ]);
 
249
                        gtk_paned_set_position (GTK_PANED (hpaned1),prefs_window->geometry[XARCHIVETYPE_DEB]);
 
250
                }
 
251
                else
 
252
                {
 
253
                        gtk_window_set_position (GTK_WINDOW(xa_main_window),GTK_WIN_POS_CENTER);
 
254
                        gtk_window_set_default_size (GTK_WINDOW(xa_main_window), 600, 400);
 
255
                        gtk_paned_set_position (GTK_PANED (hpaned1),200);
 
256
                }
 
257
                gtk_label_set_text(GTK_LABEL(total_label),_("Select \"New\" to create or \"Open\" to open an archive"));
 
258
                gtk_widget_show (xa_main_window);
 
259
 
 
260
                /* This to open the archive from the command line */
231
261
                if ( argc == 2 )
232
262
                {
233
263
                        gchar *dummy = g_strdup(argv[1]);
234
264
                        current_open_directory = g_path_get_dirname (dummy);
235
 
                        xa_open_archive ( NULL , dummy );
 
265
                        if (strcmp(current_open_directory,"..") == 0)
 
266
                        {
 
267
                                g_free (current_open_directory);
 
268
                                current_open_directory = g_get_current_dir();
 
269
                        }
 
270
                        xa_open_archive (NULL,dummy);
236
271
                }
237
272
                #ifdef HAVE_SOCKET
238
273
                if (! socket_info.ignore_socket && socket_info.lock_socket > 0)
239
274
                {
240
275
                        socket_info.read_ioc = g_io_channel_unix_new(socket_info.lock_socket);
241
 
                        socket_info.lock_socket_tag = g_io_add_watch(socket_info.read_ioc,      G_IO_IN|G_IO_PRI|G_IO_ERR, socket_lock_input_cb, MainWindow);
 
276
                        socket_info.lock_socket_tag = g_io_add_watch(socket_info.read_ioc,      G_IO_IN|G_IO_PRI|G_IO_ERR, socket_lock_input_cb, xa_main_window);
242
277
                }
243
278
                #endif
244
279
                gtk_main ();
245
 
                g_list_free ( ArchiveSuffix);
246
 
                g_list_free ( ArchiveType);
247
 
                return 0;
248
 
        }
249
 
}
250
 
 
251
 
gchar *get_argv_filename(const gchar *filename)
252
 
{
253
 
        gchar *result;
254
 
 
255
 
        if (g_path_is_absolute(filename))
256
 
                result = g_strdup(filename);
257
 
        else
258
 
        {
259
 
                //use current dir
260
 
                gchar *cur_dir = g_get_current_dir();
261
 
                result = g_strjoin(
262
 
                        G_DIR_SEPARATOR_S, cur_dir, filename, NULL);
263
 
                g_free(cur_dir);
264
 
        }
265
 
        return result;
266
 
}
267
 
 
268
 
void GetAvailableCompressors()
269
 
{
 
280
                g_list_free (ArchiveSuffix);
 
281
                g_list_free (ArchiveType);
 
282
        }
 
283
        return 0;
 
284
}
 
285
 
 
286
void xa_set_available_archivers()
 
287
{
 
288
        open_archive[0]  = 0;
 
289
        open_archive[XARCHIVETYPE_7ZIP]  = &xa_open_7zip;
 
290
        open_archive[XARCHIVETYPE_ARJ]  = &xa_open_arj;
 
291
        open_archive[XARCHIVETYPE_DEB]  = &xa_open_deb;
 
292
        open_archive[XARCHIVETYPE_BZIP2]  = &xa_open_bzip2_lzma;
 
293
        open_archive[XARCHIVETYPE_GZIP]  = &xa_open_gzip;
 
294
        open_archive[XARCHIVETYPE_LZMA]  = &xa_open_bzip2_lzma;
 
295
        open_archive[XARCHIVETYPE_RAR]  = &xa_open_rar;
 
296
        open_archive[XARCHIVETYPE_RPM]  = &xa_open_rpm;
 
297
        open_archive[XARCHIVETYPE_TAR]  = open_archive[XARCHIVETYPE_TAR_BZ2] = open_archive[XARCHIVETYPE_TAR_GZ] = open_archive[XARCHIVETYPE_TAR_LZMA] = open_archive[XARCHIVETYPE_TAR_LZOP] = &xa_open_tar;
 
298
        open_archive[XARCHIVETYPE_ZIP] = &xa_open_zip;
 
299
        open_archive[XARCHIVETYPE_LHA] = &xa_open_lha;
 
300
        open_archive[XARCHIVETYPE_LZOP] = &xa_open_bzip2_lzma;
 
301
        
 
302
        delete[0]  = 0;
 
303
        delete[XARCHIVETYPE_7ZIP]  = &xa_7zip_delete;
 
304
        delete[XARCHIVETYPE_ARJ]  = &xa_arj_delete;
 
305
        delete[XARCHIVETYPE_DEB]  = 0;
 
306
        delete[XARCHIVETYPE_BZIP2]  = 0;
 
307
        delete[XARCHIVETYPE_GZIP]  = 0;
 
308
        delete[XARCHIVETYPE_LZMA]  = 0;
 
309
        delete[XARCHIVETYPE_RAR]  = &xa_rar_delete;
 
310
        delete[XARCHIVETYPE_RPM]  = 0;
 
311
        delete[XARCHIVETYPE_TAR]  = delete[XARCHIVETYPE_TAR_BZ2] = delete[XARCHIVETYPE_TAR_GZ] = delete[XARCHIVETYPE_TAR_LZMA] = delete[XARCHIVETYPE_TAR_LZOP] = &xa_tar_delete;
 
312
        delete[XARCHIVETYPE_ZIP] = &xa_zip_delete;
 
313
        delete[XARCHIVETYPE_LHA] = &xa_lha_delete;
 
314
        delete[XARCHIVETYPE_LZOP] = 0;
 
315
        
 
316
        
 
317
        add[0]  = 0;
 
318
        add[XARCHIVETYPE_7ZIP]  = &xa_7zip_add;
 
319
        add[XARCHIVETYPE_ARJ]  = &xa_arj_add;
 
320
        add[XARCHIVETYPE_DEB]  = 0;
 
321
        add[XARCHIVETYPE_BZIP2]  = add[XARCHIVETYPE_GZIP] = add[XARCHIVETYPE_LZMA] = &xa_tar_add;
 
322
        add[XARCHIVETYPE_RAR]  = &xa_rar_add;
 
323
        add[XARCHIVETYPE_RPM]  = 0;
 
324
        add[XARCHIVETYPE_TAR]  = add[XARCHIVETYPE_TAR_BZ2] = add[XARCHIVETYPE_TAR_GZ] = add[XARCHIVETYPE_TAR_LZMA] = add[XARCHIVETYPE_TAR_LZOP] = &xa_tar_add;
 
325
        add[XARCHIVETYPE_ZIP] = &xa_zip_add;
 
326
        add[XARCHIVETYPE_LHA] = &xa_lha_add;
 
327
        add[XARCHIVETYPE_LZOP] = &xa_tar_add;
 
328
        
 
329
        extract[0]  = 0;
 
330
        extract[XARCHIVETYPE_7ZIP]  = &xa_7zip_extract;
 
331
        extract[XARCHIVETYPE_ARJ]  = &xa_arj_extract;
 
332
        extract[XARCHIVETYPE_DEB]  = &xa_deb_extract;;
 
333
        extract[XARCHIVETYPE_BZIP2]  = extract[XARCHIVETYPE_GZIP] = extract[XARCHIVETYPE_LZMA] = &xa_tar_extract;
 
334
        extract[XARCHIVETYPE_RAR]  = &xa_rar_extract;
 
335
        extract[XARCHIVETYPE_RPM]  = &xa_rpm_extract;
 
336
        extract[XARCHIVETYPE_TAR]  = extract[XARCHIVETYPE_TAR_BZ2] = extract[XARCHIVETYPE_TAR_GZ] = extract[XARCHIVETYPE_TAR_LZMA] = extract[XARCHIVETYPE_TAR_LZOP] = &xa_tar_extract;
 
337
        extract[XARCHIVETYPE_ZIP] = &xa_zip_extract;
 
338
        extract[XARCHIVETYPE_LHA] = &xa_lha_extract;
 
339
        extract[XARCHIVETYPE_LZOP] = &xa_tar_extract;
 
340
        
 
341
        test[0]  = 0;
 
342
        test[XARCHIVETYPE_7ZIP]  = &xa_7zip_test;
 
343
        test[XARCHIVETYPE_ARJ]  = &xa_arj_test;
 
344
        test[XARCHIVETYPE_DEB]  = test[XARCHIVETYPE_BZIP2] = test[XARCHIVETYPE_GZIP] = test[XARCHIVETYPE_LZMA] = 0;
 
345
        test[XARCHIVETYPE_RAR]  = &xa_rar_test;
 
346
        test[XARCHIVETYPE_RPM]  = 0;
 
347
        test[XARCHIVETYPE_TAR]  = test[XARCHIVETYPE_TAR_BZ2] = test[XARCHIVETYPE_TAR_GZ] = test[XARCHIVETYPE_TAR_LZMA] = test[XARCHIVETYPE_TAR_LZOP] = 0;
 
348
        test[XARCHIVETYPE_ZIP] = &xa_zip_test;
 
349
        test[XARCHIVETYPE_LHA] = &xa_lha_test;
 
350
        test[XARCHIVETYPE_LZOP] = 0;
 
351
 
270
352
        absolute_path = g_find_program_in_path("arj");
271
353
        if ( absolute_path )
272
354
        {
273
 
                ArchiveType = g_list_prepend ( ArchiveType, ".arj");
274
 
                ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.arj");
 
355
                ArchiveType = g_list_append(ArchiveType, "arj");
 
356
                ArchiveSuffix = g_list_append(ArchiveSuffix, "*.arj");
275
357
                g_free (absolute_path);
276
358
        }
277
359
 
278
360
        absolute_path = g_find_program_in_path("bzip2");
279
361
    if ( absolute_path )
280
362
        {
281
 
                ArchiveType = g_list_prepend ( ArchiveType, ".bz2");
282
 
                ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.bz2");
 
363
                ArchiveType = g_list_append (ArchiveType, "bz2");
 
364
                ArchiveSuffix = g_list_append (ArchiveSuffix, "*.bz2");
283
365
                g_free (absolute_path);
284
366
        }
285
367
 
286
368
        absolute_path = g_find_program_in_path("ar");
287
369
    if ( absolute_path )
288
370
        {
289
 
            ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.deb");
 
371
            ArchiveSuffix = g_list_append (ArchiveSuffix, "*.deb");
290
372
                g_free (absolute_path);
291
373
        }
292
374
 
293
375
        absolute_path = g_find_program_in_path("gzip");
294
376
        if ( absolute_path )
295
377
        {
296
 
                ArchiveType = g_list_prepend ( ArchiveType, ".gz");
297
 
                ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.gz");
298
 
                g_free (absolute_path);
299
 
        }
300
 
 
301
 
        /* In future releases of xarchiver we'll use bkisofs library to allow creation of iso images */
302
 
        ArchiveType = g_list_prepend ( ArchiveType, ".iso");
303
 
        ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.iso");
 
378
                ArchiveType = g_list_append(ArchiveType, "gz");
 
379
                ArchiveSuffix = g_list_append(ArchiveSuffix, "*.gz");
 
380
                g_free (absolute_path);
 
381
        }
 
382
 
 
383
        absolute_path = g_find_program_in_path("lzma");
 
384
        if ( absolute_path )
 
385
        {
 
386
                ArchiveType = g_list_append(ArchiveType, "lzma");
 
387
                ArchiveSuffix = g_list_append(ArchiveSuffix, "*.lzma");
 
388
                g_free (absolute_path);
 
389
        }
 
390
 
 
391
        absolute_path = g_find_program_in_path("lzop");
 
392
        if ( absolute_path )
 
393
        {
 
394
                ArchiveType = g_list_append(ArchiveType, "lzop");
 
395
                ArchiveSuffix = g_list_append(ArchiveSuffix, "*.lzop");
 
396
                g_free (absolute_path);
 
397
        }
304
398
 
305
399
        absolute_path = g_find_program_in_path("lha");
306
400
        if (absolute_path)
307
401
        {
308
 
                ArchiveType = g_list_prepend(ArchiveType, ".lzh");
309
 
                ArchiveSuffix = g_list_prepend(ArchiveSuffix, "*.lzh");
 
402
                ArchiveType = g_list_append(ArchiveType, "lzh");
 
403
                ArchiveSuffix = g_list_append(ArchiveSuffix, "*.lzh");
310
404
                g_free (absolute_path);
311
 
                //ArchiveType = g_list_prepend(ArchiveType, ".lha");
312
 
                //ArchiveSuffix = g_list_prepend(ArchiveSuffix, "");
 
405
                //ArchiveType = g_list_append(ArchiveType, ".lha");
 
406
                //ArchiveSuffix = g_list_append(ArchiveSuffix, "");
313
407
        }
314
408
 
315
409
        absolute_path = g_find_program_in_path ("rar");
316
410
    if ( absolute_path )
317
411
        {
318
 
                ArchiveType = g_list_prepend ( ArchiveType, ".rar");
319
 
                ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.rar");
 
412
                ArchiveType = g_list_append(ArchiveType, "rar");
 
413
                ArchiveSuffix = g_list_append(ArchiveSuffix, "*.rar");
320
414
                g_free (absolute_path);
321
415
        }
322
416
        else
323
417
        {
324
 
                absolute_path = g_find_program_in_path ("unrar");
 
418
                absolute_path = g_find_program_in_path("unrar");
325
419
                if ( absolute_path )
326
420
                {
327
421
                        unrar = TRUE;
328
 
                        ArchiveType = g_list_prepend ( ArchiveType, ".rar");
329
 
                        ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.rar");
 
422
                        ArchiveType = g_list_append(ArchiveType, "rar");
 
423
                        ArchiveSuffix = g_list_append(ArchiveSuffix, "*.rar");
330
424
                        g_free (absolute_path);
331
425
                }
332
426
        }
334
428
        absolute_path = g_find_program_in_path("cpio");
335
429
    if ( absolute_path )
336
430
        {
337
 
            ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.rpm");
 
431
            ArchiveSuffix = g_list_append(ArchiveSuffix, "*.rpm");
338
432
                g_free (absolute_path);
339
433
        }
340
434
 
341
 
        absolute_path = g_find_program_in_path("tar");
342
 
        if ( absolute_path )
 
435
        absolute_path = g_find_program_in_path("gtar");
 
436
        
 
437
        if (absolute_path == NULL)
343
438
        {
344
 
                ArchiveType = g_list_prepend ( ArchiveType, ".tar");
345
 
                ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.tar");
 
439
                tar = "tar";
 
440
                ArchiveType = g_list_append(ArchiveType, "tar");
 
441
                ArchiveSuffix = g_list_append(ArchiveSuffix, "*.tar");
346
442
                g_free (absolute_path);
347
 
                if ( g_list_find ( ArchiveType , ".bz2") )
 
443
                if ( g_list_find ( ArchiveType , "bz2") )
348
444
                {
349
 
                        ArchiveType = g_list_prepend ( ArchiveType, ".tar.bz2");
 
445
                        ArchiveType = g_list_append(ArchiveType, "tar.bz2");
350
446
                        /* The following to avoid double filter when opening */
351
 
                        ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "");
352
 
                }
353
 
                if ( g_list_find ( ArchiveType , ".gz") )
354
 
                {
355
 
                        ArchiveType = g_list_prepend ( ArchiveType, ".tar.gz");
356
 
                        ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.tgz");
 
447
                        ArchiveSuffix = g_list_append(ArchiveSuffix, "");
 
448
                }
 
449
                if ( g_list_find ( ArchiveType , "gz") )
 
450
                {
 
451
                        ArchiveType = g_list_append(ArchiveType, "tar.gz");
 
452
                        ArchiveSuffix = g_list_append(ArchiveSuffix, "*.tgz");
 
453
                }
 
454
                if ( g_list_find ( ArchiveType , "lzma") )
 
455
                {
 
456
                        ArchiveType = g_list_append(ArchiveType, "tar.lzma");
 
457
                        ArchiveSuffix = g_list_append(ArchiveSuffix, "*.tlz");
 
458
                }
 
459
                if ( g_list_find ( ArchiveType , "lzop") )
 
460
                {
 
461
                        ArchiveType = g_list_append(ArchiveType, "tar.lzop");
 
462
                        ArchiveSuffix = g_list_append(ArchiveSuffix, "*.tzo");
357
463
                }
358
464
        }
 
465
        else
 
466
                tar = "gtar";
359
467
 
360
468
        absolute_path = g_find_program_in_path ("zip");
361
 
    if ( absolute_path )
 
469
    if (absolute_path)
362
470
        {
363
 
                ArchiveType = g_list_prepend ( ArchiveType, ".jar");
364
 
                ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.jar");
365
471
                g_free (absolute_path);
 
472
                absolute_path = g_find_program_in_path ("zipinfo");
 
473
        if (absolute_path)
 
474
                {
 
475
                        g_free (absolute_path);
 
476
                        ArchiveType = g_list_append(ArchiveType, "jar");
 
477
                        ArchiveSuffix = g_list_append(ArchiveSuffix, "*.jar");
366
478
 
367
 
                ArchiveType = g_list_prepend ( ArchiveType, ".zip");
368
 
                ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.zip");
 
479
                        ArchiveType = g_list_append(ArchiveType, "zip");
 
480
                        ArchiveSuffix = g_list_append(ArchiveSuffix, "*.zip");
 
481
                }
369
482
        }
370
 
 
371
483
        absolute_path = g_find_program_in_path("7za");
372
 
    if ( absolute_path )
 
484
    if (absolute_path != NULL)
 
485
        sevenza = TRUE;
 
486
    else
 
487
        absolute_path = g_find_program_in_path("7zr");
 
488
    if (absolute_path != NULL)
373
489
    {
374
 
        ArchiveType = g_list_prepend ( ArchiveType, ".7z");
375
 
            ArchiveSuffix = g_list_prepend ( ArchiveSuffix, "*.7z");
 
490
        sevenzr = TRUE;
 
491
        ArchiveType = g_list_append(ArchiveType, "7z");
 
492
            ArchiveSuffix = g_list_append(ArchiveSuffix, "*.7z");
376
493
                g_free (absolute_path);
377
494
    }
378
 
}
379
 
 
380
 
void xa_set_button_state (gboolean New, gboolean Open,gboolean AddFile,gboolean Extract, gboolean exe, gboolean select)
381
 
{
382
 
        gtk_widget_set_sensitive ( New_button, New);
383
 
    gtk_widget_set_sensitive ( new1, New);
384
 
        gtk_widget_set_sensitive ( Open_button, Open);
385
 
    gtk_widget_set_sensitive ( open1, Open);
386
 
        gtk_widget_set_sensitive ( AddFile_button, AddFile);
387
 
        gtk_widget_set_sensitive ( addfile, AddFile);
388
 
        gtk_widget_set_sensitive ( Extract_button, Extract);
389
 
        gtk_widget_set_sensitive ( extract_menu, Extract);
390
 
        gtk_widget_set_sensitive ( exe_menu, exe);
391
 
        gtk_widget_set_sensitive ( Exe_button, exe);
392
 
        gtk_widget_set_sensitive ( select_all, select);
393
 
}
394
 
 
395
 
gboolean SpawnSyncCommand ( gchar *command )
396
 
{
397
 
    GError *error = NULL;
398
 
    gchar *std_out;
399
 
    gchar *std_err;
400
 
        gchar **argv;
401
 
        int argcp;
402
 
 
403
 
        g_shell_parse_argv ( command , &argcp , &argv , NULL);
404
 
        if ( ! g_spawn_sync (
405
 
                NULL,
406
 
                argv,
407
 
                NULL,
408
 
                G_SPAWN_SEARCH_PATH,
409
 
                NULL,
410
 
                NULL, //user data
411
 
                &std_out,
412
 
                &std_err,
413
 
                &exit_status,
414
 
                &error) )
415
 
        {
416
 
                response = ShowGtkMessageDialog (GTK_WINDOW (MainWindow),GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK, _("Can't spawn the command:"),error->message);
417
 
                g_error_free (error);
418
 
                g_strfreev ( argv );
419
 
        return FALSE;
420
 
        }
421
 
    if ( WIFEXITED (exit_status) )
422
 
        {
423
 
            if ( WEXITSTATUS (exit_status) )
424
 
                {
425
 
                        response = ShowGtkMessageDialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_("An error occurred!"),std_err );
426
 
                        return FALSE;
427
 
                }
428
 
        }
429
 
        g_strfreev ( argv );
430
 
    return TRUE;
 
495
    absolute_path = g_find_program_in_path("xdg-open");
 
496
    if (absolute_path != NULL)
 
497
    {
 
498
        xdg_open = TRUE;
 
499
        g_free (absolute_path);
 
500
    }
431
501
}
432
502
 
433
503
XArchive *xa_init_structure_from_cmd_line (char *filename)
434
504
{
435
 
        XArchive *archive_cmd;
436
 
        archive_cmd = xa_init_archive_structure ();
437
 
        if (archive_cmd == NULL)
 
505
        XArchive *archive;
 
506
        XArchiveType type;
 
507
 
 
508
        type = xa_detect_archive_type (filename);
 
509
        if (type == XARCHIVETYPE_UNKNOWN || type == XARCHIVETYPE_NOT_FOUND)
 
510
                return NULL;
 
511
 
 
512
        archive = xa_init_archive_structure (type);
 
513
        if (archive == NULL)
438
514
        {
439
 
                response = ShowGtkMessageDialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_("Can't allocate memory for the archive structure!"),"" );
 
515
                response = xa_show_message_dialog (NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_OK,_("Can't allocate memory for the archive structure!"),"" );
440
516
                return NULL;
441
517
        }
442
 
 
443
 
        archive_cmd->path = g_strdup (filename);
444
 
        archive_cmd->escaped_path = EscapeBadChars(filename , "$\'`\"\\!?* ()&|@#:;");
445
 
        archive_cmd->type = xa_detect_archive_type ( archive_cmd , NULL );
446
 
        if (archive_cmd->type == -2)
447
 
                return NULL;
448
 
        if ( g_str_has_suffix ( archive_cmd->escaped_path , ".tar.bz2") || g_str_has_suffix ( archive_cmd->escaped_path , ".tar.bz") || g_str_has_suffix ( archive_cmd->escaped_path , ".tbz") || g_str_has_suffix ( archive_cmd->escaped_path , ".tbz2" ) )
449
 
                archive_cmd->type = XARCHIVETYPE_TAR_BZ2;
450
 
        else if ( g_str_has_suffix ( archive_cmd->escaped_path , ".tar.gz") || g_str_has_suffix ( archive_cmd->escaped_path , ".tgz") )
451
 
                archive_cmd->type = XARCHIVETYPE_TAR_GZ;
452
 
        return (archive_cmd);
 
518
        archive->path = g_strdup (filename);
 
519
        archive->escaped_path = xa_escape_bad_chars(filename , "$\'`\"\\!?* ()&|@#:;");
 
520
        archive->type = type;
 
521
        if ( g_str_has_suffix ( archive->escaped_path , ".tar.bz2") || g_str_has_suffix ( archive->escaped_path , ".tar.bz") || g_str_has_suffix ( archive->escaped_path , ".tbz") || g_str_has_suffix ( archive->escaped_path , ".tbz2" ) )
 
522
                archive->type = XARCHIVETYPE_TAR_BZ2;
 
523
        else if ( g_str_has_suffix ( archive->escaped_path , ".tar.gz") || g_str_has_suffix ( archive->escaped_path , ".tgz") )
 
524
                archive->type = XARCHIVETYPE_TAR_GZ;
 
525
        else if ( g_str_has_suffix ( archive->escaped_path , ".tar.lzma") || g_str_has_suffix ( archive->escaped_path , ".tlz") )
 
526
                archive->type = XARCHIVETYPE_TAR_LZMA;
 
527
        else if ( g_str_has_suffix ( archive->escaped_path , ".tar.lzop") || g_str_has_suffix ( archive->escaped_path , ".tzo") )
 
528
                archive->type = XARCHIVETYPE_TAR_LZOP;
 
529
        return (archive);
453
530
}
454
531