|
1 /* |
|
2 Copyright 2010 Mikael Berthe |
|
3 |
|
4 Module "clock" -- Displays date and time |
|
5 |
|
6 Options: |
|
7 - clock_precision_onesec: boolean (default: 0) |
|
8 If true, the clock will be updated every second, |
|
9 if false, the clock is updated once per minute. |
|
10 - clock_strfmt: string (default: "%Y-%m-%d %H:%M") |
|
11 strftime format string. |
|
12 |
|
13 This module is free software: you can redistribute it and/or modify |
|
14 it under the terms of the GNU General Public License as published by |
|
15 the Free Software Foundation, either version 2 of the License, or |
|
16 (at your option) any later version. |
|
17 |
|
18 This program is distributed in the hope that it will be useful, |
|
19 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 GNU General Public License for more details. |
|
22 |
|
23 You should have received a copy of the GNU General Public License |
|
24 along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 */ |
|
26 |
|
27 #include <time.h> |
|
28 |
|
29 #include <mcabber/modules.h> |
|
30 #include <mcabber/settings.h> |
|
31 #include <mcabber/screen.h> |
|
32 |
|
33 static void clock_init(void); |
|
34 static void clock_uninit(void); |
|
35 |
|
36 /* Module description */ |
|
37 module_info_t info_clock = { |
|
38 .mcabber_version = "0.10.0", |
|
39 .requires = NULL, |
|
40 .init = clock_init, |
|
41 .uninit = clock_uninit, |
|
42 }; |
|
43 |
|
44 static guint srcno = 0; |
|
45 static gboolean precision_onesec; |
|
46 static gchar *backup_info; |
|
47 static gchar *strfmt; |
|
48 static const gchar dfltstrfmt[] = "%Y-%m-%d %H:%M"; // Default string format |
|
49 |
|
50 static gboolean clock_cb(void) |
|
51 { |
|
52 char buf[256]; |
|
53 time_t now_t; |
|
54 struct tm *now; |
|
55 |
|
56 time(&now_t); |
|
57 now = localtime(&now_t); |
|
58 strftime(buf, sizeof(buf), strfmt, now); |
|
59 settings_set(SETTINGS_TYPE_OPTION, "info", buf); |
|
60 scr_UpdateChatStatus(TRUE); |
|
61 |
|
62 if (precision_onesec) |
|
63 return TRUE; // Let's be called again in 1 second |
|
64 |
|
65 // Set up new timeout event |
|
66 srcno = g_timeout_add_seconds((now->tm_sec < 60 ? 60 - now->tm_sec : 1), |
|
67 (GSourceFunc)clock_cb, NULL); |
|
68 return FALSE; // Destroy the old timeout |
|
69 } |
|
70 |
|
71 static void clock_setup_timer(gboolean activate) |
|
72 { |
|
73 if ((activate && srcno) || (!activate && !srcno)) |
|
74 return; |
|
75 |
|
76 if (activate) { |
|
77 srcno = g_timeout_add_seconds(1, (GSourceFunc)clock_cb, NULL); |
|
78 } else { |
|
79 g_source_remove(srcno); |
|
80 srcno = 0; |
|
81 } |
|
82 } |
|
83 |
|
84 //static void do_clock(char *args) |
|
85 //{ |
|
86 //} |
|
87 |
|
88 /* Initialization */ |
|
89 static void clock_init(void) |
|
90 { |
|
91 backup_info = g_strdup(settings_opt_get("info")); |
|
92 precision_onesec = settings_opt_get_int("clock_precision_onesec"); |
|
93 strfmt = g_strdup(settings_opt_get("clock_strfmt")); |
|
94 if (!strfmt) |
|
95 strfmt = g_strdup(dfltstrfmt); |
|
96 clock_setup_timer(TRUE); |
|
97 } |
|
98 |
|
99 /* Deinitialization */ |
|
100 static void clock_uninit(void) |
|
101 { |
|
102 clock_setup_timer(FALSE); |
|
103 settings_set(SETTINGS_TYPE_OPTION, "info", backup_info); |
|
104 g_free(backup_info); |
|
105 g_free(strfmt); |
|
106 backup_info = strfmt = NULL; // probably useless... |
|
107 scr_UpdateChatStatus(TRUE); |
|
108 } |
|
109 |
|
110 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |