<commit_before>
/*
 *  Guacamole - Clientless Remote Desktop
 *  Copyright (C) 2010  <NAME>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * General set of UI elements and UI-related functions regarding user login and
 * connection management.
 */
var GuacamoleRootUI = {

    "sections": {
        "login_form"         : document.getElementById("login-form"),
        "recent_connections" : document.getElementById("recent-connections"),
        "all_connections"    : document.getElementById("all-connections"),
        "all_connections_buttons" : document.getElementById("all-connections-buttons")
    },

    "messages": {
        "login_error"           : document.getElementById("login-error"),
        "no_recent_connections" : document.getElementById("no-recent")
    },

    "fields": {
        "username"  : document.getElementById("username"),
        "password"  : document.getElementById("password"),
        "clipboard" : document.getElementById("clipboard")
    },
    
    "buttons": {
        "login"  : document.getElementById("login"),
        "logout" : document.getElementById("logout"),
        "manage" : document.getElementById("manage")
    },

    "settings": {
        "auto_fit"      : document.getElementById("auto-fit"),
        "disable_sound" : document.getElementById("disable-sound")
    },

    "views": {
        "login"       : document.getElementById("login-ui"),
        "connections" : document.getElementById("connection-list-ui")
    },

    "session_state" :  new GuacamoleSessionState()

};

/**
 * Attempts to login the given user using the given password, throwing an
 * error if the process fails.
 * 
 * @param {String} username The name of the user to login as.
 * @param {String} password The password to use to authenticate the user.
 */
GuacamoleRootUI.login = function(username, password) {

    // Get parameters from query string
    var parameters = window.location.search.substring(1);

    // Get username and password from form
    var data =
           "username=" + encodeURIComponent(username)
        + "&password=" + encodeURIComponent(password)

    // Include query parameters in submission data
    if (parameters) data += "&" + parameters;

    // Log in
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "login", false);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(data);

    // Handle failures
    if (xhr.status != 200)
        throw new Error("Invalid login");

};

/**
 * Set of all thumbnailed connections, indexed by ID.
 */
GuacamoleRootUI.thumbnailConnections = {};

/**
 * Set of all connections, indexed by ID.
 */
GuacamoleRootUI.connections = {};

/**
 * Adds the given connection to the recent connections list.
 */
GuacamoleRootUI.addRecentConnection = function(connection) {

    // Add connection object to list of thumbnailed connections
    GuacamoleRootUI.thumbnailConnections[connection.connection.id] =
        connection;
    
    // Add connection to recent list
    GuacamoleRootUI.sections.recent_connections.appendChild(
        connection.getElement());

    // Hide "No recent connections" message
    GuacamoleRootUI.messages.no_recent_connections.style.display = "none";

};


/**
 * Resets the interface such that the login UI is displayed if
 * the user is not authenticated (or authentication fails) and
 * the connection list UI (or the client for the only available
 * connection, if there is only one) is displayed if the user is
 * authenticated.
 */
GuacamoleRootUI.reset = function() {

    // Get parameters from query string
    var parameters = window.location.search.substring(1);

    function hasEntry(object) {
        for (var name in object)
            return true;
        return false;
    }

    // Read connections
    var connections;
    try {
        connections = GuacamoleService.Connections.list(parameters);

        // Show admin elements if admin permissions available
        var permissions = GuacamoleService.Permissions.list();
        if (permissions.administer
            || permissions.create_connection
            || permissions.create_user
            || hasEntry(permissions.update_user)
            || hasEntry(permissions.remove_user)
            || hasEntry(permissions.administer_user)
            || hasEntry(permissions.update_connection)
            || hasEntry(permissions.remove_connection)
            || hasEntry(permissions.administer_connection))
                GuacUI.addClass(document.body, "admin");
            else
                GuacUI.removeClass(document.body, "admin");

    }
    catch (e) {

        // Show login UI if unable to get connections
        GuacamoleRootUI.views.login.style.display = "";
        GuacamoleRootUI.views.connections.style.display = "none";

        return;

    }

    // Create pager for connections
    var connection_pager = new GuacUI.Pager(GuacamoleRootUI.sections.all_connections);
    connection_pager.page_capacity = 20;

    // Add connection icons
    for (var i=0; i<connections.length; i++) {

        // Add connection to set
        GuacamoleRootUI.connections[connections[i].id] = connections[i];

        // Get connection element
        var connection = new GuacUI.Connection(connections[i]);

        // If screenshot present, add to recent connections
        if (connection.hasThumbnail())
            GuacamoleRootUI.addRecentConnection(connection);

        // Add connection to connection list
        connection_pager.addElement(
            new GuacUI.Connection(connections[i]).getElement());

    }

    // Add buttons if more than one page
    if (connection_pager.last_page != 0)<commit_msg>Handle connection paths, add groups/connections as necessary when building root UI connection list.<commit_after>
/*
 *  Guacamole - Clientless Remote Desktop
 *  Copyright (C) 2010  <NAME>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * General set of UI elements and UI-related functions regarding user login and
 * connection management.
 */
var GuacamoleRootUI = {

    "sections": {
        "login_form"         : document.getElementById("login-form"),
        "recent_connections" : document.getElementById("recent-connections"),
        "all_connections"    : document.getElementById("all-connections"),
        "all_connections_buttons" : document.getElementById("all-connections-buttons")
    },

    "messages": {
        "login_error"           : document.getElementById("login-error"),
        "no_recent_connections" : document.getElementById("no-recent")
    },

    "fields": {
        "username"  : document.getElementById("username"),
        "password"  : document.getElementById("password"),
        "clipboard" : document.getElementById("clipboard")
    },
    
    "buttons": {
        "login"  : document.getElementById("login"),
        "logout" : document.getElementById("logout"),
        "manage" : document.getElementById("manage")
    },

    "settings": {
        "auto_fit"      : document.getElementById("auto-fit"),
        "disable_sound" : document.getElementById("disable-sound")
    },

    "views": {
        "login"       : document.getElementById("login-ui"),
        "connections" : document.getElementById("connection-list-ui")
    },

    "session_state" :  new GuacamoleSessionState()

};

/**
 * Attempts to login the given user using the given password, throwing an
 * error if the process fails.
 * 
 * @param {String} username The name of the user to login as.
 * @param {String} password The password to use to authenticate the user.
 */
GuacamoleRootUI.login = function(username, password) {

    // Get parameters from query string
    var parameters = window.location.search.substring(1);

    // Get username and password from form
    var data =
           "username=" + encodeURIComponent(username)
        + "&password=" + encodeURIComponent(password)

    // Include query parameters in submission data
    if (parameters) data += "&" + parameters;

    // Log in
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "login", false);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(data);

    // Handle failures
    if (xhr.status != 200)
        throw new Error("Invalid login");

};

/**
 * Set of all thumbnailed connections, indexed by ID.
 */
GuacamoleRootUI.thumbnailConnections = {};

/**
 * Set of all connections, indexed by ID.
 */
GuacamoleRootUI.connections = {};

/**
 * Adds the given connection to the recent connections list.
 */
GuacamoleRootUI.addRecentConnection = function(connection) {

    // Add connection object to list of thumbnailed connections
    GuacamoleRootUI.thumbnailConnections[connection.connection.id] =
        connection;
    
    // Add connection to recent list
    GuacamoleRootUI.sections.recent_connections.appendChild(
        connection.getElement());

    // Hide "No recent connections" message
    GuacamoleRootUI.messages.no_recent_connections.style.display = "none";

};


/**
 * Resets the interface such that the login UI is displayed if
 * the user is not authenticated (or authentication fails) and
 * the connection list UI (or the client for the only available
 * connection, if there is only one) is displayed if the user is
 * authenticated.
 */
GuacamoleRootUI.reset = function() {

    // Get parameters from query string
    var parameters = window.location.search.substring(1);

    function hasEntry(object) {
        for (var name in object)
            return true;
        return false;
    }

    // Read connections
    var connections;
    try {
        connections = GuacamoleService.Connections.list(parameters);

        // Show admin elements if admin permissions available
        var permissions = GuacamoleService.Permissions.list();
        if (permissions.administer
            || permissions.create_connection
            || permissions.create_user
            || hasEntry(permissions.update_user)
            || hasEntry(permissions.remove_user)
            || hasEntry(permissions.administer_user)
            || hasEntry(permissions.update_connection)
            || hasEntry(permissions.remove_connection)
            || hasEntry(permissions.administer_connection))
                GuacUI.addClass(document.body, "admin");
            else
                GuacUI.removeClass(document.body, "admin");

    }
    catch (e) {

        // Show login UI if unable to get connections
        GuacamoleRootUI.views.login.style.display = "";
        GuacamoleRootUI.views.connections.style.display = "none";

        return;

    }

    // Associative array of all existing groups
    var groups = {};

    // Create pager for connections
    var connection_pager = new GuacUI.Pager(GuacamoleRootUI.sections.all_connections);
    connection_pager.page_capacity = 20;

    // Add connection icons
    for (var i=0; i<connections.length; i++) {

        // Add connection to set
        var connection = connections[i];
        GuacamoleRootUI.connections[connection.id] = connection;

        // Get connection element
        var recent_connection = new GuacUI.Connection(connection);

        // If screenshot present, add to recent connections
        if (recent_connection.hasThumbnail())
            GuacamoleRootUI.addRecentConnection(recent_connection);

        // Construct group hierarchy, creating new group components as
        // necessary
        var parent_group = null;
        var path = "";
        for (var j=0; j<connection.path.length; j++) {

            // Pull name, update current path
            var name = connection.path[j];
            path += "/" + name;

            // Pull group from path
            var group = groups[path];

            // If path not yet defined, create it
            if (!group) {
                
                groups[path] = group = new GuacUI.ListGroup(name);

                // Add new group to parent group, or to the list directly if
                // no parent
                if (parent_group)
                    parent_group.addElement(group.getElement());
                else
                    connection_pager.addElement(group.getElement());

            }

            // Save current group as the parent of the next group/connection
            parent_group = group;

        }

        // Add connection to connection list or parent group
        var guacui_connection = new GuacUI.Connection(connection);
        if (parent_group)
            parent_group.addElement(guacui_connection.getElement());
        else
            connection_pager.addElement(guacui_connection.getElement());

    }

    // Add buttons if more than one page
    if (connection_pager.last_page != 0)
{'fraction_non_alphanumeric': 0.10030004286326619, 'fraction_numerical': 0.002071724532076011, 'mean_word_length': 2.6926932207860723, 'pattern_counts': {'":': 12, '<': 10, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1359606', 'n_tokens_mistral': 3780, 'n_tokens_neox': 3608, 'n_words': 1077}