1 /* $Id: main.c,v 1.8 2007/12/12 12:51:12 dforsi Exp $ */
2 
3 #ifdef HAVE_CONFIG_H
4 #  include <config.h>
5 #endif
6 
7 #include <string.h>
8 #include <unistd.h>
9 #include <gtk/gtk.h>
10 #include <glade/glade.h>
11 #include <gnokii.h>
12 
13 #include "gnocky.h"
14 #include "monitor.h"
15 #include "utils.h"
16 #include "statusbar.h"
17 #include "preferences.h"
18 
19 #include "phoneview.h"
20 #include "phonebookview.h"
21 #include "smsview.h"
22 #include "logosview.h"
23 
24 /* GUI stuff */
25 GladeXML *xml;
26 GtkWidget *window;
27 GtkWidget *main_view;
28 GtkWidget *phone_view;
29 GtkWidget *phonebook_view;
30 GtkWidget *sms_view;
31 GtkWidget *logos_view;
32 GtkWidget *current_view = NULL;
33 
34 GtkTooltips *tooltips = NULL;
35 static guint view_timeout_id = 0;
36 
37 extern GAsyncQueue *events;
38 extern GThread *monitor;
39 extern GnockyPhoneMonitor pm;
40 extern struct gn_statemachine statemachine;
41 
gnocky_set_current_view(GtkWidget * view)42 void gnocky_set_current_view(GtkWidget *view)
43 {
44 	if (current_view == view)
45 		return;
46 
47 	if (current_view) {
48 		gtk_widget_ref(current_view);
49 		gtk_container_remove(GTK_CONTAINER(main_view), current_view);
50 		/* FIXME - needed to keep recount at reasonable level*/
51 		if (G_OBJECT(current_view)->ref_count >= 2) {
52 			gtk_widget_unref(current_view);
53 		}
54 	}
55 	gtk_container_add(GTK_CONTAINER(main_view), view);
56 	current_view = view;
57 }
58 
gnocky_view_update(gpointer data)59 gint gnocky_view_update(gpointer data)
60 {
61 	gnocky_statusbar_update();
62 
63 	gnocky_phone_view_update(pm);
64 	gnocky_phonebook_view_update(pm);
65 	gnocky_sms_view_update(pm);
66 
67 	return TRUE;
68 }
69 
gnocky_button_menu_clicked(GtkWidget * widget,gpointer data)70 void gnocky_button_menu_clicked(GtkWidget *widget, gpointer data)
71 {
72 	g_print("button clicked\n");
73 	gnocky_set_current_view(data);
74 }
75 
on_preferences_button_clicked(GtkWidget * widget,gpointer data)76 void on_preferences_button_clicked(GtkWidget *widget, gpointer data)
77 {
78 	create_preferences(&statemachine.config);
79 }
80 
on_about_activate(GtkWidget * widget,gpointer data)81 void on_about_activate(GtkWidget *widget, gpointer data)
82 {
83 	gnocky_show_message(GTK_MESSAGE_INFO,"Gnocky " VERSION "\n(C) Copyleft 2004 Igor Popik");
84 }
85 
gnocky_set_button_active(gboolean active)86 void gnocky_set_button_active(gboolean active)
87 {
88 	GtkWidget *button;
89 
90 	if (!active) {
91 		button = glade_xml_get_widget(xml, "phone_button");
92 		gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE);
93 	}
94 
95 	button = glade_xml_get_widget(xml, "phonebook_button");
96 	gtk_widget_set_sensitive(button, active);
97 
98 	button = glade_xml_get_widget(xml, "sms_button");
99 	gtk_widget_set_sensitive(button, active);
100 
101 	button = glade_xml_get_widget(xml, "logos_button");
102 	if (active && g_strcasecmp(statemachine.config.model, "AT"))
103 		gtk_widget_set_sensitive(button, TRUE);
104 	else
105 		gtk_widget_set_sensitive(button, FALSE);
106 }
107 
gnocky_set_main_gui_callbacks()108 void gnocky_set_main_gui_callbacks()
109 {
110 	GtkWidget *button;
111 
112 	button = glade_xml_get_widget(xml, "phone_button");
113 
114 	if (button)
115 		g_signal_connect(G_OBJECT(button), "clicked", (GCallback) gnocky_button_menu_clicked, phone_view);
116 
117 	button = glade_xml_get_widget(xml, "phonebook_button");
118 	if (button)
119 		g_signal_connect(G_OBJECT(button), "clicked", (GCallback) gnocky_button_menu_clicked, phonebook_view);
120 	gtk_widget_set_sensitive(button, FALSE);
121 
122 	button = glade_xml_get_widget(xml, "sms_button");
123 	if (button)
124 		g_signal_connect(G_OBJECT(button), "clicked", (GCallback) gnocky_button_menu_clicked, sms_view);
125 	gtk_widget_set_sensitive(button, FALSE);
126 
127 	button = glade_xml_get_widget(xml, "logos_button");
128 	if (button)
129 		g_signal_connect(G_OBJECT(button), "clicked", (GCallback) gnocky_button_menu_clicked, logos_view);
130 	gtk_widget_set_sensitive(button, FALSE);
131 }
132 
gnocky_interface_destroy()133 void gnocky_interface_destroy()
134 {
135 	if (view_timeout_id)
136 		g_source_remove(view_timeout_id);
137 
138 	gnocky_logos_view_destroy(logos_view);
139 	gnocky_sms_view_destroy(sms_view);
140 	gnocky_phonebook_view_destroy(phonebook_view);
141 	gnocky_phone_view_destroy(phone_view);
142 
143 	gnocky_statusbar_terminate();
144 
145 	if (main_view)
146 		gtk_widget_destroy(main_view);
147 /* this will be destroyed later by GTK
148 	if (window)
149 		gtk_widget_destroy(window);
150 */
151 	if (xml)
152 		g_object_unref(xml);
153 }
154 
gnocky_interface_create()155 void gnocky_interface_create()
156 {
157 	/* main interface */
158 	xml = create_gladexml("gnocky.glade", NULL);
159 
160 	if (!xml) {
161 		gnocky_show_error("Cannot create user interface!\n");
162 		gtk_main_quit();
163 	}
164 
165 	glade_xml_signal_autoconnect(xml);
166 	window = glade_xml_get_widget(xml, "window");
167 
168 	main_view = glade_xml_get_widget(xml, "main_view");
169 
170 	gnocky_statusbar_init();
171 
172 	/* views creation */
173 	phone_view = gnocky_phone_view_create();
174 	phonebook_view = gnocky_phonebook_view_create();
175 	sms_view = gnocky_sms_view_create();
176 	logos_view = gnocky_logos_view_create();
177 
178 	gnocky_set_main_gui_callbacks();
179 
180 	/* setting the default view */
181 	gnocky_set_current_view(phone_view);
182 
183 	tooltips = gtk_tooltips_new();
184 
185 	gtk_tooltips_enable(tooltips);
186 
187 	view_timeout_id = g_timeout_add(1000, gnocky_view_update, NULL);
188 }
189 
gnocky_main_quit()190 void gnocky_main_quit()
191 {
192     gtk_main_quit();
193 }
194 
195 int
main(int argc,char * argv[])196 main (int argc, char *argv[])
197 {
198 
199 #ifdef ENABLE_NLS
200 	bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
201 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
202 	textdomain (GETTEXT_PACKAGE);
203 #endif
204 
205 	gtk_init(&argc, &argv);
206 	g_thread_init(NULL);
207 
208 	events = g_async_queue_new();
209 
210 	gnocky_interface_create();
211 
212 	gnocky_monitor_create();
213 
214 	gtk_main ();
215 
216 	gnocky_monitor_destroy();
217 
218 	gnocky_interface_destroy();
219 
220 	return 0;
221 }
222