forked from creationix/topcube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopcube.cpp
138 lines (119 loc) · 4.77 KB
/
topcube.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <webkit/webkitwebview.h>
// stl for n00b debugging :)
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// std::clog << "debug output";
GtkWidget *window;
GtkWidget *scrolled_window;
GtkWidget *web_view;
WebKitWebSettings *settings;
void destroy (void)
{
gtk_main_quit ();
}
void title_change (void)
{
gtk_window_set_title(GTK_WINDOW (window), webkit_web_view_get_title(WEBKIT_WEB_VIEW (web_view)));
}
void new_window (
WebKitWebView *web_view,
WebKitWebFrame *frame,
WebKitNetworkRequest *request)
{
gchar *argv[3];
argv[0] = const_cast<char*>("/usr/bin/xdg-open");
argv[1] = const_cast<char*>(webkit_network_request_get_uri(request));
argv[2] = NULL;
g_spawn_async(NULL, argv, NULL, G_SPAWN_LEAVE_DESCRIPTORS_OPEN, NULL, NULL, NULL, NULL);
}
static gboolean download (
WebKitWebView *web_view,
WebKitWebFrame *frame,
WebKitNetworkRequest *request,
const char *mime_type,
WebKitWebPolicyDecision *decision,
gpointer user_data)
{
// Any other mime types we should handle?
if (strcmp(mime_type, "text/html") != 0) {
WebKitDownload *download = webkit_download_new(request);
GtkWidget *window = gtk_widget_get_toplevel (GTK_WIDGET(web_view));
GtkWidget *dialog = gtk_file_chooser_dialog_new ("Save file",
GTK_WINDOW(window),
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
NULL);
gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), webkit_download_get_suggested_filename(download));
// @TODO getting a warning 'Unable to retrieve the file info' for the
// file as it does not exist yet... track down why?
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
gchar *destination = g_filename_to_uri(gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)), NULL, NULL);
webkit_download_set_destination_uri (download, destination);
webkit_download_start(download);
g_free(destination);
} else {
webkit_download_cancel(download);
g_object_unref(download);
}
gtk_widget_destroy (dialog);
webkit_web_policy_decision_ignore(decision);
}
return TRUE;
}
int main(int argc, char* argv[])
{
static gchar *url = const_cast<char*>("http://google.com");
static gchar *name = const_cast<char*>("TopCube");
static gint width = 800;
static gint height = 600;
static gint minwidth = 600;
static gint minheight = 400;
static GOptionEntry entries[] = {
{ "url", 'u', 0, G_OPTION_ARG_STRING, &url, "URL" },
{ "name", 'n', 0, G_OPTION_ARG_STRING, &name, "Window name" },
{ "width", 'W', 0, G_OPTION_ARG_INT, &width, "Width" },
{ "height", 'H', 0, G_OPTION_ARG_INT, &height, "Height" },
{ "minwidth", 'w', 0, G_OPTION_ARG_INT, &minwidth, "Minimum width" },
{ "minheight", 'h', 0, G_OPTION_ARG_INT, &minheight, "Minimum height" }
};
GError *error = NULL;
GOptionContext *options;
options = g_option_context_new("topcube options");
g_option_context_add_main_entries (options, entries, NULL);
g_option_context_add_group (options, gtk_get_option_group(TRUE));
if (!g_option_context_parse (options, &argc, &argv, &error)) {
g_print ("Error parsing options: %s\n", error->message);
exit (1);
}
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
web_view = webkit_web_view_new ();
// Modify default settings - disable right click menus.
settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW(web_view));
g_object_set(settings, "enable-default-context-menu", FALSE, NULL);
gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (destroy), NULL);
gtk_signal_connect (GTK_OBJECT (web_view), "title-changed", GTK_SIGNAL_FUNC (title_change), NULL);
gtk_signal_connect (GTK_OBJECT (web_view), "mime-type-policy-decision-requested", GTK_SIGNAL_FUNC (download), NULL);
gtk_signal_connect (GTK_OBJECT (web_view), "new-window-policy-decision-requested", GTK_SIGNAL_FUNC (new_window), NULL);
gtk_container_add (GTK_CONTAINER (scrolled_window), web_view);
gtk_container_add (GTK_CONTAINER (window), scrolled_window);
webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), url);
gtk_window_set_wmclass (GTK_WINDOW (window), name, name);
gtk_window_set_default_size (GTK_WINDOW (window), width, height);
GdkGeometry hints;
hints.min_width = minwidth;
hints.min_height = minheight;
gtk_window_set_geometry_hints(GTK_WINDOW (window), GTK_WIDGET (scrolled_window), &hints, GDK_HINT_MIN_SIZE);
gtk_widget_grab_focus (GTK_WIDGET(web_view));
gtk_widget_show_all (window);
gtk_main ();
return 0;
}