~indicator-multiload/indicator-multiload/trunk

1 by Michael Hofmann
Initial public version.
1
/******************************************************************************
87 by Michael Hofmann
Fix license headers.
2
 * Copyright (C) 2011-2013  Michael Hofmann <mh21@mh21.de>                    *
1 by Michael Hofmann
Initial public version.
3
 *                                                                            *
4
 * This program is free software; you can redistribute it and/or modify       *
5
 * it under the terms of the GNU General Public License as published by       *
6
 * the Free Software Foundation; either version 3 of the License, or          *
7
 * (at your option) any later version.                                        *
8
 *                                                                            *
9
 * This program is distributed in the hope that it will be useful,            *
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
12
 * GNU General Public License for more details.                               *
13
 *                                                                            *
14
 * You should have received a copy of the GNU General Public License along    *
15
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                *
17
 ******************************************************************************/
18
19
namespace Utils {
32.1.10 by Michael Hofmann
Restructuring the settings a bit.
20
    public string uifile;
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
21
1 by Michael Hofmann
Initial public version.
22
    public double max(double[] data) {
23
        if (data.length == 0)
24
            return 0;
25
        double result = data[0];
26
        foreach (var v in data)
27
            if (result < v)
28
                result = v;
29
        return result;
30
    }
31
32
    public double mean(double[] data) {
33
        if (data.length == 0)
34
            return 0;
35
        double result = 0;
36
        foreach (var v in data)
37
            result += v;
38
        return result / data.length;
39
    }
11 by Michael Hofmann
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.
40
41
    // for SI units as g_format_size_for_display uses base 2
42
    public string format_size(double val) {
43
        const string[] units = {
44
            // TRANSLATORS: Please leave {} as it is, it is replaced by the size
45
            N_("{} kB"),
46
            // TRANSLATORS: Please leave {} as it is, it is replaced by the size
47
            N_("{} MB"),
48
            // TRANSLATORS: Please leave {} as it is, it is replaced by the size
49
            N_("{} GB")
50
        };
51
        int index = -1;
51 by Michael Hofmann
Less decimals for size, speed.
52
        while (index + 1 < units.length && (val >= 1000 || index < 0)) {
11 by Michael Hofmann
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.
53
            val /= 1000;
54
            ++index;
55
        }
56
        if (index < 0)
86 by Michael Hofmann
Beginning support for color schemes.
57
            // TRANSLATORS: Please leave %u as it is, it is replaced by the size
51 by Michael Hofmann
Less decimals for size, speed.
58
            return ngettext("%u B", "%u B",
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
59
                    (ulong)val).printf((uint)val);
11 by Michael Hofmann
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.
60
        // 4 significant digits
61
        var pattern = _(units[index]).replace("{}",
51 by Michael Hofmann
Less decimals for size, speed.
62
            val <   9.95 ? "%.1f" :
63
            val <  99.5  ? "%.0f" :
64
            val < 999.5  ? "%.0f" : "%.0f");
11 by Michael Hofmann
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.
65
        return pattern.printf(val);
66
    }
67
68
    public string format_speed(double val) {
69
        const string[] units = {
70
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
71
            N_("{} kB/s"),
72
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
73
            N_("{} MB/s"),
74
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
75
            N_("{} GB/s"),
76
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
77
            N_("{} TB/s")
78
        };
79
        int index = -1;
51 by Michael Hofmann
Less decimals for size, speed.
80
        while (index + 1 < units.length && (val >= 1000 || index < 0)) {
11 by Michael Hofmann
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.
81
            val /= 1000;
82
            ++index;
83
        }
84
        if (index < 0)
86 by Michael Hofmann
Beginning support for color schemes.
85
            // TRANSLATORS: Please leave %u as it is, it is replaced by the speed
51 by Michael Hofmann
Less decimals for size, speed.
86
            return ngettext("%u B/s", "%u B/s",
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
87
                    (ulong)val).printf((uint)val);
11 by Michael Hofmann
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.
88
        // 4 significant digits
89
        var pattern = _(units[index]).replace("{}",
51 by Michael Hofmann
Less decimals for size, speed.
90
            val <   9.95 ? "%.1f" :
91
            val <  99.5  ? "%.0f" :
92
            val < 999.5  ? "%.0f" : "%.0f");
11 by Michael Hofmann
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.
93
        return pattern.printf(val);
94
    }
95
130 by mh21 at mh21
Provide bitrate function to show speed in bit/s
96
    public string format_bitrate(double val) {
97
        const string[] units = {
98
            // TRANSLATORS: Please leave {} as it is, it is replaced by the bitrate
99
            N_("{} kbit/s"),
100
            // TRANSLATORS: Please leave {} as it is, it is replaced by the bitrate
101
            N_("{} Mbit/s"),
102
            // TRANSLATORS: Please leave {} as it is, it is replaced by the bitrate
103
            N_("{} Gbit/s"),
104
            // TRANSLATORS: Please leave {} as it is, it is replaced by the bitrate
105
            N_("{} Tbit/s")
106
        };
107
        int index = -1;
108
        while (index + 1 < units.length && (val >= 1000 || index < 0)) {
109
            val /= 1000;
110
            ++index;
111
        }
112
        if (index < 0)
113
            // TRANSLATORS: Please leave %u as it is, it is replaced by the bit rate
114
            return ngettext("%u bit/s", "%u bit/s",
115
                    (ulong)val).printf((uint)val);
116
        // 4 significant digits
117
        var pattern = _(units[index]).replace("{}",
118
            val <   9.95 ? "%.1f" :
119
            val <  99.5  ? "%.0f" :
120
            val < 999.5  ? "%.0f" : "%.0f");
121
        return pattern.printf(val);
122
    }
123
109 by Michael Hofmann
Cpu frequency support.
124
    public string format_frequency(double val) {
125
        const string[] units = {
126
            // TRANSLATORS: Please leave {} as it is, it is replaced by the frequency
127
            N_("{} kHz"),
128
            // TRANSLATORS: Please leave {} as it is, it is replaced by the frequency
129
            N_("{} MHz"),
130
            // TRANSLATORS: Please leave {} as it is, it is replaced by the frequency
131
            N_("{} GHz")
132
        };
133
        int index = -1;
134
        while (index + 1 < units.length && (val >= 1000 || index < 0)) {
135
            val /= 1000;
136
            ++index;
137
        }
138
        if (index < 0)
139
            // TRANSLATORS: Please leave %u as it is, it is replaced by the frequency
140
            return ngettext("%u Hz", "%u Hz",
141
                    (ulong)val).printf((uint)val);
142
        // 4 significant digits
143
        var pattern = _(units[index]).replace("{}",
144
            val <   9.95 ? "%.1f" :
145
            val <  99.5  ? "%.0f" :
146
            val < 999.5  ? "%.0f" : "%.0f");
147
        return pattern.printf(val);
148
    }
149
32.1.10 by Michael Hofmann
Restructuring the settings a bit.
150
    public Object get_ui(string objectid, Object signalhandlers,
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
151
            string[] additional = {}, out Gtk.Builder builder = null) {
152
        builder = new Gtk.Builder();
153
        string[] ids = additional;
154
        ids += objectid;
155
        try {
156
            builder.add_objects_from_file(Utils.uifile, ids);
157
        } catch (Error e) {
158
            stderr.printf("Could not load indicator ui %s from %s: %s\n",
159
                    objectid, Utils.uifile, e.message);
160
        }
161
        builder.connect_signals(signalhandlers);
162
        return builder.get_object(objectid);
163
    }
164
86 by Michael Hofmann
Beginning support for color schemes.
165
    public bool get_settings_rgba(Value value, Variant variant,
116 by Michael Hofmann
Fix some warnings.
166
            void *user_data)
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
167
    {
86 by Michael Hofmann
Beginning support for color schemes.
168
        Gdk.RGBA rgba = Gdk.RGBA();
116 by Michael Hofmann
Fix some warnings.
169
        if (ColorMapper.parse_colorname(variant.get_string(), ref rgba)) {
86 by Michael Hofmann
Beginning support for color schemes.
170
            value.set_boxed(&rgba);
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
171
            return true;
172
        }
173
        return false;
174
    }
175
86 by Michael Hofmann
Beginning support for color schemes.
176
    public Variant set_settings_rgba(Value value, VariantType expected_type,
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
177
            void *user_data)
178
    {
86 by Michael Hofmann
Beginning support for color schemes.
179
        Gdk.RGBA rgba = *(Gdk.RGBA*)value.get_boxed();
180
        return new Variant.string(rgba.to_string());
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
181
    }
1 by Michael Hofmann
Initial public version.
182
}