1 /****************************************************************************************
2 * Copyright (c) 2008 Peter ZHOU <peterzhoulei@gmail.com> *
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 "AmarokCollectionScript.h"
18
19 #include "amarokconfig.h"
20 #include "App.h"
21 #include <core/storage/SqlStorage.h>
22 #include "core-impl/collections/support/CollectionManager.h"
23 #include "core-impl/storage/StorageManager.h"
24 #include "core/collections/Collection.h"
25 #include "core-impl/collections/db/sql/SqlCollectionLocation.h"
26 #include "core/collections/QueryMaker.h"
27
28 #include <QJSEngine>
29
30 #define SCRIPTING_DEPRECATED( x ) \
31 AmarokScriptEngine *amarokScriptEngine = dynamic_cast<AmarokScriptEngine*>(parent()); \
32 if( amarokScriptEngine ) amarokScriptEngine->invokableDeprecatedCall( x );
33
34 using namespace AmarokScript;
35
36 using Collections::Collection;
37 using Collections::CollectionList;
38 using Collections::QueryMaker;
39
AmarokCollectionScript(AmarokScriptEngine * engine)40 AmarokCollectionScript::AmarokCollectionScript( AmarokScriptEngine *engine )
41 : QObject( engine )
42 {
43 QJSValue scriptObject = engine->newQObject( this );
44 //deprecate
45 engine->setDeprecatedProperty( QStringLiteral("Amarok"), QStringLiteral("Collection"), scriptObject );
46
47 engine->globalObject().property( QStringLiteral("Amarok") ).setProperty( QStringLiteral("CollectionManager"), scriptObject );
48
49 CollectionManager *instance = CollectionManager::instance();
50 connect( instance, &CollectionManager::collectionDataChanged,
51 this, &AmarokCollectionScript::collectionDataChanged );
52 connect( instance, &CollectionManager::collectionAdded,
53 this, &AmarokCollectionScript::collectionAdded );
54 connect( instance, &CollectionManager::collectionRemoved,
55 this, &AmarokCollectionScript::collectionRemoved );
56 }
57
58 int
totalAlbums() const59 AmarokCollectionScript::totalAlbums() const
60 {
61 QStringList albums = query( QStringLiteral("SELECT COUNT( id ) FROM albums;") );
62 if( albums.size() < 1 )
63 return 0;
64 QString total = albums[0];
65 return total.toInt();
66 }
67
68 int
totalArtists() const69 AmarokCollectionScript::totalArtists() const
70 {
71 QStringList artists = query( QStringLiteral("SELECT COUNT( id ) FROM artists;") );
72 if( artists.size() < 1 )
73 return 0;
74 QString total = artists[0];
75 return total.toInt();
76 }
77
78 int
totalComposers() const79 AmarokCollectionScript::totalComposers() const
80 {
81 QStringList composers = query( QStringLiteral("SELECT COUNT( id ) FROM composers;") );
82 if( composers.size() < 1 )
83 return 0;
84 QString total = composers[0];
85 return total.toInt();
86 }
87
88 int
totalGenres() const89 AmarokCollectionScript::totalGenres() const
90 {
91 QStringList genres = query( QStringLiteral("SELECT COUNT( id ) FROM genres;") );
92 if( genres.size() < 1 )
93 return 0;
94 QString total = genres[0];
95 return total.toInt();
96 }
97
98 int
totalTracks() const99 AmarokCollectionScript::totalTracks() const
100 {
101 QStringList tracks = query( QStringLiteral("SELECT COUNT( url ) FROM tracks;") );
102 if( tracks.size() < 0 )
103 return 0;
104 QString total = tracks[0];
105 int final = total.toInt();
106 return final;
107 }
108
109 QStringList
collectionLocation() const110 AmarokCollectionScript::collectionLocation() const
111 {
112 SCRIPTING_DEPRECATED( "collectionLocation" )
113 Collections::CollectionLocation *location = CollectionManager::instance()->primaryCollection()->location();
114 QStringList result = location->actualLocation();
115 delete location;
116 return result;
117 }
118
119 QStringList
query(const QString & sql) const120 AmarokCollectionScript::query( const QString& sql ) const
121 {
122 return StorageManager::instance()->sqlStorage()->query( sql );
123 }
124
125 QString
escape(const QString & sql) const126 AmarokCollectionScript::escape( const QString& sql ) const
127 {
128 return StorageManager::instance()->sqlStorage()->escape( sql );
129 }
130
131 void
scanCollection() const132 AmarokCollectionScript::scanCollection() const
133 {
134 CollectionManager::instance()->startFullScan();
135 }
136
137 void
scanCollectionChanges() const138 AmarokCollectionScript::scanCollectionChanges() const
139 {
140 CollectionManager::instance()->checkCollectionChanges();
141 }
142
143 QueryMaker*
queryMaker() const144 AmarokCollectionScript::queryMaker() const
145 {
146 return CollectionManager::instance()->queryMaker();
147 }
148
149 CollectionList
viewableCollections() const150 AmarokCollectionScript::viewableCollections() const
151 {
152 return CollectionManager::instance()->viewableCollections();
153 }
154