diff options
-rw-r--r-- | capplets/time-admin/Makefile.am | 3 | ||||
-rw-r--r-- | capplets/time-admin/data/Makefile.am | 49 | ||||
-rw-r--r-- | capplets/time-admin/data/mate-time-admin.desktop.in | 13 | ||||
-rw-r--r-- | capplets/time-admin/src/Makefile.am | 20 | ||||
-rw-r--r-- | capplets/time-admin/src/main.c | 427 | ||||
-rw-r--r-- | capplets/time-admin/src/time-map.c | 638 | ||||
-rw-r--r-- | capplets/time-admin/src/time-map.h | 68 | ||||
-rw-r--r-- | capplets/time-admin/src/time-share.c | 154 | ||||
-rw-r--r-- | capplets/time-admin/src/time-share.h | 95 | ||||
-rw-r--r-- | capplets/time-admin/src/time-tool.c | 317 | ||||
-rw-r--r-- | capplets/time-admin/src/time-tool.h | 46 | ||||
-rw-r--r-- | capplets/time-admin/src/time-zone.c | 661 | ||||
-rw-r--r-- | capplets/time-admin/src/time-zone.h | 76 |
13 files changed, 2567 insertions, 0 deletions
diff --git a/capplets/time-admin/Makefile.am b/capplets/time-admin/Makefile.am new file mode 100644 index 00000000..22a7feac --- /dev/null +++ b/capplets/time-admin/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS = data src po-timezones + +-include $(top_srcdir)/git.mk diff --git a/capplets/time-admin/data/Makefile.am b/capplets/time-admin/data/Makefile.am new file mode 100644 index 00000000..f7b82e08 --- /dev/null +++ b/capplets/time-admin/data/Makefile.am @@ -0,0 +1,49 @@ +mapdir = $(datadir)/mate-time-admin/map +dist_map_DATA = \ +map/backward map/timezone_12.png map/timezone_4.5_dim.png map/timezone_6.png \ +map/bg_dim.png map/timezone_13_dim.png map/timezone_-4.5.png map/timezone_-7_dim.png \ +map/bg.png map/timezone_13.png map/timezone_4.5.png map/timezone_7_dim.png \ +map/cc.png map/timezone_14_dim.png map/timezone_-4_dim.png map/timezone_-7.png \ +map/pin.png map/timezone_14.png map/timezone_4_dim.png map/timezone_7.png \ +map/timezone_0_dim.png map/timezone_-1_dim.png map/timezone_-4.png map/timezone_8.5_dim.png \ +map/timezone_0.png map/timezone_1_dim.png map/timezone_4.png map/timezone_8.5.png \ +map/timezone_10.5_dim.png map/timezone_-1.png map/timezone_-5.5_dim.png map/timezone_8.75_dim.png \ +map/timezone_10.5.png map/timezone_1.png map/timezone_5.5_dim.png map/timezone_8.75.png \ +map/timezone_-10_dim.png map/timezone_-2_dim.png map/timezone_-5.5.png map/timezone_-8_dim.png \ +map/timezone_10_dim.png map/timezone_2_dim.png map/timezone_5.5.png map/timezone_8_dim.png \ +map/timezone_-10.png map/timezone_-2.png map/timezone_5.75_dim.png map/timezone_-8.png \ +map/timezone_10.png map/timezone_2.png map/timezone_5.75.png map/timezone_8.png \ +map/timezone_11.5_dim.png map/timezone_-3.5_dim.png map/timezone_-5_dim.png map/timezone_-9.5_dim.png \ +map/timezone_11.5.png map/timezone_3.5_dim.png map/timezone_5_dim.png map/timezone_9.5_dim.png \ +map/timezone_-11_dim.png map/timezone_-3.5.png map/timezone_-5.png map/timezone_-9.5.png \ +map/timezone_11_dim.png map/timezone_3.5.png map/timezone_5.png map/timezone_9.5.png \ +map/timezone_-11.png map/timezone_-3_dim.png map/timezone_6.5_dim.png map/timezone_-9_dim.png \ +map/timezone_11.png map/timezone_3_dim.png map/timezone_6.5.png map/timezone_9_dim.png \ +map/timezone_12.75_dim.png map/timezone_-3.png map/timezone_-6_dim.png map/timezone_-9.png \ +map/timezone_12.75.png map/timezone_3.png map/timezone_6_dim.png map/timezone_9.png \ +map/timezone_12_dim.png map/timezone_-4.5_dim.png map/timezone_-6.png + +icons16dir = $(datadir)/icons/hicolor/16x16/apps +dist_icons16_DATA = hicolor/16x16/apps/mate-times-admin.png +icons22dir = $(datadir)/icons/hicolor/22x22/apps +dist_icons22_DATA = hicolor/22x22/apps/mate-times-admin.png +icons24dir = $(datadir)/icons/hicolor/24x24/apps +dist_icons24_DATA = hicolor/24x24/apps/mate-times-admin.png +icons32dir = $(datadir)/icons/hicolor/32x32/apps +dist_icons32_DATA = hicolor/32x32/apps/mate-times-admin.png +icons48dir = $(datadir)/icons/hicolor/48x48/apps +dist_icons48_DATA = hicolor/48x48/apps/mate-times-admin.png + +icondir = $(datadir)/mate-time-admin/icon +dist_icon_DATA = icon/mate-times-admin.png + +@INTLTOOL_DESKTOP_RULE@ + +desktopdir = $(datadir)/applications +desktop_in_files = mate-time-admin.desktop.in +desktop_DATA = $(desktop_in_files:.desktop.in=.desktop) + +CLEANFILES = $(desktop_DATA) +EXTRA_DIST = \ + $(desktop_in_files) +-include $(top_srcdir)/git.mk diff --git a/capplets/time-admin/data/mate-time-admin.desktop.in b/capplets/time-admin/data/mate-time-admin.desktop.in new file mode 100644 index 00000000..370f5044 --- /dev/null +++ b/capplets/time-admin/data/mate-time-admin.desktop.in @@ -0,0 +1,13 @@ +[Desktop Entry] +Name=Time And Date Manager +Name[zh_CN]=时间和日期管理 +Comment=Manage Time And Date +Comment[zh_CN]=管理时间和日期。 +Terminal=false +Type=Application +Exec=mate-time-admin +Icon=mate-times-admin +StartupNotify=true +Categories=GTK;Settings;HardwareSettings; +Keywords=mate-control-center;MATE;clock;date;time;preferences; +OnlyShowIn=MATE; diff --git a/capplets/time-admin/src/Makefile.am b/capplets/time-admin/src/Makefile.am new file mode 100644 index 00000000..4898365c --- /dev/null +++ b/capplets/time-admin/src/Makefile.am @@ -0,0 +1,20 @@ +cappletname = time-admin + +bin_PROGRAMS = mate-time-admin + +mate_time_admin_SOURCES = \ + main.c time-map.c time-share.c time-tool.c time-zone.c \ + time-map.h time-share.h time-tool.h time-zone.h + +mate_time_admin_LDADD = $(MATECC_CAPPLETS_LIBS) +mate_time_admin_LDFLAGS = -export-dynamic + +AM_CPPFLAGS = \ + $(MATECC_CAPPLETS_CFLAGS) \ + -DDATADIR="\"$(datadir)\"" \ + -DMATELOCALEDIR="\"$(datadir)/locale\"" + + +CLEANFILES = $(MATECC_CAPPLETS_CLEANFILES) + +-include $(top_srcdir)/git.mk diff --git a/capplets/time-admin/src/main.c b/capplets/time-admin/src/main.c new file mode 100644 index 00000000..5952b71a --- /dev/null +++ b/capplets/time-admin/src/main.c @@ -0,0 +1,427 @@ +/* mate-user-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/>. +*/ + +#include "time-tool.h" +#include "time-zone.h" +#include "time-map.h" + +#define LOCKFILE "/tmp/time-admin.pid" +#define TIME_ADMIN_PERMISSION "org.mate.user.admin.administration" +#define ICONFILE "/usr/share/mate-time-admin/icon/mate-times-admin.png" + +static gboolean CheckClockHealth(gpointer data) +{ + TimeAdmin *ta = (TimeAdmin *)data; + Update_Clock_Start(ta); + ta->ApplyId = 0; + + return FALSE; +} +static void update_apply_timeout(TimeAdmin *ta) +{ + Update_Clock_Stop(ta); + if (ta->ApplyId > 0) + { + g_source_remove (ta->ApplyId); + ta->ApplyId = 0; + } + ta->ApplyId = g_timeout_add (10000, (GSourceFunc)CheckClockHealth,ta); +} +static void ChangeTimeValue(GtkSpinButton *spin_button, + gpointer *data) +{ + TimeAdmin *ta = (TimeAdmin *)data; + if(TimeoutFlag == 0) + { + update_apply_timeout(ta); + } +} +static GdkPixbuf * GetAppIcon(void) +{ + GdkPixbuf *Pixbuf; + GError *Error = NULL; + + Pixbuf = gdk_pixbuf_new_from_file(ICONFILE,&Error); + if(!Pixbuf) + { + MessageReport(("Get Icon Fail"),Error->message,ERROR); + g_error_free(Error); + } + + return Pixbuf; +} +static gboolean on_window_quit (GtkWidget *widget, + GdkEvent *event, + gpointer user_data) +{ + TimeAdmin *ta = (TimeAdmin *)user_data; + + QuitApp(ta); + return TRUE; +} +static void CloseWindow (GtkButton *button,gpointer data) +{ + TimeAdmin *ta = (TimeAdmin *)data; + + QuitApp(ta); +} +static void InitMainWindow(TimeAdmin *ta) +{ + GtkWidget *Window; + GdkPixbuf *AppIcon; + //GError *error = NULL; + + Window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + ta->MainWindow = WindowLogin = Window; + gtk_window_set_deletable(GTK_WINDOW(Window),FALSE); + gtk_window_set_hide_titlebar_when_maximized(GTK_WINDOW(Window),TRUE); + gtk_window_set_position(GTK_WINDOW(Window), GTK_WIN_POS_CENTER); + gtk_window_set_title(GTK_WINDOW(Window),_("Time and Date Manager")); + gtk_container_set_border_width(GTK_CONTAINER(Window),10); + gtk_widget_set_size_request(Window, 300, 360); + g_signal_connect(G_OBJECT(Window), + "delete-event", + G_CALLBACK(on_window_quit), + ta); + + /* + ua->Permission = polkit_permission_new_sync (USER_ADMIN_PERMISSION, NULL, NULL, &error); + ua->ButtonLock = gtk_lock_button_new(ua->Permission); + gtk_lock_button_set_permission(GTK_LOCK_BUTTON (ua->ButtonLock),ua->Permission); + g_signal_connect(ua->Permission, + "notify", + G_CALLBACK (on_permission_changed), + ua); + */ + AppIcon = GetAppIcon(); + if(AppIcon) + { + gtk_window_set_icon(GTK_WINDOW(Window),AppIcon); + g_object_unref(AppIcon); + } +} + +static int RecordPid(void) +{ + int pid = 0; + int fd; + int Length = 0; + char WriteBuf[30] = { 0 }; + fd = open(LOCKFILE,O_WRONLY|O_CREAT|O_TRUNC,0777); + if(fd < 0) + { + MessageReport(_("open file"),_("Create pid file failed"),ERROR); + return -1; + } + chmod(LOCKFILE,0777); + pid = getpid(); + sprintf(WriteBuf,"%d",pid); + Length = write(fd,WriteBuf,strlen(WriteBuf)); + if(Length <= 0 ) + { + MessageReport(_("write file"),_("write pid file failed"),ERROR); + return -1; + } + close(fd); + + return 0; +} +/****************************************************************************** +* Function: ProcessRuning +* +* Explain: Check whether the process has been started,If the process is not started, +* record the current process ID =====>"/tmp/user-admin.pid" +* +* Input: +* +* +* Output: start :TRUE +* not start :FALSE +* +* Author: zhuyaliang 31/07/2018 +******************************************************************************/ +static gboolean ProcessRuning(void) +{ + int fd = 0; + int pid = 0; + gboolean Run = FALSE; + char ReadBuf[30] = { 0 }; + + if(access(LOCKFILE,F_OK) == 0) + { + fd = open(LOCKFILE,O_RDONLY); + if(fd < 0) + { + MessageReport(_("open file"),_("open pid file failed"),ERROR); + return TRUE; + } + if(read(fd,ReadBuf,sizeof(ReadBuf)) <= 0) + { + MessageReport(_("read file"),_("read pid file failed"),ERROR); + goto ERROREXIT; + } + pid = atoi(ReadBuf); + if(kill(pid,0) == 0) + { + goto ERROREXIT; + } + } + + if(RecordPid() < 0) + Run = TRUE; + + return Run; +ERROREXIT: + close(fd); + return TRUE; + +} +static char *translate(const char *value) +{ + g_autofree gchar *zone_translated = NULL; + char *name; + + zone_translated = g_strdup (dgettext (GETTEXT_PACKAGE_TIMEZONES,value)); + name = g_strdup_printf (C_("timezone loc", "%s"),zone_translated); + + return name; +} +static GtkWidget * TimeZoneAndNtp(TimeAdmin *ta) +{ + GtkWidget *table; + GtkWidget *TimeZoneLabel; + GtkWidget *NtpSyncLabel; + GtkWidget *NtpSyncSwitch; + const char *TimeZone; + gboolean NtpState; + char *ZoneName; + + table = gtk_grid_new(); + gtk_grid_set_column_homogeneous(GTK_GRID(table),TRUE); + + TimeZoneLabel = gtk_label_new(NULL); + gtk_widget_set_halign(TimeZoneLabel,GTK_ALIGN_START); + SetLableFontType(TimeZoneLabel,"gray",11,_("Time Zone:")); + gtk_grid_attach(GTK_GRID(table) ,TimeZoneLabel, 0 , 0 , 1 , 1); + + SetupTimezoneDialog(ta); + TimeZone = GetTimeZone(ta); + ZoneName = translate(TimeZone); + ta->TimeZoneButton = gtk_button_new_with_label(ZoneName); + g_signal_connect (ta->TimeZoneButton, + "clicked", + G_CALLBACK (RunTimeZoneDialog), + ta); + + gtk_grid_attach(GTK_GRID(table) ,ta->TimeZoneButton,1 , 0 , 3 , 1); + + NtpSyncLabel = gtk_label_new(NULL); + gtk_widget_set_halign(NtpSyncLabel,GTK_ALIGN_START); + SetLableFontType(NtpSyncLabel,"gray",11,_("Ntp Sync:")); + gtk_grid_attach(GTK_GRID(table) ,NtpSyncLabel, 0 , 1 , 1 , 1); + + NtpSyncSwitch = gtk_switch_new(); + NtpState = GetNtpState(ta); + if(NtpState) + { + ReloadNtp(ta->proxy,NtpState); + } + ta->NtpState = NtpState; + gtk_switch_set_state (GTK_SWITCH(NtpSyncSwitch), + NtpState); + gtk_grid_attach(GTK_GRID(table) ,NtpSyncSwitch, 1 , 1 , 1 , 1); + g_signal_connect (G_OBJECT(NtpSyncSwitch), + "state-set", + G_CALLBACK (ChangeNtpSync), + ta); + + gtk_grid_set_row_spacing(GTK_GRID(table), 6); + gtk_grid_set_column_spacing(GTK_GRID(table), 12); + + return table; + +} + +static GtkWidget *GetSpinButton(int Initial,int Maximum,TimeAdmin *ta) +{ + GtkWidget *SpinButton; + GtkAdjustment *Adjustment; + + Adjustment = gtk_adjustment_new (Initial, 0, Maximum, 1, 0, 0); + SpinButton = gtk_spin_button_new (Adjustment, 1, 0); + gtk_widget_set_sensitive(SpinButton,!ta->NtpState); + gtk_widget_set_halign(SpinButton,GTK_ALIGN_START); + gtk_spin_button_set_wrap(GTK_SPIN_BUTTON (SpinButton), TRUE); + gtk_spin_button_set_numeric(GTK_SPIN_BUTTON (SpinButton),TRUE); + gtk_widget_set_hexpand (SpinButton,TRUE); + g_signal_connect (SpinButton, + "changed", + G_CALLBACK (ChangeTimeValue), + ta); + + SetTooltip(SpinButton,!ta->NtpState); + return SpinButton; +} +static GtkWidget *SetClock(TimeAdmin *ta) +{ + GtkWidget *table; + GtkWidget *TimeLabel; + struct tm *LocalTime; + + table = gtk_grid_new(); + gtk_grid_set_column_homogeneous(GTK_GRID(table),TRUE); + + TimeLabel = gtk_label_new(NULL); + SetLableFontType(TimeLabel,"green",13,_("Set Time")); + gtk_widget_set_halign(TimeLabel,GTK_ALIGN_CENTER); + gtk_widget_set_valign(TimeLabel,GTK_ALIGN_START); + gtk_widget_set_hexpand(TimeLabel,FALSE); + gtk_grid_attach(GTK_GRID(table) ,TimeLabel, 1 , 0 , 1 , 1); + + LocalTime = GetCurrentTime(); + ta->UpdateTimeId = 0; + ta->ApplyId = 0; + + ta->HourSpin = GetSpinButton(LocalTime->tm_hour,23,ta); + gtk_grid_attach(GTK_GRID(table) ,ta->HourSpin, 0 , 1 , 1 , 1); + + ta->MinuteSpin = GetSpinButton(LocalTime->tm_min,59,ta); + gtk_grid_attach(GTK_GRID(table) ,ta->MinuteSpin, 1 , 1 , 1 , 1); + + ta->SecondSpin = GetSpinButton (LocalTime->tm_sec,59,ta); + gtk_grid_attach(GTK_GRID(table) ,ta->SecondSpin, 2 , 1 , 1 , 1); + + Update_Clock_Start(ta); + + gtk_grid_set_row_spacing(GTK_GRID(table), 6); + gtk_grid_set_column_spacing(GTK_GRID(table), 12); + + return table; +} + +static GtkWidget *SetDate(TimeAdmin *ta) +{ + GtkWidget *table; + GtkWidget *DateLabel; + struct tm *LocalTime; + + table = gtk_grid_new(); + gtk_grid_set_column_homogeneous(GTK_GRID(table),TRUE); + + DateLabel = gtk_label_new(NULL); + SetLableFontType(DateLabel,"green",12,_("Set Date")); + gtk_grid_attach(GTK_GRID(table) ,DateLabel, 1 , 0 , 1 , 1); + + LocalTime = GetCurrentTime(); + ta->Calendar = gtk_calendar_new (); + gtk_widget_set_sensitive(ta->Calendar,!ta->NtpState); + SetTooltip(ta->Calendar,!ta->NtpState); + gtk_calendar_mark_day(GTK_CALENDAR(ta->Calendar),LocalTime->tm_mday); + ta->OldDay = LocalTime->tm_mday; + gtk_grid_attach(GTK_GRID(table) ,ta->Calendar, 0 , 1 , 3 , 3); + + ta->CloseButton = gtk_button_new_with_label (_("Close")); + gtk_grid_attach(GTK_GRID(table) ,ta->CloseButton, 0 , 5 , 1 , 1); + g_signal_connect (ta->CloseButton, + "clicked", + G_CALLBACK (CloseWindow), + ta); + + ta->SaveButton = gtk_button_new_with_label (_("Save")); + gtk_widget_set_sensitive(ta->SaveButton,!ta->NtpState); + gtk_grid_attach(GTK_GRID(table) ,ta->SaveButton, 2 , 5 , 1 , 1); + g_signal_connect (ta->SaveButton, + "clicked", + G_CALLBACK (SaveModifyTime), + ta); + + gtk_grid_set_row_spacing(GTK_GRID(table), 6); + gtk_grid_set_column_spacing(GTK_GRID(table), 12); + + return table; + +} +static void CreateClockInterface(TimeAdmin *ta) +{ + GtkWidget *Vbox; + GtkWidget *Vbox1; + GtkWidget *Vbox2; + GtkWidget *Vbox3; + + Vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8); + gtk_container_add(GTK_CONTAINER(ta->MainWindow), Vbox); + + Vbox1 = TimeZoneAndNtp(ta); + gtk_box_pack_start(GTK_BOX(Vbox),Vbox1,TRUE,TRUE,8); + + Vbox2 = SetClock(ta); + gtk_box_pack_start(GTK_BOX(Vbox),Vbox2,TRUE,TRUE,8); + Vbox3 = SetDate(ta); + gtk_box_pack_start(GTK_BOX(Vbox),Vbox3,TRUE,TRUE,8); +} +static gboolean InitDbusProxy(TimeAdmin *ta) +{ + GError *error = NULL; + + ta->Connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if(ta->Connection == NULL) + { + MessageReport(_("g_bus_get_sync"),error->message,ERROR); + return FALSE; + } + ta->proxy = g_dbus_proxy_new_sync (ta->Connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + "org.freedesktop.timedate1", + "/org/freedesktop/timedate1", + "org.freedesktop.timedate1", + NULL, + &error); + if(ta->proxy == NULL) + { + MessageReport(_("g_bus_proxy_new"),error->message,ERROR); + return FALSE; + } + + return TRUE; +} +int main(int argc, char **argv) +{ + TimeAdmin ta; + + bindtextdomain (PACKAGE, LOCALEDIR); + textdomain (PACKAGE); + bind_textdomain_codeset (GETTEXT_PACKAGE_TIMEZONES, "UTF-8"); + + gtk_init(&argc, &argv); + + /* Create the main window */ + InitMainWindow(&ta); + + /* Check whether the process has been started */ + if(ProcessRuning() == TRUE) + exit(0); + if(InitDbusProxy(&ta) == FALSE) + { + exit(0); + } + CreateClockInterface(&ta); + gtk_widget_show_all(ta.MainWindow); + gtk_main(); + + return TRUE; +} diff --git a/capplets/time-admin/src/time-map.c b/capplets/time-admin/src/time-map.c new file mode 100644 index 00000000..2d86660e --- /dev/null +++ b/capplets/time-admin/src/time-map.c @@ -0,0 +1,638 @@ +/* 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-map.h" +#include <math.h> + +#define PIN_HOT_POINT_X 8 +#define PIN_HOT_POINT_Y 15 + +typedef struct +{ + gdouble offset; + guchar red; + guchar green; + guchar blue; + guchar alpha; +}TimezoneMapOffset; +enum +{ + LOCATION_CHANGED, + LAST_SIGNAL +}; + +G_DEFINE_TYPE (TimezoneMap, timezone_map, GTK_TYPE_WIDGET) +static guint signals[LAST_SIGNAL]; +static TimezoneMapOffset color_codes[] = +{ + {-11.0, 43, 0, 0, 255 }, + {-10.0, 85, 0, 0, 255 }, + {-9.5, 102, 255, 0, 255 }, + {-9.0, 128, 0, 0, 255 }, + {-8.0, 170, 0, 0, 255 }, + {-7.0, 212, 0, 0, 255 }, + {-6.0, 255, 0, 1, 255 }, // north + {-6.0, 255, 0, 0, 255 }, // south + {-5.0, 255, 42, 42, 255 }, + {-4.5, 192, 255, 0, 255 }, + {-4.0, 255, 85, 85, 255 }, + {-3.5, 0, 255, 0, 255 }, + {-3.0, 255, 128, 128, 255 }, + {-2.0, 255, 170, 170, 255 }, + {-1.0, 255, 213, 213, 255 }, + {0.0, 43, 17, 0, 255 }, + {1.0, 85, 34, 0, 255 }, + {2.0, 128, 51, 0, 255 }, + {3.0, 170, 68, 0, 255 }, + {3.5, 0, 255, 102, 255 }, + {4.0, 212, 85, 0, 255 }, + {4.5, 0, 204, 255, 255 }, + {5.0, 255, 102, 0, 255 }, + {5.5, 0, 102, 255, 255 }, + {5.75, 0, 238, 207, 247 }, + {6.0, 255, 127, 42, 255 }, + {6.5, 204, 0, 254, 254 }, + {7.0, 255, 153, 85, 255 }, + {8.0, 255, 179, 128, 255 }, + {9.0, 255, 204, 170, 255 }, + {9.5, 170, 0, 68, 250 }, + {10.0, 255, 230, 213, 255 }, + {10.5, 212, 124, 21, 250 }, + {11.0, 212, 170, 0, 255 }, + {11.5, 249, 25, 87, 253 }, + {12.0, 255, 204, 0, 255 }, + {12.75, 254, 74, 100, 248 }, + {13.0, 255, 85, 153, 250 }, + {-100, 0, 0, 0, 0 } +}; +static void +cc_timezone_map_dispose (GObject *object) +{ + TimezoneMap *self = TIMEZONEMAP (object); + + g_clear_object (&self->orig_background); + g_clear_object (&self->orig_background_dim); + g_clear_object (&self->orig_color_map); + g_clear_object (&self->background); + g_clear_object (&self->pin); + g_clear_pointer (&self->bubble_text, g_free); + + if (self->color_map) + { + g_clear_object (&self->color_map); + + self->visible_map_pixels = NULL; + self->visible_map_rowstride = 0; + } + + G_OBJECT_CLASS (timezone_map_parent_class)->dispose (object); +} +static void +timezone_map_finalize (GObject *object) +{ + TimezoneMap *self = TIMEZONEMAP (object); + + g_clear_pointer (&self->tzdb, TimeZoneDateBaseFree); + + G_OBJECT_CLASS (timezone_map_parent_class)->finalize (object); +} +static void +cc_timezone_map_get_preferred_width (GtkWidget *widget, + gint *minimum, + gint *natural) +{ + TimezoneMap *map = TIMEZONEMAP (widget); + gint size; + + size = gdk_pixbuf_get_width (map->orig_background); + + if (minimum != NULL) + *minimum = size; + if (natural != NULL) + *natural = size; +} + +static void +cc_timezone_map_get_preferred_height (GtkWidget *widget, + gint *minimum, + gint *natural) +{ + TimezoneMap *map = TIMEZONEMAP (widget); + gint size; + + size = gdk_pixbuf_get_height (map->orig_background); + + if (minimum != NULL) + *minimum = size; + if (natural != NULL) + *natural = size; +} +static void +cc_timezone_map_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + TimezoneMap *map = TIMEZONEMAP (widget); + GdkPixbuf *pixbuf; + + if (map->background) + g_object_unref (map->background); + + if (!gtk_widget_is_sensitive (widget)) + pixbuf = map->orig_background_dim; + else + pixbuf = map->orig_background; + + map->background = gdk_pixbuf_scale_simple (pixbuf, + allocation->width, + allocation->height, + GDK_INTERP_BILINEAR); + + if (map->color_map) + g_object_unref (map->color_map); + + map->color_map = gdk_pixbuf_scale_simple (map->orig_color_map, + allocation->width, + allocation->height, + GDK_INTERP_BILINEAR); + + map->visible_map_pixels = gdk_pixbuf_get_pixels (map->color_map); + map->visible_map_rowstride = gdk_pixbuf_get_rowstride (map->color_map); + GTK_WIDGET_CLASS (timezone_map_parent_class)->size_allocate (widget, + allocation); +} + +static void +cc_timezone_map_realize (GtkWidget *widget) +{ + GdkWindowAttr attr = { 0, }; + GtkAllocation allocation; + GdkWindow *window; + + gtk_widget_get_allocation (widget, &allocation); + + gtk_widget_set_realized (widget, TRUE); + + attr.window_type = GDK_WINDOW_CHILD; + attr.wclass = GDK_INPUT_OUTPUT; + attr.width = allocation.width; + attr.height = allocation.height; + attr.x = allocation.x; + attr.y = allocation.y; + attr.event_mask = gtk_widget_get_events (widget) + | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK; + + window = gdk_window_new (gtk_widget_get_parent_window (widget), &attr, + GDK_WA_X | GDK_WA_Y); + + gdk_window_set_user_data (window, widget); + gtk_widget_set_window (widget, window); +} +static gdouble +convert_longitude_to_x (gdouble longitude, gint map_width) +{ + const gdouble xdeg_offset = -6; + gdouble x; + + x = (map_width * (180.0 + longitude) / 360.0) + + (map_width * xdeg_offset / 180.0); + + return x; +} + +static gdouble +radians (gdouble degrees) +{ + return (degrees / 360.0) * G_PI * 2; +} + +static gdouble +convert_latitude_to_y (gdouble latitude, gdouble map_height) +{ + gdouble bottom_lat = -59; + gdouble top_lat = 81; + gdouble top_per, y, full_range, top_offset, map_range; + + top_per = top_lat / 180.0; + y = 1.25 * log (tan (G_PI_4 + 0.4 * radians (latitude))); + full_range = 4.6068250867599998; + top_offset = full_range * top_per; + map_range = fabs (1.25 * log (tan (G_PI_4 + 0.4 * radians (bottom_lat))) - top_offset); + y = fabs (y - top_offset); + y = y / map_range; + y = y * map_height; + return y; +} + + +static void +draw_text_bubble (cairo_t *cr, + GtkWidget *widget, + gdouble pointx, + gdouble pointy) +{ + static const double corner_radius = 9.0; + static const double margin_top = 12.0; + static const double margin_bottom = 12.0; + static const double margin_left = 24.0; + static const double margin_right = 24.0; + + TimezoneMap *map = TIMEZONEMAP (widget); + GtkAllocation alloc; + PangoLayout *layout; + PangoRectangle text_rect; + double x; + double y; + double width; + double height; + + if (!map->bubble_text) + return; + + gtk_widget_get_allocation (widget, &alloc); + layout = gtk_widget_create_pango_layout (widget, NULL); + /* Layout the text */ + pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER); + pango_layout_set_spacing (layout, 3); + pango_layout_set_markup (layout, map->bubble_text, -1); + + pango_layout_get_pixel_extents (layout, NULL, &text_rect); + + /* Calculate the bubble size based on the text layout size */ + width = text_rect.width + margin_left + margin_right; + height = text_rect.height + margin_top + margin_bottom; + + if (pointx < alloc.width / 2) + x = pointx + 25; + else + x = pointx - width - 25; + + y = pointy - height / 2; + + /* Make sure it fits in the visible area */ + x = CLAMP (x, 0, alloc.width - width); + y = CLAMP (y, 0, alloc.height - height); + + cairo_save (cr); + cairo_translate (cr, x, y); + + /* Draw the bubble */ + cairo_new_sub_path (cr); + cairo_arc (cr, width - corner_radius, corner_radius, corner_radius, radians (-90), radians (0)); + cairo_arc (cr, width - corner_radius, height - corner_radius, corner_radius, radians (0), radians (90)); + cairo_arc (cr, corner_radius, height - corner_radius, corner_radius, radians (90), radians (180)); + cairo_arc (cr, corner_radius, corner_radius, corner_radius, radians (180), radians (270)); + cairo_close_path (cr); + + cairo_set_source_rgba (cr, 0.2, 0.2, 0.2, 0.7); + cairo_fill (cr); + + /* And finally draw the text */ + cairo_set_source_rgb (cr, 1, 1, 1); + cairo_move_to (cr, margin_left, margin_top); + pango_cairo_show_layout (cr, layout); + + g_object_unref (layout); + cairo_restore (cr); +} + +static gboolean +cc_timezone_map_draw (GtkWidget *widget, + cairo_t *cr) +{ + TimezoneMap *map = TIMEZONEMAP (widget); + g_autoptr(GdkPixbuf) orig_hilight = NULL; + GtkAllocation alloc; + g_autofree gchar *file = NULL; + g_autoptr(GError) err = NULL; + gdouble pointx, pointy; + char buf[16]; + + gtk_widget_get_allocation (widget, &alloc); + + /* paint background */ + gdk_cairo_set_source_pixbuf (cr, map->background, 0, 0); + cairo_paint (cr); + + /* paint hilight */ + if (gtk_widget_is_sensitive (widget)) + { + file = g_strdup_printf ("/usr/share/mate-time-admin/map/timezone_%s.png", + g_ascii_formatd (buf, sizeof (buf), + "%g", map->selected_offset)); + } + else + { + file = g_strdup_printf ("/usr/share/mate-time-admin/map/timezone_%s_dim.png", + g_ascii_formatd (buf, sizeof (buf), + "%g", map->selected_offset)); + } + + orig_hilight = gdk_pixbuf_new_from_file (file, &err); + + if (!orig_hilight) + { + g_warning ("Could not load hilight: %s", + (err) ? err->message : "Unknown Error"); + } + else + { + g_autoptr(GdkPixbuf) hilight = NULL; + + hilight = gdk_pixbuf_scale_simple (orig_hilight, alloc.width, + alloc.height, GDK_INTERP_BILINEAR); + gdk_cairo_set_source_pixbuf (cr, hilight, 0, 0); + + cairo_paint (cr); + } + + if (map->location) + { + pointx = convert_longitude_to_x (map->location->longitude, alloc.width); + pointy = convert_latitude_to_y (map->location->latitude, alloc.height); + + pointx = CLAMP (floor (pointx), 0, alloc.width); + pointy = CLAMP (floor (pointy), 0, alloc.height); + + draw_text_bubble (cr, widget, pointx, pointy); + if (map->pin) + { + gdk_cairo_set_source_pixbuf (cr, map->pin, + pointx - PIN_HOT_POINT_X, + pointy - PIN_HOT_POINT_Y); + cairo_paint (cr); + } + } + + return TRUE; +} +static void +update_cursor (GtkWidget *widget) +{ + GdkWindow *window; + g_autoptr(GdkCursor) cursor = NULL; + + if (!gtk_widget_get_realized (widget)) + return; + + if (gtk_widget_is_sensitive (widget)) + { + GdkDisplay *display; + display = gtk_widget_get_display (widget); + cursor = gdk_cursor_new_for_display (display, GDK_HAND2); + } + + window = gtk_widget_get_window (widget); + gdk_window_set_cursor (window, cursor); +} + +static void +cc_timezone_map_state_flags_changed (GtkWidget *widget, + GtkStateFlags prev_state) +{ + update_cursor (widget); + + if (GTK_WIDGET_CLASS (timezone_map_parent_class)->state_flags_changed) + GTK_WIDGET_CLASS (timezone_map_parent_class)->state_flags_changed (widget, prev_state); +} +static gint sort_locations (TzLocation *a, + TzLocation *b) +{ + if (a->dist > b->dist) + return 1; + + if (a->dist < b->dist) + return -1; + + return 0; +} + +static void +set_location (TimezoneMap *map, + TzLocation *location) +{ + g_autoptr(TzInfo) info = NULL; + + map->location = location; + info = tz_info_from_location (map->location); + map->selected_offset = tz_location_get_utc_offset (map->location) + / (60.0*60.0) + + ((info->daylight) ? -1.0 : 0.0); + + g_signal_emit (map, signals[LOCATION_CHANGED], 0, map->location,NULL); +} + +static gboolean +button_press_event (TimezoneMap *map, + GdkEventButton *event) +{ + gint x, y; + guchar r, g, b, a; + guchar *pixels; + gint rowstride; + guint i; + + const GPtrArray *array; + gint width, height; + GList *distances = NULL; + GtkAllocation alloc; + + x = event->x; + y = event->y; + + + rowstride = map->visible_map_rowstride; + pixels = map->visible_map_pixels; + + r = pixels[(rowstride * y + x * 4)]; + g = pixels[(rowstride * y + x * 4) + 1]; + b = pixels[(rowstride * y + x * 4) + 2]; + a = pixels[(rowstride * y + x * 4) + 3]; + + + for (i = 0; color_codes[i].offset != -100; i++) + { + if (color_codes[i].red == r && color_codes[i].green == g + && color_codes[i].blue == b && color_codes[i].alpha == a) + { + map->selected_offset = color_codes[i].offset; + } + } + + gtk_widget_queue_draw (GTK_WIDGET (map)); + + /* work out the co-ordinates */ + + array = tz_get_locations (map->tzdb); + + gtk_widget_get_allocation (GTK_WIDGET (map), &alloc); + width = alloc.width; + height = alloc.height; + + for (i = 0; i < array->len; i++) + { + gdouble pointx, pointy, dx, dy; + TzLocation *loc = array->pdata[i]; + + pointx = convert_longitude_to_x (loc->longitude, width); + pointy = convert_latitude_to_y (loc->latitude, height); + + dx = pointx - x; + dy = pointy - y; + + loc->dist = dx * dx + dy * dy; + distances = g_list_prepend (distances, loc); + + } + distances = g_list_sort (distances, (GCompareFunc) sort_locations); + + set_location (map, (TzLocation*) distances->data); + + g_list_free (distances); + + return TRUE; +} + +static void +timezone_map_class_init (TimezoneMapClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + object_class->dispose = cc_timezone_map_dispose; + object_class->finalize = timezone_map_finalize; + + widget_class->get_preferred_width = cc_timezone_map_get_preferred_width; + widget_class->get_preferred_height = cc_timezone_map_get_preferred_height; + widget_class->size_allocate = cc_timezone_map_size_allocate; + widget_class->realize = cc_timezone_map_realize; + widget_class->draw = cc_timezone_map_draw; + widget_class->state_flags_changed = cc_timezone_map_state_flags_changed; + + + signals[LOCATION_CHANGED] = g_signal_new ("location-changed", + TYPE_TIMEZONE_MAP, + G_SIGNAL_RUN_FIRST, + 0, + NULL, + NULL, + g_cclosure_marshal_VOID__POINTER, + G_TYPE_NONE, 1, + G_TYPE_POINTER); +/* + signals[LOCATION_CHANGED] = g_signal_new ("location-changed", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + 0, + NULL,NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +*/ +} + +static void +timezone_map_init (TimezoneMap *map) +{ + GError *err = NULL; + + map->orig_background = gdk_pixbuf_new_from_file ("/usr/share/mate-time-admin/map/bg.png",&err); + if (!map->orig_background) + { + g_warning ("Could not load background image: %s", + (err) ? err->message : "Unknown error"); + g_clear_error (&err); + } + map->orig_background_dim = gdk_pixbuf_new_from_file("/usr/share/mate-time-admin/map/bg_dim.png",&err); + if (!map->orig_background_dim) + { + g_warning ("Could not load background image: %s", + (err) ? err->message : "Unknown error"); + g_clear_error (&err); + } + + map->orig_color_map = gdk_pixbuf_new_from_file ("/usr/share/mate-time-admin/map/cc.png",&err); + if (!map->orig_color_map) + { + g_warning ("Could not load background image: %s", + (err) ? err->message : "Unknown error"); + g_clear_error (&err); + } + + map->pin = gdk_pixbuf_new_from_file ("/usr/share/mate-time-admin/map/pin.png",&err); + if (!map->pin) + { + g_warning ("Could not load pin icon: %s", + (err) ? err->message : "Unknown error"); + g_clear_error (&err); + } + + map->tzdb = tz_load_db (); + + g_signal_connect_object (map, + "button-press-event", + G_CALLBACK (button_press_event), + map, + G_CONNECT_SWAPPED); +} +gboolean timezone_map_set_timezone (TimezoneMap *map, + const gchar *timezone) +{ + GPtrArray *locations; + guint i; + g_autofree gchar *real_tz = NULL; + gboolean ret; + + real_tz = tz_info_get_clean_name (map->tzdb, timezone); + + locations = tz_get_locations (map->tzdb); + ret = FALSE; + + for (i = 0; i < locations->len; i++) + { + TzLocation *loc = locations->pdata[i]; + + if (!g_strcmp0 (loc->zone, real_tz ? real_tz : timezone)) + { + set_location (map, loc); + ret = TRUE; + break; + } + } + + if (ret) + gtk_widget_queue_draw (GTK_WIDGET (map)); + + return ret; +} +TzLocation *timezone_map_get_location (TimezoneMap *map) +{ + return map->location; +} +void timezone_map_set_bubble_text (TimezoneMap *map, + const gchar *text) +{ + g_free (map->bubble_text); + map->bubble_text = g_strdup (text); + gtk_widget_queue_draw (GTK_WIDGET (map)); +} + +TimezoneMap * timezone_map_new (void) +{ + return g_object_new (TYPE_TIMEZONE_MAP, NULL); +} diff --git a/capplets/time-admin/src/time-map.h b/capplets/time-admin/src/time-map.h new file mode 100644 index 00000000..e7e574dc --- /dev/null +++ b/capplets/time-admin/src/time-map.h @@ -0,0 +1,68 @@ +/* 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/>. +*/ + +#ifndef __TIME_MAP_H__ +#define __TIME_MAP_H__ + +#include "time-zone.h" +G_BEGIN_DECLS + +#define TYPE_TIMEZONE_MAP (timezone_map_get_type ()) +#define TIMEZONEMAP(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), TYPE_TIMEZONE_MAP,TimezoneMap)) +typedef struct TimezoneMap +{ + GtkWidget parent_instance; + GdkPixbuf *orig_background; + GdkPixbuf *orig_background_dim; + GdkPixbuf *orig_color_map; + + GdkPixbuf *background; + GdkPixbuf *color_map; + GdkPixbuf *pin; + + guchar *visible_map_pixels; + gint visible_map_rowstride; + + gdouble selected_offset; + + TzDB *tzdb; + TzLocation *location; + + gchar *bubble_text; +}TimezoneMap; + + +typedef struct TimezoneMapClass +{ + GtkWidgetClass parent_class; + +} TimezoneMapClass; + +GType timezone_map_get_type (void) G_GNUC_CONST; + +void timezone_map_set_bubble_text (TimezoneMap *map, + const gchar *text); + +gboolean timezone_map_set_timezone (TimezoneMap *map, + const gchar *timezone); + +TzLocation *timezone_map_get_location (TimezoneMap *map); + +TimezoneMap * timezone_map_new (void); + +G_END_DECLS +#endif diff --git a/capplets/time-admin/src/time-share.c b/capplets/time-admin/src/time-share.c new file mode 100644 index 00000000..12bd8801 --- /dev/null +++ b/capplets/time-admin/src/time-share.c @@ -0,0 +1,154 @@ +/* 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-share.h" + +/****************************************************************************** +* Function: MessageReport +* +* Explain: Prompt information dialog +* +* Input: @Title Message title +* @Msg Message content +* @nType Message type +* Output: +* +* Author: zhuyaliang 25/05/2018 +******************************************************************************/ +int MessageReport(const char *Title,const char *Msg,int nType) +{ + GtkWidget *dialog = NULL; + int nRet; + + switch(nType) + { + case ERROR: + { + dialog = gtk_message_dialog_new(GTK_WINDOW(WindowLogin), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_ERROR, + GTK_BUTTONS_OK, + "%s",Title); + break; + } + case WARING: + { + dialog = gtk_message_dialog_new(GTK_WINDOW(WindowLogin), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "%s",Title); + break; + } + case INFOR: + { + dialog = gtk_message_dialog_new(GTK_WINDOW(WindowLogin), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_INFO, + GTK_BUTTONS_OK, + "%s",Title); + break; + } + case QUESTION: + { + dialog = gtk_message_dialog_new(GTK_WINDOW(WindowLogin), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_YES_NO, + "%s",Title); + gtk_dialog_add_button (GTK_DIALOG (dialog),("_Return"), + GTK_RESPONSE_ACCEPT); + break; + } + case QUESTIONNORMAL: + { + dialog = gtk_message_dialog_new(GTK_WINDOW(WindowLogin), + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_QUESTION, + GTK_BUTTONS_YES_NO, + "%s",Title); + break; + } + default : + break; + + } + gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(dialog), + TYPEMSG, + Msg); + gtk_window_set_title(GTK_WINDOW(dialog),("Message")); + nRet = gtk_dialog_run(GTK_DIALOG(dialog)); + gtk_widget_destroy(dialog); + + return nRet; +} +void SetLableFontType(GtkWidget *Lable ,const char *Color,int FontSzie,const char *Word) +{ + char LableTypeBuf[200] = { 0 }; + + sprintf(LableTypeBuf, + "<span foreground=\'%s\'weight=\'light\'font_desc=\'%d\'>%s</span>", + Color,FontSzie,Word); + gtk_label_set_markup(GTK_LABEL(Lable),LableTypeBuf); + +} +void QuitApp(TimeAdmin *ta) +{ + if(ta->UpdateTimeId > 0) + { + g_source_remove (ta->UpdateTimeId); + } + if(ta->ApplyId > 0) + { + g_source_remove(ta->ApplyId); + } + + gtk_main_quit(); +} +GtkWidget* DialogAddButtonWithIconName (GtkDialog *dialog, + const gchar *button_text, + const gchar *icon_name, + gint response_id) +{ + GtkWidget *button; + + button = gtk_button_new_with_mnemonic (button_text); + gtk_button_set_image (GTK_BUTTON (button), gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_BUTTON)); + + gtk_button_set_use_underline (GTK_BUTTON (button), TRUE); + gtk_style_context_add_class (gtk_widget_get_style_context (button), "text-button"); + gtk_widget_set_can_default (button, TRUE); +// gtk_widget_show (button); + gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id); + + return button; +} +void SetTooltip(GtkWidget*box,gboolean mode) +{ + if(!mode) + { + gtk_widget_set_tooltip_markup(box, + _("Network time synchronization has been set up.prohibit Modification Time")); + } + else + { + gtk_widget_set_tooltip_markup(box,NULL); + } +} diff --git a/capplets/time-admin/src/time-share.h b/capplets/time-admin/src/time-share.h new file mode 100644 index 00000000..9ecd6999 --- /dev/null +++ b/capplets/time-admin/src/time-share.h @@ -0,0 +1,95 @@ +/* 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/>. +*/ + +#ifndef __TIME_SHARE_H__ +#define __TIME_SHARE_H__ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include <gtk/gtk.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <time.h> +#include <sys/time.h> +#include <locale.h> +#include <libintl.h> +#include <gio/gio.h> + +#define LOCALEDIR "/usr/share/locale/" +#define _(STRING) gettext(STRING) +#define C_(a,b) gettext(b) +#define TYPEMSG "<span foreground='red'font_desc='13'>%s </span>" +#define ERROR 1 +#define WARING 2 +#define INFOR 3 +#define QUESTION 4 +#define QUESTIONNORMAL 5 +#define GETTEXT_PACKAGE_TIMEZONES GETTEXT_PACKAGE "-timezones" + + +GtkWidget *WindowLogin; +int TimeoutFlag; +typedef struct +{ + GtkWidget *MainWindow; + GtkWidget *HourSpin; + GtkWidget *MinuteSpin; + GtkWidget *SecondSpin; + GtkWidget *TimeZoneButton; + GtkWidget *Calendar; + GtkWidget *CloseButton; + GtkWidget *SaveButton; + int UpdateTimeId; + int ApplyId; + int OldDay; + gboolean NtpState; + GDBusConnection *Connection; + GDBusProxy *proxy; + GtkWidget *dialog; + GtkWidget *TZconfire; + GtkWidget *TZclose; + GtkWidget *TimezoneEntry; + GtkWidget *SearchBar; + GtkWidget *map; + GtkListStore *CityListStore; + GtkTreeModelSort *CityModelSort; + +}TimeAdmin; + +int MessageReport (const char *Title, + const char *Msg, + int nType); + +void SetLableFontType (GtkWidget *Lable, + const char *Color, + int FontSzie, + const char *Word); + +void QuitApp (TimeAdmin *ta); + +GtkWidget* DialogAddButtonWithIconName (GtkDialog *dialog, + const gchar *button_text, + const gchar *icon_name, + gint response_id); + +void SetTooltip (GtkWidget *box, + gboolean mode); +#endif diff --git a/capplets/time-admin/src/time-tool.c b/capplets/time-admin/src/time-tool.c new file mode 100644 index 00000000..e8b94af8 --- /dev/null +++ b/capplets/time-admin/src/time-tool.c @@ -0,0 +1,317 @@ +/* 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); + } + else + { + + } + + +} +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); + } + +} +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); + 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); + } + else + { + + } + +} +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); + } +} diff --git a/capplets/time-admin/src/time-tool.h b/capplets/time-admin/src/time-tool.h new file mode 100644 index 00000000..393c43a5 --- /dev/null +++ b/capplets/time-admin/src/time-tool.h @@ -0,0 +1,46 @@ +/* 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/>. +*/ + +#ifndef __TIME_TOOL_H__ +#define __TIME_TOOL_H__ + +#include "time-share.h" +struct tm *GetCurrentTime (void); +void Update_Clock_Start(TimeAdmin *ta); + +void Update_Clock_Stop (TimeAdmin *ta); + +gboolean ChangeNtpSync (GtkSwitch *widget, + gboolean state, + gpointer data); + +void SelectDate (GtkCalendar *calendar, + gpointer data); + +void SaveModifyTime (GtkButton *button, + gpointer data); + +gboolean GetNtpState (TimeAdmin *ta); + +void ReloadNtp (GDBusProxy *proxy, + gboolean state); + +const gchar *GetTimeZone (TimeAdmin *ta); + +void SetTimeZone (GDBusProxy *proxy, + const char *zone); +#endif diff --git a/capplets/time-admin/src/time-zone.c b/capplets/time-admin/src/time-zone.c new file mode 100644 index 00000000..dfb86f3b --- /dev/null +++ b/capplets/time-admin/src/time-zone.c @@ -0,0 +1,661 @@ +/* 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-zone.h" +#include "time-map.h" +#include "time-tool.h" +#include <math.h> +#define MATE_DESKTOP_USE_UNSTABLE_API + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <libintl.h> + +#include <libmate-desktop/mate-languages.h> + + +#define DEFAULT_TZ "Europe/London" +#define BACKFILE "/usr/share/mate-time-admin/map/backward" +static void LocationChanged(TimezoneMap *map, + TzLocation *location,TimeAdmin *ta); +enum { + CITY_COL_CITY_HUMAN_READABLE, + CITY_COL_ZONE, + CITY_NUM_COLS +}; +static gchar *tz_data_file_get (void) +{ + gchar *file; + + file = g_strdup (TZ_DATA_FILE); + + return file; +} + +static float convert_pos (gchar *pos, int digits) +{ + gchar whole[10]; + gchar *fraction; + gint i; + float t1, t2; + + if (!pos || strlen(pos) < 4 || digits > 9) return 0.0; + + for (i = 0; i < digits + 1; i++) whole[i] = pos[i]; + whole[i] = '\0'; + fraction = pos + digits + 1; + + t1 = g_strtod (whole, NULL); + t2 = g_strtod (fraction, NULL); + + if (t1 >= 0.0) return t1 + t2/pow (10.0, strlen(fraction)); + else return t1 - t2/pow (10.0, strlen(fraction)); +} +static int compare_country_names (const void *a, const void *b) +{ + const TzLocation *tza = * (TzLocation **) a; + const TzLocation *tzb = * (TzLocation **) b; + + return strcmp (tza->zone, tzb->zone); +} + +static void sort_locations_by_country (GPtrArray *locations) +{ + qsort (locations->pdata, locations->len, sizeof (gpointer), + compare_country_names); +} +static void load_backward_tz (TzDB *tz_db) +{ + FILE *fp; + char buf[128] = { 0 }; + + tz_db->backward = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + fp = fopen(BACKFILE,"r"); + if(fp == NULL) + { + g_error("%s does not exist\r\n",BACKFILE); + + } + while(fgets(buf,128,fp)) + { + g_auto(GStrv) items = NULL; + guint j; + char *real, *alias; + + if (g_ascii_strncasecmp (buf, "Link\t", 5) != 0) + continue; + + items = g_strsplit (buf, "\t", -1); + real = NULL; + alias = NULL; + for (j = 1; items[j] != NULL; j++) + { + if (items[j][0] == '\0') + continue; + if (real == NULL) + { + real = items[j]; + continue; + } + alias = items[j]; + break; + } + if (real == NULL || alias == NULL) + g_warning ("Could not parse line: %s", buf); + + /* We don't need more than one name for it */ + if (g_str_equal (real, "Etc/UTC") || + g_str_equal (real, "Etc/UCT")) + real = "Etc/GMT"; + + g_hash_table_insert (tz_db->backward, g_strdup (alias), g_strdup (real)); + + } + fclose(fp); +} + +TzDB *tz_load_db (void) +{ + g_autofree gchar *tz_data_file = NULL; + TzDB *tz_db; + FILE *tzfile; + char buf[4096]; + + tz_data_file = tz_data_file_get (); + if (!tz_data_file) + { + g_warning ("Could not get the TimeZone data file name"); + return NULL; + } + tzfile = fopen (tz_data_file, "r"); + if (!tzfile) + { + g_warning ("Could not open *%s*\n", tz_data_file); + return NULL; + } + + tz_db = g_new0 (TzDB, 1); + tz_db->locations = g_ptr_array_new (); + + while (fgets (buf, sizeof(buf), tzfile)) + { + g_auto(GStrv) tmpstrarr = NULL; + g_autofree gchar *latstr = NULL; + g_autofree gchar *lngstr = NULL; + gchar *p; + TzLocation *loc; + + if (*buf == '#') continue; + + g_strchomp(buf); + tmpstrarr = g_strsplit(buf,"\t", 6); + + latstr = g_strdup (tmpstrarr[1]); + p = latstr + 1; + while (*p != '-' && *p != '+') p++; + lngstr = g_strdup (p); + *p = '\0'; + + loc = g_new0 (TzLocation, 1); + loc->country = g_strdup (tmpstrarr[0]); + loc->zone = g_strdup (tmpstrarr[2]); + loc->latitude = convert_pos (latstr, 2); + loc->longitude = convert_pos (lngstr, 3); + +#ifdef __sun + if (tmpstrarr[3] && *tmpstrarr[3] == '-' && tmpstrarr[4]) + loc->comment = g_strdup (tmpstrarr[4]); + + if (tmpstrarr[3] && *tmpstrarr[3] != '-' && !islower(loc->zone)) { + TzLocation *locgrp; + locgrp = g_new0 (TzLocation, 1); + locgrp->country = g_strdup (tmpstrarr[0]); + locgrp->zone = g_strdup (tmpstrarr[3]); + locgrp->latitude = convert_pos (latstr, 2); + locgrp->longitude = convert_pos (lngstr, 3); + locgrp->comment = (tmpstrarr[4]) ? g_strdup (tmpstrarr[4]) : NULL; + + g_ptr_array_add (tz_db->locations, (gpointer) locgrp); + } +#else + loc->comment = (tmpstrarr[3]) ? g_strdup(tmpstrarr[3]) : NULL; +#endif + + g_ptr_array_add (tz_db->locations, (gpointer) loc); + } + + fclose (tzfile); + + /* now sort by country */ + sort_locations_by_country (tz_db->locations); + + /* Load up the hashtable of backward links */ + load_backward_tz (tz_db); + + return tz_db; +} + +static GtkWidget *GetTimeZoneMap(TimeAdmin *ta) +{ + GtkWidget *map; + g_autoptr(GtkEntryCompletion) completion = NULL; + + map = (GtkWidget *) timezone_map_new (); + /* + g_signal_connect_object (map, + "location-changed", + G_CALLBACK (LocationChanged), + map, + G_CONNECT_SWAPPED); + */ + g_signal_connect (map, + "location-changed", + G_CALLBACK (LocationChanged), + ta); + + completion = gtk_entry_completion_new (); + gtk_entry_set_completion (GTK_ENTRY (ta->TimezoneEntry), completion); + gtk_entry_completion_set_model (completion, GTK_TREE_MODEL (ta->CityListStore)); + gtk_entry_completion_set_text_column (completion, CITY_COL_CITY_HUMAN_READABLE); + + + return map; +} +static char * +translated_city_name (TzLocation *loc) +{ + g_autofree gchar *zone_translated = NULL; + g_auto(GStrv) split_translated = NULL; + g_autofree gchar *country = NULL; + gchar *name; + gint length; + + zone_translated = g_strdup (dgettext (GETTEXT_PACKAGE_TIMEZONES, loc->zone)); + g_strdelimit (zone_translated, "_", ' '); + split_translated = g_regex_split_simple ("[\\x{2044}\\x{2215}\\x{29f8}\\x{ff0f}/]", + zone_translated, + 0, 0); + + length = g_strv_length (split_translated); + + country = mate_get_country_from_code (loc->country, NULL); + name = g_strdup_printf (C_("timezone loc", "%s, %s"), + split_translated[length-1], + country); + + return name; +} + +static void +update_timezone (TimezoneMap *map) +{ + g_autofree gchar *bubble_text = NULL; + g_autofree gchar *city_country = NULL; + g_autofree gchar *utc_label = NULL; + g_autofree gchar *time_label = NULL; + g_autofree gchar *tz_desc = NULL; + TzLocation *current_location; + GDateTime *date; + + date = g_date_time_new_now_local (); + current_location = timezone_map_get_location (TIMEZONEMAP (map)); + city_country = translated_city_name (current_location); + + utc_label = g_date_time_format (date, _("UTC%:::z")); + + tz_desc = g_strdup_printf ( "%s (%s)", + g_date_time_get_timezone_abbreviation (date), + utc_label); + time_label = g_date_time_format (date, _("%R")); + + bubble_text = g_strdup_printf ("<b>%s</b>\n" + "<small>%s</small>\n" + "<b>%s</b>", + tz_desc, + city_country, + time_label); + timezone_map_set_bubble_text (TIMEZONEMAP (map), bubble_text); +} + +static void LocationChanged(TimezoneMap *map, + TzLocation *location, + TimeAdmin *ta) +{ + + update_timezone (map); +} +static void +get_initial_timezone (TimeAdmin *ta) +{ + const gchar *timezone; + + timezone = GetTimeZone(ta); + + if (timezone == NULL || + !timezone_map_set_timezone (TIMEZONEMAP (ta->map), timezone)) + { + g_warning ("Timezone '%s' is unhandled,setting %s as default", timezone ? timezone : "(null)", DEFAULT_TZ); + timezone_map_set_timezone (TIMEZONEMAP (ta->map), DEFAULT_TZ); + } + update_timezone (TIMEZONEMAP(ta->map)); +} +static void LoadCities (TzLocation *loc, + GtkListStore *CityStore) +{ + g_autofree gchar *human_readable = NULL; + + human_readable = translated_city_name (loc); + gtk_list_store_insert_with_values (CityStore, + NULL, + 0, + CITY_COL_CITY_HUMAN_READABLE, + human_readable, + CITY_COL_ZONE, + loc->zone, + -1); +} + +static void CreateCityList(TimeAdmin *ta) +{ + g_autoptr(TzDB) db = NULL; + + ta->CityListStore = gtk_list_store_new (CITY_NUM_COLS,G_TYPE_STRING,G_TYPE_STRING); + + db = tz_load_db (); + g_ptr_array_foreach (db->locations, (GFunc) LoadCities, ta->CityListStore); + +} +static GtkWidget *CreateZoneFrame(TimeAdmin *ta) +{ + GtkWidget *TimeZoneFrame; + + TimeZoneFrame = gtk_frame_new (_("Time Zone")); + gtk_widget_set_size_request(TimeZoneFrame,300,200); + gtk_frame_set_shadow_type(GTK_FRAME(TimeZoneFrame),GTK_SHADOW_NONE); + + return TimeZoneFrame; +} +static GtkWidget *CreateZoneScrolled(TimeAdmin *ta) +{ + GtkWidget *Scrolled; + Scrolled = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (Scrolled), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (Scrolled), + GTK_SHADOW_IN); + + return Scrolled; +} +static void CreateZoneEntry(TimeAdmin *ta) +{ + GtkWidget *hbox; + + ta->TimezoneEntry = gtk_search_entry_new (); + hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10); + gtk_widget_set_halign (hbox, GTK_ALIGN_CENTER); + gtk_box_pack_start (GTK_BOX (hbox), ta->TimezoneEntry, FALSE, FALSE, 0); + + ta->SearchBar = gtk_search_bar_new (); + gtk_search_bar_connect_entry (GTK_SEARCH_BAR (ta->SearchBar), + GTK_ENTRY (ta->TimezoneEntry)); + gtk_search_bar_set_show_close_button (GTK_SEARCH_BAR (ta->SearchBar),FALSE); + gtk_container_add (GTK_CONTAINER (ta->SearchBar), hbox); + gtk_search_bar_set_search_mode(GTK_SEARCH_BAR(ta->SearchBar),TRUE); + +} + +static gboolean CityChanged(GtkEntryCompletion *completion, + GtkTreeModel *model, + GtkTreeIter *iter, + TimeAdmin *self) +{ + GtkWidget *entry; + g_autofree gchar *zone = NULL; + + gtk_tree_model_get (model, + iter, + CITY_COL_ZONE, + &zone, + -1); + timezone_map_set_timezone (TIMEZONEMAP (self->map), zone); + + entry = gtk_entry_completion_get_entry (completion); + gtk_entry_set_text (GTK_ENTRY (entry), ""); + + return TRUE; +} +static void ChoooseTimezoneDone (GtkWidget *widget, + TimeAdmin *ta) +{ + TimezoneMap *map; + g_autofree gchar *ZoneCity = NULL; + + map = TIMEZONEMAP(ta->map); + SetTimeZone(ta->proxy,map->location->zone); + + ZoneCity = translated_city_name(map->location); + gtk_button_set_label((GTK_BUTTON(ta->TimeZoneButton)),ZoneCity); + gtk_widget_hide_on_delete(GTK_WIDGET(ta->dialog)); +} + +static void ChoooseTimezoneClose(GtkWidget *widget, + TimeAdmin *ta) +{ + gtk_widget_hide_on_delete(GTK_WIDGET(ta->dialog)); +} + +void SetupTimezoneDialog(TimeAdmin *ta) +{ + GtkWidget *Vbox; + GtkWidget *TimeZoneFrame; + GtkWidget *Scrolled; + + ta->dialog = gtk_dialog_new_with_buttons (_("Time Zone Selection"), + NULL, + GTK_DIALOG_DESTROY_WITH_PARENT, + NULL, + NULL); + gtk_window_set_default_size (GTK_WINDOW (ta->dialog), 730, 520); + + ta->TZclose = DialogAddButtonWithIconName(GTK_DIALOG(ta->dialog), + _("Close"), + "window-close", + GTK_RESPONSE_CANCEL); + + ta->TZconfire = DialogAddButtonWithIconName(GTK_DIALOG(ta->dialog), + _("Confirm"), + "emblem-default", + GTK_RESPONSE_OK); + g_signal_connect (ta->TZconfire, + "clicked", + G_CALLBACK (ChoooseTimezoneDone), + ta); + + g_signal_connect (ta->TZclose, + "clicked", + G_CALLBACK (ChoooseTimezoneClose), + ta); + + Vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); + gtk_style_context_add_class (gtk_widget_get_style_context (Vbox), "linked"); + + + TimeZoneFrame = CreateZoneFrame(ta); + Scrolled = CreateZoneScrolled(ta); + gtk_container_add (GTK_CONTAINER (TimeZoneFrame), Scrolled); + CreateCityList(ta); + CreateZoneEntry(ta); + gtk_box_pack_start (GTK_BOX (Vbox), ta->SearchBar,FALSE,FALSE, 0); + ta->map = GetTimeZoneMap(ta); + gtk_widget_show (ta->map); + gtk_container_add (GTK_CONTAINER (Scrolled),ta->map); + gtk_box_pack_start(GTK_BOX(Vbox),TimeZoneFrame,TRUE,TRUE,10); + get_initial_timezone(ta); + + g_signal_connect(gtk_entry_get_completion (GTK_ENTRY (ta->TimezoneEntry)), + "match-selected", + G_CALLBACK (CityChanged), + ta); + + gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (ta->dialog))), + Vbox, + TRUE, + TRUE, 8); +} +void tz_info_free (TzInfo *tzinfo) +{ + g_return_if_fail (tzinfo != NULL); + + if (tzinfo->tzname_normal) g_free (tzinfo->tzname_normal); + if (tzinfo->tzname_daylight) g_free (tzinfo->tzname_daylight); + g_free (tzinfo); +} +struct { + const char *orig; + const char *dest; +} aliases[] = { + { "Asia/Istanbul", "Europe/Istanbul" }, /* Istanbul is in both Europe and Asia */ + { "Europe/Nicosia", "Asia/Nicosia" }, /* Ditto */ + { "EET", "Europe/Istanbul" }, /* Same tz as the 2 above */ + { "HST", "Pacific/Honolulu" }, + { "WET", "Europe/Brussels" }, /* Other name for the mainland Europe tz */ + { "CET", "Europe/Brussels" }, /* ditto */ + { "MET", "Europe/Brussels" }, + { "Etc/Zulu", "Etc/GMT" }, + { "Etc/UTC", "Etc/GMT" }, + { "GMT", "Etc/GMT" }, + { "Greenwich", "Etc/GMT" }, + { "Etc/UCT", "Etc/GMT" }, + { "Etc/GMT0", "Etc/GMT" }, + { "Etc/GMT+0", "Etc/GMT" }, + { "Etc/GMT-0", "Etc/GMT" }, + { "Etc/Universal", "Etc/GMT" }, + { "PST8PDT", "America/Los_Angeles" }, /* Other name for the Atlantic tz */ + { "EST", "America/New_York" }, /* Other name for the Eastern tz */ + { "EST5EDT", "America/New_York" }, /* ditto */ + { "CST6CDT", "America/Chicago" }, /* Other name for the Central tz */ + { "MST", "America/Denver" }, /* Other name for the mountain tz */ + { "MST7MDT", "America/Denver" }, /* ditto */ +}; +static gboolean +compare_timezones (const char *a, + const char *b) +{ + if (g_str_equal (a, b)) + return TRUE; + if (strchr (b, '/') == NULL) { + g_autofree gchar *prefixed = NULL; + + prefixed = g_strdup_printf ("/%s", b); + if (g_str_has_suffix (a, prefixed)) + return TRUE; + } + + return FALSE; +} + +char *tz_info_get_clean_name (TzDB *tz_db, + const char *tz) +{ + char *ret; + const char *timezone; + guint i; + gboolean replaced; + + /* Remove useless prefixes */ + if (g_str_has_prefix (tz, "right/")) + tz = tz + strlen ("right/"); + else if (g_str_has_prefix (tz, "posix/")) + tz = tz + strlen ("posix/"); + + /* Here start the crazies */ + replaced = FALSE; + + for (i = 0; i < G_N_ELEMENTS (aliases); i++) { + if (compare_timezones (tz, aliases[i].orig)) { + replaced = TRUE; + timezone = aliases[i].dest; + break; + } + } + + /* Try again! */ + if (!replaced) { + /* Ignore crazy solar times from the '80s */ + if (g_str_has_prefix (tz, "Asia/Riyadh") || + g_str_has_prefix (tz, "Mideast/Riyadh")) { + timezone = "Asia/Riyadh"; + replaced = TRUE; + } + } + + if (!replaced) + timezone = tz; + + ret = g_hash_table_lookup (tz_db->backward, timezone); + if (ret == NULL) + return g_strdup (timezone); + return g_strdup (ret); +} + +TzInfo *tz_info_from_location (TzLocation *loc) +{ + TzInfo *tzinfo; + time_t curtime; + struct tm *curzone; + g_autofree gchar *tz_env_value = NULL; + + g_return_val_if_fail (loc != NULL, NULL); + g_return_val_if_fail (loc->zone != NULL, NULL); + + tz_env_value = g_strdup (getenv ("TZ")); + setenv ("TZ", loc->zone, 1); + +#if 0 + tzset (); +#endif + tzinfo = g_new0 (TzInfo, 1); + + curtime = time (NULL); + curzone = localtime (&curtime); + +#ifndef __sun + tzinfo->tzname_normal = g_strdup (curzone->tm_zone); + if (curzone->tm_isdst) + tzinfo->tzname_daylight = + g_strdup (&curzone->tm_zone[curzone->tm_isdst]); + else + tzinfo->tzname_daylight = NULL; + + tzinfo->utc_offset = curzone->tm_gmtoff; +#else + tzinfo->tzname_normal = NULL; + tzinfo->tzname_daylight = NULL; + tzinfo->utc_offset = 0; +#endif + + tzinfo->daylight = curzone->tm_isdst; + + if (tz_env_value) + setenv ("TZ", tz_env_value, 1); + else + unsetenv ("TZ"); + + return tzinfo; +} +glong tz_location_get_utc_offset (TzLocation *loc) +{ + g_autoptr(TzInfo) tz_info = NULL; + glong offset; + + tz_info = tz_info_from_location (loc); + offset = tz_info->utc_offset; + return offset; +} +void RunTimeZoneDialog (GtkButton *button, + gpointer data) +{ + TimeAdmin *ta = (TimeAdmin *)data; + + gtk_widget_show_all(GTK_WIDGET(ta->dialog)); +} +static void +tz_location_free (TzLocation *loc, gpointer data) +{ + g_free (loc->country); + g_free (loc->zone); + g_free (loc->comment); + + g_free (loc); +} +void TimeZoneDateBaseFree (TzDB *db) +{ + g_ptr_array_foreach (db->locations, (GFunc) tz_location_free, NULL); + g_ptr_array_free (db->locations, TRUE); + g_hash_table_destroy (db->backward); + g_free (db); +} +GPtrArray *tz_get_locations (TzDB *db) +{ + return db->locations; +} diff --git a/capplets/time-admin/src/time-zone.h b/capplets/time-admin/src/time-zone.h new file mode 100644 index 00000000..a0c993d5 --- /dev/null +++ b/capplets/time-admin/src/time-zone.h @@ -0,0 +1,76 @@ +/* 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/>. +*/ + +#ifndef __TIME_ZONE_H__ +#define __TIME_ZONE_H__ + +#include "time-share.h" + +#ifndef __sun +# define TZ_DATA_FILE "/usr/share/zoneinfo/zone.tab" +#else +# define TZ_DATA_FILE "/usr/share/lib/zoneinfo/tab/zone_sun.tab" +#endif + +typedef struct TzDB +{ + GPtrArray *locations; + GHashTable *backward; +}TzDB; + +typedef struct TzLocation +{ + gchar *country; + gdouble latitude; + gdouble longitude; + gchar *zone; + gchar *comment; + + gdouble dist; /* distance to clicked point for comparison */ +}TzLocation; + +typedef struct TzInfo +{ + gchar *tzname_normal; + gchar *tzname_daylight; + glong utc_offset; + gint daylight; +}TzInfo; +TzDB *tz_load_db (void); + +void SetupTimezoneDialog (TimeAdmin *ta); + +void RunTimeZoneDialog (GtkButton *button, + gpointer data); + +void TimeZoneDateBaseFree (TzDB *db); + +GPtrArray *tz_get_locations (TzDB *db); + +TzInfo *tz_info_from_location (TzLocation *loc); + +glong tz_location_get_utc_offset (TzLocation *loc); + +char *tz_info_get_clean_name (TzDB *tz_db, + const char *tz); + +void tz_info_free (TzInfo *tzinfo); + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (TzDB, TimeZoneDateBaseFree) +G_DEFINE_AUTOPTR_CLEANUP_FUNC (TzInfo, tz_info_free) + +#endif |