summaryrefslogtreecommitdiffstats
path: root/ark/ar.cpp
blob: 8edb2dd1f59beb1ffa1d9fac5c971813eb91b451 (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
/*

 ark -- archiver for the KDE project

 Copyright (C)

 1997-1999: Rob Palmbos palm9744@kettering.edu
 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.

*/

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

// QT includes
#include <tqdir.h>

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

// ark includes
#include "arkwidget.h"
#include "settings.h"
#include "ar.h"

ArArch::ArArch( ArkWidget *_gui, const TQString & _fileName )
  : Arch(_gui, _fileName )
{
  m_archiver_program = m_unarchiver_program = "ar";
  verifyCompressUtilityIsAvailable( m_archiver_program );
  verifyUncompressUtilityIsAvailable( m_unarchiver_program );

  // Do not set headerString - there is none for Ar
  m_numCols = 5;
  m_dateCol = 4; m_fixYear = 8; m_repairMonth = 5; m_fixDay = 6; m_fixTime = 7;

  m_archCols.append(new ArchColumns(1, TQRegExp("[a-zA-Z-]+"), 12)); // Perms
  m_archCols.append(new ArchColumns(2, TQRegExp("[^\\s]+"), 128)); //User/grp
  m_archCols.append(new ArchColumns(3, TQRegExp("[0-9]+"))); // Size
  m_archCols.append(new ArchColumns(5, TQRegExp("[a-zA-Z]+"), 4)); // Month
  m_archCols.append(new ArchColumns(6, TQRegExp("[0-9]+"), 2)); // Day
  m_archCols.append(new ArchColumns(7, TQRegExp("[0-9:]+"), 6)); // Time
  m_archCols.append(new ArchColumns(8, TQRegExp("[0-9]+"), 5)); // Year
  m_archCols.append(new ArchColumns(0, TQRegExp("[^\\s][^\\n]+"), 4096));// File

  kdDebug(1601) << "ArArch constructor" << endl;
}

void ArArch::setHeaders()
{
  ColumnList list;
  list.append( FILENAME_COLUMN );
  list.append( PERMISSION_COLUMN );
  list.append( OWNER_GROUP_COLUMN );
  list.append( SIZE_COLUMN );
  list.append( TIMESTAMP_COLUMN );

  emit headers( list );
}

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

  m_buffer = "";

  TDEProcess *kp = m_currentProcess = new TDEProcess;
  *kp << m_archiver_program << "vt" << m_filename;
  connect( kp, TQT_SIGNAL(receivedStdout(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedTOC(TDEProcess*, char*, int)));
  connect( kp, TQT_SIGNAL(receivedStderr(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));

  connect( kp, TQT_SIGNAL(processExited(TDEProcess*)), this,
	   TQT_SLOT(slotOpenExited(TDEProcess*)));

  if (kp->start(TDEProcess::NotifyOnExit, TDEProcess::AllOutput) == false)
    {
      KMessageBox::error( 0, i18n("Could not start a subprocess.") );
      emit sigOpen(this, false, TQString(), 0 );
    }
  kdDebug(1601) << "-ArArch::open" << endl;
}

void ArArch::create()
{
  TDEProcess *kp = m_currentProcess = new TDEProcess;
  kp->clearArguments();
  *kp << m_archiver_program << "c" << m_filename;

  connect( kp, TQT_SIGNAL(receivedStdout(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));
  connect( kp, TQT_SIGNAL(receivedStderr(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));

  if (kp->start(TDEProcess::Block) == false)
    {
      KMessageBox::error( 0, i18n("Could not start a subprocess.") );
      emit sigCreate(this, false, m_filename,
		     Arch::Extract | Arch::Delete | Arch::Add
		     | Arch::View);
    }
  else
    emit sigCreate(this, true, m_filename,
		   Arch::Extract | Arch::Delete | Arch::Add
		   | Arch::View);
}

void ArArch::addFile( const TQStringList &urls )
{
  kdDebug(1601) << "+ArArch::addFile" << endl;
  TDEProcess *kp = m_currentProcess = new TDEProcess;
  kp->clearArguments();
  *kp << m_archiver_program;

  if (ArkSettings::replaceOnlyWithNewer())
	  *kp << "ru";
  else
	  *kp << "r";

  *kp << m_filename;

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

  connect( kp, TQT_SIGNAL(receivedStdout(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));
  connect( kp, TQT_SIGNAL(receivedStderr(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));

  connect( kp, TQT_SIGNAL(processExited(TDEProcess*)), this,
	   TQT_SLOT(slotAddExited(TDEProcess*)));

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

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

void ArArch::unarchFileInternal()
{
  // if m_fileList is empty, we extract all.
  // if m_destDir is empty, abort with error.

  kdDebug(1601) << "+ArArch::unarchFile" << endl;
  TQString dest;

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

  // ar has no option to specify the destination directory
  // so I have to change to it.

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

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

  *kp << m_archiver_program;
  *kp << "vx";
  *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)
    {
      for ( TQStringList::Iterator it = m_fileList->begin();
	    it != m_fileList->end(); ++it )
	{
	  *kp << (*it);
	}
    }

  connect( kp, TQT_SIGNAL(receivedStdout(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));
  connect( kp, TQT_SIGNAL(receivedStderr(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));

  connect( kp, TQT_SIGNAL(processExited(TDEProcess*)), this,
	   TQT_SLOT(slotExtractExited(TDEProcess*)));

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

void ArArch::remove(TQStringList *list)
{
  kdDebug(1601) << "+ArArch::remove" << endl;

  if (!list)
    return;

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

  *kp << m_archiver_program << "d" << m_filename;
  for ( TQStringList::Iterator it = list->begin();
	it != list->end(); ++it )
    {
      TQString str = *it;
      *kp << str;
    }

  connect( kp, TQT_SIGNAL(receivedStdout(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));
  connect( kp, TQT_SIGNAL(receivedStderr(TDEProcess*, char*, int)),
	   this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));

  connect( kp, TQT_SIGNAL(processExited(TDEProcess*)), this,
	   TQT_SLOT(slotDeleteExited(TDEProcess*)));

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

  kdDebug(1601) << "-ArArch::remove" << endl;
}


#include "ar.moc"