tazweb view src/main.c @ rev 44

Handle download-requested and finally use xterm/wget for now
author Christophe Lincoln <pankso@slitaz.org>
date Tue Apr 19 00:42:04 2011 +0200 (2011-04-19)
parents 435fdb863811
children 020cf8b6e14e
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;
21 /* Page title to window title */
22 static void
23 update_title (GtkWindow* window)
24 {
25 GString* string = g_string_new (main_title);
26 /* g_string_append (string, " - TazWeb"); */
27 if (load_progress < 100)
28 g_string_append_printf (string, " (%f%%)", load_progress);
29 gchar* title = g_string_free (string, FALSE);
30 gtk_window_set_title (window, title);
31 g_free (title);
32 }
34 static void
35 activate_uri_entry_cb (GtkWidget* entry, gpointer data)
36 {
37 const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
38 g_assert (uri);
39 webkit_web_view_load_uri (web_view, uri);
40 }
42 static void
43 notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
44 {
45 if (main_title)
46 g_free (main_title);
47 main_title = g_strdup (webkit_web_view_get_title(web_view));
48 update_title (GTK_WINDOW (main_window));
49 }
51 /* Request progress in window title */
52 static void
53 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
54 {
55 load_progress = webkit_web_view_get_progress (web_view) * 100;
56 update_title (GTK_WINDOW (main_window));
57 }
59 static void
60 notify_load_status_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
61 {
62 if (webkit_web_view_get_load_status (web_view) == WEBKIT_LOAD_COMMITTED) {
63 WebKitWebFrame* frame = webkit_web_view_get_main_frame (web_view);
64 const gchar* uri = webkit_web_frame_get_uri (frame);
65 if (uri)
66 gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
67 }
68 }
70 static void
71 destroy_cb (GtkWidget* widget, gpointer data)
72 {
73 gtk_main_quit ();
74 }
76 /* Home button function */
77 static void
78 go_home_cb (GtkWidget* widget, gpointer data)
79 {
80 const gchar* uri = ("file:///usr/share/webhome/index.html");
81 g_assert (uri);
82 webkit_web_view_load_uri (web_view, uri);
83 }
85 /* Bookmarks button function */
86 static void
87 bookmarks_cb (GtkWidget* widget, gpointer data)
88 {
89 const gchar* uri = g_strdup_printf ("file://%s/.config/tazweb/page.html",
90 g_get_home_dir ());
91 g_assert (uri);
92 webkit_web_view_load_uri (web_view, uri);
93 }
95 /* Navigation button function */
96 static void
97 go_back_cb (GtkWidget* widget, gpointer data)
98 {
99 webkit_web_view_go_back (web_view);
100 }
102 static void
103 go_forward_cb (GtkWidget* widget, gpointer data)
104 {
105 webkit_web_view_go_forward (web_view);
106 }
108 static void
109 refresh_cb (GtkWidget* widget, gpointer data)
110 {
111 webkit_web_view_reload (web_view);
112 }
114 /* Fullscreen and unfullscreen action */
115 static void
116 fullscreen_cb (GtkWindow* window, gpointer data)
117 {
118 GdkWindowState state;
119 state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (main_window)));
121 if (state & GDK_WINDOW_STATE_FULLSCREEN)
122 gtk_window_unfullscreen (GTK_WINDOW (main_window));
123 else
124 gtk_window_fullscreen (GTK_WINDOW (main_window));
125 }
127 /* TazWeb doc function */
128 static void
129 tazweb_doc_cb (GtkWidget* widget, gpointer data)
130 {
131 const gchar* uri = ("file:///usr/share/doc/tazweb/tazweb.html");
132 g_assert (uri);
133 webkit_web_view_load_uri (web_view, uri);
134 }
136 /* Download function */
137 static gboolean
138 download_requested_cb (WebKitWebView *web_view, WebKitDownload *download,
139 gpointer user_data)
140 {
141 const gchar* uri = webkit_download_get_uri (download);
142 gchar *buffer;
143 asprintf (&buffer, "xterm -e \"wget -P $HOME/Downloads %s\"", uri);
144 system (buffer);
145 }
147 static GtkWidget*
148 create_browser ()
149 {
150 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
151 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
152 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
154 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
155 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
157 g_signal_connect (web_view, "notify::title",
158 G_CALLBACK (notify_title_cb), web_view);
159 g_signal_connect (web_view, "notify::progress",
160 G_CALLBACK (notify_progress_cb), web_view);
161 g_signal_connect (web_view, "notify::load-status",
162 G_CALLBACK (notify_load_status_cb), web_view);
163 g_signal_connect (web_view, "download-requested",
164 G_CALLBACK (download_requested_cb), NULL);
166 return scrolled_window;
167 }
169 /* Create an icon */
170 static GdkPixbuf*
171 create_pixbuf (const gchar * image)
172 {
173 GdkPixbuf *pixbuf;
174 pixbuf = gdk_pixbuf_new_from_file (image, NULL);
176 return pixbuf;
177 }
179 static GtkWidget*
180 create_toolbar ()
181 {
182 GtkWidget* toolbar = gtk_toolbar_new ();
183 GtkToolItem* item;
185 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
186 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
188 /* The back button */
189 item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
190 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_back_cb), NULL);
191 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
193 /* The forward button */
194 item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_FORWARD);
195 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_forward_cb), NULL);
196 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
198 /* The Reload button */
199 item = gtk_tool_button_new_from_stock (GTK_STOCK_REFRESH);
200 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (refresh_cb), NULL);
201 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
203 /* The URL entry */
204 item = gtk_tool_item_new ();
205 gtk_tool_item_set_expand (item, TRUE);
206 uri_entry = gtk_entry_new ();
207 gtk_container_add (GTK_CONTAINER (item), uri_entry);
208 g_signal_connect (G_OBJECT (uri_entry), "activate",
209 G_CALLBACK (activate_uri_entry_cb), NULL);
210 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
212 /* The Home button */
213 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
214 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_home_cb), NULL);
215 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
217 /* The Bookmarks button */
218 item = gtk_tool_button_new_from_stock (GTK_STOCK_PREFERENCES);
219 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (bookmarks_cb), NULL);
220 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
222 /* The TazWeb doc button */
223 item = gtk_tool_button_new_from_stock (GTK_STOCK_INFO);
224 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (tazweb_doc_cb), NULL);
225 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
227 /* The Fullscreen button */
228 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
229 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (fullscreen_cb), NULL);
230 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
232 return toolbar;
233 }
235 static GtkWidget*
236 create_window ()
237 {
238 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
240 /* Default tazweb window size ratio to 3/4 ?? --> 720, 540*/
241 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
242 gtk_window_set_icon (GTK_WINDOW (window),
243 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
244 gtk_widget_set_name (window, "TazWeb");
245 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
247 return window;
248 }
250 int
251 main (int argc, char* argv[])
252 {
253 gtk_init (&argc, &argv);
254 if (!g_thread_supported ())
255 g_thread_init (NULL);
257 /* Get a default page.html if missing */
258 const gchar* config = g_strdup_printf ("%s/.config/tazweb", g_get_home_dir ());
259 if (!g_file_test(config, G_FILE_TEST_EXISTS)) {
260 g_mkdir (config, 0700);
261 system ("cp /usr/share/tazweb/*.html $HOME/.config/tazweb");
262 system ("cp /usr/share/tazweb/*.css $HOME/.config/tazweb");
263 }
265 GtkWidget* vbox = gtk_vbox_new (FALSE, 2);
266 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
267 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
269 main_window = create_window ();
270 gtk_container_add (GTK_CONTAINER (main_window), vbox);
272 /* Home page url or file */
273 gchar* uri = (gchar*) (argc > 1 ? argv[1] :
274 "file:///usr/share/webhome/index.html");
275 webkit_web_view_load_uri (web_view, uri);
277 gtk_widget_grab_focus (GTK_WIDGET (web_view));
278 gtk_widget_show_all (main_window);
279 gtk_main ();
281 return 0;
282 }