3 // Copyright 2009 Michael Cronenworth <mike@cchtml.com>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 // MA 02110-1301, USA.
28 static GtkWidget *timerLabel = NULL;
29 static GtkWidget *timerHistoryLabel1 = NULL;
30 static GtkWidget *timerHistoryLabel2 = NULL;
31 static GtkWidget *timerHistoryLabel3 = NULL;
32 static GSList *historyList = NULL;
33 static int stopishMode = STOPISH_MODE_START;
34 static int timerHandle = -1;
37 gint dbus_callback( const gchar *interface, const gchar *method,
38 GArray *arguments, gpointer data, osso_rpc_t *retval );
39 static GtkWindow *stopish_new( void );
40 static void start_cb( GtkButton* button, gpointer data );
41 static void reset_cb( GtkButton* button, gpointer data );
42 static void close_cb( GtkButton* button, gpointer data );
45 int main( int argc, char *argv[] )
51 //printf( "stopish: starting up\n" );
53 ctxt = osso_initialize( "com.nokia.stopish", PACKAGE_VERSION, TRUE, NULL );
55 fprintf( stderr, "osso_initialize failed.\n" );
59 gtk_init( &argc, &argv );
61 window = stopish_new( );
63 ret = osso_rpc_set_default_cb_f( ctxt, dbus_callback, window );
64 if ( ret != OSSO_OK ) {
65 fprintf( stderr, "osso_rpc_set_default_cb_f failed: %d.\n", ret );
75 gint dbus_callback( const gchar *interface, const gchar *method,
76 GArray *arguments, gpointer data, osso_rpc_t *retval )
78 //printf( "stopish dbus: %s, %s\n", interface, method );
80 if ( !strcmp( method, "top_application" ) )
81 gtk_window_present( GTK_WINDOW( data ) );
83 retval->type = DBUS_TYPE_INVALID;
89 int stopish_get_mode( void )
95 static GtkWindow *stopish_new( void )
97 GtkWidget *window, *hBox, *label, *button, *button0;
98 GtkWidget *vBox, *vBox0, *vBox1;
100 window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
102 gtk_container_set_border_width( GTK_CONTAINER( window ), 20 );
104 gtk_window_set_title( GTK_WINDOW( window ), "Stopish" );
106 g_signal_connect( G_OBJECT( window ), "destroy",
107 G_CALLBACK( close_cb ), window );
109 vBox = gtk_vbox_new( FALSE, 20 );
111 label = gtk_label_new( "Stopish - The Stopwatch" );
112 gtk_box_pack_start( GTK_BOX( vBox ), label, FALSE, FALSE, 0 );
114 hBox = gtk_hbox_new( FALSE, 10 );
117 vBox0 = gtk_vbox_new( FALSE, 5 );
118 gtk_widget_set_size_request( vBox0, 250, -1 );
121 timerLabel = gtk_label_new( NULL );
122 gtk_label_set_markup( GTK_LABEL( timerLabel ),
123 "<span font_family=\"monospace\" size=\"xx-large\">00:00:00.0</span>" );
124 gtk_container_add( GTK_CONTAINER( vBox0 ), timerLabel );
127 timerHistoryLabel1 = gtk_label_new( NULL );
128 gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel1, FALSE, FALSE, 0 );
129 timerHistoryLabel2 = gtk_label_new( NULL );
130 gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel2, FALSE, FALSE, 0 );
131 timerHistoryLabel3 = gtk_label_new( NULL );
132 gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel3, FALSE, FALSE, 0 );
133 label = gtk_label_new( NULL );
134 gtk_container_add( GTK_CONTAINER( vBox0 ), label );
136 gtk_container_add( GTK_CONTAINER( hBox ), vBox0 );
139 vBox1 = gtk_vbox_new( FALSE, 15 );
140 gtk_widget_set_size_request( vBox1, 200, -1 );
142 // start/pause stopwatch button
143 button = gtk_button_new_with_label( "Start" );
144 button0 = gtk_button_new_with_label( "Reset" );
145 gtk_widget_set_size_request( button, -1, 60 );
146 g_signal_connect( G_OBJECT( button ), "clicked",
147 G_CALLBACK( start_cb ), button0 );
148 gtk_box_pack_start( GTK_BOX( vBox1 ), button, FALSE, FALSE, 0 );
151 gtk_widget_set_sensitive( button0, FALSE );
152 gtk_widget_set_size_request( button0, -1, 60 );
153 g_signal_connect( G_OBJECT( button0 ), "clicked",
154 G_CALLBACK( reset_cb ), button );
155 gtk_box_pack_start( GTK_BOX( vBox1 ), button0, FALSE, FALSE, 0 );
157 gtk_container_add( GTK_CONTAINER( hBox ), vBox1 );
159 gtk_container_add( GTK_CONTAINER( vBox ), hBox );
161 gtk_container_add( GTK_CONTAINER( window ), vBox );
163 gtk_widget_show_all( window );
165 return GTK_WINDOW( window );
169 static void start_cb( GtkButton* button, gpointer data )
171 if ( stopishMode == STOPISH_MODE_START ) {
172 // set label text and add timer handle
173 gtk_button_set_label( button, "Pause" );
174 stopishMode = STOPISH_MODE_PAUSE;
175 stopish_set_time_start( stopish_current_time( ) );
176 timerHandle = g_timeout_add( 100, stopish_timeout_cb, timerLabel );
178 else if ( stopishMode == STOPISH_MODE_RESUME ) {
180 gtk_button_set_label( button, "Pause" );
181 stopishMode = STOPISH_MODE_PAUSE;
182 stopish_timer_resume( );
183 timerHandle = g_timeout_add( 100, stopish_timeout_cb, timerLabel );
186 // pause timer, remove timeout
187 gtk_button_set_label( button, "Resume" );
188 stopishMode = STOPISH_MODE_RESUME;
189 g_source_remove( timerHandle );
190 stopish_timer_save( );
193 // allow user to reset timer
194 gtk_widget_set_sensitive( GTK_WIDGET( data ), TRUE );
198 static void reset_cb( GtkButton* button, gpointer data )
203 if ( stopishMode == STOPISH_MODE_RESUME )
204 stopish_timer_resume( );
206 // set label text and remove timer handle
207 gtk_button_set_label( GTK_BUTTON( data ), "Start" );
208 stopishMode = STOPISH_MODE_START;
209 gtk_label_set_markup( GTK_LABEL( timerLabel ),
210 "<span font_family=\"monospace\" size=\"xx-large\">00:00:00.0</span>" );
211 g_source_remove( timerHandle );
213 // add current time to history
214 historyList = g_slist_prepend( historyList,
215 ( gpointer ) stopish_get_time_string( ) );
216 gtk_label_set_text( GTK_LABEL( timerHistoryLabel1 ),
217 ( char * ) historyList->data );
218 tempList = historyList;
219 tempList = g_slist_next( tempList );
221 gtk_label_set_text( GTK_LABEL( timerHistoryLabel2 ),
222 ( char * ) tempList->data );
223 tempList = g_slist_next( tempList );
225 gtk_label_set_text( GTK_LABEL( timerHistoryLabel3 ),
226 ( char * ) tempList->data );
228 // remove the history time after the 3rd
229 tempList = g_slist_next( tempList );
231 tempString = tempList->data;
232 historyList = g_slist_remove( historyList, tempList->data );
237 stopish_set_time_start( 0 );
239 // disallow user to reset timer
240 gtk_widget_set_sensitive( GTK_WIDGET( button ), FALSE );
244 static void close_cb( GtkButton* button, gpointer data )
246 // destroy main window and exit gtk main loop
247 gtk_widget_destroy( GTK_WIDGET( data ) );