tazweb view src/main.c @ rev 61

Use #define to handle config dir
author Christophe Lincoln <pankso@slitaz.org>
date Fri Apr 22 05:04:17 2011 +0200 (2011-04-22)
parents 246560e6885c
children 8389defee9b9
line source
1 /*
2 * TazWeb is a radically simple web browser providing a single window
3 * with a single toolbar with buttons, an URL entry and search as well
4 * as a contextual menu, but no menu bar or tabs.
5 *
6 * Copyright (C) 2011 SliTaz GNU/Linux <devel@slitaz.org>
7 * See AUTHORS and LICENSE for detailed information
8 *
9 */
11 #include <gtk/gtk.h>
12 #include <webkit/webkit.h>
14 #define CONFIG g_strdup_printf ("%s/.config/tazweb", g_get_home_dir ())
16 static GtkWidget *main_window, *scrolled, *toolbar;
17 static GtkWidget *uri_entry, *search_entry;
18 static WebKitWebView* web_view;
19 static WebKitWebFrame* frame;
20 static gdouble load_progress;
21 static guint status_context_id;
22 static gchar* main_title;
23 const gchar* uri;
25 /* Create an icon */
26 static GdkPixbuf*
27 create_pixbuf (const gchar * image)
28 {
29 GdkPixbuf *pixbuf;
30 pixbuf = gdk_pixbuf_new_from_file (image, NULL);
31 return pixbuf;
32 }
34 /* Get a default home.html if missing */
35 static void
36 get_config ()
37 {
38 if (! g_file_test (CONFIG, G_FILE_TEST_EXISTS)) {
39 system ("cp -r /usr/share/tazweb $HOME/.config/tazweb");
40 }
41 }
43 /* Page title to window title */
44 static void
45 update_title (GtkWindow* window)
46 {
47 GString* string = g_string_new (main_title);
48 /* g_string_append (string, " - TazWeb"); */
49 if (load_progress < 100)
50 g_string_append_printf (string, " [ %f%% ] ", load_progress);
51 gchar* title = g_string_free (string, FALSE);
52 gtk_window_set_title (window, title);
53 g_free (title);
54 }
56 static void
57 notify_title_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
58 {
59 if (main_title)
60 g_free (main_title);
61 main_title = g_strdup (webkit_web_view_get_title(web_view));
62 update_title (GTK_WINDOW (main_window));
63 }
65 /* Request progress in window title */
66 static void
67 notify_progress_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
68 {
69 load_progress = webkit_web_view_get_progress (web_view) * 100;
70 update_title (GTK_WINDOW (main_window));
71 }
73 static void
74 notify_load_status_cb (WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
75 {
76 if (webkit_web_view_get_load_status (web_view) == WEBKIT_LOAD_COMMITTED) {
77 frame = webkit_web_view_get_main_frame (web_view);
78 uri = webkit_web_frame_get_uri (frame);
79 if (uri)
80 gtk_entry_set_text (GTK_ENTRY (uri_entry), uri);
81 }
82 }
84 static void
85 destroy_cb (GtkWidget* widget, gpointer data)
86 {
87 gtk_main_quit ();
88 }
90 /* Show page source */
91 static void
92 view_source_cb ()
93 {
94 gboolean source;
96 frame = webkit_web_view_get_main_frame (web_view);
97 uri = webkit_web_frame_get_uri (frame);
98 source = webkit_web_view_get_view_source_mode (web_view);
100 webkit_web_view_set_view_source_mode(web_view, !source);
101 webkit_web_view_load_uri (web_view, uri);
102 }
104 /* URL entry callback function */
105 static void
106 uri_entry_cb (GtkWidget* entry, gpointer data)
107 {
108 uri = gtk_entry_get_text (GTK_ENTRY (entry));
109 g_assert (uri);
110 webkit_web_view_load_uri (web_view, uri);
111 }
113 /* Search entry callback function */
114 static void
115 search_entry_cb (GtkWidget* entry, gpointer data)
116 {
117 uri = g_strdup_printf ("http://www.google.com/search?q=%s",
118 gtk_entry_get_text (GTK_ENTRY (entry)));
119 g_assert (uri);
120 webkit_web_view_load_uri (web_view, uri);
121 }
123 /* Home button callback function */
124 static void
125 go_home_cb (GtkWidget* widget, gpointer data)
126 {
127 uri = g_strdup_printf ("file://%s/home.html", CONFIG);
128 g_assert (uri);
129 webkit_web_view_load_uri (web_view, uri);
130 }
132 /* Fullscreen and unfullscreen callback function */
133 static void
134 fullscreen_cb (GtkWindow* window, gpointer data)
135 {
136 GdkWindowState state;
137 state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (main_window)));
139 if (state & GDK_WINDOW_STATE_FULLSCREEN)
140 gtk_window_unfullscreen (GTK_WINDOW (main_window));
141 else
142 gtk_window_fullscreen (GTK_WINDOW (main_window));
143 }
145 /* TazWeb doc callback function */
146 static void
147 tazweb_doc_cb (GtkWidget* widget, gpointer data)
148 {
149 uri = ("file:///usr/share/doc/tazweb/tazweb.html");
150 g_assert (uri);
151 webkit_web_view_load_uri (web_view, uri);
152 }
154 /* Download function */
155 static gboolean
156 download_requested_cb (WebKitWebView *web_view, WebKitDownload *download,
157 gpointer user_data)
158 {
159 uri = webkit_download_get_uri (download);
160 gchar *buffer;
161 asprintf (&buffer, "tazbox dl-out %s", uri);
162 system (buffer);
163 }
165 /* Zoom ou and in callback function */
166 static void
167 zoom_out_cb (GtkWidget *main_window)
168 {
169 webkit_web_view_zoom_out (web_view);
170 }
172 static void
173 zoom_in_cb (GtkWidget *main_window)
174 {
175 webkit_web_view_zoom_in (web_view);
176 }
178 /* Add items to WebKit contextual menu */
179 static void
180 populate_menu_cb (WebKitWebView *web_view, GtkMenu *menu, gpointer data)
181 {
182 GtkWidget *item;
184 /* separator */
185 item = gtk_separator_menu_item_new ();
186 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
188 /* Zoom in */
189 item = gtk_image_menu_item_new_with_label ("Zoom in");
190 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
191 gtk_image_new_from_stock (GTK_STOCK_ZOOM_IN, GTK_ICON_SIZE_MENU));
192 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
193 g_signal_connect (item, "activate", G_CALLBACK (zoom_in_cb), NULL);
195 /* Zoom out */
196 item = gtk_image_menu_item_new_with_label ("Zoom out");
197 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
198 gtk_image_new_from_stock (GTK_STOCK_ZOOM_OUT, GTK_ICON_SIZE_MENU));
199 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
200 g_signal_connect (item, "activate", G_CALLBACK (zoom_out_cb), NULL);
202 /* separator */
203 item = gtk_separator_menu_item_new ();
204 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
206 /* TazWeb documentation */
207 item = gtk_image_menu_item_new_with_label ("TazWeb manual");
208 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
209 gtk_image_new_from_stock (GTK_STOCK_INFO, GTK_ICON_SIZE_MENU));
210 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
211 g_signal_connect (item, "activate", G_CALLBACK (tazweb_doc_cb), NULL);
213 /* View source mode */
214 item = gtk_image_menu_item_new_with_label ("View source mode");
215 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
216 gtk_image_new_from_stock (GTK_STOCK_PROPERTIES, GTK_ICON_SIZE_MENU));
217 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
218 g_signal_connect (item, "activate", G_CALLBACK (view_source_cb), NULL);
220 gtk_widget_show_all (GTK_WIDGET (menu));
221 }
223 /* Scrolled window for the web_view */
224 static GtkWidget*
225 create_browser ()
226 {
227 scrolled = gtk_scrolled_window_new (NULL, NULL);
228 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
229 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
231 web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
232 gtk_container_add (GTK_CONTAINER (scrolled), GTK_WIDGET (web_view));
234 /* Connect events */
235 g_signal_connect (web_view, "notify::title",
236 G_CALLBACK (notify_title_cb), web_view);
237 g_signal_connect (web_view, "notify::progress",
238 G_CALLBACK (notify_progress_cb), web_view);
239 g_signal_connect (web_view, "notify::load-status",
240 G_CALLBACK (notify_load_status_cb), web_view);
241 g_signal_connect (web_view, "download-requested",
242 G_CALLBACK (download_requested_cb), NULL);
244 /* Connect WebKit contextual menu items */
245 g_object_connect (G_OBJECT (web_view), "signal::populate-popup",
246 G_CALLBACK (populate_menu_cb), web_view, NULL);
248 return scrolled;
249 }
251 static GtkWidget*
252 create_toolbar ()
253 {
254 GtkToolItem* item;
256 toolbar = gtk_toolbar_new ();
257 gtk_widget_set_size_request (toolbar, 0, 31);
258 gtk_toolbar_set_orientation (GTK_TOOLBAR (toolbar),
259 GTK_ORIENTATION_HORIZONTAL);
260 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar),
261 GTK_TOOLBAR_BOTH_HORIZ);
263 /* Home button */
264 item = gtk_tool_button_new_from_stock (GTK_STOCK_HOME);
265 g_signal_connect (G_OBJECT (item), "clicked",
266 G_CALLBACK (go_home_cb), NULL);
267 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
269 /* URL entry */
270 item = gtk_tool_item_new ();
271 gtk_tool_item_set_expand (item, TRUE);
272 uri_entry = gtk_entry_new ();
273 gtk_container_add (GTK_CONTAINER (item), uri_entry);
274 g_signal_connect (G_OBJECT (uri_entry), "activate",
275 G_CALLBACK (uri_entry_cb), NULL);
276 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
278 /* Separator */
279 item = gtk_separator_tool_item_new ();
280 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
282 /* Search entry */
283 item = gtk_tool_item_new ();
284 search_entry = gtk_entry_new ();
285 gtk_container_add (GTK_CONTAINER (item), search_entry);
286 g_signal_connect (G_OBJECT (search_entry), "activate",
287 G_CALLBACK (search_entry_cb), NULL);
288 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
290 /* The Fullscreen button */
291 item = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
292 g_signal_connect (G_OBJECT (item), "clicked",
293 G_CALLBACK (fullscreen_cb), NULL);
294 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
296 return toolbar;
297 }
299 /* Main window */
300 static GtkWidget*
301 create_window ()
302 {
303 GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
304 GtkWidget* vbox = gtk_vbox_new (FALSE, 0);
306 gtk_box_pack_start (GTK_BOX (vbox), create_browser (), TRUE, TRUE, 0);
307 gtk_box_pack_start (GTK_BOX (vbox), create_toolbar (), FALSE, FALSE, 0);
309 /* Default TazWeb window size ratio to 3/4 --> 720, 540*/
310 gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
311 gtk_window_set_icon (GTK_WINDOW (window),
312 create_pixbuf ("/usr/share/pixmaps/tazweb.png"));
313 gtk_widget_set_name (window, "TazWeb");
314 g_signal_connect (window, "destroy", G_CALLBACK (destroy_cb), NULL);
315 gtk_container_add (GTK_CONTAINER (window), vbox);
317 main_window = window;
318 gtk_widget_show_all (main_window);
319 }
321 int
322 main (int argc, char* argv[])
323 {
324 gtk_init (&argc, &argv);
325 if (!g_thread_supported ())
326 g_thread_init (NULL);
328 get_config ();
329 create_window ();
331 /* Start page url or file */
332 uri = (gchar*) (argc > 1 ? argv[1] :
333 "file:///usr/share/webhome/index.html");
335 webkit_web_view_load_uri (web_view, uri);
336 gtk_widget_grab_focus (GTK_WIDGET (web_view));
338 gtk_main ();
339 return 0;
340 }