File: arkwidgetpart.cpp

package info (click to toggle)
kdeutils 4%3A2.2.2-9.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,892 kB
  • ctags: 10,879
  • sloc: cpp: 82,942; sh: 11,754; ansic: 4,638; perl: 1,852; makefile: 706; python: 258
file content (714 lines) | stat: -rw-r--r-- 19,132 bytes parent folder | download
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
  Copyright (C)

  2001: Macadamian Technologies Inc (author: Jian Huang, jian@macadamian.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., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

// Qt includes
#include <qregexp.h> 
#include <qheader.h>
#include <qfile.h>
#include <qstrlist.h>
#include <qfiledialog.h>
#include <qwidget.h>
#include <qdragobject.h>

// KDE includes
#include <kdebug.h>
#include <kfiledialog.h>
#include <kkeydialog.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <krun.h>
#include <ktempfile.h>
#include <kmimemagic.h>
#include <kstddirs.h>
#include <kaboutdata.h>

// c includes
#include <errno.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <config.h>
#include <stdlib.h>
#include <unistd.h>

// for statfs:
#ifdef BSD4_4
#include <sys/mount.h>
#elif defined(__linux__)
#include <sys/vfs.h>
#elif defined(__sun) || defined(__sgi)
#include <sys/statvfs.h>
#define STATFS statvfs
#elif defined(_AIX)
#include <sys/statfs.h>
#elif defined(hpux)
#include <sys/vfs.h>
#endif


#ifndef STATFS
#define STATFS statfs
#endif

// ark includes
#include "arkapp.h"
#include "dirDlg.h"
#include "selectDlg.h"
#include "extractdlg.h"
#include "adddlg.h"
#include "arch.h"
#include "arkwidgetpart.h"
#include "arkwidgetbase.h"
#include "arksettings.h"
#include "filelistview.h"

extern int errno;

/*******************************************************************
*                       diskHasSpacePart
*
*
*  check if disk has enough space to accomodate (a) new file(s) of
*  the given size in the partition containing the given directory
*/
bool Utilities::diskHasSpacePart(const QString &dir, long size)
{
  fprintf(stderr, "Size: %ld\n", size);
  struct STATFS buf;
  if (STATFS(QFile::encodeName(dir), &buf) == 0)
    {
      double nAvailable = (double)buf.f_bavail * buf.f_bsize;
      if (nAvailable < (double)size)
	{
	  KMessageBox::error(0, i18n("Sorry, you've run out of disk space."));
	  return false;
	}
    }
  else
    {
    // something bad happened
      ASSERT(0);
    }
  return true;
}

ArkWidgetPart::ArkWidgetPart( QWidget *parent, const char *name ) :
	QWidget(parent, name), ArkWidgetBase(this),
	m_bViewInProgress(false), mpTempFile(0)
{
    setIconText("Ark Kparts");
    createFileListView();
}

ArkWidgetPart::~ArkWidgetPart()
{
  QString tmpdir = m_settings->getTmpDir();
  QString ex( "rm -rf "+tmpdir );
  system( QFile::encodeName(ex) );
}

/*******************************************************************
*                        updateStatusTotals
*
*       update file information when open or close an archive
*/
void ArkWidgetPart::updateStatusTotals()
{
  m_nNumFiles = 0;
  m_nSizeOfFiles = 0;
  if (archiveContent)
    {
      FileLVI *pItem = (FileLVI *)archiveContent->firstChild();
      while (pItem)
	{
	  ++m_nNumFiles;

	  if (m_currentSizeColumn != -1)
	    m_nSizeOfFiles += pItem->text(m_currentSizeColumn).toInt();
	  pItem = (FileLVI *)pItem->nextSibling();
	}
    }
}

/*********************************************************************
*                          file_open
*
*               display the archive file contents
*/
void ArkWidgetPart::file_open(const QString & strFile, const KURL &fileURL)
{
  struct stat statbuffer;

  if (stat(strFile.local8Bit(), &statbuffer) == -1)
    {
      if (errno == ENOENT || errno == ENOTDIR || errno ==  EFAULT)
	{
	  KMessageBox::error(this, i18n("The archive %1 does not exist.").arg(strFile.local8Bit()));
	}
      else if (errno == EACCES)
	{
	  KMessageBox::error(this, i18n("Can't access the archive %1").arg(strFile.local8Bit()));	  
	}
      else
	KMessageBox::error(this, i18n("Unknown error."));      
      return;
    }
  else
    {
      // this will be the appropriate flag depending on whose file it is
      unsigned int nFlag = 0;
      if (geteuid() == statbuffer.st_uid)
	{
	  nFlag = S_IRUSR; // it's mine
	}
      else if (getegid() == statbuffer.st_gid)
	{
	  nFlag = S_IRGRP; // it's my group's
	}
      else
	{
	  nFlag = S_IROTH;  // it's someone else's
	}

      if (! ((statbuffer.st_mode & nFlag) == nFlag))
	{
	  KMessageBox::error(this, i18n("You don't have permission to access that archive") );
	  return;
	}
    }

  // see if the user is just opening the same file that's already
  // open (erm...)
	// TODO: Is this relevant with a part that'll likely use temp files?

  if (strFile == m_strArchName && m_bIsArchiveOpen)
    {
      return;
    }

  if (isArchiveOpen())
    file_close();  // close old zip

    // Set the current archive filename to the filename
  m_strArchName = strFile;
	m_url = fileURL;

  // display the archive contents
  showZip(strFile);
}

void ArkWidgetPart::edit_view_last_shell_output()
{
	viewShellOutput();
}


/*********************************************************************
*                            showZip
*
*     open a archive file and show its contents in a list view
*/
void ArkWidgetPart::showZip( QString _filename )
{
  createFileListView();
  openArchive( _filename );
}


/*********************************************************************
*                           slotOpen
*
*         This slot is invoked when an archive is opened
*/
void ArkWidgetPart::slotOpen(Arch *_newarch, bool _success,
			 const QString & _filename, int )
{
  archiveContent->setUpdatesEnabled(true);
  archiveContent->triggerUpdate();

  if ( _success )
    {
	QFileInfo fi( _filename );
	QString path = fi.dirPath( true );
	m_settings->setLastOpenDir( path );

	if (_filename.left(9) == QString("/tmp/ark.") ||
	    !fi.isWritable())
	  {
	    _newarch->setReadOnly(true);
            QApplication::restoreOverrideCursor(); // no wait cursor during a msg box
	    KMessageBox::information(this, i18n("This archive is read-only. If you want to save it under\na new name, go to the File menu and select Save As."));
            QApplication::setOverrideCursor( waitCursor );
	  }
	setCaption( _filename );
	arch = _newarch;
	updateStatusTotals();
	m_bIsArchiveOpen = true;
	QString extension;
	m_bIsSimpleCompressedFile = (COMPRESSED_FORMAT == m_archType);
    }
  QApplication::restoreOverrideCursor();
}


/*********************************************************************
*                       slotExtractDone
*
*         This slot is invoked when an archive is extracted
*/
void ArkWidgetPart::slotExtractDone()
{
  QApplication::restoreOverrideCursor();  

  if (m_bViewInProgress)
    { 
      m_bViewInProgress = false;
      m_pKRunPtr = new KRun (m_strFileToView);
    }
  else if (m_bDragInProgress)
    {
      m_bDragInProgress = false;
      QStrList list;
      for (QStringList::Iterator it = mDragFiles.begin();
	   it != mDragFiles.end(); ++it)
	{
	  QString URL;
	  URL = m_settings->getTmpDir();
	  URL += *it;
	  list.append( QUriDrag::localFileToUri(URL) );
	}
      QUriDrag *d = new QUriDrag(list, archiveContent->viewport());
      //      d->setPixmap(QPixmap(QString("document.xpm")),
      //		   QPoint(0,0));
      m_bDropSourceIsSelf = true;
      d->dragCopy();
      m_bDropSourceIsSelf = false;
    }

  if(m_extractList != 0) delete m_extractList;
  m_extractList = NULL;
  archiveContent->setUpdatesEnabled(true);
}


/*********************************************************************
*                          file_close
*
*                     close an archive file
*/
void ArkWidgetPart::file_close()
{
  closeArch();
  if (isArchiveOpen())
    {
      closeArch();
      setCaption(QString::null);
      if (mpTempFile)
	{
	  mpTempFile->unlink();
	  delete mpTempFile;
	  mpTempFile = NULL;
	}
      updateStatusTotals();
      updateStatusSelection();
    }
  else closeArch();
}


/*********************************************************************
*                    reportExtractFailures
*
*    reports extract failures when Overwrite = False and the file
*    exists already in the destination directory.
*    If list is null, it means we are extracting all files.
*    Otherwise the list contains the files we are to extract.
*/
bool ArkWidgetPart::reportExtractFailures(const QString & _dest,
				      QStringList *_list)
{
  QString strFilename, tmp;
  struct stat statbuffer;
  bool bRedoExtract = false;

  QApplication::restoreOverrideCursor();

  ASSERT(_list != NULL);
  QString strDestDir = _dest;

  // make sure the destination directory has a / at the end.
  if (strDestDir.at(0) != '/')
    strDestDir += '/';

  if (_list->isEmpty())
  {
    // make the list
    FileListView *flw = fileList();
    FileLVI *flvi = (FileLVI*)flw->firstChild();
    while (flvi)
      {
	tmp = flvi->getFileName().local8Bit();
	_list->append(tmp);
	flvi = (FileLVI*)flvi->itemBelow();
      }
  }
  QStringList existingFiles;
  // now the list contains all the names we must verify.
  for (QStringList::Iterator it = _list->begin(); it != _list->end(); ++it)
    {
      strFilename = *it;
      QString strFullName = strDestDir + strFilename;
      if (stat(QFile::encodeName(strFullName), &statbuffer) != -1)
	existingFiles.append(strFilename);
    }

  int numFilesToReport = existingFiles.count();

  // now report on the contents
  if (numFilesToReport == 1)
    { 
      strFilename = *(existingFiles.at(0));
      QString message = i18n("%1 will not be extracted because it will overwrite an existing file.\nGo back to Extract Dialog?").arg(strFilename);
      bRedoExtract =
	KMessageBox::questionYesNo(this, message) == KMessageBox::Yes;
    }
  else if (numFilesToReport != 0)
    {
      ExtractFailureDlg *fDlg = new ExtractFailureDlg(&existingFiles,
						      this);
      bRedoExtract = !fDlg->exec();
    }
  return bRedoExtract;
}

/*********************************************************************
*                       action_extract
*
*          extract file(s) from an opened archive file
*/
void ArkWidgetPart::action_extract()
{
  ExtractDlg *dlg = new ExtractDlg(m_settings);

  // if they choose pattern, we have to tell arkwidgetpart to select
  // those files... once we're in the dialog code it's too late.
  connect(dlg, SIGNAL(pattern(const QString &)),
	  this, SLOT(selectByPattern(const QString &)));
  bool bRedoExtract = false;

  if (m_nNumSelectedFiles == 0)
    dlg->disableSelectedFilesOption();
  if (archiveContent->currentItem() == NULL)
    dlg->disableCurrentFileOption();

  // list of files to be extracted
  m_extractList = new QStringList;
  if (dlg->exec())
    {
      int extractOp = dlg->extractOp();

      // if overwrite is false, then we need to check for failure of
      // extractions.
      bool bOvwrt = m_settings->getExtractOverwrite();

      switch(extractOp)
	{
	case ExtractDlg::All:
	  if (!bOvwrt)  // send empty list to indicate we're extracting all
	    bRedoExtract = reportExtractFailures(m_settings->getExtractDir(),
						 m_extractList);
	  if (!bRedoExtract) // if the user's OK with those failures, go ahead
	    {
	      // unless we have no space!
	      if (Utilities::diskHasSpacePart(m_settings->getExtractDir(),
					  m_nSizeOfFiles))
		{
		  arch->unarchFile(0);
		}
	    }
	  break;
	case ExtractDlg::Pattern:
	case ExtractDlg::Selected:
	case ExtractDlg::Current:
	  {
	    int nTotalSize = 0;
	    if (extractOp != ExtractDlg::Current )
	      {
		// make a list to send to unarchFile
		FileListView *flw = fileList();
		FileLVI *flvi = (FileLVI*)flw->firstChild();
		while (flvi)
		  {
		    if ( flw->isSelected(flvi) )
		      {
			QCString tmp = QFile::encodeName(flvi->getFileName());
			m_extractList->append(tmp);
			nTotalSize += flvi->text(getSizeColumn()).toInt();
		      }
		    flvi = (FileLVI*)flvi->itemBelow();
		  }
	      }
	    else
	      {
		FileLVI *pItem = archiveContent->currentItem();
		if (pItem == 0)	    return;
		QString tmp = pItem->getFileName();  // mj: text(0) won't work
		nTotalSize += pItem->text(getSizeColumn()).toInt();
		m_extractList->append( QFile::encodeName(tmp) );
	      }
	    if (!bOvwrt)
	      bRedoExtract =
		reportExtractFailures(m_settings->getExtractDir(),
				      m_extractList);
	    if (!bRedoExtract)
	      {
		if (Utilities::diskHasSpacePart(m_settings->getExtractDir(),
					    nTotalSize))
		  {
		    arch->unarchFile(m_extractList); // extract selected files
		  }
	      }
	    break;
	  }
	default:
	  ASSERT(0);
	  // never happens
	  break;
	}
    }

  // user might want to change some options or the selection...
  if (bRedoExtract)
    action_extract();
    
  delete dlg;
}

/*********************************************************************
*                       action_view
*
*          view a selected file in an opened archive file
*/
void ArkWidgetPart::action_view()
{
  FileLVI *pItem = archiveContent->currentItem();
  if (pItem  != NULL )
    {
      showFile(pItem);
    }
}

/*********************************************************************
*                        showFile
*
*          show a selected file using a suitable utility
*/
void ArkWidgetPart::showFile( FileLVI *_pItem )
{
  QString name = _pItem->getFileName(); // mj: NO text(0)!

  QString fullname;
  fullname = "file:";
  fullname += m_settings->getTmpDir();
  fullname += name;

  m_extractList = new QStringList;
  m_extractList->append(name);

  m_bViewInProgress = true;
  m_strFileToView = fullname;
  if (Utilities::diskHasSpacePart(m_settings->getTmpDir(),
			      _pItem->text(getSizeColumn()).toLong()))
    {
      arch->unarchFile(m_extractList, m_settings->getTmpDir() );
    }
}


/*********************************************************************
*                     slotSelectionChanged
*/
void ArkWidgetPart::slotSelectionChanged()
{
  updateStatusSelection();
  emit toKpartsView(m_nNumFiles, m_nNumSelectedFiles);
}


/*******************************************************************
*                    updateStatusSelection
*/
void ArkWidgetPart::updateStatusSelection()
{
  m_nNumSelectedFiles = 0;
  m_nSizeOfSelectedFiles = 0;

  if (archiveContent)
    {
      FileLVI * flvi = (FileLVI*)archiveContent->firstChild();
      while (flvi)
	{
	  if (flvi->isSelected())
	    {
	      ++m_nNumSelectedFiles;
	      if (m_currentSizeColumn != -1)
		m_nSizeOfSelectedFiles +=
		  flvi->text(m_currentSizeColumn).toInt();
	    }
	  flvi = (FileLVI*)flvi->itemBelow();
	}
    }
}


/*******************************************************************
*                       selectByPattern
*
*        select all the files that match a pattern
*/
void ArkWidgetPart::selectByPattern(const QString & _pattern) // slot
{
  FileLVI * flvi = (FileLVI*)archiveContent->firstChild();
  QRegExp *glob = new QRegExp(_pattern, true, true); // file globber

  archiveContent->clearSelection();
  while (flvi)
    {
      if (glob->match(flvi->getFileName(), 0, 0) != -1)
	archiveContent->setSelected(flvi, true);
      flvi = (FileLVI*)flvi->itemBelow();
    }

  delete glob;
}


/*******************************************************************
*                     createFileListView
*/
void ArkWidgetPart::createFileListView()
{
  if ( !archiveContent )
  {
    archiveContent = new FileListView(this, this);
    archiveContent->setMultiSelection(true);
    archiveContent->show();
    connect( archiveContent, SIGNAL( selectionChanged()),
  	   this, SLOT( slotSelectionChanged() ) );
  }

  archiveContent->clear();
}


/*********************************************************************
*                         badBzipName
*/
bool ArkWidgetPart::badBzipName(const QString & _filename)
{
  if (_filename.right(3) == ".BZ" || _filename.right(4) == ".TBZ")
    KMessageBox::error(this, i18n("Sorry, bzip doesn't like filename extensions that use capital letters.") );
  else if (_filename.right(4) == ".tbz")
    KMessageBox::error(this, i18n("Sorry, bzip doesn't like filename extensions that aren't exactly \".bz\"."));
  else if (_filename.right(4) == ".BZ2" ||  _filename.right(5) == ".TBZ2")
    KMessageBox::error(this, i18n("Sorry, bzip2 doesn't like filename extensions that use capital letters."));
  else if (_filename.right(5) == ".tbz2")
    KMessageBox::error(this, i18n("Sorry, bzip2 doesn't like filename extensions that aren't exactly \".bz2\".") );
  else
    return false;
  return true;
}


/*********************************************************************
*                          openArchive
*
*          open an archive file using a suitable utility
*/
void ArkWidgetPart::openArchive(const QString & _filename )
{
  // figure out what kind of archive it is
  Arch *newArch = 0;
  QString extension;
    
  goodFileType=1;
  // create Arch object accorting to the archive file type
	// use m_url to make a more intelligent guess at fileType
  m_archType = Arch::getArchType(_filename, extension, m_url);
  if(0 == (newArch = Arch::archFactory(m_archType, m_settings, this,
     _filename)))
  {
      if (!badBzipName(_filename))
	{
	  goodFileType=0;
	  // it's still a bad name, so let's try to figure out what it is.
	  // Maybe it just needs the proper extension!
	   KMimeMagic *mimePtr = KMimeMagic::self();
	   KMimeMagicResult * mimeResultPtr = mimePtr->findFileType(_filename);
	   QString mimetype = mimeResultPtr->mimeType();
	   if (mimetype == "application/x-gzip")
	     {
	       KMessageBox::error(this, i18n("Gzip archives need to have an extension `gz'."));
	       return;
	     }
	   else if (mimetype == "application/x-zoo")
	     {
	       KMessageBox::error(this, i18n("Zoo archives need to have an extension `zoo'."));
	       return;
	     }
	   else
	     {
	       KMessageBox::error( this, i18n("Unknown archive format or corrupted archive") );
	       // and just leave the old archive displayed
	       return;
	     }
	}
       else
	return;
    }

  if (!newArch->utilityIsAvailable())
    {
      goodFileType=0;
      KMessageBox::error(this, i18n("Sorry, the utility %1 is not in your PATH.\nPlease install it or contact your system administrator.").arg(newArch->getUtility()));
      return;
    }

  connect( newArch, SIGNAL(sigOpen(Arch *, bool, const QString &, int)),
	   this, SLOT(slotOpen(Arch *, bool, const QString &,int)) );
  connect( newArch, SIGNAL(sigExtract(bool)),
	   this, SLOT(slotExtractDone()));

  newArch->open();  //open the archive file
}



/*********************************************************************
*                        resizeEvent
*
*            resize the window size of the list view
*/
void ArkWidgetPart::resizeEvent ( QResizeEvent * newSize) {
   if ( archiveContent )
     archiveContent->resize( newSize->size() );
}

#include "arkwidgetpart.moc"