From be4a178cfb761f20b279f504befb7d7759b4bcd7 Mon Sep 17 00:00:00 2001 From: Thomas McGuire Date: Fri, 6 Nov 2009 18:42:01 +0000 Subject: [PATCH] Factor out subfolder creation into a separate method, I need that for the import job. svn path=/branches/kdepim/enterprise/kdepim/; revision=1045769 --- Makefile.am | 4 ++- folderutil.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++ folderutil.h | 54 ++++++++++++++++++++++++++++++ newfolderdialog.cpp | 65 ++++++++++-------------------------- 4 files changed, 155 insertions(+), 49 deletions(-) create mode 100644 folderutil.cpp create mode 100644 folderutil.h diff --git a/Makefile.am b/Makefile.am index c23fdc70d..6ed31bbb8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -154,7 +154,9 @@ libkmailprivate_la_SOURCES = kmmessage.cpp kmmainwin.cpp configuredialog.cpp \ korghelper.cpp \ foldersetselector.cpp \ treebase.cpp \ - backupjob.cpp + backupjob.cpp \ + folderutil.cpp \ +# importjob.cpp \ libkmailprivate_la_COMPILE_FIRST = globalsettings_base.h customtemplates_base.h templatesconfiguration_base.h diff --git a/folderutil.cpp b/folderutil.cpp new file mode 100644 index 000000000..2a90cc276 --- /dev/null +++ b/folderutil.cpp @@ -0,0 +1,81 @@ +/* Copyright 2009 Thomas McGuire + + 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 . +*/ +#include "folderutil.h" + +#include "kmfolder.h" +#include "kmfolderimap.h" +#include "kmfoldercachedimap.h" +#include "kmfoldermgr.h" + +using namespace KMail; +using namespace FolderUtil; + +KMFolder *KMail::FolderUtil::createSubFolder( KMFolder *parentFolder, KMFolderDir *parentDir, + const QString &folderName, const QString &namespaceName, + KMFolderType localFolderType ) +{ + KMFolder *newFolder = 0; + + if ( parentFolder && parentFolder->folderType() == KMFolderTypeImap ) { + KMFolderImap* selectedStorage = static_cast( parentFolder->storage() ); + KMAcctImap *anAccount = selectedStorage->account(); + // check if a connection is available BEFORE creating the folder + if (anAccount->makeConnection() == ImapAccountBase::Connected) { + newFolder = kmkernel->imapFolderMgr()->createFolder( folderName, false, KMFolderTypeImap, parentDir ); + if ( newFolder ) { + QString imapPath, parent; + if ( !namespaceName.isEmpty() ) { + // create folder with namespace + parent = anAccount->addPathToNamespace( namespaceName ); + imapPath = anAccount->createImapPath( parent, folderName ); + } else { + imapPath = anAccount->createImapPath( selectedStorage->imapPath(), folderName ); + } + KMFolderImap* newStorage = static_cast( newFolder->storage() ); + selectedStorage->createFolder(folderName, parent); // create it on the server + newStorage->initializeFrom( selectedStorage, imapPath, QString::null ); + static_cast(parentFolder->storage())->setAccount( selectedStorage->account() ); + return newFolder; + } + } + } else if ( parentFolder && parentFolder->folderType() == KMFolderTypeCachedImap ) { + newFolder = kmkernel->dimapFolderMgr()->createFolder( folderName, false, KMFolderTypeCachedImap, + parentDir ); + if ( newFolder ) { + KMFolderCachedImap* selectedStorage = static_cast( parentFolder->storage() ); + KMFolderCachedImap* newStorage = static_cast( newFolder->storage() ); + newStorage->initializeFrom( selectedStorage ); + if ( !namespaceName.isEmpty() ) { + // create folder with namespace + QString path = selectedStorage->account()->createImapPath( + namespaceName, folderName ); + newStorage->setImapPathForCreation( path ); + } + return newFolder; + } + } else { + // local folder + Q_ASSERT( localFolderType == KMFolderTypeMaildir || localFolderType == KMFolderTypeMbox ); + newFolder = kmkernel->folderMgr()->createFolder( folderName, false, KMFolderTypeMaildir, + parentDir ); + return newFolder; + } + + return newFolder; +} \ No newline at end of file diff --git a/folderutil.h b/folderutil.h new file mode 100644 index 000000000..af5f9fda4 --- /dev/null +++ b/folderutil.h @@ -0,0 +1,54 @@ +/* Copyright 2009 Thomas McGuire + + 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 . +*/ +#ifndef FOLDERUTIL_H +#define FOLDERUTIL_H + +#include "kmfoldertype.h" + +class KMFolder; +class KMFolderDir; +class QString; + +namespace KMail +{ + +namespace FolderUtil +{ + +/** + * Low-level function to create a subfolder for a folder of any kind. + * + * @param parentFolder parent folder of the folder that should be created. Can be 0 in case of + * local folders + * @param parentDir parent folder directory, which should be the folder directory of parentFolder + * @param folderName the name the newly created folder should have + * @param namespaceName for (d)IMAP folders, the namespace the new folder should be in. Can be empty. + * @param localFolderType for local folders, this determines if the folder should be MBOX or maildir + * + * @return the newly created folder or 0 in case an error occured + */ +KMFolder *createSubFolder( KMFolder *parentFolder, KMFolderDir *parentDir, + const QString &folderName, const QString &namespaceName, + KMFolderType localFolderType ); + +} + +} + +#endif diff --git a/newfolderdialog.cpp b/newfolderdialog.cpp index 3103538da..2ef5c377d 100644 --- a/newfolderdialog.cpp +++ b/newfolderdialog.cpp @@ -40,6 +40,7 @@ #include #include +#include "folderutil.h" #include "newfolderdialog.h" #include "kmfolder.h" #include "folderstorage.h" @@ -58,7 +59,9 @@ NewFolderDialog::NewFolderDialog( QWidget* parent, KMFolder *folder ) : KDialogBase( parent, "new_folder_dialog", false, i18n( "New Folder" ), KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true ), + mFormatComboBox( 0 ), mContentsComboBox( 0 ), + mNamespacesComboBox( 0 ), mFolder( folder ) { setWFlags( getWFlags() | WDestructiveClose ); @@ -247,55 +250,21 @@ void NewFolderDialog::slotOk() /* Ok, obvious errors caught, let's try creating it for real. */ const QString message = i18n( "Failed to create folder %1." " " ).arg(fldName); - bool success = false; - KMFolder *newFolder = 0; - - if ( mFolder && mFolder->folderType() == KMFolderTypeImap ) { - KMFolderImap* selectedStorage = static_cast( mFolder->storage() ); - KMAcctImap *anAccount = selectedStorage->account(); - // check if a connection is available BEFORE creating the folder - if (anAccount->makeConnection() == ImapAccountBase::Connected) { - newFolder = kmkernel->imapFolderMgr()->createFolder( fldName, false, KMFolderTypeImap, selectedFolderDir ); - if ( newFolder ) { - QString imapPath, parent; - if ( mNamespacesComboBox ) { - // create folder with namespace - parent = anAccount->addPathToNamespace( mNamespacesComboBox->currentText() ); - imapPath = anAccount->createImapPath( parent, fldName ); - } else { - imapPath = anAccount->createImapPath( selectedStorage->imapPath(), fldName ); - } - KMFolderImap* newStorage = static_cast( newFolder->storage() ); - selectedStorage->createFolder(fldName, parent); // create it on the server - newStorage->initializeFrom( selectedStorage, imapPath, QString::null ); - static_cast(mFolder->storage())->setAccount( selectedStorage->account() ); - success = true; - } - } - } else if ( mFolder && mFolder->folderType() == KMFolderTypeCachedImap ) { - newFolder = kmkernel->dimapFolderMgr()->createFolder( fldName, false, KMFolderTypeCachedImap, selectedFolderDir ); - if ( newFolder ) { - KMFolderCachedImap* selectedStorage = static_cast( mFolder->storage() ); - KMFolderCachedImap* newStorage = static_cast( newFolder->storage() ); - newStorage->initializeFrom( selectedStorage ); - if ( mNamespacesComboBox ) { - // create folder with namespace - QString path = selectedStorage->account()->createImapPath( - mNamespacesComboBox->currentText(), fldName ); - newStorage->setImapPathForCreation( path ); - } - success = true; - } - } else { - // local folder - if (mFormatComboBox->currentItem() == 1) - newFolder = kmkernel->folderMgr()->createFolder(fldName, false, KMFolderTypeMaildir, selectedFolderDir ); - else - newFolder = kmkernel->folderMgr()->createFolder(fldName, false, KMFolderTypeMbox, selectedFolderDir ); - if ( newFolder ) - success = true; + + QString namespaceName; + if ( mNamespacesComboBox ) { + namespaceName = mNamespacesComboBox->currentText(); } - if ( !success ) { + + KMFolderType folderType = KMFolderTypeUnknown; + if ( mFormatComboBox && mFormatComboBox->currentItem() == 1 ) + folderType = KMFolderTypeMaildir; + else if ( mFormatComboBox ) + folderType = KMFolderTypeMbox; + + KMFolder *newFolder = KMail::FolderUtil::createSubFolder( mFolder, selectedFolderDir, fldName, + namespaceName, folderType ); + if ( !newFolder ) { KMessageBox::error( this, message ); return; }