Blame src/util.js

rpm-build 858c0f
// -*- Mode: js; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*-
rpm-build 858c0f
//
rpm-build 858c0f
// Copyright (c) 2013 Giovanni Campagna <scampa.giovanni@gmail.com>
rpm-build 858c0f
//
rpm-build 858c0f
// Redistribution and use in source and binary forms, with or without
rpm-build 858c0f
//  modification, are permitted provided that the following conditions are met:
rpm-build 858c0f
//   * Redistributions of source code must retain the above copyright
rpm-build 858c0f
//     notice, this list of conditions and the following disclaimer.
rpm-build 858c0f
//   * Redistributions in binary form must reproduce the above copyright
rpm-build 858c0f
//     notice, this list of conditions and the following disclaimer in the
rpm-build 858c0f
//     documentation and/or other materials provided with the distribution.
rpm-build 858c0f
//   * Neither the name of the GNOME Foundation nor the
rpm-build 858c0f
//     names of its contributors may be used to endorse or promote products
rpm-build 858c0f
//     derived from this software without specific prior written permission.
rpm-build 858c0f
//
rpm-build 858c0f
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
rpm-build 858c0f
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
rpm-build 858c0f
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
rpm-build 858c0f
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
rpm-build 858c0f
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
rpm-build 858c0f
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
rpm-build 858c0f
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
rpm-build 858c0f
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
rpm-build 858c0f
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
rpm-build 858c0f
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
rpm-build 858c0f
rpm-build 858c0f
const Gdk = imports.gi.Gdk;
rpm-build 858c0f
const Gio = imports.gi.Gio;
rpm-build 858c0f
const GObject = imports.gi.GObject;
rpm-build 858c0f
const Gtk = imports.gi.Gtk;
rpm-build 858c0f
const Gc = imports.gi.Gc;
rpm-build 858c0f
const Lang = imports.lang;
rpm-build 858c0f
const Params = imports.params;
rpm-build 858c0f
const System = imports.system;
rpm-build 858c0f
rpm-build 858c0f
function loadUI(resourcePath, objects) {
rpm-build 858c0f
    let ui = new Gtk.Builder();
rpm-build 858c0f
rpm-build 858c0f
    if (objects) {
rpm-build 858c0f
        for (let o in objects)
rpm-build 858c0f
            ui.expose_object(o, objects[o]);
rpm-build 858c0f
    }
rpm-build 858c0f
rpm-build 858c0f
    ui.add_from_resource(resourcePath);
rpm-build 858c0f
    return ui;
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function loadStyleSheet(resource) {
rpm-build 858c0f
    let provider = new Gtk.CssProvider();
rpm-build 858c0f
    provider.load_from_file(Gio.File.new_for_uri('resource://' + resource));
rpm-build 858c0f
    Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
rpm-build 858c0f
                                             provider,
rpm-build 858c0f
                                             Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function initActions(actionMap, simpleActionEntries, context) {
rpm-build 858c0f
    simpleActionEntries.forEach(function(entry) {
rpm-build 858c0f
        let filtered = Params.filter(entry, { activate: null,
rpm-build 858c0f
                                              state_changed: null,
rpm-build 858c0f
                                              context: null });
rpm-build 858c0f
        let action = new Gio.SimpleAction(entry);
rpm-build 858c0f
rpm-build 858c0f
        let context = filtered.context || actionMap;
rpm-build 858c0f
        if (filtered.activate)
rpm-build 858c0f
            action.connect('activate', filtered.activate.bind(context));
rpm-build 858c0f
        if (filtered.state_changed)
rpm-build 858c0f
            action.connect('state-changed', filtered.state_changed.bind(context));
rpm-build 858c0f
rpm-build 858c0f
        actionMap.add_action(action);
rpm-build 858c0f
    });
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function arrayEqual(one, two) {
rpm-build 858c0f
    if (one.length != two.length)
rpm-build 858c0f
        return false;
rpm-build 858c0f
rpm-build 858c0f
    for (let i = 0; i < one.length; i++)
rpm-build 858c0f
        if (one[i] != two[i])
rpm-build 858c0f
            return false;
rpm-build 858c0f
rpm-build 858c0f
    return true;
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function getSettings(schemaId, path) {
rpm-build 858c0f
    const GioSSS = Gio.SettingsSchemaSource;
rpm-build 858c0f
    let schemaSource;
rpm-build 858c0f
rpm-build 858c0f
    if (!pkg.moduledir.startsWith('resource://')) {
rpm-build 858c0f
        // Running from the source tree
rpm-build 858c0f
        schemaSource = GioSSS.new_from_directory(pkg.pkgdatadir,
rpm-build 858c0f
                                                 GioSSS.get_default(),
rpm-build 858c0f
                                                 false);
rpm-build 858c0f
    } else {
rpm-build 858c0f
        schemaSource = GioSSS.get_default();
rpm-build 858c0f
    }
rpm-build 858c0f
rpm-build 858c0f
    let schemaObj = schemaSource.lookup(schemaId, true);
rpm-build 858c0f
    if (!schemaObj) {
rpm-build 858c0f
        log('Missing GSettings schema ' + schemaId);
rpm-build 858c0f
        System.exit(1);
rpm-build 858c0f
    }
rpm-build 858c0f
rpm-build 858c0f
    if (path === undefined)
rpm-build 858c0f
        return new Gio.Settings({ settings_schema: schemaObj });
rpm-build 858c0f
    else
rpm-build 858c0f
        return new Gio.Settings({ settings_schema: schemaObj,
rpm-build 858c0f
                                  path: path });
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function loadIcon(iconName, size) {
rpm-build 858c0f
    let theme = Gtk.IconTheme.get_default();
rpm-build 858c0f
rpm-build 858c0f
    return theme.load_icon(iconName,
rpm-build 858c0f
                           size,
rpm-build 858c0f
                           Gtk.IconLookupFlags.GENERIC_FALLBACK);
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function assertEqual(one, two) {
rpm-build 858c0f
    if (one != two)
rpm-build 858c0f
        throw Error('Assertion failed: ' + one + ' != ' + two);
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function assertNotEqual(one, two) {
rpm-build 858c0f
    if (one == two)
rpm-build 858c0f
        throw Error('Assertion failed: ' + one + ' == ' + two);
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function capitalizeWord(w) {
rpm-build 858c0f
    if (w.length > 0)
rpm-build 858c0f
        return w[0].toUpperCase() + w.slice(1).toLowerCase()
rpm-build 858c0f
    return w;
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function capitalize(s) {
rpm-build 858c0f
    return s.split(/\s+/).map(function(w) {
rpm-build 858c0f
        let acronyms = ["CJK"];
rpm-build 858c0f
        if (acronyms.indexOf(w) > -1)
rpm-build 858c0f
            return w;
rpm-build 858c0f
        let prefixes = ["IDEOGRAPH-", "SELECTOR-"];
rpm-build 858c0f
        for (let index in prefixes) {
rpm-build 858c0f
            let prefix = prefixes[index];
rpm-build 858c0f
            if (w.startsWith(prefix))
rpm-build 858c0f
                return capitalizeWord(prefix) + w.slice(prefix.length);
rpm-build 858c0f
        }
rpm-build 858c0f
        return capitalizeWord(w);
rpm-build 858c0f
    }).join(' ');
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function toCodePoint(s) {
rpm-build 858c0f
    let codePoint = s.charCodeAt(0);
rpm-build 858c0f
    if (codePoint >= 0xD800 && codePoint <= 0xDBFF) {
rpm-build 858c0f
        let high = codePoint;
rpm-build 858c0f
        let low = s.charCodeAt(1);
rpm-build 858c0f
        codePoint = 0x10000 + (high - 0xD800) * 0x400 + (low - 0xDC00);
rpm-build 858c0f
    }
rpm-build 858c0f
rpm-build 858c0f
    return codePoint;
rpm-build 858c0f
}
rpm-build 858c0f
rpm-build 858c0f
function searchResultToArray(result) {
rpm-build 858c0f
    let characters = [];
rpm-build 858c0f
    for (let index = 0; index < result.len; index++) {
rpm-build 858c0f
        characters.push(Gc.search_result_get(result, index));
rpm-build 858c0f
    }
rpm-build 858c0f
    return characters;
rpm-build 858c0f
}