Commit fb1c7f83 authored by Carit Zhu's avatar Carit Zhu 🎱

Add timemanager.ui from glade and start gtk app by it.

parent 407106c5
CC = gcc
CFLAGS = `pkg-config --cflags gtk+-3.0`
LIBS = `pkg-config --libs gtk+-3.0`
all: timemanager
timemanager: main.c
$(CC) $(CFLAGS) $< -o $@ $(LIBS)
.PHONY: clean
clean:
rm *.o timemanager
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <gtk/gtk.h>
#define GTK_EVENT_ACTIVATE "activate"
#define GTK_EVENT_DESTROY "destroy"
#define GTK_EVENT_CLICKED "clicked"
#define DEBUG
/* Time info buffer */
static time_t time_now;
static struct tm *tm_now;
/* Timezone info buffer */
#define MAX_TIME_STATUS_LINE 8
static char timezone_cur[32];
static char timezone_buffer[512][32];
static int timezone_num = 0;
/*
static void print_hello(GtkWidget *widget, gpointer data) {
g_print("Hello World\n");
}
......@@ -23,15 +44,263 @@ static void activate(GtkApplication *app, gpointer user_data) {
gtk_widget_show_all(window);
}
*/
static int get_timezone(const char *src, char *timezone) {
const char *buf = src;
int index = 0;
/* Skip space char */
while(buf[index] == ' ') index++;
for (int i = 0; ; i++, index++) {
if (buf[index] != ' ') {
timezone[i] = buf[index];
} else {
/* End by space */
timezone[i] = 0;
break;
}
}
return 0;
}
static void get_time_date_status() {
char time_status[8][32];
FILE *fp = popen("timedatectl status", "r");
for (int i = 0; i < MAX_TIME_STATUS_LINE; i++) {
char *buf = fgets(time_status[i], 32, fp);
if (buf == NULL) {
break;
} else if (strstr(time_status[i], "Time zone") != NULL) {
int line_size = strlen(buf);
fseek(fp, line_size, SEEK_CUR);
buf = strchr(time_status[i], ':');
if (buf != NULL) {
get_timezone(buf + 1, timezone_cur);
}
}
}
#ifdef DEBUG
g_print("[DEBUG] Current timezone: %s\n", timezone_cur);
#endif
pclose(fp);
}
static void timezone_init(GtkBuilder *builder) {
GObject *combo_timezone;
GtkListStore *store = NULL;
GtkTreeIter iter;
GtkCellRenderer *cell = NULL;
combo_timezone = gtk_builder_get_object(builder, "combobox-timezone");
if (combo_timezone == NULL) {
g_print("[ERROR] can not get combobox timezone object\n");
return;
}
FILE *fp = popen("timedatectl list-timezones", "r");
timezone_num = 0;
for (int i = 0; i < 512; i++) {
char *buf = fgets(timezone_buffer[i], 32, fp);
if (buf == NULL) {
break;
} else {
int size = strlen(timezone_buffer[i]);
timezone_buffer[i][size - 1] = 0; // remove '\n' char.
fseek(fp, strlen(buf), SEEK_CUR);
timezone_num++;
}
}
pclose(fp);
#ifdef DEBUG
g_print("[DEBUG] Get %d timezones\n", timezone_num);
#endif
store = gtk_list_store_new(1, G_TYPE_STRING);
for (int i = 0; i < timezone_num; i++) {
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, timezone_buffer[i], -1);
}
gtk_combo_box_set_model(GTK_COMBO_BOX(combo_timezone), GTK_TREE_MODEL(store));
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);
}
static void date_activate(GObject *entry, gpointer user_data) {
}
static void button_handler(GObject *button, gpointer user_data) {
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];
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);
#ifdef DEBUG
g_print("[DEBUG] Date: %s\n", date_text);
#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);
#ifdef DEBUG
g_print("[DEBUG] Time: %s:%s:%s\n", hour_text, minute_text, second_text);
#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);
}
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;
GObject *button_ok;
GObject *button_cancel;
gchar text[3] = {0};
entry_date = gtk_builder_get_object(builder, "entry-date");
if (entry_date == NULL) {
g_print("[ERROR] can not get entry date object\n");
return -1;
}
entry_hour = gtk_builder_get_object(builder, "comboboxtext-entry-hour");
if (entry_hour == NULL) {
g_print("[ERROR] can not get entry hour object\n");
return -1;
}
entry_minute = gtk_builder_get_object(builder, "comboboxtext-entry-minute");
if (entry_minute == NULL) {
g_print("[ERROR] can not get entry minute object\n");
return -1;
}
entry_second = gtk_builder_get_object(builder, "comboboxtext-entry-second");
if (entry_second == NULL) {
g_print("[ERROR] can not get entry second object\n");
return -1;
}
combo_hour = gtk_builder_get_object(builder, "comboboxtext-hour");
if (combo_hour == NULL) {
g_print("[ERROR] can not get combo box hour object\n");
return -1;
}
/* Add hour combo box context */
for (int i = 0; i < 24; i++) {
sprintf(text, "%d", i);
gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combo_hour), text, text);
}
combo_minute = gtk_builder_get_object(builder, "comboboxtext-minute");
if (combo_minute == NULL) {
g_print("[ERROR] can not get combo box minute object\n");
return -1;
}
/* Add minute combo box context */
for (int i = 0; i < 60; i++) {
sprintf(text, "%d", i);
gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combo_minute), text, text);
}
combo_second = gtk_builder_get_object(builder, "comboboxtext-second");
if (combo_second == NULL) {
g_print("[ERROR] can not get combo box second object\n");
return -1;
}
/* Add second combo box context */
for (int i = 0; i < 60; i++) {
sprintf(text, "%d", i);
gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combo_second), text, text);
}
button_ok = gtk_builder_get_object(builder, "button-ok");
if (button_ok == NULL) {
g_print("[ERROR] can not get button ok object\n");
return -1;
}
button_cancel = gtk_builder_get_object(builder, "button-cancel");
if (button_cancel == NULL) {
g_print("[ERROR] can not get button cancel object\n");
return -1;
}
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");
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");
return 0;
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
GtkBuilder *builder = gtk_builder_new();
GtkWidget *dialog;
gtk_init(&argc, &argv);
if (!gtk_builder_add_from_file(builder, "timemanager.ui", NULL)) {
g_print("[ERROR] can not load glade file\n");
return -1;
}
dialog = GTK_WIDGET(gtk_builder_get_object(builder, "dialog-date_time"));
if (dialog == NULL) {
g_print("[ERROR] can not get dialog object\n");
return -1;
}
gtk_window_set_title(GTK_WINDOW(dialog), "Date & Time");
gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 200);
g_signal_connect(dialog, GTK_EVENT_DESTROY, G_CALLBACK(gtk_main_quit), NULL);
gobjects_init(builder, dialog);
get_time_date_status();
timezone_init(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);
// 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;
// return status;
}
File added
This diff is collapsed.
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