process-row.vala 7.35 KB
Newer Older
1
2
using Posix;

Petr Štětka's avatar
Petr Štětka committed
3
4
namespace Usage
{
Petr Štětka's avatar
Petr Štětka committed
5
    public class ProcessRow : Gtk.Box
Petr Štětka's avatar
Petr Štětka committed
6
7
8
9
10
11
    {
		Gtk.Image icon;
        Gtk.Label title_label;
        Gtk.Label load_label;
        Gtk.Revealer revealer;
        Gtk.EventBox event_box;
Petr Štětka's avatar
Petr Štětka committed
12
        SubProcessListBox sub_process_list_box;
Petr Štětka's avatar
Petr Štětka committed
13
        bool in_box = false;
Petr Štětka's avatar
Petr Štětka committed
14
        pid_t pid;
Petr Štětka's avatar
Petr Štětka committed
15
        int value;
Petr Štětka's avatar
Petr Štětka committed
16
        string cmdline;
Petr Štětka's avatar
Petr Štětka committed
17
18
        bool alive = true;
        bool group = false;
Petr Štětka's avatar
Petr Štětka committed
19
20
21
22
23

        //public bool is_headline { get; private set; }
        public bool showing_details { get; private set; }
        public bool max_usage { get; private set; }

Petr Štětka's avatar
Petr Štětka committed
24
        public ProcessRow(pid_t pid, int value, string cmdline)
Petr Štětka's avatar
Petr Štětka committed
25
26
        {
            this.pid = pid;
Petr Štětka's avatar
Petr Štětka committed
27
            this.cmdline = cmdline;
Petr Štětka's avatar
Petr Štětka committed
28
29
30
31
32

            this.margin = 0;
            this.orientation = Gtk.Orientation.VERTICAL;
			var main_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
			main_box.margin = 12;
Petr Štětka's avatar
Petr Štětka committed
33
        	title_label = new Gtk.Label(cmdline);  //TODO implement give name
Petr Štětka's avatar
Petr Štětka committed
34
        	load_label = new Gtk.Label(null);
Petr Štětka's avatar
Petr Štětka committed
35
36
        	load_label.ellipsize = Pango.EllipsizeMode.END;
        	load_label.max_width_chars = 30;
Petr Štětka's avatar
Petr Štětka committed
37
38
39
40
41
42
        	icon = new Gtk.Image.from_icon_name("dialog-error", Gtk.IconSize.BUTTON); //TODO implement give icon

            main_box.pack_start(icon, false, false, 10);
            main_box.pack_start(title_label, false, true, 5);
            main_box.pack_end(load_label, false, true, 10);

Petr Štětka's avatar
Petr Štětka committed
43
            sub_process_list_box = new SubProcessListBox();
Petr Štětka's avatar
Petr Štětka committed
44
45
46
47
48
49
50
51

            revealer = new Gtk.Revealer();
            revealer.add(sub_process_list_box);

            event_box = new Gtk.EventBox();
            event_box.add(main_box);

            event_box.button_press_event.connect ((event) => {
Petr Štětka's avatar
Petr Štětka committed
52
53
                if(group)
                    switch_details();
Petr Štětka's avatar
Petr Štětka committed
54
55
                else
                {
Petr Štětka's avatar
Petr Štětka committed
56
                    var dialog = new ProcessDialog(pid, cmdline);
Petr Štětka's avatar
Petr Štětka committed
57
58
59
                    dialog.show_all ();
                }

Petr Štětka's avatar
Petr Štětka committed
60
61
62
63
64
                return false;
            });

            event_box.enter_notify_event.connect ((event) => {
                in_box = true;
Petr Štětka's avatar
Petr Štětka committed
65
                style_css();
Petr Štětka's avatar
Petr Štětka committed
66
67
68
69
70
                return false;
            });

            event_box.leave_notify_event.connect ((event) => {
                in_box = false;
Petr Štětka's avatar
Petr Štětka committed
71
                style_css();
Petr Štětka's avatar
Petr Štětka committed
72
73
74
75
76
77
78
                return false;
            });

            var separator = new Gtk.Separator(Gtk.Orientation.VERTICAL);

            this.pack_start(event_box, false, true, 0);
            this.pack_end(revealer, false, true, 0);
Petr Štětka's avatar
Petr Štětka committed
79
80
            this.pack_end(separator, false, true, 0); //TODO Fix for last element

Petr Štětka's avatar
Petr Štětka committed
81
            set_value(pid, value);
Petr Štětka's avatar
Petr Štětka committed
82
83
84
85

            show_all();
        }

Petr Štětka's avatar
Petr Štětka committed
86
87
88
89
90
91
92
93
94
95
96
97
98
        public void pre_update()
        {
            alive = false;
            sub_process_list_box.pre_update();
        }

        public void post_update()
        {
            sub_process_list_box.post_update();

            if(sub_process_list_box.get_sub_rows_count() == 1)
            {
                group = false;
Petr Štětka's avatar
Petr Štětka committed
99
                cmdline = sub_process_list_box.get_first_cmdline();
Petr Štětka's avatar
Petr Štětka committed
100
                pid = sub_process_list_box.get_first_pid();
Petr Štětka's avatar
Petr Štětka committed
101
                set_value(pid, sub_process_list_box.get_first_value());
Petr Štětka's avatar
Petr Štětka committed
102
103
104
105
106
107
108
109
                sub_process_list_box.remove_all();
                if(showing_details)
                    hide_details();
            }

            update();
        }

110
        public bool is_in_subrows(pid_t pid)
Petr Štětka's avatar
Petr Štětka committed
111
112
113
114
        {
            return sub_process_list_box.is_in_table(pid);
        }

Petr Štětka's avatar
Petr Štětka committed
115
        public void add_sub_row(pid_t pid, int value, string cmdline)
Petr Štětka's avatar
Petr Štětka committed
116
117
118
119
        {
            alive = true;
            if(sub_process_list_box.get_sub_rows_count() == 0)
            {
Petr Štětka's avatar
Petr Štětka committed
120
                sub_process_list_box.add_sub_row(this.pid, this.value, this.cmdline);
Petr Štětka's avatar
Petr Štětka committed
121
122
                group = true;
            }
Petr Štětka's avatar
Petr Štětka committed
123
            sub_process_list_box.add_sub_row(pid, value, cmdline);
Petr Štětka's avatar
Petr Štětka committed
124
125
        }

126
        public void update_sub_row(pid_t pid, int value)
Petr Štětka's avatar
Petr Štětka committed
127
128
129
130
131
        {
            alive = true;
            sub_process_list_box.update_sub_row(pid, value);
        }

Petr Štětka's avatar
Petr Štětka committed
132
        public string get_cmdline()
Petr Štětka's avatar
Petr Štětka committed
133
        {
Petr Štětka's avatar
Petr Štětka committed
134
            return cmdline;
Petr Štětka's avatar
Petr Štětka committed
135
136
        }

Petr Štětka's avatar
Petr Štětka committed
137
138
139
140
141
        public bool get_alive()
        {
            return alive;
        }

Petr Štětka's avatar
Petr Štětka committed
142
143
144
145
146
147
148
149
150
151
        public uint get_pid()
        {
            return pid;
        }

        public int get_value()
        {
            return value;
        }

152
        public void set_value(pid_t pid, int value)
Petr Štětka's avatar
Petr Štětka committed
153
        {
Petr Štětka's avatar
Petr Štětka committed
154
            alive = true;
Petr Štětka's avatar
Petr Štětka committed
155
156
            if(sub_process_list_box.is_in_table(pid))
                sub_process_list_box.update_sub_row(pid, value);
Petr Štětka's avatar
Petr Štětka committed
157
            else
Petr Štětka's avatar
Petr Štětka committed
158
                if(group)
Petr Štětka's avatar
Petr Štětka committed
159
                   sub_process_list_box.add_sub_row(pid, value, cmdline);
Petr Štětka's avatar
Petr Štětka committed
160
                else
Petr Štětka's avatar
Petr Štětka committed
161
162
163
164
165
166
167
168
169
170
                {
                    if(pid == this.pid)
                        this.value = int.min(value, 100);
                    else
                    {
                        this.pid = pid;
                        this.value = int.min(value, 100);
                    }

                }
Petr Štětka's avatar
Petr Štětka committed
171
172
173
174
        }

        private void update()
        {
Petr Štětka's avatar
Petr Štětka committed
175
176
177
178
179
180
            if(group)
            {
                int max_value = 0;
                string values = "";
                foreach(int sub_value in sub_process_list_box.get_values())
                {
181
                    values += "   " + sub_value.to_string() + " %";
Petr Štětka's avatar
Petr Štětka committed
182
183
184
185
186
187
188
189
                    
                    if(sub_value > max_value)
                        max_value = sub_value;
                }
                this.value = max_value;

                load_label.set_label(values);
            }
Petr Štětka's avatar
Petr Štětka committed
190
            else
191
                load_label.set_label(value.to_string() + " %");
Petr Štětka's avatar
Petr Štětka committed
192

Petr Štětka's avatar
Petr Štětka committed
193
194
195
196
             if(value >= 90)
                 max_usage = true;
             else
                 max_usage = false;
Petr Štětka's avatar
Petr Štětka committed
197

Petr Štětka's avatar
Petr Štětka committed
198
            style_css();
Petr Štětka's avatar
Petr Štětka committed
199
200
        }

Petr Štětka's avatar
Petr Štětka committed
201
        private void style_css()
Petr Štětka's avatar
Petr Štětka committed
202
        {
Petr Štětka's avatar
Petr Štětka committed
203
204
205
206
207
            event_box.get_style_context().remove_class("processRow-max");
            event_box.get_style_context().remove_class("processRow-max-hover");
            event_box.get_style_context().remove_class("processRow");
            event_box.get_style_context().remove_class("processRow-hover");
            event_box.get_style_context().remove_class("processRow-opened");
Petr Štětka's avatar
Petr Štětka committed
208

Petr Štětka's avatar
Petr Štětka committed
209
210
            if(showing_details)
                event_box.get_style_context().add_class("processRow-opened");
Petr Štětka's avatar
Petr Štětka committed
211
212
            else
            {
Petr Štětka's avatar
Petr Štětka committed
213
                if(max_usage)
Petr Štětka's avatar
Petr Štětka committed
214
                {
Petr Štětka's avatar
Petr Štětka committed
215
216
217
218
                    if(in_box)
                        event_box.get_style_context().add_class("processRow-max-hover");
                    else
                        event_box.get_style_context().add_class("processRow-max");
Petr Štětka's avatar
Petr Štětka committed
219
220
221
222
                }
                else
                {
                    if(in_box)
Petr Štětka's avatar
Petr Štětka committed
223
                        event_box.get_style_context().add_class("processRow-hover");
Petr Štětka's avatar
Petr Štětka committed
224
                    else
Petr Štětka's avatar
Petr Štětka committed
225
                        event_box.get_style_context().add_class("processRow");
Petr Štětka's avatar
Petr Štětka committed
226
227
228
229
230
231
232
233
234
                }
            }
        }

        private void hide_details()
        {
            showing_details = false;
            revealer.set_reveal_child(false);
            load_label.visible = true;
Petr Štětka's avatar
Petr Štětka committed
235
            style_css();
Petr Štětka's avatar
Petr Štětka committed
236
237
238
239
240
241
242
        }

        private void show_details()
        {
            showing_details = true;
            revealer.set_reveal_child(true);
            load_label.visible = false;
Petr Štětka's avatar
Petr Štětka committed
243
            style_css();
Petr Štětka's avatar
Petr Štětka committed
244
245
246
247
248
249
250
251
252
253
254
        }

        private void switch_details()
        {
            if(showing_details)
                hide_details();
            else
                show_details();
        }
    }
}