Blame src/searchProvider.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
// Copyright (C) 2015  Daiki Ueno <dueno@src.gnome.org>
rpm-build 858c0f
//
rpm-build 858c0f
// Gnome Weather is free software; you can redistribute it and/or modify
rpm-build 858c0f
// it under the terms of the GNU General Public License as published by the
rpm-build 858c0f
// Free Software Foundation; either version 2 of the License, or (at your
rpm-build 858c0f
// option) any later version.
rpm-build 858c0f
//
rpm-build 858c0f
// Gnome Weather is distributed in the hope that it will be useful, but
rpm-build 858c0f
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
rpm-build 858c0f
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
rpm-build 858c0f
// for more details.
rpm-build 858c0f
//
rpm-build 858c0f
// You should have received a copy of the GNU General Public License along
rpm-build 858c0f
// with Gnome Weather; if not, write to the Free Software Foundation,
rpm-build 858c0f
// Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
rpm-build 858c0f
rpm-build 858c0f
const Gdk = imports.gi.Gdk;
rpm-build 858c0f
const Gio = imports.gi.Gio;
rpm-build 858c0f
const GLib = imports.gi.GLib;
rpm-build 858c0f
const Lang = imports.lang;
rpm-build 858c0f
const Gc = imports.gi.Gc;
rpm-build 858c0f
const Util = imports.util;
rpm-build 858c0f
rpm-build 858c0f
const MAX_SEARCH_RESULTS = 100
rpm-build 858c0f
rpm-build 858c0f
const SearchProviderInterface = Gio.resources_lookup_data('/org/gnome/shell/ShellSearchProvider2.xml', 0).toArray().toString();
rpm-build 858c0f
rpm-build 858c0f
const SearchProvider = new Lang.Class({
rpm-build 858c0f
    Name: 'CharactersSearchProvider',
rpm-build 858c0f
rpm-build 858c0f
    _init: function(application) {
rpm-build 858c0f
        this._app = application;
rpm-build 858c0f
rpm-build 858c0f
        this._impl = Gio.DBusExportedObject.wrapJSObject(SearchProviderInterface, this);
rpm-build 858c0f
        this._cancellable = new Gio.Cancellable();
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    export: function(connection, path) {
rpm-build 858c0f
        return this._impl.export(connection, path);
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    unexport: function(connection) {
rpm-build 858c0f
        return this._impl.unexport_from_connection(connection);
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    _runQuery: function(keywords, invocation) {
rpm-build 858c0f
        this._cancellable.cancel();
rpm-build 858c0f
        this._cancellable.reset();
rpm-build 858c0f
rpm-build 858c0f
        let upper = keywords.map(String.toUpperCase);
rpm-build 858c0f
        let criteria = Gc.SearchCriteria.new_keywords(upper);
rpm-build 858c0f
        let context = new Gc.SearchContext({ criteria: criteria,
rpm-build 858c0f
                                             flags: Gc.SearchFlag.WORD });
rpm-build 858c0f
        context.search(
rpm-build 858c0f
            MAX_SEARCH_RESULTS,
rpm-build 858c0f
            this._cancellable,
rpm-build 858c0f
            Lang.bind(this, function(source_object, res, user_data) {
rpm-build 858c0f
                let characters = [];
rpm-build 858c0f
                try {
rpm-build 858c0f
                    let result = context.search_finish(res);
rpm-build 858c0f
                    characters = Util.searchResultToArray(result);
rpm-build 858c0f
                } catch (e) {
rpm-build 858c0f
                    log("Failed to search by keywords: " + e.message);
rpm-build 858c0f
                }
rpm-build 858c0f
                invocation.return_value(new GLib.Variant('(as)', [characters]));
rpm-build 858c0f
rpm-build 858c0f
                this._app.release();
rpm-build 858c0f
            }));
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    GetInitialResultSetAsync: function(params, invocation) {
rpm-build 858c0f
        this._app.hold();
rpm-build 858c0f
        this._runQuery(params[0], invocation);
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    GetSubsearchResultSetAsync: function(params, invocation) {
rpm-build 858c0f
        this._app.hold();
rpm-build 858c0f
        this._runQuery(params[1], invocation);
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    GetResultMetas: function(identifiers) {
rpm-build 858c0f
        this._app.hold();
rpm-build 858c0f
rpm-build 858c0f
        let ret = [];
rpm-build 858c0f
rpm-build 858c0f
        for (let i = 0; i < identifiers.length; i++) {
rpm-build 858c0f
            let character = identifiers[i];
rpm-build 858c0f
            let codePoint = Util.toCodePoint(character);
rpm-build 858c0f
            let codePointHex = codePoint.toString(16).toUpperCase();
rpm-build 858c0f
            let name = Gc.character_name(character);
rpm-build 858c0f
            if (name == null)
rpm-build 858c0f
                name = _("Unknown character name");
rpm-build 858c0f
            else
rpm-build 858c0f
                name = Util.capitalize(name);
rpm-build 858c0f
            let summary = _("U+%s, %s: %s").format(codePointHex,
rpm-build 858c0f
                                                   character,
rpm-build 858c0f
                                                   name);
rpm-build 858c0f
            ret.push({ name: new GLib.Variant('s', name),
rpm-build 858c0f
                       id: new GLib.Variant('s', identifiers[i]),
rpm-build 858c0f
                       description: new GLib.Variant('s', summary),
rpm-build 858c0f
                       icon: (new Gio.ThemedIcon({ name: 'gnome-characters' })).serialize(),
rpm-build 858c0f
                       clipboardText: new GLib.Variant('s', character)
rpm-build 858c0f
                     });
rpm-build 858c0f
        }
rpm-build 858c0f
rpm-build 858c0f
        this._app.release();
rpm-build 858c0f
rpm-build 858c0f
        return ret;
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    ActivateResult: function(id, terms, timestamp) {
rpm-build 858c0f
        let clipboard = Gc.gtk_clipboard_get();
rpm-build 858c0f
        clipboard.set_text(id, -1);
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    _getPlatformData: function(timestamp) {
rpm-build 858c0f
        let display = Gdk.Display.get_default();
rpm-build 858c0f
        let context = display.get_app_launch_context();
rpm-build 858c0f
        context.set_timestamp(timestamp);
rpm-build 858c0f
rpm-build 858c0f
        let app = Gio.DesktopAppInfo.new('org.gnome.Characters.desktop');
rpm-build 858c0f
        let id = context.get_startup_notify_id(app, []);
rpm-build 858c0f
        return {'desktop-startup-id': new GLib.Variant('s', id) };
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    _activateAction: function(action, parameter, timestamp) {
rpm-build 858c0f
        let wrappedParam;
rpm-build 858c0f
        if (parameter)
rpm-build 858c0f
            wrappedParam = [parameter];
rpm-build 858c0f
        else
rpm-build 858c0f
            wrappedParam = [];
rpm-build 858c0f
rpm-build 858c0f
        Gio.DBus.session.call('org.gnome.Characters',
rpm-build 858c0f
                              '/org/gnome/Characters',
rpm-build 858c0f
                              'org.freedesktop.Application',
rpm-build 858c0f
                              'ActivateAction',
rpm-build 858c0f
                              new GLib.Variant('(sava{sv})', [action, wrappedParam,
rpm-build 858c0f
                                                              this._getPlatformData(timestamp)]),
rpm-build 858c0f
                              null,
rpm-build 858c0f
                              Gio.DBusCallFlags.NONE,
rpm-build 858c0f
                              -1, null, Lang.bind(this, function(connection, result) {
rpm-build 858c0f
                                  try {
rpm-build 858c0f
                                      connection.call_finish(result);
rpm-build 858c0f
                                  } catch(e) {
rpm-build 858c0f
                                      log('Failed to launch application: ' + e.message);
rpm-build 858c0f
                                  }
rpm-build 858c0f
rpm-build 858c0f
                                  this._app.release();
rpm-build 858c0f
                              }));
rpm-build 858c0f
    },
rpm-build 858c0f
rpm-build 858c0f
    LaunchSearch: function(terms, timestamp) {
rpm-build 858c0f
        this._activateAction('search', new GLib.Variant('as', terms),
rpm-build 858c0f
                             timestamp);
rpm-build 858c0f
    }
rpm-build 858c0f
});