kmail

newfolderdialog.cpp
00001 /*******************************************************************************
00002 **
00003 ** Filename   : newfolderdialog.cpp
00004 ** Created on : 30 January, 2005
00005 ** Copyright  : (c) 2005 Till Adam
00006 ** Email      : adam@kde.org
00007 **
00008 *******************************************************************************/
00009 
00010 /*******************************************************************************
00011 **
00012 **   This program is free software; you can redistribute it and/or modify
00013 **   it under the terms of the GNU General Public License as published by
00014 **   the Free Software Foundation; either version 2 of the License, or
00015 **   (at your option) any later version.
00016 **
00017 **   In addition, as a special exception, the copyright holders give
00018 **   permission to link the code of this program with any edition of
00019 **   the TQt library by Trolltech AS, Norway (or with modified versions
00020 **   of TQt that use the same license as TQt), and distribute linked
00021 **   combinations including the two.  You must obey the GNU General
00022 **   Public License in all respects for all of the code used other than
00023 **   TQt.  If you modify this file, you may extend this exception to
00024 **   your version of the file, but you are not obligated to do so.  If
00025 **   you do not wish to do so, delete this exception statement from
00026 **   your version.
00027 *******************************************************************************/
00028 
00029 #include <tqvariant.h>
00030 #include <tqpushbutton.h>
00031 #include <tqlabel.h>
00032 #include <tqlineedit.h>
00033 #include <tqcombobox.h>
00034 #include <tqlayout.h>
00035 #include <tqtooltip.h>
00036 #include <tqwhatsthis.h>
00037 #include <tqregexp.h>
00038 
00039 #include <tdelocale.h>
00040 #include <kdialogbase.h>
00041 #include <tdemessagebox.h>
00042 
00043 #include "folderutil.h"
00044 #include "newfolderdialog.h"
00045 #include "kmfolder.h"
00046 #include "folderstorage.h"
00047 #include "kmfolderimap.h"
00048 #include "kmfoldercachedimap.h"
00049 #include "kmfoldermgr.h"
00050 #include "kmfolderdir.h"
00051 #include "folderstorage.h"
00052 #include "kmailicalifaceimpl.h"
00053 #include "kmacctimap.h"
00054 #include "kmacctcachedimap.h"
00055 
00056 using namespace KMail;
00057 
00058 NewFolderDialog::NewFolderDialog( TQWidget* parent, KMFolder *folder )
00059     : KDialogBase( parent, "new_folder_dialog", false, i18n( "New Folder" ),
00060                    KDialogBase::Ok|KDialogBase::Cancel,
00061                    KDialogBase::Ok, true ),
00062       mFormatComboBox( 0 ),
00063       mContentsComboBox( 0 ),
00064       mNamespacesComboBox( 0 ),
00065       mFolder( folder )
00066 {
00067   setWFlags( getWFlags() | WDestructiveClose );
00068   if ( mFolder ) {
00069     setCaption( i18n("New Subfolder of %1").arg( mFolder->prettyURL() ) );
00070   }
00071   TQWidget* privateLayoutWidget = new TQWidget( this, "mTopLevelLayout" );
00072   privateLayoutWidget->setGeometry( TQRect( 10, 10, 260, 80 ) );
00073   setMainWidget( privateLayoutWidget );
00074   mTopLevelLayout = new TQVBoxLayout( privateLayoutWidget, 0, spacingHint(),
00075                                      "mTopLevelLayout");
00076 
00077   mNameHBox = new TQHBoxLayout( 0, 0, 6, "mNameHBox");
00078 
00079   mNameLabel = new TQLabel( privateLayoutWidget, "mNameLabel" );
00080   mNameLabel->setText( i18n( "&Name:" ) );
00081   mNameHBox->addWidget( mNameLabel );
00082 
00083   mNameLineEdit = new TQLineEdit( privateLayoutWidget, "mNameLineEdit" );
00084   mNameLabel->setBuddy( mNameLineEdit );
00085   TQWhatsThis::add( mNameLineEdit, i18n( "Enter a name for the new folder." ) );
00086   mNameLineEdit->setFocus();
00087   mNameHBox->addWidget( mNameLineEdit );
00088   mTopLevelLayout->addLayout( mNameHBox );
00089   connect( mNameLineEdit, TQT_SIGNAL( textChanged ( const TQString & ) ), this, TQT_SLOT( slotFolderNameChanged( const TQString & ) ) );
00090 
00091   if ( !mFolder ||
00092       ( mFolder->folderType() != KMFolderTypeImap &&
00093         mFolder->folderType() != KMFolderTypeCachedImap ) ) {
00094     mFormatHBox = new TQHBoxLayout( 0, 0, 6, "mFormatHBox");
00095     mMailboxFormatLabel = new TQLabel( privateLayoutWidget, "mMailboxFormatLabel" );
00096     mMailboxFormatLabel->setText( i18n( "Mailbox &format:" ) );
00097     mFormatHBox->addWidget( mMailboxFormatLabel );
00098 
00099     mFormatComboBox = new TQComboBox( false, privateLayoutWidget, "mFormatComboBox" );
00100     mMailboxFormatLabel->setBuddy( mFormatComboBox );
00101     TQWhatsThis::add( mFormatComboBox, i18n( "Select whether you want to store the messages in this folder as one file per  message (maildir) or as one big file (mbox). KMail uses maildir by default and this only needs to be changed in rare circumstances. If you are unsure, leave this option as-is." ) );
00102 
00103     mFormatComboBox->insertItem("mbox", 0);
00104     mFormatComboBox->insertItem("maildir", 1);
00105     // does the below make any sense?
00106     //  mFormatComboBox->insertItem("search", 2);
00107     {
00108       TDEConfig *config = KMKernel::config();
00109       TDEConfigGroupSaver saver(config, "General");
00110       int type = config->readNumEntry("default-mailbox-format", 1);
00111       if ( type < 0 || type > 1 ) type = 1;
00112       mFormatComboBox->setCurrentItem( type );
00113     }
00114     mFormatHBox->addWidget( mFormatComboBox );
00115     mTopLevelLayout->addLayout( mFormatHBox );
00116   }
00117 
00118   // --- contents -----
00119   if ( kmkernel->iCalIface().isEnabled() &&
00120        mFolder && mFolder->folderType() != KMFolderTypeImap ) {
00121     mContentsHBox = new TQHBoxLayout( 0, 0, 6, "mContentsHBox");
00122 
00123     mContentsLabel = new TQLabel( privateLayoutWidget, "mContentsLabel" );
00124     mContentsLabel->setText( i18n( "Folder &contains:" ) );
00125     mContentsHBox->addWidget( mContentsLabel );
00126 
00127     mContentsComboBox = new TQComboBox( false, privateLayoutWidget, "mContentsComboBox" );
00128     mContentsLabel->setBuddy( mContentsComboBox );
00129     TQWhatsThis::add( mContentsComboBox, i18n( "Select whether you want the new folder to be used for mail storage of for storage of groupware items such as tasks or notes. The default is mail. If you are unsure, leave this option as-is." ) );
00130     mContentsComboBox->insertItem( i18n( "Mail" ) );
00131     mContentsComboBox->insertItem( i18n( "Calendar" ) );
00132     mContentsComboBox->insertItem( i18n( "Contacts" ) );
00133     mContentsComboBox->insertItem( i18n( "Notes" ) );
00134     mContentsComboBox->insertItem( i18n( "Tasks" ) );
00135     mContentsComboBox->insertItem( i18n( "Journal" ) );
00136     if ( mFolder ) // inherit contents type from papa
00137       mContentsComboBox->setCurrentItem( mFolder->storage()->contentsType() );
00138     mContentsHBox->addWidget( mContentsComboBox );
00139     mTopLevelLayout->addLayout( mContentsHBox );
00140   }
00141 
00142   if ( mFolder &&
00143       ( mFolder->folderType() == KMFolderTypeImap ||
00144         mFolder->folderType() == KMFolderTypeCachedImap ) ) {
00145     bool rootFolder = false;
00146     TQStringList namespaces;
00147     if ( mFolder->folderType() == KMFolderTypeImap ) {
00148       ImapAccountBase* ai = static_cast<KMFolderImap*>(mFolder->storage())->account();
00149       if ( mFolder->storage() == ai->rootFolder() ) {
00150         rootFolder = true;
00151         namespaces = ai->namespaces()[ImapAccountBase::PersonalNS];
00152       }
00153     }
00154     if ( mFolder->folderType() == KMFolderTypeCachedImap ) {
00155       ImapAccountBase* ai = static_cast<KMFolderCachedImap*>(mFolder->storage())->account();
00156       if ( ai && mFolder->storage() == ai->rootFolder() ) {
00157         rootFolder = true;
00158         namespaces = ai->namespaces()[ImapAccountBase::PersonalNS];
00159       }
00160     }
00161     if ( rootFolder && namespaces.count() > 1 ) {
00162       mNamespacesHBox = new TQHBoxLayout( 0, 0, 6, "mNamespaceHBox");
00163 
00164       mNamespacesLabel = new TQLabel( privateLayoutWidget, "mNamespacesLabel" );
00165       mNamespacesLabel->setText( i18n( "Namespace for &folder:" ) );
00166       mNamespacesHBox->addWidget( mNamespacesLabel );
00167 
00168       mNamespacesComboBox = new TQComboBox( false, privateLayoutWidget, "mNamespacesComboBox" );
00169       mNamespacesLabel->setBuddy( mNamespacesComboBox );
00170       TQWhatsThis::add( mNamespacesComboBox, i18n( "Select the personal namespace the folder should be created in." ) );
00171       mNamespacesComboBox->insertStringList( namespaces );
00172       mNamespacesHBox->addWidget( mNamespacesComboBox );
00173       mTopLevelLayout->addLayout( mNamespacesHBox );
00174     } else {
00175       mNamespacesComboBox = 0;
00176     }
00177   }
00178 
00179   resize( TQSize(282, 108).expandedTo(minimumSizeHint()) );
00180   clearWState( WState_Polished );
00181   slotFolderNameChanged( mNameLineEdit->text());
00182 }
00183 
00184 void NewFolderDialog::slotFolderNameChanged( const TQString & _text)
00185 {
00186   enableButtonOK( !_text.isEmpty() );
00187 }
00188 
00189 void NewFolderDialog::slotOk()
00190 {
00191   const TQString fldName = mNameLineEdit->text();
00192   if ( fldName.isEmpty() ) {
00193      KMessageBox::error( this, i18n("Please specify a name for the new folder."),
00194         i18n( "No Name Specified" ) );
00195      return;
00196   }
00197 
00198   TQString msg;
00199   if ( mFolder && !mFolder->isValidName( fldName, msg ) ) {
00200     KMessageBox::error( this, msg );
00201     return;
00202   }
00203 
00204   // default parent is Top Level local folders
00205   KMFolderDir * selectedFolderDir = &(kmkernel->folderMgr()->dir());
00206   // we got a parent, let's use that
00207   if ( mFolder )
00208     selectedFolderDir = mFolder->createChildFolder();
00209 
00210   // check if the folder already exists
00211   if( selectedFolderDir->hasNamedFolder( fldName )
00212       && ( !( mFolder
00213           && ( selectedFolderDir == mFolder->parent() )
00214           && ( mFolder->storage()->name() == fldName ) ) ) )
00215   {
00216     const TQString message = i18n( "<qt>Failed to create folder <b>%1</b>, folder already exists.</qt>" ).arg(fldName);
00217     KMessageBox::error( this, message );
00218     return;
00219   }
00220 
00221   /* Ok, obvious errors caught, let's try creating it for real. */
00222   const TQString message = i18n( "<qt>Failed to create folder <b>%1</b>."
00223             "</qt> " ).arg(fldName);
00224 
00225   TQString namespaceName;
00226   if ( mNamespacesComboBox ) {
00227     namespaceName = mNamespacesComboBox->currentText();
00228   }
00229 
00230   KMFolderType folderType = KMFolderTypeUnknown;
00231   if ( mFormatComboBox && mFormatComboBox->currentItem() == 1 )
00232     folderType = KMFolderTypeMaildir;
00233   else if ( mFormatComboBox )
00234     folderType = KMFolderTypeMbox;
00235 
00236   KMFolder *newFolder = KMail::FolderUtil::createSubFolder( mFolder, selectedFolderDir, fldName,
00237                                                             namespaceName, folderType );
00238   if ( !newFolder ) {
00239     KMessageBox::error( this, message );
00240     return;
00241   }
00242 
00243   // Set type field
00244   if ( kmkernel->iCalIface().isEnabled() && mContentsComboBox ) {
00245     KMail::FolderContentsType type =
00246       static_cast<KMail::FolderContentsType>( mContentsComboBox->currentItem() );
00247     newFolder->storage()->setContentsType( type );
00248     newFolder->storage()->writeConfig(); // connected slots will read it
00249   }
00250   KDialogBase::slotOk();
00251 }
00252 
00253 #include "newfolderdialog.moc"