search.cpp

Go to the documentation of this file.
00001 /**
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #include <hn/hnprec.h>
00020 #include <hn/search.h>
00021 #include <hn/log.h>
00022 #include <boost/tokenizer.hpp>
00023 
00024 // SearchResult class
00025 // ------------------
00026 SearchResult::SearchResult(const std::string &filename, uint64_t fileSize)
00027 : m_fileName(filename), m_fileSize(fileSize), m_sources(0), m_complete(0) {}
00028 SearchResult::~SearchResult() {}
00029 
00030 // Search class
00031 // ------------
00032 boost::signal<void (SearchPtr)> Search::s_sigQuery;
00033 std::vector<LinkHandler> Search::s_linkHandlers;
00034 
00035 Search::Search()
00036 : m_minSize(0), m_maxSize(std::numeric_limits<uint64_t>::max()),
00037 m_type(FT_UNKNOWN) {}
00038 
00039 Search::Search(const std::string &terms) : m_minSize(0),
00040 m_maxSize(std::numeric_limits<uint64_t>::max()), m_type(FT_UNKNOWN) {
00041         boost::tokenizer<> tok(terms);
00042         for (boost::tokenizer<>::iterator i = tok.begin(); i != tok.end(); ++i){
00043                 m_terms.push_back(*i);
00044         }
00045 }
00046 
00047 void Search::addQueryHandler(SearchHandler handler) {
00048         s_sigQuery.connect(handler);
00049 }
00050 void Search::run() {
00051         s_sigQuery(shared_from_this());
00052 }
00053 void Search::addResult(SearchResultPtr result) {
00054         m_results.push_back(result);
00055 }
00056 void Search::addResultHandler(SearchHandler handler) {
00057         m_sigResult.connect(handler);
00058 }
00059 void Search::notifyResults() {
00060         m_sigResult(shared_from_this());
00061 }
00062 
00063 void Search::addLinkHandler(LinkHandler handler) {
00064         s_linkHandlers.push_back(handler);
00065 }
00066 void Search::downloadLink(const std::string &link) {
00067         std::vector<LinkHandler>::iterator i = s_linkHandlers.begin();
00068         while (i != s_linkHandlers.end()) {
00069                 if ((*i++)(link)) {
00070                         break;
00071                 }
00072         }
00073 }