tazweb view src/main.c @ rev 47

Use 'tazbox out-dl' to download
author Christophe Lincoln <pankso@slitaz.org>
date Tue Apr 19 13:35:52 2011 +0200 (2011-04-19)
parents 329c2beaf754
children fe087b478df1
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 notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
36 {
37 if (main_title)
38 g_free (main_title);
39 main_title = g_strdup (webkit_web_view_get_title(web_view));
40 update_title (GTK_WINDOW (main_window));
41 }
43 /* Request progress in window title */
44 static void
45 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
46 {
47 load_progress = webkit_web_view_get_progress (web_view) * 100;
48 update_title (GTK_WINDOW (main_window));
49 }
51 static void
52 notify_load_status_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
53 {
54 if (webkit_web_view_get_load_status (web_view) == WEBKIT_LOAD_COMMITTED) {
55 WebKitWebFrame* frame = webkit_web_view_get_main_frame (web_view);
56 const gchar* uri = webkit_web_frame_get_uri (frame);
57 if (uri)
58 gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
59 }
60 }
62 /* URL entry callback function */
63 static void
64 activate_uri_entry_cb (GtkWidget* entry, gpointer data)
65 {
66 const gchar* uri = gtk_entry_get_text (GTK_ENTRY (entry));
67 g_assert (uri);
68 webkit_web_view_load_uri (web_view, uri);
69 }
71 static void
72 destroy_cb (GtkWidget* widget, gpointer data)
73 {
74 gtk_main_quit ();
75 }
77 /* Home button function */
78 static void
79 go_home_cb (GtkWidget* widget, gpointer data)
80 {
81 const gchar* uri = ("file:///usr/share/webhome/index.html");
82 g_assert (uri);
83 webkit_web_view_load_uri (web_view, uri);
84 }
86 /* My page button function */
87 static void
88 my_page_cb (GtkWidget* widget, gpointer data)
89 {
90 const gchar* uri = g_strdup_printf ("file://%s/.config/tazweb/page.html",
91 g_get_home_dir ());
92 g_assert (uri);
93 webkit_web_view_load_uri (web_view, uri);
94 }
96 /* Navigation button function */
97 static void
98 go_back_cb (GtkWidget* widget, gpointer data)
99 {
100 webkit_web_view_go_back (web_view);
101 }
103 static void
104 go_forward_cb (GtkWidget* widget, gpointer data)
105 {
106 webkit_web_view_go_forward (web_view);
107 }
109 static void
110 refresh_cb (GtkWidget* widget, gpointer data)
111 {
112 webkit_web_view_reload (web_view);
113 }
115 /* Fullscreen and unfullscreen function */
116 static void
117 fullscreen_cb (GtkWindow* window, gpointer data)
118 {
119 GdkWindowState state;
120 state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (main_window)));
122 if (state & GDK_WINDOW_STATE_FULLSCREEN)
123 gtk_window_unfullscreen (GTK_WINDOW (main_window));
124 else
125 gtk_window_fullscreen (GTK_WINDOW (main_window));
126 }
128 /* TazWeb doc function */
129 static void
130 tazweb_doc_cb (GtkWidget* widget, gpointer data)
131 {
132 const gchar* uri = ("file:///usr/share/doc/tazweb/tazweb.html");
133 g_assert (uri);
134 webkit_web_view_load_uri (web_view, uri);
135 }
137 /* Download function */
138 static gboolean
139 download_requested_cb (WebKitWebView *web_view, WebKitDownload *download,
140 gpointer user_data)
141 {
142 const gchar* uri = webkit_download_get_uri (download);
143 gchar *buffer;
144 asprintf (&buffer, "tazbox dl-out %s", uri);
145 system (buffer);
146 }
148 static GtkWidget*
149 create_browser ()
150 {
151 GtkWidget* scrolled_window = gtk_scrolled_window_new (NULL, NULL);
152 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
153 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
155 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
156 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
158 g_signal_connect (web_view, "notify::title",
159 G_CALLBACK (notify_title_cb), web_view);
160 g_signal_connect (web_view, "notify::progress",
161 G_CALLBACK (notify_progress_cb), web_view);
162 g_signal_connect (web_view, "notify::load-status",
163 G_CALLBACK (notify_load_status_cb), web_view);
164 g_signal_connect (web_view, "download-requested",
165 G_CALLBACK (download_requested_cb), NULL);
167 return scrolled_window;
168 }
170 /* Create an icon */
171 static GdkPixbuf*
172 create_pixbuf (const gchar * image)
173 {
174 GdkPixbuf *pixbuf;
175 pixbuf = gdk_pixbuf_new_from_file (image, NULL);
177 return pixbuf;
178 }
180 static GtkWidget*
181 create_toolbar ()
182 {
183 GtkWidget* toolbar = gtk_toolbar_new ();
184 GtkToolItem* item;
186 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
187 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
189 /* The back button */
190 item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
191 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_back_cb), NULL);
192 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
194 /* The forward button */
195 item = gtk_tool_button_new_from_stock (GTK_STOCK_GO_FORWARD);
196 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_forward_cb), NULL);
197 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
199 /* The Reload button */
200 item = gtk_tool_button_new_from_stock (GTK_STOCK_REFRESH);
201 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (refresh_cb), NULL);
202 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
204 /* The URL entry */
205 item = gtk_tool_item_new ();
206 gtk_tool_item_set_expand (item, TRUE);
207 uri_entry = gtk_entry_new ();
208 gtk_container_add (GTK_CONTAINER (item), uri_entry);
209 g_signal_connect (G_OBJECT (uri_entry), "activate",
210 G_CALLBACK (activate_uri_entry_cb), NULL);
211 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
213 /* The Home button */
214 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
215 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (go_home_cb), NULL);
216 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
218 /* The personal page button */
219 item = gtk_tool_button_new_from_stock (GTK_STOCK_PREFERENCES);
220 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (my_page_cb), NULL);
221 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
223 /* The TazWeb doc button */
224 item = gtk_tool_button_new_from_stock (GTK_STOCK_INFO);
225 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (tazweb_doc_cb), NULL);
226 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
228 /* The Fullscreen button */
229 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
230 g_signal_connect (G_OBJECT (item), "clicked", G_CALLBACK (fullscreen_cb), NULL);
231 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
233 return toolbar;
234 }
236 static GtkWidget*
237 create_window ()
238 {
239 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
241 /* Default tazweb window size ratio to 3/4 --> 720, 540*/
242 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
243 gtk_window_set_icon (GTK_WINDOW (window),
244 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
245 gtk_widget_set_name (window, "TazWeb");
246 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
248 return window;
249 }
251 int
252 main (int argc, char* argv[])
253 {
254 gtk_init (&argc, &argv);
255 if (!g_thread_supported ())
256 g_thread_init (NULL);
258 /* Get a default page.html if missing */
259 const gchar* config = g_strdup_printf ("%s/.config/tazweb", g_get_home_dir ());
260 if (!g_file_test(config, G_FILE_TEST_EXISTS)) {
261 g_mkdir (config, 0700);
262 system ("cp /usr/share/tazweb/* $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 }