~ubuntu-branches/ubuntu/vivid/krusader/vivid-proposed

« back to all changes in this revision

Viewing changes to krusader/Splitter/combiner.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-05-05 22:26:37 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505222637-ydv3cwjwy365on2r
Tags: 1:2.1.0~beta1-1ubuntu1
* Merge from Debian Unstable.  Remaining changes:
  - Retain Kubuntu doc path

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include <kmessagebox.h>
35
35
#include <kfileitem.h>
36
36
#include <kio/job.h>
37
 
#include <qfileinfo.h>
 
37
#include <QtCore/QFileInfo>
38
38
 
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)
43
43
{
44
 
  crcContext = new CRC32();
45
 
 
46
 
  splitFile = "";
47
 
  
48
 
  setMaximum( 100 );
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();
 
45
 
 
46
    splitFile = "";
 
47
 
 
48
    setMaximum(100);
 
49
    setAutoClose(false);    /* don't close or reset the dialog automatically */
 
50
    setAutoReset(false);
 
51
    setLabelText("Krusader::Combiner");
 
52
    setWindowModality(Qt::WindowModal);
53
53
}
54
54
 
55
55
Combiner::~Combiner()
56
56
{
57
 
  combineAbortJobs();
58
 
  delete crcContext;
 
57
    combineAbortJobs();
 
58
    delete crcContext;
59
59
}
60
60
 
61
61
void Combiner::combine()
62
62
{
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()));
65
65
 
66
66
    /* check whether the .crc file exists */
67
 
  splURL = baseURL;
68
 
  splURL.setFileName( baseURL.fileName() + ".crc" );
69
 
  KFileItem file(KFileItem::Unknown, KFileItem::Unknown, splURL );
70
 
  file.refresh();
71
 
 
72
 
  if( !file.isReadable() )
73
 
  {
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() ) );
77
 
 
78
 
    if( ret == KMessageBox::No )
79
 
    {
80
 
       emit reject();
81
 
       return;
 
67
    splURL = baseURL;
 
68
    splURL.setFileName(baseURL.fileName() + ".crc");
 
69
    KFileItem file(KFileItem::Unknown, KFileItem::Unknown, splURL);
 
70
    file.refresh();
 
71
 
 
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?",
 
75
                                             splURL.pathOrUrl()));
 
76
 
 
77
        if (ret == KMessageBox::No) {
 
78
            emit reject();
 
79
            return;
 
80
        }
 
81
 
 
82
        openNextFile();
 
83
    } else {
 
84
        permissions = file.permissions() | QFile::WriteUser;
 
85
 
 
86
        combineReadJob = KIO::get(splURL, KIO::NoReload, KIO::HideProgressInfo);
 
87
 
 
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 *)));
82
92
    }
83
93
 
84
 
    openNextFile();
85
 
  }
86
 
  else
87
 
  {
88
 
    permissions = file.permissions() | QFile::WriteUser;
89
 
    
90
 
    combineReadJob = KIO::get( splURL, KIO::NoReload, KIO::HideProgressInfo );
91
 
 
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 *)));
96
 
  }
97
 
 
98
 
  exec();
 
94
    exec();
99
95
}
100
96
 
101
97
void Combiner::combineSplitFileDataReceived(KIO::Job *, const QByteArray &byteArray)
102
98
{
103
 
  splitFile += QString( byteArray );
 
99
    splitFile += QString(byteArray);
104
100
}
105
101
 
106
102
void Combiner::combineSplitFileFinished(KJob *job)
107
103
{
108
 
  combineReadJob = 0;
109
 
  QString error;
110
 
  
111
 
  if( job->error() )
112
 
    error = i18n("Error at reading the CRC file (%1)!", splURL.pathOrUrl() );
113
 
  else
114
 
  {
115
 
    splitFile.remove( '\r' ); // Windows compatibility
116
 
    QStringList splitFileContent = splitFile.split( '\n' );
117
 
    
118
 
    bool hasFileName = false, hasSize = false, hasCrc = false;
119
 
    
120
 
    for( int i = 0; i != splitFileContent.count(); i++ )
121
 
    {
122
 
      int ndx = splitFileContent[i].indexOf( '=' );    
123
 
      if( ndx == -1 )
124
 
        continue;      
125
 
      QString token = splitFileContent[i].left( ndx ).trimmed();
126
 
      QString value = splitFileContent[i].mid( ndx + 1 );      
127
 
    
128
 
      if( token == "filename" )
129
 
      {
130
 
        expectedFileName = value;
131
 
        hasFileName = true;
132
 
      }
133
 
      else if( token == "size" ) 
134
 
      {
135
 
        sscanf( value.trimmed().toLocal8Bit(), "%llu", &expectedSize );
136
 
        hasSize = true;
137
 
      }
138
 
      if( token == "crc32" )
139
 
      {
140
 
        expectedCrcSum   = value.trimmed().rightJustified( 8, '0' );
141
 
        hasCrc = true;
142
 
      }
143
 
    }
144
 
    
145
 
    if( !hasFileName || !hasSize || !hasCrc )
146
 
      error = i18n("Not a valid CRC file!");
147
 
    else
148
 
      hasValidSplitFile = true;
149
 
  }
150
 
      
151
 
  if( !error.isEmpty() )
152
 
  {
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 )
156
 
    {
157
 
       emit reject();
158
 
       return;
159
 
    }
160
 
  }
161
 
      
162
 
  openNextFile();
 
104
    combineReadJob = 0;
 
105
    QString error;
 
106
 
 
107
    if (job->error())
 
108
        error = i18n("Error at reading the CRC file (%1)!", splURL.pathOrUrl());
 
109
    else {
 
110
        splitFile.remove('\r');   // Windows compatibility
 
111
        QStringList splitFileContent = splitFile.split('\n');
 
112
 
 
113
        bool hasFileName = false, hasSize = false, hasCrc = false;
 
114
 
 
115
        for (int i = 0; i != splitFileContent.count(); i++) {
 
116
            int ndx = splitFileContent[i].indexOf('=');
 
117
            if (ndx == -1)
 
118
                continue;
 
119
            QString token = splitFileContent[i].left(ndx).trimmed();
 
120
            QString value = splitFileContent[i].mid(ndx + 1);
 
121
 
 
122
            if (token == "filename") {
 
123
                expectedFileName = value;
 
124
                hasFileName = true;
 
125
            } else if (token == "size") {
 
126
                sscanf(value.trimmed().toLocal8Bit(), "%llu", &expectedSize);
 
127
                hasSize = true;
 
128
            }
 
129
            if (token == "crc32") {
 
130
                expectedCrcSum   = value.trimmed().rightJustified(8, '0');
 
131
                hasCrc = true;
 
132
            }
 
133
        }
 
134
 
 
135
        if (!hasFileName || !hasSize || !hasCrc)
 
136
            error = i18n("Not a valid CRC file!");
 
137
        else
 
138
            hasValidSplitFile = true;
 
139
    }
 
140
 
 
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) {
 
145
            emit reject();
 
146
            return;
 
147
        }
 
148
    }
 
149
 
 
150
    openNextFile();
163
151
}
164
152
 
165
153
void Combiner::openNextFile()
166
 
{  
167
 
  if( unixNaming )
168
 
  {
169
 
    if( readURL.isEmpty() )
170
 
      readURL = baseURL;
171
 
    else
172
 
    {
173
 
      QString name = readURL.fileName();
174
 
      int pos = name.length()-1;
175
 
      QChar ch;
176
 
      
177
 
      do
178
 
      {
179
 
        ch = name.at( pos ).toLatin1() + 1;
180
 
        if( ch == QChar( 'Z' + 1 ) )
181
 
          ch = 'A';
182
 
        if( ch == QChar( 'z' + 1 ) )
183
 
          ch = 'a';
184
 
        name[ pos ] = ch;
185
 
        pos--;
186
 
      } while( pos >=0 && ch.toUpper() == QChar( 'A' ) );
187
 
      
188
 
      readURL.setFileName( name );
189
 
    }    
190
 
  }
191
 
  else
192
 
  {
193
 
    QString index( "%1" );      /* determining the filename */
194
 
    index = index.arg(++fileCounter).rightJustified( 3, '0' );
195
 
    readURL = baseURL;
196
 
    readURL.setFileName( baseURL.fileName() + "." + index );
197
 
  }
198
 
 
199
 
      /* creating a write job */
200
 
  combineReadJob = KIO::get( readURL, KIO::NoReload, KIO::HideProgressInfo );
201
 
 
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)));
209
 
  
 
154
{
 
155
    if (unixNaming) {
 
156
        if (readURL.isEmpty())
 
157
            readURL = baseURL;
 
158
        else {
 
159
            QString name = readURL.fileName();
 
160
            int pos = name.length() - 1;
 
161
            QChar ch;
 
162
 
 
163
            do {
 
164
                ch = name.at(pos).toLatin1() + 1;
 
165
                if (ch == QChar('Z' + 1))
 
166
                    ch = 'A';
 
167
                if (ch == QChar('z' + 1))
 
168
                    ch = 'a';
 
169
                name[ pos ] = ch;
 
170
                pos--;
 
171
            } while (pos >= 0 && ch.toUpper() == QChar('A'));
 
172
 
 
173
            readURL.setFileName(name);
 
174
        }
 
175
    } else {
 
176
        QString index("%1");        /* determining the filename */
 
177
        index = index.arg(++fileCounter).rightJustified(3, '0');
 
178
        readURL = baseURL;
 
179
        readURL.setFileName(baseURL.fileName() + '.' + index);
 
180
    }
 
181
 
 
182
    /* creating a write job */
 
183
    combineReadJob = KIO::get(readURL, KIO::NoReload, KIO::HideProgressInfo);
 
184
 
 
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)));
 
192
 
210
193
}
211
194
 
212
195
void Combiner::combineDataReceived(KIO::Job *, const QByteArray &byteArray)
213
196
{
214
 
  if( byteArray.size() == 0 )
215
 
    return;
216
 
 
217
 
  crcContext->update( (unsigned char *)byteArray.data(), byteArray.size() );
218
 
  transferArray = QByteArray( byteArray.data(), byteArray.length());
219
 
 
220
 
  receivedSize += byteArray.size();
221
 
 
222
 
  if( combineWriteJob == 0 )
223
 
  {
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" );
230
 
    
231
 
    combineWriteJob = KIO::put( writeURL, permissions, KIO::HideProgressInfo | KIO::Overwrite );
232
 
 
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 *)));
237
 
  }  
238
 
 
239
 
  if( combineWriteJob )
240
 
  {
241
 
    if( combineReadJob ) combineReadJob->suspend();    /* start writing */
242
 
    combineWriteJob->resume();
243
 
  }
 
197
    if (byteArray.size() == 0)
 
198
        return;
 
199
 
 
200
    crcContext->update((unsigned char *)byteArray.data(), byteArray.size());
 
201
    transferArray = QByteArray(byteArray.data(), byteArray.length());
 
202
 
 
203
    receivedSize += byteArray.size();
 
204
 
 
205
    if (combineWriteJob == 0) {
 
206
        writeURL = destinationURL;
 
207
        writeURL.addPath(baseURL.fileName());
 
208
        if (hasValidSplitFile)
 
209
            writeURL.setFileName(expectedFileName);
 
210
        else if (unixNaming)
 
211
            writeURL.setFileName(baseURL.fileName() + ".out");
 
212
 
 
213
        combineWriteJob = KIO::put(writeURL, permissions, KIO::HideProgressInfo | KIO::Overwrite);
 
214
 
 
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 *)));
 
219
    }
 
220
 
 
221
    if (combineWriteJob) {
 
222
        if (combineReadJob) combineReadJob->suspend();     /* start writing */
 
223
        combineWriteJob->resume();
 
224
    }
244
225
}
245
226
 
246
227
void Combiner::combineReceiveFinished(KJob *job)
247
228
{
248
 
  combineReadJob = 0;   /* KIO automatically deletes the object after Finished signal */
249
 
    
250
 
  if( job->error() )
251
 
  {
252
 
    if( combineWriteJob )         /* write out the remaining part of the file */
253
 
      combineWriteJob->resume();
254
 
    
255
 
    if( fileCounter == 1 )
256
 
    {
257
 
      combineAbortJobs();
258
 
      KMessageBox::questionYesNo(0, i18n("Can't open the first split file of %1!",
259
 
                                         baseURL.pathOrUrl( ) ) );
260
 
      emit reject();
261
 
      return;
262
 
    }    
263
 
 
264
 
    if( hasValidSplitFile )
265
 
    {
266
 
      QString crcResult = QString( "%1" ).arg( crcContext->result(), 0, 16 ).toUpper().trimmed()
267
 
                                         .rightJustified(8, '0');
268
 
      
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 */
 
230
 
 
231
    if (job->error()) {
 
232
        if (combineWriteJob)          /* write out the remaining part of the file */
 
233
            combineWriteJob->resume();
 
234
 
 
235
        if (fileCounter == 1) {
 
236
            combineAbortJobs();
 
237
            KMessageBox::questionYesNo(0, i18n("Can't open the first split file of %1!",
 
238
                                               baseURL.pathOrUrl()));
 
239
            emit reject();
 
240
            return;
 
241
        }
 
242
 
 
243
        if (hasValidSplitFile) {
 
244
            QString crcResult = QString("%1").arg(crcContext->result(), 0, 16).toUpper().trimmed()
 
245
                                .rightJustified(8, '0');
 
246
 
 
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!");
 
251
        }
 
252
        return;
273
253
    }
274
 
    return;
275
 
  }  
276
 
  openNextFile();
 
254
    openNextFile();
277
255
}
278
256
 
279
257
void Combiner::combineDataSend(KIO::Job *, QByteArray &byteArray)
280
258
{
281
 
  byteArray = transferArray;
282
 
  transferArray = QByteArray();
 
259
    byteArray = transferArray;
 
260
    transferArray = QByteArray();
283
261
 
284
 
  if( combineReadJob )
285
 
  {
286
 
    combineReadJob->resume();    /* start reading */
287
 
    combineWriteJob->suspend();
288
 
  }
 
262
    if (combineReadJob) {
 
263
        combineReadJob->resume();    /* start reading */
 
264
        combineWriteJob->suspend();
 
265
    }
289
266
}
290
267
 
291
268
void Combiner::combineSendFinished(KJob *job)
292
269
{
293
 
  combineWriteJob = 0;  /* KIO automatically deletes the object after Finished signal */
294
 
 
295
 
  if( job->error() )    /* any error occurred? */
296
 
  {
297
 
    combineAbortJobs();
298
 
    KMessageBox::error(0, i18n("Error writing file %1!", writeURL.pathOrUrl( ) ) );
299
 
    emit reject();
300
 
    return;
301
 
  }
302
 
 
303
 
  if( !error.isEmpty() )   /* was any error message at reading ? */
304
 
  {
305
 
    combineAbortJobs();             /* we cannot write out it in combineReceiveFinished */
306
 
    KMessageBox::error(0, error );  /* because emit accept closes it in this function */
307
 
    emit reject();
308
 
    return;
309
 
  }
310
 
 
311
 
  emit accept();
 
270
    combineWriteJob = 0;  /* KIO automatically deletes the object after Finished signal */
 
271
 
 
272
    if (job->error()) {   /* any error occurred? */
 
273
        combineAbortJobs();
 
274
        KMessageBox::error(0, i18n("Error writing file %1!", writeURL.pathOrUrl()));
 
275
        emit reject();
 
276
        return;
 
277
    }
 
278
 
 
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 */
 
282
        emit reject();
 
283
        return;
 
284
    }
 
285
 
 
286
    emit accept();
312
287
}
313
288
 
314
289
void Combiner::combineAbortJobs()
315
290
{
316
 
  if( combineReadJob )
317
 
    combineReadJob->kill( KJob::EmitResult );
318
 
  if( combineWriteJob )
319
 
    combineWriteJob->kill( KJob::EmitResult );
 
291
    if (combineReadJob)
 
292
        combineReadJob->kill(KJob::EmitResult);
 
293
    if (combineWriteJob)
 
294
        combineWriteJob->kill(KJob::EmitResult);
320
295
 
321
 
  combineReadJob = combineWriteJob = 0;
 
296
    combineReadJob = combineWriteJob = 0;
322
297
}
323
298
 
324
299
void Combiner::combineWritePercent(KJob *, unsigned long)
325
300
{
326
 
  int percent = (int)((((double)receivedSize / expectedSize ) * 100. ) + 0.5 );
327
 
  setValue ( percent );
 
301
    int percent = (int)((((double)receivedSize / expectedSize) * 100.) + 0.5);
 
302
    setValue(percent);
328
303
}
329
304
 
330
305
#include "combiner.moc"