musicbrainzquery.cpp 10.1 KB
Newer Older
Zack Rusin's avatar
Zack Rusin committed
1 2 3
// musicbrainzquery.cpp
//
// Copyright (C)  2003  Zack Rusin <zack@kde.org>
4
// Copyright (C)  2003 - 2004  Zack Rusin <wheeler@kde.org>
Zack Rusin's avatar
Zack Rusin committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
// 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.

21
#include <config.h>
Zack Rusin's avatar
Zack Rusin committed
22 23 24 25

#if HAVE_MUSICBRAINZ

#include "musicbrainzquery.h"
26 27 28
#include "trackpickerdialog.h"
#include "collectionlist.h"
#include "tag.h"
Zack Rusin's avatar
Zack Rusin committed
29

30 31 32
#include <kmainwindow.h>
#include <kapplication.h>
#include <kstatusbar.h>
Zack Rusin's avatar
Zack Rusin committed
33
#include <kprocess.h>
34
#include <klocale.h>
35
#include <kdeversion.h>
Zack Rusin's avatar
Zack Rusin committed
36 37 38 39 40 41 42 43
#include <kdebug.h>

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

#include <string>
#include <vector>

44
MusicBrainzQuery::MusicBrainzQuery(QueryType query, const QStringList &args,
45 46 47 48 49 50
                                   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
51 52 53 54 55 56
{

}

void MusicBrainzQuery::start()
{
57 58 59 60 61 62 63 64 65 66 67
    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 *)));

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

void MusicBrainzQuery::slotQuery()
{
    std::string queryString;
    std::string resultString;
    std::string extractString;
    AlbumList albums;
    TrackList tracks;
86
    std::vector<std::string> v;
Zack Rusin's avatar
Zack Rusin committed
87 88 89

    queryStrings(queryString, resultString, extractString);

90
    emit signalStatusMsg( i18n( "Querying MusicBrainz server..." ), 0 );
91

92
    // UseUTF8(false);
Zack Rusin's avatar
Zack Rusin committed
93

94 95 96 97 98
    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
99

100 101
    bool ret = Query(queryString, &v);
    if(ret) {
Zack Rusin's avatar
Zack Rusin committed
102
        int numEntries = DataInt(resultString);
103 104

        for(int i = 1; i <= numEntries; ++i) {
Zack Rusin's avatar
Zack Rusin committed
105
            Select(extractString, i);
106 107

            if(m_tracks) {
Zack Rusin's avatar
Zack Rusin committed
108 109
                Track track(extractTrack(i));
                tracks.append(track);
110
            }
111
            else {
112 113
                Album album(extractAlbum());
                albums.append(album);
Zack Rusin's avatar
Zack Rusin committed
114 115
            }
        }
116
    }
117
    else {
Zack Rusin's avatar
Zack Rusin committed
118 119
        std::string error;
        GetQueryError(error);
120
        kdDebug(65432) << "Query failed: " << error.c_str() << endl;
Zack Rusin's avatar
Zack Rusin committed
121 122 123
    }

    if (m_tracks)
124
        emit signalDone(tracks);
Zack Rusin's avatar
Zack Rusin committed
125
    else
126
        emit signalDone(albums);
Zack Rusin's avatar
Zack Rusin committed
127

128
    deleteLater(); // schedule deletion
Zack Rusin's avatar
Zack Rusin committed
129 130
}

131
QString MusicBrainzQuery::dataExtract(const QString &type, int i)
Zack Rusin's avatar
Zack Rusin committed
132
{
133 134 135
    std::string s = Data(type.latin1(), i);

    if(s.empty())
Zack Rusin's avatar
Zack Rusin committed
136
        return QString::null;
137

138
    return QString::fromUtf8(s.c_str());
Zack Rusin's avatar
Zack Rusin committed
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 192 193 194 195 196 197 198
}

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:
199
        kdDebug(65432) << "Unrecognized query reported" << endl;
Zack Rusin's avatar
Zack Rusin committed
200 201 202
    }
}

203
MusicBrainzQuery::Album MusicBrainzQuery::extractAlbum()
Zack Rusin's avatar
Zack Rusin committed
204
{
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    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
224 225 226

    TrackList tracks;

227 228 229 230 231 232
    for(int i = 1; i <= album.numTracks; ++i)
        tracks.append(extractTrackFromAlbum(i));

    album.tracksList = tracks;

    return album;
Zack Rusin's avatar
Zack Rusin committed
233 234
}

235
MusicBrainzQuery::Track MusicBrainzQuery::extractTrackFromAlbum(int trackNumber)
Zack Rusin's avatar
Zack Rusin committed
236 237
{
    Track track;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    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
254 255 256
    return track;
}

257
MusicBrainzQuery::Track MusicBrainzQuery::extractTrack(int trackNumber)
Zack Rusin's avatar
Zack Rusin committed
258 259
{
    Track track;
260 261 262 263 264 265 266 267 268 269 270
    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
271
    Select(MBS_SelectTrackAlbum);
272

273
    track.album  = dataExtract(MBE_AlbumGetAlbumName, trackNumber);
274 275 276 277 278 279
    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
280
    Select(MBS_Rewind);
281

Zack Rusin's avatar
Zack Rusin committed
282 283 284
    return track;
}

285
void MusicBrainzQuery::slotTrmData(KProcess *, char *buffer, int bufferLength)
Zack Rusin's avatar
Zack Rusin committed
286
{
287
    m_trm += QString::fromLatin1(buffer, bufferLength);
Zack Rusin's avatar
Zack Rusin committed
288 289
}

Scott Wheeler's avatar
Scott Wheeler committed
290
void MusicBrainzQuery::slotTrmGenerationFinished(KProcess *process)
Zack Rusin's avatar
Zack Rusin committed
291
{
292
#if KDE_VERSION < KDE_MAKE_VERSION(3,1,90)
Scott Wheeler's avatar
Scott Wheeler committed
293
    delete process;
294
#endif
Scott Wheeler's avatar
Scott Wheeler committed
295 296
    Q_UNUSED(process);

Zack Rusin's avatar
Zack Rusin committed
297 298 299
    m_arguments.clear();
    m_arguments << m_trm;
    m_query = TrackFromTRM;
300
    kdDebug(65432) << "Generation finished " << m_trm << endl;
301 302 303 304
    if (m_trm.isEmpty())
        emit signalStatusMsg( i18n( "TRM generation failed" ), 2000 );
    else
        slotQuery();
Zack Rusin's avatar
Zack Rusin committed
305 306
}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
////////////////////////////////////////////////////////////////////////////////
// MusicBrainzFileQuery
////////////////////////////////////////////////////////////////////////////////

MusicBrainzFileQuery::MusicBrainzFileQuery(const FileHandle &file) :
    MusicBrainzQuery(MusicBrainzQuery::File, file.absFilePath()),
    m_file(file)
{
    connect(this, SIGNAL(signalDone(const MusicBrainzQuery::TrackList &)), 
            this, SLOT(slotDone(const MusicBrainzQuery::TrackList &)));

    KMainWindow *w = static_cast<KMainWindow *>(kapp->mainWidget());
    connect(this, SIGNAL(signalStatusMsg(const QString &, int)),
            w->statusBar(), SLOT(message(const QString &, int)));

    start();
}

void MusicBrainzFileQuery::slotDone(const MusicBrainzQuery::TrackList &result)
{
    KMainWindow *w = static_cast<KMainWindow *>(kapp->mainWidget());

    if(result.isEmpty()) {
        w->statusBar()->message(i18n("No matches found."), 2000);
        return;
    }

    TrackPickerDialog *trackPicker =
        new TrackPickerDialog(m_file.absFilePath(), result, w);

    if(trackPicker->exec() != QDialog::Accepted) {
        w->statusBar()->message(i18n("Canceled."), 2000);
        return;
    }

    MusicBrainzQuery::Track track = trackPicker->selectedTrack();

    if(!track.name.isEmpty())
        m_file.tag()->setTitle(track.name);
    if(!track.artist.isEmpty())
        m_file.tag()->setArtist(track.artist);
    if(!track.album.isEmpty())
        m_file.tag()->setAlbum(track.album);
    if(track.number)
        m_file.tag()->setTrack(track.number);

    m_file.tag()->save();
    CollectionList::instance()->slotRefreshItem(m_file.absFilePath());

    w->statusBar()->message(i18n("Done."), 2000);

    deleteLater();
}


Zack Rusin's avatar
Zack Rusin committed
362 363 364
#include "musicbrainzquery.moc"

#endif