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