kmail

newfolderdialog.cpp
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 *******************************************************************************/
9 
10 /*******************************************************************************
11 **
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.
16 **
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 TQt library by Trolltech AS, Norway (or with modified versions
20 ** of TQt that use the same license as TQt), 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 ** TQt. 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 *******************************************************************************/
28 
29 #include <tqvariant.h>
30 #include <tqpushbutton.h>
31 #include <tqlabel.h>
32 #include <tqlineedit.h>
33 #include <tqcombobox.h>
34 #include <tqlayout.h>
35 #include <tqtooltip.h>
36 #include <tqwhatsthis.h>
37 #include <tqregexp.h>
38 
39 #include <tdelocale.h>
40 #include <kdialogbase.h>
41 #include <tdemessagebox.h>
42 
43 #include "folderutil.h"
44 #include "newfolderdialog.h"
45 #include "kmfolder.h"
46 #include "folderstorage.h"
47 #include "kmfolderimap.h"
48 #include "kmfoldercachedimap.h"
49 #include "kmfoldermgr.h"
50 #include "kmfolderdir.h"
51 #include "folderstorage.h"
52 #include "kmailicalifaceimpl.h"
53 #include "kmacctimap.h"
54 #include "kmacctcachedimap.h"
55 
56 using namespace KMail;
57 
58 NewFolderDialog::NewFolderDialog( TQWidget* parent, KMFolder *folder )
59  : KDialogBase( parent, "new_folder_dialog", false, i18n( "New Folder" ),
60  KDialogBase::Ok|KDialogBase::Cancel,
61  KDialogBase::Ok, true ),
62  mFormatComboBox( 0 ),
63  mContentsComboBox( 0 ),
64  mNamespacesComboBox( 0 ),
65  mFolder( folder )
66 {
67  setWFlags( getWFlags() | WDestructiveClose );
68  if ( mFolder ) {
69  setCaption( i18n("New Subfolder of %1").arg( mFolder->prettyURL() ) );
70  }
71  TQWidget* privateLayoutWidget = new TQWidget( this, "mTopLevelLayout" );
72  privateLayoutWidget->setGeometry( TQRect( 10, 10, 260, 80 ) );
73  setMainWidget( privateLayoutWidget );
74  mTopLevelLayout = new TQVBoxLayout( privateLayoutWidget, 0, spacingHint(),
75  "mTopLevelLayout");
76 
77  mNameHBox = new TQHBoxLayout( 0, 0, 6, "mNameHBox");
78 
79  mNameLabel = new TQLabel( privateLayoutWidget, "mNameLabel" );
80  mNameLabel->setText( i18n( "&Name:" ) );
81  mNameHBox->addWidget( mNameLabel );
82 
83  mNameLineEdit = new TQLineEdit( privateLayoutWidget, "mNameLineEdit" );
84  mNameLabel->setBuddy( mNameLineEdit );
85  TQWhatsThis::add( mNameLineEdit, i18n( "Enter a name for the new folder." ) );
86  mNameLineEdit->setFocus();
87  mNameHBox->addWidget( mNameLineEdit );
88  mTopLevelLayout->addLayout( mNameHBox );
89  connect( mNameLineEdit, TQ_SIGNAL( textChanged ( const TQString & ) ), this, TQ_SLOT( slotFolderNameChanged( const TQString & ) ) );
90 
91  if ( !mFolder ||
92  ( mFolder->folderType() != KMFolderTypeImap &&
93  mFolder->folderType() != KMFolderTypeCachedImap ) ) {
94  mFormatHBox = new TQHBoxLayout( 0, 0, 6, "mFormatHBox");
95  mMailboxFormatLabel = new TQLabel( privateLayoutWidget, "mMailboxFormatLabel" );
96  mMailboxFormatLabel->setText( i18n( "Mailbox &format:" ) );
97  mFormatHBox->addWidget( mMailboxFormatLabel );
98 
99  mFormatComboBox = new TQComboBox( false, privateLayoutWidget, "mFormatComboBox" );
100  mMailboxFormatLabel->setBuddy( mFormatComboBox );
101  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." ) );
102 
103  mFormatComboBox->insertItem("mbox", 0);
104  mFormatComboBox->insertItem("maildir", 1);
105  // does the below make any sense?
106  // mFormatComboBox->insertItem("search", 2);
107  {
108  TDEConfig *config = KMKernel::config();
109  TDEConfigGroupSaver saver(config, "General");
110  int type = config->readNumEntry("default-mailbox-format", 1);
111  if ( type < 0 || type > 1 ) type = 1;
112  mFormatComboBox->setCurrentItem( type );
113  }
114  mFormatHBox->addWidget( mFormatComboBox );
115  mTopLevelLayout->addLayout( mFormatHBox );
116  }
117 
118  // --- contents -----
119  if ( kmkernel->iCalIface().isEnabled() &&
120  mFolder && mFolder->folderType() != KMFolderTypeImap ) {
121  mContentsHBox = new TQHBoxLayout( 0, 0, 6, "mContentsHBox");
122 
123  mContentsLabel = new TQLabel( privateLayoutWidget, "mContentsLabel" );
124  mContentsLabel->setText( i18n( "Folder &contains:" ) );
125  mContentsHBox->addWidget( mContentsLabel );
126 
127  mContentsComboBox = new TQComboBox( false, privateLayoutWidget, "mContentsComboBox" );
128  mContentsLabel->setBuddy( mContentsComboBox );
129  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." ) );
130  mContentsComboBox->insertItem( i18n( "Mail" ) );
131  mContentsComboBox->insertItem( i18n( "Calendar" ) );
132  mContentsComboBox->insertItem( i18n( "Contacts" ) );
133  mContentsComboBox->insertItem( i18n( "Notes" ) );
134  mContentsComboBox->insertItem( i18n( "Tasks" ) );
135  mContentsComboBox->insertItem( i18n( "Journal" ) );
136  if ( mFolder ) // inherit contents type from papa
137  mContentsComboBox->setCurrentItem( mFolder->storage()->contentsType() );
138  mContentsHBox->addWidget( mContentsComboBox );
139  mTopLevelLayout->addLayout( mContentsHBox );
140  }
141 
142  if ( mFolder &&
143  ( mFolder->folderType() == KMFolderTypeImap ||
144  mFolder->folderType() == KMFolderTypeCachedImap ) ) {
145  bool rootFolder = false;
146  TQStringList namespaces;
147  if ( mFolder->folderType() == KMFolderTypeImap ) {
148  ImapAccountBase* ai = static_cast<KMFolderImap*>(mFolder->storage())->account();
149  if ( mFolder->storage() == ai->rootFolder() ) {
150  rootFolder = true;
151  namespaces = ai->namespaces()[ImapAccountBase::PersonalNS];
152  }
153  }
154  if ( mFolder->folderType() == KMFolderTypeCachedImap ) {
155  ImapAccountBase* ai = static_cast<KMFolderCachedImap*>(mFolder->storage())->account();
156  if ( ai && mFolder->storage() == ai->rootFolder() ) {
157  rootFolder = true;
158  namespaces = ai->namespaces()[ImapAccountBase::PersonalNS];
159  }
160  }
161  if ( rootFolder && namespaces.count() > 1 ) {
162  mNamespacesHBox = new TQHBoxLayout( 0, 0, 6, "mNamespaceHBox");
163 
164  mNamespacesLabel = new TQLabel( privateLayoutWidget, "mNamespacesLabel" );
165  mNamespacesLabel->setText( i18n( "Namespace for &folder:" ) );
166  mNamespacesHBox->addWidget( mNamespacesLabel );
167 
168  mNamespacesComboBox = new TQComboBox( false, privateLayoutWidget, "mNamespacesComboBox" );
169  mNamespacesLabel->setBuddy( mNamespacesComboBox );
170  TQWhatsThis::add( mNamespacesComboBox, i18n( "Select the personal namespace the folder should be created in." ) );
171  mNamespacesComboBox->insertStringList( namespaces );
172  mNamespacesHBox->addWidget( mNamespacesComboBox );
173  mTopLevelLayout->addLayout( mNamespacesHBox );
174  } else {
175  mNamespacesComboBox = 0;
176  }
177  }
178 
179  resize( TQSize(282, 108).expandedTo(minimumSizeHint()) );
180  clearWState( WState_Polished );
181  slotFolderNameChanged( mNameLineEdit->text());
182 }
183 
184 void NewFolderDialog::slotFolderNameChanged( const TQString & _text)
185 {
186  enableButtonOK( !_text.isEmpty() );
187 }
188 
189 void NewFolderDialog::slotOk()
190 {
191  const TQString fldName = mNameLineEdit->text();
192  if ( fldName.isEmpty() ) {
193  KMessageBox::error( this, i18n("Please specify a name for the new folder."),
194  i18n( "No Name Specified" ) );
195  return;
196  }
197 
198  TQString msg;
199  if ( mFolder && !mFolder->isValidName( fldName, msg ) ) {
200  KMessageBox::error( this, msg );
201  return;
202  }
203 
204  // default parent is Top Level local folders
205  KMFolderDir * selectedFolderDir = &(kmkernel->folderMgr()->dir());
206  // we got a parent, let's use that
207  if ( mFolder )
208  selectedFolderDir = mFolder->createChildFolder();
209 
210  // check if the folder already exists
211  if( selectedFolderDir->hasNamedFolder( fldName )
212  && ( !( mFolder
213  && ( selectedFolderDir == mFolder->parent() )
214  && ( mFolder->storage()->name() == fldName ) ) ) )
215  {
216  const TQString message = i18n( "<qt>Failed to create folder <b>%1</b>, folder already exists.</qt>" ).arg(fldName);
217  KMessageBox::error( this, message );
218  return;
219  }
220 
221  /* Ok, obvious errors caught, let's try creating it for real. */
222  const TQString message = i18n( "<qt>Failed to create folder <b>%1</b>."
223  "</qt> " ).arg(fldName);
224 
225  TQString namespaceName;
226  if ( mNamespacesComboBox ) {
227  namespaceName = mNamespacesComboBox->currentText();
228  }
229 
230  KMFolderType folderType = KMFolderTypeUnknown;
231  if ( mFormatComboBox && mFormatComboBox->currentItem() == 1 )
232  folderType = KMFolderTypeMaildir;
233  else if ( mFormatComboBox )
234  folderType = KMFolderTypeMbox;
235 
236  KMFolder *newFolder = KMail::FolderUtil::createSubFolder( mFolder, selectedFolderDir, fldName,
237  namespaceName, folderType );
238  if ( !newFolder ) {
239  KMessageBox::error( this, message );
240  return;
241  }
242 
243  // Set type field
244  if ( kmkernel->iCalIface().isEnabled() && mContentsComboBox ) {
245  KMail::FolderContentsType type =
246  static_cast<KMail::FolderContentsType>( mContentsComboBox->currentItem() );
247  newFolder->storage()->setContentsType( type );
248  newFolder->storage()->writeConfig(); // connected slots will read it
249  }
250  KDialogBase::slotOk();
251 }
252 
253 #include "newfolderdialog.moc"
virtual void setContentsType(KMail::FolderContentsType type, bool quiet=false)
Set the type of contents held in this folder (mail, calendar, etc.)
virtual void writeConfig()
Write the config file.
KMail list that manages the contents of one directory that may contain folders and/or other directori...
Definition: kmfolderdir.h:16
virtual KMFolderNode * hasNamedFolder(const TQString &name)
Returns folder with given name or zero if it does not exist.
Mail folder.
Definition: kmfolder.h:69
@ Ok
The user rights/ACL have been fetched from the server sucessfully.
Definition: acljobs.h:66
folderdiaquotatab.h
Definition: aboutdata.cpp:40