lbry-desktop/ui/js/actions/settings.js

166 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
2017-08-24 19:33:41 +02:00
import * as settings from "constants/settings";
2017-09-07 02:52:34 +02:00
import { doAlertError } from "actions/app";
2017-08-08 11:36:14 +02:00
import batchActions from "util/batchActions";
2017-08-24 20:28:05 +02:00
2017-06-06 23:19:12 +02:00
import lbry from "lbry";
2017-08-08 11:36:14 +02:00
import fs from "fs";
import http from "http";
2017-08-24 18:11:39 +02:00
const { remote } = require("electron");
const { extname } = require("path");
2017-09-07 02:52:34 +02:00
const { readdir } = remote.require("fs");
2017-08-24 18:11:39 +02:00
export function doFetchDaemonSettings() {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
lbry.settings_get().then(settings => {
dispatch({
type: types.DAEMON_SETTINGS_RECEIVED,
data: {
2017-06-06 23:19:12 +02:00
settings,
},
});
});
};
}
export function doSetDaemonSetting(key, value) {
return function(dispatch, getState) {
let settings = {};
settings[key] = value;
2017-06-06 23:19:12 +02:00
lbry.settings_set(settings).then(settings);
lbry.settings_get().then(settings => {
dispatch({
type: types.DAEMON_SETTINGS_RECEIVED,
data: {
2017-06-06 23:19:12 +02:00
settings,
},
});
});
};
2017-06-06 06:21:55 +02:00
}
2017-06-28 09:12:01 +02:00
export function doSetClientSetting(key, value) {
return {
type: types.CLIENT_SETTING_CHANGED,
data: {
key,
value,
},
};
}
2017-08-08 11:36:14 +02:00
2017-08-24 18:11:39 +02:00
export function doGetThemes() {
return function(dispatch, getState) {
2017-09-07 02:52:34 +02:00
const dir = `${remote.app.getAppPath()}/dist/themes`;
readdir(dir, (error, files) => {
if (!error) {
dispatch(
doSetClientSetting(
settings.THEMES,
files
.filter(file => extname(file) === ".css")
.map(file => file.replace(".css", ""))
)
);
} else {
dispatch(doAlertError(error));
}
});
2017-08-26 21:04:44 +02:00
};
2017-08-24 20:28:05 +02:00
}
2017-08-26 21:04:44 +02:00
2017-08-19 20:03:51 +02:00
export function doDownloadLanguage(langFile) {
2017-08-08 11:36:14 +02:00
return function(dispatch, getState) {
const destinationPath = app.i18n.directory + "/" + langFile;
2017-08-19 20:03:51 +02:00
const language = langFile.replace(".json", "");
const req = http.get(
{
headers: {
"Content-Type": "text/html",
},
host: "i18n.lbry.io",
path: `/langs/${langFile}`,
},
response => {
if (response.statusCode === 200) {
const file = fs.createWriteStream(destinationPath);
file.on("error", errorHandler);
2017-08-08 11:36:14 +02:00
file.on("finish", () => {
file.close();
// push to our local list
dispatch({
type: types.DOWNLOAD_LANGUAGE_SUCCEEDED,
2017-08-19 20:03:51 +02:00
data: { language: language },
2017-08-08 11:36:14 +02:00
});
});
response.pipe(file);
2017-08-19 20:03:51 +02:00
} else {
errorHandler(new Error("Language request failed."));
2017-08-08 11:36:14 +02:00
}
2017-08-19 20:03:51 +02:00
}
);
2017-08-08 11:36:14 +02:00
2017-08-19 20:03:51 +02:00
const errorHandler = err => {
fs.unlink(destinationPath, () => {}); // Delete the file async. (But we don't check the result)
2017-08-08 11:36:14 +02:00
2017-08-19 20:03:51 +02:00
dispatch({
type: types.DOWNLOAD_LANGUAGE_FAILED,
data: { language },
});
};
2017-08-08 11:36:14 +02:00
2017-08-19 20:03:51 +02:00
req.setTimeout(30000, function() {
req.abort();
2017-08-08 11:36:14 +02:00
});
2017-08-19 20:03:51 +02:00
req.on("error", errorHandler);
req.end();
2017-08-08 11:36:14 +02:00
};
}
export function doDownloadLanguages() {
return function(dispatch, getState) {
2017-08-30 16:49:13 +02:00
//temporarily disable i18n so I can get a working build out -- Jeremy
return;
if (!fs.existsSync(app.i18n.directory)) {
fs.mkdirSync(app.i18n.directory);
2017-08-08 11:36:14 +02:00
}
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
} else {
throw new Error(
__("The list of available languages could not be retrieved.")
);
}
}
function parseJSON(response) {
return response.json();
}
return fetch("http://i18n.lbry.io")
.then(checkStatus)
.then(parseJSON)
.then(files => {
const actions = files.map(doDownloadLanguage);
dispatch(batchActions(...actions));
});
2017-08-19 20:03:51 +02:00
};
}
export function doChangeLanguage(language) {
return function(dispatch, getState) {
2017-09-07 03:53:42 +02:00
dispatch(doSetClientSetting(settings.LANGUAGE, language));
2017-08-19 20:03:51 +02:00
app.i18n.setLocale(language);
2017-08-08 11:36:14 +02:00
};
}