← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2789: Apply ADL searches in partial file lists

 

------------------------------------------------------------
revno: 2789
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Sat 2012-01-07 17:20:01 +0100
message:
  Apply ADL searches in partial file lists
modified:
  changelog.txt
  dcpp/ADLSearch.cpp
  dcpp/ADLSearch.h
  dcpp/DirectoryListing.cpp
  win32/DirectoryListingFrame.cpp
  win32/DirectoryListingFrame.h


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'changelog.txt'
--- changelog.txt	2012-01-06 23:14:39 +0000
+++ changelog.txt	2012-01-07 16:20:01 +0000
@@ -1,4 +1,5 @@
-* Save and restore file lists dl'd with "browse file list" (poy)
+* Save and restore partial file lists (poy)
+* Apply ADL searches in partial file lists (poy)
 
 -- 0.790 2011-12-29 --
 * Fav users frame becomes users frame and shows all users

=== modified file 'dcpp/ADLSearch.cpp'
--- dcpp/ADLSearch.cpp	2011-12-22 22:14:45 +0000
+++ dcpp/ADLSearch.cpp	2012-01-07 16:20:01 +0000
@@ -368,12 +368,11 @@
 	}
 }
 
-void ADLSearchManager::prepareDestinationDirectories(DestDirList& destDirVector, DirectoryListing::Directory* root, ParamMap& params) {
+void ADLSearchManager::prepareDestinationDirectories(DestDirList& destDirs, DirectoryListing::Directory* root, ParamMap& params) {
 	// Load default destination directory (index = 0)
-	destDirVector.clear();
-	auto id = destDirVector.insert(destDirVector.end(), DestDir());
-	id->name = "ADLSearch";
-	id->dir  = new DirectoryListing::Directory(root, "<<<" + id->name + ">>>", true, true);
+	destDirs.clear();
+	DestDir dir = { "ADLSearch", new DirectoryListing::Directory(root, "<<<ADLSearch>>>", true, true) };
+	destDirs.push_back(std::move(dir));
 
 	// Scan all loaded searches
 	for(auto is = collection.begin(); is != collection.end(); ++is) {
@@ -387,7 +386,7 @@
 		// Check if exists
 		bool isNew = true;
 		long ddIndex = 0;
-		for(id = destDirVector.begin(); id != destDirVector.end(); ++id, ++ddIndex) {
+		for(auto id = destDirs.cbegin(); id != destDirs.cend(); ++id, ++ddIndex) {
 			if(Util::stricmp(is->destDir.c_str(), id->name.c_str()) == 0) {
 				// Already exists, reuse index
 				is->ddIndex = ddIndex;
@@ -398,9 +397,8 @@
 
 		if(isNew) {
 			// Add new destination directory
-			id = destDirVector.insert(destDirVector.end(), DestDir());
-			id->name = is->destDir;
-			id->dir  = new DirectoryListing::Directory(root, "<<<" + id->name + ">>>", true, true);
+			DestDir dir = { is->destDir, new DirectoryListing::Directory(root, "<<<" + is->destDir + ">>>", true, true) };
+			destDirs.push_back(std::move(dir));
 			is->ddIndex = ddIndex;
 		}
 	}
@@ -410,17 +408,17 @@
 	}
 }
 
-void ADLSearchManager::finalizeDestinationDirectories(DestDirList& destDirVector, DirectoryListing::Directory* root) {
+void ADLSearchManager::finalizeDestinationDirectories(DestDirList& destDirs, DirectoryListing::Directory* root) {
 	string szDiscard("<<<" + string(_("Discard")) + ">>>");
 
 	// Add non-empty destination directories to the top level
-	for(auto id = destDirVector.begin(); id != destDirVector.end(); ++id) {
-		if(id->dir->files.size() == 0 && id->dir->directories.size() == 0) {
-			delete (id->dir);
-		} else if(Util::stricmp(id->dir->getName(), szDiscard) == 0) {
-			delete (id->dir);
+	for(auto i = destDirs.begin(); i != destDirs.end(); ++i) {
+		if(i->dir->files.empty() && i->dir->directories.empty()) {
+			delete i->dir;
+		} else if(Util::stricmp(i->dir->getName(), szDiscard) == 0) {
+			delete i->dir;
 		} else {
-			root->directories.push_back(id->dir);
+			root->directories.push_back(i->dir);
 		}
 	}
 }
@@ -432,14 +430,16 @@
 
 	setUser(aDirList.getUser());
 
+	auto root = aDirList.getRoot();
+
 	DestDirList destDirs;
 	prepareDestinationDirectories(destDirs, aDirList.getRoot(), params);
 	setBreakOnFirst(BOOLSETTING(ADLS_BREAK_ON_FIRST));
 
-	string path(aDirList.getRoot()->getName());
-	matchRecurse(destDirs, aDirList.getRoot(), path);
+	string path(root->getName());
+	matchRecurse(destDirs, root, path);
 
-	finalizeDestinationDirectories(destDirs, aDirList.getRoot());
+	finalizeDestinationDirectories(destDirs, root);
 }
 
 void ADLSearchManager::matchRecurse(DestDirList &aDestList, DirectoryListing::Directory* aDir, string &aPath) {

=== modified file 'dcpp/ADLSearch.h'
--- dcpp/ADLSearch.h	2011-12-23 21:15:27 +0000
+++ dcpp/ADLSearch.h	2012-01-07 16:20:01 +0000
@@ -111,7 +111,6 @@
 		DirectoryListing::Directory* dir;
 		DirectoryListing::Directory* subdir;
 		bool fileAdded;
-		DestDir() : name(""), dir(NULL), subdir(NULL) {}
 	};
 	typedef vector<DestDir> DestDirList;
 
@@ -131,7 +130,7 @@
 	GETSET(HintedUser, user, User)
 
 	// @remarks Used to add ADLSearch directories to an existing DirectoryListing
-	void matchListing(DirectoryListing& /*aDirList*/) noexcept;
+	void matchListing(DirectoryListing& aDirList) noexcept;
 
 private:
 	// @internal
@@ -144,9 +143,9 @@
 	void stepUpDirectory(DestDirList& destDirVector);
 
 	// Prepare destination directory indexing
-	void prepareDestinationDirectories(DestDirList& destDirVector, DirectoryListing::Directory* root, ParamMap& params);
+	void prepareDestinationDirectories(DestDirList& destDirs, DirectoryListing::Directory* root, ParamMap& params);
 	// Finalize destination directories
-	void finalizeDestinationDirectories(DestDirList& destDirVector, DirectoryListing::Directory* root);
+	void finalizeDestinationDirectories(DestDirList& destDirs, DirectoryListing::Directory* root);
 
 	static string getConfigFile();
 };

=== modified file 'dcpp/DirectoryListing.cpp'
--- dcpp/DirectoryListing.cpp	2012-01-06 23:14:39 +0000
+++ dcpp/DirectoryListing.cpp	2012-01-07 16:20:01 +0000
@@ -40,7 +40,7 @@
 DirectoryListing::DirectoryListing(const HintedUser& aUser) :
 user(aUser),
 abort(false),
-root(new Directory(NULL, Util::emptyString, false, false))
+root(new Directory(nullptr, Util::emptyString, false, false))
 {
 }
 
@@ -189,7 +189,7 @@
 				throw SimpleXMLException(_("Directory missing name attribute"));
 			}
 			bool incomp = getAttrib(attribs, sIncomplete, 1) == "1";
-			DirectoryListing::Directory* d = NULL;
+			DirectoryListing::Directory* d = nullptr;
 			if(updating) {
 				for(auto i = cur->directories.begin(); i != cur->directories.end(); ++i) {
 					/// @todo comparisons should be case-insensitive but it takes too long - add a cache
@@ -201,7 +201,7 @@
 					}
 				}
 			}
-			if(d == NULL) {
+			if(!d) {
 				d = new DirectoryListing::Directory(cur, n, false, !incomp);
 				cur->directories.push_back(d);
 			}
@@ -223,14 +223,14 @@
 
 		StringList sl = StringTokenizer<string>(base.substr(1), '/').getTokens();
 		for(auto i = sl.begin(); i != sl.end(); ++i) {
-			DirectoryListing::Directory* d = NULL;
+			DirectoryListing::Directory* d = nullptr;
 			for(auto j = cur->directories.begin(); j != cur->directories.end(); ++j) {
 				if((*j)->getName() == *i) {
 					d = *j;
 					break;
 				}
 			}
-			if(d == NULL) {
+			if(!d) {
 				d = new DirectoryListing::Directory(cur, *i, false, false);
 				cur->directories.push_back(d);
 			}
@@ -259,11 +259,19 @@
 
 void DirectoryListing::save(const string& path) const {
 	dcassert(!base.empty());
+
 	dcpp::File stream(path, dcpp::File::WRITE, dcpp::File::CREATE | dcpp::File::TRUNCATE);
 	stream.write(SimpleXML::utf8Header);
+
 	string indent("\t"), tmp;
-	stream.write("<FileListing Version=\"1\" CID=\"" + user.user->getCID().toBase32() + "\" Base=\"" + SimpleXML::escape(base, tmp, true) + "\" Generator=\"" APPNAME " " VERSIONSTRING "\">\r\n");
-	auto start = find(Util::toNmdcFile(base), root);
+
+	stream.write(LIT("<FileListing Version=\"1\" CID=\""));
+	stream.write(user.user->getCID().toBase32());
+	stream.write(LIT("\" Base=\""));
+	stream.write(SimpleXML::escape(base, tmp, true));
+	stream.write(LIT("\" Generator=\"" APPNAME " " VERSIONSTRING "\">\r\n"));
+
+	auto start = (base == "/") ? root : find(Util::toNmdcFile(base), root);
 	if(start) {
 		std::for_each(start->directories.cbegin(), start->directories.cend(), [&](Directory* d) {
 			d->save(stream, indent, tmp);
@@ -272,7 +280,8 @@
 			f->save(stream, indent, tmp);
 		});
 	}
-	stream.write("</FileListing>");
+
+	stream.write(LIT("</FileListing>"));
 }
 
 void DirectoryListing::Directory::save(OutputStream& stream, string& indent, string& tmp) const {
@@ -379,7 +388,7 @@
 	dcassert(aDir.size() > 2);
 	dcassert(aDir[aDir.size() - 1] == '\\'); // This should not be PATH_SEPARATOR
 	Directory* d = find(aDir, getRoot());
-	if(d != NULL)
+	if(d)
 		download(d, aTarget, highPrio);
 }
 
@@ -404,7 +413,7 @@
 		else
 			return find(aName.substr(end + 1), *i);
 	}
-	return NULL;
+	return nullptr;
 }
 
 struct HashContained {

=== modified file 'win32/DirectoryListingFrame.cpp'
--- win32/DirectoryListingFrame.cpp	2012-01-06 23:14:39 +0000
+++ win32/DirectoryListingFrame.cpp	2012-01-07 16:20:01 +0000
@@ -486,9 +486,35 @@
 		path = QueueManager::getInstance()->getListPath(dl->getUser()) + ".xml";
 		auto base = dl->updateXML(txt);
 		dl->save(path);
+
+		// remove previous ADLS matches.
+		for(auto dir = dirs->getChild(treeRoot); dir; dir = dirs->getNextSibling(dir)) {
+			auto d = dirs->getData(dir)->dir;
+			if(d->getAdls()) {
+				HTREEITEM child;
+				while(child = dirs->getChild(dir)) {
+					dirs->erase(child);
+				}
+				dirs->erase(dir);
+				auto& pdirs = d->getParent()->directories;
+				pdirs.erase(std::remove(pdirs.begin(), pdirs.end(), d), pdirs.end());
+				delete d;
+			}
+		}
+		ADLSearchManager::getInstance()->matchListing(*dl);
+
 		loaded = true;
 		addRecent();
+
 		refreshTree(Text::toT(Util::toNmdcFile(base)));
+		std::for_each(dl->getRoot()->directories.cbegin(), dl->getRoot()->directories.cend(),
+			[this](DirectoryListing::Directory* d)
+		{
+			if(d->getAdls()) {
+				addDir(d, treeRoot);
+			}
+		});
+
 	} catch(const Exception& e) {
 		error = Text::toT(e.getError());
 		updateTitle();
@@ -588,20 +614,19 @@
 
 void DirectoryListingFrame::refreshTree(const tstring& root) {
 	HoldRedraw hold(dirs);
-	HTREEITEM ht = findItem(treeRoot, root);
-	if(ht == NULL) {
+	auto ht = findItem(treeRoot, root);
+	if(!ht) {
 		ht = treeRoot;
 	}
 
-	DirectoryListing::Directory* d = dirs->getData(ht)->dir;
-
-	HTREEITEM next = NULL;
-	while((next = dirs->getChild(ht)) != NULL) {
-		dirs->erase(next);
+	auto d = dirs->getData(ht)->dir;
+	HTREEITEM child;
+	while(child = dirs->getChild(ht)) {
+		dirs->erase(child);
 	}
-	updateTree(d, ht);
+	updateDir(d, ht);
 
-	dirs->setSelected(NULL);
+	dirs->setSelected(nullptr);
 	selectItem(root);
 
 	dirs->expand(treeRoot);
@@ -969,12 +994,16 @@
 	}
 }
 
-void DirectoryListingFrame::updateTree(DirectoryListing::Directory* aTree, HTREEITEM aParent) {
-	for(auto i = aTree->directories.begin(); i != aTree->directories.end(); ++i) {
-		HTREEITEM ht = dirs->insert(aParent, new ItemInfo(*i));
-		if((*i)->getAdls())
-			dirs->setItemState(ht, TVIS_BOLD, TVIS_BOLD);
-		updateTree(*i, ht);
+void DirectoryListingFrame::addDir(DirectoryListing::Directory* d, HTREEITEM parent) {
+	auto item = dirs->insert(parent, new ItemInfo(d));
+	if(d->getAdls())
+		dirs->setItemState(item, TVIS_BOLD, TVIS_BOLD);
+	updateDir(d, item);
+}
+
+void DirectoryListingFrame::updateDir(DirectoryListing::Directory* d, HTREEITEM parent) {
+	for(auto i = d->directories.begin(); i != d->directories.end(); ++i) {
+		addDir(*i, parent);
 	}
 }
 

=== modified file 'win32/DirectoryListingFrame.h'
--- win32/DirectoryListingFrame.h	2011-12-27 22:02:43 +0000
+++ win32/DirectoryListingFrame.h	2012-01-07 16:20:01 +0000
@@ -256,7 +256,8 @@
 	bool handleXMouseUp(const dwt::MouseEvent& mouseEvent);
 
 	void changeDir(DirectoryListing::Directory* d);
-	void updateTree(DirectoryListing::Directory* tree, HTREEITEM treeItem);
+	void addDir(DirectoryListing::Directory* d, HTREEITEM parent);
+	void updateDir(DirectoryListing::Directory* d, HTREEITEM parent);
 	HTREEITEM findItem(HTREEITEM ht, const tstring& name);
 	void selectItem(const tstring& name);
 	void updateTitle();