34
34
#include <kmessagebox.h>
35
35
#include <kfileitem.h>
36
36
#include <kio/job.h>
37
#include <qfileinfo.h>
37
#include <QtCore/QFileInfo>
39
Combiner::Combiner( QWidget* parent, KUrl baseURLIn, KUrl destinationURLIn, bool unixNamingIn ) :
40
QProgressDialog( parent, 0 ), baseURL( baseURLIn ), destinationURL( destinationURLIn ),
41
hasValidSplitFile( false ), fileCounter ( 0 ), permissions( -1 ), receivedSize( 0 ),
42
combineReadJob( 0 ), combineWriteJob( 0 ), unixNaming( unixNamingIn )
39
Combiner::Combiner(QWidget* parent, KUrl baseURLIn, KUrl destinationURLIn, bool unixNamingIn) :
40
QProgressDialog(parent, 0), baseURL(baseURLIn), destinationURL(destinationURLIn),
41
hasValidSplitFile(false), fileCounter(0), permissions(-1), receivedSize(0),
42
combineReadJob(0), combineWriteJob(0), unixNaming(unixNamingIn)
44
crcContext = new CRC32();
49
setAutoClose( false ); /* don't close or reset the dialog automatically */
50
setAutoReset( false );
51
setLabelText( "Krusader::Combiner" );
52
setWindowModality( Qt::WindowModal );
44
crcContext = new CRC32();
49
setAutoClose(false); /* don't close or reset the dialog automatically */
51
setLabelText("Krusader::Combiner");
52
setWindowModality(Qt::WindowModal);
55
55
Combiner::~Combiner()
61
61
void Combiner::combine()
63
setWindowTitle( i18n("Krusader::Combining...") );
64
setLabelText( i18n("Combining the file %1...", baseURL.pathOrUrl() ));
63
setWindowTitle(i18n("Krusader::Combining..."));
64
setLabelText(i18n("Combining the file %1...", baseURL.pathOrUrl()));
66
66
/* check whether the .crc file exists */
68
splURL.setFileName( baseURL.fileName() + ".crc" );
69
KFileItem file(KFileItem::Unknown, KFileItem::Unknown, splURL );
72
if( !file.isReadable() )
74
int ret = KMessageBox::questionYesNo(0, i18n("The CRC information file (%1) is missing!\n"
75
"Validity checking is impossible without it. Continue combining?",
76
splURL.pathOrUrl() ) );
78
if( ret == KMessageBox::No )
68
splURL.setFileName(baseURL.fileName() + ".crc");
69
KFileItem file(KFileItem::Unknown, KFileItem::Unknown, splURL);
72
if (!file.isReadable()) {
73
int ret = KMessageBox::questionYesNo(0, i18n("The CRC information file (%1) is missing!\n"
74
"Validity checking is impossible without it. Continue combining?",
77
if (ret == KMessageBox::No) {
84
permissions = file.permissions() | QFile::WriteUser;
86
combineReadJob = KIO::get(splURL, KIO::NoReload, KIO::HideProgressInfo);
88
connect(combineReadJob, SIGNAL(data(KIO::Job *, const QByteArray &)),
89
this, SLOT(combineSplitFileDataReceived(KIO::Job *, const QByteArray &)));
90
connect(combineReadJob, SIGNAL(result(KJob*)),
91
this, SLOT(combineSplitFileFinished(KJob *)));
88
permissions = file.permissions() | QFile::WriteUser;
90
combineReadJob = KIO::get( splURL, KIO::NoReload, KIO::HideProgressInfo );
92
connect(combineReadJob, SIGNAL(data(KIO::Job *, const QByteArray &)),
93
this, SLOT(combineSplitFileDataReceived(KIO::Job *, const QByteArray &)));
94
connect(combineReadJob, SIGNAL(result(KJob*)),
95
this, SLOT(combineSplitFileFinished(KJob *)));
101
97
void Combiner::combineSplitFileDataReceived(KIO::Job *, const QByteArray &byteArray)
103
splitFile += QString( byteArray );
99
splitFile += QString(byteArray);
106
102
void Combiner::combineSplitFileFinished(KJob *job)
112
error = i18n("Error at reading the CRC file (%1)!", splURL.pathOrUrl() );
115
splitFile.remove( '\r' ); // Windows compatibility
116
QStringList splitFileContent = splitFile.split( '\n' );
118
bool hasFileName = false, hasSize = false, hasCrc = false;
120
for( int i = 0; i != splitFileContent.count(); i++ )
122
int ndx = splitFileContent[i].indexOf( '=' );
125
QString token = splitFileContent[i].left( ndx ).trimmed();
126
QString value = splitFileContent[i].mid( ndx + 1 );
128
if( token == "filename" )
130
expectedFileName = value;
133
else if( token == "size" )
135
sscanf( value.trimmed().toLocal8Bit(), "%llu", &expectedSize );
138
if( token == "crc32" )
140
expectedCrcSum = value.trimmed().rightJustified( 8, '0' );
145
if( !hasFileName || !hasSize || !hasCrc )
146
error = i18n("Not a valid CRC file!");
148
hasValidSplitFile = true;
151
if( !error.isEmpty() )
153
int ret = KMessageBox::questionYesNo( 0, error+ "\n" +
154
i18n("Validity checking is impossible without a good CRC file. Continue combining?") );
155
if( ret == KMessageBox::No )
108
error = i18n("Error at reading the CRC file (%1)!", splURL.pathOrUrl());
110
splitFile.remove('\r'); // Windows compatibility
111
QStringList splitFileContent = splitFile.split('\n');
113
bool hasFileName = false, hasSize = false, hasCrc = false;
115
for (int i = 0; i != splitFileContent.count(); i++) {
116
int ndx = splitFileContent[i].indexOf('=');
119
QString token = splitFileContent[i].left(ndx).trimmed();
120
QString value = splitFileContent[i].mid(ndx + 1);
122
if (token == "filename") {
123
expectedFileName = value;
125
} else if (token == "size") {
126
sscanf(value.trimmed().toLocal8Bit(), "%llu", &expectedSize);
129
if (token == "crc32") {
130
expectedCrcSum = value.trimmed().rightJustified(8, '0');
135
if (!hasFileName || !hasSize || !hasCrc)
136
error = i18n("Not a valid CRC file!");
138
hasValidSplitFile = true;
141
if (!error.isEmpty()) {
142
int ret = KMessageBox::questionYesNo(0,
143
error + i18n("\nValidity checking is impossible without a good CRC file. Continue combining?"));
144
if (ret == KMessageBox::No) {
165
153
void Combiner::openNextFile()
169
if( readURL.isEmpty() )
173
QString name = readURL.fileName();
174
int pos = name.length()-1;
179
ch = name.at( pos ).toLatin1() + 1;
180
if( ch == QChar( 'Z' + 1 ) )
182
if( ch == QChar( 'z' + 1 ) )
186
} while( pos >=0 && ch.toUpper() == QChar( 'A' ) );
188
readURL.setFileName( name );
193
QString index( "%1" ); /* determining the filename */
194
index = index.arg(++fileCounter).rightJustified( 3, '0' );
196
readURL.setFileName( baseURL.fileName() + "." + index );
199
/* creating a write job */
200
combineReadJob = KIO::get( readURL, KIO::NoReload, KIO::HideProgressInfo );
202
connect(combineReadJob, SIGNAL(data(KIO::Job *, const QByteArray &)),
203
this, SLOT(combineDataReceived(KIO::Job *, const QByteArray &)));
204
connect(combineReadJob, SIGNAL(result(KJob*)),
205
this, SLOT(combineReceiveFinished(KJob *)));
206
if( hasValidSplitFile )
207
connect(combineReadJob, SIGNAL(percent (KJob *, unsigned long)),
208
this, SLOT(combineWritePercent(KJob *, unsigned long)));
156
if (readURL.isEmpty())
159
QString name = readURL.fileName();
160
int pos = name.length() - 1;
164
ch = name.at(pos).toLatin1() + 1;
165
if (ch == QChar('Z' + 1))
167
if (ch == QChar('z' + 1))
171
} while (pos >= 0 && ch.toUpper() == QChar('A'));
173
readURL.setFileName(name);
176
QString index("%1"); /* determining the filename */
177
index = index.arg(++fileCounter).rightJustified(3, '0');
179
readURL.setFileName(baseURL.fileName() + '.' + index);
182
/* creating a write job */
183
combineReadJob = KIO::get(readURL, KIO::NoReload, KIO::HideProgressInfo);
185
connect(combineReadJob, SIGNAL(data(KIO::Job *, const QByteArray &)),
186
this, SLOT(combineDataReceived(KIO::Job *, const QByteArray &)));
187
connect(combineReadJob, SIGNAL(result(KJob*)),
188
this, SLOT(combineReceiveFinished(KJob *)));
189
if (hasValidSplitFile)
190
connect(combineReadJob, SIGNAL(percent(KJob *, unsigned long)),
191
this, SLOT(combineWritePercent(KJob *, unsigned long)));
212
195
void Combiner::combineDataReceived(KIO::Job *, const QByteArray &byteArray)
214
if( byteArray.size() == 0 )
217
crcContext->update( (unsigned char *)byteArray.data(), byteArray.size() );
218
transferArray = QByteArray( byteArray.data(), byteArray.length());
220
receivedSize += byteArray.size();
222
if( combineWriteJob == 0 )
224
writeURL = destinationURL;
225
writeURL.addPath( baseURL.fileName() );
226
if( hasValidSplitFile )
227
writeURL.setFileName( expectedFileName );
228
else if( unixNaming )
229
writeURL.setFileName( baseURL.fileName() + ".out" );
231
combineWriteJob = KIO::put( writeURL, permissions, KIO::HideProgressInfo | KIO::Overwrite );
233
connect(combineWriteJob, SIGNAL(dataReq(KIO::Job *, QByteArray &)),
234
this, SLOT(combineDataSend(KIO::Job *, QByteArray &)));
235
connect(combineWriteJob, SIGNAL(result(KJob*)),
236
this, SLOT(combineSendFinished(KJob *)));
239
if( combineWriteJob )
241
if( combineReadJob ) combineReadJob->suspend(); /* start writing */
242
combineWriteJob->resume();
197
if (byteArray.size() == 0)
200
crcContext->update((unsigned char *)byteArray.data(), byteArray.size());
201
transferArray = QByteArray(byteArray.data(), byteArray.length());
203
receivedSize += byteArray.size();
205
if (combineWriteJob == 0) {
206
writeURL = destinationURL;
207
writeURL.addPath(baseURL.fileName());
208
if (hasValidSplitFile)
209
writeURL.setFileName(expectedFileName);
211
writeURL.setFileName(baseURL.fileName() + ".out");
213
combineWriteJob = KIO::put(writeURL, permissions, KIO::HideProgressInfo | KIO::Overwrite);
215
connect(combineWriteJob, SIGNAL(dataReq(KIO::Job *, QByteArray &)),
216
this, SLOT(combineDataSend(KIO::Job *, QByteArray &)));
217
connect(combineWriteJob, SIGNAL(result(KJob*)),
218
this, SLOT(combineSendFinished(KJob *)));
221
if (combineWriteJob) {
222
if (combineReadJob) combineReadJob->suspend(); /* start writing */
223
combineWriteJob->resume();
246
227
void Combiner::combineReceiveFinished(KJob *job)
248
combineReadJob = 0; /* KIO automatically deletes the object after Finished signal */
252
if( combineWriteJob ) /* write out the remaining part of the file */
253
combineWriteJob->resume();
255
if( fileCounter == 1 )
258
KMessageBox::questionYesNo(0, i18n("Can't open the first split file of %1!",
259
baseURL.pathOrUrl( ) ) );
264
if( hasValidSplitFile )
266
QString crcResult = QString( "%1" ).arg( crcContext->result(), 0, 16 ).toUpper().trimmed()
267
.rightJustified(8, '0');
269
if( receivedSize != expectedSize )
270
error = i18n("Incorrect filesize! The file might have been corrupted!");
271
else if ( crcResult != expectedCrcSum.toUpper().trimmed() )
272
error = i18n("Incorrect CRC checksum! The file might have been corrupted!");
229
combineReadJob = 0; /* KIO automatically deletes the object after Finished signal */
232
if (combineWriteJob) /* write out the remaining part of the file */
233
combineWriteJob->resume();
235
if (fileCounter == 1) {
237
KMessageBox::questionYesNo(0, i18n("Can't open the first split file of %1!",
238
baseURL.pathOrUrl()));
243
if (hasValidSplitFile) {
244
QString crcResult = QString("%1").arg(crcContext->result(), 0, 16).toUpper().trimmed()
245
.rightJustified(8, '0');
247
if (receivedSize != expectedSize)
248
error = i18n("Incorrect filesize! The file might have been corrupted!");
249
else if (crcResult != expectedCrcSum.toUpper().trimmed())
250
error = i18n("Incorrect CRC checksum! The file might have been corrupted!");
279
257
void Combiner::combineDataSend(KIO::Job *, QByteArray &byteArray)
281
byteArray = transferArray;
282
transferArray = QByteArray();
259
byteArray = transferArray;
260
transferArray = QByteArray();
286
combineReadJob->resume(); /* start reading */
287
combineWriteJob->suspend();
262
if (combineReadJob) {
263
combineReadJob->resume(); /* start reading */
264
combineWriteJob->suspend();
291
268
void Combiner::combineSendFinished(KJob *job)
293
combineWriteJob = 0; /* KIO automatically deletes the object after Finished signal */
295
if( job->error() ) /* any error occurred? */
298
KMessageBox::error(0, i18n("Error writing file %1!", writeURL.pathOrUrl( ) ) );
303
if( !error.isEmpty() ) /* was any error message at reading ? */
305
combineAbortJobs(); /* we cannot write out it in combineReceiveFinished */
306
KMessageBox::error(0, error ); /* because emit accept closes it in this function */
270
combineWriteJob = 0; /* KIO automatically deletes the object after Finished signal */
272
if (job->error()) { /* any error occurred? */
274
KMessageBox::error(0, i18n("Error writing file %1!", writeURL.pathOrUrl()));
279
if (!error.isEmpty()) { /* was any error message at reading ? */
280
combineAbortJobs(); /* we cannot write out it in combineReceiveFinished */
281
KMessageBox::error(0, error); /* because emit accept closes it in this function */
314
289
void Combiner::combineAbortJobs()
317
combineReadJob->kill( KJob::EmitResult );
318
if( combineWriteJob )
319
combineWriteJob->kill( KJob::EmitResult );
292
combineReadJob->kill(KJob::EmitResult);
294
combineWriteJob->kill(KJob::EmitResult);
321
combineReadJob = combineWriteJob = 0;
296
combineReadJob = combineWriteJob = 0;
324
299
void Combiner::combineWritePercent(KJob *, unsigned long)
326
int percent = (int)((((double)receivedSize / expectedSize ) * 100. ) + 0.5 );
327
setValue ( percent );
301
int percent = (int)((((double)receivedSize / expectedSize) * 100.) + 0.5);
330
305
#include "combiner.moc"