process-row.vala 7.88 KB
Newer Older
Petr Štětka's avatar
Petr Štětka committed
1 2
namespace Usage
{
Petr Štětka's avatar
Petr Štětka committed
3
    public class ProcessRow : Gtk.ListBoxRow
Petr Štětka's avatar
Petr Štětka committed
4 5
    {
        Gtk.Label load_label;
6
        Gtk.Label title_label;
Petr Štětka's avatar
Petr Štětka committed
7
        Gtk.Revealer revealer;
Petr Štětka's avatar
Petr Štětka committed
8
        SubProcessListBox sub_process_list_box;
Petr Štětka's avatar
Petr Štětka committed
9
        ProcessListBoxType type;
10
        bool group;
Petr Štětka's avatar
Petr Štětka committed
11

12
        public Process process { get; private set; }
Petr Štětka's avatar
Petr Štětka committed
13 14 15
        public bool showing_details { get; private set; }
        public bool max_usage { get; private set; }

Petr Štětka's avatar
Petr Štětka committed
16
        public ProcessRow(Process process, ProcessListBoxType type, bool opened = false)
Petr Štětka's avatar
Petr Štětka committed
17
        {
Petr Štětka's avatar
Petr Štětka committed
18
            this.type = type;
19
            showing_details = opened;
20
            this.process = process;
Petr Štětka's avatar
Petr Štětka committed
21

22 23 24 25
            var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
            box.margin = 0;
			var row_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
			row_box.margin = 10;
Petr Štětka's avatar
Petr Štětka committed
26
        	load_label = new Gtk.Label(null);
Petr Štětka's avatar
Petr Štětka committed
27 28
        	load_label.ellipsize = Pango.EllipsizeMode.END;
        	load_label.max_width_chars = 30;
Petr Štětka's avatar
Petr Štětka committed
29

30 31 32 33 34
            Gtk.Image icon = load_icon(process.get_display_name());
            icon.margin_left = 10;
            icon.margin_right = 10;

            title_label = new Gtk.Label(process.get_display_name());
35 36 37 38
            row_box.pack_start(icon, false, false, 0);
            row_box.pack_start(title_label, false, true, 5);
            row_box.pack_end(load_label, false, true, 10);
            box.pack_start(row_box, false, true, 0);
Petr Štětka's avatar
Petr Štětka committed
39

40
            if(process.get_sub_processes() != null)
41
            {
42
                group = true;
Petr Štětka's avatar
Petr Štětka committed
43
                sub_process_list_box = new SubProcessListBox(process, type);
44 45 46
                revealer = new Gtk.Revealer();
                revealer.add(sub_process_list_box);
                box.pack_end(revealer, false, true, 0);
Petr Štětka's avatar
Petr Štětka committed
47

48 49 50
                if(opened)
                    show_details();
            }
51 52 53 54 55
            else
                group = false;

            update();

56 57
            this.add(box);
            show_all();
Petr Štětka's avatar
Petr Štětka committed
58

59
            if(opened && group)
60 61
                show_details();
        }
Petr Štětka's avatar
Petr Štětka committed
62

63
        private Gtk.Image load_icon(string display_name)
64
        {
65 66
            Gtk.Image icon = null;
        	foreach (AppInfo app_info in (GLib.Application.get_default() as Application).get_system_monitor().get_apps_info())
67
        	{
68
                if(app_info.get_display_name() == display_name)
69
                {
70
                    if(app_info.get_icon() != null)
71
                    {
72 73 74
                        var icon_theme = new Gtk.IconTheme();
                        var icon_info = icon_theme.lookup_by_gicon_for_scale(app_info.get_icon(), 24, 1, Gtk.IconLookupFlags.FORCE_SIZE);
                        if(icon_info != null)
Petr Štětka's avatar
Petr Štětka committed
75
                        {
76 77 78 79 80 81 82 83
                            try
                            {
                                var pixbuf = icon_info.load_icon();
                                icon = new Gtk.Image.from_pixbuf(pixbuf);
                            }
                            catch(Error e) {
                                GLib.stderr.printf ("Could not load icon for application %s: %s\n", display_name, e.message);
                            }
Petr Štětka's avatar
Petr Štětka committed
84

85
                        }
86 87
                    }
                }
88
        	}
89

Petr Štětka's avatar
Petr Štětka committed
90
        	if(icon == null)
91 92 93 94 95
            {
                icon = new Gtk.Image.from_icon_name("system-run-symbolic", Gtk.IconSize.BUTTON);
                icon.width_request = 24;
                icon.height_request = 24;
            }
96

97
        	return icon;
98 99
        }

Petr Štětka's avatar
Petr Štětka committed
100 101
        private void update()
        {
102 103
            CompareFunc<uint64?> sort = (a, b) => {
                return (int) ((uint64) (a < b) - (uint64) (a > b));
Petr Štětka's avatar
Petr Štětka committed
104 105
            };

Petr Štětka's avatar
Petr Štětka committed
106
            switch(type)
Petr Štětka's avatar
Petr Štětka committed
107
            {
Petr Štětka's avatar
Petr Štětka committed
108
                case ProcessListBoxType.PROCESSOR:
109
                    if(group)
Petr Štětka's avatar
Petr Štětka committed
110
                    {
Petr Štětka's avatar
Petr Štětka committed
111
                        string values_string = "";
112
                        var values = new GLib.List<uint64?>();
113 114
                        foreach(Process sub_process in process.get_sub_processes().get_values())
                            values.insert_sorted((uint64) sub_process.get_cpu_load(), sort);
Petr Štětka's avatar
Petr Štětka committed
115

116
                        foreach(uint64 value in values)
Petr Štětka's avatar
Petr Štětka committed
117
                            values_string += "   " + value.to_string() + " %";
Petr Štětka's avatar
Petr Štětka committed
118

119
                        title_label.label += " (" + process.get_sub_processes().size().to_string() + ")";
Petr Štětka's avatar
Petr Štětka committed
120
                        load_label.set_label(values_string);
Petr Štětka's avatar
Petr Štětka committed
121 122
                    }
                    else
123
                        load_label.set_label(((int) process.get_cpu_load()).to_string() + " %");
Petr Štětka's avatar
Petr Štětka committed
124

125
                    if(process.get_cpu_load() >= 90)
Petr Štětka's avatar
Petr Štětka committed
126 127 128 129 130
                        max_usage = true;
                    else
                        max_usage = false;
                    break;
                case ProcessListBoxType.MEMORY:
131 132
                    SystemMonitor monitor = (GLib.Application.get_default() as Application).get_system_monitor();

133
                    if(group)
Petr Štětka's avatar
Petr Štětka committed
134
                    {
Petr Štětka's avatar
Petr Štětka committed
135
                        string values_string = "";
136
                        var values = new GLib.List<uint64?>();
137 138
                        foreach(Process sub_process in process.get_sub_processes().get_values())
                            values.insert_sorted(sub_process.get_mem_usage(), sort);
Petr Štětka's avatar
Petr Štětka committed
139

140
                        foreach(uint64 value in values)
141
                            values_string += "   " + Utils.format_size_values(value);
Petr Štětka's avatar
Petr Štětka committed
142

143
                        title_label.label += " (" + process.get_sub_processes().size().to_string() + ")";
Petr Štětka's avatar
Petr Štětka committed
144
                        load_label.set_label(values_string);
Petr Štětka's avatar
Petr Štětka committed
145 146
                    }
                    else
147
                        load_label.set_label(Utils.format_size_values(process.get_mem_usage()));
Petr Štětka's avatar
Petr Štětka committed
148

149
                    if((((double) process.get_mem_usage() / monitor.ram_total) * 100) >= 90)
Petr Štětka's avatar
Petr Štětka committed
150 151 152 153
                        max_usage = true;
                    else
                        max_usage = false;
                    break;
154
                case ProcessListBoxType.NETWORK:
155
                    if(group)
156 157
                    {
                        string values_string = "";
158
                        var values = new GLib.List<uint64?>();
159 160
                        foreach(Process sub_process in process.get_sub_processes().get_values())
                            values.insert_sorted((uint64) sub_process.get_net_all(), sort);
161

162
                         foreach(uint64 value in values)
163
                            values_string += "   " + Utils.format_size_speed_values(value);
164

165
                        title_label.label += " (" + process.get_sub_processes().size().to_string() + ")";
166 167 168 169
                        load_label.set_label(values_string);
                    }
                    else
                    {
170
                        load_label.set_label(Utils.format_size_speed_values(process.get_net_all()));
171 172
                    }
                    break;
Petr Štětka's avatar
Petr Štětka committed
173
            }
Petr Štětka's avatar
Petr Štětka committed
174 175

            set_styles();
Petr Štětka's avatar
Petr Štětka committed
176 177 178 179 180 181 182
        }

        private void hide_details()
        {
            showing_details = false;
            revealer.set_reveal_child(false);
            load_label.visible = true;
183
            get_style_context().remove_class("opened");
Petr Štětka's avatar
Petr Štětka committed
184 185 186 187 188 189 190
        }

        private void show_details()
        {
            showing_details = true;
            revealer.set_reveal_child(true);
            load_label.visible = false;
191
            get_style_context().add_class("opened");
Petr Štětka's avatar
Petr Štětka committed
192 193
        }

Petr Štětka's avatar
Petr Štětka committed
194
        public new void activate()
Petr Štětka's avatar
Petr Štětka committed
195
        {
196
            if(group)
197 198 199 200 201
            {
                if(showing_details)
                    hide_details();
                else
                    show_details();
202 203

                set_styles();
204
            }
Petr Štětka's avatar
Petr Štětka committed
205
            else
206
            {
207
                var dialog = new ProcessDialog(process.get_pid(), process.get_display_name(), process.get_cmdline());
Petr Štětka's avatar
Petr Štětka committed
208
                dialog.show_all();
209
            }
Petr Štětka's avatar
Petr Štětka committed
210
        }
211 212 213 214 215 216 217 218

        private void set_styles()
        {
            if(max_usage == true && showing_details == false)
                get_style_context().add_class("max");
            else
                get_style_context().remove_class("max");
        }
Petr Štětka's avatar
Petr Štětka committed
219 220
    }
}