summaryrefslogtreecommitdiffstats
path: root/ark/zoo.cpp
blob: 1ef73776b5201ea70e4a0681027c011c577a5cd7 (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
/*

 ark -- archiver for the KDE project

 Copyright (C)

 2000: Corel Corporation (author: Emily Ezust, emilye@corel.com)
 2001: Corel Corporation (author: Michael Jarrett, michaelj@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 <config.h>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/errno.h>
#include <string.h>

// QT includes
#include <tqfile.h>
#include <tqdir.h>

// KDE includes
#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kprocess.h>

// ark includes
#include "settings.h"
#include "arkwidget.h"
#include "arch.h"
#include "zoo.h"
#include "arkutils.h"
#include "filelistview.h"

static TQString fixTime( const TQString &_strTime );

ZooArch::ZooArch( ArkWidget *gui, const TQString & fileName )
  : Arch( gui, fileName )
{
  m_archiver_program = m_unarchiver_program = "zoo";
  verifyCompressUtilityIsAvailable( m_archiver_program );
  verifyUncompressUtilityIsAvailable( m_unarchiver_program );

  m_headerString = "----";
}

bool ZooArch::processLine( const TQCString &line )
{
  const char *_line = ( const char * )line;
  char columns[11][80];
  char filename[4096];

  // Note: I'm reversing the ratio and the length for better display

  sscanf( _line,
          " %79[0-9] %79[0-9%] %79[0-9] %79[0-9] %79[a-zA-Z] %79[0-9]%79[ ]%11[ 0-9:+-]%2[C ]%4095[^\n]",
          columns[1], columns[0], columns[2], columns[3], columns[7],
          columns[8], columns[9], columns[4], columns[10], filename );

  TQString year = ArkUtils::fixYear( columns[8] );

  TQString strDate;
  strDate.sprintf( "%s-%.2d-%.2d", year.utf8().data(),
                   ArkUtils::getMonth( columns[7] ), atoi( columns[3] ) );

  strlcpy( columns[3], strDate.ascii(), sizeof( columns[3]) );
  kdDebug( 1601 ) << "New timestamp is " << columns[3] << endl;

  strlcat( columns[3], " ", sizeof( columns[3] ) );
  strlcat( columns[3], fixTime( columns[4] ).ascii(), sizeof( columns[3] ) );

  TQStringList list;
  list.append( TQFile::decodeName( filename ) );

  for ( int i=0; i<4; i++ )
  {
    list.append( TQString::fromLocal8Bit( columns[i] ) );
  }

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

  return true;
}

void ZooArch::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 ZooArch::setHeaders()
{
  ColumnList list;
  list.append( FILENAME_COLUMN );
  list.append( RATIO_COLUMN );
  list.append( SIZE_COLUMN );
  list.append( PACKED_COLUMN );
  list.append( TIMESTAMP_COLUMN );

  emit headers( list );
}


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

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

void ZooArch::addFile( const TQStringList &urls )
{
  KProcess *kp = m_currentProcess = new KProcess;
  kp->clearArguments();
  *kp << m_archiver_program;

  if ( ArkSettings::replaceOnlyWithNewer() )
    *kp << "-update";
  else
    *kp << "-add";

  *kp << m_filename;

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

  TQStringList::ConstIterator iter;

  for ( iter = urls.begin(); iter != urls.end(); ++iter )
  {
    KURL fileURL( *iter );
    *kp << fileURL.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 ZooArch::unarchFileInternal()
{
  // if _fileList is empty, we extract all.
  // if _destDir is empty, abort with error.

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

  // zoo has no option to specify the destination directory
  // so we have to change to it.

  bool ret = TQDir::setCurrent( m_destDir );
  // We already checked the validity of the dir before coming here
  Q_ASSERT(ret);

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

  *kp << m_archiver_program;

  if ( ArkSettings::extractOverwrite() )
  {
    *kp << "xOOS";
  }
  else
  {
    *kp << "x";
  }

  *kp << m_filename;

  // if the 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);
    }
  }

  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 );
  }
}

void ZooArch::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 )
  {
    TQString str = *it;
    *kp << str;
  }

  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 );
  }
}

TQString fixTime( const TQString &_strTime )
{
  // it may have come from a different time zone... get rid of trailing
  // +3 or -3 etc.
  TQString strTime = _strTime;

  if ( strTime.contains("+") || strTime.contains("-") )
  {
    TQCharRef c = strTime.at( 8 );
    int offset = strTime.right( strTime.length() - 9 ).toInt();
    TQString strHour = strTime.left( 2 );
    int nHour = strHour.toInt();
    if ( c == '+' || c == '-' )
    {
      if ( c == '+' )
        nHour = ( nHour + offset ) % 24;
      else if ( c == '-' )
      {
        nHour -= offset;
        if ( nHour < 0 )
          nHour += 24;
      }
      strTime = strTime.left( 8 );
      strTime.sprintf( "%2.2d%s", nHour, strTime.right( 6 ).utf8().data() );
    }
  }
  else
  {
    strTime = strTime.left( 8 );
  }
  return strTime;
}

#include "zoo.moc"