<commit_before>/*******************************************************************************
 * Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
 *
 * Licensed under the Apache License 2.0.
 * http://www.apache.org/licenses/LICENSE-2.0
 ******************************************************************************/

/* manage extension preferences */
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, brackets, Mustache, PathUtils */

define(function (require, exports, module) {
    'use strict';
    var PreferenceManager             = brackets.getModule('preferences/PreferencesManager'),
        ExtensionUtils                = brackets.getModule('utils/ExtensionUtils'),
        NodeDomain                    = brackets.getModule('utils/NodeDomain'),
        Dialogs                       = brackets.getModule('widgets/Dialogs'),
        StringUtils                   = brackets.getModule('utils/StringUtils'),
        SlyDomain                     = new NodeDomain('sly', ExtensionUtils.getModulePath(module, '../node/SlyDomain')),
        ProjectSettingsDialogTemplate = require('text!./project-settings-dialog.html'),
        Strings                       = require('strings'),
        slyPreferences                = PreferenceManager.getExtensionPrefs('sly'),
        defaults,
        validators = {},
        scopes = {};

    validators.serverUrl = function(url) {
        if (url === '') {
            return Strings.PROJECT_SETTING_SERVER_URL_ERROR_MISSING;
        }
        var obj = PathUtils.parseUrl(url);
        if (!obj) {
            return Strings.PROJECT_SETTING_SERVER_URL_ERROR_UNKNOWN;
        }
        if (obj.href.search(/^(http|https):\/\//i) !== 0) {
            return Strings.PROJECT_SETTING_SERVER_URL_ERROR_INVALID_PROTOCOL;
        }
        var index;
        if ((index = url.search(/[ \^\[\]\{\}<>\\"\?]+/)) !== -1) {
            return StringUtils.format(Strings.PROJECT_SETTING_SERVER_URL_ERROR_INVALID_CHAR, url[index]);
        }
        return '';
    }

    validators.remoteUser = function(user) {
        if (user === '') {
            return Strings.PROJECT_SETTING_REMOTE_USER_ERROR_EMPTY;
        }
        return '';
    }

    validators.remoteUserPassword = function(password) {
        if (password === '') {
            return Strings.PROJECT_SETTING_REMOTE_USER_PASSWORD_ERROR_EMPTY;
        }
        return '';
    }

    scopes.serverUrl = 'project';
    scopes.remoteUser = 'project';
    scopes.remoteUserPassword = '<PASSWORD>';


    /* get a preference */
    function get(pref) {
        if (!slyPreferences.get(pref) && defaults[pref]) {
            slyPreferences.set(pref, defaults[pref]);
            slyPreferences.save();
        }
        return slyPreferences.get(pref);
    }

    function getRemote() {
        return get('serverUrl');
    }

    function getRemoteUser() {
        return get('remoteUser');
    }

    function getRemotePassword() {
        return get('remoteUserPassword');
    }

    function getSyncedLanguages() {
        return get('syncedLanguages');
    }

    function load(SLYDictionary) {
        defaults = SLYDictionary.preferences;
        SlyDomain.exec('setRemote', getRemote(), getRemoteUser(), getRemotePassword());
    }

    function openProjectPreferences(errorMessage) {
        var dialog,
            templateVars,
            formData;
        templateVars = {
            Strings : Strings,
            serverUrl : getRemote(),<commit_msg>fix #13: Error with Sightly Export/Import when opened file not in jcr_root<commit_after>/*******************************************************************************
 * Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
 *
 * Licensed under the Apache License 2.0.
 * http://www.apache.org/licenses/LICENSE-2.0
 ******************************************************************************/

/* manage extension preferences */
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, brackets, Mustache, PathUtils */

define(function (require, exports, module) {
    'use strict';
    var PreferenceManager             = brackets.getModule('preferences/PreferencesManager'),
        ExtensionUtils                = brackets.getModule('utils/ExtensionUtils'),
        NodeDomain                    = brackets.getModule('utils/NodeDomain'),
        Dialogs                       = brackets.getModule('widgets/Dialogs'),
        StringUtils                   = brackets.getModule('utils/StringUtils'),
        SlyDomain                     = new NodeDomain('sly', ExtensionUtils.getModulePath(module, '../node/SlyDomain')),
        ProjectSettingsDialogTemplate = require('text!./project-settings-dialog.html'),
        Strings                       = require('strings'),
        slyPreferences                = PreferenceManager.getExtensionPrefs('sly'),
        defaults,
        validators = {},
        scopes = {};

    validators.serverUrl = function(url) {
        if (url === '') {
            return Strings.PROJECT_SETTING_SERVER_URL_ERROR_MISSING;
        }
        var obj = PathUtils.parseUrl(url);
        if (!obj) {
            return Strings.PROJECT_SETTING_SERVER_URL_ERROR_UNKNOWN;
        }
        if (obj.href.search(/^(http|https):\/\//i) !== 0) {
            return Strings.PROJECT_SETTING_SERVER_URL_ERROR_INVALID_PROTOCOL;
        }
        var index;
        if ((index = url.search(/[ \^\[\]\{\}<>\\"\?]+/)) !== -1) {
            return StringUtils.format(Strings.PROJECT_SETTING_SERVER_URL_ERROR_INVALID_CHAR, url[index]);
        }
        return '';
    }

    validators.remoteUser = function(user) {
        if (user === '') {
            return Strings.PROJECT_SETTING_REMOTE_USER_ERROR_EMPTY;
        }
        return '';
    }

    validators.remoteUserPassword = function(password) {
        if (password === '') {
            return Strings.PROJECT_SETTING_REMOTE_USER_PASSWORD_ERROR_EMPTY;
        }
        return '';
    }

    scopes.serverUrl = 'project';
    scopes.remoteUser = 'project';
    scopes.remoteUserPassword = '<PASSWORD>';
    scopes.syncedLanguages = 'user';


    /* get a preference */
    function get(pref) {
        var scope = scopes[pref] !== undefined ? scopes[pref] : 'default';
        var context = {
            location: {
                scope: scope
            },
            scopeOrder: ['project', 'user']
        };
        if (!slyPreferences.get(pref, context) && defaults[pref]) {
            slyPreferences.set(pref, defaults[pref], context);
            slyPreferences.save();
        }
        return slyPreferences.get(pref, context);
    }

    function getRemote() {
        return get('serverUrl');
    }

    function getRemoteUser() {
        return get('remoteUser');
    }

    function getRemotePassword() {
        return get('remoteUserPassword');
    }

    function getSyncedLanguages() {
        return get('syncedLanguages');
    }

    function load(SLYDictionary) {
        defaults = SLYDictionary.preferences;
        SlyDomain.exec('setRemote', getRemote(), getRemoteUser(), getRemotePassword());
    }

    function openProjectPreferences(errorMessage) {
        var dialog,
            templateVars,
            formData;
        templateVars = {
            Strings : Strings,
            serverUrl : getRemote(),
{'fraction_non_alphanumeric': 0.15957589585290566, 'fraction_numerical': 0.0037578848476714534, 'mean_word_length': 2.3492134831460674, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5913145', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1936, 'n_words': 363}