musicbrainzquery.cpp 7.72 KB
Newer Older
Zack Rusin's avatar
Zack Rusin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// musicbrainzquery.cpp
//
// Copyright (C)  2003  Zack Rusin <zack@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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

20
#include <config.h>
Zack Rusin's avatar
Zack Rusin committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34

#if HAVE_MUSICBRAINZ

#include "musicbrainzquery.h"

#include <kprocess.h>
#include <kdebug.h>

#include <qtimer.h>
#include <qvaluelist.h>

#include <string>
#include <vector>

35 36 37 38 39 40 41
MusicBrainzQuery::MusicBrainzQuery(QueryType query, const QStringList &args,
                                   QObject *parent, const char *name)
    : QObject(parent, name),
      MusicBrainz(),
      m_query(query),
      m_arguments(args),
      m_tracks(false)
Zack Rusin's avatar
Zack Rusin committed
42 43 44 45 46 47
{

}

void MusicBrainzQuery::start()
{
48
    if(m_query == File) {
Zack Rusin's avatar
Zack Rusin committed
49
#if HAVE_TRM
50 51 52 53 54 55 56 57 58 59 60
        KProcess *process = new KProcess(this);
        *process << "trm";
        *process << m_arguments.first();

        connect(process, SIGNAL(receivedStdout(KProcess *, char *, int)),
                SLOT(slotTrmData(KProcess *, char *, int)));

        connect(process, SIGNAL(processExited(KProcess *)),
                SLOT(slotTrmGenerationFinished(KProcess *)));

        process->start(KProcess::NotifyOnExit, KProcess::AllOutput);
Zack Rusin's avatar
Zack Rusin committed
61
#else
62
        emit signalDone(TrackList());
Zack Rusin's avatar
Zack Rusin committed
63
#endif
64 65
    }
    else
Zack Rusin's avatar
Zack Rusin committed
66 67 68 69 70 71 72 73 74 75
        QTimer::singleShot(0, this, SLOT(slotQuery()));
}

void MusicBrainzQuery::slotQuery()
{
    std::string queryString;
    std::string resultString;
    std::string extractString;
    AlbumList albums;
    TrackList tracks;
76
    std::vector<std::string> v;
Zack Rusin's avatar
Zack Rusin committed
77 78 79

    queryStrings(queryString, resultString, extractString);

80
    // UseUTF8(false);
Zack Rusin's avatar
Zack Rusin committed
81

82 83 84 85 86
    SetDepth(4);

    QStringList::ConstIterator it = m_arguments.begin();
    for(; it != m_arguments.end(); ++it)
        v.push_back(std::string((*it).latin1()));
Zack Rusin's avatar
Zack Rusin committed
87

88 89
    bool ret = Query(queryString, &v);
    if(ret) {
Zack Rusin's avatar
Zack Rusin committed
90
        int numEntries = DataInt(resultString);
91 92

        for(int i = 1; i <= numEntries; ++i) {
Zack Rusin's avatar
Zack Rusin committed
93
            Select(extractString, i);
94 95

            if(m_tracks) {
Zack Rusin's avatar
Zack Rusin committed
96 97
                Track track(extractTrack(i));
                tracks.append(track);
98 99 100 101
            }
	    else {
                Album album(extractAlbum());
                albums.append(album);
Zack Rusin's avatar
Zack Rusin committed
102 103
            }
        }
104 105
    } 
    else {
Zack Rusin's avatar
Zack Rusin committed
106 107
        std::string error;
        GetQueryError(error);
108
        kdDebug(65432) << "Query failed: " << error.c_str() << endl;
Zack Rusin's avatar
Zack Rusin committed
109 110 111
    }

    if (m_tracks)
112
        emit signalDone(tracks);
Zack Rusin's avatar
Zack Rusin committed
113
    else
114
        emit signalDone(albums);
Zack Rusin's avatar
Zack Rusin committed
115

116
    deleteLater(); // schedule deletion
Zack Rusin's avatar
Zack Rusin committed
117 118
}

119
QString MusicBrainzQuery::dataExtract(const QString &type, int i)
Zack Rusin's avatar
Zack Rusin committed
120
{
121 122 123
    std::string s = Data(type.latin1(), i);

    if(s.empty())
Zack Rusin's avatar
Zack Rusin committed
124
        return QString::null;
125 126

    return s.c_str();
Zack Rusin's avatar
Zack Rusin committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
}

void MusicBrainzQuery::queryStrings(std::string& query, std::string& result, std::string& extraction)
{
    switch(m_query) {
    case CD:
        query      = MBQ_GetCDInfo;
        result     = MBE_GetNumAlbums;
        extraction = MBS_SelectAlbum;
        break;
    case TrackFromTRM:
        query      = MBQ_TrackInfoFromTRMId;
        result     = MBE_GetNumTracks;
        extraction = MBS_SelectTrack;
        m_tracks   = true;
        break;
    case TrackFromID:
        query      = MBQ_QuickTrackInfoFromTrackId;
        result     = MBE_GetNumTracks;
        extraction = MBS_SelectTrack;
        m_tracks   = true;
        break;
    case ArtistByName:
        query      = MBQ_FindArtistByName;
        result     = MBE_GetNumArtists;
        extraction = MBS_SelectArtist ;
        break;
    case AlbumByName:
        query      = MBQ_FindAlbumByName;
        result     = MBE_GetNumAlbums;
        extraction = MBS_SelectAlbum ;
        break;
    case TrackByName:
        query      = MBQ_FindTrackByName;
        result     = MBE_GetNumTracks;
        extraction = MBS_SelectTrack;
        m_tracks   = true;
        break;
    case TRM:
        query      = MBQ_FindDistinctTRMId;
        result     = MBE_GetNumTrmids;
        extraction = MBS_SelectTrack;
        break;
    case ArtistByID:
        query      = MBQ_GetArtistById;
        result     = MBE_GetNumArtists;
        extraction = MBS_SelectArtist ;
        break;
    case AlbumByID:
        query      = MBQ_GetAlbumById;
        result     = MBE_GetNumAlbums;
        extraction = MBS_SelectAlbum;
        break;
    case TrackByID:
        query      = MBQ_GetTrackById;
        result     = MBE_GetNumTracks;
        extraction = MBS_SelectTrack;
        m_tracks   = true;
        break;
    default:
187
        kdDebug(65432) << "Unrecognized query reported" << endl;
Zack Rusin's avatar
Zack Rusin committed
188 189 190
    }
}

191
MusicBrainzQuery::Album MusicBrainzQuery::extractAlbum()
Zack Rusin's avatar
Zack Rusin committed
192
{
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    kdDebug(65432) << "Extracting album" << endl;

    std::string s;

    GetIDFromURL(Data(MBE_AlbumGetAlbumId), s);

    Album album;

    album.id         = s.c_str();
    album.name       = dataExtract(MBE_AlbumGetAlbumName);
    album.numTracks  = DataInt(MBE_AlbumGetNumTracks);
    album.artist     = dataExtract(MBE_AlbumGetArtistName, 1);

    GetIDFromURL(Data(MBE_AlbumGetArtistId, 1), s);

    album.artistId   = s.c_str();
    album.status     = dataExtract(MBE_AlbumGetAlbumStatus);
    album.type       = dataExtract(MBE_AlbumGetAlbumType);
    album.cdIndexId  = dataExtract(MBE_AlbumGetNumCdindexIds);
Zack Rusin's avatar
Zack Rusin committed
212 213 214

    TrackList tracks;

215 216 217 218 219 220
    for(int i = 1; i <= album.numTracks; ++i)
        tracks.append(extractTrackFromAlbum(i));

    album.tracksList = tracks;

    return album;
Zack Rusin's avatar
Zack Rusin committed
221 222
}

223
MusicBrainzQuery::Track MusicBrainzQuery::extractTrackFromAlbum(int trackNumber)
Zack Rusin's avatar
Zack Rusin committed
224 225
{
    Track track;
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

    track.number      = trackNumber;
    track.name        = dataExtract(MBE_AlbumGetTrackName, trackNumber);
    track.duration    = dataExtract(MBE_AlbumGetTrackDuration, trackNumber);
    track.artist      = dataExtract(MBE_AlbumGetArtistName, trackNumber);

    std::string s;

    GetIDFromURL(Data(MBE_AlbumGetTrackId), s);

    track.id = s.empty() ? QString::null : QString(s.c_str());

    GetIDFromURL(Data(MBE_AlbumGetArtistId), s);

    track.artistId = s.empty() ? QString::null : QString(s.c_str());

Zack Rusin's avatar
Zack Rusin committed
242 243 244
    return track;
}

245
MusicBrainzQuery::Track MusicBrainzQuery::extractTrack(int trackNumber)
Zack Rusin's avatar
Zack Rusin committed
246 247
{
    Track track;
248 249 250 251 252 253 254 255 256 257 258 259
    const std::string source = Data(MBE_TrackGetTrackId);
    std::string target = source;

    track.name     = dataExtract(MBE_TrackGetTrackName, trackNumber);
    track.duration = dataExtract(MBE_TrackGetTrackDuration, trackNumber);
    track.artist   = dataExtract(MBE_TrackGetArtistName, trackNumber);
    track.album    = dataExtract(MBE_AlbumGetAlbumName, trackNumber);

    GetIDFromURL(source, target);

    track.id = target.empty() ? QString::null : QString(target.c_str());

Zack Rusin's avatar
Zack Rusin committed
260
    Select(MBS_SelectTrackAlbum);
261 262 263 264 265 266 267

    track.number = GetOrdinalFromList(MBE_AlbumGetTrackList, source);

    GetIDFromURL(Data(MBE_AlbumGetArtistId), target);

    track.artistId = target.empty() ? QString::null : QString(target.c_str());

Zack Rusin's avatar
Zack Rusin committed
268
    Select(MBS_Rewind);
269

Zack Rusin's avatar
Zack Rusin committed
270 271 272
    return track;
}

273
void MusicBrainzQuery::slotTrmData(KProcess *, char *buffer, int bufferLength)
Zack Rusin's avatar
Zack Rusin committed
274
{
275
    m_trm += QString::fromLatin1(buffer, bufferLength);
Zack Rusin's avatar
Zack Rusin committed
276 277
}

278
void MusicBrainzQuery::slotTrmGenerationFinished(KProcess *)
Zack Rusin's avatar
Zack Rusin committed
279 280 281 282
{
    m_arguments.clear();
    m_arguments << m_trm;
    m_query = TrackFromTRM;
283
    kdDebug(65432) << "Generation finished " << m_trm << endl;
Zack Rusin's avatar
Zack Rusin committed
284 285 286 287 288 289
    slotQuery();
}

#include "musicbrainzquery.moc"

#endif