Skip to content

Instantly share code, notes, and snippets.

@andyholmes
Created December 11, 2018 02:14
Show Gist options
  • Save andyholmes/811b0096a74f1ec7bf4f3c31d517a1a9 to your computer and use it in GitHub Desktop.
Save andyholmes/811b0096a74f1ec7bf4f3c31d517a1a9 to your computer and use it in GitHub Desktop.
Gravatar GIcon in GJS
#!/usr/bin/env gjs
// Make sure we get the right Gtk
imports.gi.versions.Gtk = "3.0";
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
/**
* A select number of fallback types for Gravatars
*/
const GravatarFallback = {
// a simple, cartoon-style silhouetted outline of a person
DEFAULT: 'mp',
// 8-bit arcade-style pixelated faces
ARCADE: 'retro',
// Faces with differing features and backgrounds
FACE: 'wavatar',
// A geometric pattern based on an email hash
GEOMETRIC: 'identicon',
// A "monster" with different colors, faces, etc
MONSTER: 'monsterid',
// Robot with different colors, faces, etc
ROBOT: 'robohash'
};
/**
* A GIcon for Gravatars
* See: https://en.gravatar.com/site/implement/images/
*
* @param {string} [email] - An email address
* @param {number} [size] - Size to request; default is 80px, largest is 2048px
* @param {GravatarFallback} [fallback] - The fallback type
*/
const GravatarIcon = GObject.registerClass({
GTypeName: 'GravatarIcon'
}, class GravatarIcon extends Gio.FileIcon {
_init(params) {
params = Object.assign({
email: 'none',
fallback: GravatarFallback.DEFAULT,
size: 80
}, params);
this._email = params.email;
// Compile the URI
let uri = `https://www.gravatar.com/avatar/${this.hash}?s=${params.size}&d=${params.fallback}`;
// Initialize the Gio.FileIcon
super._init({
file: Gio.File.new_for_uri(uri)
});
}
get email() {
return this._email;
}
get hash() {
return GLib.compute_checksum_for_string(
GLib.ChecksumType.MD5,
this.email.toLowerCase(),
-1
);
}
/**
* Save the Gravatar to disk
*/
save(filename) {
this.file.copy_async(
Gio.File.new_for_path(filename),
Gio.FileCopyFlags.OVERWRITE,
GLib.PRIORITY_DEFAULT,
null,
null,
(file, res) => {
try {
file.copy_finish(res);
} catch (e) {
logError(e);
}
}
);
}
});
/**
* Example using GravatarIcon in Gtk. You could also use it in GNOME Shell
* with St.Icon
*/
// Initialize Gtk before you start calling anything from the import
Gtk.init(null);
let window = new Gtk.Window ({
type: Gtk.WindowType.TOPLEVEL,
title: 'Gravatar',
window_position: Gtk.WindowPosition.CENTER,
visible: true
});
window.connect('delete-event', () => Gtk.main_quit());
// Window Layout
let box = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
margin: 18,
spacing: 12,
visible: true
});
window.add(box);
let image = new Gtk.Image({
gicon: new GravatarIcon({
email: '[email protected]',
fallback: GravatarFallback.ARCADE,
size: 2048
}),
pixel_size: 256,
visible: true
});
box.add(image);
let entry = new Gtk.Entry({
text: '[email protected]',
placeholder_text: '[email protected]',
hexpand: true,
visible: true
});
box.add(entry);
let button = new Gtk.Button({
label: 'Save',
halign: Gtk.Align.CENTER,
visible: true
});
box.add(button);
// Update the Gravatar
entry.connect('activate', (entry) => {
image.gicon = new GravatarIcon({
email: entry.text,
fallback: GravatarFallback.ARCADE,
size: 2048
});
});
// Save the Gravatar
button.connect('clicked', () => {
let downloads = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DOWNLOAD);
image.gicon.save(`${downloads}/${image.gicon.hash}.jpg`);
});
Gtk.main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment