~indicator-multiload/indicator-multiload/trunk

1 by Michael Hofmann
Initial public version.
1
/******************************************************************************
2
 * Copyright (C) 2011  Michael Hofmann <mh21@piware.de>                       *
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.4 by mh21 at piware
Expression-based menu entries end graphs.
20
    public static string uifile;
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;
52
        while (index + 1 < units.length && val >= 1000) {
53
            val /= 1000;
54
            ++index;
55
        }
56
        if (index < 0)
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
57
            return ngettext("%u byte", "%u bytes", 
58
                    (ulong)val).printf((uint)val);
11 by Michael Hofmann
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.
59
        // 4 significant digits
60
        var pattern = _(units[index]).replace("{}",
61
            val < 9.9995 ? "%.3f" :
62
            val < 99.995 ? "%.2f" :
63
            val < 999.95 ? "%.1f" : "%.0f");
64
        return pattern.printf(val);
65
    }
66
67
    public string format_speed(double val) {
68
        const string[] units = {
69
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
70
            N_("{} kB/s"),
71
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
72
            N_("{} MB/s"),
73
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
74
            N_("{} GB/s"),
75
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
76
            N_("{} TB/s")
77
        };
78
        int index = -1;
79
        while (index + 1 < units.length && val >= 1000) {
80
            val /= 1000;
81
            ++index;
82
        }
83
        if (index < 0)
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
84
            return ngettext("%u byte/s", "%u bytes/s",
85
                    (ulong)val).printf((uint)val);
11 by Michael Hofmann
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.
86
        // 4 significant digits
87
        var pattern = _(units[index]).replace("{}",
88
            val < 9.9995 ? "%.3f" :
89
            val < 99.995 ? "%.2f" :
90
            val < 999.95 ? "%.1f" : "%.0f");
91
        return pattern.printf(val);
92
    }
93
32.1.4 by mh21 at piware
Expression-based menu entries end graphs.
94
    public Object get_ui(string objectid, Object signalhandlers, 
95
            string[] additional = {}, out Gtk.Builder builder = null) {
96
        builder = new Gtk.Builder();
97
        string[] ids = additional;
98
        ids += objectid;
99
        try {
100
            builder.add_objects_from_file(Utils.uifile, ids);
101
        } catch (Error e) {
102
            stderr.printf("Could not load indicator ui %s from %s: %s\n",
103
                    objectid, Utils.uifile, e.message);
104
        }
105
        builder.connect_signals(signalhandlers);
106
        return builder.get_object(objectid);
107
    }
108
109
    public FixedGSettings.Settings globalsettings() {
110
        return new FixedGSettings.Settings("de.mh21.indicator.multiload");
111
    }
112
113
    public FixedGSettings.Settings graphsettings(string graphid) {
114
        if (graphid.has_prefix("custom"))
115
            return new FixedGSettings.Settings.with_path
116
                ("de.mh21.indicator.multiload.graph", 
117
                 @"/apps/indicators/multiload/$graphid");
118
        return new FixedGSettings.Settings
119
            (@"de.mh21.indicator.multiload.graphs.$graphid");
120
    }
121
122
    public FixedGSettings.Settings tracesettings(string graphid, 
123
            string traceid) {
124
        if (traceid.has_prefix("custom"))
125
            return new FixedGSettings.Settings.with_path
126
                ("de.mh21.indicator.multiload.trace",
127
                 @"/apps/indicators/multiload/$graphid/$traceid");
128
        return new FixedGSettings.Settings
129
            (@"de.mh21.indicator.multiload.traces.$traceid");
130
    }
131
132
    public bool get_settings_color(Value value, Variant variant, void *user_data)
133
    {
134
        Gdk.Color color;
135
        if (Gdk.Color.parse(variant.get_string(), out color)) {
136
            value.set_boxed(&color);
137
            return true;
138
        }
139
        return false;
140
    }
141
142
    public Variant set_settings_color(Value value, VariantType expected_type, 
143
            void *user_data)
144
    {
145
        Gdk.Color color = *(Gdk.Color*)value.get_boxed();
146
        return new Variant.string(color.to_string());
147
    }
1 by Michael Hofmann
Initial public version.
148
}