Skip to content

Commit

Permalink
Get rid of global Showtime object and use modules named native/* to e…
Browse files Browse the repository at this point in the history
…xport C functions
  • Loading branch information
andoma committed Apr 2, 2015
1 parent 7daa873 commit cec9885
Show file tree
Hide file tree
Showing 29 changed files with 438 additions and 428 deletions.
2 changes: 1 addition & 1 deletion plugin_examples/webpopupplugin/devplug.js
Expand Up @@ -29,7 +29,7 @@ new page.Route("devplug:webtest", function(page) {
var auth = d.headers['Authentication-Callback'] + '?redirect_to=' + trap;

// Display web flow
var o = Showtime.webpopup(auth, "Login and auth TMDB", trap);
var o = require('native/popup').webpopup(auth, "Login and auth TMDB", trap);

if(o.result == 'trapped') {
// We ended up at our trap URL
Expand Down
62 changes: 34 additions & 28 deletions resources/ecmascript/legacy/api-v1.js
@@ -1,12 +1,16 @@
var prop = require('showtime/prop');

var cryptodigest = function(algo, str) {
var hash = Showtime.hashCreate(algo);
Showtime.hashUpdate(hash, str);
var digest = Showtime.hashFinalize(hash);
var crypto = require('native/crypto');
var hash = crypto.hashCreate(algo);
crypto.hashUpdate(hash, str);
var digest = crypto.hashFinalize(hash);
return Duktape.enc('hex', digest);
}

var misc = require('native/misc');
var string = require('native/string');
var popup = require('native/popup');

showtime = {

Expand All @@ -28,27 +32,29 @@ showtime = {
return require('showtime/http').request(url, c);
},

currentVersionInt: Showtime.currentVersionInt,
currentVersionString: Showtime.currentVersionString,
deviceId: Showtime.deviceId,
currentVersionInt: Core.currentVersionInt,
currentVersionString: Core.currentVersionString,
deviceId: Core.deviceId,

httpReq: function(url, ctrl, cb) {
return require('showtime/http').request(url, ctrl, cb);
},

entityDecode: Showtime.entityDecode,
queryStringSplit: Showtime.queryStringSplit,
pathEscape: Showtime.pathEscape,
paramEscape: Showtime.paramEscape,
message: Showtime.message,
textDialog: Showtime.textDialog,
probe: Showtime.probe,
notify: Showtime.notify,
durationToString: Showtime.durationToString,
entityDecode: string.entityDecode,
queryStringSplit: string.queryStringSplit,
pathEscape: string.pathEscape,
paramEscape: string.paramEscape,
durationToString: string.durationToString,

message: popup.message,
textDialog: popup.textDialog,
notify: popup.notify,

probe: misc.probe,

print: print,
trace: console.log,
basename: Showtime.fs.basename,
basename: require('native/fs').basename,

sha1digest: function(str) {
return cryptodigest('sha1', str);
Expand All @@ -63,22 +69,22 @@ showtime = {
},

systemIpAddress: function() {
return Showtime.systemIpAddress();
return misc.systemIpAddress();
},

getSubtitleLanguages: Showtime.getSubtitleLanguages,
getSubtitleLanguages: require('native/subtitle').getLanguages,

xmlrpc: function() {
var a = [];
for(var i = 2; i < arguments.length; i++)
a.push(arguments[i]);
var json = JSON.stringify(a);
var x = Showtime.xmlrpc(arguments[0], arguments[1], json);
var x = require('native/io').xmlrpc(arguments[0], arguments[1], json);
return require('showtime/xml').htsmsg(x);
},

sleep: function(x) {
return Showtime.sleep(x);
return Core.sleep(x);
}
};

Expand Down Expand Up @@ -122,25 +128,25 @@ var plugin = {
return this.descriptor;
},

getAuthCredentials: Showtime.getAuthCredentials,
getAuthCredentials: popup.getAuthCredentials,

addHTTPAuth: Showtime.httpInspectorCreate,
addHTTPAuth: require('native/io').httpInspectorCreate,

copyFile: Showtime.fs.copyfile,
selectView: Showtime.selectView,
copyFile: require('native/fs').copyfile,
selectView: misc.selectView,

createSettings: function(title, icon, description) {
var settings = require('showtime/settings');
return new settings.globalSettings(Plugin.id, title, icon, description);
},

cachePut: function(stash, key, obj, maxage) {
Showtime.cachePut('plugin/' + Plugin.id + '/' + stash,
misc.cachePut('plugin/' + Plugin.id + '/' + stash,
key, JSON.stringify(obj), maxage);
},

cacheGet: function(stash, key) {
var v = Showtime.cacheGet('plugin/' + Plugin.id + '/' + stash, key);
var v = misc.cacheGet('plugin/' + Plugin.id + '/' + stash, key);
return v ? JSON.parse(v) : null;
},

Expand All @@ -153,11 +159,11 @@ var plugin = {
},

addSubtitleProvider: function(fn) {
Showtime.subtitleAddProvider(function(root, query, basescore, autosel) {
require('native/subtitle').addProvider(function(root, query, basescore, autosel) {
var req = Object.create(query);
req.addSubtitle = function(url, title, language, format,
source, score) {
Showtime.subtitleAddItem(root, url, title, language, format, source,
require('native/subtitle').addItem(root, url, title, language, format, source,
basescore + score, autosel);
}
fn(req);
Expand Down
15 changes: 8 additions & 7 deletions resources/ecmascript/modules/fs.js
@@ -1,22 +1,23 @@


exports.writeFileSync = function(filename, data, opts) {
var fd = Showtime.fs.open(filename, 'w');
var fs = require('native/fs');
var fd = fs.open(filename, 'w');
try {
Showtime.fs.write(fd, data, 0, data.length, 0);
fs.write(fd, data, 0, data.length, 0);
}
finally {
Showtime.resourceDestroy(fd);
Core.resourceDestroy(fd);
}
}

exports.readFileSync = function(filename, opts) {
var fd = Showtime.fs.open(filename, 'r');
var fd = fs.open(filename, 'r');
try {
var buf = new Duktape.Buffer(Showtime.fs.fsize(fd));
Showtime.fs.read(fd, buf.valueOf(), 0, buf.length, 0);
var buf = new Duktape.Buffer(fs.fsize(fd));
fs.read(fd, buf.valueOf(), 0, buf.length, 0);
return buf;
} finally {
Showtime.resourceDestroy(fd);
Core.resourceDestroy(fd);
}
}
16 changes: 10 additions & 6 deletions resources/ecmascript/modules/showtime/http.js
Expand Up @@ -45,23 +45,25 @@ function HttpResponse(bytes, headers) {

HttpResponse.prototype.toString = function() {

var string = require('native/string');

// This function should do the equiv. of what http_response_toString()
// does in js_io.c

var cs = (/.*charset=(.*)/.exec(this.contenttype) || [])[1];

if(cs) // A character set was set in the HTTP header contenttype header
return Showtime.utf8FromBytes(this.bytes, cs);
return string.utf8FromBytes(this.bytes, cs);

if(Showtime.isUtf8(this.bytes))
if(string.isUtf8(this.bytes))
// Data validates as UTF-8, return it
return this.bytes.toString();

return Showtime.utf8FromBytes(this.bytes);
return string.utf8FromBytes(this.bytes);
}

HttpResponse.prototype.convertFromEncoding = function(encoding) {
return Showtime.utf8FromBytes(this.bytes, encoding);
return require('native/string').utf8FromBytes(this.bytes, encoding);
}


Expand All @@ -84,16 +86,18 @@ exports.request = function(url, ctrl, callback) {
}


var io = require('native/io')

if(callback) {

Showtime.httpReq(url, ctrl || {}, function(err, res) {
io.httpReq(url, ctrl || {}, function(err, res) {
if(err)
callback(err, null);
else
callback(null, new HttpResponse(res.buffer, res.responseheaders));
});
return;
}
var res = Showtime.httpReq(url, ctrl || {});
var res = io.httpReq(url, ctrl || {});
return new HttpResponse(res.buffer, res.responseheaders);
}
35 changes: 18 additions & 17 deletions resources/ecmascript/modules/showtime/page.js
Expand Up @@ -20,13 +20,14 @@ function Item(page) {

Item.prototype.bindVideoMetadata = function(obj) {
if(this.mlv)
Showtime.resourceDestroy(this.mlv);
this.mlv = Showtime.videoMetadataBind(this.root, this.root.url, obj);
Core.resourceDestroy(this.mlv);
this.mlv = require('native/metadata').videoMetadataBind(this.root,
this.root.url, obj);
}

Item.prototype.unbindVideoMetadata = function(obj) {
if(this.mlv) {
Showtime.resourceDestroy(this.mlv);
Core.resourceDestroy(this.mlv);
delete this.mlv
}
}
Expand Down Expand Up @@ -194,7 +195,7 @@ function Page(root, sync, flat) {
}
}
}
Showtime.propHaveMore(nodes, have_more);
prop.haveMore(nodes, have_more);
}

if(op == 'reqmove' && typeof this.reorderer == 'function') {
Expand All @@ -209,7 +210,7 @@ function Page(root, sync, flat) {


Page.prototype.haveMore = function(v) {
Showtime.propHaveMore(this.model.nodes, v);
prop.haveMore(this.model.nodes, v);
}

Page.prototype.findItemByProp = function(v) {
Expand Down Expand Up @@ -262,10 +263,10 @@ Page.prototype.appendItem = function(url, type, metadata) {
} catch(e) {
}
}
Showtime.bindPlayInfo(root, metabind_url);
require('native/metadata').bindPlayInfo(root, metabind_url);
}

Showtime.propSetParent(root, this.model.nodes);
prop.setParent(root, this.model.nodes);
return item;
}

Expand All @@ -277,7 +278,7 @@ Page.prototype.appendAction = function(type, data, enabled, metadata) {
root.type = type;
root.data = data;
root.metadata = metadata;
Showtime.propSetParent(root, this.model.actions);
prop.setParent(root, this.model.actions);
return item;
}

Expand All @@ -291,12 +292,12 @@ Page.prototype.appendPassiveItem = function(type, data, metadata) {
root.type = type;
root.data = data;
root.metadata = metadata;
Showtime.propSetParent(root, this.model.nodes);
prop.setParent(root, this.model.nodes);
return item;
}

Page.prototype.dump = function() {
Showtime.propPrint(this.root);
prop.print(this.root);
}

Page.prototype.flush = function() {
Expand All @@ -305,10 +306,10 @@ Page.prototype.flush = function() {

Page.prototype.redirect = function(url) {

Showtime.resourceDestroy(this.nodesub);
Core.resourceDestroy(this.nodesub);

if(this.sync) {
Showtime.backendOpen(this.root, url, true);
require('native/route').backendOpen(this.root, url, true);
} else {
prop.sendEvent(this.root.eventSink, "redirect", url);
}
Expand Down Expand Up @@ -348,7 +349,7 @@ Page.prototype.onEvent = function(type, callback) {

exports.Route = function(re, callback) {

this.route = Showtime.routeCreate(re, function(pageprop, sync, args) {
this.route = require('native/route').create(re, function(pageprop, sync, args) {

try {

Expand All @@ -371,13 +372,13 @@ exports.Route = function(re, callback) {
}

exports.Route.prototype.destroy = function() {
Showtime.resourceDestroy(this.route);
Core.resourceDestroy(this.route);
}


exports.Searcher = function(title, icon, callback) {

this.searcher = Showtime.hookRegister('searcher', function(model, query, loading) {
this.searcher = require('native/hook').register('searcher', function(model, query, loading) {

try {

Expand All @@ -393,7 +394,7 @@ exports.Searcher = function(title, icon, callback) {

var page = new Page(root, false, true);
page.type = 'directory';
root.url = Showtime.propMakeUrl(page.root);
root.url = prop.makeUrl(page.root);
prop.atomicAdd(loading, 1);
try {
callback(page, query);
Expand All @@ -414,5 +415,5 @@ exports.Searcher = function(title, icon, callback) {


exports.Searcher.prototype.destroy = function() {
Showtime.resourceDestroy(this.searcher);
Core.resourceDestroy(this.searcher);
}

0 comments on commit cec9885

Please sign in to comment.