-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebkit-03.c
143 lines (117 loc) · 4.96 KB
/
webkit-03.c
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
138
139
140
141
142
143
#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <libnotify/notify.h>
#include <JavaScriptCore/JavaScript.h>
/* Class initialize */
static void notification_init_cb(JSContextRef ctx,
JSObjectRef object)
{
/* Inits notify */
notify_init("Notification");
}
/* Notification.notify method callback implementation */
static JSValueRef notification_notify_cb(JSContextRef context,
JSObjectRef function,
JSObjectRef thisObject,
size_t argumentCount,
const JSValueRef arguments[],
JSValueRef *exception)
{
/* At least, one argument must be received */
if (argumentCount == 1 && JSValueIsString(context, arguments[0])) {
/* Converts JSValue to char */
size_t len;
char *cstr;
JSStringRef jsstr = JSValueToStringCopy(context, arguments[0], NULL);
len = JSStringGetMaximumUTF8CStringSize(jsstr);
cstr = g_new(char, len);
JSStringGetUTF8CString(jsstr, cstr, len);
/* Creates a new NotifyNotification. */
NotifyNotification *notification = notify_notification_new(cstr, NULL, NULL);
/* Sets the timeout of the notification. */
notify_notification_set_timeout(notification, 3000);
/* Sets the urgency level of this notification. */
notify_notification_set_urgency(notification, NOTIFY_URGENCY_NORMAL);
/* Tells the notification server to display the notification on the screen. */
GError *error = NULL;
notify_notification_show(notification, &error);
g_object_unref(G_OBJECT(notification));
g_free(cstr);
JSStringRelease(jsstr);
}
return JSValueMakeUndefined(context);
}
/* Class method declarations */
static const JSStaticFunction notification_staticfuncs[] =
{
{ "notify", notification_notify_cb, kJSPropertyAttributeReadOnly },
{ NULL, NULL, 0 }
};
static const JSClassDefinition notification_def =
{
0, // version
kJSClassAttributeNone, // attributes
"Notification", // className
NULL, // parentClass
NULL, // staticValues
notification_staticfuncs, // staticFunctions
notification_init_cb, // initialize
NULL, // finalize
NULL, // hasProperty
NULL, // getProperty
NULL, // setProperty
NULL, // deleteProperty
NULL, // getPropertyNames
NULL, // callAsFunction
NULL, // callAsConstructor
NULL, // hasInstance
NULL // convertToType
};
/* Callback - JavaScript window object has been cleared */
static void window_object_cleared_cb(WebKitWebView *web_view,
WebKitWebFrame *frame,
gpointer context,
gpointer window_object,
gpointer user_data)
{
/* Add classes to JavaScriptCore */
JSClassRef classDef = JSClassCreate(¬ification_def);
JSObjectRef classObj = JSObjectMake(context, classDef, context);
JSObjectRef globalObj = JSContextGetGlobalObject(context);
JSStringRef str = JSStringCreateWithUTF8CString("Notification");
JSObjectSetProperty(context, globalObj, str, classObj, kJSPropertyAttributeNone, NULL);
}
/* Destroy callback */
static void destroy(GtkWidget *widget,
gpointer data )
{
gtk_main_quit();
}
int
main (int argc, char* argv[])
{
/* Initialize the widget set */
gtk_init (&argc, &argv);
/* Create the window widgets */
GtkWidget *main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
/* Create the WebKit Web View widget */
GtkWidget *web_view = webkit_web_view_new ();
/* Connect the window object cleared event with callback */
g_signal_connect (G_OBJECT (web_view), "window-object-cleared", G_CALLBACK(window_object_cleared_cb), web_view);
/* Place the WebKitWebView in the GtkScrolledWindow */
gtk_container_add (GTK_CONTAINER (scrolled_window), web_view);
gtk_container_add (GTK_CONTAINER (main_window), scrolled_window);
/* Connect the destroy window event with destroy function */
g_signal_connect (G_OBJECT (main_window), "destroy", G_CALLBACK (destroy), NULL);
/* Open webpage */
webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), "file://webkit-03.html");
/* Create the main window */
gtk_window_set_default_size (GTK_WINDOW (main_window), 800, 600);
/* Show the application window */
gtk_widget_show_all (main_window);
/* Enter the main event loop, and wait for user interaction */
gtk_main ();
/* The user lost interest */
return 0;
}