mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-05 21:57:41 +02:00
Merge changes in tomahawk.js from tomahawk-player/tomahawk-android
This commit is contained in:
@@ -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
|
||||
@@ -231,24 +232,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 { 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;
|
||||
@@ -321,7 +338,7 @@ Tomahawk.Resolver = Tomahawk.extend(TomahawkResolver, {
|
||||
return { result: Tomahawk.ConfigTestResultType.Success };
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**** begin example implementation of a resolver ****/
|
||||
|
||||
@@ -478,7 +495,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!
|
||||
@@ -585,9 +602,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();
|
||||
|
||||
@@ -887,6 +910,27 @@ Tomahawk.PluginManager = {
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -898,17 +942,10 @@ Tomahawk.PluginManager = {
|
||||
|
||||
invoke: function (requestId, objectId, methodName, params ) {
|
||||
Promise.resolve(this.invokeSync(requestId, objectId, methodName, params)).then(function (result) {
|
||||
if (typeof result === 'object') {
|
||||
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 +955,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,
|
||||
@@ -1244,7 +1307,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]);
|
||||
});
|
||||
@@ -1862,3 +1929,54 @@ Tomahawk.addArtistResults = Tomahawk.addAlbumResults = Tomahawk.addAlbumTrackRes
|
||||
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];
|
||||
};
|
||||
|
Reference in New Issue
Block a user