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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/*
ark -- archiver for the KDE project
Copyright (C)
2003: Helio Chissini de Castro <[email protected]>
2000: Corel Corporation (author: Emily Ezust, [email protected])
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.
*/
// Std includes
#include <sys/errno.h>
#include <unistd.h>
#include <iostream>
#include <string>
// QT includes
#include <tqstring.h>
#include <tqregexp.h>
#include <tqfile.h>
#include <tqdir.h>
#include <tqtextcodec.h>
// KDE includes
#include <kdebug.h>
#include <tdeglobal.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kmimetype.h>
#include <kpassdlg.h>
#include <kprocess.h>
#include <kstandarddirs.h>
// ark includes
#include <config.h>
#include "arkwidget.h"
#include "arch.h"
#include "settings.h"
#include "rar.h"
#include "arkutils.h"
#include "filelistview.h"
// MMmmppbb ; M-major, m-minor, p-patch b-(100-beta)
#define VERSION_MAJOR 1000000
#define VERSION_MINOR 10000
#define VERSION_PATCH 100
#define VERSION_5 (5*VERSION_MAJOR - VERSION_PATCH + 1 ) // consider betas
RarArch::RarArch( ArkWidget *_gui, const TQString & _fileName )
: Arch( _gui, _fileName ), m_isFirstLine(false), m_version(0)
{
// Check if rar is available
bool have_rar = !TDEGlobal::dirs()->findExe( "rar" ).isNull();
bool have_unrar = !TDEGlobal::dirs()->findExe( "unrar" ).isNull();
bool have_unrar_free = !TDEGlobal::dirs()->findExe( "unrar-free" ).isNull();
if ( have_rar )
{
// If it is, then use it as archiver and unarchiver
m_archiver_program = m_unarchiver_program = "rar";
verifyCompressUtilityIsAvailable( m_archiver_program );
verifyUncompressUtilityIsAvailable( m_unarchiver_program );
}
else if (have_unrar)
{
// If rar is not available, try to use unrar to open the archive read-only
m_unarchiver_program = "unrar";
verifyUncompressUtilityIsAvailable( m_unarchiver_program );
setReadOnly( true );
}
else
{
// If rar is not available, try to use unrar to open the archive read-only
m_unarchiver_program = "unrar-free";
verifyUncompressUtilityIsAvailable( m_unarchiver_program );
setReadOnly( true );
}
}
bool RarArch::processLine( const TQCString &line )
{
TQString uline = TQTextCodec::codecForLocale()->toUnicode(line);
// Look for rar/unrar version first
if (!m_version)
{
TQRegExp versionRegExp (TQString::fromLatin1 ("(?:UN)?RAR\\s+(\\d+)\\.(\\d+)(\\s+beta\\s+(\\d+))?\\s.*Copyright.*"));
if (versionRegExp.exactMatch (uline))
{
// Rar displays verion in form of "M.mp (beta b)?"
m_version = versionRegExp.cap(1).toShort() * VERSION_MAJOR;
m_version += versionRegExp.cap(2).toShort()/10 * VERSION_MINOR;
m_version += versionRegExp.cap(2).toShort()%10 * VERSION_PATCH;
if (!versionRegExp.cap(4).isEmpty()) { // beta versions should go befor release ones
m_version -= VERSION_PATCH;
m_version += versionRegExp.cap(4).toShort();
}
if (m_version < VERSION_5) {
m_headerString = "-------------------------------------------------------------------------------";
m_isFirstLine = true;
} else {
m_headerString = "----------- --------- -------- ----- ---------- ----- -------- ----";
}
setHeaders(); //< Note: header order for version 5 is different, but keep the old one for consistency
return true;
}
return false;
}
TQStringList entry;
TQStringList parsedData = TQStringList::split(QChar(' '), uline);
if (m_version < VERSION_5) {
if (m_isFirstLine)
{
m_entryFilename = uline.remove( 0, 1 );
m_isFirstLine = false;
return true;
}
if (parsedData.size() < 9) {
kdError ( 1601 ) << "Failed to parse rar<5 output string: \"" << uline << "\"" << endl;
}
entry << m_entryFilename; // filename
entry << parsedData[ 0 ]; // size
entry << parsedData[ 1 ]; // packed
entry << parsedData[ 2 ]; // ratio
TQStringList date = TQStringList::split( '-', parsedData[ 3 ] );
entry << ArkUtils::fixYear( date[ 2 ].latin1() ) + '-' + date[ 1 ] + '-' + date [ 0 ] + ' ' + parsedData[4]; // date
entry << parsedData[ 5 ]; // attributes
entry << parsedData[ 6 ]; // crc
entry << parsedData[ 7 ]; // method
entry << parsedData[ 8 ]; // Version
m_isFirstLine = true;
}
else
{
// Note: don't use parsedData for names due to they may contain trailing spaces
TQRegExp nameRegExp (TQString::fromLatin1 ("\\s*(\\S+\\s+){6}\\S+ (.*)"));
if (parsedData.size() >= 8 && nameRegExp.exactMatch (uline)) {
m_entryFilename = nameRegExp.capturedTexts()[2];
if(m_version < 5*VERSION_MAJOR+3*VERSION_MINOR) { // workaround bug with extra spaces in rar<5.3.0
m_entryFilename = m_entryFilename.stripWhiteSpace();
}
entry << m_entryFilename; // filename
entry << parsedData[ 1 ]; // size
entry << parsedData[ 2 ]; // packed
entry << parsedData[ 3 ]; // ratio
entry << parsedData[ 4 ] + " " + parsedData[ 5 ]; // date and time
entry << parsedData[ 0 ]; // attributes
entry << parsedData[ 6 ]; // crc
} else {
kdError ( 1601 ) << "Failed to parse rar-5+ output string: \"" << uline << "\"" << endl;
return false;
}
}
// send to GUI
// Use addOrUpdateItem() rather than addItem() due to recent RAR version
// place directories in archive after their content.
FileLVI *item = m_gui->fileList()->addOrUpdateItem( entry );
// But archives packaged with older versions of rar may have directories
// entries first, so make sure they will get an appropriate icon
if (item && entry[5].find('d', 0, false) != -1) {
// check attr's for d (case insensitive to handle windows archives)
item->setPixmap( 0, KMimeType::mimeType( "inode/directory" )->pixmap( TDEIcon::Small ) );
}
return true;
}
void RarArch::open()
{
m_buffer = "";
m_header_removed = false;
m_finished = false;
TDEProcess *kp = m_currentProcess = new TDEProcess;
*kp << m_unarchiver_program << "v" << "-c-";
if ( !m_password.isEmpty() )
*kp << "-p" + m_password.local8Bit();
else
*kp << "-p-";
*kp << m_filename;
connect( kp, TQT_SIGNAL( receivedStdout(TDEProcess*, char*, int) ),
TQT_SLOT( slotReceivedTOC(TDEProcess*, char*, int) ) );
connect( kp, TQT_SIGNAL( receivedStderr(TDEProcess*, char*, int) ),
TQT_SLOT( slotReceivedOutput(TDEProcess*, char*, int) ) );
connect( kp, TQT_SIGNAL( processExited(TDEProcess*) ),
TQT_SLOT( slotOpenExited(TDEProcess*) ) );
if ( !kp->start( TDEProcess::NotifyOnExit, TDEProcess::AllOutput ) )
{
KMessageBox::error( 0, i18n( "Could not start a subprocess." ) );
emit sigOpen( this, false, TQString(), 0 );
}
}
void RarArch::setHeaders()
{
ColumnList list;
list.append( FILENAME_COLUMN );
list.append( SIZE_COLUMN );
list.append( PACKED_COLUMN );
list.append( RATIO_COLUMN );
list.append( TIMESTAMP_COLUMN );
list.append( PERMISSION_COLUMN );
list.append( CRC_COLUMN );
if (m_version < VERSION_5)
{
list.append( METHOD_COLUMN );
list.append( VERSION_COLUMN );
}
emit headers( list );
}
void RarArch::create()
{
emit sigCreate( this, true, m_filename,
Arch::Extract | Arch::Delete | Arch::Add | Arch::View );
}
void RarArch::createPassword()
{
if( m_password.isEmpty() && ArkSettings::askCreatePassword() )
KPasswordDialog::getNewPassword( m_password, i18n("Warning!\nUsing KGpg for encryption is more secure.\nCancel this dialog or enter password for %1 archiver:").arg(m_archiver_program) );
}
void RarArch::addDir( const TQString & _dirName )
{
if ( !_dirName.isEmpty() )
{
TQStringList list;
list.append( _dirName );
addFile( list );
}
}
void RarArch::addFile( const TQStringList & urls )
{
TDEProcess *kp = m_currentProcess = new TDEProcess;
kp->clearArguments();
*kp << m_archiver_program;
if ( ArkSettings::replaceOnlyWithNewer() )
*kp << "u";
else
*kp << "a";
if ( ArkSettings::rarStoreSymlinks() )
*kp << "-ol";
if ( ArkSettings::rarRecurseSubdirs() )
*kp << "-r";
if ( !m_password.isEmpty() )
*kp << "-p"+m_password.local8Bit();
*kp << m_filename;
KURL dir( urls.first() );
TQDir::setCurrent( dir.directory() );
TQStringList::ConstIterator iter;
for ( iter = urls.begin(); iter != urls.end(); ++iter )
{
KURL url( *iter );
*kp << url.fileName();
}
connect( kp, TQT_SIGNAL( receivedStdout(TDEProcess*, char*, int) ),
TQT_SLOT( slotReceivedOutput(TDEProcess*, char*, int) ) );
connect( kp, TQT_SIGNAL( receivedStderr(TDEProcess*, char*, int) ),
TQT_SLOT( slotReceivedOutput(TDEProcess*, char*, int) ) );
connect( kp, TQT_SIGNAL( processExited(TDEProcess*) ),
TQT_SLOT( slotAddExited(TDEProcess*) ) );
if ( !kp->start( TDEProcess::NotifyOnExit, TDEProcess::AllOutput ) )
{
KMessageBox::error( 0, i18n( "Could not start a subprocess." ) );
emit sigAdd( false );
}
}
void RarArch::unarchFileInternal()
{
if ( m_destDir.isEmpty() || m_destDir.isNull() )
{
kdError( 1601 ) << "There was no extract directory given." << endl;
return;
}
TDEProcess *kp = m_currentProcess = new TDEProcess;
kp->clearArguments();
// extract (and maybe overwrite)
*kp << m_unarchiver_program << "x";
if ( !m_password.isEmpty() )
*kp << "-p" + m_password.local8Bit();
else
*kp << "-p-";
if ( !ArkSettings::extractOverwrite() )
{
*kp << "-o+";
}
else
{
*kp << "-o-";
}
*kp << m_filename;
// if the file list is empty, no filenames go on the command line,
// and we then extract everything in the archive.
if ( m_fileList )
{
TQStringList::Iterator it;
for ( it = m_fileList->begin(); it != m_fileList->end(); ++it )
{
*kp << (*it);
}
}
*kp << m_destDir ;
connect( kp, TQT_SIGNAL( receivedStdout(TDEProcess*, char*, int) ),
TQT_SLOT( slotReceivedOutput(TDEProcess*, char*, int) ) );
connect( kp, TQT_SIGNAL( receivedStderr(TDEProcess*, char*, int) ),
TQT_SLOT( slotReceivedOutput(TDEProcess*, char*, int) ) );
connect( kp, TQT_SIGNAL( processExited(TDEProcess*) ),
TQT_SLOT( slotExtractExited(TDEProcess*) ) );
if ( !kp->start( TDEProcess::NotifyOnExit, TDEProcess::AllOutput ) )
{
KMessageBox::error( 0, i18n( "Could not start a subprocess." ) );
emit sigExtract( false );
}
}
bool RarArch::passwordRequired()
{
return m_lastShellOutput.find("Enter password") >= 0 || m_lastShellOutput.find("encrypted") >= 0;
}
void RarArch::remove( TQStringList *list )
{
if ( !list )
return;
TDEProcess *kp = m_currentProcess = new TDEProcess;
kp->clearArguments();
*kp << m_archiver_program << "d" << m_filename;
TQStringList::Iterator it;
for ( it = list->begin(); it != list->end(); ++it )
{
TQString str = *it;
*kp << str;
}
connect( kp, TQT_SIGNAL( receivedStdout(TDEProcess*, char*, int) ),
TQT_SLOT( slotReceivedOutput(TDEProcess*, char*, int) ) );
connect( kp, TQT_SIGNAL( receivedStderr(TDEProcess*, char*, int) ),
TQT_SLOT( slotReceivedOutput(TDEProcess*, char*, int) ) );
connect( kp, TQT_SIGNAL( processExited(TDEProcess*) ),
TQT_SLOT( slotDeleteExited(TDEProcess*) ) );
if ( !kp->start( TDEProcess::NotifyOnExit, TDEProcess::AllOutput ) )
{
KMessageBox::error( 0, i18n( "Could not start a subprocess." ) );
emit sigDelete( false );
}
}
void RarArch::test()
{
clearShellOutput();
TDEProcess *kp = m_currentProcess = new TDEProcess;
kp->clearArguments();
*kp << m_unarchiver_program << "t";
if ( !m_password.isEmpty() )
*kp << "-p" + m_password.local8Bit();
*kp << m_filename;
connect( kp, SIGNAL( receivedStdout(TDEProcess*, char*, int) ),
SLOT( slotReceivedOutput(TDEProcess*, char*, int) ) );
connect( kp, SIGNAL( receivedStderr(TDEProcess*, char*, int) ),
SLOT( slotReceivedOutput(TDEProcess*, char*, int) ) );
connect( kp, SIGNAL( processExited(TDEProcess*) ),
SLOT( slotTestExited(TDEProcess*) ) );
if ( !kp->start( TDEProcess::NotifyOnExit, TDEProcess::AllOutput ) )
{
KMessageBox::error( 0, i18n( "Could not start a subprocess." ) );
emit sigTest( false );
}
}
#include "rar.moc"
|