Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

genrelist.cpp 3 KB
Newer Older
1 2 3 4 5
/***************************************************************************
                          genrelist.cpp  -  description
                             -------------------
    begin                : Sun Mar 3 2002
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7
***************************************************************************/
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kdebug.h>

#include "genrelist.h"
#include "genrelistreader.h"

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

27
GenreList::GenreList(bool createIndex) : QValueList<Genre>()
28
{
29
    hasIndex = createIndex;
30 31
}

32
GenreList::GenreList(const QString &file, bool createIndex) : QValueList<Genre>()
33
{
34 35
    hasIndex = createIndex;
    load(file);
36 37 38 39
}

GenreList::~GenreList()
{
40

41 42
}

43
void GenreList::load(const QString &file)
44
{
45 46 47 48 49 50 51 52 53
    GenreListReader *handler = new GenreListReader(this);
    QFile input(file);
    QXmlInputSource source(input);
    QXmlSimpleReader reader;
    reader.setContentHandler(handler);
    reader.parse(source);

    if(hasIndex)
        initializeIndex();
54 55
}

56
QString GenreList::name(int ID3v1)
57
{
58
    if(hasIndex && ID3v1 >= 0 && ID3v1 < int(index.size()))
59
        return(index[ID3v1]);
60 61
    else
        return(QString::null);
62 63
}

64
int GenreList::findIndex(const QString &item)
65
{
66
    // cache the previous search -- since there are a lot of "two in a row"
67 68
    // searchs this should optimize things a little (this is also why this
    // method isn't a "const" method)
69 70 71 72

    static QString lastItem;
    static int lastIndex;

73 74 75
    // a cache hit

    if(!lastItem.isEmpty() && lastItem == item)
76 77 78 79 80 81 82 83 84 85
        return(lastIndex);

    int i = 0;
    for(GenreList::Iterator it = begin(); it != end(); ++it) {
        if(item == (*it)) {
            lastItem = item;
            lastIndex = i;
            return(i);
        }
        i++;
86
    }
87
    return(-1);
88 89
}

90 91 92 93 94 95
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void GenreList::initializeIndex()
{
96
    index.clear();
97
    index.resize(size());
98
    for(GenreList::Iterator it = begin(); it != end(); ++it)
99
        if((*it).getID3v1() >= 0 && (*it).getID3v1() < int(index.size()))
100
            index[(*it).getID3v1()] = QString(*it);
101
}