playlistsearch.cpp 5.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/***************************************************************************
                          playlistsearch.cpp
                             -------------------
    begin                : Sun Mar 6 2003
    copyright            : (C) 2003 by Scott Wheeler
    email                : wheeler@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

18 19
#include <kdebug.h>

20
#include "playlistsearch.h"
Scott Wheeler's avatar
Scott Wheeler committed
21 22
#include "playlist.h"
#include "playlistitem.h"
23

24 25 26 27
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

Scott Wheeler's avatar
Scott Wheeler committed
28 29 30 31 32 33
PlaylistSearch::PlaylistSearch() :
    m_mode(MatchAny)
{

}

34 35 36
PlaylistSearch::PlaylistSearch(const PlaylistSearch &search) :
    m_playlists(search.m_playlists),
    m_components(search.m_components),
37 38 39 40
    m_mode(search.m_mode),
    m_items(search.m_items),
    m_matchedItems(search.m_matchedItems),
    m_unmatchedItems(search.m_unmatchedItems)
41 42 43 44
{

}

45 46
PlaylistSearch::PlaylistSearch(const PlaylistList &playlists,
			       const ComponentList &components,
47 48
			       SearchMode mode,
			       bool searchNow) :
49
    m_playlists(playlists),
50
    m_components(components),
51 52
    m_mode(mode)
{
53 54
    if(searchNow)
	search();
55 56
}

57 58 59 60 61 62 63 64 65 66 67 68 69 70
bool PlaylistSearch::isEmpty() const
{
    if(isNull())
	return true;

    ComponentList::ConstIterator it = m_components.begin();
    for(; it != m_components.end(); ++it) {
	if(!(*it).query().isEmpty() || !(*it).pattern().isEmpty())
	    return false;
    }

    return true;
}

71 72
void PlaylistSearch::search()
{
73 74 75
    m_items.clear();
    m_matchedItems.clear();
    m_unmatchedItems.clear();
76 77

    // This really isn't as bad as it looks.  Despite the four nexted loops
78
    // most of the time this will be searching one playlist for one search
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    // component -- possibly for one column.

    // Also there should be some caching of previous searches in here and
    // allowance for appending and removing chars.  If one is added it
    // should only search the current list.  If one is removed it should
    // pop the previous search results off of a stack.

    PlaylistList::Iterator playlistIt = m_playlists.begin();
    for(; playlistIt != m_playlists.end(); ++playlistIt) {

	for(QListViewItemIterator it(*playlistIt); it.current(); ++it) {

	    PlaylistItem *item = static_cast<PlaylistItem *>(*it);

	    m_items.append(item);

	    // set our default
	    bool match = bool(m_mode);

	    ComponentList::Iterator componentIt = m_components.begin();
	    for(; componentIt != m_components.end(); ++componentIt) {

101
		bool componentMatches = (*componentIt).matches(item);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

		if(componentMatches && m_mode == MatchAny) {
		    match = true;
		    break;
		}

		if(!componentMatches && m_mode == MatchAll) {
		    match = false;
		    break;
		}
	    }

	    if(match)
		m_matchedItems.append(item);
	    else
		m_unmatchedItems.append(item);
	}
    }
}

122 123 124 125
////////////////////////////////////////////////////////////////////////////////
// Component public methods
////////////////////////////////////////////////////////////////////////////////

126 127 128 129
PlaylistSearch::Component::Component() :
    m_searchAllVisible(true),
    m_caseSensitive(false)
{
130

131 132 133 134
}

PlaylistSearch::Component::Component(const Component &component) :
    m_query(component.m_query),
135
    m_queryRe(component.m_queryRe),
136 137
    m_columns(component.m_columns),
    m_searchAllVisible(component.m_searchAllVisible),
138 139
    m_caseSensitive(component.m_caseSensitive),
    m_re(component.m_re)
140 141 142 143
{

}

144
PlaylistSearch::Component::Component(const QString &query, bool caseSensitive, const ColumnList &columns) :
145 146
    m_query(query),
    m_columns(columns),
147
    m_searchAllVisible(columns.isEmpty()),
148 149
    m_caseSensitive(caseSensitive),
    m_re(false)
150 151 152 153
{

}

154 155 156 157 158 159 160
PlaylistSearch::Component::Component(const QRegExp &query, const ColumnList& columns) :
    m_queryRe(query),
    m_columns(columns),
    m_searchAllVisible(columns.isEmpty()),
    m_caseSensitive(false),
    m_re(true)
{
Scott Wheeler's avatar
Scott Wheeler committed
161

162 163
}

164 165
bool PlaylistSearch::Component::matches(PlaylistItem *item)
{
166
    if((m_re && m_queryRe.isEmpty()) || (!m_re && m_query.isEmpty()))
167 168 169 170
	return false;

    if(m_columns.isEmpty()) {
	Playlist *p = static_cast<Playlist *>(item->listView());
171
	for(int i = 0; i < p->columns(); i++) {
172
	    if(p->isColumnVisible(i))
173
		m_columns.append(i);
174 175 176 177 178
	}
    }


    for(ColumnList::Iterator it = m_columns.begin(); it != m_columns.end(); ++it) {
179 180 181 182
        int matches = m_re ? item->text(*it).contains(m_queryRe)
                           : item->text(*it).contains(m_query, m_caseSensitive);
        if(matches > 0)
            return true;
183
    }
184

185 186
    return false;
}