1 /****************************************************************************************
2  * Copyright (c) 2010-2012 Leo Franchi <lfranchi@kde.org>                               *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #include "Generator_p.h"
18 #include "CatalogUpdateEntry.h"
19 
20 // JSON Parsing
21 #include "qjsonwrapper/Json.h"
22 
23 #include <QVariant>
24 #include <quuid.h>
25 #include <QDebug>
26 
catalogEntriesToJson(const Echonest::CatalogUpdateEntries & items)27 QByteArray Echonest::Generator::catalogEntriesToJson( const Echonest::CatalogUpdateEntries& items )
28 {
29     QVariant itms = catalogEntriesToVariant( items );
30     QByteArray serialized = QJsonWrapper::toJson( itms );
31 //    qDebug() << "Serialized:" << serialized;
32     return serialized;
33 }
34 
catalogEntryToJson(const Echonest::CatalogUpdateEntry & item)35 QByteArray Echonest::Generator::catalogEntryToJson( const Echonest::CatalogUpdateEntry& item )
36 {
37     QVariant itm = catalogEntryToVariant( item );
38     QByteArray serialized = QJsonWrapper::toJson( itm );
39    // qDebug() << "Serialized:" << serialized;
40     return serialized;
41 }
42 
43 
catalogEntriesToVariant(const Echonest::CatalogUpdateEntries & items)44 QVariantList Echonest::Generator::catalogEntriesToVariant( const Echonest::CatalogUpdateEntries& items )
45 {
46     // copy the catalog item into a QVariant
47     QVariantList itemList;
48     foreach( const Echonest::CatalogUpdateEntry& item, items )
49         itemList << catalogEntryToVariant( item );
50 
51     qDebug() << "Generated " << itemList.size() << "entries to catalog variant!";
52     return itemList;
53 }
54 
catalogEntryToVariant(const Echonest::CatalogUpdateEntry & item)55 QVariant Echonest::Generator::catalogEntryToVariant( const Echonest::CatalogUpdateEntry& item )
56 {
57     QVariantMap itemMap;
58     QVariantMap itm;
59 
60     itemMap[ QLatin1String( "action" ) ] = Echonest::catalogUpdateActionToLiteral( item.action() );
61 
62     if( item.itemId().isEmpty() )
63         itm[ QLatin1String( "item_id" ) ] = QUuid::createUuid().toString().replace( QLatin1Char( '{' ), QString() ).replace( QLatin1Char( '}' ), QString() );
64     else
65         itm[ QLatin1String( "item_id" ) ] = item.itemId();
66 
67     if( !item.fingerprint().isEmpty() )
68         itm[ QLatin1String( "fp_code" ) ] = item.fingerprint();
69 
70     if( !item.songId().isEmpty() )
71         itm[ QLatin1String( "song_id" ) ] = item.songId();
72 
73     if( !item.songName().isEmpty() )
74         itm[ QLatin1String( "song_name" ) ] = item.songName();
75 
76     if( !item.artistId().isEmpty() )
77         itm[ QLatin1String( "artist_id" ) ] = item.artistId();
78 
79     if( !item.artistName().isEmpty() )
80         itm[ QLatin1String( "artist_name" ) ] = item.artistName();
81 
82     if( !item.release().isEmpty() )
83         itm[ QLatin1String( "release" ) ] = item.release();
84 
85     if( !item.genre().isEmpty() )
86         itm[ QLatin1String( "genre" ) ] = item.genre();
87 
88     if( item.trackNumber() > -1 )
89         itm[ QLatin1String( "track_number" ) ] = item.trackNumber();
90 
91     if( item.discNumber() > -1 )
92         itm[ QLatin1String( "disc_number" ) ] = item.discNumber();
93 
94     if( !item.url().isEmpty() )
95         itm[ QLatin1String( "url" ) ] = item.url();
96 
97     if( item.favoriteSet() )
98         itm[ QLatin1String( "favorite" ) ] = item.favorite();
99 
100     if( item.bannedSet() )
101         itm[ QLatin1String( "banned" ) ] = item.banned();
102 
103     if( item.playCount() > -1 )
104         itm[ QLatin1String( "play_count" ) ] = item.playCount();
105 
106     if( item.skipCount() > -1 )
107         itm[ QLatin1String( "skip_count" ) ] = item.skipCount();
108 
109     if( item.rating() > -1 )
110         itm[ QLatin1String( "rating" ) ] = item.rating();
111 
112     itemMap[ QLatin1String( "item" ) ] = itm;
113 
114     return itemMap;
115 }
116