Patch for battery-plugin - warn as soon as the battery is fully charged

Sacher Khoudari <[email protected]> Tue, 11 Apr 2006 12:00:10 +0200
Newsgroups gmane.comp.desktop.xfce.goodies.devel
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

Because my Thinkpad 600e has this annoying battery problem I need
something that warns me as soon as my battery is fully charged, to not
overcharge it. I had once written a small python-script. But because the
battery-plugin already warns you about almost-empty batteries, I had the
idea to now patch it for this purpose. I actually just did a copy &
paste & :s/low/full/g, so it's nothing special ;)

I'm not sure how many Xfce users will need it. I think most laptops
prevent the battery from being overcharged. But it won't harm anyone to
give the users the option to be warned. So you are free to use this
patch if you want to ;)

Greetings!
Sacher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFEO34lqj8R1rngt5kRAn0ZAKCnvCt4s2UuL2wcWLcPV81l/SkhSQCgisHi
JC86m6PN11uivvzTOm0YFwg=
=Nw+j
-----END PGP SIGNATURE-----
battmon.c.patch (text/x-patch, 9.8 KB)
--- xfce4-battery-plugin/panel-plugin/battmon.c	2006-04-08 11:17:03.000000000 +0200
+++ battmon.c	2006-04-08 17:56:57.000000000 +0200
@@ -75,8 +75,10 @@
 	int		critical_percentage;
 	int		action_on_low;
 	int		action_on_critical;
+	int		action_on_full;
 	char		*command_on_low;
 	char		*command_on_critical;
+	char		*command_on_full;
 	float		hsize;
 	float		vsize;
 } t_battmon_options;
@@ -92,6 +94,7 @@
 	gboolean		flag;
 	gboolean		low;
 	gboolean		critical;
+	gboolean		full;
 	t_battmon_options	options;
 	GdkColor		colorH;
 	GdkColor		colorL;
@@ -116,8 +119,10 @@
 	GtkWidget		*sb_critical_percentage;
 	GtkWidget		*om_action_low;
 	GtkWidget		*om_action_critical;
+	GtkWidget		*om_action_full;
 	GtkWidget		*en_command_low;
 	GtkWidget		*en_command_critical;
+	GtkWidget		*en_command_full;
 	Control			*ctrl;
 	GtkWidget		*revert;
 	t_battmon		*battmon;
@@ -143,8 +148,10 @@
 	options->critical_percentage = 5;
 	options->action_on_low = 0;
 	options->action_on_critical = 0;
+	options->action_on_full = 0;
 	options->command_on_low = NULL;
 	options->command_on_critical = NULL;
+	options->command_on_full = NULL;
 	options->hsize = 1.75;
 	options->vsize = 0.5;
 }
@@ -442,6 +449,7 @@
 	  gtk_widget_modify_bg(battmon->battstatus, GTK_STATE_PRELIGHT, NULL);
 	}
 	else {
+	  battmon->full = FALSE;
 	  if(charge <= battmon->options.critical_percentage) {
 		gtk_widget_modify_bg(battmon->battstatus, GTK_STATE_PRELIGHT, &(battmon->colorC));
 	  } 
@@ -489,6 +497,22 @@
 				exec_cmd(battmon->options.command_on_low, interm, 0);
 			}
 		}
+	} else if ( acline && charge == 100 ) {
+		if ( !battmon->full ) {
+		    battmon->full = TRUE;
+			if(battmon->options.action_on_full == BM_MESSAGE){
+do_full_warn:
+				xfce_warn(_("WARNING: Your battery is now fully charged. You should consider plugging out your AC cable to not overcharge your battery."));
+				return TRUE;
+			}
+			if(battmon->options.action_on_full == BM_COMMAND ||
+			   battmon->options.action_on_full == BM_COMMAND_TERM){
+				int interm=(battmon->options.action_on_full == BM_COMMAND_TERM)?1:0;
+				if (!battmon->options.command_on_full ||
+				    !strlen(battmon->options.command_on_full)) goto do_full_warn;
+				exec_cmd(battmon->options.command_on_full, interm, 0);
+			}
+		}
 	}
 	return TRUE;
 }
@@ -709,6 +733,10 @@
 		battmon->options.action_on_critical = atoi(value);
 		g_free(value);
 	}
+	if((value = xmlGetProp(child, (const xmlChar *) "action_on_full")) != NULL) {
+		battmon->options.action_on_full = atoi(value);
+		g_free(value);
+	}
 
 	if(!child || !child->children) return;
 
@@ -723,6 +751,11 @@
 			if(battmon->options.command_on_critical) g_free(battmon->options.command_on_critical);
 			if(value) battmon->options.command_on_critical = (char *)value;
 		}
+		if(xmlStrEqual(child->name, (const xmlChar *) "command_on_full")) {
+			value = DATA(child);
+			if(battmon->options.command_on_full) g_free(battmon->options.command_on_full);
+			if(value) battmon->options.command_on_full = (char *)value;
+		}
 	}
 }
 
@@ -766,11 +799,15 @@
 	/* Action on Critical */
 	g_snprintf(value, 2, "%d", battmon->options.action_on_critical);
 	xmlSetProp(root, "action_on_critical", value);
+	/* Action on Full */
+	g_snprintf(value, 2, "%d", battmon->options.action_on_full);
+	xmlSetProp(root, "action_on_full", value);
 	/* Command on Low */
 	xmlNewTextChild(root, NULL, "command_on_low", battmon->options.command_on_low);
 	/* Command on Critical */
-
 	xmlNewTextChild(root, NULL, "command_on_critical", battmon->options.command_on_critical);
+	/* Command on Full */
+	xmlNewTextChild(root, NULL, "command_on_full", battmon->options.command_on_full);
 
 }
 
@@ -835,6 +872,11 @@
 		gtk_entry_set_text(GTK_ENTRY(dialog->en_command_critical), battmon->options.command_on_critical);
 	else
 		gtk_entry_set_text(GTK_ENTRY(dialog->en_command_critical), "");
+	gtk_option_menu_set_history(GTK_OPTION_MENU(dialog->om_action_full), battmon->options.action_on_full);
+	if(battmon->options.command_on_full)
+		gtk_entry_set_text(GTK_ENTRY(dialog->en_command_full), battmon->options.command_on_full);
+	else
+		gtk_entry_set_text(GTK_ENTRY(dialog->en_command_full), "");
 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_label), battmon->options.display_label);
 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_icon), battmon->options.display_icon);
 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_power), battmon->options.display_power);
@@ -843,6 +885,7 @@
 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cb_disp_tooltip_time), battmon->options.tooltip_display_time);
 	gtk_widget_set_sensitive(dialog->en_command_low, (battmon->options.action_on_low > 1) ? 1 : 0);
 	gtk_widget_set_sensitive(dialog->en_command_critical, (battmon->options.action_on_critical > 1) ? 1 : 0);
+	gtk_widget_set_sensitive(dialog->en_command_full, (battmon->options.action_on_full > 1) ? 1 : 0);
 }
 
 /* No longer required for the new plugin API
@@ -975,6 +1018,18 @@
 /*	gtk_widget_set_sensitive(dialog->revert, TRUE);	*/
 }
 
+static void
+set_action_full(GtkOptionMenu *om, t_battmon_dialog *dialog)
+{
+	t_battmon *battmon = dialog->battmon;
+
+	battmon->options.action_on_full = gtk_option_menu_get_history(om);
+
+	gtk_widget_set_sensitive(dialog->en_command_full, (gtk_option_menu_get_history(om) > 1) ? 1 : 0);
+	update_apm_status(dialog->battmon);
+/*	gtk_widget_set_sensitive(dialog->revert, TRUE);	*/
+}
+
 gboolean
 set_command_low(GtkEntry *en, GdkEventFocus *event, t_battmon_dialog *dialog)
 {
@@ -1009,6 +1064,23 @@
 	return FALSE;
 }
 
+gboolean
+set_command_full(GtkEntry *en, GdkEventFocus *event, t_battmon_dialog *dialog)
+{
+	t_battmon *battmon = dialog->battmon;
+	const char *temp;
+
+	g_free(battmon->options.command_on_full);
+	temp = gtk_entry_get_text(en);
+	battmon->options.command_on_full = g_strdup(temp); 
+	update_apm_status(dialog->battmon);
+
+/*	gtk_widget_set_sensitive(dialog->revert, TRUE);	*/
+
+	/* Prevents a GTK crash */
+	return FALSE;
+}
+
 static void
 command_browse_cb (GtkWidget *b, GtkEntry *entry)
 {
@@ -1026,7 +1098,7 @@
 	t_battmon *battmon = ctrl->data;
 	t_battmon_dialog *dialog;
 
-	GtkWidget *vbox, *vbox2, *hbox, *label, *menu, *mi, *button, *button2;
+	GtkWidget *vbox, *vbox2, *hbox, *label, *menu, *mi, *button, *button2, *button3;
 	GtkSizeGroup *sg;
 
 	dialog = g_new0(t_battmon_dialog, 1);
@@ -1183,6 +1255,57 @@
     	gtk_widget_show(button2);
     	gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);
 
+	/* Full battery action settings */
+
+	hbox = gtk_hbox_new(FALSE, 4);	
+	gtk_widget_show(hbox);
+	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+
+	label = gtk_label_new(_("Full battery action:"));
+	gtk_size_group_add_widget(sg, label);
+	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
+	gtk_widget_show(label);
+	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
+
+	menu = gtk_menu_new();
+ 	mi = gtk_menu_item_new_with_label(_("Do nothing"));
+    	gtk_widget_show(mi);
+    	gtk_menu_shell_append(GTK_MENU_SHELL(menu), mi);
+ 	mi = gtk_menu_item_new_with_label(_("Display a warning message"));
+    	gtk_widget_show(mi);
+    	gtk_menu_shell_append(GTK_MENU_SHELL(menu), mi);
+	mi = gtk_menu_item_new_with_label(_("Run command"));
+    	gtk_widget_show(mi);
+    	gtk_menu_shell_append(GTK_MENU_SHELL(menu), mi);
+ 	mi = gtk_menu_item_new_with_label(_("Run command in terminal"));
+    	gtk_widget_show(mi);
+    	gtk_menu_shell_append(GTK_MENU_SHELL(menu), mi);
+
+	dialog->om_action_full = gtk_option_menu_new();
+	gtk_widget_show(dialog->om_action_full);
+	gtk_option_menu_set_menu(GTK_OPTION_MENU(dialog->om_action_full), menu);
+	gtk_box_pack_start(GTK_BOX(hbox), dialog->om_action_full, FALSE, FALSE, 0);
+
+	/* Full battery command */
+
+	hbox = gtk_hbox_new(FALSE, 4);
+    	gtk_widget_show(hbox);
+  	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
+
+    	label = gtk_label_new(_("Command:"));
+    	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
+    	gtk_size_group_add_widget(sg, label);
+    	gtk_widget_show(label);
+	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
+
+    	dialog->en_command_full = gtk_entry_new();
+	gtk_widget_show(dialog->en_command_full);
+    	gtk_box_pack_start(GTK_BOX(hbox), dialog->en_command_full, FALSE, FALSE, 0);
+
+    	button3 = gtk_button_new_with_label("...");
+    	gtk_widget_show(button3);
+    	gtk_box_pack_start(GTK_BOX(hbox), button3, FALSE, FALSE, 0);
+
 	/* Create checkbox options */
 
 	hbox = gtk_hbox_new(FALSE, 4);	
@@ -1227,6 +1350,7 @@
 
 	g_signal_connect(button, "clicked", G_CALLBACK(command_browse_cb), dialog->en_command_low);
 	g_signal_connect(button2, "clicked", G_CALLBACK(command_browse_cb), dialog->en_command_critical);
+	g_signal_connect(button3, "clicked", G_CALLBACK(command_browse_cb), dialog->en_command_full);
 	g_signal_connect(dialog->cb_disp_percentage, "toggled", G_CALLBACK(set_disp_percentage), dialog);
 	g_signal_connect(dialog->cb_disp_tooltip_percentage, "toggled", G_CALLBACK(set_tooltip_disp_percentage), dialog);
 	g_signal_connect(dialog->cb_disp_power, "toggled", G_CALLBACK(set_disp_power), dialog);
@@ -1238,8 +1362,10 @@
 	g_signal_connect(dialog->sb_critical_percentage, "value-changed", G_CALLBACK(set_critical_percentage), dialog);
 	g_signal_connect(dialog->om_action_low, "changed", G_CALLBACK(set_action_low), dialog);
 	g_signal_connect(dialog->om_action_critical, "changed", G_CALLBACK(set_action_critical), dialog);
+	g_signal_connect(dialog->om_action_full, "changed", G_CALLBACK(set_action_full), dialog);
 	g_signal_connect(dialog->en_command_low, "focus-out-event", G_CALLBACK(set_command_low), dialog);
 	g_signal_connect(dialog->en_command_critical, "focus-out-event", G_CALLBACK(set_command_critical), dialog);
+	g_signal_connect(dialog->en_command_full, "focus-out-event", G_CALLBACK(set_command_full), dialog);
 /*	g_signal_connect_swapped(revert, "clicked", G_CALLBACK(battmon_restore_backup), dialog);	*/
 
 }