summaryrefslogtreecommitdiffstats
path: root/kmail/importjob.cpp
blob: f7c44d19d06f2fc98a832a7d01c497316777288a (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
389
390
391
392
393
394
395
396
/* Copyright 2009 Klarälvdalens Datakonsult AB

   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) version 3 or any later version
   accepted by the membership of KDE e.V. (or its successor approved
   by the membership of KDE e.V.), which shall act as a proxy
   defined in Section 14 of version 3 of the license.

   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, see <http://www.gnu.org/licenses/>.
*/
#include "importjob.h"

#include "kmfolder.h"
#include "folderutil.h"
#include "kmfolderdir.h"
#include "kmfolderimap.h"
#include "imapjob.h"

#include "progressmanager.h"

#include <kdebug.h>
#include <kzip.h>
#include <ktar.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kmimetype.h>

#include <tqwidget.h>
#include <tqtimer.h>
#include <tqfile.h>

using namespace KMail;

KMail::ImportJob::ImportJob( TQWidget *tqparentWidget )
  : TQObject( tqparentWidget ),
    mArchive( 0 ),
    mRootFolder( 0 ),
    mParentWidget( tqparentWidget ),
    mNumberOfImportedMessages( 0 ),
    mCurrentFolder( 0 ),
    mCurrentMessage( 0 ),
    mCurrentMessageFile( 0 ),
    mProgressItem( 0 ),
    mAborted( false )
{
}

KMail::ImportJob::~ImportJob()
{
  if ( mArchive && mArchive->isOpened() ) {
    mArchive->close();
  }
  delete mArchive;
  mArchive = 0;
}

void KMail::ImportJob::setFile( const KURL &archiveFile )
{
  mArchiveFile = archiveFile;
}

void KMail::ImportJob::setRootFolder( KMFolder *rootFolder )
{
  mRootFolder = rootFolder;
}

void KMail::ImportJob::finish()
{
  kdDebug(5006) << "Finished import job." << endl;
  mProgressItem->setComplete();
  mProgressItem = 0;
  TQString text = i18n( "Importing the archive file '%1' into the folder '%2' succeeded." )
                     .arg( mArchiveFile.path() ).arg( mRootFolder->name() );
  text += "\n" + i18n( "1 message was imported.", "%n messages were imported.", mNumberOfImportedMessages );
  KMessageBox::information( mParentWidget, text, i18n( "Import finished." ) );
  deleteLater();
}

void KMail::ImportJob::cancelJob()
{
  abort( i18n( "The operation was canceled by the user." ) );
}

void KMail::ImportJob::abort( const TQString &errorMessage )
{
  if ( mAborted )
    return;

  mAborted = true;
  TQString text = i18n( "Failed to import the archive into folder '%1'." ).arg( mRootFolder->name() );
  text += "\n" + errorMessage;
  if ( mProgressItem ) {
    mProgressItem->setComplete();
    mProgressItem = 0;
    // The progressmanager will delete it
  }
  KMessageBox::sorry( mParentWidget, text, i18n( "Importing archive failed." ) );
  deleteLater();
}

KMFolder * KMail::ImportJob::createSubFolder( KMFolder *tqparent, const TQString &folderName, mode_t permissions )
{
  KMFolder *newFolder = FolderUtil::createSubFolder( tqparent, tqparent->child(), folderName, TQString(),
                                                     KMFolderTypeMaildir );
  if ( !newFolder ) {
    abort( i18n( "Unable to create subfolder for folder '%1'." ).arg( tqparent->name() ) );
    return 0;
  }
  else {
    newFolder->createChildFolder(); // TODO: Just creating a child folder here is wasteful, only do
                                    //       that if really needed. We do it here so we can set the
                                    //       permissions
    chmod( newFolder->location().latin1(), permissions | S_IXUSR );
    chmod( newFolder->subdirLocation().latin1(), permissions | S_IXUSR );
    // TODO: chown?
    // TODO: what about subdirectories like "cur"?
    return newFolder;
  }
}

void KMail::ImportJob::enqueueMessages( const KArchiveDirectory *dir, KMFolder *folder )
{
  const KArchiveDirectory *messageDir = dynamic_cast<const KArchiveDirectory*>( dir->entry( "cur" ) );
  if ( messageDir ) {
    Messages messagesToQueue;
    messagesToQueue.tqparent = folder;
    const TQStringList entries = messageDir->entries();
    for ( uint i = 0; i < entries.size(); i++ ) {
      const KArchiveEntry *entry = messageDir->entry( entries[i] );
      Q_ASSERT( entry );
      if ( entry->isDirectory() ) {
        kdWarning(5006) << "Unexpected subdirectory in archive folder " << dir->name() << endl;
      }
      else {
        kdDebug(5006) << "Queueing message " << entry->name() << endl;
        const KArchiveFile *file = static_cast<const KArchiveFile*>( entry );
        messagesToQueue.files.append( file );
      }
    }
    mQueuedMessages.append( messagesToQueue );
  }
  else {
    kdWarning(5006) << "No 'cur' subdirectory for archive directory " << dir->name() << endl;
  }
}

void KMail::ImportJob::messageAdded()
{
  mNumberOfImportedMessages++;
  if ( mCurrentFolder->folderType() == KMFolderTypeMaildir ||
       mCurrentFolder->folderType() == KMFolderTypeCachedImap ) {
    const TQString messageFile = mCurrentFolder->location() + "/cur/" + mCurrentMessage->fileName();
    // TODO: what if the file is not in the "cur" subdirectory?
    if ( TQFile::exists( messageFile ) ) {
      chmod( messageFile.latin1(), mCurrentMessageFile->permissions() );
      // TODO: changing user/group he requires a bit more work, requires converting the strings
      //       to uid_t and gid_t
      //getpwnam()
      //chown( messageFile,
    }
    else {
      kdWarning(5006) << "Unable to change permissions for newly created file: " << messageFile << endl;
    }
  }
  // TODO: Else?

  mCurrentMessage = 0;
  mCurrentMessageFile = 0;
  TQTimer::singleShot( 0, this, TQT_SLOT( importNextMessage() ) );
}

void KMail::ImportJob::importNextMessage()
{
  if ( mAborted )
    return;

  if ( mQueuedMessages.isEmpty() ) {
    kdDebug(5006) << "importNextMessage(): Processed all messages in the queue." << endl;
    if ( mCurrentFolder ) {
      mCurrentFolder->close( "ImportJob" );
    }
    mCurrentFolder = 0;
    importNextDirectory();
    return;
  }

  Messages &messages = mQueuedMessages.front();
  if ( messages.files.isEmpty() ) {
    mQueuedMessages.pop_front();
    importNextMessage();
    return;
  }

  KMFolder *folder = messages.tqparent;
  if ( folder != mCurrentFolder ) {
    kdDebug(5006) << "importNextMessage(): Processed all messages in the current folder of the queue." << endl;
    if ( mCurrentFolder ) {
      mCurrentFolder->close( "ImportJob" );
    }
    mCurrentFolder = folder;
    if ( mCurrentFolder->open( "ImportJob" ) != 0 ) {
      abort( i18n( "Unable to open folder '%1'." ).arg( mCurrentFolder->name() ) );
      return;
    }
    kdDebug(5006) << "importNextMessage(): Current folder of queue is now: " << mCurrentFolder->name() << endl;
    mProgressItem->setqStatus( i18n( "Importing folder %1" ).arg( mCurrentFolder->name() ) );
  }

  mProgressItem->setProgress( ( mProgressItem->progress() + 5 ) );

  mCurrentMessageFile = messages.files.first();
  Q_ASSERT( mCurrentMessageFile );
  messages.files.removeFirst();

  mCurrentMessage = new KMMessage();
  mCurrentMessage->fromByteArray( mCurrentMessageFile->data(), true /* setqStatus */ );
  int retIndex;

  // If this is not an IMAP folder, we can add the message directly. Otherwise, the whole thing is
  // async, for online IMAP. While addMsg() fakes a sync call, we rather do it the async way here
  // ourselves, as otherwise the whole thing gets pretty much messed up with regards to folder
  // refcounting. Furthermore, the completion dialog would be shown before the messages are actually
  // uploaded.
  if ( mCurrentFolder->folderType() != KMFolderTypeImap ) {
    if ( mCurrentFolder->addMsg( mCurrentMessage, &retIndex ) != 0 ) {
      abort( i18n( "Failed to add a message to the folder '%1'." ).arg( mCurrentFolder->name() ) );
      return;
    }
    messageAdded();
  }
  else {
    ImapJob *imapJob = new ImapJob( mCurrentMessage, ImapJob::tPutMessage,
                                    dynamic_cast<KMFolderImap*>( mCurrentFolder->storage() ) );
    connect( imapJob, TQT_SIGNAL(result(KMail::FolderJob*)),
             TQT_SLOT(messagePutResult(KMail::FolderJob*)) );
    imapJob->start();
  }
}

void KMail::ImportJob::messagePutResult( KMail::FolderJob *job )
{
  if ( mAborted )
    return;

  if ( job->error() ) {
    abort( i18n( "Failed to upload a message to the IMAP server." ) );
    return;
  } else {

    KMFolderImap *imap = dynamic_cast<KMFolderImap*>( mCurrentFolder->storage() );
    Q_ASSERT( imap );

    // Ok, we uploaded the message, but we still need to add it to the folder. Use addMsgQuiet(),
    // otherwise it will be uploaded again.
    imap->addMsgQuiet( mCurrentMessage );
    messageAdded();
  }
}

// Input: .inbox.directory
// Output: inbox
// Can also return an empty string if this is no valid dir name
static TQString folderNameForDirectoryName( const TQString &dirName )
{
  Q_ASSERT( dirName.startsWith( "." ) );
  const TQString end = ".directory";
  const int expectedIndex = dirName.length() - end.length();
  if ( dirName.lower().tqfind( end ) != expectedIndex )
    return TQString();
  TQString returnName = dirName.left( dirName.length() - end.length() );
  returnName = returnName.right( returnName.length() - 1 );
  return returnName;
}

KMFolder* KMail::ImportJob::getOrCreateSubFolder( KMFolder *tqparentFolder, const TQString &subFolderName,
                                           mode_t subFolderPermissions )
{
  if ( !tqparentFolder->createChildFolder() ) {
    abort( i18n( "Unable to create subfolder for folder '%1'." ).arg( tqparentFolder->name() ) );
    return 0;
  }

  KMFolder *subFolder = 0;
  subFolder = dynamic_cast<KMFolder*>( tqparentFolder->child()->hasNamedFolder( subFolderName ) );

  if ( !subFolder ) {
    subFolder = createSubFolder( tqparentFolder, subFolderName, subFolderPermissions );
  }
  return subFolder;
}

void KMail::ImportJob::importNextDirectory()
{
  if ( mAborted )
    return;

  if ( mQueuedDirectories.isEmpty() ) {
    finish();
    return;
  }

  Folder folder = mQueuedDirectories.first();
  KMFolder *currentFolder = folder.tqparent;
  mQueuedDirectories.pop_front();
  kdDebug(5006) << "importNextDirectory(): Working on directory " << folder.archiveDir->name() << endl;

  TQStringList entries = folder.archiveDir->entries();
  for ( uint i = 0; i < entries.size(); i++ ) {
    const KArchiveEntry *entry = folder.archiveDir->entry( entries[i] );
    Q_ASSERT( entry );
    kdDebug(5006) << "Queueing entry " << entry->name() << endl;
    if ( entry->isDirectory() ) {
      const KArchiveDirectory *dir = static_cast<const KArchiveDirectory*>( entry );
      if ( !dir->name().startsWith( "." ) ) {

        kdDebug(5006) << "Queueing messages in folder " << entry->name() << endl;
        KMFolder *subFolder = getOrCreateSubFolder( currentFolder, entry->name(), entry->permissions() );
        if ( !subFolder )
          return;

        enqueueMessages( dir, subFolder );
      }

      // Entry starts with a dot, so we assume it is a subdirectory
      else {

        const TQString folderName = folderNameForDirectoryName( entry->name() );
        if ( folderName.isEmpty() ) {
          abort( i18n( "Unexpected subdirectory named '%1'." ).arg( entry->name() ) );
          return;
        }
        KMFolder *subFolder = getOrCreateSubFolder( currentFolder, folderName, entry->permissions() );
        if ( !subFolder )
          return;

        Folder newFolder;
        newFolder.archiveDir = dir;
        newFolder.tqparent = subFolder;
        kdDebug(5006) << "Enqueueing directory " << entry->name() << endl;
        mQueuedDirectories.push_back( newFolder );
      }
    }
  }

  importNextMessage();
}

// TODO:
// BUGS:
//    Online IMAP can fail spectacular, for example when cancelling upload
//    Online IMAP: Inform that messages are still being uploaded on finish()!
void KMail::ImportJob::start()
{
  Q_ASSERT( mRootFolder );
  Q_ASSERT( mArchiveFile.isValid() );

  KMimeType::Ptr mimeType = KMimeType::findByURL( mArchiveFile, 0, true /* local file */ );
  if ( !mimeType->patterns().grep( "tar", false /* no case-sensitive */ ).isEmpty() )
    mArchive = new KTar( mArchiveFile.path() );
  else if ( !mimeType->patterns().grep( "zip", false ).isEmpty() )
    mArchive = new KZip( mArchiveFile.path() );
  else {
    abort( i18n( "The file '%1' does not appear to be a valid archive." ).arg( mArchiveFile.path() ) );
    return;
  }

  if ( !mArchive->open( IO_ReadOnly ) ) {
    abort( i18n( "Unable to open archive file '%1'" ).arg( mArchiveFile.path() ) );
    return;
  }

  mProgressItem = KPIM::ProgressManager::createProgressItem(
      "ImportJob",
      i18n( "Importing Archive" ),
      TQString(),
      true );
  mProgressItem->setUsesBusyIndicator( true );
  connect( mProgressItem, TQT_SIGNAL(progressItemCanceled(KPIM::ProgressItem*)),
           this, TQT_SLOT(cancelJob()) );

  Folder nextFolder;
  nextFolder.archiveDir = mArchive->directory();
  nextFolder.tqparent = mRootFolder;
  mQueuedDirectories.push_back( nextFolder );
  importNextDirectory();
}

#include "importjob.moc"