~ubuntu-branches/ubuntu/natty/kdemultimedia/natty-proposed

« back to all changes in this revision

Viewing changes to kscd/mbmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Debian Qt/KDE Maintainers
  • Date: 2011-05-26 02:41:36 UTC
  • mfrom: (0.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 108.
  • Revision ID: james.westby@ubuntu.com-20110526024136-jjwsigfy402jhupm
Tags: upstream-4.6.3
ImportĀ upstreamĀ versionĀ 4.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 */
33
33
#include "mbmanager.h"
34
34
 
 
35
#include <ktoolinvocation.h>
 
36
 
 
37
#include <musicbrainz3/musicbrainz.h>
 
38
#include <musicbrainz3/query.h>
 
39
 
35
40
MBManager::MBManager():m_validInfo(true)
36
41
{
37
42
 
44
49
 
45
50
void MBManager::discLookup()
46
51
{
47
 
        MusicBrainz MB;
48
 
        string      error, data;
49
 
        bool        ret;
50
 
        int         numTracks = 0, trackNum = 1;
51
 
 
52
52
        m_validInfo = true;
53
53
 
 
54
        MusicBrainz::WebService* ws = new MusicBrainz::WebService();
 
55
 
54
56
        // Set the proper server to use. Defaults to mm.musicbrainz.org:80
55
57
        if (!qgetenv("MB_SERVER").isNull())
56
58
        {
57
 
                string server(qgetenv("MB_SERVER"));
58
 
                MB.SetServer(server, 80);
 
59
                std::string server(qgetenv("MB_SERVER"));
 
60
                ws->setHost(server);
59
61
                //kDebug() << "!! set server !!" ;
60
62
        }
61
63
        else
63
65
                //kDebug() << "no server";
64
66
        }
65
67
 
66
 
        // Check to see if the debug env var has been set
67
 
        if (!qgetenv("MB_DEBUG").isNull())
68
 
        {
69
 
                MB.SetDebug(qgetenv("MB_DEBUG").toInt() );
70
 
                //kDebug() << "!! set debug !!" ;
71
 
        }
72
 
        else
73
 
        {
74
 
                //kDebug() << "no debug";
75
 
        }
76
 
 
77
68
        // If you need to use a proxy, uncomment/edit the following line
78
69
        // as appropriate
79
 
        //MB.SetProxy("proxy.mydomain.com", 80);
80
 
 
81
 
        // Tell the client library to return data in UTF-8
82
 
        MB.UseUTF8(false);
83
 
 
84
 
        // Execute the GetCDInfo query, which pulls the TOC from the
85
 
        // audio CD in the cd-rom drive, calculates the disk id and
86
 
        // requests the data from the server
87
 
        ret = MB.Query(string(MBQ_GetCDInfo));
88
 
 
89
 
        //kDebug() << "query passed";
90
 
        if (!ret)
91
 
        {
92
 
                MB.GetQueryError(error);
93
 
                printf("Query failed: %s\n", error.c_str());
94
 
                m_validInfo = false;
95
 
        }
96
 
 
97
 
        // Check to see how many items were returned from the server
98
 
        if (MB.DataInt(MBE_GetNumAlbums) < 1)
99
 
        {
100
 
                printf("This CD was not found.\n");
101
 
                m_validInfo = false;
102
 
        }
103
 
 
104
 
        // TODO if multiple entries found
105
 
        if (MB.DataInt(MBE_GetNumAlbums) > 1)
106
 
        {
107
 
                kDebug() << MB.DataInt(MBE_GetNumAlbums) << " entries found";
108
 
        }
109
 
 
110
 
        // TODO manage multiple entries
111
 
        // Select the first album
112
 
        MB.Select(MBS_SelectAlbum, 1);
113
 
 
114
 
        // Get the number of tracks
115
 
        numTracks = MB.DataInt(MBE_AlbumGetNumTracks);
116
 
        //kDebug() << "NumTracks: " << numTracks << endl;
117
 
 
118
 
        if (m_validInfo == true)
119
 
        {
120
 
                // Sets info
121
 
                m_discInfo.Title = MB.Data(MBE_AlbumGetAlbumName).c_str();
122
 
                m_discInfo.Artist = MB.Data(MBE_AlbumGetAlbumArtistName).c_str();
123
 
 
124
 
                m_trackList.clear();
125
 
                MBTrackInfo track;
126
 
                for(int i = 1; i <= numTracks; i++)
127
 
                {
128
 
                        track.Title = MB.Data(MBE_AlbumGetTrackName, i).c_str();
129
 
                        track.Artist = MB.Data(MBE_AlbumGetArtistName, i).c_str();
130
 
                        track.Duration = MB.Data(MBE_AlbumGetTrackDuration, i).c_str();
131
 
 
132
 
                        m_trackList << track;
133
 
                }
134
 
        }
135
 
        else
136
 
        {
137
 
                // If invalid data, fill the information with something
138
 
                // Sets info
 
70
        //ws->setProxyHost("proxy.mydomain.com");
 
71
        //ws->setProxyPort(80);
 
72
 
 
73
        try
 
74
        {
 
75
                // FIXME Uses the disc found by musicbrainz, not
 
76
                // necessarily the one used by KsCD
 
77
                MusicBrainz::Disc *disc = MusicBrainz::readDisc();
 
78
                std::string discId = disc->getId();
 
79
 
 
80
                MusicBrainz::Query q(ws);
 
81
                MusicBrainz::ReleaseResultList results;
 
82
 
 
83
                try
 
84
                {
 
85
                        MusicBrainz::ReleaseFilter f = MusicBrainz::ReleaseFilter().discId(discId);
 
86
                        results = q.getReleases(&f);
 
87
 
 
88
                        // Check to see how many items were returned from the server
 
89
                        if (!results.empty())
 
90
                        {
 
91
                                // TODO if multiple entries found
 
92
                                if (results.size() > 1)
 
93
                                {
 
94
                                        kDebug() << results.size() << " entries found";
 
95
                                }
 
96
 
 
97
                                MusicBrainz::ReleaseResult *result = results.front();
 
98
                                MusicBrainz::Release *release = q.getReleaseById(result->getRelease()->getId(),
 
99
                                                                &MusicBrainz::ReleaseIncludes().tracks().artist());
 
100
                                // Sets info
 
101
                                m_discInfo.Title = QString::fromUtf8(release->getTitle().c_str());
 
102
                                m_discInfo.Artist = QString::fromUtf8(release->getArtist()->getName().c_str());
 
103
 
 
104
                                m_trackList.clear();
 
105
                                MBTrackInfo track;
 
106
                                for (MusicBrainz::TrackList::iterator j = release->getTracks().begin();
 
107
                                                                j != release->getTracks().end(); j++)
 
108
                                {
 
109
                                        MusicBrainz::Track *t = *j;
 
110
                                        MusicBrainz::Artist *artist = t->getArtist();
 
111
                                        if (!artist)
 
112
                                                artist = release->getArtist();
 
113
 
 
114
                                        track.Title = QString::fromUtf8(t->getTitle().c_str());
 
115
                                        track.Artist = QString::fromUtf8(artist->getName().c_str());
 
116
                                        track.Duration = t->getDuration();
 
117
 
 
118
                                        m_trackList << track;
 
119
                                }
 
120
                        }
 
121
                        else
 
122
                        {
 
123
                                kDebug() << "This CD was not found.";
 
124
                                m_validInfo = false;
 
125
                        }
 
126
 
 
127
 
 
128
                }
 
129
                // FIXME Doesn't seem to get caught, why?
 
130
                // catch (MusicBrainz::WebServiceError &e)
 
131
                catch (...)
 
132
                {
 
133
                        //kDebug() << "Error: " << e.what();
 
134
                        m_validInfo = false;
 
135
                }
 
136
 
 
137
                if (!m_validInfo)
 
138
                {
 
139
                        // If invalid data, fill the information with something
 
140
                        // Sets info
 
141
                        m_discInfo.Title = i18n("Unknown album");
 
142
                        m_discInfo.Artist = i18n("Unknown artist");
 
143
 
 
144
                        m_trackList.clear();
 
145
                        MBTrackInfo track;
 
146
                        for (MusicBrainz::Disc::TrackList::iterator j = disc->getTracks().begin(); j != disc->getTracks().end(); j++)
 
147
                        {
 
148
                                track.Title = i18n("Unknown title");
 
149
                                track.Artist = m_discInfo.Artist;
 
150
                                // time from mb library in sectors, 75 sectors = 1 second
 
151
                                track.Duration = (*j).second*1000/75;
 
152
 
 
153
                                m_trackList << track;
 
154
                        }
 
155
                }
 
156
                delete disc;
 
157
        }
 
158
        // FIXME Doesn't seem to get caught, why?
 
159
        //catch (MusicBrainz::DiscError &e)
 
160
        catch(...)
 
161
        {
 
162
                //kDebug() << "Error: " << e.what();
139
163
                m_discInfo.Title = i18n("Unknown album");
140
164
                m_discInfo.Artist = i18n("Unknown artist");
141
 
 
 
165
                m_discInfo.Artist = i18n( "No Disc" );
142
166
                m_trackList.clear();
143
 
                MBTrackInfo track;
144
 
                for(int i = 1; i <= numTracks; i++)
145
 
                {
146
 
 
147
 
                        track.Title = i18n("Unknown title");
148
 
                        track.Artist = m_discInfo.Artist;
149
 
                        track.Duration = MB.Data(MBE_AlbumGetTrackDuration, i).c_str();
150
 
 
151
 
                        m_trackList << track;
152
 
                }
153
167
        }
154
168
}
155
169
 
157
171
{
158
172
        showArtistLabel(m_discInfo.Artist);
159
173
 
160
 
        musicbrainz_t o;
161
 
        char          url[1025];
162
 
 
163
 
    // Create the musicbrainz object, which will be needed for subsequent calls
164
 
        o = mb_New();
165
 
 
166
 
    // Set the proper server to use. Defaults to mm.musicbrainz.org:80
167
 
        if (!qgetenv("MB_SERVER").isNull())
168
 
                mb_SetServer(o, qgetenv("MB_SERVER").data(), 80);
169
 
 
170
 
    // Check to see if the debug env var has been set
171
 
        if (!qgetenv("MB_DEBUG").isNull())
172
 
                mb_SetDebug(o, qgetenv("MB_DEBUG").toInt());
173
 
 
174
 
    // Tell the server to only return 2 levels of data, unless the MB_DEPTH env var is set
175
 
        if (!qgetenv("MB_DEPTH").isNull())
176
 
                mb_SetDepth(o, qgetenv("MB_DEPTH").toInt());
177
 
        else
178
 
                mb_SetDepth(o, 2);
179
 
 
180
 
//      m_browser = "firefox";
181
 
        m_browser = "konqueror";
182
 
 
183
 
    // Tell the client library to return data in ISO8859-1 and not UTF-8
184
 
        mb_UseUTF8(o, 0);
185
 
 
186
 
    // Now get the web submit url
187
 
        if (mb_GetWebSubmitURL(o, url, 1024))
188
 
        {
189
 
                int ret;
190
 
 
191
 
                printf("URL: %s\n", url);
192
 
 
193
 
                if (!m_browser)
194
 
                        m_browser = "konqueror";
195
 
 
196
 
                ret = LaunchBrowser(url, m_browser);
197
 
                if (ret == 0)
198
 
                        printf("Could not launch browser. (%s)\n", m_browser);
199
 
        }
200
 
        else
201
 
                printf("Could not read CD-ROM parameters. Is there a CD in the drive?\n");
202
 
 
203
 
// and clean up the musicbrainz object
204
 
        mb_Delete(o);
 
174
        try
 
175
        {
 
176
                MusicBrainz::Disc *disc = MusicBrainz::readDisc();
 
177
                std::string url = MusicBrainz::getSubmissionUrl(disc);
 
178
                delete disc;
 
179
 
 
180
                KToolInvocation::invokeBrowser(QString::fromUtf8(url.c_str()));
 
181
        }
 
182
        catch (MusicBrainz::DiscError &e)
 
183
        {
 
184
                kDebug() << "Error: " << e.what();
 
185
        }
205
186
}
206
187