summaryrefslogtreecommitdiff
path: root/src/evbp.c
blob: 0afbb238a79f21a03ef3e4c41c70f338ade72322 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "evbp.h"
#include "evbp-mime.h"
#include "evbp-debug.h"

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <npapi.h>
#include <npfunctions.h>
#include <prtypes.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gdk/gdkx.h>

#ifdef PACKAGE_VERSION
#define EVBP_VERSION PACKAGE_VERSION
#else
#define EVBP_VERSION ""
#endif
#define EVBP_NAME "Evince Plugin"
#define EVBP_DESCRIPTION "Evince Browser Plugin " EVBP_VERSION

static char *evbp_mime_string = NULL;
static const NPNetscapeFuncs *npn_funcs = NULL;
static gboolean evbp_is_initialized = FALSE;

static gboolean
evbp_initialize(void)
{
    evbp_debug(NULL);

    /* Some backends don't handle repeated init */
    if (evbp_is_initialized)
        return TRUE;

    /* Init glib threads asap */
    if (!g_thread_supported())
        g_thread_init(NULL);

#ifdef ENABLE_NLS
    /* Initialize the i18n stuff */
    bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
    textdomain(GETTEXT_PACKAGE);
#endif

    /* Initialize gtk */
    gtk_init(NULL, NULL);

    /* Initialize evince */
    if (!ev_init())
        return FALSE;
    ev_stock_icons_init();

    evbp_is_initialized = TRUE;
    return TRUE;
}

static void
evbp_shutdown(void)
{
    evbp_debug(NULL);

    if (!evbp_is_initialized)
        return;

    ev_shutdown();
    ev_stock_icons_shutdown();

    evbp_is_initialized = FALSE;
}

static NPError
evbp_new(NPMIMEType pluginType, NPP instance, uint16_t mode,
         int16_t argc, char *argn[], char *argv[],
         NPSavedData *saved)
{
    evbp_priv_t *priv;

    evbp_debug(NULL);

    /* create new private data */
    priv = npn_funcs->memalloc(sizeof(*priv));
    if (!priv)
        return NPERR_OUT_OF_MEMORY_ERROR;
    memset(priv, 0, sizeof(*priv));

    /* link the structures */
    priv->npp = instance;
    instance->pdata = priv;

    /* create new previewer */
    priv->model = ev_document_model_new();
    priv->scroll = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(priv->scroll),
                                   GTK_POLICY_AUTOMATIC,
                                   GTK_POLICY_AUTOMATIC);
    priv->view = ev_view_new();
    ev_view_set_model(EV_VIEW(priv->view), priv->model);
    ev_view_set_loading(EV_VIEW(priv->view), TRUE);
    gtk_container_add(GTK_CONTAINER(priv->scroll), priv->view);

    return NPERR_NO_ERROR;
}

static NPError
evbp_destroy(NPP instance, NPSavedData** save)
{
    evbp_priv_t *priv = instance->pdata;

    evbp_debug(NULL);

    g_object_unref(priv->document);
    g_object_unref(priv->model);
    npn_funcs->memfree(instance->pdata);
    instance->pdata = NULL;

    return NPERR_NO_ERROR;
}

static NPError
evbp_set_window(NPP instance, NPWindow *window)
{
    evbp_priv_t *priv = instance->pdata;
    GdkNativeWindow id;
    GtkWidget *plug;

    evbp_debug(NULL);

    /* If this is the same window we already have, don't create a
       new plug */
    if (window == priv->window) {
        evbp_debug("Same window; returning\n");
        return NPERR_NO_ERROR;
    }
    priv->window = window;

    /* Create a plug from the window id we were told about */
    id = (GdkNativeWindow)(unsigned long)window->window;
    evbp_debug("Plugging previewer into window id %u\n", id);
    plug = gtk_plug_new(id);
    gtk_container_add(GTK_CONTAINER(plug), priv->scroll);
    gtk_widget_show_all(plug);

    return NPERR_NO_ERROR;
}

static NPError
evbp_new_stream(NPP instance, NPMIMEType type, NPStream *stream,
                NPBool seekable, uint16_t *stype)
{
    evbp_debug(NULL);

    /* fetch the url to a local file only */
    *stype = NP_ASFILEONLY;

    return NPERR_NO_ERROR;
}

static NPError
evbp_destroy_stream(NPP instance, NPStream *stream, NPReason reason)
{
    evbp_debug(NULL);

    return NPERR_NO_ERROR;
}

static int32_t
evbp_write_ready(NPP instance, NPStream *stream)
{
    evbp_debug(NULL);

    return 0;
}

static int32_t
evbp_write(NPP instance, NPStream *stream, int32_t offset, int32_t len,
           void *buffer)
{
    evbp_debug(NULL);

    return 0;
}

static void
evbp_stream_as_file(NPP instance, NPStream *stream, const char *fname)
{
    evbp_priv_t *priv = instance->pdata;
    GFile *file;
    gchar *uri;
    GError *error = NULL;

    evbp_debug(NULL);

    if (!g_file_test(fname, G_FILE_TEST_IS_REGULAR)) {
        evbp_debug("Filename \"%s\" does not exist or is not a regular file",
                   fname);
        return;
    }
    priv->filename = fname;

    /* convert the filename to a uri */
    file = g_file_new_for_commandline_arg(fname);
    uri = g_file_get_uri(file);
    g_object_unref(file);

    /* create a new document */
    priv->document = ev_document_factory_get_document(uri, &error);
    if (error) {
        evbp_debug("Failed to load document \"%s\": %s\n", uri,
                   error->message);
        g_error_free(error);
        return;
    }
    ev_document_model_set_document(priv->model, priv->document);
    ev_view_set_loading(EV_VIEW(priv->view), FALSE);
}

static void
evbp_print(NPP instance, NPPrint *platformPrint)
{
    evbp_debug(NULL);
}

static int16_t
evbp_handle_event(NPP instance, void *event)
{
    evbp_debug(NULL);

    return 0;
}

static void
evbp_url_notify(NPP instance, const char *url, NPReason reason,
                void *notifyData)
{
    evbp_debug(NULL);
}

static NPError
evbp_get_value(NPP instance, NPPVariable variable, void *value)
{
    NPError err = NPERR_NO_ERROR;

    evbp_debug(NULL);

    switch (variable) {
    case NPPVpluginNameString:
        *((char **)value) = EVBP_NAME;
        break;
    case NPPVpluginDescriptionString:
        *((char **)value) = EVBP_DESCRIPTION;
        break;
    case NPPVpluginNeedsXEmbed:
        /* Require XEmbed */
        *((PRBool *)value) = PR_TRUE;
        break;
    default:
        err = NPERR_GENERIC_ERROR;
    }

    return err;
}

static NPError
evbp_set_value(NPP instance, NPNVariable variable, void *value)
{
    evbp_debug(NULL);

    return NPERR_GENERIC_ERROR;
}

static NPError
save_npn_funcs(const NPNetscapeFuncs *funcs)
{
    evbp_debug(NULL);

    /* Check for valid structure and compatibility */
    if (!funcs)
        return NPERR_INVALID_FUNCTABLE_ERROR;
    if (funcs->version >> 8 > NP_VERSION_MAJOR)
        return NPERR_INCOMPATIBLE_VERSION_ERROR;
    if (funcs->size < sizeof(npn_funcs))
        return NPERR_INVALID_FUNCTABLE_ERROR;

    npn_funcs = funcs;
    return NPERR_NO_ERROR;
}

static NPError
fill_npp_funcs(NPPluginFuncs *funcs)
{
    evbp_debug(NULL);

    if (!funcs)
        return NPERR_INVALID_FUNCTABLE_ERROR;

    /* Fill the browser's table */
    funcs->version       = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
    funcs->size          = sizeof(*funcs);
    funcs->newp          = evbp_new;
    funcs->destroy       = evbp_destroy;
    funcs->setwindow     = evbp_set_window;
    funcs->newstream     = evbp_new_stream;
    funcs->destroystream = evbp_destroy_stream;
    funcs->asfile        = evbp_stream_as_file;
    funcs->writeready    = evbp_write_ready;
    funcs->write         = evbp_write;
    funcs->print         = evbp_print;
    funcs->event         = evbp_handle_event;
    funcs->urlnotify     = evbp_url_notify;
    funcs->getvalue      = evbp_get_value;
    funcs->setvalue      = evbp_set_value;

    return NPERR_NO_ERROR;
}

NPError
NP_Initialize(NPNetscapeFuncs *nfuncs, NPPluginFuncs *pfuncs)
{
    NPError ret;
    PRBool xembed = PR_FALSE;
    NPNToolkitType toolkit = 0;

    evbp_debug(NULL);

    ret = save_npn_funcs(nfuncs);
    if (ret != NPERR_NO_ERROR)
        return ret;

    ret = fill_npp_funcs(pfuncs);
    if (ret != NPERR_NO_ERROR)
        return ret;

    /* Check that the browser supports XEmbed */
    ret = npn_funcs->getvalue(NULL, NPNVSupportsXEmbedBool,
                              (void *)&xembed);
    if (ret != NPERR_NO_ERROR || xembed != PR_TRUE)
        return NPERR_INCOMPATIBLE_VERSION_ERROR;

    /* Check that the toolkit is Gtk2 */
    ret = npn_funcs->getvalue(NULL, NPNVToolkit, (void *)&toolkit);
    if (ret != NPERR_NO_ERROR || toolkit != NPNVGtk2)
        return NPERR_INCOMPATIBLE_VERSION_ERROR;

    if (!evbp_initialize())
        return NPERR_MODULE_LOAD_FAILED_ERROR;

    return NPERR_NO_ERROR;
}

char *
NP_GetPluginVersion(void)
{
    evbp_debug(NULL);

    return EVBP_VERSION;
}

const char *
NP_GetMIMEDescription(void)
{
    GString *mimes;

    evbp_debug(NULL);

    if (!evbp_initialize())
        return NULL;

    mimes = evbp_get_mime_description();
    if (evbp_mime_string)
        g_free(evbp_mime_string);
    evbp_mime_string = g_string_free(mimes, FALSE);

    return evbp_mime_string;
}

NPError
NP_Shutdown(void)
{
    evbp_debug(NULL);

    if (evbp_mime_string) {
        g_free(evbp_mime_string);
        evbp_mime_string = NULL;
    }
    npn_funcs = NULL;

    evbp_shutdown();

    return NPERR_NO_ERROR;
}

NPError
NP_GetValue(void *instance, NPPVariable variable, void *value)
{
    evbp_debug(NULL);

    return evbp_get_value(NULL, variable, value);
}