summaryrefslogtreecommitdiffstats
path: root/kmail/renamejob.cpp
blob: 1c3cbef0b81bfa226d47e8dd8cc2c81b6d5d1b2d (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
/*
 * Copyright (c) 2004 Carsten Burghardt <[email protected]>
 *
 *  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; version 2 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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of this program with any edition of
 *  the TQt library by Trolltech AS, Norway (or with modified versions
 *  of TQt that use the same license as TQt), and distribute linked
 *  combinations including the two.  You must obey the GNU General
 *  Public License in all respects for all of the code used other than
 *  TQt.  If you modify this file, you may extend this exception to
 *  your version of the file, but you are not obligated to do so.  If
 *  you do not wish to do so, delete this exception statement from
 *  your version.
 */

#include "renamejob.h"
#include "copyfolderjob.h"
#include "kmfolderimap.h"
#include "kmfoldercachedimap.h"
#include "folderstorage.h"
#include "kmfolder.h"
#include "kmfolderdir.h"
#include "kmfoldermgr.h"
#include "imapaccountbase.h"
#include "kmacctimap.h"
#include "kmacctcachedimap.h"
#include "kmmsgbase.h"

#include <kdebug.h>
#include <kurl.h>
#include <tdeio/scheduler.h>
#include <tdeio/job.h>
#include <tdeio/global.h>
#include <tdelocale.h>
#include <config.h>

#include <tqmap.h>

using namespace KMail;

template <typename T> static TQStringList imapPaths( FolderStorage* storage )
{
  TQStringList rv;
  rv.append( static_cast<T>( storage )->imapPath() );
  KMFolderDir* dir = storage->folder()->child();
  if ( dir ) {
    KMFolderNode *node = dir->first();
    while ( node ) {
      if ( !node->isDir() ) {
        rv += imapPaths<T>( static_cast<KMFolder*>( node )->storage() );
      }
      node = dir->next();
    }
  }
  return rv;
}

RenameJob::RenameJob( FolderStorage* storage, const TQString& newName,
    KMFolderDir* newParent )
 : FolderJob( 0, tOther, (storage ? storage->folder() : 0) ),
   mStorage( storage ), mNewParent( newParent ),
   mNewName( newName ), mNewFolder( 0 ), mCopyFolderJob( 0 )
{
  mStorageTempOpened = 0;
  if ( storage ) {
    mOldName = storage->name();
    if ( storage->folderType() == KMFolderTypeImap ) {
      mOldImapPath = static_cast<KMFolderImap*>(storage)->imapPath();
//       mOldImapPaths = imapPaths<KMFolderImap*>( storage );
    } else if ( storage->folderType() == KMFolderTypeCachedImap ) {
      mOldImapPath = static_cast<KMFolderCachedImap*>(storage)->imapPath();
      mOldImapPaths = imapPaths<KMFolderCachedImap*>( storage );
    }
  }
}

RenameJob::~RenameJob()
{
}

// FIXME: move on the server for online imap given source and target are on the same server
void RenameJob::execute()
{
  if ( mNewParent )
  {
    // move the folder to a different parent
    KMFolderType type = mStorage->folderType();
    if ( ( type == KMFolderTypeMbox || type == KMFolderTypeMaildir ) &&
         mNewParent->type() == KMStandardDir &&
         mStorage->folderType() != KMFolderTypeCachedImap )
    {
      // local folders can handle this on their own
      mStorage->rename( mNewName, mNewParent );
      emit renameDone( mNewName, true );
      deleteLater();
      return;
    }
    // copy to the new folder
    mCopyFolderJob = new CopyFolderJob( mStorage, mNewParent );
    connect( mCopyFolderJob, TQ_SIGNAL(folderCopyComplete(bool)), TQ_SLOT(folderCopyComplete(bool)) );
    mCopyFolderJob->execute();

  } else
  {
    // only rename the folder
    if ( mStorage->folderType() != KMFolderTypeImap )
    {
      // local and dimap folder handle this directly
      mStorage->rename( mNewName );
      emit renameDone( mNewName, true );
      deleteLater();
      return;
    }
    if ( mOldImapPath.isEmpty() )
    {
      // sanity
      emit renameDone( mNewName, false );
      deleteLater();
      return;
    } else if ( mOldName == mNewName || mOldImapPath == "/INBOX/" ) {
      emit renameDone( mNewName, true ); // noop
      deleteLater();
      return;
    }
    ImapAccountBase* account = static_cast<KMFolderImap*>(mStorage)->account();
    // first rename it on the server
    mNewImapPath = mOldImapPath;
    mNewImapPath = mNewImapPath.replace( mOldName, mNewName );
    KURL src( account->getUrl() );
    src.setPath( mOldImapPath );
    KURL dst( account->getUrl() );
    dst.setPath( mNewImapPath );
    TDEIO::SimpleJob *job = TDEIO::rename( src, dst, true );
    kdDebug(5006)<< "RenameJob::rename - " << src.prettyURL()
      << " |=> " << dst.prettyURL() << endl;
    ImapAccountBase::jobData jd( src.url() );
    account->insertJob( job, jd );
    TDEIO::Scheduler::assignJobToSlave( account->slave(), job );
    connect( job, TQ_SIGNAL(result(TDEIO::Job*)),
        TQ_SLOT(slotRenameResult(TDEIO::Job*)) );
  }
}

void RenameJob::slotRenameResult( TDEIO::Job *job )
{
  ImapAccountBase* account = static_cast<KMFolderImap*>(mStorage)->account();
  ImapAccountBase::JobIterator it = account->findJob(job);
  if ( it == account->jobsEnd() )
  {
    emit renameDone( mNewName, false );
    deleteLater();
    return;
  }
  if ( job->error() )
  {
    account->handleJobError( job, i18n("Error while renaming a folder.") );
    emit renameDone( mNewName, false );
    deleteLater();
    return;
  }
  account->removeJob(it);
  // set the new path
  if ( mStorage->folderType() == KMFolderTypeImap )
    static_cast<KMFolderImap*>(mStorage)->setImapPath( mNewImapPath );
  // unsubscribe old (we don't want ghosts)
  account->changeSubscription( false, mOldImapPath );
  // subscribe new
  account->changeSubscription( true, mNewImapPath );

  // local part (will set the new name)
  mStorage->rename( mNewName );

  emit renameDone( mNewName, true );
  deleteLater();
}

void RenameJob::folderCopyComplete(bool success)
{
  kdDebug(5006) << k_funcinfo << success << endl;
  if ( !success ) {
    kdWarning(5006) << k_funcinfo << "could not copy folder" << endl;
    emit renameDone( mNewName, false );
    deleteLater();
    return;
  }
  mNewFolder = mCopyFolderJob->targetFolder();
  mCopyFolderJob = 0;

  if ( mStorageTempOpened ) {
    mStorageTempOpened->close( "renamejob" );
    mStorageTempOpened = 0;
  }

  kdDebug(5006) << "deleting old folder" << endl;
  // move complete or not necessary
  // save our settings
  TQString oldconfig = "Folder-" + mStorage->folder()->idString();
  TDEConfig* config = KMKernel::config();
  TQMap<TQString, TQString> entries = config->entryMap( oldconfig );
  TDEConfigGroupSaver saver(config, "Folder-" + mNewFolder->idString());
  for ( TQMap<TQString, TQString>::Iterator it = entries.begin();
        it != entries.end(); ++it )
  {
    if ( it.key() == "Id" || it.key() == "ImapPath" ||
          it.key() == "UidValidity" )
      continue;
    config->writeEntry( it.key(), it.data() );
  }
  mNewFolder->readConfig( config );
  // make sure the children state is correct
  if ( mNewFolder->child() &&
        ( mNewFolder->storage()->hasChildren() == FolderStorage::HasNoChildren ) )
    mNewFolder->storage()->updateChildrenState();

  // delete the old folder
  mStorage->blockSignals( false );
  if ( mStorage->folderType() == KMFolderTypeImap )
  {
    kmkernel->imapFolderMgr()->remove( mStorage->folder() );
  } else if ( mStorage->folderType() == KMFolderTypeCachedImap )
  {
    // tell the account (see KMFolderCachedImap::listDirectory2)
    KMAcctCachedImap* acct = static_cast<KMFolderCachedImap*>(mStorage)->account();
    if ( acct ) {
      for ( TQStringList::ConstIterator it = mOldImapPaths.constBegin(); it != mOldImapPaths.constEnd(); ++it )
        acct->addDeletedFolder( *it );
    }
    kmkernel->dimapFolderMgr()->remove( mStorage->folder() );
  } else if ( mStorage->folderType() == KMFolderTypeSearch )
  {
    // invalid
    kdWarning(5006) << k_funcinfo << "cannot remove a search folder" << endl;
  } else {
    kmkernel->folderMgr()->remove( mStorage->folder() );
  }

  emit renameDone( mNewName, true );
}

#include "renamejob.moc"