author | Mikael Berthe <mikael@lilotux.net> |
Sat, 03 Apr 2010 15:17:15 +0200 | |
changeset 26 | a15aa7a218ec |
parent 9 | 3a4ddded06f2 |
child 37 | a05815df848c |
permissions | -rw-r--r-- |
6 | 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 = { |
|
8
53b0091ecd47
Update modules to the new API scheme
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
38 |
.branch = MCABBER_BRANCH, |
53b0091ecd47
Update modules to the new API scheme
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
39 |
.api = MCABBER_API_VERSION, |
53b0091ecd47
Update modules to the new API scheme
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
40 |
.version = "1.00", |
53b0091ecd47
Update modules to the new API scheme
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
41 |
.description = "Simple clock module\n" |
53b0091ecd47
Update modules to the new API scheme
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
42 |
"Uses the 'info' option to display the time.", |
53b0091ecd47
Update modules to the new API scheme
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
43 |
.requires = NULL, |
53b0091ecd47
Update modules to the new API scheme
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
44 |
.init = clock_init, |
53b0091ecd47
Update modules to the new API scheme
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
45 |
.uninit = clock_uninit, |
53b0091ecd47
Update modules to the new API scheme
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
46 |
.next = NULL, |
6 | 47 |
}; |
48 |
||
49 |
static guint srcno = 0; |
|
50 |
static gboolean precision_onesec; |
|
51 |
static gchar *backup_info; |
|
52 |
static gchar *strfmt; |
|
53 |
static const gchar dfltstrfmt[] = "%Y-%m-%d %H:%M"; // Default string format |
|
54 |
||
55 |
static gboolean clock_cb(void) |
|
56 |
{ |
|
57 |
char buf[256]; |
|
58 |
time_t now_t; |
|
59 |
struct tm *now; |
|
60 |
||
61 |
time(&now_t); |
|
62 |
now = localtime(&now_t); |
|
63 |
strftime(buf, sizeof(buf), strfmt, now); |
|
64 |
settings_set(SETTINGS_TYPE_OPTION, "info", buf); |
|
9 | 65 |
scr_update_chat_status(TRUE); |
6 | 66 |
|
67 |
if (precision_onesec) |
|
68 |
return TRUE; // Let's be called again in 1 second |
|
69 |
||
70 |
// Set up new timeout event |
|
71 |
srcno = g_timeout_add_seconds((now->tm_sec < 60 ? 60 - now->tm_sec : 1), |
|
72 |
(GSourceFunc)clock_cb, NULL); |
|
73 |
return FALSE; // Destroy the old timeout |
|
74 |
} |
|
75 |
||
76 |
static void clock_setup_timer(gboolean activate) |
|
77 |
{ |
|
78 |
if ((activate && srcno) || (!activate && !srcno)) |
|
79 |
return; |
|
80 |
||
81 |
if (activate) { |
|
82 |
srcno = g_timeout_add_seconds(1, (GSourceFunc)clock_cb, NULL); |
|
83 |
} else { |
|
84 |
g_source_remove(srcno); |
|
85 |
srcno = 0; |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
//static void do_clock(char *args) |
|
90 |
//{ |
|
91 |
//} |
|
92 |
||
93 |
/* Initialization */ |
|
94 |
static void clock_init(void) |
|
95 |
{ |
|
96 |
backup_info = g_strdup(settings_opt_get("info")); |
|
97 |
precision_onesec = settings_opt_get_int("clock_precision_onesec"); |
|
98 |
strfmt = g_strdup(settings_opt_get("clock_strfmt")); |
|
99 |
if (!strfmt) |
|
100 |
strfmt = g_strdup(dfltstrfmt); |
|
101 |
clock_setup_timer(TRUE); |
|
102 |
} |
|
103 |
||
104 |
/* Deinitialization */ |
|
105 |
static void clock_uninit(void) |
|
106 |
{ |
|
107 |
clock_setup_timer(FALSE); |
|
108 |
settings_set(SETTINGS_TYPE_OPTION, "info", backup_info); |
|
109 |
g_free(backup_info); |
|
110 |
g_free(strfmt); |
|
111 |
backup_info = strfmt = NULL; // probably useless... |
|
9 | 112 |
scr_update_chat_status(TRUE); |
6 | 113 |
} |
114 |
||
115 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |