Beispiel #1
0
void DocumentCatalog::queryChanged()
{
	int newStatus = 0;
	if(query() == "")
	{
		// reset query
		dir = QDir::home();
		currentPath = "";
		queryMatched = 0;
		refreshFolders();
	} else {
		if(query().length() >= minQueryLen())
		{
			QString path = query().lower().remove(0, queryMatched);
			
			int index;
			while((index = path.find('/')) != -1) {
				QString folderQuery = path.left(index);
				QString guess = QString::null;
				
				for(QStringList::Iterator it = folders.begin(); it != folders.end(); ++it) {
					QString folderName = *it;
					if(folderName.lower().startsWith(folderQuery) && (guess.isNull() || folderName.length() < guess.length()))
						guess = folderName;
				}
				
				if(guess == QString::null) {
					path = QString::null;
					break;
				}
				
				if(!dir.cd(guess)) {
					path = QString::null;
					break;
				}
				refreshFolders();
				
				queryMatched += folderQuery.length() + 1;
				currentPath += guess + "/";
				path = path.remove(0, index+1);
			}
			
			Match newBestMatch;
			
			if(path.isNull()) {
				files.clear();
			} else {
				if(!filesListed) {
					refreshFiles();
				}
				if(!path.isEmpty()) {
					if(currentDirDoc != 0) {
						files.removeRef(currentDirDoc);
						currentDirDoc = 0;
					}
					QPtrListIterator<Document> it(files);
					Document *document;
					while((document = it.current()) != 0) {
						++it;
						if(document->name().lower().startsWith(path)) {
							int rank = 100*query().length()/document->text().length();
							if(newBestMatch.isNull() || rank > newBestMatch.rank())
								newBestMatch = Match(document, rank, currentPath.length() + path.length());
						} else {
							files.removeRef(document);
						}
					}
				}
			}
			
			if(currentDirDoc != 0 && path.isEmpty())
				newBestMatch = Match(currentDirDoc, 100, currentPath.length());

			newStatus |= S_Active;
			if(files.count() > 0)
			{
				newStatus |= S_HasResults;
				if(files.count() > 1 || files.at(0)->className() == "Directory")
					newStatus |= S_Multiple;
			} else
				newStatus |= S_NoResults;
				
 			setBestMatch(newBestMatch);
		} else {
			setBestMatch(Match());
		}
	}
	setStatus(newStatus);
}
void Katapult::setQuery(QString _query)
{
    int allStatus=0;
    bestMatch = Match();

    this->_query = _query;
    if(display != 0)
        display->setQuery(_query);
    if(_query == "")
    {
        QDictIterator<KatapultCatalog> it(catalogs);
        KatapultCatalog *catalog;
        while((catalog = it.current()) != 0)
        {
            ++it;
            catalog->setQuery("");
        }
        display->setItem(0);
        display->setAction(0);
        display->setStatus(0);
        display->setSelected(0);
        action = 0;
    } else if(catalogs.count() == 0) {
        allStatus = S_Active | S_NoResults;
        display->setStatus(allStatus);
    } else {
        QDictIterator<KatapultCatalog> it(catalogs);
        KatapultCatalog *catalog;
        int status;

        while((catalog = it.current()) != 0)
        {
            ++it;
            catalog->setQuery(_query);

            status = catalog->status();
            if(status & S_HasResults)
            {
                if(allStatus & S_HasResults)
                    allStatus |= S_Multiple;
                Match match = catalog->bestMatch();
                if(!match.isNull())
                {
                    if(bestMatch.isNull() || bestMatch.rank() < match.rank())
                        bestMatch = match;
                }
            }
            allStatus |= status;
        }
        if(bestMatch.isNull() || bestMatch.rank() == 0)
            bestMatch = Match();
        if(!bestMatch.isNull()) {
            QPtrList<KatapultAction> itemActions = ActionRegistry::self()->actionsForItem(bestMatch.item());
            action = itemActions.at(0);
        }
        if(display != 0)
        {
            display->setItem(bestMatch.item());
            if(bestMatch.isNull()) {
                display->setAction(0);
                display->setSelected(0);
            } else {
                display->setAction(action);
                display->setSelected(bestMatch.matched());
            }
            display->setStatus(allStatus);
        }

        if(!executing && settings->isAutoExecute() && allStatus & S_HasResults && !(allStatus & S_Multiple))
            execute();
    }
    if(display != 0)
    {
        if(!(allStatus & S_HasResults) && allStatus & S_Active) {
            // no results
            switch(settings->noResultsAction()) {
            case KatapultSettings::NR_HideDisplay:
                hideTimer->start(settings->noResultsDelay(), TRUE);
                break;
            case KatapultSettings::NR_ClearQuery:
                clearTimer->start(settings->noResultsDelay(), TRUE);
                break;
            default:
                break;
            }
        }
        display->update();
    }
}