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
00030
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035
00036 #include "subscriptiondialog.h"
00037 #include "folderstorage.h"
00038 #include "listjob.h"
00039 #include "imapaccountbase.h"
00040
00041 #include <klocale.h>
00042 #include <kdebug.h>
00043 #include <kmessagebox.h>
00044
00045
00046 namespace KMail {
00047
00048 SubscriptionDialog::SubscriptionDialog( QWidget *parent, const QString &caption,
00049 KAccount *acct, QString startPath )
00050 : KSubscription( parent, caption, acct, User1, QString::null, false ),
00051 mStartPath( startPath ), mSubscribed( false ), mForceSubscriptionEnable( false)
00052 {
00053
00054 hideTreeCheckbox();
00055 hideNewOnlyCheckbox();
00056
00057
00058 connect(this, SIGNAL(okClicked()), SLOT(slotSave()));
00059
00060
00061 connect(this, SIGNAL(user1Clicked()), SLOT(slotLoadFolders()));
00062
00063
00064 slotLoadFolders();
00065 }
00066
00067
00068 void SubscriptionDialog::slotListDirectory( const QStringList& subfolderNames,
00069 const QStringList& subfolderPaths,
00070 const QStringList& subfolderMimeTypes,
00071 const QStringList& subfolderAttributes,
00072 const ImapAccountBase::jobData& jobData )
00073 {
00074 mFolderNames = subfolderNames;
00075 mFolderPaths = subfolderPaths;
00076 mFolderMimeTypes = subfolderMimeTypes;
00077 mFolderAttributes = subfolderAttributes;
00078 mJobData = jobData;
00079
00080 mCount = 0;
00081
00082 createItems();
00083 }
00084
00085
00086 void SubscriptionDialog::createItems()
00087 {
00088 bool onlySubscribed = mJobData.onlySubscribed;
00089
00090 GroupItem *parent = 0;
00091 uint done = 0;
00092
00093
00094
00095 for (uint i = mCount; i < mFolderNames.count(); ++i)
00096 {
00097
00098 if (done == 1000)
00099 {
00100 emit listChanged();
00101 QTimer::singleShot(0, this, SLOT(createItems()));
00102 return;
00103 }
00104 ++mCount;
00105 ++done;
00106 GroupItem *item = 0;
00107 if (!onlySubscribed && mFolderPaths.size() > 0)
00108 {
00109
00110 GroupItem *oldItem = 0;
00111 QString parentPath;
00112 findParentItem( mFolderNames[i], mFolderPaths[i], parentPath, &parent, &oldItem );
00113
00114 if (!parent && parentPath != "/")
00115 {
00116
00117
00118
00119 QStringList folders = QStringList::split(mDelimiter, parentPath);
00120 uint i = 0;
00121 for ( QStringList::Iterator it = folders.begin(); it != folders.end(); ++it )
00122 {
00123 QString name = *it;
00124 if (name.startsWith("/"))
00125 name = name.right(name.length()-1);
00126 if (name.endsWith("/"))
00127 name.truncate(name.length()-1);
00128 KGroupInfo info(name);
00129 info.subscribed = false;
00130
00131 QStringList tmpPath;
00132 for ( uint j = 0; j <= i; ++j )
00133 tmpPath << folders[j];
00134 QString path = tmpPath.join(mDelimiter);
00135 if (!path.startsWith("/"))
00136 path = "/" + path;
00137 if (!path.endsWith("/"))
00138 path = path + "/";
00139 info.path = path;
00140 item = 0;
00141 if (folders.count() > 1)
00142 {
00143
00144
00145 item = mItemDict[path];
00146 }
00147
00148 if (!item)
00149 {
00150 if (parent)
00151 item = new GroupItem(parent, info, this, false);
00152 else
00153 item = new GroupItem(folderTree(), info, this, false);
00154 mItemDict.insert(info.path, item);
00155 }
00156
00157 parent = item;
00158 ++i;
00159 }
00160 }
00161
00162 KGroupInfo info(mFolderNames[i]);
00163 info.path = mFolderPaths[i];
00164
00165 if ( info.path == "/INBOX/" )
00166 info.name = i18n("inbox");
00167
00168
00169 bool checkable = ( mFolderMimeTypes[i] == "inode/directory" ) ? false : true;
00170
00171 if (parent)
00172 item = new GroupItem(parent, info, this, checkable);
00173 else
00174 item = new GroupItem(folderTree(), info, this, checkable);
00175
00176 if (oldItem)
00177 mItemDict.remove(info.path);
00178
00179 mItemDict.insert(info.path, item);
00180 if (oldItem)
00181 {
00182
00183 QPtrList<QListViewItem> itemsToMove;
00184 QListViewItem * myChild = oldItem->firstChild();
00185 while (myChild)
00186 {
00187 itemsToMove.append(myChild);
00188 myChild = myChild->nextSibling();
00189 }
00190 QPtrListIterator<QListViewItem> it( itemsToMove );
00191 QListViewItem *cur;
00192 while ((cur = it.current()))
00193 {
00194 oldItem->takeItem(cur);
00195 item->insertItem(cur);
00196 if ( cur->isSelected() )
00197 folderTree()->ensureItemVisible( cur );
00198 ++it;
00199 }
00200 delete oldItem;
00201 itemsToMove.clear();
00202 }
00203
00204 if ( mFolderPaths[i] == mStartPath )
00205 {
00206 item->setSelected( true );
00207 folderTree()->ensureItemVisible( item );
00208 }
00209
00210 } else if (onlySubscribed)
00211 {
00212
00213 if ( mItemDict[mFolderPaths[i]] )
00214 {
00215 GroupItem* item = mItemDict[mFolderPaths[i]];
00216 item->setOn( true );
00217 }
00218 }
00219 }
00220 processNext();
00221 }
00222
00223
00224 void SubscriptionDialog::findParentItem( QString &name, QString &path, QString &parentPath,
00225 GroupItem **parent, GroupItem **oldItem )
00226 {
00227
00228 int start = path.length() - (name.length()+2);
00229 int length = name.length()+1;
00230 if (start < 0) start = 0;
00231 parentPath = path;
00232 parentPath.remove(start, length);
00233
00234
00235 *parent = mItemDict[parentPath];
00236
00237
00238 *oldItem = mItemDict[path];
00239 }
00240
00241
00242 void SubscriptionDialog::slotSave()
00243 {
00244 KMail::ImapAccountBase *account = static_cast<KMail::ImapAccountBase*>(mAcct);
00245 if( !account->onlySubscribedFolders() ) {
00246 int result = KMessageBox::questionYesNoCancel( this,
00247 i18n("Currently subscriptions are not used for server %1\ndo you want to enable subscriptions?")
00248 .arg( account->name() ),
00249 i18n("Enable Subscriptions?"), i18n("Enable"), i18n("Do Not Enable"));
00250 switch(result) {
00251 case KMessageBox::Yes:
00252 mForceSubscriptionEnable = true;
00253 break;
00254 case KMessageBox::No:
00255 break;
00256 case KMessageBox::Cancel:
00257 cancel();
00258 }
00259 }
00260
00261
00262 QListViewItemIterator it(subView);
00263 for ( ; it.current(); ++it)
00264 {
00265 account->changeSubscription(true,
00266 static_cast<GroupItem*>(it.current())->info().path);
00267 }
00268
00269
00270 QListViewItemIterator it2(unsubView);
00271 for ( ; it2.current(); ++it2)
00272 {
00273 account->changeSubscription(false,
00274 static_cast<GroupItem*>(it2.current())->info().path);
00275 }
00276
00277 if ( mForceSubscriptionEnable ) {
00278 account->setOnlySubscribedFolders(true);
00279 }
00280 }
00281
00282
00283 void SubscriptionDialog::slotLoadFolders()
00284 {
00285 ImapAccountBase* ai = static_cast<ImapAccountBase*>(account());
00286
00287 if ( ai->makeConnection() == ImapAccountBase::Error )
00288 {
00289 kdWarning(5006) << "SubscriptionDialog - got no connection" << endl;
00290 return;
00291 } else if ( ai->makeConnection() == ImapAccountBase::Connecting )
00292 {
00293
00294 kdDebug(5006) << "SubscriptionDialog - waiting for connection" << endl;
00295 connect( ai, SIGNAL( connectionResult(int, const QString&) ),
00296 this, SLOT( slotConnectionResult(int, const QString&) ) );
00297 return;
00298 }
00299
00300 KSubscription::slotLoadFolders();
00301 mItemDict.clear();
00302 mSubscribed = false;
00303 mLoading = true;
00304
00305 initPrefixList();
00306
00307 processNext();
00308 }
00309
00310
00311 void SubscriptionDialog::processNext()
00312 {
00313 if ( mPrefixList.isEmpty() )
00314 {
00315 if ( !mSubscribed )
00316 {
00317 mSubscribed = true;
00318 initPrefixList();
00319 if ( mPrefixList.isEmpty() )
00320 {
00321
00322 slotLoadingComplete();
00323 return;
00324 }
00325 } else {
00326 slotLoadingComplete();
00327 return;
00328 }
00329 }
00330 ImapAccountBase* ai = static_cast<ImapAccountBase*>(account());
00331 ImapAccountBase::ListType type = ( mSubscribed ?
00332 ImapAccountBase::ListSubscribedNoCheck : ImapAccountBase::List );
00333
00334 bool completeListing = true;
00335 mCurrentNamespace = mPrefixList.first();
00336 mDelimiter = ai->delimiterForNamespace( mCurrentNamespace );
00337 mPrefixList.pop_front();
00338 if ( mCurrentNamespace == "/INBOX/" )
00339 {
00340 type = mSubscribed ?
00341 ImapAccountBase::ListFolderOnlySubscribed : ImapAccountBase::ListFolderOnly;
00342 completeListing = false;
00343 }
00344
00345
00346 ListJob* job = new ListJob( ai, type, 0, ai->addPathToNamespace( mCurrentNamespace ), completeListing );
00347 connect( job, SIGNAL(receivedFolders(const QStringList&, const QStringList&,
00348 const QStringList&, const QStringList&, const ImapAccountBase::jobData&)),
00349 this, SLOT(slotListDirectory(const QStringList&, const QStringList&,
00350 const QStringList&, const QStringList&, const ImapAccountBase::jobData&)));
00351 job->start();
00352 }
00353
00354
00355 void SubscriptionDialog::initPrefixList()
00356 {
00357 ImapAccountBase* ai = static_cast<ImapAccountBase*>(account());
00358 ImapAccountBase::nsMap map = ai->namespaces();
00359 mPrefixList.clear();
00360
00361 bool hasInbox = false;
00362 const QStringList ns = map[ImapAccountBase::PersonalNS];
00363 for ( QStringList::ConstIterator it = ns.begin(); it != ns.end(); ++it )
00364 {
00365 if ( (*it).isEmpty() )
00366 hasInbox = true;
00367 }
00368 if ( !hasInbox && !ns.isEmpty() )
00369 {
00370
00371
00372 mPrefixList += "/INBOX/";
00373 }
00374
00375 mPrefixList += map[ImapAccountBase::PersonalNS];
00376 mPrefixList += map[ImapAccountBase::OtherUsersNS];
00377 mPrefixList += map[ImapAccountBase::SharedNS];
00378 }
00379
00380 void SubscriptionDialog::slotConnectionResult( int errorCode, const QString& errorMsg )
00381 {
00382 Q_UNUSED( errorMsg );
00383 if ( !errorCode )
00384 slotLoadFolders();
00385 }
00386
00387 void SubscriptionDialog::show()
00388 {
00389 KDialogBase::show();
00390 }
00391
00392 }
00393
00394 #include "subscriptiondialog.moc"