Sorry, this time with patch. :-)

2010/11/4 Andre Bogus <bogusan...@googlemail.com>

> Hi folks
>
> I found that my calendar extension would falsely format dates as yyyydm (if
> d and/or m had only one digit). Please forget my earlier patch and
> use/discuss/improve on this one.
>
> Regards,
> Andre
>
diff --git a/src/plugins/dclock.c b/src/plugins/dclock.c
index 438b882..f8a95cf 100644
--- a/src/plugins/dclock.c
+++ b/src/plugins/dclock.c
@@ -17,6 +17,7 @@
  */
 
 #include <time.h>
+#include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <stdio.h>
@@ -39,6 +40,9 @@ typedef struct {
     GtkWidget * clock_label;			/* Label containing clock value */
     GtkWidget * clock_icon;			/* Icon when icon_only */
     GtkWidget * calendar_window;		/* Calendar window, if it is being displayed */
+    GKeyFile * calendar_dates;			/* Keyfile instance for details */
+    gchar * calendar_current_date;		/* A pointer to the current date */
+    time_t calendar_last_loaded;		/* A timestamp of the last update */
     char * clock_format;			/* Format string for clock value */
     char * tooltip_format;			/* Format string for tooltip value */
     char * action;				/* Command to execute on a click */
@@ -74,6 +78,51 @@ static void dclock_popup_map(GtkWidget * widget, DClockPlugin * dc)
     plugin_adjust_popup_position(widget, dc->plugin);
 }
 
+/* Keep the calendar dates up to date */
+static void dclock_load_calendar_dates(DClockPlugin * dc)
+{
+    struct stat statbuf;
+    gchar *dates_ini = g_strdup_printf("%s/lxpanel/calendar", g_get_user_config_dir());
+    
+    if (dc->calendar_dates == NULL)
+        dc->calendar_dates = g_key_file_new();
+    else
+    {
+	if (stat(dates_ini, &statbuf) != 0)
+	    return; /* can not load file */
+	if (statbuf.st_mtime < dc->calendar_last_loaded)
+	    return; /* file has not been changed */
+	dc->calendar_last_loaded = statbuf.st_mtime;
+    }
+    if (!g_key_file_load_from_file(dc->calendar_dates, dates_ini, 0, NULL)) 
+    {
+    	g_warning("Could not load %s.\n", dates_ini);
+    }
+    g_free(dates_ini);
+}
+
+gchar *dclock_get_calendar_date(GtkCalendar *calendar, guint year, guint month, guint day, gpointer dc)
+{
+    gchar selected_date[11];
+    DClockPlugin *plugin = (DClockPlugin *) dc;
+    
+    if (plugin->calendar_dates == NULL)
+        return NULL;
+    g_snprintf(selected_date, 11, "%02i%02i%02i", year, month + 1, day);
+    plugin->calendar_current_date = g_key_file_get_string(plugin->calendar_dates, "cal", selected_date, NULL);
+    return plugin->calendar_current_date;
+}
+
+void dclock_free_calendar_date(gpointer dc)
+{
+    DClockPlugin *plugin = (DClockPlugin *) dc;
+    if (plugin->calendar_current_date != NULL)
+    {
+        g_free(plugin->calendar_current_date);
+        plugin->calendar_current_date = NULL;
+    }
+}
+
 /* Display a window containing the standard calendar widget. */
 static GtkWidget * dclock_create_calendar(DClockPlugin * dc)
 {
@@ -93,6 +142,7 @@ static GtkWidget * dclock_create_calendar(DClockPlugin * dc)
 
     /* Create a standard calendar widget as a child of the vertical box. */
     GtkWidget * calendar = gtk_calendar_new();
+    gtk_calendar_set_detail_func((GtkCalendar *)calendar, dclock_get_calendar_date, dc, dclock_free_calendar_date);
     gtk_calendar_display_options(
         GTK_CALENDAR(calendar),
         GTK_CALENDAR_SHOW_WEEK_NUMBERS | GTK_CALENDAR_SHOW_DAY_NAMES | GTK_CALENDAR_SHOW_HEADING);
@@ -121,6 +171,8 @@ static gboolean dclock_button_press_event(GtkWidget * widget, GdkEventButton * e
     /* If no action is set, toggle the presentation of the calendar. */
     else
     {
+	/* ensure current calendar dates are loaded */
+        dclock_load_calendar_dates(dc);
         if (dc->calendar_window == NULL)
         {
             dc->calendar_window = dclock_create_calendar(dc);
@@ -367,6 +419,10 @@ static void dclock_destructor(Plugin * p)
     if (dc->calendar_window != NULL)
         gtk_widget_destroy(dc->calendar_window);
 
+    /* Ensure that the dates.ini key file is dismissed. */
+    if (dc->calendar_dates != NULL)
+        g_key_file_free(dc->calendar_dates);
+
     /* Deallocate all memory. */
     g_free(dc->clock_format);
     g_free(dc->tooltip_format);
------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a 
Billion" shares his insights and actions to help propel your 
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
_______________________________________________
Lxde-list mailing list
Lxde-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lxde-list

Reply via email to