New module: clock
authorMikael Berthe <mikael@lilotux.net>
Tue, 09 Mar 2010 21:47:34 +0100
changeset 6 c2ad0b151a5e
parent 5 fbfc64c5d6be
child 7 ad87c711de84
New module: clock
Makefile.am
clock/Makefile.am
clock/clock.c
configure.ac
--- a/Makefile.am	Mon Mar 08 22:00:09 2010 +0100
+++ b/Makefile.am	Tue Mar 09 21:47:34 2010 +0100
@@ -1,1 +1,1 @@
-SUBDIRS = comment extsay
+SUBDIRS = comment extsay clock
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clock/Makefile.am	Tue Mar 09 21:47:34 2010 +0100
@@ -0,0 +1,11 @@
+
+if INSTALL_MODULE_CLOCK
+
+pkglib_LTLIBRARIES = libclock.la
+libclock_la_SOURCES = clock.c
+libclock_la_LDFLAGS = -module -avoid-version -shared
+
+LDADD = $(GLIB_LIBS) $(MCABBER_LIBS)
+AM_CPPFLAGS = -I$(top_srcdir) $(GLIB_CFLAGS) $(MCABBER_CFLAGS)
+
+endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clock/clock.c	Tue Mar 09 21:47:34 2010 +0100
@@ -0,0 +1,110 @@
+/*
+   Copyright 2010 Mikael Berthe
+
+  Module "clock"      -- Displays date and time
+
+Options:
+- clock_precision_onesec: boolean (default: 0)
+  If true, the clock will be updated every second,
+  if false, the clock is updated once per minute.
+- clock_strfmt: string (default: "%Y-%m-%d %H:%M")
+  strftime format string.
+
+This module is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY 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 <time.h>
+
+#include <mcabber/modules.h>
+#include <mcabber/settings.h>
+#include <mcabber/screen.h>
+
+static void clock_init(void);
+static void clock_uninit(void);
+
+/* Module description */
+module_info_t info_clock = {
+        .mcabber_version = "0.10.0",
+        .requires        = NULL,
+        .init            = clock_init,
+        .uninit          = clock_uninit,
+};
+
+static guint srcno = 0;
+static gboolean precision_onesec;
+static gchar *backup_info;
+static gchar *strfmt;
+static const gchar dfltstrfmt[] = "%Y-%m-%d %H:%M"; // Default string format
+
+static gboolean clock_cb(void)
+{
+  char buf[256];
+  time_t now_t;
+  struct tm *now;
+
+  time(&now_t);
+  now = localtime(&now_t);
+  strftime(buf, sizeof(buf), strfmt, now);
+  settings_set(SETTINGS_TYPE_OPTION, "info", buf);
+  scr_UpdateChatStatus(TRUE);
+
+  if (precision_onesec)
+    return TRUE;  // Let's be called again in 1 second
+
+  // Set up new timeout event
+  srcno = g_timeout_add_seconds((now->tm_sec < 60 ? 60 - now->tm_sec : 1),
+                                (GSourceFunc)clock_cb, NULL);
+  return FALSE;   // Destroy the old timeout
+}
+
+static void clock_setup_timer(gboolean activate)
+{
+  if ((activate && srcno) || (!activate && !srcno))
+    return;
+
+  if (activate) {
+    srcno = g_timeout_add_seconds(1, (GSourceFunc)clock_cb, NULL);
+  } else {
+    g_source_remove(srcno);
+    srcno = 0;
+  }
+}
+
+//static void do_clock(char *args)
+//{
+//}
+
+/* Initialization */
+static void clock_init(void)
+{
+  backup_info = g_strdup(settings_opt_get("info"));
+  precision_onesec = settings_opt_get_int("clock_precision_onesec");
+  strfmt = g_strdup(settings_opt_get("clock_strfmt"));
+  if (!strfmt)
+    strfmt = g_strdup(dfltstrfmt);
+  clock_setup_timer(TRUE);
+}
+
+/* Deinitialization */
+static void clock_uninit(void)
+{
+  clock_setup_timer(FALSE);
+  settings_set(SETTINGS_TYPE_OPTION, "info", backup_info);
+  g_free(backup_info);
+  g_free(strfmt);
+  backup_info = strfmt = NULL; // probably useless...
+  scr_UpdateChatStatus(TRUE);
+}
+
+/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */
--- a/configure.ac	Mon Mar 08 22:00:09 2010 +0100
+++ b/configure.ac	Tue Mar 09 21:47:34 2010 +0100
@@ -97,6 +97,9 @@
               AC_HELP_STRING([--enable-all-modules], [enable all modules]),
               enable_all_modules=$enableval)
 
+AC_ARG_ENABLE(module-clock,
+              AC_HELP_STRING([--enable-module-clock], [enable module clock]),
+              enable_module_clock=$enableval)
 AC_ARG_ENABLE(module-comment,
               AC_HELP_STRING([--enable-module-comment], [enable module comment]),
               enable_module_comment=$enableval)
@@ -104,6 +107,10 @@
               AC_HELP_STRING([--enable-module-extsay], [enable module extsay]),
               enable_module_extsay=$enableval)
 
+AM_CONDITIONAL([INSTALL_MODULE_CLOCK],
+               [test x"${enable_all_modules}" = x"yes" -o \
+                     x"${enable_module_clock}" = x"yes"])
+
 AM_CONDITIONAL([INSTALL_MODULE_COMMENT],
                [test x"${enable_all_modules}" = x"yes" -o \
                      x"${enable_module_comment}" = x"yes"])
@@ -112,7 +119,8 @@
                [test x"${enable_all_modules}" = x"yes" -o \
                      x"${enable_module_extsay}" = x"yes"])
 
-AC_CONFIG_FILES([comment/Makefile
+AC_CONFIG_FILES([clock/Makefile
+                 comment/Makefile
                  extsay/Makefile
                  Makefile])
 AC_OUTPUT