summaryrefslogtreecommitdiffstats
path: root/ark/compressedfile.cpp
blob: f935eac64c7d0defdfced0a1ac6c3a78bacbed1b (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
    ark: A program for modifying archives via a GUI.

    Copyright (C)

    2000: Corel Corporation (author: Emily Ezust, emilye@corel.com)
    2001: Corel Corporation (author: Michael Jarrett, michaelj@corel.com)
    2003: Georg Robbers <Georg.Robbers@urz.uni-hd.de>

    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.

*/

// C includes
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

// TQt includes
#include <tqdir.h>

// KDE includes
#include <kdebug.h>
#include <klargefile.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <ktempdir.h>
#include <kprocess.h>
#include <kmimetype.h>
#include <kio/netaccess.h>
#include <kio/global.h>
#include <kfileitem.h>
#include <kapplication.h>
#include <kglobal.h>

// ark includes
#include "arkwidget.h"
#include "filelistview.h"
#include "compressedfile.h"

// encapsulates the idea of a compressed file

CompressedFile::CompressedFile( ArkWidget *_gui, const TQString & _fileName, const TQString & _openAsMimeType )
  : Arch( _gui, _fileName )
{
  m_tempDirectory = NULL;
  m_openAsMimeType = _openAsMimeType;
  kdDebug(1601) << "CompressedFile constructor" << endl;
  m_tempDirectory = new KTempDir( _gui->tmpDir()
                          + TQString::tqfromLatin1( "compressed_file_temp" ) );
  m_tempDirectory->setAutoDelete( true );
  m_tmpdir = m_tempDirectory->name();
  initData();
  verifyCompressUtilityIsAvailable( m_archiver_program );
  verifyUncompressUtilityIsAvailable( m_unarchiver_program );

  if (!TQFile::exists(_fileName))
  {
    KMessageBox::information(0,
              i18n("You are creating a simple compressed archive which contains only one input file.\n"
                  "When uncompressed, the file name will be based on the name of the archive file.\n"
                  "If you add more files you will be prompted to convert it to a real archive."),
              i18n("Simple Compressed Archive"), "CreatingCompressedArchive");
  }
}

CompressedFile::~CompressedFile()
{
    if ( m_tempDirectory )
        delete m_tempDirectory;
}

void CompressedFile::setHeaders()
{
  ColumnList list;
  list.append(FILENAME_COLUMN);
  list.append(PERMISSION_COLUMN);
  list.append(OWNER_COLUMN);
  list.append(GROUP_COLUMN);
  list.append(SIZE_COLUMN);

  emit headers(list);
}

void CompressedFile::initData()
{
    m_unarchiver_program = TQString();
    m_archiver_program = TQString();

    TQString mimeType;
    if ( !m_openAsMimeType.isNull() )
        mimeType = m_openAsMimeType;
    else
        mimeType = KMimeType::findByPath( m_filename )->name();

    if ( mimeType == "application/x-gzip" )
    {
        m_unarchiver_program = "gunzip";
        m_archiver_program = "gzip";
        m_defaultExtensions << ".gz" << "-gz" << ".z" << "-z" << "_z" << ".Z";
    }
    if ( mimeType == "application/x-bzip" )
    {
        m_unarchiver_program = "bunzip";
        m_archiver_program = "bzip";
        m_defaultExtensions << ".bz";
    }
    if ( mimeType == "application/x-bzip2" )
    {
        m_unarchiver_program = "bunzip2";
        m_archiver_program = "bzip2";
        m_defaultExtensions << ".bz2" << ".bz";
    }
    if ( mimeType == "application/x-lzma" )
    {
        m_unarchiver_program = "unlzma";
        m_archiver_program = "lzma";
        m_defaultExtensions << ".lzma";
    }
    if ( mimeType == "application/x-xz" )
    {
        m_unarchiver_program = "unxz";
        m_archiver_program = "xz";
        m_defaultExtensions << ".xz";
    }
    if ( mimeType == "application/x-lzop" )
    { m_unarchiver_program = "lzop";
        m_archiver_program = "lzop";
        m_defaultExtensions << ".lzo";
    }
    if ( mimeType == "application/x-compress" )
    {
        m_unarchiver_program = KGlobal::dirs()->findExe( "uncompress" ).isNull()? "gunzip" : "uncompress";
        m_archiver_program   = "compress";
        m_defaultExtensions  = ".Z";
    }

}

TQString CompressedFile::extension()
{
  TQStringList::Iterator it = m_defaultExtensions.begin();
  for( ; it != m_defaultExtensions.end(); ++it )
    if( m_filename.endsWith( *it ) )
        return TQString();
  return m_defaultExtensions.first();
}

void CompressedFile::open()
{
  kdDebug(1601) << "+CompressedFile::open" << endl;
  setHeaders();

  // We copy the file into the temporary directory, uncompress it,
  // and when the uncompression is done, list it
  // (that code is in the slot slotOpenDone)

  m_tmpfile = m_gui->realURL().fileName();
  if ( m_tmpfile.isEmpty() )
    m_tmpfile = m_filename;
  m_tmpfile += extension();
  m_tmpfile = m_tmpdir + m_tmpfile;

  KURL src, target;
  src.setPath( m_filename );
  target.setPath( m_tmpfile );

  KIO::NetAccess::copy( src, target, m_gui );
  kdDebug(1601) << "Temp file name is " << target << endl;

  if ( !KIO::NetAccess::exists( target, true, NULL ) )
    return;

  KProcess *kp = m_currentProcess = new KProcess;
  kp->clearArguments();
  *kp << m_unarchiver_program << "-f" ;
  if ( m_unarchiver_program == "lzop")
  {
    *kp << "-d";
    // lzop hack, see comment in tar.cpp createTmp()
    kp->setUsePty( KProcess::Stdin, false );
  }
  // gunzip 1.3 seems not to like original names with directories in them
  // testcase: https://listman.redhat.com/pipermail/valhalla-list/2006-October.txt.gz
  /*if ( m_unarchiver_program == "gunzip" )
    *kp << "-N";
  */
  *kp << m_tmpfile;

  kdDebug(1601) << "Command is " << m_unarchiver_program << " " << m_tmpfile<< endl;

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

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

  kdDebug(1601) << "-CompressedFile::open" << endl;
}

void CompressedFile::slotUncompressDone(KProcess *_kp)
{
  bool bSuccess = false;
  kdDebug(1601) << "normalExit = " << _kp->normalExit() << endl;
  if( _kp->normalExit() )
    kdDebug(1601) << "exitStatus = " << _kp->exitStatus() << endl;

  if( _kp->normalExit() && (_kp->exitStatus()==0) )
  {
    bSuccess = true;
  }

  delete _kp;
  _kp = m_currentProcess = 0;

  if ( !bSuccess )
  {
      emit sigOpen( this, false, TQString(), 0 );
      return;
  }

  TQDir dir( m_tmpdir );
  TQStringList lst( dir.entryList() );
  lst.remove( ".." );
  lst.remove( "." );
  KURL url;
  url.setPath( m_tmpdir + lst.first() );
  m_tmpfile = url.path();
  KIO::UDSEntry udsInfo;
  KIO::NetAccess::stat( url, udsInfo, m_gui );
  KFileItem fileItem( udsInfo, url );
  TQStringList list;
  list << fileItem.name();
  list << fileItem.permissionsString();
  list << fileItem.user();
  list << fileItem.group();
  list << KIO::number( fileItem.size() );
  m_gui->fileList()->addItem(list); // send to GUI

  emit sigOpen( this, bSuccess, m_filename,
                Arch::Extract | Arch::Delete | Arch::Add | Arch::View );
}

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

void CompressedFile::addFile( const TQStringList &urls )
{
  // only used for adding ONE file to an EMPTY gzip file, i.e., one that
  // has just been created

  kdDebug(1601) << "+CompressedFile::addFile" << endl;

  Q_ASSERT(m_gui->getNumFilesInArchive() == 0);
  Q_ASSERT(urls.count() == 1);

  KURL url = KURL::fromPathOrURL(urls.first());
  Q_ASSERT(url.isLocalFile());

  TQString file;
  file = url.path();

  KProcess proc;
  proc << "cp" << file << m_tmpdir;
  proc.start(KProcess::Block);

  m_tmpfile = file.right(file.length()
			 - file.findRev("/")-1);
  m_tmpfile = m_tmpdir + '/' + m_tmpfile;

  kdDebug(1601) << "Temp file name is " << m_tmpfile << endl;

  kdDebug(1601) << "File is " << file << endl;

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

  // lzop hack, see comment in tar.cpp createTmp()
  if ( m_archiver_program == "lzop")
    kp->setUsePty( KProcess::Stdin, false );

  TQString compressor = m_archiver_program;

  *kp << compressor << "-c" << file;

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

  int f_desc = KDE_open(TQFile::encodeName(m_filename), O_CREAT | O_TRUNC | O_WRONLY, 0666);
  if (f_desc != -1)
      fd = fdopen( f_desc, "w" );
  else
      fd = NULL;

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

  kdDebug(1601) << "-CompressedFile::addFile" << endl;
}

void CompressedFile::slotAddInProgress(KProcess*, char* _buffer, int _bufflen)
{
  // we're trying to capture the output of a command like this
  //    gzip -c myfile
  // and feed the output to the compressed file
  int size;
  size = fwrite(_buffer, 1, _bufflen, fd);
  if (size != _bufflen)
    {
      KMessageBox::error(0, i18n("Trouble writing to the archive..."));
      exit(99);
    }
}

void CompressedFile::slotAddDone(KProcess *_kp)
{
  fclose(fd);
  slotAddExited(_kp);
}

void CompressedFile::unarchFileInternal()
{
  if (m_destDir != m_tmpdir)
    {
      TQString dest;
      if (m_destDir.isEmpty() || m_destDir.isNull())
      {
          kdError(1601) << "There was no extract directory given." << endl;
          return;
      }
      else
          dest=m_destDir;

      KProcess proc;
      proc << "cp" << m_tmpfile << dest;
      proc.start(KProcess::Block);
    }
  emit sigExtract(true);
}

void CompressedFile::remove(TQStringList *)
{
  kdDebug(1601) << "+CompressedFile::remove" << endl;
  TQFile::remove(m_tmpfile);

  // do not delete but truncate the compressed file in case someone
  // does a reload and finds it no longer exists!
  truncate(TQFile::encodeName(m_filename), 0);

  m_tmpfile = "";
  emit sigDelete(true);
  kdDebug(1601) << "-CompressedFile::remove" << endl;
}



#include "compressedfile.moc"