~indicator-multiload/indicator-multiload/trunk

55 by Michael Hofmann
Automatic widest strings for indicator text.
1
/******************************************************************************
87 by Michael Hofmann
Fix license headers.
2
 * Copyright (C) 2011-2013  Michael Hofmann <mh21@mh21.de>                    *
55 by Michael Hofmann
Automatic widest strings for indicator text.
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
public abstract class Function : GLib.Object {
20
    public string id { get; construct; }
21
    public string[] parameterdescs { get; construct; }
22
23
    public Function(string id, string[] parameterdescs) {
24
        Object(id: id, parameterdescs: parameterdescs);
25
    }
26
27
    protected static Error error(string message) {
28
        return new Error(Quark.from_string("function-error-quark"), 0, "%s", message);
29
    }
30
137 by mh21 at mh21
Remove support for indicator items.
31
    public abstract string call(string[] parameters) throws Error;
55 by Michael Hofmann
Automatic widest strings for indicator text.
32
}
33
34
public class DecimalsFunction : Function {
35
    public DecimalsFunction() {
36
        base("decimals", {"value", "decimals"});
37
    }
38
137 by mh21 at mh21
Remove support for indicator items.
39
    public override string call(string[] parameters) throws Error {
55 by Michael Hofmann
Automatic widest strings for indicator text.
40
        if (parameters.length != 2)
41
            throw error("two parameters expected");
42
        return "%.*f".printf(int.parse(parameters[1]), double.parse(parameters[0]));
43
    }
44
}
45
46
public class SizeFunction : Function {
47
    public SizeFunction() {
48
        base("size", {"value"});
49
    }
50
137 by mh21 at mh21
Remove support for indicator items.
51
    public override string call(string[] parameters) throws Error {
55 by Michael Hofmann
Automatic widest strings for indicator text.
52
        if (parameters.length != 1)
53
            throw error("one parameter expected");
54
        return Utils.format_size(double.parse(parameters[0]));
55
    }
56
}
57
140 by mh21 at mh21
Add size2 function with IEC units (base 2).
58
public class Size2Function : Function {
59
    public Size2Function() {
60
        base("size2", {"value"});
61
    }
62
63
    public override string call(string[] parameters) throws Error {
64
        if (parameters.length != 1)
65
            throw error("one parameter expected");
66
        return GLib.format_size(uint64.parse(parameters[0]), FormatSizeFlags.IEC_UNITS);
67
    }
68
}
69
55 by Michael Hofmann
Automatic widest strings for indicator text.
70
public class SpeedFunction : Function {
71
    public SpeedFunction() {
72
        base("speed", {"value"});
73
    }
74
137 by mh21 at mh21
Remove support for indicator items.
75
    public override string call(string[] parameters) throws Error {
55 by Michael Hofmann
Automatic widest strings for indicator text.
76
        if (parameters.length != 1)
77
            throw error("one parameter expected");
78
        return Utils.format_speed(double.parse(parameters[0]));
79
    }
80
}
81
130 by mh21 at mh21
Provide bitrate function to show speed in bit/s
82
public class BitrateFunction : Function {
83
    public BitrateFunction() {
84
        base("bitrate", {"value"});
85
    }
86
137 by mh21 at mh21
Remove support for indicator items.
87
    public override string call(string[] parameters) throws Error {
130 by mh21 at mh21
Provide bitrate function to show speed in bit/s
88
        if (parameters.length != 1)
89
            throw error("one parameter expected");
90
        return Utils.format_bitrate(8 * double.parse(parameters[0]));
91
    }
92
}
93
109 by Michael Hofmann
Cpu frequency support.
94
public class FrequencyFunction : Function {
95
    public FrequencyFunction() {
96
        base("frequency", {"value"});
97
    }
98
137 by mh21 at mh21
Remove support for indicator items.
99
    public override string call(string[] parameters) throws Error {
109 by Michael Hofmann
Cpu frequency support.
100
        if (parameters.length != 1)
101
            throw error("one parameter expected");
102
        return Utils.format_frequency(double.parse(parameters[0]));
103
    }
104
}
105
55 by Michael Hofmann
Automatic widest strings for indicator text.
106
public class PercentFunction : Function {
107
    public PercentFunction() {
108
        base("percent", {"value"});
109
    }
110
137 by mh21 at mh21
Remove support for indicator items.
111
    public override string call(string[] parameters) throws Error {
55 by Michael Hofmann
Automatic widest strings for indicator text.
112
        if (parameters.length != 1)
113
            throw error("one parameter expected");
114
        return _("%u%%").printf
115
            ((uint) Math.round(100 * double.parse(parameters[0])));
116
    }
117
}