[Xfce4-commits] <orage:master> 4.8.1.12 Implemented enhancement 7914 - Alarm Procedure Replacement Codes

Juha Kautto noreply at xfce.org
Mon Sep 5 14:30:01 CEST 2011


Updating branch refs/heads/master
         to 25595b12290fd5c20ac177f56a4c61a918416cec (commit)
       from 39df98ebb5ff0fe891b5b7d5e6385e1cc088f85d (commit)

commit 25595b12290fd5c20ac177f56a4c61a918416cec
Author: Juha Kautto <juha at xfce.org>
Date:   Mon Sep 5 15:24:31 2011 +0300

    4.8.1.12 Implemented enhancement 7914 - Alarm Procedure Replacement Codes
    
    Created the following codes in procedure alarms, which are replaced by
    corresponding texts in run time:
    <&T>  appointment title
    <&D>  appointment description
    <&AT> alarm time
    <&ST> appointment start time
    <&ET> appointment end time

 src/appointment.c |    2 +-
 src/functions.c   |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/functions.h   |    1 +
 src/reminder.c    |   42 ++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 89 insertions(+), 3 deletions(-)

diff --git a/src/appointment.c b/src/appointment.c
index f886362..a2b6596 100644
--- a/src/appointment.c
+++ b/src/appointment.c
@@ -3053,7 +3053,7 @@ static void build_alarm_page(appt_win *apptw)
 
     apptw->Proc_entry = gtk_entry_new();
     gtk_tooltips_set_tip(apptw->Tooltips, apptw->Proc_entry
-            , _("You must enter all escape etc characters yourself.\n This string is just given to shell to process"), NULL);
+            , _("You must enter all escape etc characters yourself.\nThis string is just given to shell to process.\nThe following special commands are replaced at run time:\n\t<&T>  appointment title\n\t<&D>  appointment description\n\t<&AT> alarm time\n\t<&ST> appointment start time\n\t<&ET> appointment end time"), NULL);
     gtk_box_pack_start(GTK_BOX(apptw->Proc_hbox), apptw->Proc_entry
             , TRUE, TRUE, 0);
 
diff --git a/src/functions.c b/src/functions.c
index ffc92a4..3587a1d 100644
--- a/src/functions.c
+++ b/src/functions.c
@@ -325,6 +325,53 @@ GtkWidget *orage_menu_item_new_with_mnemonic(const gchar *label
     return menu_item;
 }
 
+/* replace old with new string in text.
+   if changes are done, returns newly allocated char *, which needs to be freed
+   if there are no changes, it returns the original string without freeing it.
+   You can always use this like 
+   str=orage_replace_text(str, old, new);
+   but it may point to new place.
+*/
+char *orage_replace_text(char *text, char *old, char *new)
+{
+#undef P_N
+#define P_N "orage_replace_text: "
+    /* these point to the original string and travel it until no more commands 
+     * are found:
+     * cur points to the current head (after each old if any found)
+     * cmd points to the start of next old in text */
+    char *cur, *cmd;
+    char *beq=NULL; /* beq is the total new string. */
+    char *tmp;      /* temporary pointer to handle freeing */
+
+    for (cur = text; cur && (cmd = strstr(cur, old)); cur = cmd + strlen(old)) {
+        cmd[0] = '\0'; /* temporarily */
+        if (beq) { /* we already have done changes */
+            tmp = beq;
+            beq = g_strconcat(tmp, cur, new, NULL);
+            g_free(tmp);
+        }
+        else /* first round */
+            beq = g_strconcat(cur, new, NULL);
+        cmd[0] = old[0]; /* back to real value */
+    }
+
+    if (beq) {
+        /* we found and processed at least one command, 
+         * add the remaining fragment and return it */
+        tmp = beq;
+        beq = g_strconcat(tmp, cur, NULL);
+        g_free(tmp);
+        g_free(text); /* free original string as we changed it */
+    }
+    else {
+        /* we did not find any commands,
+         * so just return the original string */
+        beq = text;
+    }
+    return(beq);
+}
+
 /* this will change <&Xnnn> type commands to numbers or text as defined.
  * Currently the only command is 
  * <&Ynnnn> which calculates years between current year and nnnn */
diff --git a/src/functions.h b/src/functions.h
index f379cac..be1dbed 100644
--- a/src/functions.h
+++ b/src/functions.h
@@ -85,6 +85,7 @@ GtkWidget *orage_image_menu_item_new_from_stock(const gchar *stock_id
 GtkWidget *orage_separator_menu_item_new(GtkWidget *menu);
 GtkWidget *orage_menu_item_new_with_mnemonic(const gchar *label
         , GtkWidget *menu);
+char *orage_replace_text(char *text, char *old, char *new);
 char *orage_process_text_commands(char *text);
 GtkWidget *orage_period_hbox_new(gboolean head_space, gboolean tail_space
         , GtkWidget *spin_dd, GtkWidget *dd_label
diff --git a/src/reminder.c b/src/reminder.c
index c376c90..a5d641c 100644
--- a/src/reminder.c
+++ b/src/reminder.c
@@ -826,7 +826,7 @@ static void create_procedure_reminder(alarm_struct *l_alarm)
     gboolean status, active; / * active not used * /
     GError *error = NULL;
     */
-    gchar *cmd;
+    gchar *cmd, *tmp, *atime, *sep;
     gint status;
 
 #ifdef ORAGE_DEBUG
@@ -836,9 +836,47 @@ static void create_procedure_reminder(alarm_struct *l_alarm)
     status = orage_exec(l_alarm->cmd, &active, &error);
     */
     cmd = g_strconcat(l_alarm->cmd, " &", NULL);
+
+    cmd = orage_replace_text(cmd, "<&T>", l_alarm->title);
+
+    cmd = orage_replace_text(cmd, "<&D>", l_alarm->description);
+
+    if (l_alarm->alarm_time)
+        atime = g_strdup(orage_icaltime_to_i18_time(l_alarm->alarm_time));
+    else
+        atime = g_strdup(orage_tm_time_to_i18_time(orage_localtime()));
+    cmd = orage_replace_text(cmd, "<&AT>", atime);
+    g_free(atime);
+
+    /* l_alarm->action_time format is <start-time - end-time>
+    and times are in user format already. Problem is if that format contain
+    string " - " already, but let's hope not.  */
+    sep = strstr(l_alarm->action_time, " - ");
+    if (sep) { /* we should always have this */
+        if (strstr(sep+1, " - ")) {
+            /* this is problem as now we have more than one separator.
+               We could try to find the middle one using strlen, but as
+               there probably is not this kind of issues, it is not worth
+               the trouble */
+            orage_message(10, P_N "<&ST>/<&ET> string conversion failed (%s)"
+                    , l_alarm->action_time);
+        }
+        else {
+            sep[0] = '\0'; /* temporarily to end start-time string */
+            cmd = orage_replace_text(cmd, "<&ST>", l_alarm->action_time);
+            sep[0] = ' '; /* back to real value */
+
+            sep += strlen(" - "); /* points now to the end-time */
+            cmd = orage_replace_text(cmd, "<&ET>", sep);
+        }
+    }
+    else 
+        orage_message(10, P_N "<&ST>/<&ET> string conversion failed 2 (%s)"
+                , l_alarm->action_time);
+
     status = system(cmd);
     if (status)
-        g_warning(P_N "cmd failed(%s) status:%d", l_alarm->cmd, status);
+        g_warning(P_N "cmd failed(%s)->(%s) status:%d", l_alarm->cmd, cmd, status);
     g_free(cmd);
 }
 


More information about the Xfce4-commits mailing list