1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-20 21:02:26 +02:00

Merge pull request #333 from mrmaffen/master

Merge tomahawk.js from tomahawk-android + some fixes
This commit is contained in:
Dominik Schmidt
2015-10-14 10:09:26 +02:00

View File

@@ -42,17 +42,18 @@ Tomahawk.apiVersion = "0.2.2";
//Statuses considered a success for HTTP request
var httpSuccessStatuses = [200, 201];
// install RSVP.Promise as global Promise
if(window.Promise === undefined) {
window.Promise = window.RSVP.Promise;
window.RSVP.on('error', function(reason) {
// install RSVP error handler for uncaught(!) errors
RSVP.on('error', function (reason) {
var resolverName = "";
if (Tomahawk.resolver.instance) {
resolverName = Tomahawk.resolver.instance.settings.name + " - ";
}
if (reason) {
console.error(reason.message, reason);
console.error(resolverName + 'Uncaught error:' + JSON.stringify(reason));
} else {
console.error('Error: error thrown from RSVP but it was empty');
console.error(resolverName + 'Uncaught error: error thrown from RSVP but it was empty');
}
});
}
/**
* Compares versions strings
@@ -61,8 +62,12 @@ if(window.Promise === undefined) {
* (version1 > version2) == 1
*/
Tomahawk.versionCompare = function (version1, version2) {
var v1 = version1.split('.').map(function (item) { return parseInt(item); });
var v2 = version2.split('.').map(function (item) { return parseInt(item); });
var v1 = version1.split('.').map(function (item) {
return parseInt(item);
});
var v2 = version2.split('.').map(function (item) {
return parseInt(item);
});
var length = Math.max(v1.length, v2.length);
var i = 0;
@@ -103,7 +108,6 @@ Tomahawk.atLeastVersion = function (version) {
return (Tomahawk.versionCompare(Tomahawk.apiVersion, version) >= 0);
};
Tomahawk.resolver = {
scriptPath: Tomahawk.resolverData().scriptPath
};
@@ -132,10 +136,9 @@ Tomahawk.htmlDecode = (function() {
})();
Tomahawk.dumpResult = function (result) {
var results = result.results,
i = 0;
var results = result.results;
Tomahawk.log("Dumping " + results.length + " results for query " + result.qid + "...");
for (i = 0; i < results.length; i++) {
for (var i = 0; i < results.length; i++) {
Tomahawk.log(results[i].artist + " - " + results[i].track + " | " + results[i].url);
}
@@ -155,7 +158,7 @@ Tomahawk.extend = function (object, members) {
return newObject;
};
//Deprecated for 0.9 resolvers. Reporting resolver capabilities is no longer necessary.
var TomahawkResolverCapability = {
NullCapability: 0,
Browsable: 1,
@@ -164,6 +167,7 @@ var TomahawkResolverCapability = {
UrlLookup: 8
};
//Deprecated for 0.9 resolvers. Use Tomahawk.UrlType instead.
var TomahawkUrlType = {
Any: 0,
Playlist: 1,
@@ -172,6 +176,7 @@ var TomahawkUrlType = {
Artist: 8
};
//Deprecated for 0.9 resolvers. Use Tomahawk.ConfigTestResultType instead.
var TomahawkConfigTestResultType = {
Other: 0,
Success: 1,
@@ -231,24 +236,40 @@ var TomahawkResolver = {
collection: function () {
return {};
},
getStreamUrl: function(params) {
Tomahawk.reportStreamUrl(params.qid, params.url);
},
_testConfig: function (config) {
return Promise.resolve(this.testConfig(config)).then(function() {
return RSVP.Promise.resolve(this.testConfig(config)).then(function () {
return {result: Tomahawk.ConfigTestResultType.Success};
});
},
testConfig: function () {
this.configTest();
},
getStreamUrl: function (qid, url) {
Tomahawk.reportStreamUrl(qid, url);
}
};
Tomahawk.Resolver = Tomahawk.extend(TomahawkResolver, {
Tomahawk.Resolver = {
init: function () {
},
scriptPath: function () {
return Tomahawk.resolverData().scriptPath;
},
getConfigUi: function () {
return {};
},
getUserConfig: function () {
return JSON.parse(window.localStorage[this.scriptPath()] || "{}");
},
saveUserConfig: function () {
window.localStorage[this.scriptPath()] = JSON.stringify(Tomahawk.resolverData().config);
this.newConfigSaved(Tomahawk.resolverData().config);
},
newConfigSaved: function () {
},
getStreamUrl: function (params) {
return params;
},
_convertUrls: function (results) {
var that = this;
@@ -264,14 +285,19 @@ Tomahawk.Resolver = Tomahawk.extend(TomahawkResolver, {
var that = this;
var collectionPromises = [];
Tomahawk.collections.forEach(function (col) {
if(col.resolve)
if (col.resolve) {
collectionPromises.push(col.resolve({artist: artist, album: album, track: title}));
}
});
Promise.all(collectionPromises).then(function(collectionResults){
RSVP.Promise.all(collectionPromises).then(function (collectionResults) {
var merged = [];
return merged.concat.apply(merged, collectionResults);
}).then(function (collectionResults) {
Promise.resolve(that.resolve({artist: artist, album: album, track:title})).then(function(results){
RSVP.Promise.resolve(that.resolve({
artist: artist,
album: album,
track: title
})).then(function (results) {
Tomahawk.addTrackResults({
'qid': qid,
'results': that._convertUrls(results.concat(collectionResults))
@@ -280,8 +306,7 @@ Tomahawk.Resolver = Tomahawk.extend(TomahawkResolver, {
});
},
_adapter_init: function ()
{
_adapter_init: function () {
this._urlProtocol = this.settings.name.replace(/[^a-zA-Z]/g, '').toLowerCase();
Tomahawk.addCustomUrlHandler(this._urlProtocol, 'getStreamUrl', true);
Tomahawk.log('Registered custom url handler for protocol "' + this._urlProtocol + '"');
@@ -290,24 +315,24 @@ Tomahawk.Resolver = Tomahawk.extend(TomahawkResolver, {
_adapter_getStreamUrl: function (params) {
params.url = params.url.slice(this._urlProtocol.length + 3);
Promise.resolve(this.getStreamUrl(params)).then(function(result){
RSVP.Promise.resolve(this.getStreamUrl(params)).then(function (result) {
Tomahawk.reportStreamUrl(params.qid, result.url, result.headers);
});
},
_adapter_search: function (qid, query)
{
_adapter_search: function (qid, query) {
var that = this;
var collectionPromises = [];
Tomahawk.collections.forEach(function (col) {
if(col.search)
if (col.search) {
collectionPromises.push(col.search({query: query}));
}
});
Promise.all(collectionPromises).then(function(collectionResults){
RSVP.Promise.all(collectionPromises).then(function (collectionResults) {
var merged = [];
return merged.concat.apply(merged, collectionResults);
}).then(function (collectionResults) {
Promise.resolve(that.search({query:query})).then(function(results){
RSVP.Promise.resolve(that.search({query: query})).then(function (results) {
Tomahawk.addTrackResults({
'qid': qid,
'results': that._convertUrls(results.concat(collectionResults))
@@ -317,56 +342,11 @@ Tomahawk.Resolver = Tomahawk.extend(TomahawkResolver, {
},
_adapter_testConfig: function (config) {
return Promise.resolve(this.testConfig(config)).then(function() {
return RSVP.Promise.resolve(this.testConfig(config)).then(function () {
return {result: Tomahawk.ConfigTestResultType.Success};
});
}
});
/**** begin example implementation of a resolver ****/
// implement the resolver
/*
* var DemoResolver = Tomahawk.extend(TomahawkResolver,
* {
* getSettings: function()
* {
* return {
* name: "Demo Resolver",
* weigth: 95,
* timeout: 5,
* limit: 10
};
},
resolve: function( qid, artist, album, track )
{
return {
qid: qid,
results: [
{
artist: "Mokele",
album: "You Yourself are Me Myself and I am in Love",
track: "Hiding In Your Insides (php)",
source: "Mokele.co.uk",
url: "http://play.mokele.co.uk/music/Hiding%20In%20Your%20Insides.mp3",
bitrate: 160,
duration: 248,
size: 4971780,
score: 1.0,
extension: "mp3",
mimetype: "audio/mpeg"
}
]
};
}
}
);
// register the resolver
Tomahawk.resolver.instance = DemoResolver;*/
/**** end example implementation of a resolver ****/
// help functions
@@ -478,7 +458,7 @@ Tomahawk.asyncRequestIdCounter = 0;
Tomahawk.asyncRequestCallbacks = {};
/**
* Pass the natively retrived reply back to the javascript callback
* Pass the natively retrieved reply back to the javascript callback
* and augment the fake XMLHttpRequest object.
*
* Internal use only!
@@ -541,7 +521,8 @@ Tomahawk.asyncRequest = function (url, callback, extraHeaders, options) {
}
}
xmlHttpRequest.onreadystatechange = function () {
if (xmlHttpRequest.readyState == 4 && httpSuccessStatuses.indexOf(xmlHttpRequest.status) != -1) {
if (xmlHttpRequest.readyState == 4
&& httpSuccessStatuses.indexOf(xmlHttpRequest.status) != -1) {
callback.call(window, xmlHttpRequest);
} else if (xmlHttpRequest.readyState === 4) {
Tomahawk.log("Failed to do " + method + " request: to: " + url);
@@ -585,9 +566,15 @@ Tomahawk.ajax = function(url, settings) {
var str = [];
for (var p in obj) {
if (obj[p] !== undefined) {
if (Array.isArray(obj[p])) {
for (var i = 0; i < obj[p].length; i++) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p][i]));
}
} else {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
}
}
str.sort();
@@ -601,7 +588,8 @@ Tomahawk.ajax = function(url, settings) {
settings.data = JSON.stringify(settings.data);
settings.contentType = settings.contentType || 'application/json';
} else {
throw new Error("Tomahawk.ajax: unknown dataFormat requested: " + settings.dataFormat);
throw new Error("Tomahawk.ajax: unknown dataFormat requested: "
+ settings.dataFormat);
}
} else {
throw new Error("Tomahawk.ajax: data should be either object or string");
@@ -618,7 +606,7 @@ Tomahawk.ajax = function(url, settings) {
}
}
return new Promise(function (resolve, reject) {
return new RSVP.Promise(function (resolve, reject) {
settings.errorHandler = reject;
Tomahawk.asyncRequest(settings.url, resolve, settings.headers, settings);
}).then(function (xhr) {
@@ -629,7 +617,7 @@ Tomahawk.ajax = function(url, settings) {
var contentType;
if (settings.dataType === 'json') {
contentType = 'application/json';
} else if (contentType === 'xml') {
} else if (settings.dataType === 'xml') {
contentType = 'text/xml';
} else if (typeof xhr.getResponseHeader !== 'undefined') {
contentType = xhr.getResponseHeader('Content-Type');
@@ -820,8 +808,12 @@ Tomahawk.localStorage = Tomahawk.localStorage || {
// some aliases
Tomahawk.setTimeout = Tomahawk.setTimeout || window.setTimeout;
Tomahawk.setInterval = Tomahawk.setInterval || window.setInterval;
Tomahawk.base64Decode = function(a) { return window.atob(a); };
Tomahawk.base64Encode = function(b) { return window.btoa(b); };
Tomahawk.base64Decode = function (a) {
return window.atob(a);
};
Tomahawk.base64Encode = function (b) {
return window.btoa(b);
};
Tomahawk.PluginManager = {
objects: {},
@@ -835,8 +827,7 @@ Tomahawk.PluginManager = {
},
registerPlugin: function (type, object) {
this.objects[this.identifyObject(object)] = object;
if (type === 'collection')
{
if (type === 'collection') {
Tomahawk.collections.push(object);
}
@@ -871,22 +862,44 @@ Tomahawk.PluginManager = {
}
if (typeof this.objects[objectId][methodName] === 'function') {
if (!Tomahawk.resolver.instance.apiVersion || Tomahawk.resolver.instance.apiVersion < 0.9) {
if (!Tomahawk.resolver.instance.apiVersion
|| Tomahawk.resolver.instance.apiVersion < 0.9) {
if (methodName == 'artists') {
return new Promise(function (resolve, reject) {
return new RSVP.Promise(function (resolve, reject) {
pluginManager.resolve[requestId] = resolve;
Tomahawk.resolver.instance.artists(requestId);
});
} else if (methodName == 'albums') {
return new Promise(function (resolve, reject) {
return new RSVP.Promise(function (resolve, reject) {
pluginManager.resolve[requestId] = resolve;
Tomahawk.resolver.instance.albums(requestId, params.artist);
});
} else if (methodName == 'tracks') {
return new Promise(function (resolve, reject) {
return new RSVP.Promise(function (resolve, reject) {
pluginManager.resolve[requestId] = resolve;
Tomahawk.resolver.instance.tracks(requestId, params.artist, params.album);
});
} else if (methodName == 'lookupUrl') {
return new RSVP.Promise(function (resolve, reject) {
pluginManager.resolve[params.url] = resolve;
Tomahawk.resolver.instance.lookupUrl(params.url);
});
} else if (methodName == 'getStreamUrl') {
return new RSVP.Promise(function (resolve, reject) {
pluginManager.resolve[requestId] = resolve;
Tomahawk.resolver.instance.getStreamUrl(requestId, params.url);
});
} else if (methodName == 'resolve') {
return new RSVP.Promise(function (resolve, reject) {
pluginManager.resolve[requestId] = resolve;
Tomahawk.resolver.instance.resolve(requestId, params.artist,
params.album, params.track);
});
} else if (methodName == 'search') {
return new RSVP.Promise(function (resolve, reject) {
pluginManager.resolve[requestId] = resolve;
Tomahawk.resolver.instance.search(requestId, params.query);
});
}
}
@@ -897,18 +910,12 @@ Tomahawk.PluginManager = {
},
invoke: function (requestId, objectId, methodName, params) {
Promise.resolve(this.invokeSync(requestId, objectId, methodName, params)).then(function (result) {
if (typeof result === 'object') {
RSVP.Promise.resolve(this.invokeSync(requestId, objectId, methodName, params))
.then(function (result) {
Tomahawk.reportScriptJobResults({
requestId: requestId,
data: result
});
} else {
Tomahawk.reportScriptJobResults({
requestId: requestId,
error: "Scripts need to return objects for requests: methodName: " + methodName + " params: " + JSON.stringify(params)
});
}
}, function (error) {
Tomahawk.reportScriptJobResults({
requestId: requestId,
@@ -918,6 +925,32 @@ Tomahawk.PluginManager = {
}
};
Tomahawk.NativeScriptJobManager = {
idCounter: 0,
deferreds: {},
invoke: function (methodName, params) {
var requestId = this.idCounter++;
Tomahawk.invokeNativeScriptJob(requestId, methodName, JSON.stringify(params));
this.deferreds[requestId] = RSVP.defer();
return this.deferreds[requestId].promise;
},
reportNativeScriptJobResult: function (requestId, result) {
var deferred = this.deferreds[requestId];
if (!deferred) {
Tomahawk.log("Deferred object with the given requestId is not present!");
}
deferred.resolve(result);
}
};
Tomahawk.UrlType = {
Any: 0,
Playlist: 1,
Track: 2,
Album: 3,
Artist: 4,
XspfPlaylist: 5
};
Tomahawk.ConfigTestResultType = {
Other: 0,
@@ -1194,8 +1227,6 @@ Tomahawk.Collection = {
Transaction: function (collection, id) {
this.ensureDb = function () {
var that = this;
return new RSVP.Promise(function (resolve, reject) {
if (!collection.cachedDbs.hasOwnProperty(id)) {
Tomahawk.log("Opening database");
@@ -1244,7 +1275,11 @@ Tomahawk.Collection = {
"FOREIGN KEY(artistId) REFERENCES artists(_id)," +
"FOREIGN KEY(albumId) REFERENCES albums(_id))", []);
});
//m.migration(2, function (tx) {
// //Tomahawk.log("Migrating to db version 2");
//});
m.execute();
}
resolve(collection.cachedDbs[id]);
});
@@ -1261,18 +1296,18 @@ Tomahawk.Collection = {
});
};
this.execDefferedStatements = function (resolve, reject) {
this.execDeferredStatements = function (resolve, reject) {
var that = this;
that.stmtsToResolve = that.statements.length;
that.results = that.statements.slice();
Tomahawk.log('Executing ' + that.stmtsToResolve + ' deffered SQL statements in transaction');
return new Promise(function (resolve, reject) {
if (that.statements.length == 0)
Tomahawk.log('Executing ' + that.stmtsToResolve
+ ' deferred SQL statements in transaction');
return new RSVP.Promise(function (resolve, reject) {
if (that.statements.length == 0) {
resolve([]);
else{
} else {
that.db.transaction(function (tx) {
for (var i = 0; i < that.statements.length; ++i)
{
for (var i = 0; i < that.statements.length; ++i) {
var stmt = that.statements[i];
tx.executeSql(stmt.statement, stmt.args,
(function () {
@@ -1280,8 +1315,7 @@ Tomahawk.Collection = {
//capture value of i
var originalI = i;
return function (tx, results) {
if (typeof that.statements[originalI].map !== 'undefined')
{
if (typeof that.statements[originalI].map !== 'undefined') {
var map = that.statements[originalI].map;
that.results[originalI] = [];
for (var ii = 0; ii < results.rows.length; ii++) {
@@ -1290,11 +1324,11 @@ Tomahawk.Collection = {
));
}
}
else
else {
that.results[originalI] = results;
}
that.stmtsToResolve--;
if(that.stmtsToResolve == 0)
{
if (that.stmtsToResolve == 0) {
that.statements = [];
resolve(that.results);
}
@@ -1310,7 +1344,7 @@ Tomahawk.Collection = {
});
}
});
},
};
this.sql = function (sqlStatement, sqlArgs, mapFunction) {
this.statements.push({statement: sqlStatement, args: sqlArgs, map: mapFunction});
@@ -1368,6 +1402,7 @@ Tomahawk.Collection = {
};
},
addTracks: function (params) {
var that = this;
var id = params.id;
@@ -1404,7 +1439,7 @@ Tomahawk.Collection = {
});
})(tracks[i]);
}
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function () {
// Get all artists' and albumArtists' db ids
t.sqlSelect("albumArtists", function (r) {
@@ -1421,7 +1456,7 @@ Tomahawk.Collection = {
_id: r._id
};
});
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (resultsArray) {
// Store the db ids in a map
var i, row, albumArtists = {};
@@ -1457,7 +1492,6 @@ Tomahawk.Collection = {
}
}).then(function () {
// Insert all albums
var promises = [];
for (var i = 0; i < tracks.length; i++) {
(function (track) {
var albumArtistId = cachedAlbumArtists[track.album].albumArtistId;
@@ -1467,7 +1501,7 @@ Tomahawk.Collection = {
});
})(tracks[i]);
}
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function () {
// Get the albums' db ids
t.sqlSelect("albums", function (r) {
@@ -1477,7 +1511,7 @@ Tomahawk.Collection = {
_id: r._id
};
});
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (results) {
// Store the db ids in a map
results = results[0];
@@ -1491,7 +1525,6 @@ Tomahawk.Collection = {
}
}).then(function () {
// Now we are ready to insert the tracks
var promises = [];
for (var i = 0; i < tracks.length; i++) {
(function (track) {
// Get all relevant ids that we stored in the previous steps
@@ -1517,7 +1550,7 @@ Tomahawk.Collection = {
});
})(tracks[i]);
}
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function () {
var resultMap = function (r) {
return {
@@ -1529,7 +1562,7 @@ Tomahawk.Collection = {
};
// Get the tracks' db ids
t.sqlSelect("tracks", resultMap, ["_id", "artistId", "albumId", "track"]);
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (results) {
that._trackCount = results[0].length;
Tomahawk.log("Added " + results[0].length + " tracks to collection '" + id + "'");
@@ -1560,9 +1593,9 @@ Tomahawk.Collection = {
t.sqlDrop("albums");
t.sqlDrop("artistAlbums");
t.sqlDrop("tracks");
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function () {
return new Promise(function (resolve, reject) {
return new RSVP.Promise(function (resolve, reject) {
that.cachedDbs[id].changeVersion(that.cachedDbs[id].version, "", null,
function (err) {
if (console.error) {
@@ -1580,9 +1613,9 @@ Tomahawk.Collection = {
},
_fuzzyIndexIdsToTracks: function (resultIds, id) {
var that = this;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
var mapFn = function (row) {
@@ -1619,7 +1652,7 @@ Tomahawk.Collection = {
]
);
}
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (results) {
var merged = [];
return merged.concat.apply(merged,
@@ -1631,17 +1664,11 @@ Tomahawk.Collection = {
},
resolve: function (params) {
var id = params.id;
if(typeof id === 'undefined')
id = this.settings.id;
var resultIds = Tomahawk.resolveFromFuzzyIndex(params.artist, params.album, params.track);
return this._fuzzyIndexIdsToTracks(resultIds);
},
search: function (params) {
var id = params.id;
if(typeof id === 'undefined')
id = this.settings.id;
var resultIds = Tomahawk.searchFuzzyIndex(params.query);
return this._fuzzyIndexIdsToTracks(resultIds);
},
@@ -1649,8 +1676,9 @@ Tomahawk.Collection = {
tracks: function (params, where) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
@@ -1684,7 +1712,7 @@ Tomahawk.Collection = {
}
]
);
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (results) {
return {results: Tomahawk.resolver.instance._convertUrls(results[0])};
});
@@ -1693,8 +1721,9 @@ Tomahawk.Collection = {
albums: function (params, where) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
@@ -1716,14 +1745,18 @@ Tomahawk.Collection = {
}
]
);
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (results) {
results = results[0].filter(function (e) {
return (e.albumArtist != '' && e.album != '');
});
return {
artists: results.map(function(i){ return i.albumArtist;}),
albums: results.map(function(i){ return i.album;})
artists: results.map(function (i) {
return i.albumArtist;
}),
albums: results.map(function (i) {
return i.album;
})
};
});
},
@@ -1731,17 +1764,17 @@ Tomahawk.Collection = {
artists: function (params) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
var mapFn = function(r)
{
var mapFn = function (r) {
return r.artist;
};
t.sqlSelect("artists", mapFn, ["artist", "artistDisambiguation"]);
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (artists) {
return {artists: artists[0]};
});
@@ -1760,7 +1793,7 @@ Tomahawk.Collection = {
//};
//};
//t.sqlSelect("albumArtists", ["albumArtist", "albumArtistDisambiguation"]);
//return t.execDefferedStatements();
//return t.execDeferredStatements();
//}).then(function (results) {
//return results[0];
//});
@@ -1769,24 +1802,27 @@ Tomahawk.Collection = {
artistAlbums: function (params) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var artist = params.artist;
//var artistDisambiguation = params.artistDisambiguation;
var that = this;
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
t.sqlSelect("artists",function(r){return r._id;}, ["_id"], {
t.sqlSelect("artists", function (r) {
return r._id;
}, ["_id"], {
artist: artist
//artistDisambiguation: artistDisambiguation
});
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (results) {
var artistId = results[0][0];
t.sqlSelect("artistAlbums",function(r){return r.album;}, ["albumId", 'album'], {
t.sqlSelect("artistAlbums", function (r) {
return r.album;
}, ["albumId", 'album'], {
artistId: artistId
}, [
{
@@ -1796,7 +1832,7 @@ Tomahawk.Collection = {
}
}
]);
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (results) {
return {
artist: artist,
@@ -1808,8 +1844,9 @@ Tomahawk.Collection = {
albumTracks: function (params) {
//TODO filter/where support
var id = params.id;
if(typeof id === 'undefined')
if (typeof id === 'undefined') {
id = this.settings.id;
}
var albumArtist = params.artist;
//var albumArtistDisambiguation = params.albumArtistDisambiguation;
var album = params.album;
@@ -1818,18 +1855,22 @@ Tomahawk.Collection = {
var t = new Tomahawk.Collection.Transaction(this, id);
return t.beginTransaction().then(function () {
t.sqlSelect("artists", function(r){return r._id;},["_id"], {
artist: albumArtist,
t.sqlSelect("artists", function (r) {
return r._id;
}, ["_id"], {
artist: albumArtist
//artistDisambiguation: albumArtistDisambiguation
});
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (results) {
var albumArtistId = results[0][0];
t.sqlSelect("albums",function(r){return r._id;}, ["_id"], {
t.sqlSelect("albums", function (r) {
return r._id;
}, ["_id"], {
album: album,
albumArtistId: albumArtistId
});
return t.execDefferedStatements();
return t.execDeferredStatements();
}).then(function (results) {
var albumId = results[0][0];
return that.tracks(params, {
@@ -1840,15 +1881,16 @@ Tomahawk.Collection = {
collection: function () {
this.settings.trackcount = this._trackCount;
if(! this.settings.description)
if (!this.settings.description) {
this.settings.description = this.settings.prettyname;
}
this.settings.capabilities = [Tomahawk.Collection.BrowseCapability.Artists,
Tomahawk.Collection.BrowseCapability.Albums, Tomahawk.Collection.BrowseCapability.Tracks];
Tomahawk.Collection.BrowseCapability.Albums,
Tomahawk.Collection.BrowseCapability.Tracks];
return this.settings;
}
};
// Legacy compability for 0.8 and before
Tomahawk.reportCapabilities = function (capabilities) {
if (capabilities & TomahawkResolverCapability.Browsable) {
@@ -1858,7 +1900,59 @@ Tomahawk.reportCapabilities = function (capabilities) {
Tomahawk.nativeReportCapabilities(capabilities);
};
Tomahawk.addArtistResults = Tomahawk.addAlbumResults = Tomahawk.addAlbumTrackResults = function (result) {
Tomahawk.addArtistResults = Tomahawk.addAlbumResults = Tomahawk.addAlbumTrackResults
= function (result) {
Tomahawk.PluginManager.resolve[result.qid](result);
delete Tomahawk.PluginManager.resolve[result.qid];
};
Tomahawk.addTrackResults = function (result) {
Tomahawk.PluginManager.resolve[result.qid](result.results);
delete Tomahawk.PluginManager.resolve[result.qid];
};
Tomahawk.reportStreamUrl = function (qid, streamUrl, headers) {
Tomahawk.PluginManager.resolve[qid]({
url: streamUrl,
headers: headers
});
delete Tomahawk.PluginManager.resolve[qid];
};
Tomahawk.addUrlResult = function (url, result) {
/* Merge the whole mess into one consistent result which is independent of type
var cleanResult = {
type: result.type,
guid: result.guid,
info: result.info,
creator: result.creator,
linkUrl: result.url
};
if (cleanResult.type == "track") {
cleanResult.track = result.title;
cleanResult.artist = result.artist;
} else if (cleanResult.type == "artist") {
cleanResult.artist = result.name;
} else if (cleanResult.type == "album") {
cleanResult.album = result.name;
cleanResult.artist = result.artist;
} else if (cleanResult.type == "playlist") {
cleanResult.title = result.title;
} else if (cleanResult.type == "xspf-url") {
cleanResult.url = result.url;
}
if (result.tracks) {
cleanResult.tracks = [];
var i;
for (i=0;i<result.tracks.length;i++) {
var cleanTrack = {
track: result.tracks[i].title,
artist: result.tracks[i].artist
};
cleanResult.push(cleanTrack)
}
Tomahawk.PluginManager.resolve[url](cleanResult);
*/
Tomahawk.PluginManager.resolve[url](result);
delete Tomahawk.PluginManager.resolve[url];
};