Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
T
TimeManager
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
CI / CD
CI / CD
Pipelines
Schedules
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
linux-tools
TimeManager
Commits
fb1c7f83
Commit
fb1c7f83
authored
Jun 12, 2019
by
Carit Zhu
🎱
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add timemanager.ui from glade and start gtk app by it.
parent
407106c5
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
539 additions
and
7 deletions
+539
-7
Makefile
Makefile
+12
-0
main.c
main.c
+276
-7
timemanager
timemanager
+0
-0
timemanager.ui
timemanager.ui
+251
-0
No files found.
Makefile
0 → 100644
View file @
fb1c7f83
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
main.c
View file @
fb1c7f83
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <gtk/gtk.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) {
static void print_hello(GtkWidget *widget, gpointer data) {
g_print("Hello World\n");
g_print("Hello World\n");
}
}
...
@@ -23,15 +44,263 @@ static void activate(GtkApplication *app, gpointer user_data) {
...
@@ -23,15 +44,263 @@ static void activate(GtkApplication *app, gpointer user_data) {
gtk_widget_show_all(window);
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
)
{
int
main
(
int
argc
,
char
**
argv
)
{
GtkApplication
*
app
;
GtkBuilder
*
builder
=
gtk_builder_new
();
int
status
;
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
);
//
app = gtk_application_new("com.witium.timemanager", G_APPLICATION_FLAGS_NONE);
g_signal_connect
(
app
,
"activate"
,
G_CALLBACK
(
activate
),
NULL
);
//
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status
=
g_application_run
(
G_APPLICATION
(
app
),
argc
,
argv
);
//
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref
(
app
);
//
g_object_unref(app);
return
status
;
//
return status;
}
}
timemanager
0 → 100755
View file @
fb1c7f83
File added
timemanager.ui
0 → 100644
View file @
fb1c7f83
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires
lib=
"gtk+"
version=
"3.12"
/>
<object
class=
"GtkDialog"
id=
"dialog-date_time"
>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"window_position"
>
center
</property>
<property
name=
"type_hint"
>
utility
</property>
<property
name=
"gravity"
>
center
</property>
<child
internal-child=
"vbox"
>
<object
class=
"GtkBox"
id=
"dialog-vbox-date_time"
>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"orientation"
>
vertical
</property>
<property
name=
"spacing"
>
2
</property>
<child
internal-child=
"action_area"
>
<object
class=
"GtkButtonBox"
id=
"dialog-action_area-date_time"
>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"layout_style"
>
end
</property>
<child>
<object
class=
"GtkButton"
id=
"button-ok"
>
<property
name=
"label"
translatable=
"yes"
>
OK
</property>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
True
</property>
<property
name=
"receives_default"
>
True
</property>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
0
</property>
</packing>
</child>
<child>
<object
class=
"GtkButton"
id=
"button-cancel"
>
<property
name=
"label"
translatable=
"yes"
>
Cancel
</property>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
True
</property>
<property
name=
"receives_default"
>
True
</property>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
1
</property>
</packing>
</child>
</object>
<packing>
<property
name=
"expand"
>
False
</property>
<property
name=
"fill"
>
False
</property>
<property
name=
"position"
>
0
</property>
</packing>
</child>
<child>
<object
class=
"GtkBox"
id=
"box-date_time"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"orientation"
>
vertical
</property>
<child>
<object
class=
"GtkBox"
id=
"box-date"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<child>
<object
class=
"GtkLabel"
id=
"label-date"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"label"
translatable=
"yes"
>
Date
</property>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
0
</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
0
</property>
</packing>
</child>
<child>
<object
class=
"GtkEntry"
id=
"entry-date"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
True
</property>
<property
name=
"margin_left"
>
48
</property>
<property
name=
"margin_right"
>
48
</property>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
False
</property>
<property
name=
"padding"
>
2
</property>
<property
name=
"position"
>
1
</property>
</packing>
</child>
<child>
<object
class=
"GtkBox"
id=
"box-time"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<child>
<object
class=
"GtkLabel"
id=
"label-time"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"label"
translatable=
"yes"
>
Time
</property>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
0
</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
2
</property>
</packing>
</child>
<child>
<object
class=
"GtkBox"
id=
"box-time_clock"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<child>
<object
class=
"GtkComboBoxText"
id=
"comboboxtext-hour"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"button_sensitivity"
>
on
</property>
<property
name=
"has_entry"
>
True
</property>
<child
internal-child=
"entry"
>
<object
class=
"GtkEntry"
id=
"comboboxtext-entry-hour"
>
<property
name=
"can_focus"
>
False
</property>
</object>
</child>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"padding"
>
2
</property>
<property
name=
"position"
>
0
</property>
</packing>
</child>
<child>
<object
class=
"GtkComboBoxText"
id=
"comboboxtext-minute"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"button_sensitivity"
>
on
</property>
<property
name=
"has_entry"
>
True
</property>
<child
internal-child=
"entry"
>
<object
class=
"GtkEntry"
id=
"comboboxtext-entry-minute"
>
<property
name=
"can_focus"
>
False
</property>
</object>
</child>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"padding"
>
2
</property>
<property
name=
"position"
>
1
</property>
</packing>
</child>
<child>
<object
class=
"GtkComboBoxText"
id=
"comboboxtext-second"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"button_sensitivity"
>
on
</property>
<property
name=
"has_entry"
>
True
</property>
<child
internal-child=
"entry"
>
<object
class=
"GtkEntry"
id=
"comboboxtext-entry-second"
>
<property
name=
"can_focus"
>
False
</property>
</object>
</child>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"padding"
>
2
</property>
<property
name=
"position"
>
2
</property>
</packing>
</child>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
3
</property>
</packing>
</child>
<child>
<object
class=
"GtkBox"
id=
"box-timezone"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<child>
<object
class=
"GtkLabel"
id=
"label-timezone"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"label"
translatable=
"yes"
>
TimeZone
</property>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
0
</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
4
</property>
</packing>
</child>
<child>
<object
class=
"GtkComboBox"
id=
"combobox-timezone"
>
<property
name=
"visible"
>
True
</property>
<property
name=
"can_focus"
>
False
</property>
<property
name=
"margin_left"
>
48
</property>
<property
name=
"margin_right"
>
48
</property>
<property
name=
"active"
>
0
</property>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
False
</property>
<property
name=
"padding"
>
2
</property>
<property
name=
"position"
>
5
</property>
</packing>
</child>
</object>
<packing>
<property
name=
"expand"
>
True
</property>
<property
name=
"fill"
>
True
</property>
<property
name=
"position"
>
1
</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment