tazweb view src/main.c @ rev 51

Code clean-up (use tab to ident)
author Christophe Lincoln <pankso@slitaz.org>
date Thu Apr 21 19:43:42 2011 +0200 (2011-04-21)
parents 22991c13b29b
children 6995fa0828c0
line source
1 /*
2 * TazWeb is a radically simple web browser providing a single window
3 * with a single toolbar with buttons and an URL entry, but no menu or
4 * tabs.
5 *
6 * Copyright (C) 2011 SliTaz GNU/Linux <devel@slitaz.org>
7 *
8 *
9 */
11 #include <gtk/gtk.h>
12 #include <webkit/webkit.h>
14 static GtkWidget* main_window;
15 static WebKitWebView* web_view;
16 static GtkWidget* uri_entry;
17 static gchar* main_title;
18 static gdouble load_progress;
19 static guint status_context_id;
20 const char* config;
22 /* Create an icon */
23 static GdkPixbuf*
24 create_pixbuf (const gchar * image)
25 {
26 GdkPixbuf *pixbuf;
27 pixbuf = gdk_pixbuf_new_from_file (image, NULL);
28 return pixbuf;
29 }
31 /* Get a default page.html if missing */
32 static void
33 get_config ()
34 {
35 config = g_strdup_printf ("%s/.config/tazweb", g_get_home_dir ());
36 if (!g_file_test(config, G_FILE_TEST_EXISTS)) {
37 g_mkdir (config, 0700);
38 system ("cp /usr/share/tazweb/* $HOME/.config/tazweb");
39 }
40 }
42 /* Page title to window title */
43 static void
44 update_title (GtkWindow* window)
45 {
46 GString* string = g_string_new (main_title);
47 /* g_string_append (string, " - TazWeb"); */
48 if (load_progress < 100)
49 g_string_append_printf (string, " [ %f%% ] ", load_progress);
50 gchar* title = g_string_free (string, FALSE);
51 gtk_window_set_title (window, title);
52 g_free (title);
53 }
55 static void
56 notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
57 {
58 if (main_title)
59 g_free (main_title);
60 main_title = g_strdup (webkit_web_view_get_title(web_view));
61 update_title (GTK_WINDOW (main_window));
62 }
64 /* Request progress in window title */
65 static void
66 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
67 {
68 load_progress = webkit_web_view_get_progress (web_view) * 100;
69 update_title (GTK_WINDOW (main_window));
70 }
72 static void
73 notify_load_status_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
74 {
75 if (webkit_web_view_get_load_status (web_view) == WEBKIT_LOAD_COMMITTED) {
76 WebKitWebFrame* frame = webkit_web_view_get_main_frame (web_view);
77 const gchar* uri = webkit_web_frame_get_uri (frame);
78 if (uri)
79 gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
80 }
81 }
83 /* URL entry callback function */
84 static void
85 activate_uri_entry_cb (GtkWidget* entry, gpointer data)
86 {
87 const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
88 g_assert (uri);
89 webkit_web_view_load_uri (web_view, uri);
90 }
92 static void
93 destroy_cb (GtkWidget* widget, gpointer data)
94 {
95 gtk_main_quit ();
96 }
98 /* Home button function */
99 static void
100 go_home_cb (GtkWidget* widget, gpointer data)
101 {
102 const gchar* uri = ("file:///usr/share/webhome/index.html");
103 g_assert (uri);
104 webkit_web_view_load_uri (web_view, uri);
105 }
107 /* My page button function */
108 static void
109 my_page_cb (GtkWidget* widget, gpointer data)
110 {
111 const gchar* uri = g_strdup_printf ("file://%s/.config/tazweb/page.html",
112 g_get_home_dir ());
113 g_assert (uri);
114 webkit_web_view_load_uri (web_view, uri);
115 }
117 /* Fullscreen and unfullscreen function */
118 static void
119 fullscreen_cb (GtkWindow* window, gpointer data)
120 {
121 GdkWindowState state;
122 state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (main_window)));
124 if (state & GDK_WINDOW_STATE_FULLSCREEN)
125 gtk_window_unfullscreen (GTK_WINDOW (main_window));
126 else
127 gtk_window_fullscreen (GTK_WINDOW (main_window));
128 }
130 /* TazWeb doc function */
131 static void
132 tazweb_doc_cb (GtkWidget* widget, gpointer data)
133 {
134 const gchar* uri = ("file:///usr/share/doc/tazweb/tazweb.html");
135 g_assert (uri);
136 webkit_web_view_load_uri (web_view, uri);
137 }
139 /* Download function */
140 static gboolean
141 download_requested_cb (WebKitWebView *web_view, WebKitDownload *download,
142 gpointer user_data)
143 {
144 const gchar* uri = webkit_download_get_uri (download);
145 gchar *buffer;
146 asprintf (&buffer, "tazbox dl-out %s", uri);
147 system (buffer);
148 }
150 static GtkWidget*
151 create_browser ()
152 {
153 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
154 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
155 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
157 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
158 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
160 g_signal_connect (web_view, "notify::title",
161 G_CALLBACK (notify_title_cb), web_view);
162 g_signal_connect (web_view, "notify::progress",
163 G_CALLBACK (notify_progress_cb), web_view);
164 g_signal_connect (web_view, "notify::load-status",
165 G_CALLBACK (notify_load_status_cb), web_view);
166 g_signal_connect (web_view, "download-requested",
167 G_CALLBACK (download_requested_cb), NULL);
169 return scrolled_window;
170 }
172 static GtkWidget*
173 create_toolbar ()
174 {
175 GtkToolItem* item;
177 GtkWidget* toolbar = gtk_toolbar_new ();
178 gtk_widget_set_size_request (toolbar, 0, 31);
179 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar),
180 GTK_ORIENTATION_HORIZONTAL);
181 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar),
182 GTK_TOOLBAR_BOTH_HORIZ);
184 /* The Home button */
185 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
186 g_signal_connect (G_OBJECT (item), "clicked",
187 G_CALLBACK (go_home_cb), NULL);
188 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
190 /* The personal page button */
191 item = gtk_tool_button_new_from_stock (GTK_STOCK_PREFERENCES);
192 g_signal_connect (G_OBJECT (item), "clicked",
193 G_CALLBACK (my_page_cb), NULL);
194 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
196 /* The URL entry */
197 item = gtk_tool_item_new ();
198 gtk_tool_item_set_expand (item, TRUE);
199 uri_entry = gtk_entry_new ();
200 gtk_container_add (GTK_CONTAINER (item), uri_entry);
201 g_signal_connect (G_OBJECT (uri_entry), "activate",
202 G_CALLBACK (activate_uri_entry_cb), NULL);
203 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
205 /* The TazWeb doc button */
206 item = gtk_tool_button_new_from_stock (GTK_STOCK_INFO);
207 g_signal_connect (G_OBJECT (item), "clicked",
208 G_CALLBACK (tazweb_doc_cb), NULL);
209 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
211 /* The Fullscreen button */
212 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
213 g_signal_connect (G_OBJECT (item), "clicked",
214 G_CALLBACK (fullscreen_cb), NULL);
215 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
217 return toolbar;
218 }
220 static GtkWidget*
221 create_window ()
222 {
223 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
225 /* Default tazweb window size ratio to 3/4 --> 720, 540*/
226 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
227 gtk_window_set_icon (GTK_WINDOW (window),
228 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
229 gtk_widget_set_name (window, "TazWeb");
230 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
232 return window;
233 }
235 int
236 main (int argc, char* argv[])
237 {
238 gtk_init (&argc, &argv);
239 if (!g_thread_supported ())
240 g_thread_init (NULL);
242 get_config ();
244 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
245 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
246 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
248 main_window = create_window ();
249 gtk_container_add (GTK_CONTAINER (main_window), vbox);
251 /* Home page url or file */
252 gchar* uri = (gchar*) (argc > 1 ? argv[1] :
253 "file:///usr/share/webhome/index.html");
254 webkit_web_view_load_uri (web_view, uri);
256 gtk_widget_grab_focus (GTK_WIDGET (web_view));
257 gtk_widget_show_all (main_window);
258 gtk_main ();
260 return 0;
261 }