summaryrefslogtreecommitdiff
path: root/capplets/time-admin/src/time-tool.c
blob: e48f936826ef08244111bdbb2e51e2c49fc2ac70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*  time-admin 
*   Copyright (C) 2018  zhuyaliang https://github.com/zhuyaliang/
*
*   This program 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 3 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 <https://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "time-tool.h"

struct tm *GetCurrentTime(void)
{
	time_t tt;
	tzset();
	tt=time(NULL);
	
    return localtime(&tt);
}
static void UpdateDate(TimeAdmin *ta,gboolean state)
{
    struct tm *LocalTime;
    
    if(state == TRUE)
    {    
        LocalTime = GetCurrentTime();
        gtk_calendar_select_month (GTK_CALENDAR (ta->Calendar), 
                                   LocalTime->tm_mon,
                                   LocalTime->tm_year+1900);
        gtk_calendar_select_day   (GTK_CALENDAR (ta->Calendar), 
                                   LocalTime->tm_mday);
    }
}
static gboolean UpdateClock(gpointer data)
{
    TimeAdmin *ta = (TimeAdmin *)data;
    struct tm *LocalTime;
    gchar     *str;
    
    LocalTime = GetCurrentTime();
    TimeoutFlag = 1;
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(ta->HourSpin),LocalTime->tm_hour);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(ta->MinuteSpin),LocalTime->tm_min);
    str = g_strdup_printf ("%02d", LocalTime->tm_sec);
    gtk_entry_set_text(GTK_ENTRY(ta->SecondSpin),str);
    
    UpdateDate(ta,ta->NtpState); 
    gtk_calendar_mark_day(GTK_CALENDAR(ta->Calendar),LocalTime->tm_mday);
    if(LocalTime->tm_mday != ta->OldDay)
    {   
        gtk_calendar_unmark_day(GTK_CALENDAR(ta->Calendar),ta->OldDay);
        ta->OldDay = LocalTime->tm_mday;
    }
    TimeoutFlag = 0;

    g_free(str);
    return TRUE;
}
void Update_Clock_Start(TimeAdmin *ta)
{
    if(ta->UpdateTimeId <= 0)
    {
        ta->UpdateTimeId = g_timeout_add(1000,(GSourceFunc)UpdateClock,ta);
    }    

}    
void Update_Clock_Stop(TimeAdmin *ta)
{
    if(ta->UpdateTimeId > 0)
    {
        g_source_remove(ta->UpdateTimeId);
        ta->UpdateTimeId = 0;
    }    
}
gboolean GetNtpState(TimeAdmin *ta)
{
    GDBusProxy *proxy = NULL;
    GError     *error = NULL;
    GVariant   *ret;
    GVariant   *ntp;

    proxy = g_dbus_proxy_new_sync (ta->Connection,
                                   G_DBUS_PROXY_FLAGS_NONE,
                                   NULL,
                                  "org.freedesktop.timedate1",
                                  "/org/freedesktop/timedate1",
                                  "org.freedesktop.DBus.Properties",
                                   NULL,
                                   &error);
    if(proxy == NULL)
    {
        goto EXIT;
    }
 
    ret = g_dbus_proxy_call_sync (proxy,
                                 "Get",
                                  g_variant_new ("(ss)",
                                 "org.freedesktop.timedate1",
                                 "NTP"),
                                  G_DBUS_CALL_FLAGS_NONE,
                                 -1,
                                  NULL,
                                  &error);
    if(ret == NULL)
    {
        goto EXIT;
    }
    g_variant_get (ret, "(v)", &ntp);
    return g_variant_get_boolean (ntp);

EXIT:
    MessageReport(_("GetNtpState"),error->message,ERROR);
    g_error_free(error);
    return FALSE;

}   

const gchar *GetTimeZone(TimeAdmin *ta)
{
    GDBusProxy *proxy = NULL;
    GError     *error = NULL;
    GVariant   *ret;
    GVariant   *timezone;

    proxy = g_dbus_proxy_new_sync (ta->Connection,
                                   G_DBUS_PROXY_FLAGS_NONE,
                                   NULL,
                                  "org.freedesktop.timedate1",
                                  "/org/freedesktop/timedate1",
                                  "org.freedesktop.DBus.Properties",
                                   NULL,
                                   &error);
    if(proxy == NULL)
    {
        goto EXIT;
    }
 
    ret = g_dbus_proxy_call_sync (proxy,
                                 "Get",
                                  g_variant_new ("(ss)",
                                 "org.freedesktop.timedate1",
                                 "Timezone"),
                                  G_DBUS_CALL_FLAGS_NONE,
                                 -1,
                                  NULL,
                                  &error);
    if(ret == NULL)
    {
        goto EXIT;
    }
    g_variant_get (ret, "(v)", &timezone);
    return g_variant_get_string (timezone,0);

EXIT:
    g_error_free(error);
    return NULL;

}

void SetTimeZone(GDBusProxy *proxy,const char *zone)
{
    GError *error = NULL;
    GVariant *ret;
    
    ret = g_dbus_proxy_call_sync (proxy,
                                 "SetTimezone",
                                  g_variant_new ("(sb)",zone,1),
                                  G_DBUS_CALL_FLAGS_NONE,
                                  -1,
                                  NULL,
                                  &error);

    if(ret == NULL)
    {
        MessageReport(_("Set time zone"),error->message,ERROR);
    }    
}    
static void ChangeSpinBttonState(TimeAdmin *ta,gboolean State)
{
    gtk_widget_set_sensitive(ta->SaveButton,!State);
    SetTooltip(ta->SaveButton,!ta->NtpState); 
    gtk_widget_set_sensitive(ta->HourSpin,  !State);
    SetTooltip(ta->HourSpin,!ta->NtpState); 
    gtk_widget_set_sensitive(ta->MinuteSpin,!State);
    SetTooltip(ta->MinuteSpin,!ta->NtpState); 
    gtk_widget_set_sensitive(ta->SecondSpin,!State);
    SetTooltip(ta->SecondSpin,!ta->NtpState); 
    gtk_widget_set_sensitive(ta->Calendar,  !State);
    SetTooltip(ta->Calendar,!ta->NtpState); 
}

void ReloadNtp(GDBusProxy *proxy,gboolean state)
{
    GError *error = NULL;
    GVariant *ret1,*ret2;
   
    ret1 = g_dbus_proxy_call_sync (proxy,
                                 "SetNTP",
                                  g_variant_new ("(bb)",!state,!state),
                                  G_DBUS_CALL_FLAGS_NONE,
                                  -1,
                                  NULL,
                                  &error);

    ret2 = g_dbus_proxy_call_sync (proxy,
                                 "SetNTP",
                                  g_variant_new ("(bb)",state,state),
                                  G_DBUS_CALL_FLAGS_NONE,
                                  -1,
                                  NULL,
                                  &error);

    if(ret1 == NULL || ret2 == NULL)
    {
        MessageReport(_("Reload Ntp sync"),error->message,ERROR);
        g_error_free(error);
    }    

}    
gboolean ChangeNtpSync(GtkSwitch *widget,gboolean state,gpointer data)
{
    TimeAdmin *ta = (TimeAdmin *)data;
    GError *error = NULL;
    GVariant *ret;
   
    ret = g_dbus_proxy_call_sync (ta->proxy,
                                 "SetNTP",
                                  g_variant_new ("(bb)",state,state),
                                  G_DBUS_CALL_FLAGS_NONE,
                                  -1,
                                  NULL,
                                  &error);

    if(ret == NULL)
    {
        MessageReport(_("Set Ntp sync"),error->message,ERROR);
        g_error_free(error);
        return TRUE;
    }    
    else
    {
        ta->NtpState = state;
        ChangeSpinBttonState(ta,state);
        Update_Clock_Start(ta);
        UpdateDate(ta,state);
    }    
    return FALSE;
}   
static guint GetTimeStamp(TimeAdmin *ta)
{
    guint year,month,day,hour,min,sec;
    GDateTime *dt;
    char      *st;
    
    gtk_calendar_get_date(GTK_CALENDAR(ta->Calendar),&year,&month,&day);
    hour = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ta->HourSpin));
    min  = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ta->MinuteSpin));
    sec = atoi(gtk_entry_get_text(GTK_ENTRY(ta->SecondSpin)));

    dt = g_date_time_new_local(year,month+1,day,hour,min,sec);
    st = g_date_time_format(dt,"%s");
    return atoi(st);

}
static void SetTime(GDBusProxy *proxy,gint64 TimeSec)
{
    GError *error = NULL;
    GVariant *ret;
    
    ret = g_dbus_proxy_call_sync (proxy,
                                 "SetTime",
                                  g_variant_new ("(xbb)",TimeSec * 1000 * 1000,0,0),
                                  G_DBUS_CALL_FLAGS_NONE,
                                  -1,
                                  NULL,
                                  &error);

    if(ret == NULL)
    {
        MessageReport(_("Set Ntp sync"),error->message,ERROR);
        g_error_free(error);
    }    
}    
void SaveModifyTime (GtkButton *button,gpointer data)
{
    TimeAdmin *ta = (TimeAdmin *)data;
    guint    ts;

    if(ta->NtpState == FALSE)
    {
        ts = GetTimeStamp(ta);
        SetTime(ta->proxy,ts);
    }
    else
    {
        MessageReport(_("Set Time"),_("NTP synchronization has been started, the modification is invalid"),INFOR);
    }    
}