Commit ca5b1b5f authored by Lorenzo's avatar Lorenzo

First commit

parent a873747f
.vscode
\ No newline at end of file
/*
* ChromeApp
* A manager for the Clipboard
*
* Copyright (C) 2018
* Lorenzo Carbonell <lorenzo.carbonell.cerezo@gmail.com>,
*
* This file is part of Clipman.
*
* Clipman is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Clipman 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gnome-shell-extension-openweather.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
class ChromeApp{
constructor(content){
this.name = this._helper(/Name=(.*)/, content);
this.exec = this._helper(/Exec=(.*)/, content);
this.icon = this._helper(/Icon=(.*)/, content);
this.app_id = this._helper(/--app-id=(.*)/, content);
this.directory = this._helper(/--profile-directory=([^\s]*)/, content);
this.app = this._helper(/Exec=([^\s]*)/, content);
let re = /chromium/;
this.chromium = re.test(this.app);
}
_helper(re, content){
return re.exec(content)[1];
}
}
\ No newline at end of file
/*
* Clipman
* This a extension for manage the Touchpad
* with GNOME Shell
* A manager for the Clipboard
*
* Copyright (C) 2018
* Lorenzo Carbonell <lorenzo.carbonell.cerezo@gmail.com>,
......@@ -40,100 +39,98 @@ imports.gi.versions.Gtk = "3.0";
imports.gi.versions.Gio = "2.0";
imports.gi.versions.GLib = "2.0";
const _DEBUG_ = true;
/* Import St because is the library that allow you to create UI elements */
const St = imports.gi.St;
/* Import Clutter because is the library that allow you to layout UI elements */
const Clutter = imports.gi.Clutter;
const Gtk = imports.gi.Gtk;
const Gdk = imports.gi.Gdk;
const GMenu = imports.gi.GMenu;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Cogl = imports.gi.Cogl;
const Params = imports.misc.params;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Util = imports.misc.util;
const ExtensionUtils = imports.misc.extensionUtils;
const Extension = ExtensionUtils.getCurrentExtension();
const FileModule = Extension.imports.helpers.file;
const ChromeApp = Extension.imports.chromeapp.ChromeApp;
const Convenience = Extension.imports.convenience;
const Configuration = Extension.imports.configuration.Configuration;
const Gettext = imports.gettext.domain(Extension.uuid);
const _ = Gettext.gettext;
function Log(message){
if (_DEBUG_){
let app = Extension.metadata.name.toString();
global.log(app.toUpperCase() + ': '+message);
}
}
function getTimeInSeconds(){
return Math.round(Date.now() / 1000);
}
class IconButton extends St.Button{
constructor(icon_name, icon_size, params){
super(params)
// Icon
this.icon = new St.Icon({
icon_name: icon_name,
icon_size: icon_size,
style_class: 'clipman-button'
});
super.set_child(this.icon);
}
}
class ActionsIcons extends St.BoxLayout{
constructor(on_copy, on_delete){
constructor(position, on_copy, on_delete){
super({vertical: true,
y_align: Clutter.ActorAlign.CENTER,
x_align: Clutter.ActorAlign.CENTER});
x_align: Clutter.ActorAlign.END});
let copy_button = new IconButton('clipman-copy', 20);
//copy_button.connect('button-press-event', on_copy);
this.add(copy_button);
this.copy_icon = new St.Icon({icon_name: 'copy',
icon_size: 48,
y_align: Clutter.ActorAlign.CENTER,
x_align: Clutter.ActorAlign.CENTER});
this.copy_icon.connect('click', on_copy);
this.add(this.copy_icon);
this.delete_icon = new St.Icon({icon_name: 'delete',
icon_size: 48,
y_align: Clutter.ActorAlign.CENTER,
x_align: Clutter.ActorAlign.CENTER});
this.delete_icon.connect('click', on_delete);
this.add(this.delete_icon);
let remove_button = new IconButton('clipman-remove', 20);
this.add(remove_button);
//this.set_width(30);
}
}
class TextItem extends St.BoxLayout{
constructor(text, on_copy, on_delete){
super({vertical: false,
y_align: Clutter.ActorAlign.CENTER,
x_align: Clutter.ActorAlign.CENTER});
text_label = new St.Label({text: text,
class PopupMenuTextItem extends PopupMenu.PopupBaseMenuItem{
constructor(atext, params){
super(params);
this.actor.y_align = Clutter.ActorAlign.CENTER;
let boxLayout = new St.BoxLayout({vertical: false});
boxLayout.set_width(200);
boxLayout.set_height(80);
let text_label = new St.Label({text: atext,
y_align: Clutter.ActorAlign.CENTER,
x_align: Clutter.ActorAlign.CENTER});
this.add(text_label);
this.add(new ActionsIcons(on_copy, on_delete));
this._text = text;
}
}
x_align: Clutter.ActorAlign.START});
class ImageItem extends St.BoxLayout{
constructor(pixbuf, on_copy, on_delete){
super({vertical: false,
y_align: Clutter.ActorAlign.CENTER,
x_align: Clutter.ActorAlign.CENTER});
this.image_container = new St.Bin({y_align: Clutter.ActorAlign.CENTER,
x_align: Clutter.ActorAlign.CENTER});
this.add(this.image_container);
this.add(new ActionsIcons(on_copy, on_delete));
this._pixbuf = pixbuf;
this
}
text_label.set_width(180);
boxLayout.add(text_label);
boxLayout.add(new ActionsIcons(1, null, null));
set_image(pixbuf){
let thumbnail_pixbuf = pixbuf.scale_simple(256, 256, GdkPixbuf.InterpType. BILINEAR)
let {width, height} = thumbnail_pixbuf;
if (height == 0) {
return;
}
let image = new Clutter.Image();
let success = image.set_data(
thumbnail_pixbuf.get_pixels(),
thumbnail_pixbuf.get_has_alpha()
? Cogl.PixelFormat.RGBA_8888
: Cogl.PixelFormat.RGB_888,
width,
height,
thumbnail_pixbuf.get_rowstride()
);
if (!success) {
throw Error("error creating Clutter.Image()");
}
this.image_container.set_child(image);
this.actor.add_child(boxLayout)
this._text = atext;
}
}
class Clipman extends PanelMenu.Button{
class ChromeApps extends PanelMenu.Button{
constructor(){
super(St.Align.START);
......@@ -146,40 +143,20 @@ class Clipman extends PanelMenu.Button{
y_expand: true,
y_align: Clutter.ActorAlign.CENTER });
//box.add(label);
this.icon = new St.Icon({icon_name: 'clipman',
this.icon = new St.Icon({icon_name: 'chrome',
style_class: 'system-status-icon'});
box.add(this.icon);
//box.add(PopupMenu.arrowIcon(St.Side.BOTTOM));
this.actor.add_child(box);
log('--- init menu start');
this.touchpadSwitch = new PopupMenu.PopupSwitchMenuItem(_('Touchpad status'),
{active: true})
this.touchpadSwitch.label.set_text(_('Disable touchpad'));
this.touchpadSwitch.connect('toggled', (widget, value) => {
log('--- active: ' + value);
log('--- active: ' + widget);
if(value){
this.icon.set_icon_name('touchpad-light-enabled');
this.touchpadSwitch.label.set_text(_('Disable touchpad'));
notify('Touchpad Manager',
_('Touchpad enabled'),
'touchpad-light-enabled');
}else{
this.icon.set_icon_name('touchpad-light-disabled');
this.touchpadSwitch.label.set_text(_('Enable touchpad'));
notify('Touchpad Manager',
_('Touchpad disabled'),
'touchpad-light-disabled');
}
});
this.menu.addMenuItem(this.touchpadSwitch)
this.settingsMenuItem = new PopupMenu.PopupMenuItem(_("Settings"));
this.settingsMenuItem.connect('activate', () => {
GLib.spawn_command_line_async(
"gnome-shell-extension-prefs touchpad-manager@atareao.es"
"gnome-shell-extension-prefs chromeapps@atareao.es"
);
});
this._load_chrome_apps();
this.menu.addMenuItem(this.settingsMenuItem);
this.menu.addMenuItem(this._get_help());
}
......@@ -213,6 +190,121 @@ class Clipman extends PanelMenu.Button{
_('Follow me in Google+'), 'google', 'https://plus.google.com/118214486317320563625/posts'));
return menu_help;
}
_load_chrome_apps(){
let home = GLib.getenv('HOME');
let dir_chrome_apps = Gio.File.new_for_path(home + '/.local/share/applications/');
let enumerador = dir_chrome_apps.enumerate_children('*', Gio.FileQueryInfoFlags.NONE, null);
let nextItem;
let re = /chrome-[a-z]*-Default\.desktop/
let columns = 0;
let rows = 0;
this.linesOfButtons = new Array();
while((nextItem = enumerador.next_file(null)) != null){
let child = enumerador.get_child(nextItem);
if(re.test(child.get_basename()))
{
let ans = child.load_contents(null);
if(ans[0] == true){
let ca = new ChromeApp(ans[1]);
Log('==========');
Log(ca.name);
Log(ca.exec);
Log(ca.icon);
Log(ca.app_id);
Log(ca.directory);
Log(ca.app);
Log(ca.chromium);
let afile = null;
if(ca.chromium){
afile = Gio.File.new_for_path(home + '/.cache/chromium/' + ca.directory + '/Storage/ext/' + ca.app_id);
}else{
afile = Gio.File.new_for_path(home + '/.config/google-chrome/' + ca.directory + '/Extensions/' + ca.app_id);
}
if(afile.query_exists(null)){
Log('Existe');
if(columns == 0){
let lineOfButtons = new PopupMenu.PopupBaseMenuItem({
reactive: false
});
this.linesOfButtons.push(lineOfButtons);
this.menu.addMenuItem(lineOfButtons)
}
let item = this._createChromeAppButton(ca.icon, ca.name);
item.set_style_class_name('item');
item.connect('clicked', ()=>{
//Util.spawn('/usr/bin/chromium-browser --profile-directory=Default --app-id=cnidaodnidkbaplmghlelgikaiejfhja'.split(' '));
Util.spawn([ca.app, '--profile-directory=' + ca.directory, '--app-id=' + ca.app_id]);
});
this.linesOfButtons[this.linesOfButtons.length - 1].actor.add_actor(item);
columns ++;
if(columns == 3){
columns = 0;
}
}else{
Log('No existe');
}
}
/*
for(let property in child){
if (_DEBUG_) global.log('ZZZ3:'+property);
}
*/
}
}
/*
let docs = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DOCUMENTS);
let desktop = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DESKTOP);
let pics = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PICTURES);
let videos = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_VIDEOS);
let music = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_MUSIC);
let downloads = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DOWNLOAD);
let public_dir = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PUBLIC_SHARE);
let templates = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_TEMPLATES);
if (_DEBUG_) global.log('ZZZ4: ' + docs);
if (_DEBUG_) global.log('ZZZ4: ' + desktop);
if (_DEBUG_) global.log('ZZZ4: ' + pics);
if (_DEBUG_) global.log('ZZZ4: ' + videos);
if (_DEBUG_) global.log('ZZZ4: ' + music);
if (_DEBUG_) global.log('ZZZ4: ' + downloads);
if (_DEBUG_) global.log('ZZZ4: ' + public_dir);
if (_DEBUG_) global.log('ZZZ4: ' + templates);
*/
//Util.spawn('/usr/bin/chromium-browser --profile-directory=Default --app-id=cnidaodnidkbaplmghlelgikaiejfhja'.split(' '));
/*
let tree = GMenu.Tree.new_for_path('/home/lorenzo/.local/share/applications/', GMenu.TreeFlags.INCLUDE_NODISPLAY);
let tree = new GMenu.Tree({ menu_basename: 'applications.menu' });
tree.load_sync();
if(_DEBUG_) global.log("ZZZ: "+tree.get_canonical_menu_path());
let root = tree.get_root_directory();
let iter = root.iter();
let nextType;
while ((nextType = iter.next()) != GMenu.TreeItemType.INVALID) {
if (nextType == GMenu.TreeItemType.DIRECTORY) {
let dir = iter.get_directory();
}
else if (nextType == GMenu.TreeItemType.ENTRY ) {
let entry = iter.get_entry();
let appinfo = entry.get_app_info();
if (_DEBUG_) global.log("ZZZ: ==================");
if (_DEBUG_) global.log("ZZZ: "+appinfo.get_generic_name());
if (_DEBUG_) global.log("ZZZ: "+appinfo.get_categories());
if (_DEBUG_) global.log("ZZZ: "+appinfo.get_filename());
}
}
*/
}
_createChromeAppButton(iconName, accessibleName) {
let icon = new St.Button({ reactive: true,
can_focus: true,
track_hover: true,
accessible_name: accessibleName,
style_class: 'system-menu-action' });
icon.child = new St.Icon({ icon_name: iconName });
return icon;
}
}
var button;
......@@ -224,8 +316,8 @@ function init() {
}
function enable() {
button = new TouchpadManagerButton();
Main.panel.addToStatusArea('Touchpad-Indicator', button, 0, 'right');
button = new ChromeApps();
Main.panel.addToStatusArea('ChromeApps', button, 0, 'right');
}
function disable() {
......
This diff is collapsed.
# GNOME JavaScript helpers
These helper modules are made to make working with GJS easier. Especially when you're coming from a `node` environment.
Currently these include,
* `Promise` implementation based on the [ES6 spec](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise).
* Polyfill for `setTimeout` and `setInterval`.
* Promise based library for working with files.
* Promise based library for fetching data over network.
## Usage
Since I'm not aware of any good module architecture for GJS, you've to manually copy the files to your project directory and use them. Which means you'll miss on updates.
First you'll need to add the current directory to search path.
```javascript
imports.searchPath.unshift(".");
```
Then, say you want to use the `Promise` library, place the `promise.js` file in a directory named `helpers`, and import it,
```javascript
const Promise = imports.helpers.promise.Promise;
```
Now you have access to the `Promise` object for use.
```javascript
let promise = new Promise((resolve, reject) => {
let file = Gio.File.new_for_path("/tmp/new_dir");
file.make_directory_async(GLib.PRIORITY_DEFAULT, null, (source, res) => {
resolve(source.make_directory_finish(res));
});
});
promise.then(() => print("Directory created successfully!"));
promise.catch(() => print("Directory creation failed!"));
```
Note that the `Promise` library automatically catches synchronously thrown errors and the promise fails.
## Running tests
Tests are written using a custom tool (included as `litmus.js` in the repo). To run tests, simply `cd` into the `src`
directory and run the intended test file.
```sh
cd src
gjs promise-test.js
```
/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (c) 2011-2012, Giovanni Campagna <scampa.giovanni@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the GNOME nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
const Gettext = imports.gettext;
const Gio = imports.gi.Gio;
const Config = imports.misc.config;
const ExtensionUtils = imports.misc.extensionUtils;
/**
* initTranslations:
* @domain: (optional): the gettext domain to use
*
* Initialize Gettext to load translations from extensionsdir/locale.
* If @domain is not provided, it will be taken from metadata['gettext-domain']
*/
function initTranslations(domain) {
let extension = ExtensionUtils.getCurrentExtension();
domain = domain || extension.metadata['gettext-domain'];
// check if this extension was built with "make zip-file", and thus
// has the locale files in a subfolder
// otherwise assume that extension has been installed in the
// same prefix as gnome-shell
let localeDir = extension.dir.get_child('locale');
if (localeDir.query_exists(null))
Gettext.bindtextdomain(domain, localeDir.get_path());
else
Gettext.bindtextdomain(domain, Config.LOCALEDIR);
}
/**
* getSettings:
* @schema: (optional): the GSettings schema id
*
* Builds and return a GSettings schema for @schema, using schema files
* in extensionsdir/schemas. If @schema is not provided, it is taken from
* metadata['settings-schema'].
*/
function getSettings(schema) {
let extension = ExtensionUtils.getCurrentExtension();
schema = schema || extension.metadata['settings-schema'];
const GioSSS = Gio.SettingsSchemaSource;
// check if this extension was built with "make zip-file", and thus
// has the schema files in a subfolder
// otherwise assume that extension has been installed in the
// same prefix as gnome-shell (and therefore schemas are available
// in the standard folders)
let schemaDir = extension.dir.get_child('schemas');
let schemaSource;
if (schemaDir.query_exists(null))
schemaSource = GioSSS.new_from_directory(schemaDir.get_path(),
GioSSS.get_default(),
false);
else
schemaSource = GioSSS.get_default();
let schemaObj = schemaSource.lookup(schema, true);
if (!schemaObj)
throw new Error('Schema ' + schema + ' could not be found for extension '
+ extension.metadata.uuid + '. Please check your installation.');
return new Gio.Settings({ settings_schema: schemaObj });
}
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Promise = Me.imports.helpers.promise.Promise;
function File(path) {
if (path.indexOf('http://') == -1) {
this.file = Gio.File.new_for_path(path);
} else {
this.file = Gio.File.new_for_uri(path);
}
}
File.prototype.read = function() {
return new Promise((resolve, reject) => {
try {
this.file.load_contents_async(null, function(file, res) {
try {
let contents = file.load_contents_finish(res)[1];
// are we running gnome 3.30 or higher?
if (contents instanceof Uint8Array) {
resolve(imports.byteArray.toString(contents).trim());
} else {
resolve(contents.toString().trim());
}
} catch (e) {
reject(e.message);
}
});
} catch (e) {
reject(e.message);
}
});
};
File.prototype.list = function() {
return new Promise((resolve, reject) => {
let max_items = 100, results = [];
try {
this.file.enumerate_children_async(Gio.FILE_ATTRIBUTE_STANDARD_NAME, Gio.FileQueryInfoFlags.NONE, GLib.PRIORITY_LOW, null, function(file, res) {
try {
let enumerator = file.enumerate_children_finish(res);
let callback = function(enumerator, res) {
try {
let files = enumerator.next_files_finish(res);
for (let i = 0; i < files.length; i++) {
let file_info = files[i];
results.push(file_info.get_attribute_as_string(Gio.FILE_ATTRIBUTE_STANDARD_NAME));
}
if (files.length == 0) {
enumerator.close_async(GLib.PRIORITY_LOW, null, function(){});
resolve(results);
} else {
enumerator.next_files_async(max_items, GLib.PRIORITY_LOW, null, callback);
}
} catch (e) {
reject(e.message);
}
};
enumerator.next_files_async(max_items, GLib.PRIORITY_LOW, null, callback);
} catch (e) {
reject(e.message);
}
});
} catch (e) {
reject(e.message);
}
});
};
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
'use strict';
if (typeof start !== 'number')
start = 0;
if (start + search.length > this.length)
return false;
else
return this.indexOf(search, start) !== -1;
}
}
// in parts of the system you may think that we can use Object.values
// instead of "key in" statements. Gnome 3.18 - 3.22 doesn't like doing that.
if (!Object.values)
Object.values = obj => Object.keys(obj).map(key => obj[key]);
if (!Math.getMaxOfArray) {
Math.getMaxOfArray = function(numArray) {
return Math.max.apply(null, numArray);
}
}
const GLib = imports.gi.GLib;
const PENDING = 0,
FULFILLED = 1,
REJECTED = 2;
function Promise(executor) {
if (false === (this instanceof Promise)) {
throw new TypeError("Promises must be constructed via new");
}
if (typeof executor !== "function") {
throw new TypeError("Promise resolver " + executor + " is not a function");
}
// Create an array to add handlers
this._deferreds = [];
// Set the promise status
this._state = PENDING;
this._caught = false;
this._handle = deferred => {
if (this._state === PENDING) {
this._deferreds.push(deferred);
return;
}
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1, () => {
let cb = this._state === FULFILLED ? deferred.onFulfilled : deferred.onRejected;
if (cb === null) {
(this._state === FULFILLED ? deferred.resolve : deferred.reject)(this._value);
return false;
}
if (typeof cb !== "function") {
deferred.reject(this._value);
return false;
}
let ret;
try {
ret = cb(this._value);
} catch (e) {
deferred.reject(e);
return false;
}
deferred.resolve(ret);
return false; // Don't repeat
});
};
let doresolve = (fn, onFulfilled, onRejected) => {
let done = false;
try {
fn(value => {
if (done) {
return;
}
done = true;
onFulfilled(value);
}, function(reason) {
if (done) {
return;
}
done = true;
onRejected(reason);
});
} catch (e) {
if (done) {
return;
}
done = true;
onRejected(e);
}
};
let finale = () => {