00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "newfolderdialog.h"
00030 #include "kmfolder.h"
00031 #include "folderstorage.h"
00032 #include "kmfolderimap.h"
00033 #include "kmfoldercachedimap.h"
00034 #include "kmfoldermgr.h"
00035 #include "kmfolderdir.h"
00036 #include "kmailicalifaceimpl.h"
00037 #include "kmacctimap.h"
00038 #include "kmacctcachedimap.h"
00039
00040 #include <kcombobox.h>
00041 #include <klineedit.h>
00042 #include <klocale.h>
00043 #include <kdialog.h>
00044 #include <kmessagebox.h>
00045 #include <kconfiggroup.h>
00046
00047 #include <QVariant>
00048 #include <QPushButton>
00049 #include <QLabel>
00050 #include <QLayout>
00051 #include <QRegExp>
00052 #include <QVBoxLayout>
00053 #include <QHBoxLayout>
00054
00055 using namespace KMail;
00056
00057 NewFolderDialog::NewFolderDialog( QWidget* parent, KMFolder *folder )
00058 : KDialog( parent ), mFolder( folder )
00059 {
00060 setCaption( i18n( "New Folder" ) );
00061 setButtons( Ok | Cancel );
00062 setModal( false );
00063 setObjectName( "new_folder_dialog" );
00064 setAttribute( Qt::WA_DeleteOnClose );
00065 if ( mFolder ) {
00066 setCaption( i18n( "New Subfolder of %1", mFolder->prettyUrl() ) );
00067 }
00068 connect( this, SIGNAL(okClicked()), SLOT(slotOk()) );
00069 QWidget *privateLayoutWidget = new QWidget( this );
00070 privateLayoutWidget->setObjectName( "mTopLevelLayout" );
00071 privateLayoutWidget->setGeometry( QRect( 10, 10, 260, 80 ) );
00072 setMainWidget( privateLayoutWidget );
00073 mTopLevelLayout = new QVBoxLayout( privateLayoutWidget );
00074 mTopLevelLayout->setObjectName( "mTopLevelLayout" );
00075 mTopLevelLayout->setSpacing( spacingHint() );
00076 mTopLevelLayout->setMargin( 0 );
00077
00078 mNameHBox = new QHBoxLayout();
00079 mNameHBox->setSpacing( 6 );
00080 mNameHBox->setMargin( 0 );
00081 mNameHBox->setObjectName( "mNameHBox" );
00082
00083 mNameLabel = new QLabel( privateLayoutWidget );
00084 mNameLabel->setObjectName( "mNameLabel" );
00085 mNameLabel->setText( i18nc( "@label:textbox Name of the new folder.", "&Name:" ) );
00086 mNameHBox->addWidget( mNameLabel );
00087
00088 mNameLineEdit = new KLineEdit( privateLayoutWidget );
00089 mNameLineEdit->setObjectName( "mNameLineEdit" );
00090 mNameLineEdit->setClearButtonShown( true );
00091 mNameLabel->setBuddy( mNameLineEdit );
00092 mNameLineEdit->setWhatsThis( i18n( "Enter a name for the new folder." ) );
00093 mNameLineEdit->setFocus();
00094 mNameHBox->addWidget( mNameLineEdit );
00095 mTopLevelLayout->addLayout( mNameHBox );
00096 connect( mNameLineEdit, SIGNAL( textChanged ( const QString & ) ), this, SLOT( slotFolderNameChanged( const QString & ) ) );
00097
00098 if ( !mFolder ||
00099 ( mFolder->folderType() != KMFolderTypeImap &&
00100 mFolder->folderType() != KMFolderTypeCachedImap ) ) {
00101 mFormatHBox = new QHBoxLayout();
00102 mFormatHBox->setSpacing( 6 );
00103 mFormatHBox->setMargin( 0 );
00104 mFormatHBox->setObjectName( "mFormatHBox" );
00105 mMailboxFormatLabel = new QLabel( privateLayoutWidget );
00106 mMailboxFormatLabel->setObjectName( "mMailboxFormatLabel" );
00107 mMailboxFormatLabel->setText( i18n( "Mailbox &format:" ) );
00108 mFormatHBox->addWidget( mMailboxFormatLabel );
00109
00110 mFormatComboBox = new KComboBox( privateLayoutWidget );
00111 mFormatComboBox->setEditable( false );
00112 mFormatComboBox->setObjectName( "mFormatComboBox" );
00113 mMailboxFormatLabel->setBuddy( mFormatComboBox );
00114 mFormatComboBox->setWhatsThis( 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." ) );
00115
00116 mFormatComboBox->insertItem(0,"mbox");
00117 mFormatComboBox->insertItem(1,"maildir");
00118
00119
00120 {
00121 KConfig *config = KMKernel::config();
00122 KConfigGroup group(config, "General");
00123 int type = group.readEntry("default-mailbox-format", 1 );
00124 if ( type < 0 || type > 1 ) type = 1;
00125 mFormatComboBox->setCurrentIndex( type );
00126 }
00127 mFormatHBox->addWidget( mFormatComboBox );
00128 mTopLevelLayout->addLayout( mFormatHBox );
00129 }
00130
00131
00132 if ( kmkernel->iCalIface().isEnabled() ) {
00133 mContentsHBox = new QHBoxLayout();
00134 mContentsHBox->setSpacing( 6 );
00135 mContentsHBox->setMargin( 0 );
00136 mContentsHBox->setObjectName( "mContentsHBox" );
00137
00138 mContentsLabel = new QLabel( privateLayoutWidget );
00139 mContentsLabel->setObjectName( "mContentsLabel" );
00140 mContentsLabel->setText( i18n( "Folder &contains:" ) );
00141 mContentsHBox->addWidget( mContentsLabel );
00142
00143 mContentsComboBox = new KComboBox( privateLayoutWidget );
00144 mContentsComboBox->setEditable( false );
00145 mContentsComboBox->setObjectName( "mContentsComboBox" );
00146 mContentsLabel->setBuddy( mContentsComboBox );
00147 mContentsComboBox->setWhatsThis( 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." ) );
00148 mContentsComboBox->addItem( i18n( "Mail" ) );
00149 mContentsComboBox->addItem( i18n( "Calendar" ) );
00150 mContentsComboBox->addItem( i18n( "Contacts" ) );
00151 mContentsComboBox->addItem( i18n( "Notes" ) );
00152 mContentsComboBox->addItem( i18n( "Tasks" ) );
00153 mContentsComboBox->addItem( i18n( "Journal" ) );
00154 if ( mFolder )
00155 mContentsComboBox->setCurrentIndex( mFolder->storage()->contentsType() );
00156 mContentsHBox->addWidget( mContentsComboBox );
00157 mTopLevelLayout->addLayout( mContentsHBox );
00158 }
00159
00160 if ( mFolder &&
00161 ( mFolder->folderType() == KMFolderTypeImap ||
00162 mFolder->folderType() == KMFolderTypeCachedImap ) ) {
00163 bool rootFolder = false;
00164 QStringList namespaces;
00165 if ( mFolder->folderType() == KMFolderTypeImap ) {
00166 ImapAccountBase* ai = static_cast<KMFolderImap*>(mFolder->storage())->account();
00167 if ( mFolder->storage() == ai->rootFolder() ) {
00168 rootFolder = true;
00169 namespaces = ai->namespaces()[ImapAccountBase::PersonalNS];
00170 }
00171 }
00172 if ( mFolder->folderType() == KMFolderTypeCachedImap ) {
00173 ImapAccountBase* ai = static_cast<KMFolderCachedImap*>(mFolder->storage())->account();
00174 if ( ai && mFolder->storage() == ai->rootFolder() ) {
00175 rootFolder = true;
00176 namespaces = ai->namespaces()[ImapAccountBase::PersonalNS];
00177 }
00178 }
00179 if ( rootFolder && namespaces.count() > 1 ) {
00180 mNamespacesHBox = new QHBoxLayout();
00181 mNamespacesHBox->setSpacing( 6 );
00182 mNamespacesHBox->setMargin( 0 );
00183 mNamespacesHBox->setObjectName( "mNamespaceHBox" );
00184
00185 mNamespacesLabel = new QLabel( privateLayoutWidget );
00186 mNamespacesLabel->setObjectName( "mNamespacesLabel" );
00187 mNamespacesLabel->setText( i18n( "Namespace for &folder:" ) );
00188 mNamespacesHBox->addWidget( mNamespacesLabel );
00189
00190 mNamespacesComboBox = new KComboBox( privateLayoutWidget );
00191 mNamespacesComboBox->setEditable( false );
00192 mNamespacesComboBox->setObjectName( "mNamespacesComboBox" );
00193 mNamespacesLabel->setBuddy( mNamespacesComboBox );
00194 mNamespacesComboBox->setWhatsThis( i18n( "Select the personal namespace the folder should be created in." ) );
00195 mNamespacesComboBox->addItems( namespaces );
00196 mNamespacesHBox->addWidget( mNamespacesComboBox );
00197 mTopLevelLayout->addLayout( mNamespacesHBox );
00198 } else {
00199 mNamespacesComboBox = 0;
00200 }
00201 }
00202
00203 resize( QSize(282, 108).expandedTo(minimumSizeHint()) );
00204 setAttribute(Qt::WA_WState_Polished);
00205 slotFolderNameChanged( mNameLineEdit->text());
00206 }
00207
00208 void NewFolderDialog::slotFolderNameChanged( const QString & _text)
00209 {
00210 enableButtonOk( !_text.isEmpty() );
00211 }
00212
00213 void NewFolderDialog::slotOk()
00214 {
00215 const QString fldName = mNameLineEdit->text();
00216 if ( fldName.isEmpty() ) {
00217 KMessageBox::error( this, i18n("Please specify a name for the new folder."),
00218 i18n( "No Name Specified" ) );
00219 return;
00220 }
00221
00222
00223 if ( fldName.contains( '/' ) &&
00224 ( !mFolder ||
00225 ( mFolder->folderType() != KMFolderTypeImap &&
00226 mFolder->folderType() != KMFolderTypeCachedImap ) ) ) {
00227 KMessageBox::error( this, i18n( "Folder names cannot contain the / (slash) character; please choose another folder name." ) );
00228 return;
00229 }
00230
00231
00232 if ( fldName.startsWith( '.' ) ) {
00233 KMessageBox::error( this, i18n( "Folder names cannot start with a . (dot) character; please choose another folder name." ) );
00234 return;
00235 }
00236
00237
00238 if ( mFolder &&
00239 ( mFolder->folderType() == KMFolderTypeImap ||
00240 mFolder->folderType() == KMFolderTypeCachedImap ) ) {
00241 QString delimiter;
00242 if ( mFolder->folderType() == KMFolderTypeImap ) {
00243 KMAcctImap* ai = static_cast<KMFolderImap*>( mFolder->storage() )->account();
00244 if ( ai )
00245 delimiter = ai->delimiterForFolder( mFolder->storage() );
00246 } else {
00247 KMAcctCachedImap* ai = static_cast<KMFolderCachedImap*>( mFolder->storage() )->account();
00248 if ( ai )
00249 delimiter = ai->delimiterForFolder( mFolder->storage() );
00250 }
00251 if ( !delimiter.isEmpty() && fldName.contains( delimiter ) ) {
00252 KMessageBox::error( this, i18n( "Your IMAP server does not allow the character '%1'; please choose another folder name.", delimiter ) );
00253 return;
00254 }
00255 }
00256
00257
00258 KMFolderDir * selectedFolderDir = &(kmkernel->folderMgr()->dir());
00259
00260 if ( mFolder )
00261 selectedFolderDir = mFolder->createChildFolder();
00262
00263
00264 if( selectedFolderDir->hasNamedFolder( fldName )
00265 && ( !( mFolder
00266 && ( selectedFolderDir == mFolder->parent() )
00267 && ( mFolder->storage()->objectName() == fldName ) ) ) )
00268 {
00269 const QString message = i18n( "<qt>Failed to create folder <b>%1</b>, folder already exists.</qt>", fldName);
00270 KMessageBox::error( this, message );
00271 return;
00272 }
00273
00274
00275 const QString message = i18n( "<qt>Failed to create folder <b>%1</b>."
00276 "</qt> ", fldName);
00277 bool success = false;
00278 KMFolder *newFolder = 0;
00279
00280 if ( mFolder && mFolder->folderType() == KMFolderTypeImap ) {
00281 KMFolderImap* selectedStorage = static_cast<KMFolderImap*>( mFolder->storage() );
00282 KMAcctImap *anAccount = selectedStorage->account();
00283
00284 if (anAccount->makeConnection() == ImapAccountBase::Connected) {
00285 newFolder = kmkernel->imapFolderMgr()->createFolder( fldName, false, KMFolderTypeImap, selectedFolderDir );
00286 if ( newFolder ) {
00287 QString imapPath, parent;
00288 if ( mNamespacesComboBox ) {
00289
00290 parent = anAccount->addPathToNamespace( mNamespacesComboBox->currentText() );
00291 imapPath = anAccount->createImapPath( parent, fldName );
00292 }
00293 KMFolderImap* newStorage = static_cast<KMFolderImap*>( newFolder->storage() );
00294 selectedStorage->createFolder(fldName, parent);
00295 newStorage->initializeFrom( selectedStorage, imapPath, QString() );
00296 static_cast<KMFolderImap*>(mFolder->storage())->setAccount( selectedStorage->account() );
00297 success = true;
00298 }
00299 }
00300 } else if ( mFolder && mFolder->folderType() == KMFolderTypeCachedImap ) {
00301 newFolder = kmkernel->dimapFolderMgr()->createFolder( fldName, false, KMFolderTypeCachedImap, selectedFolderDir );
00302 if ( newFolder ) {
00303 KMFolderCachedImap* selectedStorage = static_cast<KMFolderCachedImap*>( mFolder->storage() );
00304 KMFolderCachedImap* newStorage = static_cast<KMFolderCachedImap*>( newFolder->storage() );
00305 newStorage->initializeFrom( selectedStorage );
00306 if ( mNamespacesComboBox ) {
00307
00308 QString path = selectedStorage->account()->createImapPath(
00309 mNamespacesComboBox->currentText(), fldName );
00310 newStorage->setImapPathForCreation( path );
00311 }
00312 success = true;
00313 }
00314 } else {
00315
00316 if (mFormatComboBox->currentIndex() == 1)
00317 newFolder = kmkernel->folderMgr()->createFolder(fldName, false, KMFolderTypeMaildir, selectedFolderDir );
00318 else
00319 newFolder = kmkernel->folderMgr()->createFolder(fldName, false, KMFolderTypeMbox, selectedFolderDir );
00320 if ( newFolder )
00321 success = true;
00322 }
00323 if ( !success ) {
00324 KMessageBox::error( this, message );
00325 return;
00326 }
00327
00328
00329 if ( kmkernel->iCalIface().isEnabled() && mContentsComboBox ) {
00330 KMail::FolderContentsType type =
00331 static_cast<KMail::FolderContentsType>( mContentsComboBox->currentIndex() );
00332 newFolder->storage()->setContentsType( type );
00333 newFolder->storage()->writeConfig();
00334 }
00335 }
00336
00337 #include "newfolderdialog.moc"