tazweb view src/main.c @ rev 147

Makefile fix /var/www/cgi-bin
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Apr 06 19:03:13 2014 +0000 (2014-04-06)
parents 967ea05136b7
children 68248b2673dd
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. Commented line code
5 * starts with // and comments are between * *
6 *
7 * Copyright (C) 2011-2014 SliTaz GNU/Linux - BSD License
8 * See AUTHORS and LICENSE for detailed information
9 *
10 */
12 #include <glib.h>
13 #include <glib/gi18n.h>
14 #define GETTEXT_PACKAGE "tazweb"
16 #include <gtk/gtk.h>
17 #include <webkit/webkit.h>
19 #define HOME g_get_home_dir()
20 #define CONFIG g_strdup_printf("%s/.config/tazweb", HOME)
21 #define BMTXT g_strdup_printf("%s/bookmarks.txt", CONFIG)
22 #define BMURL "http://localhost/cgi-bin/bookmarks.cgi"
23 #define WEBHOME "file:///usr/share/webhome/index.html"
24 #define SEARCH "http://duckduckgo.com/?q=%s&t=slitaz"
26 /* Needs AppleWebKit/531.2+ to handle all sites ? */
27 static gchar *useragent = "TazWeb (X11; SliTaz GNU/Linux; U; en_US)";
28 static GtkWidget* create_window(WebKitWebView** newwebview);
29 static GtkWidget *mainwindow, *vbox, *browser, *toolbar;
30 static WebKitWebView *webview;
31 static WebKitWebFrame *frame;
32 static gint count = 0;
33 static gboolean notoolbar;
34 static gboolean noquit;
35 static gboolean kiosk;
36 const gchar* uri;
38 /* Create an icon */
39 static GdkPixbuf*
40 create_pixbuf(const gchar* image)
41 {
42 GdkPixbuf *pixbuf;
43 pixbuf = gdk_pixbuf_new_from_file(image, NULL);
44 return pixbuf;
45 }
47 /* Can be: http://hg.slitaz.org or hg.slitaz.org */
48 static void
49 check_requested_uri()
50 {
51 uri = g_strrstr(uri, "://") ? g_strdup(uri)
52 : g_strdup_printf("http://%s", uri);
53 }
55 /* Update title */
56 static void
57 update_title(GtkWindow* window, WebKitWebView* webview)
58 {
59 GString *string;
60 gdouble progress;
61 gchar *title;
63 string = g_string_new(webkit_web_view_get_title(webview));
64 progress = webkit_web_view_get_progress(webview) * 100;
65 if (progress < 100)
66 g_string_append_printf(string, " [ %f%% ] ", progress);
68 title = g_string_free(string, FALSE);
69 gtk_window_set_title(window, title);
70 g_free(title);
71 }
73 /* Get the page title */
74 static void
75 notify_title_cb(WebKitWebView* webview, GParamSpec* pspec, GtkWidget* window)
76 {
77 update_title(GTK_WINDOW(window), webview);
78 }
80 /* Request progress in window title */
81 static void
82 notify_progress_cb(WebKitWebView* webview, GParamSpec* pspec, GtkWidget* window)
83 {
84 update_title(GTK_WINDOW(window), webview);
85 }
87 /* Notify url entry */
88 static void
89 notify_load_status_cb(WebKitWebView* webview, GParamSpec* pspec, GtkWidget* urientry)
90 {
91 if (webkit_web_view_get_load_status(webview) == WEBKIT_LOAD_COMMITTED) {
92 frame = webkit_web_view_get_main_frame(webview);
93 uri = webkit_web_frame_get_uri(frame);
94 if (uri)
95 gtk_entry_set_text(GTK_ENTRY(urientry), uri);
96 }
97 }
99 /* Destroy the window */
100 static void
101 destroy_cb(GtkWidget* widget, GtkWindow* window)
102 {
103 if (g_atomic_int_dec_and_test(&count))
104 gtk_main_quit();
105 }
107 /* Show page source */
108 static void
109 view_source_cb(GtkWidget* widget, WebKitWebView* webview)
110 {
111 gboolean source;
113 frame = webkit_web_view_get_main_frame(webview);
114 uri = webkit_web_frame_get_uri(frame);
115 source = webkit_web_view_get_view_source_mode(webview);
117 webkit_web_view_set_view_source_mode(webview, !source);
118 webkit_web_view_reload(webview);
119 }
121 /* URL entry callback function */
122 static void
123 uri_entry_cb(GtkWidget* urientry, WebKitWebView* webview)
124 {
125 uri = gtk_entry_get_text(GTK_ENTRY(urientry));
126 g_assert(uri);
127 check_requested_uri();
128 webkit_web_view_load_uri(webview, uri);
129 }
131 /* Search entry and icon callback function */
132 static void
133 search_web(GtkWidget* search, WebKitWebView* webview)
134 {
135 uri = g_strdup_printf(SEARCH, gtk_entry_get_text(GTK_ENTRY(search)));
136 g_assert(uri);
137 webkit_web_view_load_uri(webview, uri);
138 }
140 static void
141 search_entry_cb(GtkWidget* search, WebKitWebView* webview)
142 {
143 search_web(search, webview);
144 }
146 static void
147 search_icon_press_cb(GtkWidget *search, GtkEntryIconPosition pos,
148 GdkEvent *event, WebKitWebView* webview)
149 {
150 search_web(search, webview);
151 }
153 /* Navigation button function: */
154 static void
155 go_bookmarks_cb(GtkWidget* widget, WebKitWebView* webview)
156 {
157 uri = g_strdup_printf("%s?home=%s", BMURL, HOME);
158 g_assert(uri);
159 webkit_web_view_load_uri(webview, uri);
160 }
162 static void
163 go_home_cb(GtkWidget* widget, WebKitWebView* webview)
164 {
165 uri = WEBHOME;
166 g_assert(uri);
167 webkit_web_view_load_uri(webview, uri);
168 }
170 static void
171 go_back_cb(GtkWidget* widget, WebKitWebView* webview)
172 {
173 webkit_web_view_go_back(webview);
174 }
176 static void
177 go_forward_cb(GtkWidget* widget, WebKitWebView* webview)
178 {
179 webkit_web_view_go_forward(webview);
180 }
182 /* Fullscreen and unfullscreen callback function */
183 //static void
184 //fullscreen_cb(GtkWindow* window, gpointer data)
185 //{
186 //GdkWindowState state;
187 //state = gdk_window_get_state(gtk_widget_get_window(GTK_WIDGET(mainwindow)));
189 //if(state & GDK_WINDOW_STATE_FULLSCREEN)
190 //gtk_window_unfullscreen(GTK_WINDOW(mainwindow));
191 //else
192 //gtk_window_fullscreen(GTK_WINDOW(mainwindow));
193 //}
195 /* TazWeb doc callback function */
196 static void
197 tazweb_doc_cb(GtkWidget* widget, WebKitWebView *webview)
198 {
199 uri = ("file:///usr/share/doc/tazweb/tazweb.html");
200 g_assert(uri);
201 webkit_web_view_load_uri(webview, uri);
202 }
204 /* Download function */
205 static gboolean
206 download_requested_cb(WebKitWebView *webview, WebKitDownload *download,
207 gpointer user_data)
208 {
209 const gchar* buffer;
210 uri = webkit_download_get_uri(download);
211 asprintf(&buffer, "xterm -T 'Download' -geom 72x10+0-24 -e \
212 'mkdir -p $HOME/Downloads && cd $HOME/Downloads && \
213 wget -c %s; sleep 2' &", uri);
214 system(buffer);
215 }
217 /* Zoom out and in callback function */
218 static void
219 zoom_out_cb(GtkWidget *widget, WebKitWebView* webview)
220 {
221 webkit_web_view_zoom_out(webview);
222 }
224 static void
225 zoom_in_cb(GtkWidget *widget, WebKitWebView* webview)
226 {
227 webkit_web_view_zoom_in(webview);
228 }
230 /* Printing callback function */
231 static void
232 print_page_cb (GtkWidget *widget, gpointer data)
233 {
234 webkit_web_frame_print (webkit_web_view_get_main_frame (
235 WEBKIT_WEB_VIEW (webview)));
236 }
238 /* New webview callbacks */
239 static WebKitWebView*
240 create_web_view_cb(WebKitWebView* webview, WebKitWebFrame* web_frame,
241 GtkWidget* window)
242 {
243 WebKitWebView *newview;
244 create_window(&newview);
245 return newview;
246 }
248 static gboolean
249 webview_ready_cb(WebKitWebView* webview, GtkWidget* window)
250 {
251 gtk_widget_grab_focus(GTK_WIDGET(webview));
252 gtk_widget_show_all(window);
253 return FALSE;
254 }
256 static gboolean
257 close_webview_cb(WebKitWebView* webview, GtkWidget* window)
258 {
259 gtk_widget_destroy(window);
260 return TRUE;
261 }
263 /* Add a bookmark to home.html */
264 void add_bookmark_cb(GtkWidget *widget, gpointer data)
265 {
266 const gchar* title;
267 const gchar* buffer;
269 title = webkit_web_view_get_title(WEBKIT_WEB_VIEW (webview));
270 uri = webkit_web_view_get_uri(WEBKIT_WEB_VIEW (webview));
272 asprintf(&buffer, "echo '%s|%s' >> %s", title, uri, BMTXT);
273 system(buffer);
274 }
276 /* Add items to WebKit contextual menu */
277 static void
278 populate_menu_cb(WebKitWebView *webview, GtkMenu *menu, gpointer data)
279 {
280 GtkWidget* item;
282 /* Separator */
283 item = gtk_separator_menu_item_new();
284 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
286 /* Zoom in */
287 item = gtk_image_menu_item_new_with_label(_("Zoom in"));
288 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
289 gtk_image_new_from_stock(GTK_STOCK_ZOOM_IN, GTK_ICON_SIZE_MENU));
290 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
291 g_signal_connect(item, "activate", G_CALLBACK(zoom_in_cb), webview);
293 /* Zoom out */
294 item = gtk_image_menu_item_new_with_label(_("Zoom out"));
295 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
296 gtk_image_new_from_stock(GTK_STOCK_ZOOM_OUT, GTK_ICON_SIZE_MENU));
297 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
298 g_signal_connect(item, "activate", G_CALLBACK(zoom_out_cb), webview);
300 /* Separator */
301 item = gtk_separator_menu_item_new();
302 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
304 if (! kiosk) {
305 /* Add a bookmark */
306 item = gtk_image_menu_item_new_with_label(_("Add a bookmark"));
307 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
308 gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
309 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
310 g_signal_connect(item, "activate", G_CALLBACK(add_bookmark_cb), webview);
311 }
313 /* Printing */
314 item = gtk_image_menu_item_new_with_label(_("Print this page"));
315 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
316 gtk_image_new_from_stock(GTK_STOCK_PRINT, GTK_ICON_SIZE_MENU));
317 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
318 g_signal_connect(item, "activate", G_CALLBACK(print_page_cb), webview);
320 /* View source mode */
321 item = gtk_image_menu_item_new_with_label(_("View source mode"));
322 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
323 gtk_image_new_from_stock(GTK_STOCK_PROPERTIES, GTK_ICON_SIZE_MENU));
324 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
325 g_signal_connect(item, "activate", G_CALLBACK(view_source_cb), webview);
327 /* Separator */
328 item = gtk_separator_menu_item_new();
329 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
331 /* TazWeb documentation */
332 item = gtk_image_menu_item_new_with_label(_("TazWeb manual"));
333 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
334 gtk_image_new_from_stock(GTK_STOCK_HELP, GTK_ICON_SIZE_MENU));
335 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
336 g_signal_connect(item, "activate", G_CALLBACK(tazweb_doc_cb), webview);
338 /* Quit TazWeb */
339 item = gtk_image_menu_item_new_with_label(_("Quit TazWeb"));
340 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
341 gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
342 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
343 g_signal_connect(item, "activate", G_CALLBACK(destroy_cb), webview);
345 gtk_widget_show_all(GTK_WIDGET(menu));
346 }
348 /* Scrolled window for the webview */
349 static GtkWidget*
350 create_browser(GtkWidget* window, GtkWidget* urientry, GtkWidget* search,
351 WebKitWebView* webview)
352 {
353 WebKitWebSettings *settings;
355 browser = gtk_scrolled_window_new(NULL, NULL);
356 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(browser),
357 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
359 gtk_container_add(GTK_CONTAINER(browser), GTK_WIDGET(webview));
361 /* User agent */
362 settings = webkit_web_view_get_settings (webview);
363 g_object_set(G_OBJECT(settings), "user-agent", useragent, NULL);
365 /* Connect WebKit events */
366 g_signal_connect(webview, "notify::title",
367 G_CALLBACK(notify_title_cb), window);
368 g_signal_connect(webview, "notify::progress",
369 G_CALLBACK(notify_progress_cb), window);
370 g_signal_connect(webview, "notify::load-status",
371 G_CALLBACK(notify_load_status_cb), urientry);
372 g_signal_connect(webview, "web-view-ready",
373 G_CALLBACK(webview_ready_cb), window);
374 g_signal_connect(webview, "close-web-view",
375 G_CALLBACK(close_webview_cb), window);
377 /* Impossible to open in new window or download in kiosk mode */
378 if (! kiosk) {
379 g_signal_connect(webview, "download-requested",
380 G_CALLBACK(download_requested_cb), NULL);
381 g_signal_connect(webview, "create-web-view",
382 G_CALLBACK(create_web_view_cb), window);
383 }
385 /* Connect WebKit contextual menu items */
386 g_object_connect(G_OBJECT(webview), "signal::populate-popup",
387 G_CALLBACK(populate_menu_cb), webview, NULL);
389 return browser;
390 }
392 static GtkWidget*
393 create_toolbar(GtkWidget* urientry, GtkWidget* search, WebKitWebView* webview)
394 {
395 GtkToolItem* item;
397 toolbar = gtk_toolbar_new();
398 gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar),
399 GTK_ORIENTATION_HORIZONTAL);
400 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),
401 GTK_TOOLBAR_BOTH_HORIZ);
403 /* The back button */
404 item = gtk_tool_button_new_from_stock(GTK_STOCK_GO_BACK);
405 g_signal_connect(G_OBJECT(item), "clicked",
406 G_CALLBACK(go_back_cb), webview);
407 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
409 /* The forward button */
410 item = gtk_tool_button_new_from_stock(GTK_STOCK_GO_FORWARD);
411 g_signal_connect(G_OBJECT(item), "clicked",
412 G_CALLBACK(go_forward_cb), webview);
413 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
415 /* Home button */
416 item = gtk_tool_button_new_from_stock(GTK_STOCK_HOME);
417 g_signal_connect(G_OBJECT(item), "clicked",
418 G_CALLBACK(go_home_cb), webview);
419 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
421 /* URL entry */
422 item = gtk_tool_item_new();
423 gtk_tool_item_set_expand(item, TRUE);
424 gtk_widget_set_size_request(urientry, 0, 20);
425 gtk_container_add(GTK_CONTAINER(item), urientry);
426 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
427 g_signal_connect(G_OBJECT(urientry), "activate",
428 G_CALLBACK(uri_entry_cb), webview);
430 /* Separator */
431 item = gtk_separator_tool_item_new();
432 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
434 /* Search entry */
435 item = gtk_tool_item_new();
436 gtk_widget_set_size_request(search, 160, 20);
437 gtk_container_add(GTK_CONTAINER(item), search);
438 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
439 gtk_entry_set_icon_from_stock(GTK_ENTRY(search),
440 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_FIND);
441 g_signal_connect(GTK_ENTRY(search), "icon-press",
442 G_CALLBACK(search_icon_press_cb), webview);
443 g_signal_connect(G_OBJECT(search), "activate",
444 G_CALLBACK(search_entry_cb), webview);
446 /* Bookmark button */
447 item = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
448 g_signal_connect(G_OBJECT(item), "clicked",
449 G_CALLBACK(go_bookmarks_cb), webview);
450 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1);
452 return toolbar;
453 }
455 /* Main window */
456 static GtkWidget*
457 create_window(WebKitWebView** newwebview)
458 {
459 GtkWidget *window;
460 GtkWidget *urientry;
461 GtkWidget *search;
463 g_atomic_int_inc(&count);
465 /* Default TazWeb window */
466 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
467 gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
468 gtk_window_set_icon(GTK_WINDOW(window),
469 create_pixbuf("/usr/share/pixmaps/tazweb.png"));
470 gtk_widget_set_name(window, "TazWeb");
471 g_signal_connect(window, "destroy", G_CALLBACK(destroy_cb), NULL);
473 /* Webview and widgets */
474 webview = WEBKIT_WEB_VIEW(webkit_web_view_new());
475 urientry = gtk_entry_new();
476 search = gtk_entry_new();
477 vbox = gtk_vbox_new(FALSE, 0);
479 /* Pack box and container */
480 if (! notoolbar)
481 gtk_box_pack_start(GTK_BOX(vbox),
482 create_toolbar(urientry, search, webview), FALSE, FALSE, 0);
483 gtk_box_pack_start(GTK_BOX(vbox),
484 create_browser(window, urientry, search, webview), TRUE, TRUE, 0);
486 gtk_container_add(GTK_CONTAINER(window), vbox);
488 if (newwebview)
489 *newwebview = webview;
491 return window;
492 }
494 int
495 main(int argc, char* argv[])
496 {
497 textdomain (GETTEXT_PACKAGE);
499 while (argc > 1) {
500 if (!strcmp(argv[1],"--notoolbar")) {
501 notoolbar++;
502 }
503 else if (!strcmp(argv[1],"--kiosk")) {
504 kiosk++;
505 }
506 else if (!strcmp(argv[1],"--useragent") && argc > 2) {
507 argc--;
508 argv++;
509 useragent = argv[1];
510 }
511 else if (!strcmp(argv[1],"--help")) {
512 printf ("Usage: tazweb [--notoolbar|--kiosk|--useragent] [ua]\n");
513 printf ("Bookmarks: %s\n", BMTXT);
514 return 0;
515 }
516 else break;
517 argc--;
518 argv++;
519 }
521 gtk_init(NULL, NULL);
522 if (!g_thread_supported())
523 g_thread_init(NULL);
525 /* Get a default home.html if missing */
526 if (! g_file_test(CONFIG, G_FILE_TEST_EXISTS)) {
527 system("install -m 0777 $HOME/.config/tazweb");
528 system("install -m 0666 /usr/share/tazweb/bookmarks.txt \
529 $HOME/.config/tazweb");
530 }
532 /* Load the start page file or the url in argument */
533 uri =(char*)(argc > 1 ? argv[1] : WEBHOME);
534 if (argv[1])
535 check_requested_uri();
537 mainwindow = create_window(&webview);
538 gtk_widget_show_all(mainwindow);
540 /* Fullscreen for Kiosk mode */
541 if (kiosk)
542 gtk_window_fullscreen(GTK_WINDOW(mainwindow));
544 webkit_web_view_load_uri(webview, uri);
545 gtk_widget_grab_focus(GTK_WIDGET(webview));
546 gtk_main();
548 return 0;
549 }