aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am82
-rw-r--r--src/calendar-menu-item.c100
-rw-r--r--src/calendar-menu-item.h59
-rw-r--r--src/datetime-interface.c207
-rw-r--r--src/datetime-interface.h57
-rw-r--r--src/datetime-prefs-locations.c500
-rw-r--r--src/datetime-prefs-locations.h35
-rw-r--r--src/datetime-prefs.c769
-rw-r--r--src/datetime-service.c1493
-rw-r--r--src/datetime-service.xml11
-rw-r--r--src/dbus-shared.h42
-rw-r--r--src/indicator-datetime.c1535
-rw-r--r--src/settings-shared.h68
-rw-r--r--src/timezone-completion.c688
-rw-r--r--src/timezone-completion.h63
-rw-r--r--src/utils.c276
-rw-r--r--src/utils.h38
17 files changed, 6023 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..6d388c7
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,82 @@
+
+bin_PROGRAMS = indicator-datetime-preferences
+libexec_PROGRAMS = indicator-datetime-service
+
+indicator_datetime_service_SOURCES = \
+ datetime-interface.c \
+ datetime-interface.h \
+ gen-datetime-service.xml.c \
+ calendar-menu-item.c \
+ calendar-menu-item.h \
+ datetime-service.c \
+ utils.c \
+ utils.h \
+ dbus-shared.h \
+ settings-shared.h
+indicator_datetime_service_CFLAGS = \
+ -Wall \
+ -Werror \
+ $(SERVICE_CFLAGS) \
+ -DTIMEZONE_FILE="\"/etc/timezone\"" \
+ -DG_LOG_DOMAIN=\"Indicator-Datetime\"
+indicator_datetime_service_LDADD = \
+ $(SERVICE_LIBS)
+
+datetimelibdir = $(INDICATORDIR)
+datetimelib_LTLIBRARIES = libdatetime.la
+libdatetime_la_SOURCES = \
+ gen-datetime-service.xml.h \
+ dbus-shared.h \
+ settings-shared.h \
+ utils.c \
+ utils.h \
+ indicator-datetime.c
+libdatetime_la_CFLAGS = \
+ $(INDICATOR_CFLAGS) \
+ -Wall -Werror \
+ -DG_LOG_DOMAIN=\"Indicator-Datetime\"
+libdatetime_la_LIBADD = \
+ $(INDICATOR_LIBS)
+libdatetime_la_LDFLAGS = \
+ -module \
+ -avoid-version
+
+indicator_datetime_preferences_SOURCES =\
+ datetime-prefs.c \
+ datetime-prefs-locations.c \
+ datetime-prefs-locations.h \
+ timezone-completion.c \
+ timezone-completion.h \
+ utils.c \
+ utils.h \
+ settings-shared.h
+indicator_datetime_preferences_CFLAGS = \
+ -Wall \
+ -Werror \
+ -I$(top_srcdir)/libmap \
+ $(PREF_CFLAGS) \
+ -DPKGDATADIR="\"$(pkgdatadir)\""
+indicator_datetime_preferences_LDADD = \
+ $(top_builddir)/libmap/libmap.la \
+ $(PREF_LIBS)
+
+gen-%.xml.c: %.xml
+ @echo "Building $@ from $<"
+ @echo "const char * _$(subst -,_,$(subst .,_,$(basename $<))) = " > $@
+ @sed -e "s:\":\\\\\":g" -e s:^:\": -e s:\$$:\\\\n\": $< >> $@
+ @echo ";" >> $@
+
+gen-%.xml.h: %.xml
+ @echo "Building $@ from $<"
+ @echo "extern const char * _$(subst -,_,$(subst .,_,$(basename $<)));" > $@
+
+BUILT_SOURCES = \
+ gen-datetime-service.xml.c \
+ gen-datetime-service.xml.h
+
+
+CLEANFILES = \
+ $(BUILT_SOURCES)
+
+EXTRA_DIST = \
+ datetime-service.xml
diff --git a/src/calendar-menu-item.c b/src/calendar-menu-item.c
new file mode 100644
index 0000000..c2ceec3
--- /dev/null
+++ b/src/calendar-menu-item.c
@@ -0,0 +1,100 @@
+/*
+Calendar menu item dbusmenu "transport" for the corresponding IDO widget.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ David Barth <david.barth@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gdk/gdk.h>
+#include <glib/gi18n.h>
+#include "calendar-menu-item.h"
+
+#include "dbus-shared.h"
+
+#include <libdbusmenu-glib/client.h>
+#include <libdbusmenu-glib/server.h>
+#include <libdbusmenu-glib/menuitem.h>
+
+enum {
+ LAST_SIGNAL
+};
+
+/* static guint signals[LAST_SIGNAL] = { }; */
+
+typedef struct _CalendarMenuItemPrivate CalendarMenuItemPrivate;
+struct _CalendarMenuItemPrivate
+{
+ void * placeholder;
+};
+
+#define CALENDAR_MENU_ITEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CALENDAR_MENU_ITEM_TYPE, CalendarMenuItemPrivate))
+
+/* Prototypes */
+static void calendar_menu_item_class_init (CalendarMenuItemClass *klass);
+static void calendar_menu_item_init (CalendarMenuItem *self);
+static void calendar_menu_item_dispose (GObject *object);
+static void calendar_menu_item_finalize (GObject *object);
+
+G_DEFINE_TYPE (CalendarMenuItem, calendar_menu_item, DBUSMENU_TYPE_MENUITEM);
+
+static void
+calendar_menu_item_class_init (CalendarMenuItemClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (CalendarMenuItemPrivate));
+
+ object_class->dispose = calendar_menu_item_dispose;
+ object_class->finalize = calendar_menu_item_finalize;
+
+ return;
+}
+
+static void
+calendar_menu_item_init (CalendarMenuItem *self)
+{
+ return;
+}
+
+static void
+calendar_menu_item_dispose (GObject *object)
+{
+ G_OBJECT_CLASS (calendar_menu_item_parent_class)->dispose (object);
+}
+
+static void
+calendar_menu_item_finalize (GObject *object)
+{
+ G_OBJECT_CLASS (calendar_menu_item_parent_class)->finalize (object);
+
+ return;
+}
+
+CalendarMenuItem *
+calendar_menu_item_new ()
+{
+ CalendarMenuItem * self = g_object_new(CALENDAR_MENU_ITEM_TYPE, NULL);
+
+ dbusmenu_menuitem_property_set(DBUSMENU_MENUITEM(self), DBUSMENU_MENUITEM_PROP_TYPE, DBUSMENU_CALENDAR_MENUITEM_TYPE);
+
+ return self;
+}
+
diff --git a/src/calendar-menu-item.h b/src/calendar-menu-item.h
new file mode 100644
index 0000000..7a56f96
--- /dev/null
+++ b/src/calendar-menu-item.h
@@ -0,0 +1,59 @@
+/*
+Calendar menu item dbusmenu "transport" for the corresponding IDO widget.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ David Barth <david.barth@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __CALENDAR_MENU_ITEM_H__
+#define __CALENDAR_MENU_ITEM_H__
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include <libdbusmenu-glib/menuitem.h>
+
+G_BEGIN_DECLS
+
+#define CALENDAR_MENU_ITEM_TYPE (calendar_menu_item_get_type ())
+#define CALENDAR_MENU_ITEM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CALENDAR_MENU_ITEM_TYPE, CalendarMenuItem))
+#define CALENDAR_MENU_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CALENDAR_MENU_ITEM_TYPE, CalendarMenuItemClass))
+#define IS_CALENDAR_MENU_ITEM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CALENDAR_MENU_ITEM_TYPE))
+#define IS_CALENDAR_MENU_ITEM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CALENDAR_MENU_ITEM_TYPE))
+#define CALENDAR_MENU_ITEM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CALENDAR_MENU_ITEM_TYPE, CalendarMenuItemClass))
+
+#define CALENDAR_MENU_ITEM_SIGNAL_ACTIVATE "activate"
+#define CALENDAR_MENUITEM_PROP_TEXT "text"
+
+typedef struct _CalendarMenuItem CalendarMenuItem;
+typedef struct _CalendarMenuItemClass CalendarMenuItemClass;
+
+struct _CalendarMenuItemClass {
+ DbusmenuMenuitemClass parent_class;
+};
+
+struct _CalendarMenuItem {
+ DbusmenuMenuitem parent;
+};
+
+GType calendar_menu_item_get_type (void);
+CalendarMenuItem * calendar_menu_item_new ();
+
+G_END_DECLS
+
+#endif /* __CALENDAR_MENU_ITEM_H__ */
+
diff --git a/src/datetime-interface.c b/src/datetime-interface.c
new file mode 100644
index 0000000..5939061
--- /dev/null
+++ b/src/datetime-interface.c
@@ -0,0 +1,207 @@
+/*
+An indicator to time and date related information in the menubar.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ Ted Gould <ted@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <gio/gio.h>
+
+#include "datetime-interface.h"
+#include "gen-datetime-service.xml.h"
+#include "dbus-shared.h"
+
+/**
+ DatetimeInterfacePrivate:
+ @dbus_registration: The handle for this object being registered
+ on dbus.
+
+ Structure to define the memory for the private area
+ of the datetime interface instance.
+*/
+struct _DatetimeInterfacePrivate {
+ GDBusConnection * bus;
+ GCancellable * bus_cancel;
+ guint dbus_registration;
+};
+
+#define DATETIME_INTERFACE_GET_PRIVATE(o) (DATETIME_INTERFACE(o)->priv)
+
+/* GDBus Stuff */
+static GDBusNodeInfo * node_info = NULL;
+static GDBusInterfaceInfo * interface_info = NULL;
+
+static void datetime_interface_class_init (DatetimeInterfaceClass *klass);
+static void datetime_interface_init (DatetimeInterface *self);
+static void datetime_interface_dispose (GObject *object);
+static void datetime_interface_finalize (GObject *object);
+static void bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data);
+
+G_DEFINE_TYPE (DatetimeInterface, datetime_interface, G_TYPE_OBJECT);
+
+static void
+datetime_interface_class_init (DatetimeInterfaceClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (DatetimeInterfacePrivate));
+
+ object_class->dispose = datetime_interface_dispose;
+ object_class->finalize = datetime_interface_finalize;
+
+ /* Setting up the DBus interfaces */
+ if (node_info == NULL) {
+ GError * error = NULL;
+
+ node_info = g_dbus_node_info_new_for_xml(_datetime_service, &error);
+ if (error != NULL) {
+ g_error("Unable to parse Datetime Service Interface description: %s", error->message);
+ g_error_free(error);
+ }
+ }
+
+ if (interface_info == NULL) {
+ interface_info = g_dbus_node_info_lookup_interface(node_info, SERVICE_IFACE);
+
+ if (interface_info == NULL) {
+ g_error("Unable to find interface '" SERVICE_IFACE "'");
+ }
+ }
+
+ return;
+}
+
+static void
+datetime_interface_init (DatetimeInterface *self)
+{
+ self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, DATETIME_INTERFACE_TYPE, DatetimeInterfacePrivate);
+
+ self->priv->bus = NULL;
+ self->priv->bus_cancel = NULL;
+ self->priv->dbus_registration = 0;
+
+ self->priv->bus_cancel = g_cancellable_new();
+ g_bus_get(G_BUS_TYPE_SESSION,
+ self->priv->bus_cancel,
+ bus_get_cb,
+ self);
+
+ return;
+}
+
+static void
+bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data)
+{
+ GError * error = NULL;
+ GDBusConnection * connection = g_bus_get_finish(res, &error);
+
+ if (error != NULL) {
+ g_error("OMG! Unable to get a connection to DBus: %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ DatetimeInterfacePrivate * priv = DATETIME_INTERFACE_GET_PRIVATE(user_data);
+
+ g_warn_if_fail(priv->bus == NULL);
+ priv->bus = connection;
+
+ if (priv->bus_cancel != NULL) {
+ g_object_unref(priv->bus_cancel);
+ priv->bus_cancel = NULL;
+ }
+
+ /* Now register our object on our new connection */
+ priv->dbus_registration = g_dbus_connection_register_object(priv->bus,
+ SERVICE_OBJ,
+ interface_info,
+ NULL,
+ user_data,
+ NULL,
+ &error);
+
+ if (error != NULL) {
+ g_error("Unable to register the object to DBus: %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ return;
+}
+
+static void
+datetime_interface_dispose (GObject *object)
+{
+ DatetimeInterfacePrivate * priv = DATETIME_INTERFACE_GET_PRIVATE(object);
+
+ if (priv->dbus_registration != 0) {
+ g_dbus_connection_unregister_object(priv->bus, priv->dbus_registration);
+ /* Don't care if it fails, there's nothing we can do */
+ priv->dbus_registration = 0;
+ }
+
+ if (priv->bus != NULL) {
+ g_object_unref(priv->bus);
+ priv->bus = NULL;
+ }
+
+ if (priv->bus_cancel != NULL) {
+ g_cancellable_cancel(priv->bus_cancel);
+ g_object_unref(priv->bus_cancel);
+ priv->bus_cancel = NULL;
+ }
+
+ G_OBJECT_CLASS (datetime_interface_parent_class)->dispose (object);
+ return;
+}
+
+static void
+datetime_interface_finalize (GObject *object)
+{
+
+ G_OBJECT_CLASS (datetime_interface_parent_class)->finalize (object);
+ return;
+}
+
+void
+datetime_interface_update (DatetimeInterface *self)
+{
+ g_return_if_fail(IS_DATETIME_INTERFACE(self));
+
+ DatetimeInterfacePrivate * priv = DATETIME_INTERFACE_GET_PRIVATE(self);
+ GError * error = NULL;
+
+ g_dbus_connection_emit_signal (priv->bus,
+ NULL,
+ SERVICE_OBJ,
+ SERVICE_IFACE,
+ "UpdateTime",
+ NULL,
+ &error);
+
+ if (error != NULL) {
+ g_error("Unable to send UpdateTime signal: %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ return;
+}
diff --git a/src/datetime-interface.h b/src/datetime-interface.h
new file mode 100644
index 0000000..ae85605
--- /dev/null
+++ b/src/datetime-interface.h
@@ -0,0 +1,57 @@
+/*
+An indicator to time and date related information in the menubar.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ Ted Gould <ted@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __DATETIME_INTERFACE_H__
+#define __DATETIME_INTERFACE_H__
+
+#include <glib.h>
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define DATETIME_INTERFACE_TYPE (datetime_interface_get_type ())
+#define DATETIME_INTERFACE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DATETIME_INTERFACE_TYPE, DatetimeInterface))
+#define DATETIME_INTERFACE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DATETIME_INTERFACE_TYPE, DatetimeInterfaceClass))
+#define IS_DATETIME_INTERFACE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DATETIME_INTERFACE_TYPE))
+#define IS_DATETIME_INTERFACE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DATETIME_INTERFACE_TYPE))
+#define DATETIME_INTERFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DATETIME_INTERFACE_TYPE, DatetimeInterfaceClass))
+
+typedef struct _DatetimeInterface DatetimeInterface;
+typedef struct _DatetimeInterfacePrivate DatetimeInterfacePrivate;
+typedef struct _DatetimeInterfaceClass DatetimeInterfaceClass;
+
+struct _DatetimeInterfaceClass {
+ GObjectClass parent_class;
+
+ void (*update_time) (void);
+};
+
+struct _DatetimeInterface {
+ GObject parent;
+ DatetimeInterfacePrivate * priv;
+};
+
+GType datetime_interface_get_type (void);
+void datetime_interface_update (DatetimeInterface *self);
+
+G_END_DECLS
+
+#endif
diff --git a/src/datetime-prefs-locations.c b/src/datetime-prefs-locations.c
new file mode 100644
index 0000000..af0e10d
--- /dev/null
+++ b/src/datetime-prefs-locations.c
@@ -0,0 +1,500 @@
+/* -*- Mode: C; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*-
+
+A dialog for setting time and date preferences.
+
+Copyright 2011 Canonical Ltd.
+
+Authors:
+ Michael Terry <michael.terry@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <glib/gi18n-lib.h>
+#include <gtk/gtk.h>
+
+#include "datetime-prefs-locations.h"
+#include "settings-shared.h"
+#include "utils.h"
+#include "timezone-completion.h"
+
+#define DATETIME_DIALOG_UI_FILE PKGDATADIR "/datetime-dialog.ui"
+
+#define COL_NAME 0
+#define COL_TIME 1
+#define COL_ZONE 2
+#define COL_VISIBLE_NAME 3
+#define COL_ICON 4
+
+static gboolean update_times (GtkWidget * dlg);
+static void save_when_idle (GtkWidget * dlg);
+
+static void
+handle_add (GtkWidget * button, GtkTreeView * tree)
+{
+ GtkListStore * store = GTK_LIST_STORE (gtk_tree_view_get_model (tree));
+
+ GtkTreeIter iter;
+ gtk_list_store_append (store, &iter);
+
+ GtkTreePath * path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
+ gtk_tree_view_set_cursor (tree, path, gtk_tree_view_get_column (tree, 0), TRUE);
+ gtk_tree_path_free (path);
+}
+
+static void
+handle_remove (GtkWidget * button, GtkTreeView * tree)
+{
+ GtkListStore * store = GTK_LIST_STORE (gtk_tree_view_get_model (tree));
+ GtkTreeSelection * selection = gtk_tree_view_get_selection (tree);
+
+ GList * paths = gtk_tree_selection_get_selected_rows (selection, NULL);
+
+ /* Convert all paths to iters so we can safely delete multiple paths. For a
+ GtkListStore, iters persist past model changes. */
+ GList * tree_iters = NULL;
+ GList * iter;
+ for (iter = paths; iter; iter = iter->next) {
+ GtkTreeIter * tree_iter = g_new(GtkTreeIter, 1);
+ if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), tree_iter, (GtkTreePath *)iter->data)) {
+ tree_iters = g_list_prepend (tree_iters, tree_iter);
+ }
+ gtk_tree_path_free (iter->data);
+ }
+ g_list_free (paths);
+
+ // Find the next item to select
+ GtkTreeIter *last_selected = g_list_nth_data(tree_iters, 0);
+ GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL (store), last_selected);
+ GtkTreeIter titer;
+ if (!gtk_tree_model_get_iter(GTK_TREE_MODEL (store), &titer, path)) {
+ g_debug("Failed to get last selected iter from path");
+ last_selected = NULL;
+ } else {
+ if (!gtk_tree_model_iter_next(GTK_TREE_MODEL (store), &titer)) {
+ if (gtk_tree_path_prev(path)) {
+ if (!gtk_tree_model_get_iter(GTK_TREE_MODEL (store), &titer, path)) {
+ g_debug("Failed to get iter from path");
+ last_selected = NULL;
+ } else {
+ last_selected = &titer;
+ }
+ } else {
+ g_debug("handle_remove: Failed to find another location to select (assume single selected)");
+ last_selected = NULL;
+ }
+ } else {
+ g_debug("Got next item in model");
+ last_selected = &titer;
+ }
+ }
+
+ if (last_selected) {
+ gboolean clear = TRUE;
+ path = gtk_tree_model_get_path(GTK_TREE_MODEL (store), last_selected);
+
+ // Step over the path to find an item which isn't in the delete list
+ if (g_list_length(tree_iters) > 1) {
+ for (iter = tree_iters; iter; iter = iter->next) {
+ GtkTreePath *ipath = gtk_tree_model_get_path(GTK_TREE_MODEL (store), (GtkTreeIter *)iter->data);
+ if (gtk_tree_path_compare(path, ipath) == 0) {
+ clear = FALSE;
+ break;
+ }
+ }
+ while (clear == FALSE) {
+ if (gtk_tree_path_prev(path)) {
+ clear = TRUE;
+ for (iter = tree_iters; iter; iter = iter->next) {
+ GtkTreePath *ipath = gtk_tree_model_get_path(GTK_TREE_MODEL (store), (GtkTreeIter *)iter->data);
+ if (gtk_tree_path_compare(path, ipath) == 0) {
+ clear = FALSE;
+ break;
+ }
+ }
+ if (clear) {
+ if (!gtk_tree_model_get_iter(GTK_TREE_MODEL (store), &titer, path)) {
+ g_debug("Failed to get iter from path");
+ last_selected = NULL;
+ } else {
+ last_selected = &titer;
+ }
+ }
+ } else {
+ last_selected = NULL;
+ break;
+ }
+ }
+ }
+ }
+
+ /* Now delete each iterator */
+ for (iter = tree_iters; iter; iter = iter->next) {
+ gtk_list_store_remove (store, (GtkTreeIter *)iter->data);
+ g_free (iter->data);
+ }
+ g_list_free (tree_iters);
+
+ if (last_selected)
+ gtk_tree_selection_select_iter(selection, last_selected);
+}
+
+static void
+handle_edit (GtkCellRendererText * renderer, gchar * path, gchar * new_text,
+ GtkListStore * store)
+{
+ GtkTreeIter iter;
+
+ // Manual user edits are always wrong (unless they are undoing a previous
+ // edit), so we set the error icon here if needed. Common way to get to
+ // this code path is to lose entry focus.
+ if (gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (store), &iter, path)) {
+ const gchar * name;
+ gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, COL_NAME, &name, -1);
+ gboolean correct = g_strcmp0 (name, new_text) == 0;
+
+ gtk_list_store_set (store, &iter,
+ COL_VISIBLE_NAME, new_text,
+ COL_ICON, correct ? NULL : GTK_STOCK_DIALOG_ERROR,
+ -1);
+ }
+}
+
+static gboolean
+timezone_selected (GtkEntryCompletion * widget, GtkTreeModel * model,
+ GtkTreeIter * iter, GtkWidget * dlg)
+{
+ const gchar * zone, * name;
+
+ gtk_tree_model_get (model, iter,
+ TIMEZONE_COMPLETION_ZONE, &zone,
+ TIMEZONE_COMPLETION_NAME, &name,
+ -1);
+
+ if (zone == NULL || zone[0] == 0) {
+ const gchar * strlon, * strlat;
+ gdouble lon = 0.0, lat = 0.0;
+
+ gtk_tree_model_get (model, iter,
+ TIMEZONE_COMPLETION_LONGITUDE, &strlon,
+ TIMEZONE_COMPLETION_LATITUDE, &strlat,
+ -1);
+
+ if (strlon != NULL && strlon[0] != 0) {
+ lon = strtod(strlon, NULL);
+ }
+
+ if (strlat != NULL && strlat[0] != 0) {
+ lat = strtod(strlat, NULL);
+ }
+
+ CcTimezoneMap * tzmap = CC_TIMEZONE_MAP (g_object_get_data (G_OBJECT (widget), "tzmap"));
+ zone = cc_timezone_map_get_timezone_at_coords (tzmap, lon, lat);
+ }
+
+ GtkListStore * store = GTK_LIST_STORE (g_object_get_data (G_OBJECT (widget), "store"));
+ GtkTreeIter * store_iter = (GtkTreeIter *)g_object_get_data (G_OBJECT (widget), "store_iter");
+ if (store != NULL && store_iter != NULL) {
+ gtk_list_store_set (store, store_iter,
+ COL_VISIBLE_NAME, name,
+ COL_ICON, NULL,
+ COL_NAME, name,
+ COL_ZONE, zone, -1);
+ }
+
+ update_times (dlg);
+
+ return FALSE; // Do normal action too
+}
+
+static gboolean
+query_tooltip (GtkTreeView * tree, gint x, gint y, gboolean keyboard_mode,
+ GtkTooltip * tooltip, GtkCellRenderer * cell)
+{
+ GtkTreeModel * model;
+ GtkTreeIter iter;
+ if (!gtk_tree_view_get_tooltip_context (tree, &x, &y, keyboard_mode,
+ &model, NULL, &iter))
+ return FALSE;
+
+ const gchar * icon;
+ gtk_tree_model_get (model, &iter, COL_ICON, &icon, -1);
+ if (icon == NULL)
+ return FALSE;
+
+ GtkTreeViewColumn * col = gtk_tree_view_get_column (tree, 0);
+ gtk_tree_view_set_tooltip_cell (tree, tooltip, NULL, col, cell);
+ gtk_tooltip_set_text (tooltip, _("You need to complete this location for it to appear in the menu."));
+ return TRUE;
+}
+
+static void
+handle_edit_started (GtkCellRendererText * renderer, GtkCellEditable * editable,
+ gchar * path, TimezoneCompletion * completion)
+{
+ if (GTK_IS_ENTRY (editable)) {
+ GtkEntry *entry = GTK_ENTRY (editable);
+ timezone_completion_watch_entry (completion, entry);
+
+ GtkListStore * store = GTK_LIST_STORE (g_object_get_data (G_OBJECT (completion), "store"));
+ GtkTreeIter * store_iter = g_new(GtkTreeIter, 1);
+ if (gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (store), store_iter, path)) {
+ g_object_set_data_full (G_OBJECT (completion), "store_iter", store_iter, g_free);
+ }
+ }
+}
+
+static gboolean
+update_times (GtkWidget * dlg)
+{
+ /* For each entry, check zone in column 2 and set column 1 to it's time */
+ TimezoneCompletion * completion = TIMEZONE_COMPLETION (g_object_get_data (G_OBJECT (dlg), "completion"));
+ GtkListStore * store = GTK_LIST_STORE (g_object_get_data (G_OBJECT (completion), "store"));
+ GObject * cell = G_OBJECT (g_object_get_data (G_OBJECT (completion), "name-cell"));
+
+ gboolean editing;
+ g_object_get (cell, "editing", &editing, NULL);
+ if (editing) { /* No updates while editing, it cancels the edit */
+ return TRUE;
+ }
+
+ g_signal_handlers_block_by_func (store, save_when_idle, dlg);
+
+ GDateTime * now = g_date_time_new_now_local ();
+
+ GtkTreeIter iter;
+ if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) {
+ do {
+ const gchar * strzone;
+
+ gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, COL_ZONE, &strzone, -1);
+
+ if (strzone != NULL && strzone[0] != 0) {
+ GTimeZone * tz = g_time_zone_new (strzone);
+ GDateTime * now_tz = g_date_time_to_timezone (now, tz);
+ gchar * format = generate_format_string_at_time (now_tz);
+ gchar * time_str = g_date_time_format (now_tz, format);
+
+ gtk_list_store_set (store, &iter, COL_TIME, time_str, -1);
+
+ g_free (time_str);
+ g_free (format);
+ g_date_time_unref (now_tz);
+ g_time_zone_unref (tz);
+ }
+ } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter));
+ }
+
+ g_date_time_unref (now);
+
+ g_signal_handlers_unblock_by_func (store, save_when_idle, dlg);
+
+ return TRUE;
+}
+
+static void
+fill_from_settings (GObject * store, GSettings * conf)
+{
+ gchar ** locations = g_settings_get_strv (conf, SETTINGS_LOCATIONS_S);
+
+ gtk_list_store_clear (GTK_LIST_STORE (store));
+
+ gchar ** striter;
+ GtkTreeIter iter;
+ for (striter = locations; *striter; ++striter) {
+ gchar * zone, * name;
+ split_settings_location (*striter, &zone, &name);
+
+ gtk_list_store_append (GTK_LIST_STORE (store), &iter);
+ gtk_list_store_set (GTK_LIST_STORE (store), &iter,
+ COL_VISIBLE_NAME, name,
+ COL_ICON, NULL,
+ COL_NAME, name,
+ COL_ZONE, zone, -1);
+
+ g_free (zone);
+ g_free (name);
+ }
+
+ g_strfreev (locations);
+}
+
+static void
+save_to_settings (GObject * store, GSettings * conf)
+{
+ gboolean empty = TRUE;
+ GVariantBuilder builder;
+ g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
+
+ GtkTreeIter iter;
+ if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) {
+ do {
+ const gchar * strzone, * strname;
+
+ gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
+ COL_NAME, &strname,
+ COL_ZONE, &strzone,
+ -1);
+
+ if (strzone != NULL && strzone[0] != 0 && strname != NULL && strname[0] != 0) {
+ gchar * settings_string = g_strdup_printf("%s %s", strzone, strname);
+ g_variant_builder_add (&builder, "s", settings_string);
+ g_free (settings_string);
+ empty = FALSE;
+ }
+ } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter));
+ }
+
+ if (empty) {
+ /* Empty list */
+ g_variant_builder_clear (&builder);
+ g_settings_set_strv (conf, SETTINGS_LOCATIONS_S, NULL);
+ }
+ else {
+ GVariant * locations = g_variant_builder_end (&builder);
+ g_settings_set_strv (conf, SETTINGS_LOCATIONS_S, g_variant_get_strv (locations, NULL));
+ g_variant_unref (locations);
+ }
+}
+
+static gboolean
+save_now (GtkWidget *dlg)
+{
+ GSettings * conf = G_SETTINGS (g_object_get_data (G_OBJECT (dlg), "conf"));
+ GObject * completion = G_OBJECT (g_object_get_data (G_OBJECT (dlg), "completion"));
+ GObject * store = G_OBJECT (g_object_get_data (completion, "store"));
+
+ save_to_settings (store, conf);
+
+ g_object_set_data (G_OBJECT (dlg), "save-id", GINT_TO_POINTER(0));
+
+ return FALSE;
+}
+
+static void
+save_when_idle (GtkWidget *dlg)
+{
+ guint save_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dlg), "save-id"));
+
+ if (save_id == 0) {
+ save_id = g_idle_add ((GSourceFunc)save_now, dlg);
+ g_object_set_data (G_OBJECT (dlg), "save-id", GINT_TO_POINTER(save_id));
+ }
+}
+
+static void
+dialog_closed (GtkWidget * dlg, GObject * store)
+{
+ /* Cleanup a tad */
+ guint time_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dlg), "time-id"));
+ g_source_remove (time_id);
+
+ guint save_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dlg), "save-id"));
+ if (save_id > 0)
+ g_source_remove (save_id);
+}
+
+static void
+selection_changed (GtkTreeSelection * selection, GtkWidget * remove_button)
+{
+ gint count = gtk_tree_selection_count_selected_rows (selection);
+ gtk_widget_set_sensitive (remove_button, count > 0);
+}
+
+GtkWidget *
+datetime_setup_locations_dialog (CcTimezoneMap * map)
+{
+ GError * error = NULL;
+ GtkBuilder * builder = gtk_builder_new ();
+ gtk_builder_add_from_file (builder, DATETIME_DIALOG_UI_FILE, &error);
+ if (error != NULL) {
+ /* We have to abort, we can't continue without the ui file */
+ g_error ("Could not load ui file %s: %s", DATETIME_DIALOG_UI_FILE, error->message);
+ g_error_free (error);
+ return NULL;
+ }
+
+ gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
+
+ GSettings * conf = g_settings_new (SETTINGS_INTERFACE);
+
+#define WIG(name) GTK_WIDGET (gtk_builder_get_object (builder, name))
+
+ GtkWidget * dlg = WIG ("locationsDialog");
+ GtkWidget * tree = WIG ("locationsView");
+ GObject * store = gtk_builder_get_object (builder, "locationsStore");
+
+ /* Configure tree */
+ TimezoneCompletion * completion = timezone_completion_new ();
+ g_object_set_data (G_OBJECT (completion), "tzmap", map);
+ g_object_set_data (G_OBJECT (completion), "store", store);
+ g_signal_connect (completion, "match-selected", G_CALLBACK (timezone_selected), dlg);
+
+ GtkCellRenderer * cell = gtk_cell_renderer_text_new ();
+ g_object_set (cell, "editable", TRUE, NULL);
+ g_signal_connect (cell, "editing-started", G_CALLBACK (handle_edit_started), completion);
+ g_signal_connect (cell, "edited", G_CALLBACK (handle_edit), store);
+ gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
+ _("Location"), cell,
+ "text", COL_VISIBLE_NAME, NULL);
+ GtkTreeViewColumn * loc_col = gtk_tree_view_get_column (GTK_TREE_VIEW (tree), 0);
+ gtk_tree_view_column_set_expand (loc_col, TRUE);
+ g_object_set_data (G_OBJECT (completion), "name-cell", cell);
+
+ cell = gtk_cell_renderer_pixbuf_new ();
+ gtk_tree_view_column_pack_start (loc_col, cell, FALSE);
+ gtk_tree_view_column_add_attribute (loc_col, cell, "icon-name", COL_ICON);
+
+ gtk_widget_set_has_tooltip (tree, TRUE);
+ g_signal_connect (tree, "query-tooltip", G_CALLBACK (query_tooltip), cell);
+
+ cell = gtk_cell_renderer_text_new ();
+ gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1,
+ _("Time"), cell,
+ "text", COL_TIME, NULL);
+
+ GtkTreeSelection * selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
+ gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
+ g_signal_connect (selection, "changed", G_CALLBACK (selection_changed), WIG ("removeButton"));
+ selection_changed (selection, WIG ("removeButton"));
+
+ g_signal_connect (WIG ("addButton"), "clicked", G_CALLBACK (handle_add), tree);
+ g_signal_connect (WIG ("removeButton"), "clicked", G_CALLBACK (handle_remove), tree);
+
+ fill_from_settings (store, conf);
+ g_signal_connect_swapped (store, "row-deleted", G_CALLBACK (save_when_idle), dlg);
+ g_signal_connect_swapped (store, "row-inserted", G_CALLBACK (save_when_idle), dlg);
+ g_signal_connect_swapped (store, "row-changed", G_CALLBACK (save_when_idle), dlg);
+ g_signal_connect_swapped (store, "rows-reordered", G_CALLBACK (save_when_idle), dlg);
+
+ g_object_set_data_full (G_OBJECT (dlg), "conf", g_object_ref (conf), g_object_unref);
+ g_object_set_data_full (G_OBJECT (dlg), "completion", completion, g_object_unref);
+ g_signal_connect (dlg, "destroy", G_CALLBACK (dialog_closed), store);
+
+ guint time_id = g_timeout_add_seconds (2, (GSourceFunc)update_times, dlg);
+ g_object_set_data (G_OBJECT (dlg), "time-id", GINT_TO_POINTER(time_id));
+ update_times (dlg);
+
+#undef WIG
+
+ g_object_unref (conf);
+ g_object_unref (builder);
+
+ return dlg;
+}
+
diff --git a/src/datetime-prefs-locations.h b/src/datetime-prefs-locations.h
new file mode 100644
index 0000000..e312894
--- /dev/null
+++ b/src/datetime-prefs-locations.h
@@ -0,0 +1,35 @@
+/* -*- Mode: C; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*-
+
+A dialog for setting time and date preferences.
+
+Copyright 2011 Canonical Ltd.
+
+Authors:
+ Michael Terry <michael.terry@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __DATETIME_PREFS_LOCATIONS_H__
+#define __DATETIME_PREFS_LOCATIONS_H__
+
+#include <gtk/gtk.h>
+#include "cc-timezone-map.h"
+
+G_BEGIN_DECLS
+
+GtkWidget * datetime_setup_locations_dialog (CcTimezoneMap * map);
+
+G_END_DECLS
+
+#endif
diff --git a/src/datetime-prefs.c b/src/datetime-prefs.c
new file mode 100644
index 0000000..9b00e60
--- /dev/null
+++ b/src/datetime-prefs.c
@@ -0,0 +1,769 @@
+/* -*- Mode: C; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*-
+
+A dialog for setting time and date preferences.
+
+Copyright 2011 Canonical Ltd.
+
+Authors:
+ Ted Gould <ted@canonical.com>
+ Michael Terry <michael.terry@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <libintl.h>
+#include <locale.h>
+#include <langinfo.h>
+#include <glib/gi18n-lib.h>
+#include <gdk/gdkkeysyms.h>
+#include <gtk/gtk.h>
+#include <unique/unique.h>
+#include <polkitgtk/polkitgtk.h>
+
+#include "dbus-shared.h"
+#include "settings-shared.h"
+#include "utils.h"
+#include "datetime-prefs-locations.h"
+#include "timezone-completion.h"
+#include "cc-timezone-map.h"
+
+#define DATETIME_DIALOG_UI_FILE PKGDATADIR "/datetime-dialog.ui"
+
+GDBusProxy * proxy = NULL;
+GtkWidget * auto_radio = NULL;
+GtkWidget * tz_entry = NULL;
+CcTimezoneMap * tzmap = NULL;
+GtkWidget * time_spin = NULL;
+GtkWidget * date_spin = NULL;
+guint save_time_id = 0;
+gboolean user_edited_time = FALSE;
+gboolean changing_time = FALSE;
+GtkWidget * loc_dlg = NULL;
+
+/* Turns the boolean property into a string gsettings */
+static GVariant *
+bind_hours_set (const GValue * value, const GVariantType * type, gpointer user_data)
+{
+ const gchar * output = NULL;
+ gboolean is_12hour_button = (gboolean)GPOINTER_TO_INT(user_data);
+
+ if (g_value_get_boolean(value)) {
+ /* Only do anything if we're setting active = true */
+ output = is_12hour_button ? "12-hour" : "24-hour";
+ } else {
+ return NULL;
+ }
+
+ return g_variant_new_string (output);
+}
+
+/* Turns a string gsettings into a boolean property */
+static gboolean
+bind_hours_get (GValue * value, GVariant * variant, gpointer user_data)
+{
+ const gchar * str = g_variant_get_string(variant, NULL);
+ gboolean output = FALSE;
+ gboolean is_12hour_button = (gboolean)GPOINTER_TO_INT(user_data);
+
+ if (g_strcmp0(str, "locale-default") == 0) {
+ output = (is_12hour_button == is_locale_12h ());
+ } else if (g_strcmp0(str, "12-hour") == 0) {
+ output = is_12hour_button;
+ } else if (g_strcmp0(str, "24-hour") == 0) {
+ output = !is_12hour_button;
+ } else {
+ return FALSE;
+ }
+
+ g_value_set_boolean (value, output);
+ return TRUE;
+}
+
+static void
+widget_dependency_cb (GtkWidget * parent, GParamSpec *pspec, GtkWidget * dependent)
+{
+ gboolean active, sensitive;
+ g_object_get (G_OBJECT (parent),
+ "active", &active,
+ "sensitive", &sensitive, NULL);
+ gtk_widget_set_sensitive (dependent, active && sensitive);
+}
+
+static void
+add_widget_dependency (GtkWidget * parent, GtkWidget * dependent)
+{
+ g_signal_connect (parent, "notify::active", G_CALLBACK(widget_dependency_cb),
+ dependent);
+ g_signal_connect (parent, "notify::sensitive", G_CALLBACK(widget_dependency_cb),
+ dependent);
+ widget_dependency_cb (parent, NULL, dependent);
+}
+
+static void
+polkit_dependency_cb (GtkWidget * parent, GParamSpec *pspec, GtkWidget * dependent)
+{
+ gboolean authorized, sensitive;
+ g_object_get (G_OBJECT (parent),
+ "is-authorized", &authorized,
+ "sensitive", &sensitive, NULL);
+ gtk_widget_set_sensitive (dependent, authorized && sensitive);
+}
+
+static void
+add_polkit_dependency (GtkWidget * parent, GtkWidget * dependent)
+{
+ g_signal_connect (parent, "notify::is-authorized", G_CALLBACK(polkit_dependency_cb),
+ dependent);
+ g_signal_connect (parent, "notify::sensitive", G_CALLBACK(polkit_dependency_cb),
+ dependent);
+ polkit_dependency_cb (parent, NULL, dependent);
+}
+
+static void
+dbus_set_answered (GObject *object, GAsyncResult *res, gpointer command)
+{
+ GError * error = NULL;
+ GVariant * answers = g_dbus_proxy_call_finish (proxy, res, &error);
+
+ if (error != NULL) {
+ g_warning("Could not set '%s' for SettingsDaemon: %s", (gchar *)command, error->message);
+ g_error_free(error);
+ return;
+ }
+
+ g_variant_unref (answers);
+}
+
+static void
+toggle_ntp (GtkWidget * radio, GParamSpec * pspec, gpointer user_data)
+{
+ gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radio));
+
+ g_dbus_proxy_call (proxy, "SetUsingNtp", g_variant_new ("(b)", active),
+ G_DBUS_CALL_FLAGS_NONE, -1, NULL, dbus_set_answered, "using_ntp");
+}
+
+static void
+ntp_query_answered (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ GError * error = NULL;
+ GVariant * answers = g_dbus_proxy_call_finish (proxy, res, &error);
+
+ if (error != NULL) {
+ g_warning("Could not query DBus proxy for SettingsDaemon: %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ gboolean can_use_ntp, is_using_ntp;
+ g_variant_get (answers, "(bb)", &can_use_ntp, &is_using_ntp);
+
+ gtk_widget_set_sensitive (GTK_WIDGET (auto_radio), can_use_ntp);
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (auto_radio), is_using_ntp);
+
+ g_signal_connect (auto_radio, "notify::active", G_CALLBACK (toggle_ntp), NULL);
+
+ g_variant_unref (answers);
+}
+
+static void
+sync_entry (const gchar * location)
+{
+ gchar * name = get_current_zone_name (location);
+ gtk_entry_set_text (GTK_ENTRY (tz_entry), name);
+ g_free (name);
+
+ gtk_entry_set_icon_from_stock (GTK_ENTRY (tz_entry), GTK_ENTRY_ICON_SECONDARY, NULL);
+}
+
+static void
+tz_changed (CcTimezoneMap * map, TzLocation * location)
+{
+ if (location == NULL)
+ return;
+
+ gchar * file = g_build_filename ("/usr/share/zoneinfo", location->zone, NULL);
+ g_dbus_proxy_call (proxy, "SetTimezone", g_variant_new ("(s)", file),
+ G_DBUS_CALL_FLAGS_NONE, -1, NULL, dbus_set_answered, "timezone");
+ g_free (file);
+
+ sync_entry (location->zone);
+}
+
+static void
+tz_query_answered (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ GError * error = NULL;
+ GVariant * answers = g_dbus_proxy_call_finish (proxy, res, &error);
+
+ if (error != NULL) {
+ g_warning("Could not query DBus proxy for SettingsDaemon: %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ const gchar * timezone;
+ g_variant_get (answers, "(&s)", &timezone);
+
+ cc_timezone_map_set_timezone (tzmap, timezone);
+
+ sync_entry (timezone);
+ g_signal_connect (tzmap, "location-changed", G_CALLBACK (tz_changed), NULL);
+
+ g_variant_unref (answers);
+}
+
+static void
+proxy_ready (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ GError * error = NULL;
+
+ proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
+
+ if (error != NULL) {
+ g_critical("Could not grab DBus proxy for SettingsDaemon: %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ /* And now, do initial proxy configuration */
+ g_dbus_proxy_call (proxy, "GetUsingNtp", NULL, G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, ntp_query_answered, auto_radio);
+ g_dbus_proxy_call (proxy, "GetTimezone", NULL, G_DBUS_CALL_FLAGS_NONE, -1,
+ NULL, tz_query_answered, NULL);
+}
+
+static void
+service_name_owner_changed (GDBusProxy * proxy, GParamSpec *pspec, gpointer user_data)
+{
+ GtkWidget * widget = GTK_WIDGET (user_data);
+ gchar * owner = g_dbus_proxy_get_name_owner (proxy);
+
+ gtk_widget_set_sensitive (widget, (owner != NULL));
+
+ g_free (owner);
+}
+
+static void
+service_proxy_ready (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ GError * error = NULL;
+
+ proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
+
+ if (error != NULL) {
+ g_critical("Could not grab DBus proxy for indicator-datetime-service: %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ /* And now, do initial proxy configuration */
+ g_signal_connect (proxy, "notify::g-name-owner", G_CALLBACK (service_name_owner_changed), user_data);
+ service_name_owner_changed (proxy, NULL, user_data);
+}
+
+static gboolean
+are_spinners_focused (void)
+{
+ // save_time_id means that we were in focus and haven't finished our save
+ // yet, so act like we are still focused.
+ return save_time_id || gtk_widget_has_focus (time_spin) || gtk_widget_has_focus (date_spin);
+}
+
+static gboolean
+save_time (gpointer user_data)
+{
+ if (user_edited_time) {
+ gdouble current_value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (date_spin));
+ g_dbus_proxy_call (proxy, "SetTime", g_variant_new ("(x)", (guint64)current_value),
+ G_DBUS_CALL_FLAGS_NONE, -1, NULL, dbus_set_answered, "time");
+ }
+ user_edited_time = FALSE;
+ save_time_id = 0;
+ return FALSE;
+}
+
+static gboolean
+spin_focus_in (void)
+{
+ if (save_time_id > 0) {
+ g_source_remove (save_time_id);
+ save_time_id = 0;
+ }
+ return FALSE;
+}
+
+static gboolean
+spin_focus_out (void)
+{
+ /* We want to only save when both spinners are unfocused. But it's difficult
+ to tell who is about to get focus during a focus-out. So we set an idle
+ callback to save the time if we don't focus in to another spinner by that
+ time. */
+ if (save_time_id == 0) {
+ save_time_id = g_idle_add ((GSourceFunc)save_time, NULL);
+ }
+ return FALSE;
+}
+
+static int
+input_time_text (GtkWidget * spinner, gdouble * value, gpointer user_data)
+{
+ gboolean is_time = (gboolean)GPOINTER_TO_INT (g_object_get_data (G_OBJECT (spinner), "is-time"));
+ const gchar * text = gtk_entry_get_text (GTK_ENTRY (spinner));
+
+ gdouble current_value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinner));
+ *value = current_value;
+
+ GDateTime * now = g_date_time_new_from_unix_local (current_value);
+ gint year, month, day, hour, minute, second;
+ year = g_date_time_get_year (now);
+ month = g_date_time_get_month (now);
+ day = g_date_time_get_day_of_month (now);
+ hour = g_date_time_get_hour (now);
+ minute = g_date_time_get_minute (now);
+ second = g_date_time_get_second (now);
+ g_date_time_unref (now);
+
+ /* Parse this string as if it were in the output format */
+ gint scanned = 0;
+ gboolean passed = TRUE, skip = FALSE;
+ if (is_time) {
+ gint hour_in, minute_in, second_in;
+
+ if (is_locale_12h ()) { // TODO: make this look-at/watch gsettings?
+ char ampm[51];
+
+ scanned = sscanf (text, "%u:%u:%u %50s", &hour_in, &minute_in, &second_in, ampm);
+ passed = (scanned == 4);
+
+ if (passed) {
+ const char *pm_str = nl_langinfo (PM_STR);
+ if (g_ascii_strcasecmp (pm_str, ampm) == 0) {
+ hour_in += 12;
+ }
+ }
+ } else {
+ scanned = sscanf (text, "%u:%u:%u", &hour_in, &minute_in, &second_in);
+ passed = (scanned == 3);
+ }
+
+ if (passed && (hour_in > 23 || minute_in > 59 || second_in > 59)) {
+ passed = FALSE;
+ }
+ if (passed && hour == hour_in && minute == minute_in && second == second_in) {
+ skip = TRUE; // no change
+ } else {
+ hour = hour_in;
+ minute = minute_in;
+ second = second_in;
+ }
+ }
+ else {
+ gint year_in, month_in, day_in;
+
+ scanned = sscanf (text, "%u-%u-%u", &year_in, &month_in, &day_in);
+
+ if (scanned != 3 || year_in < 1 || year_in > 9999 ||
+ month_in < 1 || month_in > 12 || day_in < 1 || day_in > 31) {
+ passed = FALSE;
+ }
+ if (passed && year == year_in && month == month_in && day == day_in) {
+ skip = TRUE; // no change
+ } else {
+ year = year_in;
+ month = month_in;
+ day = day_in;
+ }
+ }
+
+ if (!passed) {
+ g_warning ("Could not understand %s", text);
+ return TRUE;
+ }
+
+ if (skip) {
+ return TRUE;
+ }
+
+ gboolean prev_changing = changing_time;
+ changing_time = TRUE;
+ GDateTime * new_time = g_date_time_new_local (year, month, day, hour, minute, second);
+ *value = g_date_time_to_unix (new_time);
+ user_edited_time = TRUE;
+ g_date_time_unref (new_time);
+ changing_time = prev_changing;
+
+ return TRUE;
+}
+
+static gboolean
+format_time_text (GtkWidget * spinner, gpointer user_data)
+{
+ gboolean is_time = (gboolean)GPOINTER_TO_INT (g_object_get_data (G_OBJECT (spinner), "is-time"));
+
+ const gchar * format;
+ if (is_time) {
+ if (is_locale_12h ()) { // TODO: make this look-at/watch gsettings?
+ format = "%I:%M:%S %p";
+ } else {
+ format = "%H:%M:%S";
+ }
+ }
+ else {
+ format = "%Y-%m-%d";
+ }
+
+ GDateTime * datetime = g_date_time_new_from_unix_local (gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinner)));
+ gchar * formatted = g_date_time_format (datetime, format);
+ gtk_entry_set_text (GTK_ENTRY (spinner), formatted);
+ g_date_time_unref (datetime);
+
+ return TRUE;
+}
+
+static void
+spin_copy_value (GtkSpinButton * spinner, GtkSpinButton * other)
+{
+ if (gtk_spin_button_get_value (spinner) != gtk_spin_button_get_value (other)) {
+ gtk_spin_button_set_value (other, gtk_spin_button_get_value (spinner));
+ }
+ if (!changing_time) { /* Means user pressed spin buttons */
+ user_edited_time = TRUE;
+ }
+}
+
+static gboolean
+update_spinners (void)
+{
+ /* Add datetime object to spinner, which will hold the real time value, rather
+ then using the value of the spinner itself. And don't update while user is
+ editing. */
+ if (!are_spinners_focused ()) {
+ gboolean prev_changing = changing_time;
+ changing_time = TRUE;
+ GDateTime * now = g_date_time_new_now_local ();
+ gtk_spin_button_set_value (GTK_SPIN_BUTTON (time_spin), (gdouble)g_date_time_to_unix (now));
+ /* will be copied to other spin button */
+ g_date_time_unref (now);
+ changing_time = prev_changing;
+ }
+ return TRUE;
+}
+
+static void
+setup_time_spinners (GtkWidget * time, GtkWidget * date)
+{
+ g_signal_connect (time, "input", G_CALLBACK (input_time_text), date);
+ g_signal_connect (date, "input", G_CALLBACK (input_time_text), time);
+
+ g_signal_connect (time, "output", G_CALLBACK (format_time_text), date);
+ g_signal_connect (date, "output", G_CALLBACK (format_time_text), time);
+
+ g_signal_connect (time, "focus-in-event", G_CALLBACK (spin_focus_in), date);
+ g_signal_connect (date, "focus-in-event", G_CALLBACK (spin_focus_in), time);
+
+ g_signal_connect (time, "focus-out-event", G_CALLBACK (spin_focus_out), date);
+ g_signal_connect (date, "focus-out-event", G_CALLBACK (spin_focus_out), time);
+
+ g_signal_connect (time, "value-changed", G_CALLBACK (spin_copy_value), date);
+ g_signal_connect (date, "value-changed", G_CALLBACK (spin_copy_value), time);
+
+ g_object_set_data (G_OBJECT (time), "is-time", GINT_TO_POINTER (TRUE));
+ g_object_set_data (G_OBJECT (date), "is-time", GINT_TO_POINTER (FALSE));
+
+ time_spin = time;
+ date_spin = date;
+
+ /* 2 seconds is what the indicator itself uses */
+ guint time_id = g_timeout_add_seconds (2, (GSourceFunc)update_spinners, NULL);
+ g_signal_connect_swapped (time_spin, "destroy", G_CALLBACK (g_source_remove), GINT_TO_POINTER (time_id));
+ update_spinners ();
+}
+
+static void
+hide_locations ()
+{
+ if (loc_dlg != NULL)
+ gtk_widget_destroy (loc_dlg);
+}
+
+static void
+show_locations (GtkWidget * button, GtkWidget * dlg)
+{
+ if (loc_dlg == NULL) {
+ loc_dlg = datetime_setup_locations_dialog (tzmap);
+ gtk_window_set_transient_for (GTK_WINDOW (loc_dlg), GTK_WINDOW (dlg));
+ g_signal_connect (loc_dlg, "destroy", G_CALLBACK (gtk_widget_destroyed), &loc_dlg);
+ g_signal_connect (dlg, "focus-in-event", G_CALLBACK (hide_locations), NULL);
+ gtk_widget_show_all (loc_dlg);
+ }
+ else {
+ gtk_window_present_with_time (GTK_WINDOW (loc_dlg), gtk_get_current_event_time ());
+ }
+}
+
+static gboolean
+timezone_selected (GtkEntryCompletion * widget, GtkTreeModel * model,
+ GtkTreeIter * iter, gpointer user_data)
+{
+ const gchar * name, * zone;
+
+ gtk_tree_model_get (model, iter,
+ TIMEZONE_COMPLETION_NAME, &name,
+ TIMEZONE_COMPLETION_ZONE, &zone,
+ -1);
+
+ if (zone == NULL || zone[0] == 0) {
+ const gchar * strlon, * strlat;
+ gdouble lon = 0.0, lat = 0.0;
+
+ gtk_tree_model_get (model, iter,
+ TIMEZONE_COMPLETION_LONGITUDE, &strlon,
+ TIMEZONE_COMPLETION_LATITUDE, &strlat,
+ -1);
+
+ if (strlon != NULL && strlon[0] != 0) {
+ lon = strtod(strlon, NULL);
+ }
+
+ if (strlat != NULL && strlat[0] != 0) {
+ lat = strtod(strlat, NULL);
+ }
+
+ zone = cc_timezone_map_get_timezone_at_coords (tzmap, lon, lat);
+ }
+
+ GSettings * conf = g_settings_new (SETTINGS_INTERFACE);
+ gchar * tz_name = g_strdup_printf ("%s %s", zone, name);
+ g_settings_set_string (conf, SETTINGS_TIMEZONE_NAME_S, tz_name);
+ g_free (tz_name);
+ g_object_unref (conf);
+
+ cc_timezone_map_set_timezone (tzmap, zone);
+
+ return FALSE; // Do normal action too
+}
+
+static gboolean
+entry_focus_out (GtkEntry * entry, GdkEventFocus * event)
+{
+ // If the name left in the entry doesn't match the current timezone name,
+ // show an error icon. It's always an error for the user to manually type in
+ // a timezone.
+ TzLocation * location = cc_timezone_map_get_location (tzmap);
+ if (location == NULL)
+ return FALSE;
+
+ gchar * name = get_current_zone_name (location->zone);
+ gboolean correct = (g_strcmp0 (gtk_entry_get_text (entry), name) == 0);
+ g_free (name);
+
+ gtk_entry_set_icon_from_stock (entry, GTK_ENTRY_ICON_SECONDARY,
+ correct ? NULL : GTK_STOCK_DIALOG_ERROR);
+ gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY,
+ _("You need to choose a location to change the time zone."));
+ gtk_entry_set_icon_activatable (entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
+ return FALSE;
+}
+
+static gboolean
+key_pressed (GtkWidget * widget, GdkEventKey * event, gpointer user_data)
+{
+ switch (event->keyval) {
+ case GDK_KEY_Escape:
+ gtk_widget_destroy (widget);
+ return TRUE;
+ }
+ return FALSE;
+}
+
+static GtkWidget *
+get_child_of_type (GtkContainer * parent, GType type)
+{
+ GList * children, * iter;
+
+ children = gtk_container_get_children (parent);
+ for (iter = children; iter; iter = iter->next) {
+ if (G_TYPE_CHECK_INSTANCE_TYPE (iter->data, type)) {
+ return GTK_WIDGET (iter->data);
+ }
+ }
+
+ return NULL;
+}
+
+static GtkWidget *
+create_dialog (void)
+{
+ GError * error = NULL;
+
+ GtkBuilder * builder = gtk_builder_new ();
+ gtk_builder_add_from_file (builder, DATETIME_DIALOG_UI_FILE, &error);
+ if (error != NULL) {
+ /* We have to abort, we can't continue without the ui file */
+ g_error ("Could not load ui file %s: %s", DATETIME_DIALOG_UI_FILE, error->message);
+ g_error_free (error);
+ return NULL;
+ }
+
+ gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
+
+ GSettings * conf = g_settings_new (SETTINGS_INTERFACE);
+
+#define WIG(name) GTK_WIDGET (gtk_builder_get_object (builder, name))
+
+ /* Add policykit button */
+ GtkWidget * polkit_button = polkit_lock_button_new ("org.gnome.settingsdaemon.datetimemechanism.configure");
+ polkit_lock_button_set_unlock_text (POLKIT_LOCK_BUTTON (polkit_button), _("Unlock to change these settings"));
+ polkit_lock_button_set_lock_text (POLKIT_LOCK_BUTTON (polkit_button), _("Lock to prevent further changes"));
+ gtk_box_pack_start (GTK_BOX (WIG ("timeDateBox")), polkit_button, FALSE, TRUE, 0);
+ /* Make sure border around button is visible */
+ GtkWidget * polkit_button_button = get_child_of_type (GTK_CONTAINER (polkit_button), GTK_TYPE_BUTTON);
+ if (polkit_button_button != NULL) {
+ gtk_button_set_relief (GTK_BUTTON (polkit_button_button), GTK_RELIEF_NORMAL);
+ }
+
+ /* Add map */
+ tzmap = cc_timezone_map_new ();
+ gtk_container_add (GTK_CONTAINER (WIG ("mapBox")), GTK_WIDGET (tzmap));
+ /* Fufill the CC by Attribution license requirements for the Geonames lookup */
+ cc_timezone_map_set_watermark (tzmap, "Geonames.org");
+
+ /* And completion entry */
+ TimezoneCompletion * completion = timezone_completion_new ();
+ timezone_completion_watch_entry (completion, GTK_ENTRY (WIG ("timezoneEntry")));
+ g_signal_connect (completion, "match-selected", G_CALLBACK (timezone_selected), NULL);
+ g_signal_connect (WIG ("timezoneEntry"), "focus-out-event", G_CALLBACK (entry_focus_out), NULL);
+
+ /* Set up settings bindings */
+ g_settings_bind (conf, SETTINGS_SHOW_CLOCK_S, WIG ("showClockCheck"),
+ "active", G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind (conf, SETTINGS_SHOW_DAY_S, WIG ("showWeekdayCheck"),
+ "active", G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind (conf, SETTINGS_SHOW_DATE_S, WIG ("showDateTimeCheck"),
+ "active", G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind (conf, SETTINGS_SHOW_SECONDS_S, WIG ("showSecondsCheck"),
+ "active", G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind_with_mapping (conf, SETTINGS_TIME_FORMAT_S,
+ WIG ("show12HourRadio"), "active",
+ G_SETTINGS_BIND_DEFAULT,
+ bind_hours_get, bind_hours_set,
+ GINT_TO_POINTER(TRUE), NULL);
+ g_settings_bind_with_mapping (conf, SETTINGS_TIME_FORMAT_S,
+ WIG ("show24HourRadio"), "active",
+ G_SETTINGS_BIND_DEFAULT,
+ bind_hours_get, bind_hours_set,
+ GINT_TO_POINTER(FALSE), NULL);
+ g_settings_bind (conf, SETTINGS_SHOW_CALENDAR_S, WIG ("showCalendarCheck"),
+ "active", G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind (conf, SETTINGS_SHOW_WEEK_NUMBERS_S, WIG ("includeWeekNumbersCheck"),
+ "active", G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind (conf, SETTINGS_SHOW_EVENTS_S, WIG ("showEventsCheck"),
+ "active", G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind (conf, SETTINGS_SHOW_LOCATIONS_S, WIG ("showLocationsCheck"),
+ "active", G_SETTINGS_BIND_DEFAULT);
+
+ /* Set up sensitivities */
+ add_widget_dependency (WIG ("showCalendarCheck"), WIG ("calendarOptions"));
+ add_widget_dependency (WIG ("showClockCheck"), WIG ("clockOptions"));
+ add_widget_dependency (WIG ("showLocationsCheck"), WIG ("locationsButton"));
+ add_widget_dependency (WIG ("manualTimeRadio"), WIG ("manualOptions"));
+ add_polkit_dependency (polkit_button, WIG ("timeDateOptions"));
+
+ /* Hacky proxy test for whether evolution-data-server is installed */
+ gchar * evo_path = g_find_program_in_path ("evolution");
+ gtk_widget_set_sensitive (WIG ("showEventsCheck"), (evo_path != NULL));
+ g_free (evo_path);
+
+ setup_time_spinners (WIG ("timeSpinner"), WIG ("dateSpinner"));
+
+ GtkWidget * dlg = WIG ("timeDateDialog");
+ auto_radio = WIG ("automaticTimeRadio");
+ tz_entry = WIG ("timezoneEntry");
+
+ g_signal_connect (WIG ("locationsButton"), "clicked", G_CALLBACK (show_locations), dlg);
+ g_signal_connect (dlg, "key-press-event", G_CALLBACK (key_pressed), NULL);
+
+ /* Grab proxy for settings daemon */
+ g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
+ "org.gnome.SettingsDaemon.DateTimeMechanism",
+ "/",
+ "org.gnome.SettingsDaemon.DateTimeMechanism",
+ NULL, proxy_ready, NULL);
+
+ /* Grab proxy for datetime service, to see if it's running. It would
+ actually be more ideal to see if the indicator module itself is running,
+ but that doesn't yet claim a name on the bus. Presumably the service
+ would have been started by any such indicator, so this will at least tell
+ us if there *was* a datetime module run this session. */
+ g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, NULL,
+ SERVICE_NAME, SERVICE_OBJ, SERVICE_IFACE,
+ NULL, service_proxy_ready, WIG ("showClockCheck"));
+
+#undef WIG
+
+ g_object_unref (conf);
+ g_object_unref (builder);
+
+ return dlg;
+}
+
+static UniqueResponse
+message_received (UniqueApp * app, gint command, UniqueMessageData *message_data,
+ guint time, gpointer user_data)
+{
+ if (command == UNIQUE_ACTIVATE) {
+ gtk_window_present_with_time (GTK_WINDOW (user_data), time);
+ return UNIQUE_RESPONSE_OK;
+ }
+ return UNIQUE_RESPONSE_PASSTHROUGH;
+}
+
+int
+main (int argc, char ** argv)
+{
+ g_type_init ();
+
+ /* Setting up i18n and gettext. Apparently, we need
+ all of these. */
+ setlocale (LC_ALL, "");
+ bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
+ textdomain (GETTEXT_PACKAGE);
+
+ gtk_init (&argc, &argv);
+
+ UniqueApp * app = unique_app_new ("com.canonical.indicator.datetime.preferences", NULL);
+
+ if (unique_app_is_running (app)) {
+ unique_app_send_message (app, UNIQUE_ACTIVATE, NULL);
+ } else {
+ // We're first instance. Yay!
+ GtkWidget * dlg = create_dialog ();
+
+ g_signal_connect (app, "message-received", G_CALLBACK(message_received), dlg);
+ unique_app_watch_window (app, GTK_WINDOW (dlg));
+
+ gtk_widget_show_all (dlg);
+ g_signal_connect (dlg, "destroy", G_CALLBACK(gtk_main_quit), NULL);
+ gtk_main ();
+ }
+
+ return 0;
+}
+
diff --git a/src/datetime-service.c b/src/datetime-service.c
new file mode 100644
index 0000000..08ff9ad
--- /dev/null
+++ b/src/datetime-service.c
@@ -0,0 +1,1493 @@
+/*
+An indicator to time and date related information in the menubar.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ Ted Gould <ted@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <config.h>
+#include <libindicator/indicator-service.h>
+#include <locale.h>
+
+#include <gtk/gtk.h>
+#include <gdk/gdk.h>
+#include <glib/gi18n.h>
+#include <gio/gio.h>
+#include <math.h>
+#include <gconf/gconf-client.h>
+
+#include <libdbusmenu-gtk/menuitem.h>
+#include <libdbusmenu-glib/server.h>
+#include <libdbusmenu-glib/client.h>
+#include <libdbusmenu-glib/menuitem.h>
+
+#include <geoclue/geoclue-master.h>
+#include <geoclue/geoclue-master-client.h>
+
+#include <time.h>
+#include <libecal/e-cal.h>
+#include <libical/ical.h>
+#include <libecal/e-cal-time-util.h>
+#include <libedataserver/e-source.h>
+#include <libedataserverui/e-passwords.h>
+// Other users of ecal seem to also include these, not sure why they should be included by the above
+#include <libical/icaltime.h>
+#include <cairo/cairo.h>
+
+#include "datetime-interface.h"
+#include "dbus-shared.h"
+#include "settings-shared.h"
+#include "utils.h"
+
+
+static void geo_create_client (GeoclueMaster * master, GeoclueMasterClient * client, gchar * path, GError * error, gpointer user_data);
+static gboolean update_appointment_menu_items (gpointer user_data);
+static gboolean update_timezone_menu_items(gpointer user_data);
+static void setup_timer (void);
+static void geo_client_invalid (GeoclueMasterClient * client, gpointer user_data);
+static void geo_address_change (GeoclueMasterClient * client, gchar * a, gchar * b, gchar * c, gchar * d, gpointer user_data);
+
+static IndicatorService * service = NULL;
+static GMainLoop * mainloop = NULL;
+static DbusmenuServer * server = NULL;
+static DbusmenuMenuitem * root = NULL;
+static DatetimeInterface * dbus = NULL;
+
+/* Global Items */
+static DbusmenuMenuitem * date = NULL;
+static DbusmenuMenuitem * calendar = NULL;
+static DbusmenuMenuitem * settings = NULL;
+static DbusmenuMenuitem * events_separator = NULL;
+static DbusmenuMenuitem * locations_separator = NULL;
+static DbusmenuMenuitem * geo_location = NULL;
+static DbusmenuMenuitem * current_location = NULL;
+//static DbusmenuMenuitem * ecal_location = NULL;
+static DbusmenuMenuitem * add_appointment = NULL;
+static GList * appointments = NULL;
+static GList * dconflocations = NULL;
+static GList * comp_instances = NULL;
+static gboolean updating_appointments = FALSE;
+static time_t start_time_appointments = (time_t) 0;
+GSettings *conf;
+GConfClient* gconf;
+
+
+/* Geoclue trackers */
+static GeoclueMasterClient * geo_master = NULL;
+static GeoclueAddress * geo_address = NULL;
+
+/* Our 2 important timezones */
+static gchar * current_timezone = NULL;
+static gchar * geo_timezone = NULL;
+
+struct comp_instance {
+ ECalComponent *comp;
+ time_t start;
+ time_t end;
+ ESource *source;
+};
+
+static void
+set_timezone_label (DbusmenuMenuitem * mi, const gchar * location)
+{
+ gchar * zone, * name;
+ split_settings_location (location, &zone, &name);
+
+ dbusmenu_menuitem_property_set (mi, TIMEZONE_MENUITEM_PROP_NAME, name);
+ dbusmenu_menuitem_property_set (mi, TIMEZONE_MENUITEM_PROP_ZONE, zone);
+
+ g_free (zone);
+ g_free (name);
+}
+
+static void
+set_current_timezone_label (DbusmenuMenuitem * mi, const gchar * location)
+{
+ gchar * name = get_current_zone_name (location);
+
+ dbusmenu_menuitem_property_set (mi, TIMEZONE_MENUITEM_PROP_NAME, name);
+ dbusmenu_menuitem_property_set (mi, TIMEZONE_MENUITEM_PROP_ZONE, location);
+
+ g_free (name);
+}
+
+/* Check to see if our timezones are the same */
+static void
+check_timezone_sync (void) {
+ gchar * label;
+ gboolean in_sync = FALSE;
+
+ if (geo_timezone == NULL) {
+ in_sync = TRUE;
+ }
+
+ if (current_timezone == NULL) {
+ in_sync = TRUE;
+ }
+
+ if (!in_sync && g_strcmp0(geo_timezone, current_timezone) == 0) {
+ in_sync = TRUE;
+ }
+
+ if (in_sync) {
+ g_debug("Timezones in sync");
+ } else {
+ g_debug("Timezones are different");
+ }
+
+ gboolean show = g_settings_get_boolean (conf, SETTINGS_SHOW_LOCATIONS_S);
+
+ if (geo_location != NULL && current_location != NULL) {
+ g_debug("Got timezone %s", current_timezone);
+ g_debug("Got timezone %s", geo_timezone);
+ // Show neither current location nor geo location if both are the same
+ // however, we want to set their time and label accordingly
+ if (in_sync) {
+ if (current_timezone == NULL && geo_timezone == NULL) {
+ dbusmenu_menuitem_property_set_bool(locations_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_property_set_bool (current_location, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_property_set_bool (geo_location, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ update_timezone_menu_items(NULL); // Update the timezone menu items
+ return;
+ }
+
+ dbusmenu_menuitem_property_set_bool (locations_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_property_set_bool (current_location, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_property_set_bool (current_location, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
+ dbusmenu_menuitem_property_set_bool (geo_location, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_property_set_bool (geo_location, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
+
+ if (current_timezone != NULL) {
+ label = current_timezone;
+ } else {
+ label = geo_timezone;
+ }
+
+ if (label != NULL) {
+ // TODO work out the current location name in a nice way
+ set_current_timezone_label (current_location, label);
+ // TODO work out the current time at that location
+ dbusmenu_menuitem_property_set_bool (current_location, DBUSMENU_MENUITEM_PROP_VISIBLE, show);
+ dbusmenu_menuitem_property_set_bool(current_location, TIMEZONE_MENUITEM_PROP_RADIO, TRUE);
+ } else {
+ g_debug("Label for current location is null, this shouldn't happen");
+ }
+ if (geo_timezone != NULL) {
+ // TODO work out the geo location name in a nice way
+ set_current_timezone_label (geo_location, geo_timezone);
+ // TODO work out the current time at that location
+ dbusmenu_menuitem_property_set_bool (geo_location, DBUSMENU_MENUITEM_PROP_VISIBLE, show);
+ }
+ } else {
+ // TODO work out the geo location name in a nice way
+ set_current_timezone_label (geo_location, geo_timezone);
+ // TODO work out the current time at that location
+ dbusmenu_menuitem_property_set_bool(geo_location, DBUSMENU_MENUITEM_PROP_VISIBLE, show);
+
+ // TODO work out the current location name in a nice way
+ set_current_timezone_label (current_location, current_timezone);
+ // TODO work out the current time at that location
+ dbusmenu_menuitem_property_set_bool(current_location, TIMEZONE_MENUITEM_PROP_RADIO, TRUE);
+ dbusmenu_menuitem_property_set_bool(current_location, DBUSMENU_MENUITEM_PROP_VISIBLE, show);
+ dbusmenu_menuitem_property_set_bool(locations_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, show);
+ }
+ }
+ g_debug("Finished checking timezone sync");
+ update_timezone_menu_items(NULL); // Update the timezone menu items
+
+ return;
+}
+
+/* Update the current timezone */
+static void
+update_current_timezone (void) {
+ /* Clear old data */
+ if (current_timezone != NULL) {
+ g_free(current_timezone);
+ current_timezone = NULL;
+ }
+
+ GError * error = NULL;
+ gchar * tempzone = NULL;
+ if (!g_file_get_contents(TIMEZONE_FILE, &tempzone, NULL, &error)) {
+ g_warning("Unable to read timezone file '" TIMEZONE_FILE "': %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ /* This shouldn't happen, so let's make it a big boom! */
+ g_return_if_fail(tempzone != NULL);
+
+ /* Note: this really makes sense as strstrip works in place
+ so we end up with something a little odd without the dup
+ so we have the dup to make sure everything is as expected
+ for everyone else. */
+ current_timezone = g_strdup(g_strstrip(tempzone));
+ g_free(tempzone);
+
+ g_debug("System timezone is: %s", current_timezone);
+
+ check_timezone_sync();
+
+ if (error != NULL) g_error_free(error);
+ return;
+}
+
+static void
+quick_set_tz_cb (GObject *object, GAsyncResult *res, gpointer data)
+{
+ GError * error = NULL;
+ GVariant * answers = g_dbus_proxy_call_finish (G_DBUS_PROXY (object), res, &error);
+
+ if (error != NULL) {
+ g_warning("Could not set timezone for SettingsDaemon: %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ g_variant_unref (answers);
+}
+
+static void
+quick_set_tz_proxy_cb (GObject *object, GAsyncResult *res, gpointer zone)
+{
+ GError * error = NULL;
+
+ GDBusProxy * proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
+
+ if (error != NULL) {
+ g_warning("Could not grab DBus proxy for SettingsDaemon: %s", error->message);
+ g_error_free(error);
+ g_free (zone);
+ return;
+ }
+
+ gchar * file = g_build_filename ("/usr/share/zoneinfo", (char *)zone, NULL);
+ g_dbus_proxy_call (proxy, "SetTimezone", g_variant_new ("(s)", file),
+ G_DBUS_CALL_FLAGS_NONE, -1, NULL, quick_set_tz_cb, NULL);
+ g_free (file);
+ g_free (zone);
+ g_object_unref (proxy);
+}
+
+static void
+quick_set_tz (DbusmenuMenuitem * menuitem, guint timestamp, gpointer user_data)
+{
+ const gchar * tz = dbusmenu_menuitem_property_get(menuitem, TIMEZONE_MENUITEM_PROP_ZONE);
+ g_debug("Quick setting timezone to: %s", tz);
+
+ g_return_if_fail(tz != NULL);
+
+ const gchar * name = dbusmenu_menuitem_property_get(menuitem, TIMEZONE_MENUITEM_PROP_NAME);
+
+ /* Set it in gsettings so we don't lose user's preferred name */
+ GSettings * conf = g_settings_new (SETTINGS_INTERFACE);
+ gchar * tz_full = g_strdup_printf ("%s %s", tz, name);
+ g_settings_set_string (conf, SETTINGS_TIMEZONE_NAME_S, tz_full);
+ g_free (tz_full);
+ g_object_unref (conf);
+
+ g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
+ "org.gnome.SettingsDaemon.DateTimeMechanism",
+ "/",
+ "org.gnome.SettingsDaemon.DateTimeMechanism",
+ NULL, quick_set_tz_proxy_cb, g_strdup (tz));
+
+ return;
+}
+
+/* Updates the label in the date menuitem */
+static gboolean
+update_datetime (gpointer user_data)
+{
+ g_debug("Updating Date/Time");
+
+ gchar longstr[128];
+ time_t t;
+ struct tm *ltime;
+
+ t = time(NULL);
+ ltime = localtime(&t);
+ if (ltime == NULL) {
+ g_warning("Error getting local time");
+ dbusmenu_menuitem_property_set(date, DBUSMENU_MENUITEM_PROP_LABEL, _("Error getting time"));
+ return FALSE;
+ }
+
+ /* Translators: strftime(3) style date format on top of the menu when you click on the clock */
+ strftime(longstr, 128, _("%A, %e %B %Y"), ltime);
+
+ gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);
+ dbusmenu_menuitem_property_set(date, DBUSMENU_MENUITEM_PROP_LABEL, utf8);
+ g_free(utf8);
+
+ return FALSE;
+}
+
+/* Run a particular program based on an activation */
+static void
+activate_cb (DbusmenuMenuitem * menuitem, guint timestamp, const gchar *command)
+{
+ GError * error = NULL;
+
+ g_debug("Issuing command '%s'", command);
+ if (!g_spawn_command_line_async(command, &error)) {
+ g_warning("Unable to start %s: %s", (char *)command, error->message);
+ g_error_free(error);
+ }
+}
+
+static gboolean
+update_appointment_menu_items_idle (gpointer user_data)
+{
+ update_appointment_menu_items(user_data);
+ return FALSE;
+}
+
+static gboolean
+month_changed_cb (DbusmenuMenuitem * menuitem, gchar *name, GVariant *variant, guint timestamp)
+{
+ start_time_appointments = (time_t)g_variant_get_uint32(variant);
+
+ g_debug("Received month changed with timestamp: %d -> %s",(int)start_time_appointments, ctime(&start_time_appointments));
+ /* By default one of the first things we do is
+ clear the marks as we don't know the correct
+ ones yet and we don't want to confuse the
+ user. */
+ dbusmenu_menuitem_property_remove(menuitem, CALENDAR_MENUITEM_PROP_MARKS);
+
+ GList * appointment;
+ for (appointment = appointments; appointment != NULL; appointment = g_list_next(appointment)) {
+ DbusmenuMenuitem * mi = DBUSMENU_MENUITEM(appointment->data);
+ dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
+ }
+
+ g_idle_add(update_appointment_menu_items_idle, NULL);
+ return TRUE;
+}
+
+static gboolean
+day_selected_cb (DbusmenuMenuitem * menuitem, gchar *name, GVariant *variant, guint timestamp)
+{
+ time_t new_time = (time_t)g_variant_get_uint32(variant);
+ g_warn_if_fail(new_time != 0);
+
+ if (start_time_appointments == 0 || new_time == 0) {
+ /* If we've got nothing, assume everyhting is going to
+ get repopulated, let's start with a clean slate */
+ dbusmenu_menuitem_property_remove(menuitem, CALENDAR_MENUITEM_PROP_MARKS);
+ } else {
+ /* No check to see if we changed months. If we did we'll
+ want to clear the marks. Otherwise we're cool keeping
+ them around. */
+ struct tm start_tm;
+ struct tm new_tm;
+
+ localtime_r(&start_time_appointments, &start_tm);
+ localtime_r(&new_time, &new_tm);
+
+ if (start_tm.tm_mon != new_tm.tm_mon) {
+ dbusmenu_menuitem_property_remove(menuitem, CALENDAR_MENUITEM_PROP_MARKS);
+ }
+ }
+
+ GList * appointment;
+ for (appointment = appointments; appointment != NULL; appointment = g_list_next(appointment)) {
+ DbusmenuMenuitem * mi = DBUSMENU_MENUITEM(appointment->data);
+ dbusmenu_menuitem_property_set_bool(mi, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
+ }
+
+ start_time_appointments = new_time;
+
+ g_debug("Received day-selected with timestamp: %d -> %s",(int)start_time_appointments, ctime(&start_time_appointments));
+ g_idle_add(update_appointment_menu_items_idle, NULL);
+
+ return TRUE;
+}
+
+static gboolean
+day_selected_double_click_cb (DbusmenuMenuitem * menuitem, gchar *name, GVariant *variant, guint timestamp)
+{
+ time_t evotime = (time_t)g_variant_get_uint32(variant);
+
+ g_debug("Received day-selected-double-click with timestamp: %d -> %s",(int)evotime, ctime(&evotime));
+
+ gchar *ad = isodate_from_time_t(evotime);
+ gchar *cmd = g_strconcat("evolution calendar:///?startdate=", ad, NULL);
+
+ GError * error = NULL;
+
+ g_debug("Issuing command '%s'", cmd);
+ if (!g_spawn_command_line_async(cmd, &error)) {
+ g_warning("Unable to start %s: %s", (char *)cmd, error->message);
+ g_error_free(error);
+ }
+
+ return TRUE;
+}
+
+static guint ecaltimer = 0;
+
+static void
+start_ecal_timer(void)
+{
+ if (ecaltimer != 0) g_source_remove(ecaltimer);
+ if (update_appointment_menu_items(NULL))
+ ecaltimer = g_timeout_add_seconds(60*5, update_appointment_menu_items, NULL);
+}
+
+static void
+stop_ecal_timer(void)
+{
+ if (ecaltimer != 0) g_source_remove(ecaltimer);
+}
+static gboolean
+idle_start_ecal_timer (gpointer data)
+{
+ start_ecal_timer();
+ return FALSE;
+}
+
+static void
+show_events_changed (void)
+{
+ if (g_settings_get_boolean(conf, SETTINGS_SHOW_EVENTS_S)) {
+ dbusmenu_menuitem_property_set_bool(add_appointment, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
+ dbusmenu_menuitem_property_set_bool(events_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
+ start_ecal_timer();
+ } else {
+ dbusmenu_menuitem_property_set_bool(add_appointment, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_property_set_bool(events_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ /* Remove all of the previous appointments */
+ if (appointments != NULL) {
+ g_debug("Hiding old appointments");
+ GList * appointment;
+ for (appointment = appointments; appointment != NULL; appointment = g_list_next(appointment)) {
+ DbusmenuMenuitem * litem = DBUSMENU_MENUITEM(appointment->data);
+ g_debug("Hiding old appointment: %p", litem);
+ // Remove all the existing menu items which are in appointments.
+ dbusmenu_menuitem_property_set_bool(DBUSMENU_MENUITEM(litem), DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ }
+ }
+ stop_ecal_timer();
+ }
+}
+
+/* Looks for the calendar application and enables the item if
+ we have one, starts ecal timer if events are turned on */
+static gboolean
+check_for_calendar (gpointer user_data)
+{
+ g_return_val_if_fail (calendar != NULL, FALSE);
+
+ dbusmenu_menuitem_property_set_bool(date, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
+
+ gchar *evo = g_find_program_in_path("evolution");
+ if (evo != NULL) {
+ g_debug("Found the calendar application: %s", evo);
+
+ g_signal_connect (G_OBJECT(date), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED,
+ G_CALLBACK (activate_cb), "evolution -c calendar");
+
+ events_separator = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set(events_separator, DBUSMENU_MENUITEM_PROP_TYPE, DBUSMENU_CLIENT_TYPES_SEPARATOR);
+ dbusmenu_menuitem_child_add_position(root, events_separator, 2);
+ add_appointment = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set (add_appointment, DBUSMENU_MENUITEM_PROP_LABEL, _("Add Event..."));
+ dbusmenu_menuitem_property_set_bool(add_appointment, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
+ g_signal_connect(G_OBJECT(add_appointment), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(activate_cb), "evolution -c calendar");
+ dbusmenu_menuitem_child_add_position (root, add_appointment, 3);
+
+
+ if (g_settings_get_boolean(conf, SETTINGS_SHOW_EVENTS_S)) {
+ dbusmenu_menuitem_property_set_bool(add_appointment, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
+ dbusmenu_menuitem_property_set_bool(events_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
+ g_idle_add((GSourceFunc)idle_start_ecal_timer, NULL);
+ } else {
+ dbusmenu_menuitem_property_set_bool(add_appointment, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_property_set_bool(events_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ stop_ecal_timer();
+ }
+
+ // Connect to calendar events
+ g_signal_connect(calendar, "event::month-changed", G_CALLBACK(month_changed_cb), NULL);
+ g_signal_connect(calendar, "event::day-selected", G_CALLBACK(day_selected_cb), NULL);
+ g_signal_connect(calendar, "event::day-selected-double-click", G_CALLBACK(day_selected_double_click_cb), NULL);
+ g_free(evo);
+ } else {
+ g_debug("Unable to find calendar app.");
+ dbusmenu_menuitem_property_set_bool(add_appointment, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_property_set_bool(events_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ }
+
+ if (g_settings_get_boolean(conf, SETTINGS_SHOW_CALENDAR_S)) {
+ dbusmenu_menuitem_property_set_bool(calendar, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
+ dbusmenu_menuitem_property_set_bool(calendar, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
+ } else {
+ dbusmenu_menuitem_property_set_bool(calendar, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
+ dbusmenu_menuitem_property_set_bool(calendar, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ }
+
+ return FALSE;
+}
+
+
+static gboolean
+update_timezone_menu_items(gpointer user_data) {
+ g_debug("Updating timezone menu items");
+
+ gchar ** locations = g_settings_get_strv(conf, SETTINGS_LOCATIONS_S);
+
+ if (locations == NULL) {
+ g_debug("No locations configured (NULL)");
+ return FALSE;
+ }
+ guint len = g_strv_length(locations);
+ DbusmenuMenuitem *item;
+ gint i, offset;
+
+ /* Remove all of the previous locations */
+ if (dconflocations != NULL) {
+ while (dconflocations != NULL) {
+ DbusmenuMenuitem * litem = DBUSMENU_MENUITEM(dconflocations->data);
+ // Remove all the existing menu items which are in dconflocations.
+ dconflocations = g_list_remove(dconflocations, litem);
+ dbusmenu_menuitem_child_delete(root, DBUSMENU_MENUITEM(litem));
+ g_object_unref(G_OBJECT(litem));
+ }
+ }
+
+ gboolean show = g_settings_get_boolean (conf, SETTINGS_SHOW_LOCATIONS_S);
+
+ dbusmenu_menuitem_property_set_bool (locations_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, show);
+ dbusmenu_menuitem_property_set_bool (current_location, DBUSMENU_MENUITEM_PROP_VISIBLE, show);
+ dbusmenu_menuitem_property_set_bool (current_location, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
+
+ if (len == 0) {
+ g_debug("No locations configured (Empty List)");
+ return FALSE;
+ }
+
+ offset = dbusmenu_menuitem_get_position (current_location, root)+1;
+ for (i = 0; i < len; i++) {
+ // Iterate over configured places and add any which aren't already listed
+ if (g_strcmp0(locations[i], current_timezone) != 0 &&
+ g_strcmp0(locations[i], geo_timezone) != 0) {
+ g_debug("Adding timezone in update_timezones %s", locations[i]);
+ item = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set (item, DBUSMENU_MENUITEM_PROP_TYPE, TIMEZONE_MENUITEM_TYPE);
+ set_timezone_label (item, locations[i]);
+ dbusmenu_menuitem_property_set_bool (item, TIMEZONE_MENUITEM_PROP_RADIO, FALSE);
+ dbusmenu_menuitem_property_set_bool (item, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
+ dbusmenu_menuitem_property_set_bool (item, DBUSMENU_MENUITEM_PROP_VISIBLE, show);
+ dbusmenu_menuitem_child_add_position (root, item, offset++);
+ g_signal_connect(G_OBJECT(item), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(quick_set_tz), NULL);
+ dconflocations = g_list_append(dconflocations, item);
+ }
+ }
+ g_strfreev (locations);
+ return FALSE;
+}
+
+// Authentication function
+static gchar *
+auth_func (ECal *ecal,
+ const gchar *prompt,
+ const gchar *key,
+ gpointer user_data)
+{
+ ESource *source = e_cal_get_source (ecal);
+ gchar *auth_domain = e_source_get_duped_property (source, "auth-domain");
+
+ const gchar *component_name;
+ if (auth_domain) component_name = auth_domain;
+ else component_name = "Calendar";
+
+ gchar *password = e_passwords_get_password (component_name, key);
+
+ g_free (auth_domain);
+
+ return password;
+}
+
+static gint
+compare_comp_instances (gconstpointer a,
+ gconstpointer b)
+{
+ const struct comp_instance *ci_a = a;
+ const struct comp_instance *ci_b = b;
+ time_t d = ci_a->start - ci_b->start;
+ if (d < 0) return -1;
+ else if (d > 0) return 1;
+ return 0;
+}
+
+static gboolean
+populate_appointment_instances (ECalComponent *comp,
+ time_t instance_start,
+ time_t instance_end,
+ gpointer data)
+{
+ g_debug("Appending item %p", comp);
+
+ ECalComponentVType vtype = e_cal_component_get_vtype (comp);
+ if (vtype != E_CAL_COMPONENT_EVENT && vtype != E_CAL_COMPONENT_TODO) return FALSE;
+
+ icalproperty_status status;
+ e_cal_component_get_status (comp, &status);
+ if (status == ICAL_STATUS_COMPLETED || status == ICAL_STATUS_CANCELLED) return FALSE;
+
+ g_object_ref(comp);
+
+ ECalComponentDateTime datetime;
+ icaltimezone *appointment_zone = NULL;
+ icaltimezone *current_zone = NULL;
+
+ if (vtype == E_CAL_COMPONENT_EVENT)
+ e_cal_component_get_dtstart (comp, &datetime);
+ else
+ e_cal_component_get_due (comp, &datetime);
+
+ appointment_zone = icaltimezone_get_builtin_timezone_from_tzid(datetime.tzid);
+ current_zone = icaltimezone_get_builtin_timezone_from_tzid(current_timezone);
+ if (!appointment_zone || datetime.value->is_date) { // If it's today put in the current timezone?
+ appointment_zone = current_zone;
+ }
+
+ // TODO: Convert the timezone into a 3 letter abbreviation if it's different to current_timezone
+ // TODO: Add the appointment timezone to the list if it's not already there.
+
+ struct comp_instance *ci;
+ ci = g_new (struct comp_instance, 1);
+
+ g_debug("Using times start %s, end %s", ctime(&instance_start), ctime(&instance_end));
+
+ ci->comp = comp;
+ ci->source = E_SOURCE(data);
+ ci->start = instance_start;
+ ci->end = instance_end;
+
+ comp_instances = g_list_append(comp_instances, ci);
+ return TRUE;
+}
+
+/* Populate the menu with todays, next 5 appointments.
+ * we should hook into the ABOUT TO SHOW signal and use that to update the appointments.
+ * Experience has shown that caldav's and webcals can be slow to load from eds
+ * this is a problem mainly on the EDS side of things, not ours.
+ */
+static gboolean
+update_appointment_menu_items (gpointer user_data)
+{
+ // FFR: we should take into account short term timers, for instance
+ // tea timers, pomodoro timers etc... that people may add, this is hinted to in the spec.
+ g_debug("Update appointments called");
+ if (calendar == NULL) return FALSE;
+ if (!g_settings_get_boolean(conf, SETTINGS_SHOW_EVENTS_S)) return FALSE;
+ if (updating_appointments) return TRUE;
+ updating_appointments = TRUE;
+
+ time_t curtime = 0, t1 = 0, t2 = 0;
+ gchar *ad;
+ GList *l;
+ GSList *g;
+ GError *gerror = NULL;
+ gint i;
+ gint width = 0, height = 0;
+ ESourceList * sources = NULL;
+
+ // Get today & work out query times
+ time(&curtime);
+ struct tm *today = localtime(&curtime);
+ const int mday = today->tm_mday;
+ const int mon = today->tm_mon;
+ const int year = today->tm_year;
+
+ int start_month_saved = mon;
+
+ struct tm *start_tm = NULL;
+ int this_year = today->tm_year + 1900;
+ int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
+ if ((this_year % 400 == 0) || (this_year % 100 > 0 && this_year % 4 == 0)) days[1] = 29;
+
+ int highlightdays = days[mon] - mday + 1;
+ t1 = curtime; // By default the current time is the appointment start time.
+
+ if (start_time_appointments > 0) {
+ start_tm = localtime(&start_time_appointments);
+ int start_month = start_tm->tm_mon;
+ start_month_saved = start_month;
+ int start_year = start_tm->tm_year + 1900;
+ if ((start_month != mon) || (start_year != this_year)) {
+ // Set t1 to the start of that month.
+ struct tm month_start = {0};
+ month_start.tm_year = start_tm->tm_year;
+ month_start.tm_mon = start_tm->tm_mon;
+ month_start.tm_mday = 1;
+ t1 = mktime(&month_start);
+ highlightdays = days[start_month];
+ }
+ }
+
+ g_debug("Will highlight %d days from %s", highlightdays, ctime(&t1));
+
+ highlightdays = highlightdays + 7; // Minimum of 7 days ahead
+ t2 = t1 + (time_t) (highlightdays * 24 * 60 * 60);
+
+ if (!e_cal_get_sources(&sources, E_CAL_SOURCE_TYPE_EVENT, &gerror)) {
+ g_debug("Failed to get ecal sources\n");
+ return FALSE;
+ }
+
+ // Free comp_instances if not NULL
+ if (comp_instances != NULL) {
+ g_debug("Freeing comp_instances: may be an overlap\n");
+ for (l = comp_instances; l; l = l->next) {
+ const struct comp_instance *ci = l->data;
+ g_object_unref(ci->comp);
+ g_list_free(comp_instances);
+ comp_instances = NULL;
+ }
+ }
+ GSList *cal_list = gconf_client_get_list(gconf, "/apps/evolution/calendar/display/selected_calendars", GCONF_VALUE_STRING, &gerror);
+ if (gerror) {
+ g_debug("Failed to get evolution preference for enabled calendars");
+ g_error_free(gerror);
+ gerror = NULL;
+ cal_list = NULL;
+ }
+ // Generate instances for all sources
+ for (g = e_source_list_peek_groups (sources); g; g = g->next) {
+ ESourceGroup *group = E_SOURCE_GROUP (g->data);
+ GSList *s;
+
+ for (s = e_source_group_peek_sources (group); s; s = s->next) {
+ ESource *source = E_SOURCE (s->data);
+ g_signal_connect (G_OBJECT(source), "changed", G_CALLBACK (update_appointment_menu_items), NULL);
+ ECal *ecal = e_cal_new(source, E_CAL_SOURCE_TYPE_EVENT);
+ e_cal_set_auth_func (ecal, (ECalAuthFunc) auth_func, NULL);
+
+ if (!e_cal_open(ecal, FALSE, &gerror)) {
+ g_debug("Failed to get ecal sources %s", gerror->message);
+ g_error_free(gerror);
+ gerror = NULL;
+ continue;
+ }
+ const gchar *ecal_uid = e_source_peek_uid(source);
+ gboolean match = FALSE;
+ g_debug("Checking ecal_uid is enabled: %s", ecal_uid);
+ for (i = 0; i<g_slist_length(cal_list);i++) {
+ char *cuid = (char *)g_slist_nth_data(cal_list, i);
+ if (g_strcmp0(cuid, ecal_uid) == 0) {
+ match = TRUE;
+ break;
+ }
+ }
+ if (!match) continue;
+ g_debug("ecal_uid is enabled, generating instances");
+
+ e_cal_generate_instances (ecal, t1, t2, (ECalRecurInstanceFn) populate_appointment_instances, (gpointer) source);
+ }
+ }
+ g_debug("Number of ECalComponents returned: %d", g_list_length(comp_instances));
+ GList *sorted_comp_instances = g_list_sort(comp_instances, compare_comp_instances);
+ comp_instances = NULL;
+ g_debug("Components sorted");
+
+ /* Hiding all of the previous appointments */
+ if (appointments != NULL) {
+ g_debug("Hiding old appointments");
+ GList * appointment;
+ for (appointment = appointments; appointment != NULL; appointment = g_list_next(appointment)) {
+ DbusmenuMenuitem * litem = DBUSMENU_MENUITEM(appointment->data);
+ g_debug("Hiding old appointment: %p", litem);
+ // Remove all the existing menu items which are in appointments.
+ dbusmenu_menuitem_property_set_bool(DBUSMENU_MENUITEM(litem), DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ }
+ }
+
+ gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height);
+ if (width <= 0) width = 12;
+ if (height <= 0) height = 12;
+ if (width > 30) width = 12;
+ if (height > 30) height = 12;
+
+ gchar *time_format_str = g_settings_get_string(conf, SETTINGS_TIME_FORMAT_S);
+ gint apt_output;
+ if (g_strcmp0(time_format_str, "12-hour") == 0) {
+ apt_output = SETTINGS_TIME_12_HOUR;
+ } else if (g_strcmp0(time_format_str, "24-hour") == 0) {
+ apt_output = SETTINGS_TIME_24_HOUR;
+ } else {
+ if (is_locale_12h()) {
+ apt_output = SETTINGS_TIME_12_HOUR;
+ } else {
+ apt_output = SETTINGS_TIME_24_HOUR;
+ }
+ }
+
+ GVariantBuilder markeddays;
+ g_variant_builder_init (&markeddays, G_VARIANT_TYPE_ARRAY);
+
+ i = 0;
+ GList * cached_appointment = appointments;
+ for (l = sorted_comp_instances; l; l = l->next) {
+ struct comp_instance *ci = l->data;
+ ECalComponent *ecalcomp = ci->comp;
+ ECalComponentText valuetext;
+ gchar *summary, *cmd;
+ char right[20];
+ //const gchar *uri;
+ DbusmenuMenuitem * item;
+
+ ECalComponentVType vtype = e_cal_component_get_vtype (ecalcomp);
+ struct tm due_data = {0};
+ struct tm *due = NULL;
+ if (vtype == E_CAL_COMPONENT_EVENT) due = localtime_r(&ci->start, &due_data);
+ else if (vtype == E_CAL_COMPONENT_TODO) due = localtime_r(&ci->end, &due_data);
+ else continue;
+
+ const int dmday = due->tm_mday;
+ const int dmon = due->tm_mon;
+ const int dyear = due->tm_year;
+
+ if (start_month_saved == dmon) {
+ // Mark day if our query hasn't hit the next month.
+ g_debug("Adding marked date %s, %d", ctime(&ci->start), dmday);
+ g_variant_builder_add (&markeddays, "i", dmday);
+ }
+
+ // If the appointment time is less than the selected date,
+ // don't create an appointment item for it.
+ if (vtype == E_CAL_COMPONENT_EVENT) {
+ if (ci->start < start_time_appointments) continue;
+ } else if (vtype == E_CAL_COMPONENT_TODO) {
+ if (ci->end < start_time_appointments) continue;
+ }
+
+ if (i >= 5) continue;
+ i++;
+
+ if (cached_appointment == NULL) {
+ g_debug("Create menu item");
+
+ item = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set (item, DBUSMENU_MENUITEM_PROP_TYPE, APPOINTMENT_MENUITEM_TYPE);
+
+ dbusmenu_menuitem_child_add_position (root, item, 2+i);
+ appointments = g_list_append (appointments, item); // Keep track of the items here to make them easy to remove
+ } else {
+ item = DBUSMENU_MENUITEM(cached_appointment->data);
+ cached_appointment = g_list_next(cached_appointment);
+
+ /* Remove the icon as we might not replace it on error */
+ dbusmenu_menuitem_property_remove(item, APPOINTMENT_MENUITEM_PROP_ICON);
+
+ /* Remove the activate handler */
+ g_signal_handlers_disconnect_matched(G_OBJECT(item), G_SIGNAL_MATCH_FUNC, 0, 0, NULL, G_CALLBACK(activate_cb), NULL);
+ }
+
+ dbusmenu_menuitem_property_set_bool (item, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
+ dbusmenu_menuitem_property_set_bool (item, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
+
+
+ // Label text
+ e_cal_component_get_summary (ecalcomp, &valuetext);
+ summary = g_strdup (valuetext.value);
+
+ dbusmenu_menuitem_property_set (item, APPOINTMENT_MENUITEM_PROP_LABEL, summary);
+ g_debug("Summary: %s", summary);
+ g_free (summary);
+
+ gboolean full_day = FALSE;
+ if (vtype == E_CAL_COMPONENT_EVENT) {
+ time_t start = ci->start;
+ if (time_add_day(start, 1) == ci->end) {
+ full_day = TRUE;
+ }
+ }
+
+ // Due text
+ if (full_day) {
+ struct tm fulldaytime = {0};
+ gmtime_r(&ci->start, &fulldaytime);
+
+ /* TRANSLATORS: This is a strftime string for the day for full day events
+ in the menu. It should most likely be either '%A' for a full text day
+ (Wednesday) or '%a' for a shortened one (Wed). You should only need to
+ change for '%a' in the case of langauges with very long day names. */
+ strftime(right, 20, _("%A"), &fulldaytime);
+ } else {
+ if (apt_output == SETTINGS_TIME_12_HOUR) {
+ if ((mday == dmday) && (mon == dmon) && (year == dyear))
+ strftime(right, 20, _(DEFAULT_TIME_12_FORMAT), due);
+ else
+ strftime(right, 20, _(DEFAULT_TIME_12_FORMAT_WITH_DAY), due);
+ } else if (apt_output == SETTINGS_TIME_24_HOUR) {
+ if ((mday == dmday) && (mon == dmon) && (year == dyear))
+ strftime(right, 20, _(DEFAULT_TIME_24_FORMAT), due);
+ else
+ strftime(right, 20, _(DEFAULT_TIME_24_FORMAT_WITH_DAY), due);
+ }
+ }
+ g_debug("Appointment time: %s, for date %s", right, asctime(due));
+ dbusmenu_menuitem_property_set (item, APPOINTMENT_MENUITEM_PROP_RIGHT, right);
+
+ // Now we pull out the URI for the calendar event and try to create a URI that'll work when we execute evolution
+ // FIXME Because the URI stuff is really broken, we're going to open the calendar at todays date instead
+ //e_cal_component_get_uid(ecalcomp, &uri);
+ ad = isodate_from_time_t(mktime(due));
+ cmd = g_strconcat("evolution calendar:///?startdate=", ad, NULL);
+ g_signal_connect (G_OBJECT(item), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED,
+ G_CALLBACK (activate_cb), cmd);
+
+ g_debug("Command to Execute: %s", cmd);
+
+ const gchar *color_spec = e_source_peek_color_spec(ci->source);
+ g_debug("Colour to use: %s", color_spec);
+
+ // Draw the correct icon for the appointment type and then tint it using mask fill.
+ // For now we'll create a circle
+ if (color_spec != NULL) {
+ GdkColor color;
+ gdk_color_parse (color_spec, &color);
+ g_debug("Creating a cairo surface: size, %d by %d", width, height);
+ cairo_surface_t *surface = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, width, height );
+
+ cairo_t *cr = cairo_create(surface);
+ gdk_cairo_set_source_color(cr, &color);
+ cairo_paint(cr);
+ cairo_set_source_rgba(cr, 0,0,0,0.5);
+ cairo_set_line_width(cr, 1);
+ cairo_rectangle (cr, 0.5, 0.5, width-1, height-1);
+ cairo_stroke(cr);
+ // Convert to pixbuf, in gtk3 this is done with gdk_pixbuf_get_from_surface
+ cairo_content_t content = cairo_surface_get_content (surface) | CAIRO_CONTENT_COLOR;
+ GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
+ !!(content & CAIRO_CONTENT_ALPHA),
+ 8, width, height);
+ if (pixbuf != NULL) {
+ gint sstride = cairo_image_surface_get_stride( surface );
+ gint dstride = gdk_pixbuf_get_rowstride (pixbuf);
+ guchar *spixels = cairo_image_surface_get_data( surface );
+ guchar *dpixels = gdk_pixbuf_get_pixels (pixbuf);
+
+ int x, y;
+ for (y = 0; y < height; y++) {
+ guint32 *src = (guint32 *) spixels;
+
+ for (x = 0; x < width; x++) {
+ guint alpha = src[x] >> 24;
+
+ if (alpha == 0) {
+ dpixels[x * 4 + 0] = 0;
+ dpixels[x * 4 + 1] = 0;
+ dpixels[x * 4 + 2] = 0;
+ } else {
+ dpixels[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
+ dpixels[x * 4 + 1] = (((src[x] & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha;
+ dpixels[x * 4 + 2] = (((src[x] & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha;
+ }
+ dpixels[x * 4 + 3] = alpha;
+ }
+ spixels += sstride;
+ dpixels += dstride;
+ }
+
+ dbusmenu_menuitem_property_set_image (item, APPOINTMENT_MENUITEM_PROP_ICON, pixbuf);
+ } else {
+ g_debug("Creating pixbuf from surface failed");
+ }
+ cairo_surface_destroy (surface);
+ cairo_destroy(cr);
+ }
+ g_debug("Adding appointment: %p", item);
+ }
+
+ if (gerror != NULL) g_error_free(gerror);
+ for (l = sorted_comp_instances; l; l = l->next) {
+ const struct comp_instance *ci = l->data;
+ g_object_unref(ci->comp);
+ }
+ g_list_free(sorted_comp_instances);
+
+ GVariant * marks = g_variant_builder_end (&markeddays);
+ dbusmenu_menuitem_property_set_variant (calendar, CALENDAR_MENUITEM_PROP_MARKS, marks);
+
+ updating_appointments = FALSE;
+ g_debug("End of objects");
+ return TRUE;
+}
+
+/* Looks for the time and date admin application and enables the
+ item we have one */
+static gboolean
+check_for_timeadmin (gpointer user_data)
+{
+ g_return_val_if_fail (settings != NULL, FALSE);
+
+ gchar * timeadmin = g_find_program_in_path("indicator-datetime-preferences");
+ if (timeadmin != NULL) {
+ g_debug("Found the indicator-datetime-preferences application: %s", timeadmin);
+ dbusmenu_menuitem_property_set_bool(settings, DBUSMENU_MENUITEM_PROP_ENABLED, TRUE);
+ g_free(timeadmin);
+ } else {
+ g_debug("Unable to find indicator-datetime-preferences app.");
+ dbusmenu_menuitem_property_set_bool(settings, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
+ }
+
+ return FALSE;
+}
+
+static void
+show_locations_changed (void)
+{
+ /* Re-calculate */
+ check_timezone_sync();
+}
+
+static void
+time_format_changed (void)
+{
+ update_appointment_menu_items(NULL);
+}
+
+/* Does the work to build the default menu, really calls out
+ to other functions but this is the core to clean up the
+ main function. */
+static void
+build_menus (DbusmenuMenuitem * root)
+{
+ g_debug("Building Menus.");
+ if (date == NULL) {
+ date = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set (date, DBUSMENU_MENUITEM_PROP_LABEL, _("No date yet..."));
+ dbusmenu_menuitem_property_set_bool(date, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
+ dbusmenu_menuitem_child_append(root, date);
+
+ g_idle_add(update_datetime, NULL);
+ }
+
+ if (calendar == NULL) {
+ calendar = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set (calendar, DBUSMENU_MENUITEM_PROP_TYPE, DBUSMENU_CALENDAR_MENUITEM_TYPE);
+ /* insensitive until we check for available apps */
+ dbusmenu_menuitem_property_set_bool(calendar, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
+ g_signal_connect (G_OBJECT(calendar), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED,
+ G_CALLBACK (activate_cb), "evolution -c calendar");
+ dbusmenu_menuitem_child_append(root, calendar);
+
+ g_idle_add(check_for_calendar, NULL);
+ }
+
+ locations_separator = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set(locations_separator, DBUSMENU_MENUITEM_PROP_TYPE, DBUSMENU_CLIENT_TYPES_SEPARATOR);
+ dbusmenu_menuitem_property_set_bool (locations_separator, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_child_append(root, locations_separator);
+
+ geo_location = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set (geo_location, DBUSMENU_MENUITEM_PROP_TYPE, TIMEZONE_MENUITEM_TYPE);
+ set_current_timezone_label (geo_location, "");
+ dbusmenu_menuitem_property_set_bool (geo_location, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
+ dbusmenu_menuitem_property_set_bool (geo_location, DBUSMENU_MENUITEM_PROP_VISIBLE, TRUE);
+ g_signal_connect(G_OBJECT(geo_location), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(quick_set_tz), NULL);
+ dbusmenu_menuitem_child_append(root, geo_location);
+
+ current_location = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set (current_location, DBUSMENU_MENUITEM_PROP_TYPE, TIMEZONE_MENUITEM_TYPE);
+ set_current_timezone_label (current_location, "");
+ dbusmenu_menuitem_property_set_bool (current_location, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
+ dbusmenu_menuitem_property_set_bool (current_location, DBUSMENU_MENUITEM_PROP_VISIBLE, FALSE);
+ dbusmenu_menuitem_child_append(root, current_location);
+
+ check_timezone_sync();
+
+ g_signal_connect (conf, "changed::" SETTINGS_SHOW_LOCATIONS_S, G_CALLBACK (show_locations_changed), NULL);
+ g_signal_connect (conf, "changed::" SETTINGS_LOCATIONS_S, G_CALLBACK (show_locations_changed), NULL);
+ g_signal_connect (conf, "changed::" SETTINGS_SHOW_EVENTS_S, G_CALLBACK (show_events_changed), NULL);
+ g_signal_connect (conf, "changed::" SETTINGS_TIME_FORMAT_S, G_CALLBACK (time_format_changed), NULL);
+
+ DbusmenuMenuitem * separator = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set(separator, DBUSMENU_MENUITEM_PROP_TYPE, DBUSMENU_CLIENT_TYPES_SEPARATOR);
+ dbusmenu_menuitem_child_append(root, separator);
+
+ settings = dbusmenu_menuitem_new();
+ dbusmenu_menuitem_property_set (settings, DBUSMENU_MENUITEM_PROP_LABEL, _("Time & Date Settings..."));
+ /* insensitive until we check for available apps */
+ dbusmenu_menuitem_property_set_bool(settings, DBUSMENU_MENUITEM_PROP_ENABLED, FALSE);
+ g_signal_connect(G_OBJECT(settings), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(activate_cb), "indicator-datetime-preferences");
+ dbusmenu_menuitem_child_append(root, settings);
+ g_idle_add(check_for_timeadmin, NULL);
+
+ return;
+}
+
+/* Run when the timezone file changes */
+static void
+timezone_changed (GFileMonitor * monitor, GFile * file, GFile * otherfile, GFileMonitorEvent event, gpointer user_data)
+{
+ update_current_timezone();
+ datetime_interface_update(DATETIME_INTERFACE(user_data));
+ update_datetime(NULL);
+ setup_timer();
+ return;
+}
+
+/* Set up monitoring the timezone file */
+static void
+build_timezone (DatetimeInterface * dbus)
+{
+ GFile * timezonefile = g_file_new_for_path(TIMEZONE_FILE);
+ GFileMonitor * monitor = g_file_monitor_file(timezonefile, G_FILE_MONITOR_NONE, NULL, NULL);
+ if (monitor != NULL) {
+ g_signal_connect(G_OBJECT(monitor), "changed", G_CALLBACK(timezone_changed), dbus);
+ g_debug("Monitoring timezone file: '" TIMEZONE_FILE "'");
+ } else {
+ g_warning("Unable to monitor timezone file: '" TIMEZONE_FILE "'");
+ }
+ return;
+}
+
+/* Source ID for the timer */
+static guint timer = 0;
+
+/* Execute at a given time, update and setup a new
+ timer to go again. */
+static gboolean
+timer_func (gpointer user_data)
+{
+ timer = 0;
+ /* Reset up each time to reduce error */
+ setup_timer();
+ update_datetime(NULL);
+ return FALSE;
+}
+
+/* Sets up the time to launch the timer to update the
+ date in the datetime entry */
+static void
+setup_timer (void)
+{
+ if (timer != 0) {
+ g_source_remove(timer);
+ timer = 0;
+ }
+
+ time_t t;
+ t = time(NULL);
+ struct tm * ltime = localtime(&t);
+
+ timer = g_timeout_add_seconds(((23 - ltime->tm_hour) * 60 * 60) +
+ ((59 - ltime->tm_min) * 60) +
+ ((60 - ltime->tm_sec)) + 60 /* one minute past */,
+ timer_func, NULL);
+
+ return;
+}
+
+static void
+session_active_change_cb (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name,
+ GVariant * parameters, gpointer user_data)
+{
+ // Just returned from suspend
+ if (g_strcmp0(signal_name, "SystemIdleHintChanged") == 0) {
+ gboolean idle = FALSE;
+ g_variant_get(parameters, "(b)", &idle);
+ if (!idle) {
+ datetime_interface_update(DATETIME_INTERFACE(user_data));
+ update_datetime(NULL);
+ setup_timer();
+ }
+ }
+ return;
+}
+
+/* for hooking into console kit signal on wake from suspend */
+static void
+system_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data)
+{
+ GError * error = NULL;
+
+ GDBusProxy * proxy = g_dbus_proxy_new_for_bus_finish(res, &error);
+
+ if (error != NULL) {
+ g_warning("Could not grab DBus proxy for ConsoleKit: %s", error->message);
+ g_error_free(error);
+ return;
+ }
+
+ g_signal_connect(proxy, "g-signal", G_CALLBACK(session_active_change_cb), user_data);
+}
+
+/* Callback from getting the address */
+static void
+geo_address_cb (GeoclueAddress * address, int timestamp, GHashTable * addy_data, GeoclueAccuracy * accuracy, GError * error, gpointer user_data)
+{
+ if (error != NULL) {
+ g_warning("Unable to get Geoclue address: %s", error->message);
+ return;
+ }
+
+ g_debug("Geoclue timezone is: %s", (gchar *)g_hash_table_lookup(addy_data, "timezone"));
+
+ if (geo_timezone != NULL) {
+ g_free(geo_timezone);
+ geo_timezone = NULL;
+ }
+
+ gpointer tz_hash = g_hash_table_lookup(addy_data, "timezone");
+ if (tz_hash != NULL) {
+ geo_timezone = g_strdup((gchar *)tz_hash);
+ }
+
+ check_timezone_sync();
+
+ return;
+}
+
+/* Clean up the reference we kept to the address and make sure to
+ drop the signals incase someone else has one. */
+static void
+geo_address_clean (void)
+{
+ if (geo_address == NULL) {
+ return;
+ }
+
+ g_signal_handlers_disconnect_by_func(G_OBJECT(geo_address), geo_address_cb, NULL);
+ g_object_unref(G_OBJECT(geo_address));
+
+ geo_address = NULL;
+
+ return;
+}
+
+/* Clean up and remove all signal handlers from the client as we
+ unreference it as well. */
+static void
+geo_client_clean (void)
+{
+ if (geo_master == NULL) {
+ return;
+ }
+
+ g_signal_handlers_disconnect_by_func(G_OBJECT(geo_master), geo_client_invalid, NULL);
+ g_signal_handlers_disconnect_by_func(G_OBJECT(geo_master), geo_address_change, NULL);
+ g_object_unref(G_OBJECT(geo_master));
+
+ geo_master = NULL;
+
+ return;
+}
+
+/* Callback from creating the address */
+static void
+geo_create_address (GeoclueMasterClient * master, GeoclueAddress * address, GError * error, gpointer user_data)
+{
+ if (error != NULL) {
+ g_warning("Unable to create GeoClue address: %s", error->message);
+ return;
+ }
+
+ /* We shouldn't have created a new address if we already had one
+ so this is a warning. But, it really is only a mem-leak so we
+ don't need to error out. */
+ g_warn_if_fail(geo_address == NULL);
+ geo_address_clean();
+
+ g_debug("Created Geoclue Address");
+ geo_address = address;
+ g_object_ref(G_OBJECT(geo_address));
+
+ geoclue_address_get_address_async(geo_address, geo_address_cb, NULL);
+
+ g_signal_connect(G_OBJECT(address), "address-changed", G_CALLBACK(geo_address_cb), NULL);
+
+ return;
+}
+
+/* Callback from setting requirements */
+static void
+geo_req_set (GeoclueMasterClient * master, GError * error, gpointer user_data)
+{
+ if (error != NULL) {
+ g_warning("Unable to set Geoclue requirements: %s", error->message);
+ }
+ return;
+}
+
+/* Client is killing itself rather oddly */
+static void
+geo_client_invalid (GeoclueMasterClient * client, gpointer user_data)
+{
+ g_warning("Master client invalid, rebuilding.");
+
+ /* Client changes we can assume the address is now invalid so we
+ need to unreference the one we had. */
+ geo_address_clean();
+
+ /* And our master client is invalid */
+ geo_client_clean();
+
+ GeoclueMaster * master = geoclue_master_get_default();
+ geoclue_master_create_client_async(master, geo_create_client, NULL);
+
+ if (geo_timezone != NULL) {
+ g_free(geo_timezone);
+ geo_timezone = NULL;
+ }
+
+ check_timezone_sync();
+
+ return;
+}
+
+/* Address provider changed, we need to get that one */
+static void
+geo_address_change (GeoclueMasterClient * client, gchar * a, gchar * b, gchar * c, gchar * d, gpointer user_data)
+{
+ g_warning("Address provider changed. Let's change");
+
+ /* If the address is supposed to have changed we need to drop the old
+ address before starting to get the new one. */
+ geo_address_clean();
+
+ geoclue_master_client_create_address_async(geo_master, geo_create_address, NULL);
+
+ if (geo_timezone != NULL) {
+ g_free(geo_timezone);
+ geo_timezone = NULL;
+ }
+
+ check_timezone_sync();
+
+ return;
+}
+
+/* Callback from creating the client */
+static void
+geo_create_client (GeoclueMaster * master, GeoclueMasterClient * client, gchar * path, GError * error, gpointer user_data)
+{
+ g_debug("Created Geoclue client at: %s", path);
+
+ geo_master = client;
+
+ if (geo_master != NULL) {
+ g_warning(_("Unable to get a GeoClue client! Geolocation based timezone support will not be available."));
+ return;
+ }
+
+ g_object_ref(G_OBJECT(geo_master));
+
+ /* New client, make sure we don't have an address hanging on */
+ geo_address_clean();
+
+ geoclue_master_client_set_requirements_async(geo_master,
+ GEOCLUE_ACCURACY_LEVEL_REGION,
+ 0,
+ FALSE,
+ GEOCLUE_RESOURCE_ALL,
+ geo_req_set,
+ NULL);
+
+ geoclue_master_client_create_address_async(geo_master, geo_create_address, NULL);
+
+ g_signal_connect(G_OBJECT(client), "invalidated", G_CALLBACK(geo_client_invalid), NULL);
+ g_signal_connect(G_OBJECT(client), "address-provider-changed", G_CALLBACK(geo_address_change), NULL);
+
+ return;
+}
+
+/* Repsonds to the service object saying it's time to shutdown.
+ It stops the mainloop. */
+static void
+service_shutdown (IndicatorService * service, gpointer user_data)
+{
+ g_warning("Shutting down service!");
+ g_main_loop_quit(mainloop);
+ return;
+}
+
+/* Function to build everything up. Entry point from asm. */
+int
+main (int argc, char ** argv)
+{
+ g_type_init();
+
+ /* Acknowledging the service init and setting up the interface */
+ service = indicator_service_new_version(SERVICE_NAME, SERVICE_VERSION);
+ g_signal_connect(service, INDICATOR_SERVICE_SIGNAL_SHUTDOWN, G_CALLBACK(service_shutdown), NULL);
+
+ /* Setting up i18n and gettext. Apparently, we need
+ all of these. */
+ setlocale (LC_ALL, "");
+ bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
+ textdomain (GETTEXT_PACKAGE);
+
+ /* Set up GSettings */
+ conf = g_settings_new(SETTINGS_INTERFACE);
+ /* Set up gconf for getting evolution enabled calendars */
+ gconf = gconf_client_get_default();
+ // TODO Add a signal handler to catch gsettings changes and respond to them
+
+ /* Building the base menu */
+ server = dbusmenu_server_new(MENU_OBJ);
+ root = dbusmenu_menuitem_new();
+ dbusmenu_server_set_root(server, root);
+
+ build_menus(root);
+
+ /* Cache the timezone */
+ update_current_timezone();
+
+ /* Setup geoclue */
+ GeoclueMaster * master = geoclue_master_get_default();
+ geoclue_master_create_client_async(master, geo_create_client, NULL);
+
+ /* Setup dbus interface */
+ dbus = g_object_new(DATETIME_INTERFACE_TYPE, NULL);
+
+ /* Setup timezone watch */
+ build_timezone(dbus);
+
+ /* Setup the timer */
+ setup_timer();
+
+ /* And watch for system resumes */
+ g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
+ G_DBUS_PROXY_FLAGS_NONE,
+ NULL,
+ "org.freedesktop.ConsoleKit",
+ "/org/freedesktop/ConsoleKit/Manager",
+ "org.freedesktop.ConsoleKit.Manager",
+ NULL, system_proxy_cb, dbus);
+
+ mainloop = g_main_loop_new(NULL, FALSE);
+ g_main_loop_run(mainloop);
+
+ g_object_unref(G_OBJECT(conf));
+ g_object_unref(G_OBJECT(master));
+ g_object_unref(G_OBJECT(dbus));
+ g_object_unref(G_OBJECT(service));
+ g_object_unref(G_OBJECT(server));
+ g_object_unref(G_OBJECT(root));
+
+ geo_address_clean();
+ geo_client_clean();
+
+ return 0;
+}
diff --git a/src/datetime-service.xml b/src/datetime-service.xml
new file mode 100644
index 0000000..eda064f
--- /dev/null
+++ b/src/datetime-service.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<node name="/">
+ <interface name="com.canonical.indicator.datetime.service">
+
+<!-- Methods -->
+
+<!-- Signals -->
+ <signal name="UpdateTime" />
+
+ </interface>
+</node>
diff --git a/src/dbus-shared.h b/src/dbus-shared.h
new file mode 100644
index 0000000..9e3a781
--- /dev/null
+++ b/src/dbus-shared.h
@@ -0,0 +1,42 @@
+/*
+An indicator to show date and time information.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ Ted Gould <ted@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#define SERVICE_NAME "com.canonical.indicator.datetime"
+#define SERVICE_IFACE "com.canonical.indicator.datetime.service"
+#define SERVICE_OBJ "/com/canonical/indicator/datetime/service"
+#define SERVICE_VERSION 1
+
+#define MENU_OBJ "/com/canonical/indicator/datetime/menu"
+
+#define DBUSMENU_CALENDAR_MENUITEM_TYPE "x-canonical-calendar-item"
+
+#define CALENDAR_MENUITEM_PROP_MARKS "calendar-marks"
+
+#define APPOINTMENT_MENUITEM_TYPE "appointment-item"
+#define APPOINTMENT_MENUITEM_PROP_LABEL "appointment-label"
+#define APPOINTMENT_MENUITEM_PROP_ICON "appointment-icon"
+#define APPOINTMENT_MENUITEM_PROP_RIGHT "appointment-time"
+
+#define TIMEZONE_MENUITEM_TYPE "timezone-item"
+#define TIMEZONE_MENUITEM_PROP_ZONE "timezone-zone"
+#define TIMEZONE_MENUITEM_PROP_NAME "timezone-name"
+#define TIMEZONE_MENUITEM_PROP_RADIO "timezone-radio"
diff --git a/src/indicator-datetime.c b/src/indicator-datetime.c
new file mode 100644
index 0000000..eb4b695
--- /dev/null
+++ b/src/indicator-datetime.c
@@ -0,0 +1,1535 @@
+/*
+An indicator to time and date related information in the menubar.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ Ted Gould <ted@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <locale.h>
+#include <langinfo.h>
+#include <string.h>
+#include <time.h>
+
+/* GStuff */
+#include <glib.h>
+#include <glib/gprintf.h>
+#include <glib-object.h>
+#include <glib/gi18n-lib.h>
+#include <gio/gio.h>
+
+/* Indicator Stuff */
+#include <libindicator/indicator.h>
+#include <libindicator/indicator-object.h>
+#include <libindicator/indicator-service-manager.h>
+
+/* DBusMenu */
+#include <libdbusmenu-gtk/menu.h>
+#include <libido/libido.h>
+#include <libdbusmenu-gtk/menuitem.h>
+
+#include "utils.h"
+#include "dbus-shared.h"
+#include "settings-shared.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;
+
+ gchar * time_string;
+
+ gboolean show_clock;
+ gint time_mode;
+ gboolean show_seconds;
+ gboolean show_date;
+ gboolean show_day;
+ gchar * custom_string;
+ gboolean custom_show_seconds;
+
+ gboolean show_week_numbers;
+ gboolean show_calendar;
+ gint week_start;
+
+ guint idle_measure;
+ gint max_width;
+
+ IndicatorServiceManager * sm;
+ DbusmenuGtkMenu * menu;
+
+ GCancellable * service_proxy_cancel;
+ GDBusProxy * service_proxy;
+ IdoCalendarMenuItem *ido_calendar;
+
+ GList * timezone_items;
+
+ GSettings * settings;
+
+ GtkSizeGroup * indicator_right_group;
+};
+
+/* Enum for the properties so that they can be quickly
+ found and looked up. */
+enum {
+ PROP_0,
+ PROP_SHOW_CLOCK,
+ PROP_TIME_FORMAT,
+ PROP_SHOW_SECONDS,
+ PROP_SHOW_DAY,
+ PROP_SHOW_DATE,
+ PROP_CUSTOM_TIME_FORMAT,
+ PROP_SHOW_WEEK_NUMBERS,
+ PROP_SHOW_CALENDAR
+};
+
+typedef struct _indicator_item_t indicator_item_t;
+struct _indicator_item_t {
+ IndicatorDatetime * self;
+ DbusmenuMenuitem * mi;
+ GtkWidget * gmi;
+ GtkWidget * icon;
+ GtkWidget * label;
+ GtkWidget * right;
+};
+
+#define PROP_SHOW_CLOCK_S "show-clock"
+#define PROP_TIME_FORMAT_S "time-format"
+#define PROP_SHOW_SECONDS_S "show-seconds"
+#define PROP_SHOW_DAY_S "show-day"
+#define PROP_SHOW_DATE_S "show-date"
+#define PROP_CUSTOM_TIME_FORMAT_S "custom-time-format"
+#define PROP_SHOW_WEEK_NUMBERS_S "show-week-numbers"
+#define PROP_SHOW_CALENDAR_S "show-calendar"
+
+#define INDICATOR_DATETIME_GET_PRIVATE(o) \
+(G_TYPE_INSTANCE_GET_PRIVATE ((o), INDICATOR_DATETIME_TYPE, IndicatorDatetimePrivate))
+
+enum {
+ STRFTIME_MASK_NONE = 0, /* Hours or minutes as we always test those */
+ STRFTIME_MASK_SECONDS = 1 << 0, /* Seconds count */
+ STRFTIME_MASK_AMPM = 1 << 1, /* AM/PM counts */
+ STRFTIME_MASK_WEEK = 1 << 2, /* Day of the week maters (Sat, Sun, etc.) */
+ STRFTIME_MASK_DAY = 1 << 3, /* Day of the month counts (Feb 1st) */
+ STRFTIME_MASK_MONTH = 1 << 4, /* Which month matters */
+ STRFTIME_MASK_YEAR = 1 << 5, /* Which year matters */
+ /* Last entry, combines all previous */
+ STRFTIME_MASK_ALL = (STRFTIME_MASK_SECONDS | STRFTIME_MASK_AMPM | STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR)
+};
+
+GType indicator_datetime_get_type (void);
+
+static void indicator_datetime_class_init (IndicatorDatetimeClass *klass);
+static void indicator_datetime_init (IndicatorDatetime *self);
+static void set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
+static void get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
+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);
+static const gchar * get_accessible_desc (IndicatorObject * io);
+static GVariant * bind_enum_set (const GValue * value, const GVariantType * type, gpointer user_data);
+static gboolean bind_enum_get (GValue * value, GVariant * variant, gpointer user_data);
+static gchar * generate_format_string_now (IndicatorDatetime * self);
+static void update_label (IndicatorDatetime * io, GDateTime ** datetime);
+static void guess_label_size (IndicatorDatetime * self);
+static void setup_timer (IndicatorDatetime * self, GDateTime * datetime);
+static void update_time (IndicatorDatetime * self);
+static void receive_signal (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name, GVariant * parameters, gpointer user_data);
+static void service_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data);
+static gint generate_strftime_bitmask (const char *time_str);
+static void timezone_update_labels (indicator_item_t * mi_data);
+static gboolean new_calendar_item (DbusmenuMenuitem * newitem, DbusmenuMenuitem * parent, DbusmenuClient * client, gpointer user_data);
+static gboolean new_appointment_item (DbusmenuMenuitem * newitem, DbusmenuMenuitem * parent, DbusmenuClient * client, gpointer user_data);
+static gboolean new_timezone_item (DbusmenuMenuitem * newitem, DbusmenuMenuitem * parent, DbusmenuClient * client, gpointer user_data);
+
+/* 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;
+
+ object_class->set_property = set_property;
+ object_class->get_property = get_property;
+
+ IndicatorObjectClass * io_class = INDICATOR_OBJECT_CLASS(klass);
+
+ io_class->get_label = get_label;
+ io_class->get_menu = get_menu;
+ io_class->get_accessible_desc = get_accessible_desc;
+
+ g_object_class_install_property (object_class,
+ PROP_SHOW_CLOCK,
+ g_param_spec_boolean(PROP_SHOW_CLOCK_S,
+ "Whether to show the clock in the menu bar.",
+ "Shows indicator-datetime in the shell's menu bar.",
+ TRUE, /* default */
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class,
+ PROP_TIME_FORMAT,
+ g_param_spec_int(PROP_TIME_FORMAT_S,
+ "A choice of which format should be used on the panel",
+ "Chooses between letting the locale choose the time, 12-hour time, 24-time or using the custom string passed to strftime().",
+ SETTINGS_TIME_LOCALE, /* min */
+ SETTINGS_TIME_CUSTOM, /* max */
+ SETTINGS_TIME_LOCALE, /* default */
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class,
+ PROP_SHOW_SECONDS,
+ g_param_spec_boolean(PROP_SHOW_SECONDS_S,
+ "Whether to show seconds in the indicator.",
+ "Shows seconds along with the time in the indicator. Also effects refresh interval.",
+ FALSE, /* default */
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class,
+ PROP_SHOW_DAY,
+ g_param_spec_boolean(PROP_SHOW_DAY_S,
+ "Whether to show the day of the week in the indicator.",
+ "Shows the day of the week along with the time in the indicator.",
+ FALSE, /* default */
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class,
+ PROP_SHOW_DATE,
+ g_param_spec_boolean(PROP_SHOW_DATE_S,
+ "Whether to show the day and month in the indicator.",
+ "Shows the day and month along with the time in the indicator.",
+ FALSE, /* default */
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class,
+ PROP_CUSTOM_TIME_FORMAT,
+ g_param_spec_string(PROP_CUSTOM_TIME_FORMAT_S,
+ "The format that is used to show the time on the panel.",
+ "A format string in the form used to pass to strftime to make a string for displaying on the panel.",
+ DEFAULT_TIME_FORMAT,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_property (object_class,
+ PROP_SHOW_WEEK_NUMBERS,
+ g_param_spec_boolean(PROP_SHOW_WEEK_NUMBERS_S,
+ "Whether to show the week numbers in the calendar.",
+ "Shows the week numbers in the monthly calendar in indicator-datetime's menu.",
+ FALSE, /* default */
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class,
+ PROP_SHOW_CALENDAR,
+ g_param_spec_boolean(PROP_SHOW_CALENDAR_S,
+ "Whether to show the calendar.",
+ "Shows the monthly calendar in indicator-datetime's menu.",
+ TRUE, /* default */
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ return;
+}
+
+static void
+menu_visible_notfy_cb(GtkWidget * menu, G_GNUC_UNUSED GParamSpec *pspec, gpointer user_data)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
+ g_debug("notify visible signal recieved");
+
+ // we should only react if we're currently visible
+ gboolean visible;
+ g_object_get(G_OBJECT(menu), "visible", &visible, NULL);
+ if (visible) return;
+ g_debug("notify visible menu hidden, resetting date");
+
+ time_t curtime;
+
+ time(&curtime);
+ struct tm *today = localtime(&curtime);
+ int y = today->tm_year;
+ int m = today->tm_mon;
+ int d = today->tm_mday;
+
+ // Set the calendar to todays date
+ ido_calendar_menu_item_set_date (self->priv->ido_calendar, y+1900, m, d);
+
+ // Make sure the day-selected signal is sent so the menu updates - may duplicate
+ /*GVariant *variant = g_variant_new_uint32((guint)curtime);
+ guint timestamp = (guint)time(NULL);
+ dbusmenu_menuitem_handle_event(DBUSMENU_MENUITEM(self->priv->ido_calendar), "day-selected", variant, timestamp);*/
+}
+
+static void
+indicator_datetime_init (IndicatorDatetime *self)
+{
+ self->priv = INDICATOR_DATETIME_GET_PRIVATE(self);
+
+ self->priv->label = NULL;
+ self->priv->timer = 0;
+
+ self->priv->idle_measure = 0;
+ self->priv->max_width = 0;
+
+ self->priv->show_clock = TRUE;
+ self->priv->time_mode = SETTINGS_TIME_LOCALE;
+ self->priv->show_seconds = FALSE;
+ self->priv->show_date = FALSE;
+ self->priv->show_day = FALSE;
+ self->priv->custom_string = g_strdup(DEFAULT_TIME_FORMAT);
+ self->priv->custom_show_seconds = FALSE;
+
+ self->priv->time_string = generate_format_string_now(self);
+
+ self->priv->service_proxy = NULL;
+
+ self->priv->sm = NULL;
+ self->priv->menu = NULL;
+
+ self->priv->settings = g_settings_new(SETTINGS_INTERFACE);
+ if (self->priv->settings != NULL) {
+ g_settings_bind(self->priv->settings,
+ SETTINGS_SHOW_CLOCK_S,
+ self,
+ PROP_SHOW_CLOCK_S,
+ G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind_with_mapping(self->priv->settings,
+ SETTINGS_TIME_FORMAT_S,
+ self,
+ PROP_TIME_FORMAT_S,
+ G_SETTINGS_BIND_DEFAULT,
+ bind_enum_get,
+ bind_enum_set,
+ NULL, NULL); /* Userdata and destroy func */
+ g_settings_bind(self->priv->settings,
+ SETTINGS_SHOW_SECONDS_S,
+ self,
+ PROP_SHOW_SECONDS_S,
+ G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind(self->priv->settings,
+ SETTINGS_SHOW_DAY_S,
+ self,
+ PROP_SHOW_DAY_S,
+ G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind(self->priv->settings,
+ SETTINGS_SHOW_DATE_S,
+ self,
+ PROP_SHOW_DATE_S,
+ G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind(self->priv->settings,
+ SETTINGS_CUSTOM_TIME_FORMAT_S,
+ self,
+ PROP_CUSTOM_TIME_FORMAT_S,
+ G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind(self->priv->settings,
+ SETTINGS_SHOW_WEEK_NUMBERS_S,
+ self,
+ PROP_SHOW_WEEK_NUMBERS_S,
+ G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind(self->priv->settings,
+ SETTINGS_SHOW_CALENDAR_S,
+ self,
+ PROP_SHOW_CALENDAR_S,
+ G_SETTINGS_BIND_DEFAULT);
+ } else {
+ g_warning("Unable to get settings for '" SETTINGS_INTERFACE "'");
+ }
+
+ self->priv->sm = indicator_service_manager_new_version(SERVICE_NAME, SERVICE_VERSION);
+ self->priv->indicator_right_group = GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
+
+ self->priv->menu = dbusmenu_gtkmenu_new(SERVICE_NAME, MENU_OBJ);
+
+ g_signal_connect(self->priv->menu, "notify::visible", G_CALLBACK(menu_visible_notfy_cb), self);
+
+ DbusmenuGtkClient *client = dbusmenu_gtkmenu_get_client(self->priv->menu);
+ dbusmenu_client_add_type_handler_full(DBUSMENU_CLIENT(client), DBUSMENU_CALENDAR_MENUITEM_TYPE, new_calendar_item, self, NULL);
+ dbusmenu_client_add_type_handler_full(DBUSMENU_CLIENT(client), APPOINTMENT_MENUITEM_TYPE, new_appointment_item, self, NULL);
+ dbusmenu_client_add_type_handler_full(DBUSMENU_CLIENT(client), TIMEZONE_MENUITEM_TYPE, new_timezone_item, self, NULL);
+
+ self->priv->service_proxy_cancel = g_cancellable_new();
+
+ g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
+ G_DBUS_PROXY_FLAGS_NONE,
+ NULL,
+ SERVICE_NAME,
+ SERVICE_OBJ,
+ SERVICE_IFACE,
+ self->priv->service_proxy_cancel,
+ service_proxy_cb,
+ self);
+
+ return;
+}
+
+/* Callback from trying to create the proxy for the serivce, this
+ could include starting the service. Sometime it'll fail and
+ we'll try to start that dang service again! */
+static void
+service_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data)
+{
+ GError * error = NULL;
+
+ IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
+ g_return_if_fail(self != NULL);
+
+ GDBusProxy * proxy = g_dbus_proxy_new_for_bus_finish(res, &error);
+
+ IndicatorDatetimePrivate * priv = INDICATOR_DATETIME_GET_PRIVATE(self);
+
+ if (priv->service_proxy_cancel != NULL) {
+ g_object_unref(priv->service_proxy_cancel);
+ priv->service_proxy_cancel = NULL;
+ }
+
+ if (error != NULL) {
+ g_warning("Could not grab DBus proxy for %s: %s", SERVICE_NAME, error->message);
+ g_error_free(error);
+ return;
+ }
+
+ /* Okay, we're good to grab the proxy at this point, we're
+ sure that it's ours. */
+ priv->service_proxy = proxy;
+
+ g_signal_connect(proxy, "g-signal", G_CALLBACK(receive_signal), self);
+
+ 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;
+ }
+
+ if (self->priv->idle_measure != 0) {
+ g_source_remove(self->priv->idle_measure);
+ self->priv->idle_measure = 0;
+ }
+
+ if (self->priv->menu != NULL) {
+ g_object_unref(G_OBJECT(self->priv->menu));
+ self->priv->menu = NULL;
+ }
+
+ if (self->priv->sm != NULL) {
+ g_object_unref(G_OBJECT(self->priv->sm));
+ self->priv->sm = NULL;
+ }
+
+ if (self->priv->settings != NULL) {
+ g_object_unref(G_OBJECT(self->priv->settings));
+ self->priv->settings = NULL;
+ }
+
+ if (self->priv->service_proxy != NULL) {
+ g_object_unref(self->priv->service_proxy);
+ self->priv->service_proxy = NULL;
+ }
+
+ if (self->priv->indicator_right_group != NULL) {
+ g_object_unref(G_OBJECT(self->priv->indicator_right_group));
+ self->priv->indicator_right_group = NULL;
+ }
+
+ G_OBJECT_CLASS (indicator_datetime_parent_class)->dispose (object);
+ return;
+}
+
+static void
+indicator_datetime_finalize (GObject *object)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(object);
+
+ if (self->priv->time_string != NULL) {
+ g_free(self->priv->time_string);
+ self->priv->time_string = NULL;
+ }
+
+ if (self->priv->custom_string != NULL) {
+ g_free(self->priv->custom_string);
+ self->priv->custom_string = NULL;
+ }
+
+ G_OBJECT_CLASS (indicator_datetime_parent_class)->finalize (object);
+ return;
+}
+
+/* Turns the int value into a string GVariant */
+static GVariant *
+bind_enum_set (const GValue * value, const GVariantType * type, gpointer user_data)
+{
+ switch (g_value_get_int(value)) {
+ case SETTINGS_TIME_LOCALE:
+ return g_variant_new_string("locale-default");
+ case SETTINGS_TIME_12_HOUR:
+ return g_variant_new_string("12-hour");
+ case SETTINGS_TIME_24_HOUR:
+ return g_variant_new_string("24-hour");
+ case SETTINGS_TIME_CUSTOM:
+ return g_variant_new_string("custom");
+ default:
+ return NULL;
+ }
+}
+
+/* Turns a string GVariant into an int value */
+static gboolean
+bind_enum_get (GValue * value, GVariant * variant, gpointer user_data)
+{
+ const gchar * str = g_variant_get_string(variant, NULL);
+ gint output = 0;
+
+ if (g_strcmp0(str, "locale-default") == 0) {
+ output = SETTINGS_TIME_LOCALE;
+ } else if (g_strcmp0(str, "12-hour") == 0) {
+ output = SETTINGS_TIME_12_HOUR;
+ } else if (g_strcmp0(str, "24-hour") == 0) {
+ output = SETTINGS_TIME_24_HOUR;
+ } else if (g_strcmp0(str, "custom") == 0) {
+ output = SETTINGS_TIME_CUSTOM;
+ } else {
+ return FALSE;
+ }
+
+ g_value_set_int(value, output);
+ return TRUE;
+}
+
+static void
+timezone_update_all_labels (IndicatorDatetime * self)
+{
+ IndicatorDatetimePrivate *priv = INDICATOR_DATETIME_GET_PRIVATE(self);
+ g_list_foreach(priv->timezone_items, (GFunc)timezone_update_labels, NULL);
+}
+
+/* Sets a property on the object */
+static void
+set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(object);
+ gboolean update = FALSE;
+
+ switch(prop_id) {
+ case PROP_SHOW_CLOCK: {
+ if (g_value_get_boolean(value) != self->priv->show_clock) {
+ self->priv->show_clock = g_value_get_boolean(value);
+ if (self->priv->label != NULL) {
+ gtk_widget_set_visible (GTK_WIDGET (self->priv->label), self->priv->show_clock);
+ }
+ }
+ break;
+ }
+ case PROP_TIME_FORMAT: {
+ gint newval = g_value_get_int(value);
+ if (newval != self->priv->time_mode) {
+ update = TRUE;
+ self->priv->time_mode = newval;
+ setup_timer(self, NULL);
+ }
+ break;
+ }
+ case PROP_SHOW_SECONDS: {
+ if (g_value_get_boolean(value) != self->priv->show_seconds) {
+ self->priv->show_seconds = !self->priv->show_seconds;
+ if (self->priv->time_mode != SETTINGS_TIME_CUSTOM) {
+ update = TRUE;
+ setup_timer(self, NULL);
+ }
+ }
+ break;
+ }
+ case PROP_SHOW_DAY: {
+ if (g_value_get_boolean(value) != self->priv->show_day) {
+ self->priv->show_day = !self->priv->show_day;
+ if (self->priv->time_mode != SETTINGS_TIME_CUSTOM) {
+ update = TRUE;
+ }
+ }
+ break;
+ }
+ case PROP_SHOW_DATE: {
+ if (g_value_get_boolean(value) != self->priv->show_date) {
+ self->priv->show_date = !self->priv->show_date;
+ if (self->priv->time_mode != SETTINGS_TIME_CUSTOM) {
+ update = TRUE;
+ }
+ }
+ break;
+ }
+ case PROP_CUSTOM_TIME_FORMAT: {
+ const gchar * newstr = g_value_get_string(value);
+ if (g_strcmp0(newstr, self->priv->custom_string) != 0) {
+ if (self->priv->custom_string != NULL) {
+ g_free(self->priv->custom_string);
+ self->priv->custom_string = NULL;
+ }
+ self->priv->custom_string = g_strdup(newstr);
+ gint time_mask = generate_strftime_bitmask(newstr);
+ self->priv->custom_show_seconds = (time_mask & STRFTIME_MASK_SECONDS);
+ if (self->priv->time_mode == SETTINGS_TIME_CUSTOM) {
+ update = TRUE;
+ setup_timer(self, NULL);
+ }
+ }
+ break;
+ }
+ case PROP_SHOW_WEEK_NUMBERS: {
+ if (g_value_get_boolean(value) != self->priv->show_week_numbers) {
+ GtkCalendarDisplayOptions flags = ido_calendar_menu_item_get_display_options (self->priv->ido_calendar);
+ if (g_value_get_boolean(value) == TRUE)
+ flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
+ else
+ flags &= ~GTK_CALENDAR_SHOW_WEEK_NUMBERS;
+ ido_calendar_menu_item_set_display_options (self->priv->ido_calendar, flags);
+ self->priv->show_week_numbers = g_value_get_boolean(value);
+ }
+ break;
+ }
+ case PROP_SHOW_CALENDAR: {
+ if (g_value_get_boolean(value) != self->priv->show_calendar) {
+ self->priv->show_calendar = g_value_get_boolean(value);
+ if (self->priv->ido_calendar != NULL) {
+ gtk_widget_set_visible (GTK_WIDGET (self->priv->ido_calendar), self->priv->show_calendar);
+ }
+ }
+ break;
+ }
+ default: {
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ return;
+ }
+ }
+
+ if (!update) {
+ return;
+ }
+
+ /* Get the new format string */
+ gchar * newformat = generate_format_string_now(self);
+
+ /* check to ensure the format really changed */
+ if (g_strcmp0(self->priv->time_string, newformat) == 0) {
+ g_free(newformat);
+ return;
+ }
+
+ /* Okay now process the change */
+ if (self->priv->time_string != NULL) {
+ g_free(self->priv->time_string);
+ self->priv->time_string = NULL;
+ }
+ self->priv->time_string = newformat;
+
+ /* And update everything */
+ update_label(self, NULL);
+ timezone_update_all_labels(self);
+ guess_label_size(self);
+
+ return;
+}
+
+/* Gets a property from the object */
+static void
+get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(object);
+
+ switch(prop_id) {
+ case PROP_SHOW_CLOCK:
+ g_value_set_boolean(value, self->priv->show_clock);
+ break;
+ case PROP_TIME_FORMAT:
+ g_value_set_int(value, self->priv->time_mode);
+ break;
+ case PROP_SHOW_SECONDS:
+ g_value_set_boolean(value, self->priv->show_seconds);
+ break;
+ case PROP_SHOW_DAY:
+ g_value_set_boolean(value, self->priv->show_day);
+ break;
+ case PROP_SHOW_DATE:
+ g_value_set_boolean(value, self->priv->show_date);
+ break;
+ case PROP_CUSTOM_TIME_FORMAT:
+ g_value_set_string(value, self->priv->custom_string);
+ break;
+ case PROP_SHOW_WEEK_NUMBERS:
+ g_value_set_boolean(value, self->priv->show_week_numbers);
+ break;
+ case PROP_SHOW_CALENDAR:
+ g_value_set_boolean(value, self->priv->show_calendar);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ return;
+ }
+
+ return;
+}
+
+/* Looks at the size of the label, if it grew beyond what we
+ thought was the max, make sure it doesn't shrink again. */
+static gboolean
+idle_measure (gpointer data)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(data);
+ self->priv->idle_measure = 0;
+
+ GtkAllocation allocation;
+ gtk_widget_get_allocation(GTK_WIDGET(self->priv->label), &allocation);
+
+ if (allocation.width > self->priv->max_width) {
+ if (self->priv->max_width != 0) {
+ g_warning("Guessed wrong. We thought the max would be %d but we're now at %d", self->priv->max_width, allocation.width);
+ }
+ self->priv->max_width = allocation.width;
+ gtk_widget_set_size_request(GTK_WIDGET(self->priv->label), self->priv->max_width, -1);
+ }
+
+ return FALSE;
+}
+
+/* Updates the accessible description */
+static void
+update_accessible_description (IndicatorDatetime * io)
+{
+ GList * entries = indicator_object_get_entries(INDICATOR_OBJECT(io));
+ IndicatorObjectEntry * entry = (IndicatorObjectEntry *)entries->data;
+
+ entry->accessible_desc = get_accessible_desc(INDICATOR_OBJECT(io));
+
+ g_signal_emit(G_OBJECT(io),
+ INDICATOR_OBJECT_SIGNAL_ACCESSIBLE_DESC_UPDATE_ID,
+ 0,
+ entry,
+ TRUE);
+
+ g_list_free(entries);
+
+ return;
+}
+
+/* Updates the label to be the current time. */
+static void
+set_label_to_time_in_zone (IndicatorDatetime * self, GtkLabel * label,
+ GTimeZone * tz, const gchar * format,
+ GDateTime ** datetime)
+{
+ GDateTime * datetime_now;
+ if (tz == NULL)
+ datetime_now = g_date_time_new_now_local();
+ else
+ datetime_now = g_date_time_new_now(tz);
+
+ gchar * timestr;
+ if (format == NULL) {
+ gchar * format_for_time = generate_format_string_at_time(datetime_now);
+ timestr = g_date_time_format(datetime_now, format_for_time);
+ g_free(format_for_time);
+ }
+ else {
+ timestr = g_date_time_format(datetime_now, format);
+ }
+
+ gboolean use_markup = FALSE;
+ if (pango_parse_markup(timestr, -1, 0, NULL, NULL, NULL, NULL))
+ use_markup = TRUE;
+
+ if (use_markup)
+ gtk_label_set_markup(label, timestr);
+ else
+ gtk_label_set_text(label, timestr);
+
+ g_free(timestr);
+
+ if (datetime)
+ *datetime = datetime_now;
+ else
+ g_date_time_unref(datetime_now);
+
+ return;
+}
+
+/* Updates the label to be the current time. */
+static void
+update_label (IndicatorDatetime * io, GDateTime ** datetime)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(io);
+
+ if (self->priv->label == NULL) return;
+
+ set_label_to_time_in_zone(self, self->priv->label, NULL, self->priv->time_string, datetime);
+
+ if (self->priv->idle_measure == 0) {
+ self->priv->idle_measure = g_idle_add(idle_measure, io);
+ }
+
+ update_accessible_description(io);
+
+ return;
+}
+
+/* Update the time right now. Usually the result of a timezone switch. */
+static void
+update_time (IndicatorDatetime * self)
+{
+ GDateTime * dt;
+ update_label(self, &dt);
+ timezone_update_all_labels(self);
+ setup_timer(self, dt);
+ g_date_time_unref(dt);
+ return;
+}
+
+/* Receives all signals from the service, routed to the appropriate functions */
+static void
+receive_signal (GDBusProxy * proxy, gchar * sender_name, gchar * signal_name,
+ GVariant * parameters, gpointer user_data)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
+
+ if (g_strcmp0(signal_name, "UpdateTime") == 0) {
+ update_time(self);
+ }
+
+ return;
+}
+
+/* Runs every minute and updates the time */
+gboolean
+timer_func (gpointer user_data)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
+ self->priv->timer = 0;
+ GDateTime * dt;
+ update_label(self, &dt);
+ timezone_update_all_labels(self);
+ setup_timer(self, dt);
+ g_date_time_unref(dt);
+ return FALSE;
+}
+
+/* Configure the timer to run the next time through */
+static void
+setup_timer (IndicatorDatetime * self, GDateTime * datetime)
+{
+ gboolean unref = FALSE;
+
+ if (self->priv->timer != 0) {
+ g_source_remove(self->priv->timer);
+ self->priv->timer = 0;
+ }
+
+ if (self->priv->show_seconds ||
+ (self->priv->time_mode == SETTINGS_TIME_CUSTOM && self->priv->custom_show_seconds)) {
+ self->priv->timer = g_timeout_add_full(G_PRIORITY_HIGH, 999, timer_func, self, NULL);
+ } else {
+ if (datetime == NULL) {
+ datetime = g_date_time_new_now_local();
+ unref = TRUE;
+ }
+
+ /* Plus 2 so we're just after the minute, don't want to be early. */
+ gint seconds = (gint)g_date_time_get_seconds(datetime);
+ self->priv->timer = g_timeout_add_seconds(60 - seconds + 2, timer_func, self);
+
+ if (unref) {
+ g_date_time_unref(datetime);
+ }
+ }
+
+ return;
+}
+
+/* Does a quick meausre of how big the string is in
+ pixels with a Pango layout */
+static gint
+measure_string (GtkStyle * style, PangoContext * context, const gchar * string)
+{
+ PangoLayout * layout = pango_layout_new(context);
+
+ if (pango_parse_markup(string, -1, 0, NULL, NULL, NULL, NULL))
+ pango_layout_set_markup(layout, string, -1);
+ else
+ pango_layout_set_text(layout, string, -1);
+
+ pango_layout_set_font_description(layout, style->font_desc);
+
+ gint width;
+ pango_layout_get_pixel_size(layout, &width, NULL);
+ g_object_unref(layout);
+ return width;
+}
+
+/* Format for the table of strftime() modifiers to what
+ we need to check when determining the length */
+typedef struct _strftime_type_t strftime_type_t;
+struct _strftime_type_t {
+ char character;
+ gint mask;
+};
+
+/* A table taken from the man page of strftime to what the different
+ characters can effect. These are worst case in that we need to
+ test the length based on all these things to ensure that we have
+ a reasonable string lenght measurement. */
+const static strftime_type_t strftime_type[] = {
+ {'a', STRFTIME_MASK_WEEK},
+ {'A', STRFTIME_MASK_WEEK},
+ {'b', STRFTIME_MASK_MONTH},
+ {'B', STRFTIME_MASK_MONTH},
+ {'c', STRFTIME_MASK_ALL}, /* We don't know, so we have to assume all */
+ {'C', STRFTIME_MASK_YEAR},
+ {'d', STRFTIME_MASK_MONTH},
+ {'D', STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR | STRFTIME_MASK_DAY},
+ {'e', STRFTIME_MASK_DAY},
+ {'F', STRFTIME_MASK_MONTH | STRFTIME_MASK_YEAR | STRFTIME_MASK_DAY},
+ {'G', STRFTIME_MASK_YEAR},
+ {'g', STRFTIME_MASK_YEAR},
+ {'h', STRFTIME_MASK_MONTH},
+ {'j', STRFTIME_MASK_DAY},
+ {'m', STRFTIME_MASK_MONTH},
+ {'p', STRFTIME_MASK_AMPM},
+ {'P', STRFTIME_MASK_AMPM},
+ {'r', STRFTIME_MASK_AMPM},
+ {'s', STRFTIME_MASK_SECONDS},
+ {'S', STRFTIME_MASK_SECONDS},
+ {'T', STRFTIME_MASK_SECONDS},
+ {'u', STRFTIME_MASK_WEEK},
+ {'U', STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH},
+ {'V', STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH},
+ {'w', STRFTIME_MASK_DAY},
+ {'W', STRFTIME_MASK_DAY | STRFTIME_MASK_MONTH},
+ {'x', STRFTIME_MASK_YEAR | STRFTIME_MASK_MONTH | STRFTIME_MASK_DAY | STRFTIME_MASK_WEEK},
+ {'X', STRFTIME_MASK_SECONDS},
+ {'y', STRFTIME_MASK_YEAR},
+ {'Y', STRFTIME_MASK_YEAR},
+ /* Last one */
+ {0, 0}
+};
+
+#define FAT_NUMBER 8
+
+/* Looks through the characters in the format string to
+ ensure that we can figure out which of the things we
+ need to check in determining the length. */
+static gint
+generate_strftime_bitmask (const char *time_str)
+{
+ gint retval = 0;
+ glong strlength = g_utf8_strlen(time_str, -1);
+ gint i;
+ g_debug("Evaluating bitmask for '%s'", time_str);
+
+ for (i = 0; i < strlength; i++) {
+ if (time_str[i] == '%' && i + 1 < strlength) {
+ gchar evalchar = time_str[i + 1];
+
+ /* If we're using alternate formats we need to skip those characters */
+ if (evalchar == 'E' || evalchar == 'O') {
+ if (i + 2 < strlength) {
+ evalchar = time_str[i + 2];
+ } else {
+ continue;
+ }
+ }
+
+ /* Let's look at that character in the table */
+ int j;
+ for (j = 0; strftime_type[j].character != 0; j++) {
+ if (strftime_type[j].character == evalchar) {
+ retval |= strftime_type[j].mask;
+ break;
+ }
+ }
+ }
+ }
+
+ return retval;
+}
+
+/* Build an array up of all the time values that we want to check
+ for length to ensure we're in a good place */
+static void
+build_timeval_array (GArray * timevals, gint mask)
+{
+ struct tm mytm = {0};
+
+ /* Sun 12/28/8888 00:00 */
+ mytm.tm_hour = 0;
+ mytm.tm_mday = 28;
+ mytm.tm_mon = 11;
+ mytm.tm_year = 8888 - 1900;
+ mytm.tm_wday = 0;
+ mytm.tm_yday = 363;
+ g_array_append_val(timevals, mytm);
+
+ if (mask & STRFTIME_MASK_AMPM) {
+ /* Sun 12/28/8888 12:00 */
+ mytm.tm_hour = 12;
+ g_array_append_val(timevals, mytm);
+ }
+
+ /* NOTE: Ignoring year 8888 should handle it */
+
+ if (mask & STRFTIME_MASK_MONTH) {
+ gint oldlen = timevals->len;
+ gint i, j;
+ for (i = 0; i < oldlen; i++) {
+ for (j = 0; j < 11; j++) {
+ struct tm localval = g_array_index(timevals, struct tm, i);
+ localval.tm_mon = j;
+ /* Not sure if I need to adjust yday & wday, hope not */
+ g_array_append_val(timevals, localval);
+ }
+ }
+ }
+
+ /* Doing these together as it seems like just slightly more
+ coverage on the numerical days, but worth it. */
+ if (mask & (STRFTIME_MASK_WEEK | STRFTIME_MASK_DAY)) {
+ gint oldlen = timevals->len;
+ gint i, j;
+ for (i = 0; i < oldlen; i++) {
+ for (j = 22; j < 28; j++) {
+ struct tm localval = g_array_index(timevals, struct tm, i);
+
+ gint diff = 28 - j;
+
+ localval.tm_mday = j;
+ localval.tm_wday = localval.tm_wday - diff;
+ if (localval.tm_wday < 0) {
+ localval.tm_wday += 7;
+ }
+ localval.tm_yday = localval.tm_yday - diff;
+
+ g_array_append_val(timevals, localval);
+ }
+ }
+ }
+
+ return;
+}
+
+/* Try to get a good guess at what a maximum width of the entire
+ string would be. */
+static void
+guess_label_size (IndicatorDatetime * self)
+{
+ /* This is during startup. */
+ if (self->priv->label == NULL) return;
+
+ GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(self->priv->label));
+ PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(self->priv->label));
+ gint * max_width = &(self->priv->max_width);
+ gint posibilitymask = generate_strftime_bitmask(self->priv->time_string);
+
+ /* Reset max width */
+ *max_width = 0;
+
+ /* Build the array of possibilities that we want to test */
+ GArray * timevals = g_array_new(FALSE, TRUE, sizeof(struct tm));
+ build_timeval_array(timevals, posibilitymask);
+
+ g_debug("Checking against %d posible times", timevals->len);
+ gint check_time;
+ for (check_time = 0; check_time < timevals->len; check_time++) {
+ gchar longstr[256];
+ strftime(longstr, 256, self->priv->time_string, &(g_array_index(timevals, struct tm, check_time)));
+
+ gchar * utf8 = g_locale_to_utf8(longstr, -1, NULL, NULL, NULL);
+ gint length = measure_string(style, context, utf8);
+ g_free(utf8);
+
+ if (length > *max_width) {
+ *max_width = length;
+ }
+ }
+
+ g_array_free(timevals, TRUE);
+
+ gtk_widget_set_size_request(GTK_WIDGET(self->priv->label), self->priv->max_width, -1);
+ g_debug("Guessing max time width: %d", self->priv->max_width);
+
+ return;
+}
+
+/* React to the style changing, which could mean an font
+ update. */
+static void
+style_changed (GtkWidget * widget, GtkStyle * oldstyle, gpointer data)
+{
+ g_debug("New style for time label");
+ IndicatorDatetime * self = INDICATOR_DATETIME(data);
+ guess_label_size(self);
+ update_label(self, NULL);
+ timezone_update_all_labels(self);
+ return;
+}
+
+/* Respond to changes in the screen to update the text gravity */
+static void
+update_text_gravity (GtkWidget *widget, GdkScreen *previous_screen, gpointer data)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(data);
+ if (self->priv->label == NULL) return;
+
+ PangoLayout *layout;
+ PangoContext *context;
+
+ layout = gtk_label_get_layout (GTK_LABEL(self->priv->label));
+ context = pango_layout_get_context(layout);
+ pango_context_set_base_gravity(context, PANGO_GRAVITY_AUTO);
+}
+
+static gchar *
+generate_format_string_now (IndicatorDatetime * self)
+{
+ if (self->priv->time_mode == SETTINGS_TIME_CUSTOM) {
+ return g_strdup(self->priv->custom_string);
+ }
+ else {
+ return generate_format_string_full(self->priv->show_day,
+ self->priv->show_date);
+ }
+}
+
+static void
+timezone_update_labels (indicator_item_t * mi_data)
+{
+ const gchar * zone = dbusmenu_menuitem_property_get(mi_data->mi, TIMEZONE_MENUITEM_PROP_ZONE);
+ const gchar * name = dbusmenu_menuitem_property_get(mi_data->mi, TIMEZONE_MENUITEM_PROP_NAME);
+
+ gtk_label_set_text(GTK_LABEL(mi_data->label), name);
+
+ /* Show current time in that zone on the right */
+ GTimeZone * tz = g_time_zone_new(zone);
+ set_label_to_time_in_zone(mi_data->self, GTK_LABEL(mi_data->right), tz, NULL, NULL);
+ g_time_zone_unref(tz);
+}
+
+/* Whenever we have a property change on a DbusmenuMenuitem
+ we need to be responsive to that. */
+static void
+indicator_prop_change_cb (DbusmenuMenuitem * mi, gchar * prop, GVariant *value, indicator_item_t * mi_data)
+{
+ if (!g_strcmp0(prop, APPOINTMENT_MENUITEM_PROP_LABEL)) {
+ /* Set the main label */
+ gtk_label_set_text(GTK_LABEL(mi_data->label), g_variant_get_string(value, NULL));
+ } else if (!g_strcmp0(prop, APPOINTMENT_MENUITEM_PROP_RIGHT)) {
+ /* Set the right label */
+ gtk_label_set_text(GTK_LABEL(mi_data->right), g_variant_get_string(value, NULL));
+ } else if (!g_strcmp0(prop, APPOINTMENT_MENUITEM_PROP_ICON)) {
+ /* We don't use the value here, which is probably less efficient,
+ but it's easier to use the easy function. And since th value
+ is already cached, shouldn't be a big deal really. */
+ GdkPixbuf * pixbuf = dbusmenu_menuitem_property_get_image(mi, APPOINTMENT_MENUITEM_PROP_ICON);
+ if (pixbuf != NULL) {
+ /* If we've got a pixbuf we need to make sure it's of a reasonable
+ size to fit in the menu. If not, rescale it. */
+ GdkPixbuf * resized_pixbuf;
+ gint width, height;
+ gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height);
+ if (gdk_pixbuf_get_width(pixbuf) > width ||
+ gdk_pixbuf_get_height(pixbuf) > height) {
+ g_debug("Resizing icon from %dx%d to %dx%d", gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), width, height);
+ resized_pixbuf = gdk_pixbuf_scale_simple(pixbuf,
+ width,
+ height,
+ GDK_INTERP_BILINEAR);
+ } else {
+ g_debug("Happy with icon sized %dx%d", gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf));
+ resized_pixbuf = pixbuf;
+ }
+ gtk_image_set_from_pixbuf(GTK_IMAGE(mi_data->icon), resized_pixbuf);
+ /* The other pixbuf should be free'd by the dbusmenu. */
+ if (resized_pixbuf != pixbuf) {
+ g_object_unref(resized_pixbuf);
+ }
+ }
+ } else if (!g_strcmp0(prop, TIMEZONE_MENUITEM_PROP_ZONE)) {
+ timezone_update_labels(mi_data);
+ } else if (!g_strcmp0(prop, TIMEZONE_MENUITEM_PROP_NAME)) {
+ timezone_update_labels(mi_data);
+ } else if (!g_strcmp0(prop, TIMEZONE_MENUITEM_PROP_RADIO)) {
+ gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(mi_data->gmi), g_variant_get_boolean(value));
+ }
+ return;
+}
+// Properties for marking and unmarking the calendar
+static void
+calendar_prop_change_cb (DbusmenuMenuitem * mi, gchar * prop, GVariant *value, IdoCalendarMenuItem * mi_data)
+{
+ g_debug("Changing calendar property: %s", prop);
+ if (!g_strcmp0(prop, CALENDAR_MENUITEM_PROP_MARKS)) {
+ ido_calendar_menu_item_clear_marks (IDO_CALENDAR_MENU_ITEM (mi_data));
+
+ if (value != NULL) {
+ GVariantIter *iter;
+ gint day;
+
+ g_debug("\tMarks: %s", g_variant_print(value, FALSE));
+
+ g_variant_get (value, "ai", &iter);
+ while (g_variant_iter_loop (iter, "i", &day)) {
+ ido_calendar_menu_item_mark_day (IDO_CALENDAR_MENU_ITEM (mi_data), day);
+ }
+ g_variant_iter_free (iter);
+ } else {
+ g_debug("\tMarks: <cleared>");
+ }
+ }
+ return;
+}
+
+/* We have a small little menuitem type that handles all
+ of the fun stuff for indicators. Mostly this is the
+ shifting over and putting the icon in with some right
+ side text that'll be determined by the service.
+ Copied verbatim from an old revision (including comments) of indicator-messages
+*/
+static gboolean
+new_appointment_item (DbusmenuMenuitem * newitem, DbusmenuMenuitem * parent, DbusmenuClient * client, gpointer user_data)
+{
+ g_return_val_if_fail(DBUSMENU_IS_MENUITEM(newitem), FALSE);
+ g_return_val_if_fail(DBUSMENU_IS_GTKCLIENT(client), FALSE);
+ g_return_val_if_fail(IS_INDICATOR_DATETIME(user_data), FALSE);
+ /* Note: not checking parent, it's reasonable for it to be NULL */
+ IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
+
+ indicator_item_t * mi_data = g_new0(indicator_item_t, 1);
+
+ mi_data->gmi = gtk_menu_item_new();
+
+ GtkWidget * hbox = gtk_hbox_new(FALSE, 4);
+
+ /* Icon, probably someone's face or avatar on an IM */
+ mi_data->icon = gtk_image_new();
+ GdkPixbuf * pixbuf = dbusmenu_menuitem_property_get_image(newitem, APPOINTMENT_MENUITEM_PROP_ICON);
+
+ if (pixbuf != NULL) {
+ /* If we've got a pixbuf we need to make sure it's of a reasonable
+ size to fit in the menu. If not, rescale it. */
+ GdkPixbuf * resized_pixbuf;
+ gint width, height;
+ gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &width, &height);
+ if (gdk_pixbuf_get_width(pixbuf) > width ||
+ gdk_pixbuf_get_height(pixbuf) > height) {
+ g_debug("Resizing icon from %dx%d to %dx%d", gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), width, height);
+ resized_pixbuf = gdk_pixbuf_scale_simple(pixbuf,
+ width,
+ height,
+ GDK_INTERP_BILINEAR);
+ } else {
+ g_debug("Happy with icon sized %dx%d", gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf));
+ resized_pixbuf = pixbuf;
+ }
+
+ gtk_image_set_from_pixbuf(GTK_IMAGE(mi_data->icon), resized_pixbuf);
+
+ /* The other pixbuf should be free'd by the dbusmenu. */
+ if (resized_pixbuf != pixbuf) {
+ g_object_unref(resized_pixbuf);
+ }
+ }
+ gtk_misc_set_alignment(GTK_MISC(mi_data->icon), 0.0, 0.5);
+ gtk_box_pack_start(GTK_BOX(hbox), mi_data->icon, FALSE, FALSE, 0);
+ gtk_widget_show(mi_data->icon);
+
+ /* Label, probably a username, chat room or mailbox name */
+ mi_data->label = gtk_label_new(dbusmenu_menuitem_property_get(newitem, APPOINTMENT_MENUITEM_PROP_LABEL));
+ gtk_misc_set_alignment(GTK_MISC(mi_data->label), 0.0, 0.5);
+
+ GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(mi_data->label));
+ PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(mi_data->label));
+ gint length = measure_string(style, context, "MMMMMMMMMMMMMMM"); // 15 char wide string max
+ gtk_widget_set_size_request(GTK_WIDGET(mi_data->label), length, -1); // Set the min size in pixels
+
+ gtk_label_set_ellipsize(GTK_LABEL(mi_data->label), PANGO_ELLIPSIZE_END);
+ gtk_box_pack_start(GTK_BOX(hbox), mi_data->label, TRUE, TRUE, 0);
+ gtk_widget_show(mi_data->label);
+
+ /* Usually either the time or the count on the individual
+ item. */
+ mi_data->right = gtk_label_new(dbusmenu_menuitem_property_get(newitem, APPOINTMENT_MENUITEM_PROP_RIGHT));
+ gtk_size_group_add_widget(self->priv->indicator_right_group, mi_data->right);
+ gtk_misc_set_alignment(GTK_MISC(mi_data->right), 1.0, 0.5);
+ gtk_box_pack_start(GTK_BOX(hbox), mi_data->right, FALSE, FALSE, 0);
+ gtk_widget_show(mi_data->right);
+
+ gtk_container_add(GTK_CONTAINER(mi_data->gmi), hbox);
+ gtk_widget_show(hbox);
+
+ dbusmenu_gtkclient_newitem_base(DBUSMENU_GTKCLIENT(client), newitem, GTK_MENU_ITEM(mi_data->gmi), parent);
+
+ g_signal_connect(G_OBJECT(newitem), DBUSMENU_MENUITEM_SIGNAL_PROPERTY_CHANGED, G_CALLBACK(indicator_prop_change_cb), mi_data);
+ return TRUE;
+}
+
+static void
+month_changed_cb (IdoCalendarMenuItem *ido,
+ gpointer user_data)
+{
+ guint d,m,y;
+ DbusmenuMenuitem * item = DBUSMENU_MENUITEM (user_data);
+ ido_calendar_menu_item_get_date(ido, &y, &m, &d);
+ struct tm date = {0};
+ date.tm_mday = d;
+ date.tm_mon = m;
+ date.tm_year = y - 1900;
+ guint selecteddate = (guint)mktime(&date);
+ g_debug("Got month changed signal: %s", asctime(&date));
+ GVariant *variant = g_variant_new_uint32(selecteddate);
+ guint timestamp = (guint)time(NULL);
+ dbusmenu_menuitem_handle_event(DBUSMENU_MENUITEM(item), "month-changed", variant, timestamp);
+}
+
+static void
+day_selected_cb (IdoCalendarMenuItem *ido,
+ gpointer user_data)
+{
+ guint d,m,y;
+ DbusmenuMenuitem * item = DBUSMENU_MENUITEM (user_data);
+ ido_calendar_menu_item_get_date(ido, &y, &m, &d);
+ struct tm date = {0};
+ date.tm_mday = d;
+ date.tm_mon = m;
+ date.tm_year = y - 1900;
+ guint selecteddate = (guint)mktime(&date);
+ g_debug("Got day selected signal: %s", asctime(&date));
+ GVariant *variant = g_variant_new_uint32(selecteddate);
+ guint timestamp = (guint)time(NULL);
+ dbusmenu_menuitem_handle_event(DBUSMENU_MENUITEM(item), "day-selected", variant, timestamp);
+}
+
+static void
+day_selected_double_click_cb (IdoCalendarMenuItem *ido,
+ gpointer user_data)
+{
+ guint d,m,y;
+ DbusmenuMenuitem * item = DBUSMENU_MENUITEM (user_data);
+ ido_calendar_menu_item_get_date(ido, &y, &m, &d);
+ struct tm date = {0};
+ date.tm_mday = d;
+ date.tm_mon = m;
+ date.tm_year = y - 1900;
+ guint selecteddate = (guint)mktime(&date);
+ g_debug("Got day selected double click signal: %s", asctime(&date));
+ GVariant *variant = g_variant_new_uint32(selecteddate);
+ guint timestamp = (guint)time(NULL);
+ dbusmenu_menuitem_handle_event(DBUSMENU_MENUITEM(item), "day-selected-double-click", variant, timestamp);
+}
+
+static gboolean
+new_calendar_item (DbusmenuMenuitem * newitem,
+ DbusmenuMenuitem * parent,
+ DbusmenuClient * client,
+ gpointer user_data)
+{
+ g_debug("New calendar item");
+ g_return_val_if_fail(DBUSMENU_IS_MENUITEM(newitem), FALSE);
+ g_return_val_if_fail(DBUSMENU_IS_GTKCLIENT(client), FALSE);
+ g_return_val_if_fail(IS_INDICATOR_DATETIME(user_data), FALSE);
+ /* Note: not checking parent, it's reasonable for it to be NULL */
+
+ IndicatorDatetime *self = INDICATOR_DATETIME(user_data);
+ self->priv = INDICATOR_DATETIME_GET_PRIVATE(self);
+
+ IdoCalendarMenuItem *ido = IDO_CALENDAR_MENU_ITEM (ido_calendar_menu_item_new ());
+ self->priv->ido_calendar = ido;
+
+ GtkCalendarDisplayOptions flags = ido_calendar_menu_item_get_display_options (self->priv->ido_calendar);
+ if (self->priv->show_week_numbers == TRUE)
+ flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
+ else
+ flags &= ~GTK_CALENDAR_SHOW_WEEK_NUMBERS;
+ ido_calendar_menu_item_set_display_options (self->priv->ido_calendar, flags);
+
+ gtk_widget_set_visible (GTK_WIDGET (self->priv->ido_calendar), self->priv->show_calendar);
+
+ dbusmenu_gtkclient_newitem_base(DBUSMENU_GTKCLIENT(client), newitem, GTK_MENU_ITEM(ido), parent);
+
+ g_signal_connect_after(ido, "month-changed", G_CALLBACK(month_changed_cb), (gpointer)newitem);
+ g_signal_connect_after(ido, "day-selected", G_CALLBACK(day_selected_cb), (gpointer)newitem);
+ g_signal_connect_after(ido, "day-selected-double-click", G_CALLBACK(day_selected_double_click_cb), (gpointer)newitem);
+
+ g_signal_connect(G_OBJECT(newitem), DBUSMENU_MENUITEM_SIGNAL_PROPERTY_CHANGED, G_CALLBACK(calendar_prop_change_cb), ido);
+
+ /* Run the current values through prop changed */
+ GVariant * propval = NULL;
+
+ propval = dbusmenu_menuitem_property_get_variant(newitem, CALENDAR_MENUITEM_PROP_MARKS);
+ if (propval != NULL) {
+ calendar_prop_change_cb(newitem, CALENDAR_MENUITEM_PROP_MARKS, propval, ido);
+ }
+
+ return TRUE;
+}
+
+static void
+timezone_toggled_cb (GtkCheckMenuItem *checkmenuitem, DbusmenuMenuitem * dbusitem)
+{
+ /* Make sure that the displayed radio-active setting is always
+ consistent with the dbus menuitem */
+ gtk_check_menu_item_set_active(checkmenuitem,
+ dbusmenu_menuitem_property_get_bool(dbusitem, TIMEZONE_MENUITEM_PROP_RADIO));
+}
+
+static void
+timezone_destroyed_cb (indicator_item_t * mi_data, DbusmenuMenuitem * dbusitem)
+{
+ IndicatorDatetimePrivate *priv = INDICATOR_DATETIME_GET_PRIVATE(mi_data->self);
+ priv->timezone_items = g_list_remove(priv->timezone_items, mi_data);
+ g_signal_handlers_disconnect_by_func(G_OBJECT(mi_data->gmi), G_CALLBACK(timezone_toggled_cb), dbusitem);
+ g_free(mi_data);
+}
+
+static gboolean
+new_timezone_item(DbusmenuMenuitem * newitem,
+ DbusmenuMenuitem * parent,
+ DbusmenuClient * client,
+ gpointer user_data)
+{
+ g_return_val_if_fail(DBUSMENU_IS_MENUITEM(newitem), FALSE);
+ g_return_val_if_fail(DBUSMENU_IS_GTKCLIENT(client), FALSE);
+ g_return_val_if_fail(IS_INDICATOR_DATETIME(user_data), FALSE);
+ /* Note: not checking parent, it's reasonable for it to be NULL */
+
+ IndicatorDatetime * self = INDICATOR_DATETIME(user_data);
+ IndicatorDatetimePrivate *priv = INDICATOR_DATETIME_GET_PRIVATE(self);
+
+ // Menu item with a radio button and a right aligned time
+ indicator_item_t * mi_data = g_new0(indicator_item_t, 1);
+
+ priv->timezone_items = g_list_prepend(priv->timezone_items, mi_data);
+
+ mi_data->self = self;
+ mi_data->mi = newitem;
+ mi_data->gmi = gtk_check_menu_item_new();
+
+ gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(mi_data->gmi), TRUE);
+ gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(mi_data->gmi),
+ dbusmenu_menuitem_property_get_bool(newitem, TIMEZONE_MENUITEM_PROP_RADIO));
+
+ GtkWidget * hbox = gtk_hbox_new(FALSE, 4);
+
+ /* Label, probably a username, chat room or mailbox name */
+ mi_data->label = gtk_label_new("");
+ gtk_misc_set_alignment(GTK_MISC(mi_data->label), 0.0, 0.5);
+ gtk_box_pack_start(GTK_BOX(hbox), mi_data->label, TRUE, TRUE, 0);
+ gtk_widget_show(mi_data->label);
+
+ /* Usually either the time or the count on the individual
+ item. */
+ mi_data->right = gtk_label_new("");
+ gtk_size_group_add_widget(self->priv->indicator_right_group, mi_data->right);
+ gtk_misc_set_alignment(GTK_MISC(mi_data->right), 1.0, 0.5);
+ gtk_box_pack_start(GTK_BOX(hbox), mi_data->right, FALSE, FALSE, 0);
+ gtk_widget_show(mi_data->right);
+
+ timezone_update_labels(mi_data);
+
+ gtk_container_add(GTK_CONTAINER(mi_data->gmi), hbox);
+ gtk_widget_show(hbox);
+
+ dbusmenu_gtkclient_newitem_base(DBUSMENU_GTKCLIENT(client), newitem, GTK_MENU_ITEM(mi_data->gmi), parent);
+
+ g_signal_connect(G_OBJECT(mi_data->gmi), "toggled", G_CALLBACK(timezone_toggled_cb), newitem);
+ g_signal_connect(G_OBJECT(newitem), DBUSMENU_MENUITEM_SIGNAL_PROPERTY_CHANGED, G_CALLBACK(indicator_prop_change_cb), mi_data);
+ g_object_weak_ref(G_OBJECT(newitem), (GWeakNotify)timezone_destroyed_cb, mi_data);
+
+ return TRUE;
+}
+
+/* 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"));
+ gtk_label_set_justify (GTK_LABEL(self->priv->label), GTK_JUSTIFY_CENTER);
+ g_object_ref(G_OBJECT(self->priv->label));
+ g_signal_connect(G_OBJECT(self->priv->label), "style-set", G_CALLBACK(style_changed), self);
+ g_signal_connect(G_OBJECT(self->priv->label), "screen-changed", G_CALLBACK(update_text_gravity), self);
+ guess_label_size(self);
+ update_label(self, NULL);
+ gtk_widget_set_visible(GTK_WIDGET (self->priv->label), self->priv->show_clock);
+ }
+
+ if (self->priv->timer == 0) {
+ setup_timer(self, NULL);
+ }
+
+ return self->priv->label;
+}
+
+static GtkMenu *
+get_menu (IndicatorObject * io)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(io);
+
+ return GTK_MENU(self->priv->menu);
+}
+
+static const gchar *
+get_accessible_desc (IndicatorObject * io)
+{
+ IndicatorDatetime * self = INDICATOR_DATETIME(io);
+ const gchar * name;
+
+ if (self->priv->label != NULL) {
+ name = gtk_label_get_text(self->priv->label);
+ return name;
+ }
+ return NULL;
+}
diff --git a/src/settings-shared.h b/src/settings-shared.h
new file mode 100644
index 0000000..da66205
--- /dev/null
+++ b/src/settings-shared.h
@@ -0,0 +1,68 @@
+/*
+An indicator to show date and time information.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ Ted Gould <ted@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __DATETIME_SETTINGS_SHARED_H__
+#define __DATETIME_SETTINGS_SHARED_H__
+
+#define SETTINGS_INTERFACE "com.canonical.indicator.datetime"
+#define SETTINGS_SHOW_CLOCK_S "show-clock"
+#define SETTINGS_TIME_FORMAT_S "time-format"
+#define SETTINGS_SHOW_SECONDS_S "show-seconds"
+#define SETTINGS_SHOW_DAY_S "show-day"
+#define SETTINGS_SHOW_DATE_S "show-date"
+#define SETTINGS_CUSTOM_TIME_FORMAT_S "custom-time-format"
+#define SETTINGS_SHOW_CALENDAR_S "show-calendar"
+#define SETTINGS_SHOW_WEEK_NUMBERS_S "show-week-numbers"
+#define SETTINGS_SHOW_EVENTS_S "show-events"
+#define SETTINGS_SHOW_LOCATIONS_S "show-locations"
+#define SETTINGS_LOCATIONS_S "locations"
+#define SETTINGS_TIMEZONE_NAME_S "timezone-name"
+
+enum {
+ SETTINGS_TIME_LOCALE = 0,
+ SETTINGS_TIME_12_HOUR = 1,
+ SETTINGS_TIME_24_HOUR = 2,
+ SETTINGS_TIME_CUSTOM = 3
+};
+
+/* TRANSLATORS: A format string for the strftime function for
+ a clock showing 12-hour time without seconds. */
+#define DEFAULT_TIME_12_FORMAT N_("%l:%M %p")
+
+/* TRANSLATORS: A format string for the strftime function for
+ a clock showing 24-hour time without seconds. */
+#define DEFAULT_TIME_24_FORMAT N_("%H:%M")
+
+#define DEFAULT_TIME_FORMAT DEFAULT_TIME_12_FORMAT
+#define DEFAULT_TIME_FORMAT_WITH_DAY DEFAULT_TIME_12_FORMAT_WITH_DAY
+
+/* TRANSLATORS: A format string for the strftime function for
+ a clock showing the day of the week and the time in 12-hour format without
+ seconds. */
+#define DEFAULT_TIME_12_FORMAT_WITH_DAY N_("%a %l:%M %p")
+
+/* TRANSLATORS: A format string for the strftime function for
+ a clock showing the day of the week and the time in 24-hour format without
+ seconds. Information is available in this Launchpad answer:
+ https://answers.launchpad.net/ubuntu/+source/indicator-datetime/+question/149752 */
+#define DEFAULT_TIME_24_FORMAT_WITH_DAY N_("%a %H:%M")
+
+#endif
diff --git a/src/timezone-completion.c b/src/timezone-completion.c
new file mode 100644
index 0000000..7dcc28e
--- /dev/null
+++ b/src/timezone-completion.c
@@ -0,0 +1,688 @@
+/* -*- Mode: C; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*-
+
+Copyright 2011 Canonical Ltd.
+
+Authors:
+ Michael Terry <michael.terry@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <json-glib/json-glib.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkkeysyms.h>
+#include <glib/gi18n.h>
+#include "timezone-completion.h"
+#include "tz.h"
+
+enum {
+ LAST_SIGNAL
+};
+
+/* static guint signals[LAST_SIGNAL] = { }; */
+
+typedef struct _TimezoneCompletionPrivate TimezoneCompletionPrivate;
+struct _TimezoneCompletionPrivate
+{
+ GtkTreeModel * initial_model;
+ GtkEntry * entry;
+ guint queued_request;
+ guint changed_id;
+ guint keypress_id;
+ GCancellable * cancel;
+ gchar * request_text;
+ GHashTable * request_table;
+};
+
+#define TIMEZONE_COMPLETION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), TIMEZONE_COMPLETION_TYPE, TimezoneCompletionPrivate))
+
+#define GEONAME_URL "http://geoname-lookup.ubuntu.com/?query=%s&release=%s&lang=%s"
+
+/* Prototypes */
+static void timezone_completion_class_init (TimezoneCompletionClass *klass);
+static void timezone_completion_init (TimezoneCompletion *self);
+static void timezone_completion_dispose (GObject *object);
+static void timezone_completion_finalize (GObject *object);
+
+G_DEFINE_TYPE (TimezoneCompletion, timezone_completion, GTK_TYPE_ENTRY_COMPLETION);
+
+static gboolean
+match_func (GtkEntryCompletion *completion, const gchar *key,
+ GtkTreeIter *iter, gpointer user_data)
+{
+ // geonames does the work for us
+ return TRUE;
+}
+
+static void
+save_and_use_model (TimezoneCompletion * completion, GtkTreeModel * model)
+{
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE(completion);
+
+ g_hash_table_insert (priv->request_table, g_strdup (priv->request_text), g_object_ref_sink (model));
+
+ if (model == priv->initial_model)
+ gtk_entry_completion_set_match_func (GTK_ENTRY_COMPLETION (completion), NULL, NULL, NULL);
+ else
+ gtk_entry_completion_set_match_func (GTK_ENTRY_COMPLETION (completion), match_func, NULL, NULL);
+
+ gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (completion), model);
+
+ if (priv->entry != NULL) {
+ gtk_entry_completion_complete (GTK_ENTRY_COMPLETION (completion));
+
+ /* By this time, the changed signal has come and gone. We didn't give a
+ model to use, so no popup appeared for user. Poke the entry again to show
+ popup in 300ms. */
+ g_signal_emit_by_name (priv->entry, "changed");
+ }
+}
+
+static gint
+sort_zone (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
+ gpointer user_data)
+{
+ /* Anything that has text as a prefix goes first, in mostly sorted order.
+ Then everything else goes after, in mostly sorted order. */
+ const gchar *casefolded_text = (const gchar *)user_data;
+
+ const gchar *namea = NULL, *nameb = NULL;
+ gtk_tree_model_get (model, a, TIMEZONE_COMPLETION_NAME, &namea, -1);
+ gtk_tree_model_get (model, b, TIMEZONE_COMPLETION_NAME, &nameb, -1);
+
+ gchar *casefolded_namea = NULL, *casefolded_nameb = NULL;
+ casefolded_namea = g_utf8_casefold (namea, -1);
+ casefolded_nameb = g_utf8_casefold (nameb, -1);
+
+ gboolean amatches = FALSE, bmatches = FALSE;
+ amatches = strncmp (casefolded_text, casefolded_namea, strlen(casefolded_text)) == 0;
+ bmatches = strncmp (casefolded_text, casefolded_nameb, strlen(casefolded_text)) == 0;
+
+ gint rv;
+ if (amatches && !bmatches)
+ rv = -1;
+ else if (bmatches && !amatches)
+ rv = 1;
+ else
+ rv = g_utf8_collate (casefolded_namea, casefolded_nameb);
+
+ g_free (casefolded_namea);
+ g_free (casefolded_nameb);
+ return rv;
+}
+
+static void
+json_parse_ready (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ TimezoneCompletion * completion = TIMEZONE_COMPLETION (user_data);
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE(completion);
+ GError * error = NULL;
+ const gchar * prev_name = NULL;
+ const gchar * prev_admin1 = NULL;
+ const gchar * prev_country = NULL;
+
+ json_parser_load_from_stream_finish (JSON_PARSER (object), res, &error);
+
+ if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) && priv->cancel) {
+ g_cancellable_reset (priv->cancel);
+ }
+
+ if (error != NULL) {
+ if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+ save_and_use_model (completion, priv->initial_model);
+ g_warning ("Could not parse geoname JSON data: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+ GtkListStore * store = gtk_list_store_new (TIMEZONE_COMPLETION_LAST,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING);
+
+ JsonReader * reader = json_reader_new (json_parser_get_root (JSON_PARSER (object)));
+
+ if (!json_reader_is_array (reader)) {
+ g_warning ("Could not parse geoname JSON data");
+ save_and_use_model (completion, priv->initial_model);
+ g_object_unref (G_OBJECT (reader));
+ return;
+ }
+
+ gint i, count = json_reader_count_elements (reader);
+ for (i = 0; i < count; ++i) {
+ if (!json_reader_read_element (reader, i))
+ continue;
+
+ if (json_reader_is_object (reader)) {
+ const gchar * name = NULL;
+ const gchar * admin1 = NULL;
+ const gchar * country = NULL;
+ const gchar * longitude = NULL;
+ const gchar * latitude = NULL;
+ gboolean skip = FALSE;
+ if (json_reader_read_member (reader, "name")) {
+ name = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+ if (json_reader_read_member (reader, "admin1")) {
+ admin1 = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+ if (json_reader_read_member (reader, "country")) {
+ country = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+ if (json_reader_read_member (reader, "longitude")) {
+ longitude = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+ if (json_reader_read_member (reader, "latitude")) {
+ latitude = json_reader_get_string_value (reader);
+ json_reader_end_member (reader);
+ }
+
+ if (g_strcmp0(name, prev_name) == 0 &&
+ g_strcmp0(admin1, prev_admin1) == 0 &&
+ g_strcmp0(country, prev_country) == 0) {
+ // Sometimes the data will have duplicate entries that only differ
+ // in longitude and latitude. e.g. "rio de janeiro", "wellington"
+ skip = TRUE;
+ }
+
+ if (!skip) {
+ GtkTreeIter iter;
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter,
+ TIMEZONE_COMPLETION_ZONE, NULL,
+ TIMEZONE_COMPLETION_NAME, name,
+ TIMEZONE_COMPLETION_ADMIN1, admin1,
+ TIMEZONE_COMPLETION_COUNTRY, country,
+ TIMEZONE_COMPLETION_LONGITUDE, longitude,
+ TIMEZONE_COMPLETION_LATITUDE, latitude,
+ -1);
+ gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store),
+ TIMEZONE_COMPLETION_NAME, sort_zone,
+ g_utf8_casefold(priv->request_text, -1),
+ g_free);
+ gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
+ TIMEZONE_COMPLETION_NAME,
+ GTK_SORT_ASCENDING);
+ }
+
+ prev_name = name;
+ prev_admin1 = admin1;
+ prev_country = country;
+ }
+
+ json_reader_end_element (reader);
+ }
+
+ if (strlen (priv->request_text) < 4) {
+ gchar * lower_text = g_ascii_strdown (priv->request_text, -1);
+ if (g_strcmp0 (lower_text, "ut") == 0 ||
+ g_strcmp0 (lower_text, "utc") == 0) {
+ GtkTreeIter iter;
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter,
+ TIMEZONE_COMPLETION_ZONE, "UTC",
+ TIMEZONE_COMPLETION_NAME, "UTC",
+ -1);
+ }
+ g_free (lower_text);
+ }
+
+ save_and_use_model (completion, GTK_TREE_MODEL (store));
+ g_object_unref (G_OBJECT (reader));
+}
+
+static void
+geonames_data_ready (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ TimezoneCompletion * completion = TIMEZONE_COMPLETION (user_data);
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+ GError * error = NULL;
+ GFileInputStream * stream;
+
+ stream = g_file_read_finish (G_FILE (object), res, &error);
+
+ if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) && priv->cancel) {
+ g_cancellable_reset (priv->cancel);
+ }
+
+ if (error != NULL) {
+ if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+ save_and_use_model (completion, priv->initial_model);
+ g_warning ("Could not connect to geoname lookup server: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+ JsonParser * parser = json_parser_new ();
+ json_parser_load_from_stream_async (parser, G_INPUT_STREAM (stream), priv->cancel,
+ json_parse_ready, user_data);
+}
+
+/* Returns message locale, with possible country info too like en_US */
+static gchar *
+get_locale (void)
+{
+ /* Check LANGUAGE, LC_ALL, LC_MESSAGES, and LANG, treat as colon-separated */
+ const gchar *env_names[] = {"LANGUAGE", "LC_ALL", "LC_MESSAGES", "LANG", NULL};
+ const gchar *env = NULL;
+ gint i;
+
+ for (i = 0; env_names[i]; i++) {
+ env = g_getenv (env_names[i]);
+ if (env != NULL && env[0] != 0)
+ break;
+ }
+
+ if (env == NULL)
+ return NULL;
+
+ /* Now, we split on colons as expected, but also on . and @ to filter out
+ extra pieces of locale we don't care about as we only use first chunk. */
+ gchar **split = g_strsplit_set (env, ":.@", 2);
+ if (split == NULL)
+ return NULL;
+
+ if (split[0] == NULL) {
+ g_strfreev (split);
+ return NULL;
+ }
+
+ gchar *locale = g_strdup (split[0]);
+ g_strfreev (split);
+ return locale;
+}
+
+static gchar *
+get_version (void)
+{
+ static gchar *version = NULL;
+
+ if (version == NULL) {
+ gchar *stdout = NULL;
+ g_spawn_command_line_sync ("lsb_release -rs", &stdout, NULL, NULL, NULL);
+
+ if (stdout != NULL)
+ version = g_strstrip (stdout);
+ else
+ version = g_strdup("");
+ }
+
+ return version;
+}
+
+static gboolean
+request_zones (TimezoneCompletion * completion)
+{
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+
+ priv->queued_request = 0;
+
+ if (priv->entry == NULL) {
+ return FALSE;
+ }
+
+ /* Cancel any ongoing request */
+ if (priv->cancel) {
+ g_cancellable_cancel (priv->cancel);
+ g_cancellable_reset (priv->cancel);
+ }
+ g_free (priv->request_text);
+
+ const gchar * text = gtk_entry_get_text (priv->entry);
+ priv->request_text = g_strdup (text);
+
+ gchar * escaped = g_uri_escape_string (text, NULL, FALSE);
+ gchar * version = get_version ();
+ gchar * locale = get_locale ();
+ gchar * url = g_strdup_printf (GEONAME_URL, escaped, version, locale);
+ g_free (locale);
+ g_free (version);
+ g_free (escaped);
+
+ GFile * file = g_file_new_for_uri (url);
+ g_free (url);
+
+ g_file_read_async (file, G_PRIORITY_DEFAULT, priv->cancel,
+ geonames_data_ready, completion);
+
+ return FALSE;
+}
+
+static void
+entry_changed (GtkEntry * entry, TimezoneCompletion * completion)
+{
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+
+ if (priv->queued_request) {
+ g_source_remove (priv->queued_request);
+ priv->queued_request = 0;
+ }
+
+ /* See if we've already got this one */
+ const gchar * text = gtk_entry_get_text (priv->entry);
+ gpointer data;
+ if (g_hash_table_lookup_extended (priv->request_table, text, NULL, &data)) {
+ gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (completion), GTK_TREE_MODEL (data));
+ }
+ else {
+ priv->queued_request = g_timeout_add (300, (GSourceFunc)request_zones, completion);
+ gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (completion), NULL);
+ }
+ gtk_entry_completion_complete (GTK_ENTRY_COMPLETION (completion));
+}
+
+static GtkWidget *
+get_descendent (GtkWidget * parent, GType type)
+{
+ if (g_type_is_a (G_OBJECT_TYPE (parent), type))
+ return parent;
+
+ if (GTK_IS_CONTAINER (parent)) {
+ GList * children = gtk_container_get_children (GTK_CONTAINER (parent));
+ GList * iter;
+ for (iter = children; iter; iter = iter->next) {
+ GtkWidget * found = get_descendent (GTK_WIDGET (iter->data), type);
+ if (found) {
+ g_list_free (children);
+ return found;
+ }
+ }
+ g_list_free (children);
+ }
+
+ return NULL;
+}
+
+/**
+ * The popup window and its GtkTreeView are private to our parent completion
+ * object. We can't get access to discover if there is a highlighted item or
+ * even if the window is showing right now. So this is a super hack to find
+ * it by looking through our toplevel's window group and finding a window with
+ * a GtkTreeView that points at our model. There should be only one ever, so
+ * we'll use the first one we find.
+ */
+static GtkTreeView *
+find_popup_treeview (GtkWidget * widget, GtkTreeModel * model)
+{
+ GtkWidget * toplevel = gtk_widget_get_toplevel (widget);
+ if (!GTK_IS_WINDOW (toplevel))
+ return NULL;
+
+ GtkWindowGroup * group = gtk_window_get_group (GTK_WINDOW (toplevel));
+ GList * windows = gtk_window_group_list_windows (group);
+ GList * iter;
+ for (iter = windows; iter; iter = iter->next) {
+ if (iter->data == toplevel)
+ continue; // Skip our own window, we don't have it
+ GtkWidget * view = get_descendent (GTK_WIDGET (iter->data), GTK_TYPE_TREE_VIEW);
+ if (view != NULL) {
+ GtkTreeModel * tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
+ if (GTK_IS_TREE_MODEL_FILTER (tree_model))
+ tree_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (tree_model));
+ if (tree_model == model) {
+ g_list_free (windows);
+ return GTK_TREE_VIEW (view);
+ }
+ }
+ }
+ g_list_free (windows);
+
+ return NULL;
+}
+
+static gboolean
+entry_keypress (GtkEntry * entry, GdkEventKey *event, TimezoneCompletion * completion)
+{
+ if (event->keyval == GDK_ISO_Enter ||
+ event->keyval == GDK_KP_Enter ||
+ event->keyval == GDK_Return) {
+ /* Make sure that user has a selection to choose, otherwise ignore */
+ GtkTreeModel * model = gtk_entry_completion_get_model (GTK_ENTRY_COMPLETION (completion));
+ GtkTreeView * view = find_popup_treeview (GTK_WIDGET (entry), model);
+ if (view == NULL) {
+ // Just beep if popup hasn't appeared yet.
+ gtk_widget_error_bell (GTK_WIDGET (entry));
+ return TRUE;
+ }
+
+ GtkTreeSelection * sel = gtk_tree_view_get_selection (view);
+ GtkTreeModel * sel_model = NULL;
+ if (!gtk_tree_selection_get_selected (sel, &sel_model, NULL)) {
+ // No selection, we should help them out and select first item in list
+ GtkTreeIter iter;
+ if (gtk_tree_model_get_iter_first (sel_model, &iter))
+ gtk_tree_selection_select_iter (sel, &iter);
+ // And fall through to normal handler code
+ }
+ }
+
+ return FALSE;
+}
+
+void
+timezone_completion_watch_entry (TimezoneCompletion * completion, GtkEntry * entry)
+{
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+
+ if (priv->queued_request) {
+ g_source_remove (priv->queued_request);
+ priv->queued_request = 0;
+ }
+ if (priv->entry) {
+ g_signal_handler_disconnect (priv->entry, priv->changed_id);
+ g_signal_handler_disconnect (priv->entry, priv->keypress_id);
+ g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry);
+ gtk_entry_set_completion (priv->entry, NULL);
+ }
+
+ guint id = g_signal_connect (entry, "changed", G_CALLBACK (entry_changed), completion);
+ priv->changed_id = id;
+
+ id = g_signal_connect (entry, "key-press-event", G_CALLBACK (entry_keypress), completion);
+ priv->keypress_id = id;
+
+ priv->entry = entry;
+ g_object_add_weak_pointer (G_OBJECT (entry), (gpointer *)&priv->entry);
+
+ gtk_entry_set_completion (entry, GTK_ENTRY_COMPLETION (completion));
+}
+
+static GtkListStore *
+get_initial_model (void)
+{
+ TzDB * db = tz_load_db ();
+ GPtrArray * locations = tz_get_locations (db);
+
+ GtkListStore * store = gtk_list_store_new (TIMEZONE_COMPLETION_LAST,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING);
+
+ gint i;
+ for (i = 0; i < locations->len; ++i) {
+ TzLocation * loc = g_ptr_array_index (locations, i);
+ GtkTreeIter iter;
+ gtk_list_store_append (store, &iter);
+
+ /* FIXME: need something better than below for non-English locales */
+ const gchar * last_bit = ((const gchar *)strrchr (loc->zone, '/')) + 1;
+ if (last_bit == NULL)
+ last_bit = loc->zone;
+ gchar * name = g_strdup (last_bit);
+ gchar * underscore;
+ while ((underscore = strchr (name, '_'))) {
+ *underscore = ' ';
+ }
+
+ gtk_list_store_set (store, &iter,
+ TIMEZONE_COMPLETION_ZONE, loc->zone,
+ TIMEZONE_COMPLETION_NAME, name,
+ TIMEZONE_COMPLETION_COUNTRY, loc->country,
+ -1);
+
+ g_free (name);
+ }
+
+ GtkTreeIter iter;
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter,
+ TIMEZONE_COMPLETION_ZONE, "UTC",
+ TIMEZONE_COMPLETION_NAME, "UTC",
+ -1);
+
+ tz_db_free (db);
+ return store;
+}
+
+static void
+data_func (GtkCellLayout *cell_layout, GtkCellRenderer *cell,
+ GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
+{
+ const gchar * name, * admin1, * country;
+
+ gtk_tree_model_get (GTK_TREE_MODEL (tree_model), iter,
+ TIMEZONE_COMPLETION_NAME, &name,
+ TIMEZONE_COMPLETION_ADMIN1, &admin1,
+ TIMEZONE_COMPLETION_COUNTRY, &country,
+ -1);
+
+ gchar * user_name;
+ if (country == NULL || country[0] == 0) {
+ user_name = g_strdup (name);
+ } else if (admin1 == NULL || admin1[0] == 0) {
+ user_name = g_strdup_printf ("%s <small>(%s)</small>", name, country);
+ } else {
+ user_name = g_strdup_printf ("%s <small>(%s, %s)</small>", name, admin1, country);
+ }
+
+ g_object_set (G_OBJECT (cell), "markup", user_name, NULL);
+}
+
+static void
+timezone_completion_class_init (TimezoneCompletionClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (TimezoneCompletionPrivate));
+
+ object_class->dispose = timezone_completion_dispose;
+ object_class->finalize = timezone_completion_finalize;
+
+ return;
+}
+
+static void
+timezone_completion_init (TimezoneCompletion * self)
+{
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (self);
+
+ priv->initial_model = GTK_TREE_MODEL (get_initial_model ());
+
+ g_object_set (G_OBJECT (self),
+ "text-column", TIMEZONE_COMPLETION_NAME,
+ "popup-set-width", FALSE,
+ NULL);
+
+ priv->cancel = g_cancellable_new ();
+
+ priv->request_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
+
+ GtkCellRenderer * cell = gtk_cell_renderer_text_new ();
+ gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, TRUE);
+ gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (self), cell, data_func, NULL, NULL);
+
+ return;
+}
+
+static void
+timezone_completion_dispose (GObject * object)
+{
+ G_OBJECT_CLASS (timezone_completion_parent_class)->dispose (object);
+
+ TimezoneCompletion * completion = TIMEZONE_COMPLETION (object);
+ TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
+
+ if (priv->changed_id) {
+ if (priv->entry)
+ g_signal_handler_disconnect (priv->entry, priv->changed_id);
+ priv->changed_id = 0;
+ }
+
+ if (priv->keypress_id) {
+ if (priv->entry)
+ g_signal_handler_disconnect (priv->entry, priv->keypress_id);
+ priv->keypress_id = 0;
+ }
+
+ if (priv->entry != NULL) {
+ g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry);
+ }
+
+ if (priv->initial_model != NULL) {
+ g_object_unref (G_OBJECT (priv->initial_model));
+ priv->initial_model = NULL;
+ }
+
+ if (priv->queued_request) {
+ g_source_remove (priv->queued_request);
+ priv->queued_request = 0;
+ }
+
+ if (priv->cancel != NULL) {
+ g_cancellable_cancel (priv->cancel);
+ g_object_unref (priv->cancel);
+ priv->cancel = NULL;
+ }
+
+ if (priv->request_text != NULL) {
+ g_free (priv->request_text);
+ priv->request_text = NULL;
+ }
+
+ if (priv->request_table != NULL) {
+ g_hash_table_destroy (priv->request_table);
+ priv->request_table = NULL;
+ }
+
+ return;
+}
+
+static void
+timezone_completion_finalize (GObject * object)
+{
+ G_OBJECT_CLASS (timezone_completion_parent_class)->finalize (object);
+ return;
+}
+
+TimezoneCompletion *
+timezone_completion_new ()
+{
+ TimezoneCompletion * self = g_object_new (TIMEZONE_COMPLETION_TYPE, NULL);
+ return self;
+}
+
diff --git a/src/timezone-completion.h b/src/timezone-completion.h
new file mode 100644
index 0000000..fdfb234
--- /dev/null
+++ b/src/timezone-completion.h
@@ -0,0 +1,63 @@
+/* -*- Mode: C; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*-
+
+Copyright 2011 Canonical Ltd.
+
+Authors:
+ Michael Terry <michael.terry@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __TIMEZONE_COMPLETION_H__
+#define __TIMEZONE_COMPLETION_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define TIMEZONE_COMPLETION_TYPE (timezone_completion_get_type ())
+#define TIMEZONE_COMPLETION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TIMEZONE_COMPLETION_TYPE, TimezoneCompletion))
+#define TIMEZONE_COMPLETION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TIMEZONE_COMPLETION_TYPE, TimezoneCompletionClass))
+#define IS_TIMEZONE_COMPLETION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TIMEZONE_COMPLETION_TYPE))
+#define IS_TIMEZONE_COMPLETION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TIMEZONE_COMPLETION_TYPE))
+#define TIMEZONE_COMPLETION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TIMEZONE_COMPLETION_TYPE, TimezoneCompletionClass))
+
+typedef struct _TimezoneCompletion TimezoneCompletion;
+typedef struct _TimezoneCompletionClass TimezoneCompletionClass;
+
+struct _TimezoneCompletionClass {
+ GtkEntryCompletionClass parent_class;
+};
+
+struct _TimezoneCompletion {
+ GtkEntryCompletion parent;
+};
+
+#define TIMEZONE_COMPLETION_ZONE 0
+#define TIMEZONE_COMPLETION_NAME 1
+#define TIMEZONE_COMPLETION_ADMIN1 2
+#define TIMEZONE_COMPLETION_COUNTRY 3
+#define TIMEZONE_COMPLETION_LONGITUDE 4
+#define TIMEZONE_COMPLETION_LATITUDE 5
+#define TIMEZONE_COMPLETION_LAST 6
+
+GType timezone_completion_get_type (void);
+TimezoneCompletion * timezone_completion_new ();
+void timezone_completion_watch_entry (TimezoneCompletion * completion, GtkEntry * entry);
+
+G_END_DECLS
+
+#endif /* __TIMEZONE_COMPLETION_H__ */
+
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..ab93ecf
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,276 @@
+/* -*- Mode: C; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*-
+
+A dialog for setting time and date preferences.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ Michael Terry <michael.terry@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <glib/gi18n-lib.h>
+#include <gio/gio.h>
+#include <locale.h>
+#include <langinfo.h>
+#include <string.h>
+#include "utils.h"
+#include "settings-shared.h"
+
+/* Check the system locale setting to see if the format is 24-hour
+ time or 12-hour time */
+gboolean
+is_locale_12h (void)
+{
+ static const char *formats_24h[] = {"%H", "%R", "%T", "%OH", "%k", NULL};
+ const char *t_fmt = nl_langinfo (T_FMT);
+ int i;
+
+ for (i = 0; formats_24h[i]; ++i) {
+ if (strstr (t_fmt, formats_24h[i])) {
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+void
+split_settings_location (const gchar * location, gchar ** zone, gchar ** name)
+{
+ gchar * location_dup = g_strdup (location);
+ gchar * first = strchr (location_dup, ' ');
+
+ if (first) {
+ first[0] = 0;
+ }
+
+ if (zone) {
+ *zone = location_dup;
+ }
+
+ if (name) {
+ gchar * after = first ? g_strstrip (first + 1) : NULL;
+ if (after == NULL || after[0] == 0) {
+ /* Make up name from zone */
+ gchar * chr = strrchr (location_dup, '/');
+ after = g_strdup (chr ? chr + 1 : location_dup);
+ while ((chr = strchr (after, '_')) != NULL) { /* and turn underscores to spaces */
+ *chr = ' ';
+ }
+ *name = after;
+ }
+ else {
+ *name = g_strdup (after);
+ }
+ }
+}
+
+gchar *
+get_current_zone_name (const gchar * location)
+{
+ gchar * new_zone, * new_name;
+ gchar * old_zone, * old_name;
+ gchar * rv;
+
+ split_settings_location (location, &new_zone, &new_name);
+
+ GSettings * conf = g_settings_new (SETTINGS_INTERFACE);
+ gchar * tz_name = g_settings_get_string (conf, SETTINGS_TIMEZONE_NAME_S);
+ split_settings_location (tz_name, &old_zone, &old_name);
+ g_free (tz_name);
+ g_object_unref (conf);
+
+ // new_name is always just a sanitized version of a timezone.
+ // old_name is potentially a saved "pretty" version of a timezone name from
+ // geonames. So we prefer to use it if available and the zones match.
+
+ if (g_strcmp0 (old_zone, new_zone) == 0) {
+ rv = old_name;
+ old_name = NULL;
+ }
+ else {
+ rv = new_name;
+ new_name = NULL;
+ }
+
+ g_free (new_zone);
+ g_free (old_zone);
+ g_free (new_name);
+ g_free (old_name);
+
+ return rv;
+}
+
+/* Translate msg according to the locale specified by LC_TIME */
+static char *
+T_(const char *msg)
+{
+ /* General strategy here is to make sure LANGUAGE is empty (since that
+ trumps all LC_* vars) and then to temporarily swap LC_TIME and
+ LC_MESSAGES. Then have gettext translate msg.
+
+ We strdup the strings because the setlocale & *env functions do not
+ guarantee anything about the storage used for the string, and thus
+ the string may not be portably safe after multiple calls.
+
+ Note that while you might think g_dcgettext would do the trick here,
+ that actually looks in /usr/share/locale/XX/LC_TIME, not the
+ LC_MESSAGES directory, so we won't find any translation there.
+ */
+ char *message_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
+ char *time_locale = g_strdup(setlocale(LC_TIME, NULL));
+ char *language = g_strdup(g_getenv("LANGUAGE"));
+ char *rv;
+ g_unsetenv("LANGUAGE");
+ setlocale(LC_MESSAGES, time_locale);
+
+ /* Get the LC_TIME version */
+ rv = _(msg);
+
+ /* Put everything back the way it was */
+ setlocale(LC_MESSAGES, message_locale);
+ g_setenv("LANGUAGE", language, TRUE);
+ g_free(message_locale);
+ g_free(time_locale);
+ g_free(language);
+ return rv;
+}
+
+/* Tries to figure out what our format string should be. Lots
+ of translator comments in here. */
+gchar *
+generate_format_string_full (gboolean show_day, gboolean show_date)
+{
+ gboolean twelvehour = TRUE;
+
+ GSettings * settings = g_settings_new (SETTINGS_INTERFACE);
+ gint time_mode = g_settings_get_enum (settings, SETTINGS_TIME_FORMAT_S);
+ gboolean show_seconds = g_settings_get_boolean (settings, SETTINGS_SHOW_SECONDS_S);
+ g_object_unref (settings);
+
+ if (time_mode == SETTINGS_TIME_LOCALE) {
+ twelvehour = is_locale_12h();
+ } else if (time_mode == SETTINGS_TIME_24_HOUR) {
+ twelvehour = FALSE;
+ }
+
+ const gchar * time_string = NULL;
+ if (twelvehour) {
+ if (show_seconds) {
+ /* TRANSLATORS: A format string for the strftime function for
+ a clock showing 12-hour time with seconds. */
+ time_string = T_("%l:%M:%S %p");
+ } else {
+ time_string = T_(DEFAULT_TIME_12_FORMAT);
+ }
+ } else {
+ if (show_seconds) {
+ /* TRANSLATORS: A format string for the strftime function for
+ a clock showing 24-hour time with seconds. */
+ time_string = T_("%H:%M:%S");
+ } else {
+ time_string = T_(DEFAULT_TIME_24_FORMAT);
+ }
+ }
+
+ /* Checkpoint, let's not fail */
+ g_return_val_if_fail(time_string != NULL, g_strdup(DEFAULT_TIME_FORMAT));
+
+ /* If there's no date or day let's just leave now and
+ not worry about the rest of this code */
+ if (!show_date && !show_day) {
+ return g_strdup(time_string);
+ }
+
+ const gchar * date_string = NULL;
+ if (show_date && show_day) {
+ /* TRANSLATORS: This is a format string passed to strftime to represent
+ the day of the week, the month and the day of the month. */
+ date_string = T_("%a %b %e");
+ } else if (show_date) {
+ /* TRANSLATORS: This is a format string passed to strftime to represent
+ the month and the day of the month. */
+ date_string = T_("%b %e");
+ } else if (show_day) {
+ /* TRANSLATORS: This is a format string passed to strftime to represent
+ the day of the week. */
+ date_string = T_("%a");
+ }
+
+ /* Check point, we should have a date string */
+ g_return_val_if_fail(date_string != NULL, g_strdup(time_string));
+
+ /* TRANSLATORS: This is a format string passed to strftime to combine the
+ date and the time. The value of "%s %s" would result in a string like
+ this in US English 12-hour time: 'Fri Jul 16 11:50 AM' */
+ return g_strdup_printf(T_("%s %s"), date_string, time_string);
+}
+
+gchar *
+generate_format_string_at_time (GDateTime * time)
+{
+ /* This is a bit less free-form than for the main "now" time label. */
+ /* If it is today, just the time should be shown (e.g. “3:55 PM”)
+ If it is a different day this week, the day and time should be shown (e.g. “Wed 3:55 PM”)
+ If it is after this week, the day, date, and time should be shown (e.g. “Wed 21 Apr 3:55 PM”).
+ In addition, when presenting the times of upcoming events, the time should be followed by the timezone if it is different from the one the computer is currently set to. For example, “Wed 3:55 PM UTC−5”. */
+ gboolean show_day = FALSE;
+ gboolean show_date = FALSE;
+
+ GDateTime * now = g_date_time_new_now_local();
+
+ /* First, are we same day? */
+ gint time_year, time_month, time_day;
+ gint now_year, now_month, now_day;
+ g_date_time_get_ymd(time, &time_year, &time_month, &time_day);
+ g_date_time_get_ymd(now, &now_year, &now_month, &now_day);
+
+ if (time_year != now_year ||
+ time_month != now_month ||
+ time_day != now_day) {
+ /* OK, different days so we must at least show the day. */
+ show_day = TRUE;
+
+ /* Is it this week? */
+ /* Here, we define "is this week" as yesterday, today, or the next five days */
+ GDateTime * past = g_date_time_add_days(now, -1);
+ GDateTime * future = g_date_time_add_days(now, 5);
+ GDateTime * past_bound = g_date_time_new_local(g_date_time_get_year(past),
+ g_date_time_get_month(past),
+ g_date_time_get_day_of_month(past),
+ 0, 0, 0.0);
+ GDateTime * future_bound = g_date_time_new_local(g_date_time_get_year(future),
+ g_date_time_get_month(future),
+ g_date_time_get_day_of_month(future),
+ 23, 59, 59.9);
+ if (g_date_time_compare(time, past_bound) < 0 ||
+ g_date_time_compare(time, future_bound) > 0) {
+ show_date = TRUE;
+ }
+ g_date_time_unref(past);
+ g_date_time_unref(future);
+ g_date_time_unref(past_bound);
+ g_date_time_unref(future_bound);
+ }
+
+ g_date_time_unref (now);
+
+ return generate_format_string_full(show_day, show_date);
+}
+
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..c2bc0c5
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,38 @@
+/* -*- Mode: C; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*-
+
+A dialog for setting time and date preferences.
+
+Copyright 2010 Canonical Ltd.
+
+Authors:
+ Michael Terry <michael.terry@canonical.com>
+
+This program is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License version 3, as published
+by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranties of
+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
+PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __DATETIME_UTILS_H__
+#define __DATETIME_UTILS_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+gboolean is_locale_12h (void);
+void split_settings_location (const gchar * location, gchar ** zone, gchar ** name);
+gchar * get_current_zone_name (const gchar * location);
+gchar * generate_format_string_full (gboolean show_day, gboolean show_date);
+gchar * generate_format_string_at_time (GDateTime * time);
+
+G_END_DECLS
+
+#endif