summaryrefslogtreecommitdiffstats
path: root/ark/sevenzip.cpp
blob: 0673f6c1ddb45b749636104d89b70bbc9d84db55 (plain)
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
/*

  ark -- archiver for the KDE project

  Copyright (C)

  2004: Henrique Pinto <henrique.pinto@kdemail.net>
  2003: Helio Chissini de Castro <helio@conectiva.com>
  2000: Corel Corporation (author: Emily Ezust, emilye@corel.com)

  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 <tqdir.h>

#include <kglobal.h>
#include <klocale.h>
#include <kdebug.h>
#include <kurl.h>
#include <kmessagebox.h>
#include <kprocess.h>
#include <kstandarddirs.h>

#include "sevenzip.h"
#include "arkwidget.h"
#include "settings.h"
#include "arkutils.h"
#include "filelistview.h"

SevenZipArch::SevenZipArch( ArkWidget *gui, const TQString &filename )
  : Arch( gui, filename ), m_nameColumnPos( -1 )
{
  // Check if 7z is available
  bool have_7z = !KGlobal::dirs()->findExe( "7z" ).isNull();
  // Check if 7za is available
  bool have_7za = !KGlobal::dirs()->findExe( "7za" ).isNull();

  if ( have_7z )
    m_archiver_program = m_unarchiver_program = "7z";  // Use 7z
  else if ( have_7za )
    m_archiver_program = m_unarchiver_program = "7za"; // Try 7za
  else
    m_archiver_program = m_unarchiver_program = "7zr";

  verifyCompressUtilityIsAvailable( m_archiver_program );
  verifyUncompressUtilityIsAvailable( m_unarchiver_program );

  m_headerString = "------------------";

  m_repairYear = 5; m_fixMonth = 6; m_fixDay = 7; m_fixTime = 8;
  m_dateCol = 3;
  m_numCols = 5;

  m_archCols.append( new ArchColumns( 5, TQRegExp( "[0-2][0-9][0-9][0-9]" ), 4 ) ); // Year
  m_archCols.append( new ArchColumns( 6, TQRegExp( "[01][0-9]" ), 2 ) ); // Month
  m_archCols.append( new ArchColumns( 7, TQRegExp( "[0-3][0-9]" ), 2 ) ); // Day
  m_archCols.append( new ArchColumns( 8, TQRegExp( "[0-9:]+" ), 8 ) ); // Time
  m_archCols.append( new ArchColumns( 4, TQRegExp( "[^\\s]+" ) ) ); // Attributes
  m_archCols.append( new ArchColumns( 1, TQRegExp( "[0-9]+" ) ) ); // Size
  m_archCols.append( new ArchColumns( 2, TQRegExp( "[0-9]+" ), 64, true ) ); // Compressed Size
}

SevenZipArch::~SevenZipArch()
{
}

void SevenZipArch::setHeaders()
{
  ColumnList columns;
  columns.append( FILENAME_COLUMN );
  columns.append( SIZE_COLUMN );
  columns.append( PACKED_COLUMN );
  columns.append( TIMESTAMP_COLUMN );
  columns.append( PERMISSION_COLUMN );

  emit headers( columns );
}

void SevenZipArch::open()
{
  setHeaders();

  m_buffer = "";
  m_header_removed = false;
  m_finished = false;

  KProcess *kp = m_currentProcess = new KProcess;
  *kp << m_archiver_program << "l" << m_filename;

  connect( kp, TQT_SIGNAL( receivedStdout(KProcess*, char*, int) ),
           TQT_SLOT( slotReceivedTOC(KProcess*, char*, int) ) );
  connect( kp, TQT_SIGNAL( receivedStderr(KProcess*, char*, int) ),
           TQT_SLOT( slotReceivedOutput(KProcess*, char*, int) ) );
  connect( kp, TQT_SIGNAL( processExited(KProcess*) ),
           TQT_SLOT( slotOpenExited(KProcess*) ) );

  if ( !kp->start( KProcess::NotifyOnExit, KProcess::AllOutput ) )
  {
    KMessageBox::error( 0, i18n( "Could not start a subprocess." ) );
    emit sigOpen( this, false, TQString(), 0 );
  }
}

void SevenZipArch::create()
{
  emit sigCreate( this, true, m_filename,
                  Arch::Extract | Arch::Delete | Arch::Add | Arch::View );
}

void SevenZipArch::addFile( const TQStringList & urls )
{
  KProcess *kp = m_currentProcess = new KProcess;

  kp->clearArguments();
  *kp << m_archiver_program << "a" ;

  KURL url( urls.first() );
  TQDir::setCurrent( url.directory() );

  *kp << m_filename;

  TQStringList::ConstIterator iter;
  for ( iter = urls.begin(); iter != urls.end(); ++iter )
  {
    KURL url( *iter );
    *kp << url.fileName();
  }

  connect( kp, TQT_SIGNAL( receivedStdout(KProcess*, char*, int) ),
           TQT_SLOT( slotReceivedOutput(KProcess*, char*, int) ) );
  connect( kp, TQT_SIGNAL( receivedStderr(KProcess*, char*, int) ),
           TQT_SLOT( slotReceivedOutput(KProcess*, char*, int) ) );
  connect( kp, TQT_SIGNAL( processExited(KProcess*) ),
           TQT_SLOT( slotAddExited(KProcess*) ) );

  if ( !kp->start( KProcess::NotifyOnExit, KProcess::AllOutput ) )
  {
    KMessageBox::error( 0, i18n( "Could not start a subprocess." ) );
    emit sigAdd( false );
  }
}

void SevenZipArch::addDir( const TQString & dirName )
{
  if ( !dirName.isEmpty() )
  {
    TQStringList list;
    list.append( dirName );
    addFile( list );
  }
}

void SevenZipArch::remove( TQStringList *list )
{
  if ( !list )
    return;

  KProcess *kp = m_currentProcess = new KProcess;
  kp->clearArguments();

  *kp << m_archiver_program << "d" << m_filename;

  TQStringList::Iterator it;
  for ( it = list->begin(); it != list->end(); ++it )
  {
    *kp << *it;
  }

  connect( kp, TQT_SIGNAL( receivedStdout(KProcess*, char*, int) ),
           TQT_SLOT( slotReceivedOutput(KProcess*, char*, int) ) );
  connect( kp, TQT_SIGNAL( receivedStderr(KProcess*, char*, int) ),
           TQT_SLOT( slotReceivedOutput(KProcess*, char*, int) ) );
  connect( kp, TQT_SIGNAL( processExited(KProcess*) ),
           TQT_SLOT( slotDeleteExited(KProcess*) ) );

  if ( !kp->start( KProcess::NotifyOnExit, KProcess::AllOutput ) )
  {
    KMessageBox::error( 0, i18n( "Could not start a subprocess." ) );
    emit sigDelete( false );
  }
}

void SevenZipArch::unarchFileInternal( )
{
  if ( m_destDir.isEmpty() || m_destDir.isNull() )
  {
    kdError( 1601 ) << "There was no extract directory given." << endl;
    return;
  }

  KProcess *kp = m_currentProcess = new KProcess;
  kp->clearArguments();

  // extract (and maybe overwrite)
  *kp << m_unarchiver_program << "x";

  if ( ArkSettings::extractOverwrite() )
  {
    //*kp << "-ao";
  }

  *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 << "-o" + m_destDir ;

  connect( kp, TQT_SIGNAL( receivedStdout(KProcess*, char*, int) ),
           TQT_SLOT( slotReceivedOutput(KProcess*, char*, int) ) );
  connect( kp, TQT_SIGNAL( receivedStderr(KProcess*, char*, int) ),
           TQT_SLOT( slotReceivedOutput(KProcess*, char*, int) ) );
  connect( kp, TQT_SIGNAL( processExited(KProcess*) ),
           TQT_SLOT( slotExtractExited(KProcess*) ) );

  if ( !kp->start( KProcess::NotifyOnExit, KProcess::AllOutput ) )
  {
    KMessageBox::error( 0, i18n( "Could not start a subprocess." ) );
    emit sigExtract( false );
  }
}

bool SevenZipArch::processLine( const TQCString& _line )
{
  TQCString line( _line );
  TQString columns[ 11 ];
  unsigned int pos = 0;
  int strpos, len;

  columns[ 0 ] = line.right( line.length() - m_nameColumnPos +1);
  line.truncate( m_nameColumnPos );

  // Go through our columns, try to pick out data, return silently on failure
  for ( TQPtrListIterator <ArchColumns>col( m_archCols ); col.current(); ++col )
  {
    ArchColumns *curCol = *col;

    strpos = curCol->pattern.search( line, pos );
    len = curCol->pattern.matchedLength();

    if ( ( strpos == -1 ) || ( len > curCol->maxLength ) )
    {
      if ( curCol->optional )
        continue; // More?
      else
      {
        kdDebug(1601) << "processLine failed to match critical column" << endl;
        return false;
      }
    }

    pos = strpos + len;

    columns[ curCol->colRef ] = line.mid( strpos, len );
  }


  if ( m_dateCol >= 0 )
  {
    TQString year = ( m_repairYear >= 0 ) ?
                   ArkUtils::fixYear( columns[ m_repairYear ].ascii())
                   : columns[ m_fixYear ];
    TQString month = ( m_repairMonth >= 0 ) ?
                   TQString( "%1" )
                   .tqarg( ArkUtils::getMonth( columns[ m_repairMonth ].ascii() ) )
                   : columns[ m_fixMonth ];
    TQString timestamp = TQString::tqfromLatin1( "%1-%2-%3 %4" )
                        .tqarg( year )
                        .tqarg( month )
                        .tqarg( columns[ m_fixDay ] )
                        .tqarg( columns[ m_fixTime ] );

    columns[ m_dateCol ] = timestamp;
  }

  TQStringList list;

  for ( int i = 0; i < m_numCols; ++i )
  {
    list.append( columns[ i ] );
  }

  m_gui->fileList()->addItem( list ); // send the entry to the GUI

  return true;
}

void SevenZipArch::slotReceivedTOC( KProcess*, char* data, int length )
{
  char endchar = data[ length ];
  data[ length ] = '\0';

  appendShellOutputData( data );

  int startChar = 0;

  while ( !m_finished )
  {
    int lfChar;
    for ( lfChar = startChar; data[ lfChar ] != '\n' && lfChar < length;
          lfChar++ );

    if ( data[ lfChar ] != '\n')
      break; // We are done all the complete lines

    data[ lfChar ] = '\0';
    m_buffer.append( data + startChar );
    data[ lfChar ] = '\n';
    startChar = lfChar + 1;

    // Check if the header was found
    if ( m_buffer.find( m_headerString.data() ) != -1 )
    {
      if ( !m_header_removed )
      {
        m_nameColumnPos = m_buffer.findRev( ' ' ) + 1;
        m_header_removed = true;
      }
      else
      {
        m_finished = true;
      }
    }
    else
    {
      // If the header was not found, process the line
      if ( m_header_removed && !m_finished )
      {
        if ( !processLine( m_buffer ) )
        {
          m_header_removed = false;
          m_error = true;
        }
      }
    }

    m_buffer.resize( 0 );
  }

  if ( !m_finished )
    m_buffer.append( data + startChar);	// Append what's left of the buffer

  data[ length ] = endchar;
}

#include "sevenzip.moc"