Skip to content

Commit

Permalink
Non-free repos switch (#83)
Browse files Browse the repository at this point in the history
* frontend: properly handle empty repositories

* frontend: add non-free software switch

* frontend: refresh when coming back to browser panel

* services: get rid of elevate-service symlink, remove legacy script
  • Loading branch information
Informatic authored Feb 2, 2022
1 parent 479f0d2 commit 9dbc87a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 98 deletions.
50 changes: 50 additions & 0 deletions frontend/configutils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var repositoryBaseURL = 'https://repo.webosbrew.org/api/apps.json';
var repositoryNonfreeBaseURL = 'https://repo.webosbrew.org/api/non-free/apps.json';

function getConfig() {
var repositoriesConfig = {
repositories: [],
disableDefault: false,
enableNonfree: false,
};

try {
var parsed = JSON.parse(window.localStorage['repositoriesConfig']);
if (parsed.disableDefault !== undefined)
repositoriesConfig.disableDefault = parsed.disableDefault;
if (parsed.repositories !== undefined)
repositoriesConfig.repositories = parsed.repositories;
if (parsed.enableNonfree !== undefined)
repositoriesConfig.enableNonfree = parsed.enableNonfree;
} catch (err) {
console.warn('Config load failed:', err);
}

return repositoriesConfig;
}

function getRepositories() {
try {
var repositoriesConfig = getConfig();
var repos = repositoriesConfig.repositories.map(function (repo) { return repo.url; });
if (!repositoriesConfig.disableDefault) repos.push(repositoryBaseURL);
if (repositoriesConfig.enableNonfree) repos.push(repositoryNonfreeBaseURL);
return repos;
} catch (err) {
console.warn(err);
return [repositoryBaseURL];
}
}

function setConfig(config) {
window.localStorage['repositoriesConfig'] = JSON.stringify(config);
console.info(window.localStorage['repositoriesConfig']);
}

module.exports = {
repositoryBaseURL: repositoryBaseURL,

getRepositories: getRepositories,
getConfig: getConfig,
setConfig: setConfig,
};
48 changes: 19 additions & 29 deletions frontend/views/BrowserPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ var
AjaxSource = require('enyo/AjaxSource'),
Ajax = require('enyo/Ajax'),
Collection = require('enyo/Collection'),
SettingsPanel = require('./SettingsPanel.js');
SettingsPanel = require('./SettingsPanel.js'),
ConfigUtils = require('../configutils.js');

// TODO: Support pagniation https://repo.webosbrew.org/api/apps/{page}.json
// Page starts with 1

var repositoryBaseURL = 'https://repo.webosbrew.org/api/apps.json';

var RepoPackageModel = kind({
kind: Model,
name: "RepoPackageModel",
Expand All @@ -40,9 +39,9 @@ var NestedSource = kind({
this.collection.fetch({
success: function(col, _, result) {
console.info('nested success:', result, col);
if (!result.length) {
if (result.length === 1 && result[0].attributes.repository === null) {
col.errorCode = '600';
opts.error(result, '600');
opts.error([], '600');
} else {
opts.success(result);
}
Expand Down Expand Up @@ -128,30 +127,17 @@ module.exports = kind({
this.refresh();
},

transitionFinished: function (evt) {
if (!evt.isOffscreen) {
console.info('transitionFinished:', evt);
this.refresh();
}
},

refresh: function () {
console.info('refresh');

var repositoriesConfig = {repositories: [], disableDefault: false};

try {
var parsed = JSON.parse(window.localStorage['repositoriesConfig']);
if (parsed.disableDefault !== undefined)
repositoriesConfig.disableDefault = parsed.disableDefault;
if (parsed.repositories !== undefined)
repositoriesConfig.repositories = parsed.repositories;
} catch (err) {
console.warn('Config load failed:', err);
}

console.info(repositoriesConfig);
try {
var repos = repositoriesConfig.repositories.map(function (repo) { return repo.url; });
if (!repositoriesConfig.disableDefault) repos.push(repositoryBaseURL);
this.loadRepositories(repos);
} catch (err) {
console.warn('Load failed: ', err);
this.loadRepositories([repositoryBaseURL]);
}
this.loadRepositories(ConfigUtils.getRepositories());
},

loadRepositories: function (repos) {
Expand All @@ -174,13 +160,17 @@ module.exports = kind({
data.packages.forEach(function (element) {
element.uid = element.id + '|' + url;
element.repository = url;
element.official = url === repositoryBaseURL;
element.official = url === ConfigUtils.repositoryBaseURL;
});
return data.packages;
}
} catch (err) { console.warn('parsing failed', arguments, this); }
} catch (err) { console.warn('parsing failed', err, arguments, this); }

return [];
// This is very dirty. We don't have a way of indicating an
// invalid repository to NestedResource. Since uid is generated
// by our code, let's just assume `null` uid is an indicator of
// a failed parse...
return [{repository: null}];
},
}),
});
Expand Down
20 changes: 8 additions & 12 deletions frontend/views/SettingsPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var
Popup = require('moonstone/Popup'),
TooltipDecorator = require('moonstone/TooltipDecorator'),
LabeledTextItem = require('moonstone/LabeledTextItem'),
LunaService = require('enyo-webos/LunaService');
LunaService = require('enyo-webos/LunaService'),
ConfigUtils = require('../configutils.js');

var not = function (x) { return !x };

Expand Down Expand Up @@ -84,7 +85,8 @@ module.exports = kind({
{
components: [
{kind: Divider, content: 'Repositories'},
{kind: ToggleItem, content: 'Default repository - https://repo.webosbrew.org', checked: true, name: 'enableDefault', ontap: 'saveRepositories'},
{kind: ToggleItem, content: 'https://repo.webosbrew.org - Default repository', checked: true, name: 'enableDefault', ontap: 'saveRepositories'},
{kind: ToggleItem, content: 'https://repo.webosbrew.org - Extra non-free software', checked: false, name: 'enableNonfree', ontap: 'saveRepositories'},
{
kind: DataRepeater, name: 'extraRepositories',
components: [
Expand Down Expand Up @@ -163,17 +165,11 @@ module.exports = kind({
this.inherited(arguments);
this.$.getConfiguration.send({});

var repositoriesConfig = {repositories: [], disableDefault: false};
try {
var parsed = JSON.parse(window.localStorage['repositoriesConfig']);
if (parsed.disableDefault !== undefined)
repositoriesConfig.disableDefault = parsed.disableDefault;
if (parsed.repositories !== undefined)
repositoriesConfig.repositories = parsed.repositories;
} catch (err) { }
var repositoriesConfig = ConfigUtils.getConfig();

this.set('extraRepositories', new Collection(repositoriesConfig.repositories));
this.$.enableDefault.set('checked', !repositoriesConfig.disableDefault);
this.$.enableNonfree.set('checked', repositoriesConfig.enableNonfree);

global.webOS.fetchAppInfo((function (info) {
this.$.version.set('text', info.version);
Expand Down Expand Up @@ -214,11 +210,11 @@ module.exports = kind({
repos.push(this.extraRepositories.models[i]);
}

window.localStorage['repositoriesConfig'] = JSON.stringify({
ConfigUtils.setConfig({
repositories: repos,
disableDefault: !this.$.enableDefault.get('checked'),
enableNonfree: this.$.enableNonfree.get('checked'),
});
console.info(window.localStorage['repositoriesConfig']);
},
onGetConfiguration: function (sender, response) {
console.info(sender, response);
Expand Down
1 change: 0 additions & 1 deletion services/elevate-service

This file was deleted.

56 changes: 0 additions & 56 deletions services/elevate-service.legacy

This file was deleted.

Empty file modified services/elevate-service.ts
100644 → 100755
Empty file.

0 comments on commit 9dbc87a

Please sign in to comment.