[Patch] Newmail command for mailchecker

Bernhard Walle bernhard.walle at gmx.de
Sun Jan 11 21:59:30 CET 2004


Hello,

I wrote a patch for the mailchecker of the XFce4-Panel that executes a
command if new mail arrives. I use this to play a sound (just use the
"play", "aplay" or whatever command) but you can use it to display a
message with xmessage or something like this.

I didn't test heavily but it seems to work well. I hope that the patch
(or just the feature) an be included in HEAD.


Regards,
Bernhard

-- 

-------------- next part --------------
--- xfce4-panel-4.0.3.orig/plugins/mailcheck/mailcheck.c	2003-07-18 17:58:49.000000000 +0200
+++ xfce4-panel-4.0.3/plugins/mailcheck/mailcheck.c	2004-01-11 21:41:58.000000000 +0100
@@ -50,6 +50,7 @@
 {
     char *mbox;
     char *command;
+    char *newmail_command;
     gboolean term;
     gboolean use_sn;
     int interval;
@@ -135,6 +136,17 @@
 }
 
 static gboolean
+exec_newmail_cmd (t_mailcheck * mc)
+{
+    if (mc->newmail_command && strlen(mc->newmail_command) != 0)
+    {
+        exec_cmd (mc->newmail_command, FALSE, FALSE);
+    }
+
+    return FALSE;
+}
+
+static gboolean
 check_mail (t_mailcheck * mailcheck)
 {
     int mail;
@@ -153,8 +165,12 @@
 
     if (mail != mailcheck->status)
     {
-	mailcheck->status = mail;
+        if (mail == NEW_MAIL && mailcheck->status != NEW_MAIL)
+        {
+            g_idle_add ((GSourceFunc) exec_newmail_cmd, mailcheck);
+        }
 
+        mailcheck->status = mail;
 	g_idle_add ((GSourceFunc) set_mail_icon, mailcheck);
     }
 
@@ -222,6 +238,16 @@
 		mc->mbox = (char *) value;
 	    }
 	}
+        else if (xmlStrEqual (node->name, (const xmlChar *) "newmail-command"))
+        {
+            value = DATA (node);
+
+            if (value)
+            {
+                g_free (mc->newmail_command);
+                mc->newmail_command = (char *) value;
+            }
+        }
 	else if (xmlStrEqual (node->name, (const xmlChar *) "Command"))
 	{
 	    value = DATA (node);
@@ -288,6 +314,8 @@
 
     snprintf (value, 2, "%d", mc->use_sn);
     xmlSetProp (node, "sn", value);
+
+    xmlNewTextChild (root, NULL, "newmail-command", mc->newmail_command);
 }
 
 static void
@@ -321,6 +349,8 @@
     mailcheck->oldmail_pb = get_mailcheck_pixbuf (OLD_MAIL);
     mailcheck->newmail_pb = get_mailcheck_pixbuf (NEW_MAIL);
 
+    mailcheck->newmail_command = g_strdup("");
+
     mail = g_getenv ("MAIL");
 
     if (mail)
@@ -356,6 +386,7 @@
 
     g_free (mailcheck->mbox);
     g_free (mailcheck->command);
+    g_free (mailcheck->newmail_command);
 
     g_object_unref (mailcheck->nomail_pb);
     g_object_unref (mailcheck->oldmail_pb);
@@ -400,6 +431,7 @@
 
     /* backup */
     char *mbox;
+    char *newmail_command;
     char *command;
     gboolean term;
     gboolean use_sn;
@@ -410,6 +442,7 @@
 
     /* options */
     GtkWidget *mbox_entry;
+    GtkWidget *newmail_cmd_entry;
     GtkWidget *cmd_entry;
     GtkWidget *term_cb;
     GtkWidget *sn_cb;
@@ -424,6 +457,7 @@
     t_mailcheck *mc = md->mc;
 
     md->mbox = g_strdup (mc->mbox);
+    md->newmail_command = g_strdup (mc->newmail_command);
     md->command = g_strdup (mc->command);
     md->term = mc->term;
     md->use_sn = mc->use_sn;
@@ -466,6 +500,14 @@
 	mc->mbox = g_strdup (tmp);
     }
 
+    tmp = gtk_entry_get_text (GTK_ENTRY (md->newmail_cmd_entry));
+
+    if (tmp && *tmp)
+    {
+        g_free (mc->newmail_command);
+        mc->newmail_command = g_strdup (tmp);
+    }
+
     mc->interval =
 	gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
 					  (md->interval_spin));
@@ -518,8 +560,27 @@
     }
 }
 
+/* newmail command */
+static void
+newmail_cmd_brows_cb (GtkWidget * b, MailDialog * md)
+{
+    const char *text;
+    char *file;
+
+    text = gtk_entry_get_text (GTK_ENTRY (md->newmail_cmd_entry));
+
+    file = select_file_name (NULL, text, md->dialog);
+
+    if (file)
+    {
+        gtk_entry_set_text (GTK_ENTRY (md->newmail_cmd_entry), file);
+        g_free (file);
+        mailcheck_apply_options (md);
+    }
+}
+
 gboolean
-mbox_entry_lost_focus (MailDialog * md)
+entry_lost_focus (MailDialog * md)
 {
     mailcheck_apply_options (md);
 
@@ -527,6 +588,7 @@
     return FALSE;
 }
 
+
 static void
 add_mbox_box (GtkWidget * vbox, GtkSizeGroup * sg, MailDialog * md)
 {
@@ -560,9 +622,46 @@
 
     /* only set label on focus out */
     g_signal_connect_swapped (md->mbox_entry, "focus-out-event",
-			      G_CALLBACK (mbox_entry_lost_focus), md);
+			      G_CALLBACK (entry_lost_focus), md);
 }
 
+static void
+add_newmail_cmd_box (GtkWidget * vbox, GtkSizeGroup * sg, MailDialog * md)
+{
+    GtkWidget *hbox, *label, *button, *image;
+
+    hbox = gtk_hbox_new (FALSE, BORDER);
+    gtk_widget_show (hbox);
+    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
+
+    label = gtk_label_new (_("Newmail 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);
+
+    md->newmail_cmd_entry = gtk_entry_new ();
+    if (md->newmail_command)
+	gtk_entry_set_text (GTK_ENTRY (md->newmail_cmd_entry), md->newmail_command);
+    gtk_widget_show (md->newmail_cmd_entry);
+    gtk_box_pack_start (GTK_BOX (hbox), md->newmail_cmd_entry, TRUE, TRUE, 0);
+
+    button = gtk_button_new();
+    gtk_widget_show (button);
+    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
+
+    image = gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
+    gtk_widget_show(image);
+    gtk_container_add(GTK_CONTAINER(button), image);
+    
+    g_signal_connect (button, "clicked", G_CALLBACK (newmail_cmd_brows_cb), md);
+
+    /* only set label on focus out */
+    g_signal_connect_swapped (md->mbox_entry, "focus-out-event",
+			      G_CALLBACK (entry_lost_focus), md);
+}
+
+
 /* command */
 static void
 command_browse_cb (GtkWidget * b, MailDialog * md)
@@ -741,6 +840,8 @@
 
     add_mbox_box (vbox, sg, md);
 
+    add_newmail_cmd_box (vbox, sg, md);
+
     add_command_box (vbox, sg, md);
 
     add_interval_box (vbox, sg, md);


More information about the Xfce4-dev mailing list