Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main.js #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 23 additions & 53 deletions src/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,30 @@

pkg.initGettext();
pkg.initFormat();
pkg.require({ 'Gdk': '3.0',
'Gio': '2.0',
'GLib': '2.0',
'GObject': '2.0',
'Gtk': '3.0',
'WebKit2': '4.0' });

const GObject = imports.gi.GObject;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const WebKit = imports.gi.WebKit2;
pkg.require({
'Gdk': '3.0',
'Gio': '2.0',
'GLib': '2.0',
'GObject': '2.0',
'Gtk': '3.0',
'WebKit2': '4.0'
});

const Util = imports.common.util;
const Window = imports.app.window;
const Service = imports.common.serviceproxy.Service;
const PreferenceAction = imports.app.prefs.PreferenceAction;
const { ImageCacher } = imports.app.imagecacher;
const { spawnService } = imports.app.servicelaunch;
const { GObject, Gio, GLib, Gtk, WebKit } = imports.gi;
const { Util, Window, Service, PreferenceAction } = imports.common;
const { ImageCacher, spawnService } = imports.app;

function initEnvironment() {
window.getApp = function() {
return Gio.Application.get_default();
};
window.getApp = () => Gio.Application.get_default();
}

const AlmondApplication = GObject.registerClass(
class AlmondApplication extends Gtk.Application {
_init() {
super._init({ application_id: pkg.name });

GLib.set_application_name(_("Almond"));
this._service = null;

this.cache = new ImageCacher();

this._activating = false;
}

Expand All @@ -65,18 +53,13 @@ class AlmondApplication extends Gtk.Application {

vfunc_startup() {
super.vfunc_startup();

Util.loadStyleSheet('/edu/stanford/Almond/application.css');

Util.initActions(this,
[{ name: 'quit',
activate: this._onQuit }]);

let webDataManager = new WebKit.WebsiteDataManager({
Util.initActions(this, [{ name: 'quit', activate: this._onQuit }]);
const webDataManager = new WebKit.WebsiteDataManager({
base_cache_directory: GLib.get_user_cache_dir() + '/almond/webview',
base_data_directory: GLib.get_user_config_dir() + '/almond/webview'
});
let webCookieManager = webDataManager.get_cookie_manager();
const webCookieManager = webDataManager.get_cookie_manager();
webCookieManager.set_accept_policy(WebKit.CookieAcceptPolicy.NO_THIRD_PARTY);
webCookieManager.set_persistent_storage(GLib.get_user_config_dir() + '/almond/webview/cookies.db',
WebKit.CookiePersistentStorage.SQLITE);
Expand All @@ -86,30 +69,22 @@ class AlmondApplication extends Gtk.Application {
}

vfunc_activate() {
var window = this.get_active_window();
if (window === null) {
let window = this.get_active_window();
if (!window) {
if (this._service === null) {
if (this._activating)
return;

if (this._activating) return;
this._activating = true;
this.hold();
new Service(Gio.DBus.session, 'edu.stanford.Almond.BackgroundService', '/edu/stanford/Almond/BackgroundService', (result, error) => {
this.release();
this._activating = false;
if (error)
throw error; // die
if (error) throw error;
this._service = result;

for (let pref of ['enable-voice-input', 'enable-voice-output'])
this.add_action(new PreferenceAction(this._service, pref, 'b'));
this.add_action(new PreferenceAction(this._service, 'sabrina-store-log', 'b', (fromjson) => {
return new GLib.Variant('b', fromjson.deep_unpack() === 'yes');
}, (tojson) => {
return new GLib.Variant('s', tojson.deep_unpack() ? 'yes' : 'no');
}));

var window = new Window.MainWindow(this, this._service);
this.add_action(new PreferenceAction(this._service, 'sabrina-store-log', 'b', (fromjson) => new GLib.Variant('b', fromjson.deep_unpack() === 'yes'),
(tojson) => new GLib.Variant('s', tojson.deep_unpack() ? 'yes' : 'no')));
window = new Window.MainWindow(this, this._service);
window.present();
});
} else {
Expand All @@ -122,15 +97,10 @@ class AlmondApplication extends Gtk.Application {
}
});

/* exported main */
function main(argv) {
const service = spawnService();
initEnvironment();

const exitCode = (new AlmondApplication()).run(argv);

if (service)
service.send_signal(15 /* sigterm */);

if (service) service.send_signal(15);
return exitCode;
}