21 #include <tdelocale.h>
22 #include <tdemessagebox.h>
23 #include <tdeconfig.h>
25 #include <tdeapplication.h>
27 #include "kmmainwin.h"
28 #include "kmfiltermgr.h"
29 #include "kmfoldermgr.h"
30 #include "folderstorage.h"
32 #include "kmfoldercachedimap.h"
33 #include "kmacctcachedimap.h"
34 #include "renamejob.h"
35 #include "copyfolderjob.h"
41 KMFolderMgr::KMFolderMgr(
const TQString& aBasePath, KMFolderDirType dirType):
42 TQObject(), mDir(this, TQString(), dirType)
44 if ( dirType == KMStandardDir )
45 mDir.setBaseURL( I18N_NOOP(
"Local Folders") );
48 setBasePath(aBasePath);
54 KMFolderMgr::~KMFolderMgr()
56 mBasePath = TQString();
61 void KMFolderMgr::expireAll() {
62 TDEConfig *config = KMKernel::config();
63 TDEConfigGroupSaver saver(config,
"General");
64 int ret = KMessageBox::Continue;
66 if (config->readBoolEntry(
"warn-before-expire",
true)) {
67 ret = KMessageBox::warningContinueCancel(TDEMainWindow::memberList->first(),
68 i18n(
"Are you sure you want to expire old messages?"),
69 i18n(
"Expire Old Messages?"), i18n(
"Expire"));
72 if (ret == KMessageBox::Continue) {
73 expireAllFolders(
true );
78 #define DO_FOR_ALL(function, folder_code) \
80 TQPtrListIterator<KMFolderNode> it(*dir); \
81 for ( ; (node = it.current()); ) { \
83 if (node->isDir()) continue; \
84 KMFolder *folder = static_cast<KMFolder*>(node); \
86 KMFolderDir *child = folder->child(); \
98 count += folderCount( child );
111 void KMFolderMgr::compactAllFolders(
bool immediate,
KMFolderDir* dir)
117 compactAllFolders( immediate, child );
120 if ( folder->needsCompacting() )
121 folder->compact( immediate ? KMFolder::CompactNow : KMFolder::CompactLater );
128 void KMFolderMgr::setBasePath(
const TQString& aBasePath)
130 assert(!aBasePath.isNull());
132 if (aBasePath[0] ==
'~')
134 mBasePath = TQDir::homeDirPath();
135 mBasePath.append(
"/");
136 mBasePath.append(aBasePath.mid(1));
139 mBasePath = aBasePath;
141 TQFileInfo info( mBasePath );
145 if ( info.exists() ) {
146 if ( !info.isDir() ) {
147 KMessageBox::sorry(0, i18n(
"'%1' does not appear to be a folder.\n"
148 "Please move the file out of the way.")
152 if ( !info.isReadable() || !info.isWritable() ) {
153 KMessageBox::sorry(0, i18n(
"The permissions of the folder '%1' are "
155 "please make sure that you can view and modify "
156 "the content of this folder.")
162 if ( ::mkdir( TQFile::encodeName( mBasePath ) , S_IRWXU ) == -1 ) {
163 KMessageBox::sorry(0, i18n(
"KMail could not create folder '%1';\n"
164 "please make sure that you can view and "
165 "modify the content of the folder '%2'.")
166 .arg( mBasePath ).arg( TQDir::homeDirPath() ) );
170 mDir.setPath(mBasePath);
177 KMFolder* KMFolderMgr::createFolder(
const TQString& fName,
bool sysFldr,
178 KMFolderType aFolderType,
190 KMFolderCachedImap *storage =
static_cast<KMFolderCachedImap*
>( fldDir->
owner()->storage() );
191 KMAcctCachedImap *account = storage->account();
193 TQString imapPath = storage->imapPath();
194 if ( !imapPath.endsWith(
"/" ) )
197 if ( account->isDeletedFolder( imapPath ) || account->isDeletedFolder( imapPath +
"/" )
198 || account->isPreviouslyDeletedFolder( imapPath )
199 || account->isPreviouslyDeletedFolder( imapPath +
"/" ) ) {
200 KMessageBox::error( 0, i18n(
"A folder with the same name has been deleted since the last mail check."
201 "You need to check mails first before creating another folder with the same name."),
202 i18n(
"Could Not Create Folder") );
207 fld = fldDir->
createFolder(fName, sysFldr, aFolderType);
209 if ( fld->id() == 0 )
210 fld->setId( createId() );
212 emit folderAdded(fld);
213 if (kmkernel->filterMgr())
214 kmkernel->filterMgr()->folderCreated(fld);
222 KMFolder* KMFolderMgr::find(
const TQString& folderName,
bool foldersOnly)
226 for (node=mDir.first(); node; node=mDir.next())
228 if (node->isDir() && foldersOnly)
continue;
229 if (node->name()==folderName)
return (
KMFolder*)node;
235 KMFolder* KMFolderMgr::findById(
const uint
id)
237 return findIdString( TQString(),
id );
241 KMFolder* KMFolderMgr::findIdString(
const TQString& folderId,
250 KMFolder *folder = findIdString( folderId,
id, child );
255 if ( ( !folderId.isEmpty() && folder->
idString() == folderId ) ||
256 (
id != 0 && folder->id() ==
id ) )
264 void KMFolderMgr::getFolderURLS( TQStringList& flist,
const TQString& prefix,
271 getFolderURLS( flist, prefix +
"/" + folder->name(), child );
274 flist << prefix +
"/" + folder->name();
279 KMFolder* KMFolderMgr::getFolderByURL(
const TQString& vpath,
280 const TQString& prefix,
286 TQString a = prefix +
"/" + folder->name();
287 KMFolder * mfolder = getFolderByURL( vpath, a,child );
292 TQString comp = prefix +
"/" + folder->name();
301 KMFolder* KMFolderMgr::findOrCreate(
const TQString& aFolderName,
bool sysFldr,
306 folder = find(aFolderName);
308 folder = findById(
id);
312 static bool know_type =
false;
313 static KMFolderType type = KMFolderTypeMaildir;
314 if (know_type ==
false)
317 TDEConfig *config = KMKernel::config();
318 TDEConfigGroupSaver saver(config,
"General");
319 if (config->hasKey(
"default-mailbox-format"))
321 if (config->readNumEntry(
"default-mailbox-format", 1) == 0)
322 type = KMFolderTypeMbox;
327 folder = createFolder(aFolderName, sysFldr, type);
329 KMessageBox::error(0,(i18n(
"Cannot create file `%1' in %2.\nKMail cannot start without it.").arg(aFolderName).arg(mBasePath)));
340 void KMFolderMgr::remove(
KMFolder* aFolder)
342 if (!aFolder)
return;
344 if (!mRemoveOrig) mRemoveOrig = aFolder;
345 if (aFolder->
child())
349 TQPtrListIterator<KMFolderNode> it(*aFolder->
child());
350 for ( ; (node = it.current()); )
353 if (node->isDir())
continue;
358 emit folderRemoved(aFolder);
359 removeFolder(aFolder);
362 void KMFolderMgr::removeFolder(
KMFolder* aFolder)
364 connect(aFolder, TQ_SIGNAL(removed(
KMFolder*,
bool)),
365 this, TQ_SLOT(removeFolderAux(
KMFolder*,
bool)));
373 TQString parentName = fdir->name();
374 parentName = parentName.mid( 1, parentName.length()-11 );
376 if ( !parent && fdir->parent() )
381 parentF =
dynamic_cast<KMFolder*
>( parent );
385 void KMFolderMgr::removeFolderAux(
KMFolder* aFolder,
bool success)
394 for (fN = fdir->first(); fN != 0; fN = fdir->next()) {
395 if (fN->isDir() && (fN->name() ==
"." + aFolder->
fileName() +
".directory")) {
400 KMFolder* parentF = parentFolder( aFolder );
403 aFolder->parent()->
remove(aFolder);
408 if ( parentF != aFolder )
414 kdWarning(5006) <<
"Can not find parent folder" << endl;
416 if (aFolder == mRemoveOrig) {
423 void KMFolderMgr::removeDirAux(
KMFolderDir* aFolderDir)
426 TQString folderDirLocation = aFolderDir->
path();
428 aFolderDir->parent()->remove(aFolderDir);
429 dir.rmdir(folderDirLocation);
433 KMFolderRootDir& KMFolderMgr::dir(
void)
440 void KMFolderMgr::contentsChanged(
void)
442 if (mQuiet) mChanged = TRUE;
448 void KMFolderMgr::reload(
void)
453 void KMFolderMgr::createFolderList(TQStringList *str,
454 TQValueList<TQGuardedPtr<KMFolder> > *folders)
456 createFolderList( str, folders, 0,
"" );
460 void KMFolderMgr::createI18nFolderList(TQStringList *str,
461 TQValueList<TQGuardedPtr<KMFolder> > *folders)
463 createFolderList( str, folders, 0, TQString(),
true );
467 void KMFolderMgr::createFolderList(TQStringList *str,
468 TQValueList<TQGuardedPtr<KMFolder> > *folders,
470 const TQString& prefix,
477 createFolderList(str, folders, child,
" " + prefix, i18nized );
481 str->append(prefix + folder->
label());
483 str->append(prefix + folder->name());
484 folders->append( folder );
490 void KMFolderMgr::syncAllFolders(
KMFolderDir *adir )
495 syncAllFolders(child);
512 void KMFolderMgr::expireAllFolders(
bool immediate,
KMFolderDir *adir) {
517 expireAllFolders(immediate, child);
528 void KMFolderMgr::quiet(
bool beQuiet)
537 if (mChanged) emit changed();
549 tryReleasingFolder(f, child);
559 uint KMFolderMgr::createId()
564 newId = kapp->random();
565 }
while ( findById( newId ) != 0 );
573 renameFolder( folder, folder->name(), newParent );
577 void KMFolderMgr::renameFolder(
KMFolder* folder,
const TQString& newName,
581 connect( job, TQ_SIGNAL( renameDone( TQString,
bool ) ),
582 this, TQ_SLOT( slotRenameDone( TQString,
bool ) ) );
583 connect( job, TQ_SIGNAL( renameDone( TQString,
bool ) ),
584 this, TQ_SIGNAL( folderMoveOrCopyOperationFinished() ) );
591 kdDebug(5006) <<
"Copy folder: " << folder->
prettyURL() << endl;
593 connect( job, TQ_SIGNAL( folderCopyComplete(
bool ) ),
594 this, TQ_SIGNAL( folderMoveOrCopyOperationFinished() ) );
599 void KMFolderMgr::slotRenameDone( TQString,
bool success )
601 kdDebug(5006) << k_funcinfo << success << endl;
604 #include "kmfoldermgr.moc"
virtual void updateChildrenState()
Updates the hasChildren() state.
virtual void tryReleasingFolder(KMFolder *)
Try releasing folder if possible, something is attempting an exclusive access to it.
KMail list that manages the contents of one directory that may contain folders and/or other directori...
virtual KMFolder * createFolder(const TQString &folderName, bool sysFldr=false, KMFolderType folderType=KMFolderTypeMbox)
Create a mail folder in this directory with given name.
virtual TQString path() const
Return full pathname of this directory.
virtual KMFolderNode * hasNamedFolder(const TQString &name)
Returns folder with given name or zero if it does not exist.
KMFolder * owner() const
Returns the folder whose children we are holding.
bool isOpened() const
Test if folder is opened.
void sync()
fsync buffers to disk
TQString idString() const
Returns a string that can be used to identify this folder.
virtual TQString prettyURL() const
URL of the node for visualization purposes.
virtual TQString label() const
Returns the label of the folder for visualization.
KMFolderDir * child() const
Returns the folder directory associated with this node or 0 if no such directory exists.
TQString fileName() const
Returns the filename of the folder (reimplemented in KMFolderImap)
bool isAutoExpire() const
Does this folder automatically expire old messages?
void expireOldMessages(bool immediate)
Expire old messages in this folder.
KMFolderType folderType() const
Returns the type of this folder.
void remove()
Removes the folder physically from disk and empties the contents of the folder in memory.
Copy a hierarchy of folders somewhere else in the folder tree.
Rename and move (d)imap folders They can be moved everywhere (except search folders) as a new folder ...