4
This file is part of KMail, the KDE mail client.
5
Copyright (C) 2002 Carsten Burghardt <burghardt@kde.org>
6
Copyright (C) 2009 Kevin Ottens <ervin@kde.org>
8
KMail is free software; you can redistribute it and/or modify it
9
under the terms of the GNU General Public License, version 2, as
10
published by the Free Software Foundation.
12
KMail is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
In addition, as a special exception, the copyright holders give
22
permission to link the code of this program with any edition of
23
the Qt library by Trolltech AS, Norway (or with modified versions
24
of Qt that use the same license as Qt), and distribute linked
25
combinations including the two. You must obey the GNU General
26
Public License in all respects for all of the code used other than
27
Qt. If you modify this file, you may extend this exception to
28
your version of the file, but you are not obligated to do so. If
29
you do not wish to do so, delete this exception statement from
33
#include "subscriptiondialog.h"
35
#include <QtCore/QTimer>
39
#include <kmessagebox.h>
41
#include <kimap/session.h>
42
#include <kimap/listjob.h>
43
#include <kimap/unsubscribejob.h>
44
#include <kimap/subscribejob.h>
46
SubscriptionDialogBase::SubscriptionDialogBase( QWidget *parent, const QString &caption,
47
KAccount *acct, bool &selectionChanged, const QString &startPath )
48
: KSubscription( parent, caption, acct, User1, QString(), false ),
49
mStartPath( startPath ), mSubscribed( false ), mForceSubscriptionEnable( false ), mSelectionChanged( selectionChanged )
51
// hide unneeded checkboxes
53
hideNewOnlyCheckbox();
56
connect(this, SIGNAL(user1Clicked()), SLOT(slotLoadFolders()));
58
// get the folders, delayed execution style, otherwise there's bother
59
// with virtuals from ctors and whatnot
60
QTimer::singleShot(0, this, SLOT(slotLoadFolders()));
63
//------------------------------------------------------------------------------
64
void SubscriptionDialogBase::slotListDirectory( KJob *job )
66
KIMAP::ListJob *list = static_cast<KIMAP::ListJob*>( job );
68
QList<KIMAP::MailBoxDescriptor> mailBoxes = list->mailBoxes();
70
foreach ( const KIMAP::MailBoxDescriptor &mailBox, mailBoxes ) {
71
mDelimiters << mailBox.separator;
72
mFolderPaths << mailBox.name;
73
mFolderNames << mailBox.name.split(mailBox.separator).last();
74
mFolderSelectable << !list->flags()[mailBox].contains("\\NoSelect");
77
mOnlySubscribed = !list->isIncludeUnsubscribed();
80
processFolderListing();
83
void SubscriptionDialogBase::slotButtonClicked( int button )
85
if ( button == KDialog::Ok ) {
91
KDialog::slotButtonClicked( button );
95
void SubscriptionDialogBase::moveChildrenToNewParent( GroupItem *oldItem, GroupItem *item )
97
if ( !oldItem || !item ) return;
99
QList<QTreeWidgetItem*> itemsToMove;
101
while ( QTreeWidgetItem* curItem = oldItem->QTreeWidgetItem::child( childIndex++ ) ) {
102
itemsToMove.append( curItem );
105
foreach( QTreeWidgetItem *cur, itemsToMove ) {
106
QTreeWidgetItem *taken = oldItem->takeChild( oldItem->indexOfChild( cur ) );
108
item->QTreeWidgetItem::insertChild( 0, taken );
109
if ( taken->isSelected() ) // we have new parents so open them
110
folderTree()->scrollToItem( taken );
113
// Delete the old item
114
QTreeWidgetItem *oldItemParent = oldItem->QTreeWidgetItem::parent();
116
delete oldItemParent->takeChild( oldItemParent->indexOfChild( oldItem ) );
118
QTreeWidget *treeWidget = oldItem->treeWidget();
119
delete treeWidget->takeTopLevelItem( treeWidget->indexOfTopLevelItem( oldItem ) );
124
void SubscriptionDialogBase::createListViewItem( int i )
127
GroupItem *parent = 0;
130
GroupItem *oldItem = 0;
132
findParentItem( mFolderNames[i], mFolderPaths[i], parentPath, &parent, &oldItem );
134
if (!parent && !parentPath.isEmpty())
136
// the parent is not available and it's no root-item
137
// this happens when the folders do not arrive in hierarchical order
138
// so we create each parent in advance
139
QStringList folders = parentPath.split( mDelimiters[i], QString::SkipEmptyParts );
141
for ( QStringList::Iterator it = folders.begin(); it != folders.end(); ++it )
143
KGroupInfo info(*it);
144
info.subscribed = false;
147
for ( uint j = 0; j <= i; ++j )
148
tmpPath << folders[j];
149
QString path = tmpPath.join(mDelimiters[i]);
151
item = mItemDict[path];
152
// as these items are "dummies" we create them non-checkable
156
item = new GroupItem(parent, info, this, false);
159
item = new GroupItem(folderTree(), info, this, false);
161
mItemDict.insert(info.path, item);
169
KGroupInfo info(mFolderNames[i]);
170
if (mFolderNames[i].toUpper() == "INBOX" &&
171
mFolderPaths[i] == "INBOX")
173
info.subscribed = false;
174
info.path = mFolderPaths[i];
175
// only checkable when the folder is selectable
176
bool checkable = mFolderSelectable[i];
179
item = new GroupItem(parent, info, this, checkable);
182
item = new GroupItem(folderTree(), info, this, checkable);
185
if (oldItem) // remove old item
186
mItemDict.remove(info.path);
188
mItemDict.insert(info.path, item);
190
moveChildrenToNewParent( oldItem, item );
192
// select the start item
193
if ( mFolderPaths[i] == mStartPath )
195
item->setSelected( true );
196
folderTree()->scrollToItem( item );
202
//------------------------------------------------------------------------------
203
void SubscriptionDialogBase::findParentItem( QString &name, QString &path, QString &parentPath,
204
GroupItem **parent, GroupItem **oldItem )
206
// remove the name (and the separator) from the path to get the parent path
207
int start = path.length() - (name.length()+1);
208
int length = name.length()+1;
209
if (start < 0) start = 0;
211
parentPath.remove(start, length);
213
// find the parent by it's path
214
QMap<QString, GroupItem*>::Iterator it = mItemDict.find( parentPath );
215
if ( it != mItemDict.end() )
218
// check if the item already exists
219
it = mItemDict.find( path );
220
if ( it != mItemDict.end() )
224
//------------------------------------------------------------------------------
225
void SubscriptionDialogBase::slotLoadFolders()
227
ImapAccount* ai = static_cast<ImapAccount*>(account());
228
// we need a connection
229
if ( !ai->session() || ai->session()->state() != KIMAP::Session::Authenticated )
231
kWarning() <<"SubscriptionDialog - got no connection";
236
KSubscription::slotLoadFolders();
241
// first step is to load a list of all available folders and create listview
243
listAllAvailableAndCreateItems();
246
//------------------------------------------------------------------------------
247
void SubscriptionDialogBase::processNext()
250
mFolderNames.clear();
251
mFolderPaths.clear();
252
mFolderSelectable.clear();
254
ImapAccount* ai = static_cast<ImapAccount*>(account());
256
KIMAP::ListJob *list = new KIMAP::ListJob( ai->session() );
257
list->setIncludeUnsubscribed( !mSubscribed );
258
connect( list, SIGNAL( result(KJob*) ), this, SLOT( slotListDirectory(KJob*) ) );
262
void SubscriptionDialogBase::loadingComplete()
264
slotLoadingComplete();
268
//------------------------------------------------------------------------------
269
// implementation for server side subscription
270
//------------------------------------------------------------------------------
272
SubscriptionDialog::SubscriptionDialog( QWidget *parent, const QString &caption,
273
KAccount *acct, bool &selectionChanged, const QString & startPath )
274
: SubscriptionDialogBase( parent, caption, acct, selectionChanged, startPath )
279
SubscriptionDialog::~SubscriptionDialog()
284
void SubscriptionDialog::listAllAvailableAndCreateItems()
289
//------------------------------------------------------------------------------
290
void SubscriptionDialogBase::slotConnectionSuccess()
295
bool SubscriptionDialogBase::checkIfSubscriptionsEnabled()
297
ImapAccount *account = static_cast<ImapAccount*>(mAcct);
300
if( subscriptionOptionEnabled( account ) )
303
int result = KMessageBox::questionYesNoCancel( this,
304
subscriptionOptionQuestion( account->server() ),
305
i18n("Enable Subscriptions?"), KGuiItem( i18n("Enable") ), KGuiItem( i18n("Do Not Enable") ) );
308
case KMessageBox::Yes:
309
mForceSubscriptionEnable = true;
311
case KMessageBox::No:
313
case KMessageBox::Cancel:
323
void SubscriptionDialog::processFolderListing()
329
bool SubscriptionDialog::doSave()
331
if ( !checkIfSubscriptionsEnabled() )
334
ImapAccount *ai = static_cast<ImapAccount*>(account());
337
QTreeWidgetItemIterator it(subView);
340
KIMAP::SubscribeJob *subscribe = new KIMAP::SubscribeJob( ai->session() );
341
subscribe->setMailBox(
342
static_cast<GroupItem*>(*it)->info().path
345
mSelectionChanged = true;
349
QTreeWidgetItemIterator it2(unsubView);
352
KIMAP::UnsubscribeJob *unsubscribe = new KIMAP::UnsubscribeJob( ai->session() );
353
unsubscribe->setMailBox(
354
static_cast<GroupItem*>(*it2)->info().path
357
mSelectionChanged = true;
360
if ( mForceSubscriptionEnable ) {
361
ai->setSubscriptionEnabled( true );
367
bool SubscriptionDialog::subscriptionOptionEnabled( const ImapAccount *account ) const
369
return account->isSubscriptionEnabled();
372
QString SubscriptionDialog::subscriptionOptionQuestion( const QString &accountName ) const
374
return i18nc( "@info", "Currently subscriptions are not used for server <resource>%1</resource>.<nl/>"
375
"\nDo you want to enable subscriptions?", accountName );
378
void SubscriptionDialog::processItems()
380
bool onlySubscribed = mOnlySubscribed;
382
for (int i = mCount; i < mFolderNames.count(); ++i)
384
// give the dialog a chance to repaint
388
QTimer::singleShot(0, this, SLOT(processItems()));
393
if (!onlySubscribed && mFolderPaths.size() > 0)
395
createListViewItem( i );
396
} else if (onlySubscribed)
399
if ( mItemDict[mFolderPaths[i]] )
401
GroupItem* item = mItemDict[mFolderPaths[i]];
407
if ( !mSubscribed ) {
415
#include "subscriptiondialog.moc"