Commit 9f5cc878 authored by Carit Zhu's avatar Carit Zhu 🎱

Add setting date/time/timezone functions

parent fb1c7f83
### Temp files ###
*.*~
### VS Code ###
/.vscode/
......@@ -7,45 +7,31 @@
#define GTK_EVENT_ACTIVATE "activate"
#define GTK_EVENT_DESTROY "destroy"
#define GTK_EVENT_CLICKED "clicked"
#define GTK_EVENT_CHANGED "changed"
#define DEBUG
/* GObjects */
static GObject *entry_date;
static GObject *entry_hour;
static GObject *entry_minute;
static GObject *entry_second;
/* Time info buffer */
static time_t time_now;
static struct tm *tm_now;
static char active_date[11];
static char active_hour[3];
static char active_min[3];
static char active_sec[3];
/* Timezone info buffer */
#define MAX_TIME_STATUS_LINE 8
static char timezone_cur[32];
static char timezone_cur[32] = {0};
static char timezone_new[32] = {0};
static char timezone_buffer[512][32];
static int timezone_num = 0;
/*
static void print_hello(GtkWidget *widget, gpointer data) {
g_print("Hello World\n");
}
static void activate(GtkApplication *app, gpointer user_data) {
GtkWidget *window;
GtkWidget *button;
GtkWidget *button_box;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Window");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
gtk_container_add(GTK_CONTAINER(window), button_box);
button = gtk_button_new_with_label("Hello World");
g_signal_connect(button, "clicked", G_CALLBACK(print_hello), NULL);
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
gtk_container_add(GTK_CONTAINER(button_box), button);
gtk_widget_show_all(window);
}
*/
static int get_timezone(const char *src, char *timezone) {
const char *buf = src;
int index = 0;
......@@ -66,9 +52,47 @@ static int get_timezone(const char *src, char *timezone) {
return 0;
}
static void set_timedatectl(char *command, char *data) {
char cmd[64] = "timedatectl ";
strcat(cmd, command);
strcat(cmd, " ");
strcat(cmd, data);
#ifdef DEBUG
g_print("[DEBUG] CMD: %s\n", cmd);
#endif
FILE *fp = popen(cmd, "r");
pclose(fp);
}
static void set_timezone(char *timezone) {
if (strcmp(timezone_cur, timezone)) {
set_timedatectl("set-timezone", timezone);
}
}
static void set_date_time(const char *date, const char *hour, const char *min, const char *sec) {
char data[32] = {0};
if (strcmp(active_date, date)) {
sprintf(data, "'%s %s:%s:%s'", date, hour, min, sec);
set_timedatectl("set-time", data);
strcpy(active_date, date);
strcpy(active_hour, hour);
strcpy(active_min, min);
strcpy(active_sec, sec);
} else if (strcmp(active_hour, hour) || strcmp(active_min, min) || strcmp(active_sec, sec)) {
sprintf(data, "'%s:%s:%s'", hour, min, sec);
set_timedatectl("set-time", data);
strcpy(active_hour, hour);
strcpy(active_min, min);
strcpy(active_sec, sec);
}
}
static void get_time_date_status() {
char time_status[8][32];
FILE *fp = popen("timedatectl status", "r");
FILE *fp = popen("timedatectl status | grep \"Time zone\"", "r");
for (int i = 0; i < MAX_TIME_STATUS_LINE; i++) {
char *buf = fgets(time_status[i], 32, fp);
......@@ -81,6 +105,7 @@ static void get_time_date_status() {
buf = strchr(time_status[i], ':');
if (buf != NULL) {
get_timezone(buf + 1, timezone_cur);
strcpy(timezone_new, timezone_cur);
}
}
}
......@@ -90,10 +115,41 @@ static void get_time_date_status() {
pclose(fp);
}
static void combo_handler(GObject *combo, gpointer user_data) {
GtkTreeIter iter;
GtkTreeModel *model;
gchar *string = NULL;
if (!g_strcmp0((gchar *)user_data, "timezone")) {
/* Obtain currently selected item from combo box.
* If nothing is selected, do nothing. */
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter)) {
/* Obtain data model from combo box. */
model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
/* Obtain string from model. */
gtk_tree_model_get(model, &iter, 0, &string, -1);
}
#ifdef DEBUG
/* Print string to the console - if string is NULL, print NULL. */
g_print("[DEBUG] Selected (complex): >> %s <<\n", ( string ? string : "NULL" ));
#endif
/* Update current timezone */
strcpy(timezone_new, string);
/* Free string (if not NULL). */
if (string)
g_free(string);
}
}
static void timezone_init(GtkBuilder *builder) {
GObject *combo_timezone;
GtkListStore *store = NULL;
GtkTreeIter iter;
GtkTreeIter active_iter;
GtkCellRenderer *cell = NULL;
combo_timezone = gtk_builder_get_object(builder, "combobox-timezone");
......@@ -127,6 +183,9 @@ static void timezone_init(GtkBuilder *builder) {
for (int i = 0; i < timezone_num; i++) {
gtk_list_store_append(store, &iter);
if (!g_strcmp0(timezone_buffer[i], timezone_cur)) {
active_iter = iter;
}
gtk_list_store_set(store, &iter, 0, timezone_buffer[i], -1);
}
......@@ -135,47 +194,61 @@ static void timezone_init(GtkBuilder *builder) {
cell = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_timezone), cell, TRUE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_timezone), cell, "text", 0, NULL);
/* Set default active item by iter */
gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo_timezone), &active_iter);
g_signal_connect(combo_timezone, GTK_EVENT_CHANGED, G_CALLBACK(combo_handler), "timezone");
}
static void date_activate(GObject *entry, gpointer user_data) {
static void combobox_timezone_changed_cb(GObject *object, gpointer user_data) {
#ifdef DEBUG
g_print("combobox_timezone_changed_cb");
#endif
}
static void button_handler(GObject *button, gpointer user_data) {
const gchar *date;
const gchar *hour;
const gchar *min;
const gchar *sec;
if (!g_strcmp0((gchar *)user_data, "ok")) {
date = gtk_entry_get_text(GTK_ENTRY(entry_date));
hour = gtk_entry_get_text(GTK_ENTRY(entry_hour));
min = gtk_entry_get_text(GTK_ENTRY(entry_minute));
sec = gtk_entry_get_text(GTK_ENTRY(entry_second));
set_date_time(date, hour, min, sec);
set_timezone(timezone_new);
} else if (!g_strcmp0((gchar *)user_data, "cancel")) {
gtk_main_quit();
}
}
static void date_init(GtkEntry *entry_date, GtkEntry *entry_hour, GtkEntry *entry_minute, GtkEntry *entry_second) {
gchar date_text[11];
gchar hour_text[3];
gchar minute_text[3];
gchar second_text[3];
/* Get current local time */
time(&time_now);
tm_now = localtime(&time_now);
sprintf(date_text, "%04d-%02d-%02d", (1900 + tm_now->tm_year), tm_now->tm_mon, tm_now->tm_mday);
sprintf(active_date, "%04d-%02d-%02d", (1900 + tm_now->tm_year), (tm_now->tm_mon + 1), tm_now->tm_mday);
#ifdef DEBUG
g_print("[DEBUG] Date: %s\n", date_text);
g_print("[DEBUG] Date: %s\n", active_date);
#endif
sprintf(hour_text, "%02d", tm_now->tm_hour);
sprintf(minute_text, "%02d", tm_now->tm_min);
sprintf(second_text, "%02d", tm_now->tm_sec);
sprintf(active_hour, "%02d", tm_now->tm_hour);
sprintf(active_min, "%02d", tm_now->tm_min);
sprintf(active_sec, "%02d", tm_now->tm_sec);
#ifdef DEBUG
g_print("[DEBUG] Time: %s:%s:%s\n", hour_text, minute_text, second_text);
g_print("[DEBUG] Time: %s:%s:%s\n", active_hour, active_min, active_sec);
#endif
gtk_entry_set_text(entry_date, date_text);
gtk_entry_set_text(entry_hour, hour_text);
gtk_entry_set_text(entry_minute, minute_text);
gtk_entry_set_text(entry_second, second_text);
gtk_entry_set_text(entry_date, active_date);
gtk_entry_set_text(entry_hour, active_hour);
gtk_entry_set_text(entry_minute, active_min);
gtk_entry_set_text(entry_second, active_sec);
}
static int gobjects_init(GtkBuilder *builder, GtkWidget *dialog) {
GObject *entry_date;
GObject *entry_hour;
GObject *entry_minute;
GObject *entry_second;
GObject *combo_hour;
GObject *combo_minute;
GObject *combo_second;
......@@ -255,7 +328,14 @@ static int gobjects_init(GtkBuilder *builder, GtkWidget *dialog) {
date_init(GTK_ENTRY(entry_date), GTK_ENTRY(entry_hour), GTK_ENTRY(entry_minute), GTK_ENTRY(entry_second));
g_signal_connect(entry_date, GTK_EVENT_ACTIVATE, G_CALLBACK(date_activate), "date");
// /* Set default time combo box item */
// gtk_combo_box_set_active_id(GTK_COMBO_BOX(combo_hour), active_hour);
// gtk_combo_box_set_active_id(GTK_COMBO_BOX(combo_minute), active_min);
// gtk_combo_box_set_active_id(GTK_COMBO_BOX(combo_second), active_sec);
// gtk_combo_box_set_active(GTK_COMBO_BOX(combo_hour), tm_now->tm_hour);
// g_signal_connect(entry_date, GTK_EVENT_ACTIVATE, G_CALLBACK(date_activate), "date");
g_signal_connect(combo_hour, GTK_EVENT_CHANGED, G_CALLBACK(combo_handler), "hour");
g_signal_connect(button_ok, GTK_EVENT_CLICKED, G_CALLBACK(button_handler), "ok");
g_signal_connect(button_cancel, GTK_EVENT_CLICKED, G_CALLBACK(button_handler), "cancel");
......@@ -282,6 +362,7 @@ int main(int argc, char **argv) {
gtk_window_set_title(GTK_WINDOW(dialog), "Date & Time");
gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 200);
gtk_builder_connect_signals(builder, NULL);
g_signal_connect(dialog, GTK_EVENT_DESTROY, G_CALLBACK(gtk_main_quit), NULL);
gobjects_init(builder, dialog);
......@@ -289,18 +370,9 @@ int main(int argc, char **argv) {
get_time_date_status();
timezone_init(builder);
g_object_unref(G_OBJECT (builder));
gtk_widget_show_all(dialog);
gtk_main();
return 0;
// GtkApplication *app;
// int status;
// app = gtk_application_new("com.witium.timemanager", G_APPLICATION_FLAGS_NONE);
// g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
// status = g_application_run(G_APPLICATION(app), argc, argv);
// g_object_unref(app);
// return status;
}
No preview for this file type
......@@ -134,6 +134,7 @@
<object class="GtkComboBoxText" id="comboboxtext-hour">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="active">0</property>
<property name="button_sensitivity">on</property>
<property name="has_entry">True</property>
<child internal-child="entry">
......@@ -153,6 +154,7 @@
<object class="GtkComboBoxText" id="comboboxtext-minute">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="active">0</property>
<property name="button_sensitivity">on</property>
<property name="has_entry">True</property>
<child internal-child="entry">
......@@ -172,6 +174,7 @@
<object class="GtkComboBoxText" id="comboboxtext-second">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="active">0</property>
<property name="button_sensitivity">on</property>
<property name="has_entry">True</property>
<child internal-child="entry">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment