summaryrefslogtreecommitdiff
path: root/wikipedia/utils.js
blob: 0838bd5ba81018272df424646cf2aae4454dabad (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const Gio = imports.gi.Gio;
const GdkPixbuf = imports.gi.GdkPixbuf;
const GLib = imports.gi.GLib;

const set_default = function(obj, key, val) {
    // Similar to python's dict.setdefault(key, val)
    if(obj[key] === undefined)
        obj[key] = val;
}

const default_param = function(variable, defaultValue) {
    // Emulate python's default parameter values
    if(variable === undefined)
        return defaultValue;
    else
        return variable;
}

const range = function(a, b, step) {
    // Emulate python's range function
    var A = [];
    if(typeof a == 'number'){
        if(typeof b == 'undefined'){
            b = a - 1;
            a = 0;
        }
        A[0] = a;
        step = step || 1;
        while(a + step < b){
            A[A.length] = a += step;
        }
    }
    return A;
}

/*
 * ...Taken from utils.js from the eos-weather branch...
 */
function load_file_from_resource(filename) {
    // Return the text stored in the file at filename
    var file = Gio.file_new_for_uri(filename);
    var fstream = file.read(null);
    var dstream = new Gio.DataInputStream({
        base_stream: fstream
    });
    var data = dstream.read_until("", null);
    fstream.close(null);
    return data[0];
}

function get_path_for_relative_path(relative_path){
    let file = Gio.file_new_for_path(relative_path);
    return file.get_path();
}

function write_contents_to_file(filename, content){
    let file = Gio.file_new_for_path(filename);
    file.replace_contents(content, null, false, 0, null);
    return file.get_uri();
}

function write_contents_to_temp_file(name, content){
    let file = Gio.file_new_tmp("XXXXXX" + name)[0];
    file.replace_contents(content, null, false, 0, null);
    return file.get_uri();
}

function make_temp_dir(name){
    return GLib.dir_make_tmp(name + "XXXXXX");
}

/*
 * ...Taken from utils.js from the eos-weather branch...
 */
function load_file(filename) {
    // Return the text stored in the file at filename
    var file = Gio.file_new_for_path(filename);
    var fstream = file.read(null);
    var dstream = new Gio.DataInputStream({
        base_stream: fstream
    });
    var data = dstream.read_until("", null);
    fstream.close(null);
    return data[0];
}

/*
 * Compares two lists *a* and *b* to see if they contain
 * the same contents, ignoring order.  Contents are defined
 * to be equal via JSON representation, unless they are
 * primitives, then String() casting is used.
 *
 * If passed non-list objects, this method has undefined
 * behavior. Algorithm is not exceptionally optimized and has
 * O(n^2) performance.
 *
 * Method treats elements that are logically equivalent but
 * not type-equivalent to be the same.  Thus the string of
 * "3" is considered to equal the integer 3 in these comparisons.
 *
 * Returns true if a and b contain the same elements in
 * any order.
 */
const set_equals = function (a, b) {
    if (typeof a != "object" || typeof b != "object") {
        return false;
    }
    if (a.length != b.length) {
        // Optimization
        return false;
    }
    let matched_of_a = 0;
    for (let i = 0; i < a.length; i++) {
        let i_string = typeof a[i] == "object" ? JSON.stringify (a[i]) : String(a[i]);
        for (let j = 0; j < b.length; j++) {
            let j_string = typeof b[j] == "object" ? JSON.stringify (b[j]) : String(b[j]);
            if (j_string == i_string) {
                matched_of_a++;
            }
        }
    }
    let matched_of_b = 0;
    for (let i = 0; i < b.length; i++) {
        let i_string = typeof b[i] == "object" ? JSON.stringify (b[i]) : String(b[i]);
        for (let j = 0; j < a.length; j++) {
            let j_string = typeof a[j] == "object" ? JSON.stringify (a[j]) : String(a[j]);
            if (j_string == i_string) {
                matched_of_b++;
            }
        }
    }
    if (matched_of_a == a.length &&
        matched_of_b == matched_of_a)
    {
        return true;
    }
    return false;
};

/*
 * Checks to see if an array *arr* contains on element *obj*.  If
 * *same_type* is provided (and is true), the objects must
 * actually be the same type as well to be considered part of the
 * array.
 *
 * Somehow reminds the user of a pirate.
 */
const array_contains = function (arr, obj, same_type) {
    if (same_type == true) {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] === obj) {
              return true;
            }
        }
  } else {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] == obj) {
                return true;
            }
        }
  }
  return false;
};

/*
 * Loads a pixbuf sized to cover the dest_width and dest_height with the
 * image in res_path, while mataining the aspect ratio of the image.
 * The anchor point for cropping is the bottom left of the image.
 */
function load_pixbuf_cover(res_path, dest_width, dest_height) {
    let [load_width, load_height] = [dest_width, dest_height];
    // TODO: We need to get the size of the source image, so right now we
    // are loading the image twice, once to get the size, and the again at
    // the proper size. We should eventually use a GdkPixbuf.Loader and
    // connect to the size-prepared signal, as described in the
    // documentation
    let temp_pixbuf = GdkPixbuf.Pixbuf.new_from_resource(res_path);
    let source_aspect = temp_pixbuf.width / temp_pixbuf.height;
    let dest_aspect = dest_width / dest_height;
    if(dest_aspect > source_aspect)
        load_height = -1;
    else
        load_width = -1;
    let source_pixbuf = GdkPixbuf.Pixbuf.new_from_resource_at_scale(res_path,
        load_width, load_height, true);
    let cropped_pixbuf = source_pixbuf;
    if(dest_width < source_pixbuf.width || dest_height < source_pixbuf.height)
        cropped_pixbuf = source_pixbuf.new_subpixbuf(0, source_pixbuf.height - dest_height,
            dest_width, dest_height);
    return cropped_pixbuf;
}

// Convenience function to convert a resource URI to a resource path, for
// APIs that expect a path rather than an URI
function resourceUriToPath(uri) {
    if(uri.startsWith('resource://'))
        return uri.slice('resource://'.length);
    throw new Error('Resource URI did not start with "resource://"');
}