-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathicons.js
106 lines (83 loc) · 3.43 KB
/
icons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
AUTHOR: Mario Wenzel
LICENSE: GPL3.0
**/
import Gtk from 'gi://Gtk';
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import Gdk from 'gi://Gdk';
import * as Api from './api.js';
const icons_path = GLib.get_user_cache_dir() + '/twitchlive-extension';
/* exported init_icons, trigger_download_by_name, trigger_download_by_url, get_streamericon, has_icon */
export function init_icons() {
// TODO: everything needs to be disabled if the creation fails or if it isn't a writable directory
GLib.mkdir_with_parents(icons_path, 448);
var display = Gdk.Display.get_default();
if (display == null) {
return;
}
var icon_theme = Gtk.IconTheme.get_for_display(display);
if (icon_theme == null) {
return;
}
icon_theme.add_search_path(icons_path);
}
function mogrify_available() { // TODO: cache this result
return GLib.find_program_in_path('mogrify'); // this is null if it isnt available
}
function curl_available() { // TODO: cache this result
return GLib.find_program_in_path('curl'); // this is null if it isnt available
}
function get_final_icon_path(streamername) {
let filename = icons_path + '/' + get_icon_name(streamername) + '.png';
return filename;
}
export function has_icon(streamername) {
// check whether any icon is already available
return GLib.file_test(get_final_icon_path(streamername), GLib.FileTest.EXISTS);
}
export function trigger_download_by_names(names, session) {
Api.users(session, names).then((data) => {
data.forEach((streamer) => {
if (streamer.profile_image_url) trigger_download_by_url(streamer.login, streamer.profile_image_url);
});
});
}
export function trigger_download_by_url(streamername, imageurl) {
// TODO: this should return immediately
if (!curl_available()) {
return; // we can't download anything
}
let download_filename = imageurl.split('/').pop();
let unique_path = icons_path + '/' + download_filename;
let source_extension = imageurl.split('.').pop();
let target_name = icons_path + '/' + get_icon_name(streamername) + '.' + source_extension;
if (GLib.file_test(unique_path, GLib.FileTest.EXISTS)) {
return; // these icon names appear to be unique. We don't need to download it again.
}
if (source_extension !== 'png' && !mogrify_available()) {
return; // we can't convert the file anyways so we don't even download it
}
// download the file
let cmd = "curl -s %url% -o %output%".replace('%url%', imageurl).replace('%output%', unique_path);
GLib.spawn_command_line_sync(cmd);
// symlink the dowloaded file to the proper name (overwrite if existing)
cmd = "ln -sf %linksource% %linktarget%".replace('%linksource%', unique_path).replace('%linktarget%', target_name);
GLib.spawn_command_line_sync(cmd);
if (source_extension !== 'png') {
// convert the linked file (this overwrites previously converted files)
cmd = "mogrify -format png " + target_name;
GLib.spawn_command_line_async(cmd);
}
}
export function get_icon_name(streamername) {
return 'twitchlive-' + streamername.toLowerCase();
}
export function get_streamericon(streamername, style_class) {
// St is not imported globally since it's not available in the preferences context
let icon = new imports.gi.St.Icon({ gicon: Gio.icon_new_for_string(get_final_icon_path(streamername)), style_class: style_class });
if (icon.set_fallback_icon_name) { // this isnt available before 3.16
icon.set_fallback_icon_name('twitchlive');
}
return icon;
}