diff options
author | Didier Roche <didier.roche@canonical.com> | 2010-02-10 11:40:20 +0100 |
---|---|---|
committer | Didier Roche <didier.roche@canonical.com> | 2010-02-10 11:40:20 +0100 |
commit | 2c193e71a9e62ecf27c2484434c6a05443dc9c61 (patch) | |
tree | e2656fe11fc1531b5c565a302076cdc38e8158a0 /src/indicator-datetime.c | |
parent | 988a0e6cc220f1b78cbabded6fc7e5ab8c40aa61 (diff) | |
parent | d1e6eaaa5de14e0489299191e25351c697914825 (diff) | |
download | ayatana-indicator-datetime-2c193e71a9e62ecf27c2484434c6a05443dc9c61.tar.gz ayatana-indicator-datetime-2c193e71a9e62ecf27c2484434c6a05443dc9c61.tar.bz2 ayatana-indicator-datetime-2c193e71a9e62ecf27c2484434c6a05443dc9c61.zip |
Import upstream version 0.0.1~r2
Diffstat (limited to 'src/indicator-datetime.c')
-rw-r--r-- | src/indicator-datetime.c | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/src/indicator-datetime.c b/src/indicator-datetime.c new file mode 100644 index 0000000..9b4bec4 --- /dev/null +++ b/src/indicator-datetime.c @@ -0,0 +1,196 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +/* GStuff */ +#include <glib.h> +#include <glib-object.h> +#include <glib/gi18n.h> + +/* Indicator Stuff */ +#include <libindicator/indicator.h> +#include <libindicator/indicator-object.h> + + +#define INDICATOR_DATETIME_TYPE (indicator_datetime_get_type ()) +#define INDICATOR_DATETIME(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_DATETIME_TYPE, IndicatorDatetime)) +#define INDICATOR_DATETIME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_DATETIME_TYPE, IndicatorDatetimeClass)) +#define IS_INDICATOR_DATETIME(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_DATETIME_TYPE)) +#define IS_INDICATOR_DATETIME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_DATETIME_TYPE)) +#define INDICATOR_DATETIME_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_DATETIME_TYPE, IndicatorDatetimeClass)) + +typedef struct _IndicatorDatetime IndicatorDatetime; +typedef struct _IndicatorDatetimeClass IndicatorDatetimeClass; +typedef struct _IndicatorDatetimePrivate IndicatorDatetimePrivate; + +struct _IndicatorDatetimeClass { + IndicatorObjectClass parent_class; +}; + +struct _IndicatorDatetime { + IndicatorObject parent; + IndicatorDatetimePrivate * priv; +}; + +struct _IndicatorDatetimePrivate { + GtkLabel * label; + guint timer; +}; + +#define INDICATOR_DATETIME_GET_PRIVATE(o) \ +(G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_DATETIME_TYPE, IndicatorDatetimePrivate)) + +GType indicator_datetime_get_type (void); + +static void indicator_datetime_class_init (IndicatorDatetimeClass *klass); +static void indicator_datetime_init (IndicatorDatetime *self); +static void indicator_datetime_dispose (GObject *object); +static void indicator_datetime_finalize (GObject *object); +static GtkLabel * get_label (IndicatorObject * io); +static GtkMenu * get_menu (IndicatorObject * io); + +/* Indicator Module Config */ +INDICATOR_SET_VERSION +INDICATOR_SET_TYPE(INDICATOR_DATETIME_TYPE) + +G_DEFINE_TYPE (IndicatorDatetime, indicator_datetime, INDICATOR_OBJECT_TYPE); + +static void +indicator_datetime_class_init (IndicatorDatetimeClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (IndicatorDatetimePrivate)); + + object_class->dispose = indicator_datetime_dispose; + object_class->finalize = indicator_datetime_finalize; + + IndicatorObjectClass * io_class = INDICATOR_OBJECT_CLASS(klass); + + io_class->get_label = get_label; + io_class->get_menu = get_menu; + + return; +} + +static void +indicator_datetime_init (IndicatorDatetime *self) +{ + self->priv = INDICATOR_DATETIME_GET_PRIVATE(self); + + self->priv->label = NULL; + self->priv->timer = 0; + + return; +} + +static void +indicator_datetime_dispose (GObject *object) +{ + IndicatorDatetime * self = INDICATOR_DATETIME(object); + + if (self->priv->label != NULL) { + g_object_unref(self->priv->label); + self->priv->label = NULL; + } + + if (self->priv->timer != 0) { + g_source_remove(self->priv->timer); + self->priv->timer = 0; + } + + G_OBJECT_CLASS (indicator_datetime_parent_class)->dispose (object); + return; +} + +static void +indicator_datetime_finalize (GObject *object) +{ + + G_OBJECT_CLASS (indicator_datetime_parent_class)->finalize (object); + return; +} + +/* Updates the label to be the current time. */ +static void +update_label (GtkLabel * label) +{ + if (label == NULL) return; + + gchar longstr[128]; + time_t t; + struct tm *ltime; + + t = time(NULL); + ltime = localtime(&t); + if (ltime == NULL) { + g_debug("Error getting local time"); + gtk_label_set_label(label, _("Error getting time")); + return; + } + + strftime(longstr, 128, "%I:%M %p", ltime); + + gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL); + gtk_label_set_label(label, utf8); + g_free(utf8); + + return; +} + +/* Runs every minute and updates the time */ +gboolean +minute_timer_func (gpointer user_data) +{ + IndicatorDatetime * self = INDICATOR_DATETIME(user_data); + + if (self->priv->label != NULL) { + update_label(self->priv->label); + return TRUE; + } else { + self->priv->timer = 0; + return FALSE; + } + + return FALSE; +} + +/* Grabs the label. Creates it if it doesn't + exist already */ +static GtkLabel * +get_label (IndicatorObject * io) +{ + IndicatorDatetime * self = INDICATOR_DATETIME(io); + + /* If there's not a label, we'll build ourselves one */ + if (self->priv->label == NULL) { + self->priv->label = GTK_LABEL(gtk_label_new("Time")); + g_object_ref(G_OBJECT(self->priv->label)); + update_label(self->priv->label); + gtk_widget_show(GTK_WIDGET(self->priv->label)); + } + + if (self->priv->timer == 0) { + self->priv->timer = g_timeout_add_seconds(60, minute_timer_func, self); + } + + return self->priv->label; +} + +/* Build a dummy menu for now */ +static GtkMenu * +get_menu (IndicatorObject * io) +{ + GtkWidget * menu = NULL; + GtkWidget * item = NULL; + + menu = gtk_menu_new(); + + item = gtk_menu_item_new_with_label("No menu yet."); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); + gtk_widget_show(item); + + gtk_widget_show(menu); + + return GTK_MENU(menu); +} |