diff --git a/knode/kngroupbrowser.cpp b/knode/kngroupbrowser.cpp index e77f7687..bafa065c 100644 --- a/knode/kngroupbrowser.cpp +++ b/knode/kngroupbrowser.cpp @@ -41,10 +41,8 @@ KNGroupBrowser::KNGroupBrowser(TQWidget *parent, const TQString &caption, KNNntp { refilterTimer = new TQTimer(); - allList=new TQPtrList; - allList->setAutoDelete(true); - matchList=new TQPtrList; - matchList->setAutoDelete(false); + allList = new std::list(); + matchList = new std::list(); //create Widgets page=new TQWidget(this); @@ -148,7 +146,16 @@ KNGroupBrowser::~KNGroupBrowser() knGlobals.netAccess()->stopJobsNntp(KNJobData::JTCheckNewGroups); delete matchList; + + for (KNGroupInfo *g : *allList) + { + if (g) + { + delete g; + } + } delete allList; + delete refilterTimer; } @@ -159,6 +166,13 @@ void KNGroupBrowser::slotReceiveList(KNGroupListData* d) enableButton(User2,true); if (d) { // d==0 if something has gone wrong... + for (KNGroupInfo *g : *allList) + { + if (g) + { + delete g; + } + } delete allList; allList = d->extractList(); incrementalFilter=false; @@ -206,8 +220,8 @@ void KNGroupBrowser::createListItems(TQListViewItem *parent) } } - for(KNGroupInfo *gn=matchList->first(); gn; gn=matchList->next()) { - + for (KNGroupInfo *gn : *matchList) + { if(!prefix.isEmpty() && !gn->name.startsWith(prefix)) if(!compare.isNull()) break; @@ -321,14 +335,14 @@ void KNGroupBrowser::slotFilter(const TQString &txt) bool doIncrementalUpdate = (!isRegexp && incrementalFilter && (filtertxt.left(lastFilter.length())==lastFilter)); if (doIncrementalUpdate) { - TQPtrList *tempList = new TQPtrList(); - tempList->setAutoDelete(false); + std::list *tempList = new std::list(); - for(KNGroupInfo *g=matchList->first(); g; g=matchList->next()) { + for (KNGroupInfo *g : *matchList) + { if ((notCheckSub||g->subscribed)&& (notCheckNew||g->newGroup)&& (notCheckStr||(g->name.find(filtertxt)!=-1))) - tempList->append(g); + tempList->push_back(g); } delete matchList; @@ -336,18 +350,20 @@ void KNGroupBrowser::slotFilter(const TQString &txt) } else { matchList->clear(); - for(KNGroupInfo *g=allList->first(); g; g=allList->next()) { + for (KNGroupInfo *g : *allList) + { if ((notCheckSub||g->subscribed)&& (notCheckNew||g->newGroup)&& (notCheckStr||(isRegexp? (reg.search(g->name,0) != -1):(g->name.find(filtertxt)!=-1)))) - matchList->append(g); + matchList->push_back(g); } } groupView->clear(); - if((matchList->count() < MIN_FOR_TREE) || noTreeCB->isChecked()) { - for(KNGroupInfo *g=matchList->first(); g; g=matchList->next()) { + if((matchList->size() < MIN_FOR_TREE) || noTreeCB->isChecked()) { + for (KNGroupInfo *g : *matchList) + { cit=new CheckItem(groupView, *g, this); updateItemState(cit); } @@ -358,7 +374,7 @@ void KNGroupBrowser::slotFilter(const TQString &txt) lastFilter = filtertxt; incrementalFilter = !isRegexp; - leftLabel->setText(i18n("Groups on %1: (%2 displayed)").arg(a_ccount->name()).arg(matchList->count())); + leftLabel->setText(i18n("Groups on %1: (%2 displayed)").arg(a_ccount->name()).arg(matchList->size())); arrowBtn1->setEnabled(false); arrowBtn2->setEnabled(false); diff --git a/knode/kngroupbrowser.h b/knode/kngroupbrowser.h index c7391b3a..d787fb4a 100644 --- a/knode/kngroupbrowser.h +++ b/knode/kngroupbrowser.h @@ -98,7 +98,7 @@ class KNGroupBrowser : public KDialogBase { bool incrementalFilter; KNNntpAccount *a_ccount; - TQPtrList *allList, *matchList; + std::list *allList, *matchList; protected slots: void slotLoadList(); diff --git a/knode/kngroupdialog.cpp b/knode/kngroupdialog.cpp index f8922e07..fc3353c0 100644 --- a/knode/kngroupdialog.cpp +++ b/knode/kngroupdialog.cpp @@ -121,18 +121,17 @@ void KNGroupDialog::updateItemState(CheckItem *it) -void KNGroupDialog::toSubscribe(TQPtrList *l) +void KNGroupDialog::toSubscribe(std::list *l) { KNGroupInfo *info; l->clear(); - l->setAutoDelete(true); bool moderated=false; TQListViewItemIterator it(subView); for(; it.current(); ++it) { info = new KNGroupInfo(); *info = ((static_cast(it.current()))->info); - l->append(info); + l->push_back(info); if (info->status==KNGroup::moderated) moderated=true; } diff --git a/knode/kngroupdialog.h b/knode/kngroupdialog.h index 49a3971d..6ac45eb9 100644 --- a/knode/kngroupdialog.h +++ b/knode/kngroupdialog.h @@ -29,7 +29,7 @@ class KNGroupDialog : public KNGroupBrowser { KNGroupDialog(TQWidget *parent, KNNntpAccount *a); ~KNGroupDialog(); - void toSubscribe(TQPtrList *l); + void toSubscribe(std::list *l); void toUnsubscribe(TQStringList *l); protected: diff --git a/knode/kngroupmanager.cpp b/knode/kngroupmanager.cpp index 188a8e1b..d2d21762 100644 --- a/knode/kngroupmanager.cpp +++ b/knode/kngroupmanager.cpp @@ -12,6 +12,7 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US */ +#include #include #include #include @@ -74,6 +75,10 @@ bool KNGroupInfo::operator< (const KNGroupInfo &gi2) return (name < gi2.name); } +bool KNGroupInfo::PtrCompFn(KNGroupInfo *a, KNGroupInfo *b) +{ + return *a < *b; +} //=============================================================================== @@ -81,15 +86,24 @@ bool KNGroupInfo::operator< (const KNGroupInfo &gi2) KNGroupListData::KNGroupListData() : codecForDescriptions(0) { - groups = new TQPtrList; - groups->setAutoDelete(true); + groups = new std::list(); } KNGroupListData::~KNGroupListData() { - delete groups; + if (groups) + { + for (KNGroupInfo *g : *groups) + { + if (g) + { + delete g; + } + } + delete groups; + } } @@ -145,7 +159,7 @@ bool KNGroupListData::readIn(KNProtocolClient *client) } else sub = false; - groups->append(new KNGroupInfo(name,description,false,sub,status)); + groups->push_back(new KNGroupInfo(name,description,false,sub,status)); if (timer.elapsed() > 200) { // don't flicker timer.restart(); @@ -169,7 +183,8 @@ bool KNGroupListData::writeOut() TQCString temp; if(f.open(IO_WriteOnly)) { - for (KNGroupInfo *i=groups->first(); i; i=groups->next()) { + for (KNGroupInfo *i : *groups) + { temp = i->name.utf8(); switch (i->status) { case KNGroup::unknown: temp += " u "; @@ -196,27 +211,33 @@ bool KNGroupListData::writeOut() // merge in new groups, we want to preserve the "subscribed"-flag // of the loaded groups and the "new"-flag of the new groups. -void KNGroupListData::merge(TQPtrList* newGroups) +void KNGroupListData::merge(std::list *newGroups) { bool subscribed; - for (KNGroupInfo *i=newGroups->first(); i; i=newGroups->next()) { - if (groups->find(i)>=0) { - subscribed = groups->current()->subscribed; - groups->remove(); // avoid duplicates + for (KNGroupInfo *i : *newGroups) + { + std::list::iterator ngIt = std::find(groups->begin(), groups->end(), i); + + if (ngIt != std::end(*groups)) + { + KNGroupInfo *newGr = *ngIt; + subscribed = newGr->subscribed; + groups->erase(ngIt); // avoid duplicates + delete newGr; } else subscribed = false; - groups->append(new KNGroupInfo(i->name,i->description,true,subscribed,i->status)); + groups->push_back(new KNGroupInfo(i->name,i->description,true,subscribed,i->status)); } - groups->sort(); + groups->sort(KNGroupInfo::PtrCompFn); } -TQPtrList* KNGroupListData::extractList() +std::list* KNGroupListData::extractList() { - TQPtrList* temp = groups; - groups = 0; + std::list* temp = groups; + groups = nullptr; return temp; } @@ -417,10 +438,12 @@ void KNGroupManager::showGroupDialog(KNNntpAccount *a, TQWidget *parent) } } - TQPtrList lst2; + std::list lst2; gDialog->toSubscribe(&lst2); - for(KNGroupInfo *var=lst2.first(); var; var=lst2.next()) { + for (KNGroupInfo *var : lst2) + { subscribeGroup(var, a); + delete var; } } @@ -593,12 +616,14 @@ void KNGroupManager::processJob(KNJobData *j) // update the descriptions of the subscribed groups for ( TQValueList::Iterator it = mGroupList.begin(); it != mGroupList.end(); ++it ) { if ( (*it)->account() == j->account() ) { - for ( KNGroupInfo* inf = d->groups->first(); inf; inf = d->groups->next() ) + for (KNGroupInfo *inf : *d->groups) + { if ( inf->name == (*it)->groupname() ) { (*it)->setDescription( inf->description ); (*it)->setStatus( inf->status ); break; } + } } } } diff --git a/knode/kngroupmanager.h b/knode/kngroupmanager.h index f713a90b..72104097 100644 --- a/knode/kngroupmanager.h +++ b/knode/kngroupmanager.h @@ -15,8 +15,9 @@ #ifndef KNGROUPMANAGER_H #define KNGROUPMANAGER_H +#include + #include -#include #include "knjobdata.h" #include "kngroup.h" @@ -48,6 +49,8 @@ class KNGroupInfo { bool operator== (const KNGroupInfo &gi2); bool operator< (const KNGroupInfo &gi2); + + static bool PtrCompFn(KNGroupInfo *a, KNGroupInfo *b); }; @@ -59,13 +62,13 @@ class KNGroupListData : public KNJobItem { bool readIn(KNProtocolClient *client=0); bool writeOut(); - void merge(TQPtrList* newGroups); + void merge(std::list *newGroups); - TQPtrList* extractList(); + std::list* extractList(); TQStringList subscribed; TQString path; - TQPtrList *groups; + std::list *groups; TQDate fetchSince; bool getDescriptions; TQTextCodec *codecForDescriptions; diff --git a/knode/knnntpclient.cpp b/knode/knnntpclient.cpp index ceaeafe0..0755dc96 100644 --- a/knode/knnntpclient.cpp +++ b/knode/knnntpclient.cpp @@ -15,6 +15,9 @@ */ #include +#include +#include + #include #include #include @@ -132,7 +135,7 @@ void KNNntpClient::doFetchGroups() default : status = KNGroup::unknown; } - target->groups->append(new KNGroupInfo(name,TQString(),false,subscribed,status)); + target->groups->push_back(new KNGroupInfo(name,TQString(),false,subscribed,status)); } doneLines++; } @@ -140,15 +143,18 @@ void KNNntpClient::doFetchGroups() if (!job->success() || job->canceled()) return; // stopped... - TQPtrVector tempVector; - target->groups->toVector(&tempVector); - tempVector.sort(); + std::vector tempVector; + for (KNGroupInfo *g : *target->groups) + { + tempVector.push_back(g); + } + std::sort(tempVector.begin(), tempVector.end(), KNGroupInfo::PtrCompFn); if (target->getDescriptions) { errorPrefix = i18n("The group descriptions could not be retrieved.\nThe following error occurred:\n"); progressValue = 100; doneLines = 0; - predictedLines = target->groups->count(); + predictedLines = target->groups->size(); sendSignal(TSdownloadDesc); sendSignal(TSprogressUpdate); @@ -189,8 +195,11 @@ void KNNntpClient::doFetchGroups() description = TQString::fromLocal8Bit(s); info.name = name; - if ((pos=tempVector.bsearch(&info))!=-1) - tempVector[pos]->description = description; + if (std::binary_search(tempVector.begin(), tempVector.end(), &info, KNGroupInfo::PtrCompFn)) + { + KNGroupInfo *posGroup = *std::lower_bound(tempVector.begin(), tempVector.end(), &info, KNGroupInfo::PtrCompFn); + posGroup->description = description; + } } doneLines++; } @@ -200,9 +209,11 @@ void KNNntpClient::doFetchGroups() return; // stopped... } - target->groups->setAutoDelete(false); - tempVector.toList(target->groups); - target->groups->setAutoDelete(true); + target->groups->clear(); + for (KNGroupInfo *g : tempVector) + { + target->groups->push_back(g); + } sendSignal(TSwriteGrouplist); if (!target->writeOut()) @@ -229,8 +240,7 @@ void KNNntpClient::doCheckNewGroups() char *s, *line; TQString name; KNGroup::Status status; - TQPtrList tmpList; - tmpList.setAutoDelete(true); + std::list tmpList; while (getNextLine()) { line = getCurrentLine(); @@ -261,19 +271,23 @@ void KNNntpClient::doCheckNewGroups() default : status = KNGroup::unknown; } - tmpList.append(new KNGroupInfo(name,TQString(),true,false,status)); + tmpList.push_back(new KNGroupInfo(name,TQString(),true,false,status)); } doneLines++; } + bool statusOk = true; + if (!job->success() || job->canceled()) - return; // stopped... + { + statusOk = false; // stopped... + } - if (target->getDescriptions) { + if (statusOk && target->getDescriptions) { errorPrefix = i18n("The group descriptions could not be retrieved.\nThe following error occurred:\n"); progressValue = 100; doneLines = 0; - predictedLines = tmpList.count()*3; + predictedLines = tmpList.size()*3; sendSignal(TSdownloadDesc); sendSignal(TSprogressUpdate); @@ -283,14 +297,21 @@ void KNNntpClient::doCheckNewGroups() char *s; int rep; - for (KNGroupInfo *group=tmpList.first(); group; group=tmpList.next()) { + for (KNGroupInfo *group : tmpList) + { if (!sendCommand(cmd+group->name.utf8(),rep)) - return; + { + statusOk = false; + break; + } if (rep != 215) // 215 informations follows break; desList.clear(); if (!getMsg(desList)) - return; + { + statusOk = false; + break; + } if (desList.count()>0) { // group has a description s = desList.first(); @@ -310,17 +331,26 @@ void KNNntpClient::doCheckNewGroups() } } - sendSignal(TSloadGrouplist); + if (statusOk) + { + sendSignal(TSloadGrouplist); - if (!target->readIn()) { - job->setErrorString(i18n("Unable to read the group list file")); - return; + if (!target->readIn()) { + job->setErrorString(i18n("Unable to read the group list file")); + } + else + { + target->merge(&tmpList); + sendSignal(TSwriteGrouplist); + if (!target->writeOut()) { + job->setErrorString(i18n("Unable to write the group list file")); + } + } } - target->merge(&tmpList); - sendSignal(TSwriteGrouplist); - if (!target->writeOut()) { - job->setErrorString(i18n("Unable to write the group list file")); - return; + + for (KNGroupInfo *group : tmpList) + { + delete group; } } diff --git a/knode/utilities.h b/knode/utilities.h index 98938875..f20d99e1 100644 --- a/knode/utilities.h +++ b/knode/utilities.h @@ -20,8 +20,6 @@ #include #include -#include -#include class TQWidget; class TQString; @@ -44,7 +42,7 @@ class KNFile : public TQFile { const TQCString& readLine(); const TQCString& readLineWnewLine(); /** searches for the string from the current file position - returns -1 when the string wasn't found. */ + returns -1 when the string wasn't found. */ int findString(const char *s); protected: