/*
*
* Copyright © 2015-2016 Antergos
*
* greeter.js
*
* This file is part of lightdm-webkit-theme-antergos
*
* lightdm-webkit-theme-antergos 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 any later version.
*
* lightdm-webkit-theme-antergos 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.
*
* The following additional terms are in effect as per Section 7 of this license:
*
* The preservation of all legal notices and author attributions in
* the material or in the Appropriate Legal Notices displayed
* by works containing it is required.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
`);
}
/**
* Get a key's value from localStorage. Keys can have two or more parts.
* For example: "ant:user:john:session".
*
* @param {...string} key_parts - Strings that are combined to form the key.
*/
cache_get(...key_parts) {
var key = `ant`;
for (var part of key_parts) {
key += `:${ part }`;
}
return localStorage.getItem(key);
}
/**
* Set a key's value in localStorage. Keys can have two or more parts.
* For example: "ant:user:john:session".
*
* @param {string} value - The value to set.
* @param {...string} key_parts - Strings that are combined to form the key.
*/
cache_set(value, ...key_parts) {
var key = `ant`;
for (var part of key_parts) {
key += `:${ part }`;
}
return localStorage.setItem(key, value);
}
/**
* Get some values from `lightdm-webkit2-greeter.conf` and save them for later.
*/
init_config_values() {
let logo = '',
user_image = '',
background_images = [],
background_images_dir = '';
if ('undefined' !== typeof config) {
if (this instanceof AntergosTheme) {
logo = config.get_str('branding', 'logo') || '';
user_image = config.get_str('branding', 'user_image') || '';
} else if (this instanceof AntergosBackgroundManager) {
background_images_dir = config.get_str('branding', 'background_images') || '';
if (background_images_dir) {
background_images = greeterutil.dirlist(background_images_dir) || [];
}
if (background_images.length) {
let images = [];
for (var file of background_images) {
if (file.match(/(png|PNG)|(jpg|JPEG)|(bmp|BMP)/)) {
images.push(file);
}
}
background_images = images;
}
}
}
this.logo = logo;
this.user_image = user_image;
this.background_images = background_images;
this.background_images_dir = background_images_dir;
}
}
/**
* This class handles the theme's background switcher.
*/
class AntergosBackgroundManager extends GreeterThemeComponent {
constructor() {
super();
if (null === _bg_self) {
_bg_self = this;
}
this.current_background = this.cache_get('background_manager', 'current_background');
if (!this.background_images_dir.length || !this.background_images.length) {
this.log('AntergosBackgroundManager: [ERROR] No background images detected.');
$('.header').fadeTo(300, 0.5, function () {
$('.header').css("background", '#000000');
}).fadeTo(300, 1);
}
return _bg_self;
}
initialize() {
if (!this.current_background) {
// For backwards compatibility
if (null !== localStorage.getItem('bgsaved') && '0' === localStorage.getItem('bgrandom')) {
this.current_background = localStorage.getItem('bgsaved');
this.cache_set(this.current_background, 'background_manager', 'current_background');
localStorage.removeItem('bgrandom');
localStorage.removeItem('bgsaved');
} else if ('0' === localStorage.getItem('bgrandom')) {
this.current_background = this.get_random_image();
this.cache_set('true', 'background_manager', 'random_background');
localStorage.removeItem('bgrandom');
}
}
if (!this.current_background) {
// For current and future versions
let current_background = this.cache_get('background_manager', 'current_background'),
random_background = this.cache_get('background_manager', 'random_background');
if ('true' === random_background || !current_background) {
current_background = this.get_random_image();
}
this.current_background = current_background;
}
this.do_background();
}
do_background() {
$('.header').fadeTo(300, 0.5, function () {
let tpl = `url('file://${ this.current_background }')`;
$('.header').css("background-image", tpl);
}).fadeTo(300, 1);
}
get_random_image() {
let random_bg;
random_bg = Math.floor(Math.random() * this.background_images.length);
return this.background_images[random_bg];
}
setup_background_thumbnails() {
if (this.background_images.length) {
for (var image_file of this.background_images) {
let $link = $(''),
$img_el = $link.children('img'),
tpl = `file://${ image_file }`;
$link.addClass('bg clearfix').attr('data-img', tpl);
$img_el.attr('src', tpl);
$link.appendTo($('.bgs')).click(this.background_selected_handler);
}
}
}
background_selected_handler(event) {
let img = $(this).attr('data-img');
if ('random' === img) {
this.cache_set('true', 'background_manager', 'randmom_background');
img = this.get_random_image();
}
this.cache_set(img, 'background_manager', 'current_background');
this.current_background = img;
this.do_background();
}
}
/**
* This is the theme's main class object. It contains almost all the theme's logic.
*/
class AntergosTheme extends GreeterThemeComponent {
constructor() {
super();
if (null === _self) {
_self = this;
}
this.tux = 'img/antergos-logo-user.png';
this.user_list_visible = false;
this.auth_pending = false;
this.selected_user = null;
this.$user_list = $('#user-list2');
this.$session_list = $('#sessions');
this.$clock_container = $('#collapseOne');
this.$clock = $("#current_time");
this.$actions_container = $("#actionsArea");
this.$msg_area_container = $('#statusArea');
this.$msg_area = $('#showMsg');
this.background_manager = new AntergosBackgroundManager();
this.background_manager.initialize();
this.initialize();
return _self;
}
initialize() {
this.prepare_translations();
this.do_static_translations();
this.initialize_clock();
this.prepare_login_panel_header();
this.prepare_user_list();
this.prepare_session_list();
this.prepare_system_action_buttons();
$("#login").addClass("in");
this.register_callbacks();
this.background_manager.setup_background_thumbnails();
}
/**
* Register callbacks for the LDM Greeter as well as any others that haven't been registered
* elsewhere.
*/
register_callbacks() {
var events = 'shown.bs.collapse, hidden.bs.collapse';
this.$user_list.parents('.collapse').on(events, this.user_list_collapse_handler);
$(document).keydown(this.key_press_handler);
$('.cancel_auth').click(this.cancel_authentication);
$('.submit_passwd').click(this.submit_password);
window.show_prompt = this.show_prompt;
window.show_message = this.show_message;
window.start_authentication = this.start_authentication;
window.cancel_authentication = this.cancel_authentication;
window.authentication_complete = this.authentication_complete;
window.autologin_timer_expired = this.cancel_authentication;
}
/**
* Initialize the user list.
*/
prepare_user_list() {
var template;
// Loop through the array of LightDMUser objects to create our user list.
for (var user of lightdm.users) {
var last_session = this.cache_get('user', user.name, 'session'),
image_src = user.image.length ? user.image : this.user_image;
if (null === last_session) {
// For backwards compatibility
last_session = localStorage.getItem(user.name);
if (null === last_session) {
// This user has never logged in before let's enable the system's default
// session.
last_session = lightdm.default_session;
}
this.cache_set(last_session, 'user', user.name, 'session');
}
this.log(`Last session for ${ user.name } was: ${ last_session }`);
template = `
${ user.display_name }
`;
// Register event handler here so we don't have to iterate over the users again later.
$(template).appendTo(this.$user_list).click(this.start_authentication).on('error.antergos', this.user_image_error_handler);
} // END for ( var user of lightdm.users )
if ($(this.$user_list).children().length > 3) {
// Make the user list two columns instead of one.
$(this.$user_list).css('column-count', '2').parent().css('max-width', '85%');
}
}
/**
* Initialize the session selection dropdown.
*/
prepare_session_list() {
// Loop through the array of LightDMSession objects to create our session list.
for (var session of lightdm.sessions) {
var css_class = session.name.replace(/ /g, ''),
template;
this.log(`Adding ${ session.name } to the session list...`);
template = `