musicbrainzquery.cpp 8.2 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

#if HAVE_MUSICBRAINZ

#include "musicbrainzquery.h"

#include <kprocess.h>
27
#include <klocale.h>
28
#include <kdeversion.h>
Zack Rusin's avatar
Zack Rusin committed
29 30 31 32 33 34 35 36
#include <kdebug.h>

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

#include <string>
#include <vector>

37 38 39 40 41 42 43
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
44 45 46 47 48 49
{

}

void MusicBrainzQuery::start()
{
50 51 52 53 54 55 56 57 58 59 60
    if(m_query == File) {
        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 *)));

61
        emit signalStatusMsg( i18n( "Generating TRM signature..." ), 0 );
62
        bool started = process->start(KProcess::NotifyOnExit, KProcess::AllOutput);
63 64 65 66
        if(!started) {
            kdDebug(65432) << "trm utility could not be started." << endl;
            emit signalDone();
        }
67 68
    }
    else
Zack Rusin's avatar
Zack Rusin committed
69 70 71 72 73 74 75 76 77 78
        QTimer::singleShot(0, this, SLOT(slotQuery()));
}

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

    queryStrings(queryString, resultString, extractString);

83
    emit signalStatusMsg( i18n( "Querying MusicBrainz server..." ), 0 );
84

85
    // UseUTF8(false);
Zack Rusin's avatar
Zack Rusin committed
86

87 88 89 90 91
    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
92

93 94
    bool ret = Query(queryString, &v);
    if(ret) {
Zack Rusin's avatar
Zack Rusin committed
95
        int numEntries = DataInt(resultString);
96 97

        for(int i = 1; i <= numEntries; ++i) {
Zack Rusin's avatar
Zack Rusin committed
98
            Select(extractString, i);
99 100

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

    if (m_tracks)
117
        emit signalDone(tracks);
Zack Rusin's avatar
Zack Rusin committed
118
    else
119
        emit signalDone(albums);
Zack Rusin's avatar
Zack Rusin committed
120

121
    deleteLater(); // schedule deletion
Zack Rusin's avatar
Zack Rusin committed
122 123
}

124
QString MusicBrainzQuery::dataExtract(const QString &type, int i)
Zack Rusin's avatar
Zack Rusin committed
125
{
126 127 128
    std::string s = Data(type.latin1(), i);

    if(s.empty())
Zack Rusin's avatar
Zack Rusin committed
129
        return QString::null;
130 131

    return s.c_str();
Zack Rusin's avatar
Zack Rusin committed
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 187 188 189 190 191
}

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:
192
        kdDebug(65432) << "Unrecognized query reported" << endl;
Zack Rusin's avatar
Zack Rusin committed
193 194 195
    }
}

196
MusicBrainzQuery::Album MusicBrainzQuery::extractAlbum()
Zack Rusin's avatar
Zack Rusin committed
197
{
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    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
217 218 219

    TrackList tracks;

220 221 222 223 224 225
    for(int i = 1; i <= album.numTracks; ++i)
        tracks.append(extractTrackFromAlbum(i));

    album.tracksList = tracks;

    return album;
Zack Rusin's avatar
Zack Rusin committed
226 227
}

228
MusicBrainzQuery::Track MusicBrainzQuery::extractTrackFromAlbum(int trackNumber)
Zack Rusin's avatar
Zack Rusin committed
229 230
{
    Track track;
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

    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
247 248 249
    return track;
}

250
MusicBrainzQuery::Track MusicBrainzQuery::extractTrack(int trackNumber)
Zack Rusin's avatar
Zack Rusin committed
251 252
{
    Track track;
253 254 255 256 257 258 259 260 261 262 263
    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);

    GetIDFromURL(source, target);

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

Zack Rusin's avatar
Zack Rusin committed
264
    Select(MBS_SelectTrackAlbum);
265

266
    track.album  = dataExtract(MBE_AlbumGetAlbumName, trackNumber);
267 268 269 270 271 272
    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
273
    Select(MBS_Rewind);
274

Zack Rusin's avatar
Zack Rusin committed
275 276 277
    return track;
}

278
void MusicBrainzQuery::slotTrmData(KProcess *, char *buffer, int bufferLength)
Zack Rusin's avatar
Zack Rusin committed
279
{
280
    m_trm += QString::fromLatin1(buffer, bufferLength);
Zack Rusin's avatar
Zack Rusin committed
281 282
}

Scott Wheeler's avatar
Scott Wheeler committed
283
void MusicBrainzQuery::slotTrmGenerationFinished(KProcess *process)
Zack Rusin's avatar
Zack Rusin committed
284
{
285
#if KDE_VERSION < KDE_MAKE_VERSION(3,1,90)
Scott Wheeler's avatar
Scott Wheeler committed
286
    delete process;
287
#endif
Scott Wheeler's avatar
Scott Wheeler committed
288 289
    Q_UNUSED(process);

Zack Rusin's avatar
Zack Rusin committed
290 291 292
    m_arguments.clear();
    m_arguments << m_trm;
    m_query = TrackFromTRM;
293
    kdDebug(65432) << "Generation finished " << m_trm << endl;
294 295 296 297
    if (m_trm.isEmpty())
        emit signalStatusMsg( i18n( "TRM generation failed" ), 2000 );
    else
        slotQuery();
298

Zack Rusin's avatar
Zack Rusin committed
299 300 301 302 303
}

#include "musicbrainzquery.moc"

#endif