1 /*
2  * Copyright (c) 2010 Mike Massonnet, <mmassonnet@xfce.org>
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 2 of the License, or (at
7  * your option) any later version.
8  */
9 
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13 
14 #include <glib-object.h>
15 #include <glib/gi18n.h>
16 #include <gtk/gtk.h>
17 
18 #include "process-statusbar.h"
19 #include "settings.h"
20 
21 
22 
23 enum
24 {
25 	PROP_CPU = 1,
26 	PROP_MEMORY,
27 	PROP_SWAP,
28 	PROP_SHOW_SWAP,
29 	PROP_NUM_PROCESSES,
30 };
31 typedef struct _XtmProcessStatusbarClass XtmProcessStatusbarClass;
32 struct _XtmProcessStatusbarClass
33 {
34 	GtkStatusbarClass	parent_class;
35 };
36 struct _XtmProcessStatusbar
37 {
38 	GtkHBox			parent;
39 	/*<private>*/
40 	XtmSettings *		settings;
41 
42 	GtkWidget *		label_num_processes;
43 	GtkWidget *		label_cpu;
44 	GtkWidget *		label_memory;
45 	GtkWidget *		label_swap;
46 
47 	gfloat			cpu;
48 	gchar			memory[64];
49 	gchar			swap[64];
50 	guint			num_processes;
51 };
52 G_DEFINE_TYPE (XtmProcessStatusbar, xtm_process_statusbar, GTK_TYPE_BOX)
53 
54 static void	xtm_process_statusbar_finalize			(GObject *object);
55 static void	xtm_process_statusbar_set_property		(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
56 
57 
58 
59 static void
xtm_process_statusbar_class_init(XtmProcessStatusbarClass * klass)60 xtm_process_statusbar_class_init (XtmProcessStatusbarClass *klass)
61 {
62 	GObjectClass *class = G_OBJECT_CLASS (klass);
63 	xtm_process_statusbar_parent_class = g_type_class_peek_parent (klass);
64 	class->finalize = xtm_process_statusbar_finalize;
65 	class->set_property = xtm_process_statusbar_set_property;
66 	g_object_class_install_property (class, PROP_CPU,
67 		g_param_spec_float ("cpu", "CPU", "CPU usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
68 	g_object_class_install_property (class, PROP_MEMORY,
69 		g_param_spec_string ("memory", "Memory", "Memory usage", "", G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
70 	g_object_class_install_property (class, PROP_SWAP,
71 		g_param_spec_string ("swap", "Swap", "Swap usage", "", G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
72 	g_object_class_install_property (class, PROP_SHOW_SWAP,
73 		g_param_spec_boolean ("show-swap", "ShowSwap", "Show or hide swap usage", TRUE, G_PARAM_WRITABLE));
74 	g_object_class_install_property (class, PROP_NUM_PROCESSES,
75 		g_param_spec_uint ("num-processes", "NumProcesses", "Number of processes", 0, G_MAXUINT, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
76 }
77 
78 static void
xtm_process_statusbar_init(XtmProcessStatusbar * statusbar)79 xtm_process_statusbar_init (XtmProcessStatusbar *statusbar)
80 {
81 	GtkWidget *hbox, *hbox_cpu, *hbox_mem;
82 	statusbar->settings = xtm_settings_get_default ();
83 
84 	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 16);
85 	hbox_cpu = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 16);
86 	hbox_mem = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 16);
87 
88 	statusbar->label_cpu = gtk_label_new (NULL);
89 	gtk_box_pack_start (GTK_BOX (hbox_cpu), statusbar->label_cpu, TRUE, FALSE, 0);
90 
91 	statusbar->label_num_processes = gtk_label_new (NULL);
92 	gtk_box_pack_start (GTK_BOX (hbox_cpu), statusbar->label_num_processes, TRUE, FALSE, 0);
93 
94 	statusbar->label_memory = gtk_label_new (NULL);
95 	gtk_label_set_ellipsize (GTK_LABEL (statusbar->label_memory), PANGO_ELLIPSIZE_END);
96 	gtk_box_pack_start (GTK_BOX (hbox_mem), statusbar->label_memory, TRUE, FALSE, 0);
97 
98 	statusbar->label_swap = gtk_label_new (NULL);
99 	gtk_label_set_ellipsize (GTK_LABEL (statusbar->label_swap), PANGO_ELLIPSIZE_END);
100 	gtk_box_pack_start (GTK_BOX (hbox_mem), statusbar->label_swap, TRUE, FALSE, 0);
101 
102 	gtk_box_pack_start (GTK_BOX (hbox), hbox_cpu, TRUE, TRUE, 0);
103 	gtk_box_pack_start (GTK_BOX (hbox), hbox_mem, TRUE, TRUE, 0);
104 	gtk_box_set_homogeneous (GTK_BOX (hbox), TRUE);
105 
106 	gtk_box_pack_start (GTK_BOX (statusbar), hbox, TRUE, TRUE, 0);
107 
108 	gtk_widget_show_all (hbox);
109 }
110 
111 static void
xtm_process_statusbar_finalize(GObject * object)112 xtm_process_statusbar_finalize (GObject *object)
113 {
114 	g_object_unref (XTM_PROCESS_STATUSBAR (object)->settings);
115 	G_OBJECT_CLASS (xtm_process_statusbar_parent_class)->finalize (object);
116 }
117 
118 static gchar *
rounded_float_value(gfloat value,XtmSettings * settings)119 rounded_float_value (gfloat value, XtmSettings *settings)
120 {
121 	gboolean precision;
122 	g_object_get (settings, "more-precision", &precision, NULL);
123 	return g_strdup_printf ((precision) ? "%.2f" : "%.0f", value);
124 }
125 
126 static void
xtm_process_statusbar_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)127 xtm_process_statusbar_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
128 {
129 	XtmProcessStatusbar *statusbar = XTM_PROCESS_STATUSBAR (object);
130 	gchar *text;
131 	gchar *float_value;
132 	GdkRGBA color;
133 
134 	switch (property_id)
135 	{
136 		case PROP_CPU:
137 		statusbar->cpu = g_value_get_float (value);
138 		float_value = rounded_float_value (statusbar->cpu, statusbar->settings);
139 		text = g_strdup_printf (_("CPU: %s%%"), float_value);
140 		gtk_label_set_text (GTK_LABEL (statusbar->label_cpu), text);
141 		gdk_rgba_parse (&color, "#ff6e00");
142 		gtk_widget_override_color (statusbar->label_cpu, GTK_STATE_NORMAL, &color);
143 		g_free (float_value);
144 		g_free (text);
145 		break;
146 
147 		case PROP_MEMORY:
148 		g_strlcpy(statusbar->memory, g_value_get_string (value), sizeof(statusbar->memory));
149 		text = g_strdup_printf (_("Memory: %s"), statusbar->memory);
150 		gtk_label_set_text (GTK_LABEL (statusbar->label_memory), text);
151 		gtk_widget_set_tooltip_text (statusbar->label_memory, text);
152 		gdk_rgba_parse (&color, "#cb386c");
153 		gtk_widget_override_color (statusbar->label_memory, GTK_STATE_NORMAL, &color);
154 		g_free (text);
155 		break;
156 
157 		case PROP_SWAP:
158 		g_strlcpy(statusbar->swap, g_value_get_string (value), sizeof(statusbar->swap));
159 		text = g_strdup_printf (_("Swap: %s"), statusbar->swap);
160 		gtk_label_set_text (GTK_LABEL (statusbar->label_swap), text);
161 		gtk_widget_set_tooltip_text (statusbar->label_swap, text);
162 		g_free (text);
163 		break;
164 
165 		case PROP_SHOW_SWAP:
166 		if (g_value_get_boolean (value))
167 			gtk_widget_show (statusbar->label_swap);
168 		else
169 			gtk_widget_hide (statusbar->label_swap);
170 		break;
171 
172 		case PROP_NUM_PROCESSES:
173 		statusbar->num_processes = g_value_get_uint (value);
174 		text = g_strdup_printf (_("Processes: %d"), statusbar->num_processes);
175 		gtk_label_set_text (GTK_LABEL (statusbar->label_num_processes), text);
176 		g_free (text);
177 		break;
178 
179 		default:
180 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
181 		break;
182 	}
183 }
184 
185 
186 
187 GtkWidget *
xtm_process_statusbar_new(void)188 xtm_process_statusbar_new (void)
189 {
190 	return g_object_new (XTM_TYPE_PROCESS_STATUSBAR, NULL);
191 }
192