kcmshell renamed to kcmshell4 to fix co-installability between kdelibs3 and kdebase4...
[kdepim.git] / kmail / newfolderdialog.cpp
blob030e70279ad00404ac3354a49f0b9829a3ccab2c
1 /*******************************************************************************
2 **
3 ** Filename : newfolderdialog.cpp
4 ** Created on : 30 January, 2005
5 ** Copyright : (c) 2005 Till Adam
6 ** Email : adam@kde.org
7 **
8 *******************************************************************************/
10 /*******************************************************************************
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
17 ** In addition, as a special exception, the copyright holders give
18 ** permission to link the code of this program with any edition of
19 ** the Qt library by Trolltech AS, Norway (or with modified versions
20 ** of Qt that use the same license as Qt), and distribute linked
21 ** combinations including the two. You must obey the GNU General
22 ** Public License in all respects for all of the code used other than
23 ** Qt. If you modify this file, you may extend this exception to
24 ** your version of the file, but you are not obligated to do so. If
25 ** you do not wish to do so, delete this exception statement from
26 ** your version.
27 *******************************************************************************/
30 #include <QVariant>
31 #include <QPushButton>
32 #include <QLabel>
33 #include <QLineEdit>
34 #include <QComboBox>
35 #include <QLayout>
38 #include <QRegExp>
39 #include <QVBoxLayout>
40 #include <QHBoxLayout>
42 #include <klocale.h>
43 #include <kdialog.h>
44 #include <kmessagebox.h>
45 #include <kconfiggroup.h>
47 #include "newfolderdialog.h"
48 #include "kmfolder.h"
49 #include "folderstorage.h"
50 #include "kmfolderimap.h"
51 #include "kmfoldercachedimap.h"
52 #include "kmfoldermgr.h"
53 #include "kmfolderdir.h"
54 #include "kmailicalifaceimpl.h"
55 #include "kmacctimap.h"
56 #include "kmacctcachedimap.h"
58 using namespace KMail;
60 NewFolderDialog::NewFolderDialog( QWidget* parent, KMFolder *folder )
61 : KDialog( parent ),
62 mFolder( folder )
64 setCaption( i18n( "New Folder" ) );
65 setButtons( Ok | Cancel );
66 setModal( false );
67 setObjectName( "new_folder_dialog" );
68 setAttribute( Qt::WA_DeleteOnClose );
69 if ( mFolder ) {
70 setCaption( i18n("New Subfolder of %1", mFolder->prettyUrl() ) );
72 connect( this, SIGNAL( okClicked() ), SLOT( slotOk() ) );
73 QWidget* privateLayoutWidget = new QWidget( this );
74 privateLayoutWidget->setObjectName( "mTopLevelLayout" );
75 privateLayoutWidget->setGeometry( QRect( 10, 10, 260, 80 ) );
76 setMainWidget( privateLayoutWidget );
77 mTopLevelLayout = new QVBoxLayout( privateLayoutWidget );
78 mTopLevelLayout->setObjectName( "mTopLevelLayout" );
79 mTopLevelLayout->setSpacing( spacingHint() );
80 mTopLevelLayout->setMargin( 0 );
82 mNameHBox = new QHBoxLayout();
83 mNameHBox->setSpacing( 6 );
84 mNameHBox->setMargin( 0 );
85 mNameHBox->setObjectName( "mNameHBox" );
87 mNameLabel = new QLabel( privateLayoutWidget );
88 mNameLabel->setObjectName( "mNameLabel" );
89 mNameLabel->setText( i18n( "&Name:" ) );
90 mNameHBox->addWidget( mNameLabel );
92 mNameLineEdit = new QLineEdit( privateLayoutWidget );
93 mNameLineEdit->setObjectName( "mNameLineEdit" );
94 mNameLabel->setBuddy( mNameLineEdit );
95 mNameLineEdit->setWhatsThis( i18n( "Enter a name for the new folder." ) );
96 mNameLineEdit->setFocus();
97 mNameHBox->addWidget( mNameLineEdit );
98 mTopLevelLayout->addLayout( mNameHBox );
99 connect( mNameLineEdit, SIGNAL( textChanged ( const QString & ) ), this, SLOT( slotFolderNameChanged( const QString & ) ) );
101 if ( !mFolder ||
102 ( mFolder->folderType() != KMFolderTypeImap &&
103 mFolder->folderType() != KMFolderTypeCachedImap ) ) {
104 mFormatHBox = new QHBoxLayout();
105 mFormatHBox->setSpacing( 6 );
106 mFormatHBox->setMargin( 0 );
107 mFormatHBox->setObjectName( "mFormatHBox" );
108 mMailboxFormatLabel = new QLabel( privateLayoutWidget );
109 mMailboxFormatLabel->setObjectName( "mMailboxFormatLabel" );
110 mMailboxFormatLabel->setText( i18n( "Mailbox &format:" ) );
111 mFormatHBox->addWidget( mMailboxFormatLabel );
113 mFormatComboBox = new QComboBox( privateLayoutWidget );
114 mFormatComboBox->setEditable( false );
115 mFormatComboBox->setObjectName( "mFormatComboBox" );
116 mMailboxFormatLabel->setBuddy( mFormatComboBox );
117 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." ) );
119 mFormatComboBox->insertItem(0,"mbox");
120 mFormatComboBox->insertItem(1,"maildir");
121 // does the below make any sense?
122 // mFormatComboBox->insertItem(2, "search");
124 KConfig *config = KMKernel::config();
125 KConfigGroup group(config, "General");
126 int type = group.readEntry("default-mailbox-format", 1 );
127 if ( type < 0 || type > 1 ) type = 1;
128 mFormatComboBox->setCurrentIndex( type );
130 mFormatHBox->addWidget( mFormatComboBox );
131 mTopLevelLayout->addLayout( mFormatHBox );
134 // --- contents -----
135 if ( kmkernel->iCalIface().isEnabled() ) {
136 mContentsHBox = new QHBoxLayout();
137 mContentsHBox->setSpacing( 6 );
138 mContentsHBox->setMargin( 0 );
139 mContentsHBox->setObjectName( "mContentsHBox" );
141 mContentsLabel = new QLabel( privateLayoutWidget );
142 mContentsLabel->setObjectName( "mContentsLabel" );
143 mContentsLabel->setText( i18n( "Folder &contains:" ) );
144 mContentsHBox->addWidget( mContentsLabel );
146 mContentsComboBox = new QComboBox( privateLayoutWidget );
147 mContentsComboBox->setEditable( false );
148 mContentsComboBox->setObjectName( "mContentsComboBox" );
149 mContentsLabel->setBuddy( mContentsComboBox );
150 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." ) );
151 mContentsComboBox->addItem( i18n( "Mail" ) );
152 mContentsComboBox->addItem( i18n( "Calendar" ) );
153 mContentsComboBox->addItem( i18n( "Contacts" ) );
154 mContentsComboBox->addItem( i18n( "Notes" ) );
155 mContentsComboBox->addItem( i18n( "Tasks" ) );
156 mContentsComboBox->addItem( i18n( "Journal" ) );
157 if ( mFolder ) // inherit contents type from papa
158 mContentsComboBox->setCurrentIndex( mFolder->storage()->contentsType() );
159 mContentsHBox->addWidget( mContentsComboBox );
160 mTopLevelLayout->addLayout( mContentsHBox );
163 if ( mFolder &&
164 ( mFolder->folderType() == KMFolderTypeImap ||
165 mFolder->folderType() == KMFolderTypeCachedImap ) ) {
166 bool rootFolder = false;
167 QStringList namespaces;
168 if ( mFolder->folderType() == KMFolderTypeImap ) {
169 ImapAccountBase* ai = static_cast<KMFolderImap*>(mFolder->storage())->account();
170 if ( mFolder->storage() == ai->rootFolder() ) {
171 rootFolder = true;
172 namespaces = ai->namespaces()[ImapAccountBase::PersonalNS];
175 if ( mFolder->folderType() == KMFolderTypeCachedImap ) {
176 ImapAccountBase* ai = static_cast<KMFolderCachedImap*>(mFolder->storage())->account();
177 if ( mFolder->storage() == ai->rootFolder() ) {
178 rootFolder = true;
179 namespaces = ai->namespaces()[ImapAccountBase::PersonalNS];
182 if ( rootFolder && namespaces.count() > 1 ) {
183 mNamespacesHBox = new QHBoxLayout();
184 mNamespacesHBox->setSpacing( 6 );
185 mNamespacesHBox->setMargin( 0 );
186 mNamespacesHBox->setObjectName( "mNamespaceHBox" );
188 mNamespacesLabel = new QLabel( privateLayoutWidget );
189 mNamespacesLabel->setObjectName( "mNamespacesLabel" );
190 mNamespacesLabel->setText( i18n( "Namespace for &folder:" ) );
191 mNamespacesHBox->addWidget( mNamespacesLabel );
193 mNamespacesComboBox = new QComboBox( privateLayoutWidget );
194 mNamespacesComboBox->setEditable( false );
195 mNamespacesComboBox->setObjectName( "mNamespacesComboBox" );
196 mNamespacesLabel->setBuddy( mNamespacesComboBox );
197 mNamespacesComboBox->setWhatsThis( i18n( "Select the personal namespace the folder should be created in." ) );
198 mNamespacesComboBox->addItems( namespaces );
199 mNamespacesHBox->addWidget( mNamespacesComboBox );
200 mTopLevelLayout->addLayout( mNamespacesHBox );
201 } else {
202 mNamespacesComboBox = 0;
206 resize( QSize(282, 108).expandedTo(minimumSizeHint()) );
207 setAttribute(Qt::WA_WState_Polished);
208 slotFolderNameChanged( mNameLineEdit->text());
211 void NewFolderDialog::slotFolderNameChanged( const QString & _text)
213 enableButtonOk( !_text.isEmpty() );
216 void NewFolderDialog::slotOk()
218 const QString fldName = mNameLineEdit->text();
219 if ( fldName.isEmpty() ) {
220 KMessageBox::error( this, i18n("Please specify a name for the new folder."),
221 i18n( "No Name Specified" ) );
222 return;
225 // names of local folders must not contain a '/'
226 if ( fldName.contains( '/' ) &&
227 ( !mFolder ||
228 ( mFolder->folderType() != KMFolderTypeImap &&
229 mFolder->folderType() != KMFolderTypeCachedImap ) ) ) {
230 KMessageBox::error( this, i18n( "Folder names cannot contain the / (slash) character; please choose another folder name." ) );
231 return;
234 // folder names must not start with a '.'
235 if ( fldName.startsWith( '.' ) ) {
236 KMessageBox::error( this, i18n( "Folder names cannot start with a . (dot) character; please choose another folder name." ) );
237 return;
240 // names of IMAP folders must not contain the folder delimiter
241 if ( mFolder &&
242 ( mFolder->folderType() == KMFolderTypeImap ||
243 mFolder->folderType() == KMFolderTypeCachedImap ) ) {
244 QString delimiter;
245 if ( mFolder->folderType() == KMFolderTypeImap ) {
246 KMAcctImap* ai = static_cast<KMFolderImap*>( mFolder->storage() )->account();
247 delimiter = ai->delimiterForFolder( mFolder->storage() );
248 } else {
249 KMAcctCachedImap* ai = static_cast<KMFolderCachedImap*>( mFolder->storage() )->account();
250 delimiter = ai->delimiterForFolder( mFolder->storage() );
252 if ( !delimiter.isEmpty() && fldName.contains( delimiter ) ) {
253 KMessageBox::error( this, i18n( "Your IMAP server does not allow the character '%1'; please choose another folder name.", delimiter ) );
254 return;
258 // default parent is Top Level local folders
259 KMFolderDir * selectedFolderDir = &(kmkernel->folderMgr()->dir());
260 // we got a parent, let's use that
261 if ( mFolder )
262 selectedFolderDir = mFolder->createChildFolder();
264 // check if the folder already exists
265 if( selectedFolderDir->hasNamedFolder( fldName )
266 && ( !( mFolder
267 && ( selectedFolderDir == mFolder->parent() )
268 && ( mFolder->storage()->objectName() == fldName ) ) ) )
270 const QString message = i18n( "<qt>Failed to create folder <b>%1</b>, folder already exists.</qt>" , fldName);
271 KMessageBox::error( this, message );
272 return;
275 /* Ok, obvious errors caught, let's try creating it for real. */
276 const QString message = i18n( "<qt>Failed to create folder <b>%1</b>."
277 "</qt> " , fldName);
278 bool success = false;
279 KMFolder *newFolder = 0;
281 if ( mFolder && mFolder->folderType() == KMFolderTypeImap ) {
282 KMFolderImap* selectedStorage = static_cast<KMFolderImap*>( mFolder->storage() );
283 KMAcctImap *anAccount = selectedStorage->account();
284 // check if a connection is available BEFORE creating the folder
285 if (anAccount->makeConnection() == ImapAccountBase::Connected) {
286 newFolder = kmkernel->imapFolderMgr()->createFolder( fldName, false, KMFolderTypeImap, selectedFolderDir );
287 if ( newFolder ) {
288 QString imapPath, parent;
289 if ( mNamespacesComboBox ) {
290 // create folder with namespace
291 parent = anAccount->addPathToNamespace( mNamespacesComboBox->currentText() );
292 imapPath = anAccount->createImapPath( parent, fldName );
294 KMFolderImap* newStorage = static_cast<KMFolderImap*>( newFolder->storage() );
295 selectedStorage->createFolder(fldName, parent); // create it on the server
296 newStorage->initializeFrom( selectedStorage, imapPath, QString() );
297 static_cast<KMFolderImap*>(mFolder->storage())->setAccount( selectedStorage->account() );
298 success = true;
301 } else if ( mFolder && mFolder->folderType() == KMFolderTypeCachedImap ) {
302 newFolder = kmkernel->dimapFolderMgr()->createFolder( fldName, false, KMFolderTypeCachedImap, selectedFolderDir );
303 if ( newFolder ) {
304 KMFolderCachedImap* selectedStorage = static_cast<KMFolderCachedImap*>( mFolder->storage() );
305 KMFolderCachedImap* newStorage = static_cast<KMFolderCachedImap*>( newFolder->storage() );
306 newStorage->initializeFrom( selectedStorage );
307 if ( mNamespacesComboBox ) {
308 // create folder with namespace
309 QString path = selectedStorage->account()->createImapPath(
310 mNamespacesComboBox->currentText(), fldName );
311 newStorage->setImapPathForCreation( path );
313 success = true;
315 } else {
316 // local folder
317 if (mFormatComboBox->currentIndex() == 1)
318 newFolder = kmkernel->folderMgr()->createFolder(fldName, false, KMFolderTypeMaildir, selectedFolderDir );
319 else
320 newFolder = kmkernel->folderMgr()->createFolder(fldName, false, KMFolderTypeMbox, selectedFolderDir );
321 if ( newFolder )
322 success = true;
324 if ( !success ) {
325 KMessageBox::error( this, message );
326 return;
329 // Set type field
330 if ( kmkernel->iCalIface().isEnabled() && mContentsComboBox ) {
331 KMail::FolderContentsType type =
332 static_cast<KMail::FolderContentsType>( mContentsComboBox->currentIndex() );
333 newFolder->storage()->setContentsType( type );
334 newFolder->storage()->writeConfig(); // connected slots will read it
338 #include "newfolderdialog.moc"