FreeTube/src/js/settings.js

427 lines
11 KiB
JavaScript
Raw Normal View History

2018-03-02 04:48:12 +01:00
/*
This file is part of FreeTube.
FreeTube is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FreeTube is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FreeTube. If not, see <http://www.gnu.org/licenses/>.
*/
/*
2018-03-06 05:22:12 +01:00
* A file for functions used for settings.
*/
2018-03-02 04:48:12 +01:00
// To any third party devs that fork the project, please be ethical and change the API keys.
const apiKeyBank = ['AIzaSyC9E579nh_qqxg6BH4xIce3k_7a9mT4uQc', 'AIzaSyCKplYT6hZIlm2O9FbWTi1G7rkpsLNTq78', 'AIzaSyAE5xzh5GcA_tEDhXmMFd1pEzrL-W7z51E', 'AIzaSyDoFzqwuO9l386eF6BmNkVapjiTJ93CBy4', 'AIzaSyBljfZFPioB0TRJAj-0LS4tlIKl2iucyY4'];
2018-03-02 04:48:12 +01:00
/**
2018-03-06 05:22:12 +01:00
* Display the settings screen to the user.
*
* @return {Void}
*/
function showSettings() {
2018-03-02 04:48:12 +01:00
clearMainContainer();
startLoadingAnimation();
2018-03-02 04:48:12 +01:00
let isChecked = '';
let key = '';
/*
2018-03-06 05:22:12 +01:00
* Check the settings database for the user's current settings. This is so the
* settings page has the correct toggles related when it is rendered.
*/
2018-03-02 04:48:12 +01:00
settingsDb.find({}, (err, docs) => {
docs.forEach((setting) => {
switch (setting['_id']) {
case 'apiKey':
if (apiKeyBank.indexOf(setting['value']) == -1) {
2018-03-02 04:48:12 +01:00
key = setting['value'];
}
break;
case 'theme':
2018-03-06 05:22:12 +01:00
if (currentTheme == '') {
2018-03-02 04:48:12 +01:00
currentTheme = setting['value'];
}
}
});
// Grab the settings.html template to prepare for rendering
const settingsTemplate = require('./templates/settings.html')
mustache.parse(settingsTemplate);
const rendered = mustache.render(settingsTemplate, {
isChecked: isChecked,
key: key,
2018-03-02 04:48:12 +01:00
});
// Render template to application
$('#main').html(rendered);
stopLoadingAnimation();
// Check / uncheck the switch depending on the user's settings.
if (currentTheme === 'light') {
document.getElementById('themeSwitch').checked = false;
} else {
document.getElementById('themeSwitch').checked = true;
}
if (useTor) {
document.getElementById('torSwitch').checked = true;
} else {
document.getElementById('torSwitch').checked = false;
}
2018-03-02 04:48:12 +01:00
});
}
/**
2018-03-06 05:22:12 +01:00
* Check the user's default settings. Set the the default settings if none are found.
*
* @return {Void}
*/
function checkDefaultSettings() {
// Grab a random API Key.
apiKey = apiKeyBank[Math.floor(Math.random() * apiKeyBank.length)];
let newSetting;
2018-03-06 05:22:12 +01:00
let settingDefaults = {
'theme': 'light',
'apiKey': apiKey,
'useTor': false
};
2018-03-06 05:22:12 +01:00
console.log(settingDefaults);
2018-03-02 04:48:12 +01:00
for (let key in settingDefaults){
settingsDb.find({_id: key}, (err, docs) => {
if (jQuery.isEmptyObject(docs)) {
newSetting = {
_id: key,
value: settingDefaults[key]
};
2018-03-02 04:48:12 +01:00
settingsDb.insert(newSetting);
2018-03-02 04:48:12 +01:00
if (key == 'theme'){
setTheme('light');
}
}
else{
switch (docs[0]['_id']) {
2018-03-02 04:48:12 +01:00
case 'theme':
setTheme(docs[0]['value']);
2018-03-02 04:48:12 +01:00
break;
case 'apiKey':
if (apiKeyBank.indexOf(docs[0]['value']) == -1) {
apiKey = docs[0]['value'];
2018-03-06 05:22:12 +01:00
}
2018-03-02 04:48:12 +01:00
break;
case 'useTor':
useTor = docs[0]['value'];
break;
2018-03-02 04:48:12 +01:00
default:
break;
}
}
});
}
2018-03-02 04:48:12 +01:00
}
/**
2018-03-06 05:22:12 +01:00
* Updates the settings based on what the user has changed.
*
* @return {Void}
*/
function updateSettings() {
let themeSwitch = document.getElementById('themeSwitch').checked;
let torSwitch = document.getElementById('torSwitch').checked;
let key = document.getElementById('api-key').value;
let theme = 'light';
2018-03-02 04:48:12 +01:00
apiKey = apiKeyBank[Math.floor(Math.random() * apiKeyBank.length)];
console.log(themeSwitch);
if (themeSwitch === true) {
theme = 'dark';
2018-03-02 04:48:12 +01:00
}
console.log(theme);
2018-03-02 04:48:12 +01:00
// Update default theme
2018-03-06 05:22:12 +01:00
settingsDb.update({
_id: 'theme'
}, {
value: theme
}, {}, function(err, numReplaced) {
2018-03-02 04:48:12 +01:00
console.log(err);
console.log(numReplaced);
});
// Update tor usage.
settingsDb.update({
_id: 'useTor'
}, {
value: torSwitch
}, {}, function(err, numReplaced) {
console.log(err);
console.log(numReplaced);
useTor = torSwitch;
});
2018-03-06 05:22:12 +01:00
if (key != '') {
settingsDb.update({
_id: 'apiKey'
}, {
value: key
}, {});
} else {
2018-03-02 04:48:12 +01:00
// To any third party devs that fork the project, please be ethical and change the API key.
2018-03-06 05:22:12 +01:00
settingsDb.update({
_id: 'apiKey'
}, {
value: apiKey
2018-03-06 05:22:12 +01:00
}, {});
2018-03-02 04:48:12 +01:00
}
showToast('Settings have been saved.');
}
/**
2018-03-06 05:22:12 +01:00
* Toggle back and forth with the current theme
*
* @param {boolean} themeValue - The value of the switch based on if it was turned on or not.
*
* @return {Void}
*/
function toggleTheme(themeValue) {
if (themeValue.checked === true) {
2018-03-02 04:48:12 +01:00
setTheme('dark');
currentTheme = 'dark';
2018-03-06 05:22:12 +01:00
} else {
2018-03-02 04:48:12 +01:00
setTheme('light');
currentTheme = 'light';
}
}
/**
2018-03-06 05:22:12 +01:00
* Set the theme of the application
*
* @param {string} option - The theme to be changed to.
*
* @return {Void}
*/
function setTheme(option) {
2018-03-02 04:48:12 +01:00
let cssFile;
const currentTheme = document.getElementsByTagName("link").item(1);
// Create a link element
const newTheme = document.createElement("link");
newTheme.setAttribute("rel", "stylesheet");
newTheme.setAttribute("type", "text/css");
// Grab the css file to be used.
switch (option) {
case 'light':
cssFile = './style/lightTheme.css';
document.getElementById('menuText').src = 'icons/textBlack.png';
document.getElementById('menuIcon').src = 'icons/iconBlack.png';
document.getElementById('menuButton').style.color = 'black';
2018-03-02 04:48:12 +01:00
break;
case 'dark':
cssFile = './style/darkTheme.css';
document.getElementById('menuText').src = 'icons/textColor.png';
document.getElementById('menuIcon').src = 'icons/iconColor.png';
document.getElementById('menuButton').style.color = 'white';
2018-03-02 04:48:12 +01:00
break;
default:
// Default to the light theme
cssFile = './style/lightTheme.css';
2018-03-02 04:48:12 +01:00
break;
}
newTheme.setAttribute("href", cssFile);
// Replace the current theme with the new theme
document.getElementsByTagName("head").item(0).replaceChild(newTheme, currentTheme);
}
2018-03-04 05:24:31 +01:00
/**
* Import Subscriptions from an OPML file.
*
* @param {string} subFile - The file location of the OPML file.
*
* @return {Void}
*/
function importOpmlSubs(json){
if(!json[0]['folder'].includes('YouTube')){
2018-03-04 05:24:31 +01:00
showToast('Invalid OPML File. Import is unsuccessful.');
return;
}
json.forEach((channel) => {
let channelId = channel['xmlurl'].replace('https://www.youtube.com/feeds/videos.xml?channel_id=', '');
addSubscription(channelId, false);
});
window.setTimeout(displaySubs, 1000);
showToast('Subscriptions have been imported!');
return;
}
2018-03-02 04:48:12 +01:00
/**
* Import a subscriptions file that the user provides.
*
* @return {Void}
*/
function importSubscriptions(){
const appDatabaseFile = localDataStorage + '/subscriptions.db';
// Open user's file browser. Only show .db files.
dialog.showOpenDialog({
properties: ['openFile'],
filters: [
{name: 'Database File', extensions: ['*']},
2018-03-02 04:48:12 +01:00
]
}, function(fileLocation){
if(typeof(fileLocation) === 'undefined'){
console.log('Import Aborted');
return;
}
2018-03-04 05:24:31 +01:00
console.log(fileLocation);
let i = fileLocation[0].lastIndexOf('.');
let fileType = (i < 0) ? '' : fileLocation[0].substr(i);
console.log(fileType);
/*if (fileType !== '.db'){
showToast('Incorrect filetype. Import was unsuccessful.');
2018-03-02 04:48:12 +01:00
return;
2018-03-04 05:24:31 +01:00
}*/
2018-03-02 04:48:12 +01:00
fs.readFile(fileLocation[0], function(readErr, data){
if(readErr){
showToast('Unable to read file. File may be corrupt or have invalid permissions.');
throw readErr;
}
2018-03-04 05:24:31 +01:00
if (data.includes("<opml")){
getOpml(data, function (error, json){
if (!error){
2018-03-04 05:24:31 +01:00
clearFile('subscriptions', false);
importOpmlSubs(json['children'][0]['children']);
}
});
return;
}
else if (fileType !== '.db'){
2018-03-04 05:24:31 +01:00
showToast('Incorrect file type. Import unsuccessful.');
return;
}
clearFile('subscriptions', false);
2018-03-02 04:48:12 +01:00
fs.writeFile(appDatabaseFile, data, function(writeErr){
if(writeErr){
showToast('Unable to create file. Please check your permissions and try again.');
throw writeErr;
}
showToast('Susbcriptions have been successfully imported. Please restart FreeTube for the changes to take effect.');
});
})
});
}
/**
2018-03-06 05:22:12 +01:00
* Export the susbcriptions database to a file.
*
* @return {Void}
*/
function exportSubscriptions() {
2018-03-02 04:48:12 +01:00
const appDatabaseFile = localDataStorage + '/subscriptions.db';
const date = new Date();
let dateMonth = date.getMonth() + 1;
if (dateMonth < 10){
dateMonth = '0' + dateMonth;
}
let dateDay = date.getDate();
if (dateDay < 10){
dateDay = '0' + dateDay;
}
const dateYear = date.getFullYear();
const dateString = 'freetube-subscriptions-' + dateYear + '-' + dateMonth + '-' + dateDay;
2018-03-02 04:48:12 +01:00
// Open user file browser. User gives location of file to be created.
dialog.showSaveDialog({
defaultPath: dateString,
2018-03-06 05:22:12 +01:00
filters: [{
name: 'Database File',
extensions: ['db']
}, ]
}, function(fileLocation) {
2018-03-02 04:48:12 +01:00
console.log(fileLocation);
2018-03-06 05:22:12 +01:00
if (typeof(fileLocation) === 'undefined') {
2018-03-02 04:48:12 +01:00
console.log('Export Aborted');
return;
}
2018-03-06 05:22:12 +01:00
fs.readFile(appDatabaseFile, function(readErr, data) {
if (readErr) {
2018-03-02 04:48:12 +01:00
throw readErr;
}
2018-03-06 05:22:12 +01:00
fs.writeFile(fileLocation, data, function(writeErr) {
if (writeErr) {
2018-03-02 04:48:12 +01:00
throw writeErr;
}
showToast('Susbcriptions have been successfully exported');
});
})
});
}
/**
* Clear out the data in a file.
*
* @param {string} type - The type of file to be cleared.
*/
function clearFile(type, showMessage = true){
2018-03-02 04:48:12 +01:00
console.log(type);
let dataBaseFile;
switch (type) {
case 'subscriptions':
dataBaseFile = localDataStorage + '/subscriptions.db';
break;
case 'history':
dataBaseFile = localDataStorage + '/videohistory.db';
break;
case 'saved':
dataBaseFile = localDataStorage + '/savedvideos.db';
break;
default:
showToast('Unknown file: ' + type)
return
}
// Replace data with an empty string.
2018-03-06 05:22:12 +01:00
fs.writeFile(dataBaseFile, '', function(err) {
if (err) {
2018-03-02 04:48:12 +01:00
throw err;
}
2018-03-04 05:24:31 +01:00
if (showMessage){
2018-03-04 05:24:31 +01:00
showToast('File has been cleared. Restart FreeTube to see the changes');
}
2018-03-02 04:48:12 +01:00
})
}