~ubuntu-branches/ubuntu/oneiric/gecko-mediaplayer/oneiric

« back to all changes in this revision

Viewing changes to .pc/dont_use_libxul.patch/src/plugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2011-05-27 15:19:19 UTC
  • Revision ID: james.westby@ubuntu.com-20110527151919-rkrubbf0f2od2js0
Tags: 1.0.0-0ubuntu3
* Don't use any Mozilla interfaces at all. The only interfaces used
  previously were nsIPrefBranch and nsIPrefService to change a preference
  which configured the UA string to "QuickTime/7.6.4". This functionality
  is Firefox specific anyway, and so doesn't work in any Webkit based browser
  (Chromium, Epiphany, Midori etc). It also doesn't work in Firefox 4
  and later since the plugin is running in a separate process, where setting
  prefs in the plugin process has no effect on the browser process at all
  - add debian/patches/dont_use_libxul.patch
  - add debian/patches/autotools.patch
  - update debian/patches/series
  - update debian/control to build-depend on firefox-dev
  - see https://blueprints.launchpad.net/ubuntu/+spec/
          desktop-o-mozilla-rapid-release-maintenance and
    https://lists.ubuntu.com/archives/ubuntu-devel/2011-May/033229.html

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
17
 * The Initial Developer of the Original Code is
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1998
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
 
 
39
#include "plugin.h"
 
40
//#include "npupp.h"
 
41
#include "plugin_list.h"
 
42
#include "plugin_setup.h"
 
43
#include "plugin_types.h"
 
44
#include "plugin_dbus.h"
 
45
#include <nsIPrefBranch.h>
 
46
#include <nsIPrefService.h>
 
47
#include <nsIServiceManager.h>
 
48
#include <dlfcn.h>
 
49
 
 
50
nsIPrefBranch *prefBranch = NULL;
 
51
nsIPrefService *prefService = NULL;
 
52
static NPObject *sWindowObj;
 
53
 
 
54
//#include "nsIServiceManager.h"
 
55
//#include "nsISupportsUtils.h"   // some usefule macros are defined here
 
56
 
 
57
#define MIME_TYPES_HANDLED  "application/scriptable-plugin"
 
58
#define PLUGIN_NAME         "Scriptable Example Plugin for Mozilla"
 
59
#define MIME_TYPES_DESCRIPTION  MIME_TYPES_HANDLED":scr:"PLUGIN_NAME
 
60
#define PLUGIN_DESCRIPTION  PLUGIN_NAME " (Plug-ins SDK sample)"
 
61
 
 
62
int32 STREAMBUFSIZE = 0X0FFFFFFF;
 
63
 
 
64
// methods
 
65
static NPIdentifier Play_id;
 
66
static NPIdentifier PlayAt_id;
 
67
static NPIdentifier Pause_id;
 
68
static NPIdentifier PlayPause_id;
 
69
static NPIdentifier Stop_id;
 
70
static NPIdentifier quit_id;
 
71
static NPIdentifier DoPlay_id;
 
72
static NPIdentifier DoPause_id;
 
73
static NPIdentifier FastForward_id;
 
74
static NPIdentifier FastReverse_id;
 
75
static NPIdentifier ff_id;
 
76
static NPIdentifier rew_id;
 
77
static NPIdentifier rewind_id;
 
78
static NPIdentifier Seek_id;
 
79
static NPIdentifier Open_id;
 
80
static NPIdentifier SetVolume_id;
 
81
static NPIdentifier GetVolume_id;
 
82
static NPIdentifier SetFileName_id;
 
83
static NPIdentifier GetFileName_id;
 
84
static NPIdentifier SetIsLooping_id;
 
85
static NPIdentifier GetIsLooping_id;
 
86
static NPIdentifier SetAutoPlay_id;
 
87
static NPIdentifier GetAutoPlay_id;
 
88
static NPIdentifier SetHREF_id;
 
89
static NPIdentifier GetHREF_id;
 
90
static NPIdentifier SetURL_id;
 
91
static NPIdentifier GetURL_id;
 
92
static NPIdentifier GetMIMEType_id;
 
93
static NPIdentifier getTime_id;
 
94
static NPIdentifier getDuration_id;
 
95
static NPIdentifier getPercent_id;
 
96
static NPIdentifier isplaying_id;
 
97
static NPIdentifier playlistAppend_id;
 
98
static NPIdentifier playlistClear_id;
 
99
static NPIdentifier onClick_id;
 
100
static NPIdentifier onMediaComplete_id;
 
101
static NPIdentifier onMouseUp_id;
 
102
static NPIdentifier onMouseDown_id;
 
103
static NPIdentifier onMouseOut_id;
 
104
static NPIdentifier onMouseOver_id;
 
105
static NPIdentifier onDestroy_id;
 
106
 
 
107
static NPIdentifier controls_play_id;
 
108
static NPIdentifier controls_pause_id;
 
109
static NPIdentifier controls_stop_id;
 
110
static NPIdentifier controls_fastForward_id;
 
111
static NPIdentifier controls_fastReverse_id;
 
112
static NPIdentifier controls_step_id;
 
113
 
 
114
 
 
115
// properties
 
116
static NPIdentifier filename_id;
 
117
static NPIdentifier src_id;
 
118
static NPIdentifier ShowControls_id;
 
119
static NPIdentifier fullscreen_id;
 
120
static NPIdentifier showlogo_id;
 
121
static NPIdentifier playState_id;
 
122
static NPIdentifier controls_id;
 
123
 
 
124
static NPIdentifier controls_currentPosition_id;
 
125
//////////////////////////////////////
 
126
//
 
127
// general initialization and shutdown
 
128
//
 
129
NPError NS_PluginInitialize()
 
130
{
 
131
    return NPERR_NO_ERROR;
 
132
}
 
133
 
 
134
void NS_PluginShutdown()
 
135
{
 
136
}
 
137
 
 
138
// get values per plugin
 
139
NPError NS_PluginGetValue(NPPVariable aVariable, void *aValue)
 
140
{
 
141
 
 
142
    return PluginGetValue(aVariable, aValue);
 
143
}
 
144
 
 
145
 
 
146
/////////////////////////////////////////////////////////////
 
147
//
 
148
// construction and destruction of our plugin instance object
 
149
//
 
150
/*
 
151
CPlugin *NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
 
152
{
 
153
    if (!aCreateDataStruct)
 
154
        return NULL;
 
155
 
 
156
    CPlugin *plugin = new CPlugin(aCreateDataStruct->instance);
 
157
 
 
158
    new_instance(plugin, aCreateDataStruct);
 
159
    return plugin;
 
160
}
 
161
 
 
162
void NS_DestroyPluginInstance(CPlugin * aPlugin)
 
163
{
 
164
    if (aPlugin)
 
165
        delete(CPlugin *) aPlugin;
 
166
}
 
167
 
 
168
 
 
169
*/
 
170
void postDOMEvent(NPP mInstance, const gchar * id, const gchar * event)
 
171
{
 
172
    gchar *jscript;
 
173
 
 
174
    jscript = g_strdup_printf("javascript:obj_gmp=document.getElementById('%s');"
 
175
                              "e_gmp=document.createEvent('Events');"
 
176
                              "e_gmp.initEvent('%s',true,true);" "obj_gmp.dispatchEvent(e_gmp);",
 
177
                              id, event);
 
178
    NPN_GetURL(mInstance, jscript, NULL);
 
179
    g_free(jscript);
 
180
}
 
181
 
 
182
void setPreference(CPlugin * instance, const gchar * name, const gchar * value)
 
183
{
 
184
    nsIServiceManager *sm = NULL;
 
185
    nsIServiceManager *ServiceManager = NULL;
 
186
    PRBool v;
 
187
    void (*get_sm)(nsIServiceManager**);
 
188
 
 
189
    get_sm = (void (*)(nsIServiceManager**))dlsym(RTLD_DEFAULT,"NS_GetServiceManager");
 
190
    if (get_sm) {
 
191
        (*get_sm)(&sm);
 
192
    } else {
 
193
        NPN_GetValue(NULL, NPNVserviceManager, &sm);
 
194
    }
 
195
 
 
196
    if (sm) {
 
197
        sm->QueryInterface(NS_GET_IID(nsIServiceManager), (void **) (&ServiceManager));
 
198
        NS_RELEASE(sm);
 
199
    }
 
200
 
 
201
    if (ServiceManager) {
 
202
        ServiceManager->GetServiceByContractID(NS_PREFSERVICE_CONTRACTID, NS_GET_IID(nsIPrefBranch),
 
203
                                               (void **) &prefBranch);
 
204
        if (prefBranch) {
 
205
            instance->user_agent = g_new0(gchar, 1024);
 
206
            prefBranch->PrefHasUserValue(name, &v);
 
207
            if (v) {
 
208
                prefBranch->GetCharPref(name, &(instance->user_agent));
 
209
                prefBranch->ClearUserPref(name);
 
210
            }
 
211
            prefBranch->SetCharPref(name, value);
 
212
            printf("Set preference %s to %s, old value was '%s'\n", name, value,
 
213
                   instance->user_agent);
 
214
        }
 
215
        NS_RELEASE(ServiceManager);
 
216
    }
 
217
 
 
218
}
 
219
 
 
220
void clearPreference(CPlugin * instance, const gchar * name)
 
221
{
 
222
    nsIServiceManager *sm = NULL;
 
223
    nsIServiceManager *ServiceManager = NULL;
 
224
    PRBool v;
 
225
    void (*get_sm)(nsIServiceManager**);
 
226
 
 
227
    get_sm = (void (*)(nsIServiceManager**))dlsym(RTLD_DEFAULT,"NS_GetServiceManager");
 
228
    if (get_sm) {
 
229
        (*get_sm)(&sm);
 
230
    } else {
 
231
        NPN_GetValue(NULL, NPNVserviceManager, &sm);
 
232
    }
 
233
 
 
234
    if (sm) {
 
235
        sm->QueryInterface(NS_GET_IID(nsIServiceManager), (void **) (&ServiceManager));
 
236
        NS_RELEASE(sm);
 
237
    }
 
238
 
 
239
    if (ServiceManager) {
 
240
        ServiceManager->GetServiceByContractID(NS_PREFSERVICE_CONTRACTID, NS_GET_IID(nsIPrefBranch),
 
241
                                               (void **) &prefBranch);
 
242
        if (prefBranch) {
 
243
            if (instance->user_agent == NULL || strlen(instance->user_agent) == 0) {
 
244
                prefBranch->ClearUserPref(name);
 
245
            } else {
 
246
                if (g_strrstr(instance->user_agent, "QuickTime/7.6.4")) {
 
247
                    prefBranch->ClearUserPref(name);
 
248
                } else {
 
249
                    prefBranch->ClearUserPref(name);
 
250
                    prefBranch->SetCharPref(name, instance->user_agent);
 
251
                }
 
252
            }
 
253
            g_free(instance->user_agent);
 
254
        }
 
255
        NS_RELEASE(ServiceManager);
 
256
    }
 
257
}
 
258
 
 
259
////////////////////////////////////////
 
260
//
 
261
// CPlugin class implementation
 
262
//
 
263
CPlugin::CPlugin(NPP pNPInstance):
 
264
mInstance(pNPInstance),
 
265
m_pNPStream(NULL),
 
266
mInitialized(FALSE),
 
267
m_pScriptableObject(NULL),
 
268
m_pScriptableObjectControls(NULL),
 
269
mWindow(0),
 
270
windowless(FALSE),
 
271
playlist(NULL),
 
272
player_launched(FALSE),
 
273
connection(NULL),
 
274
dbus_dispatch(NULL),
 
275
path(NULL),
 
276
acceptdata(TRUE),
 
277
playerready(FALSE),
 
278
nextid(1),
 
279
lastopened(NULL),
 
280
cache_size(2048),
 
281
hidden(FALSE),
 
282
autostart(1),
 
283
lastupdate(0),
 
284
show_controls(1),
 
285
name(NULL),
 
286
id(NULL),
 
287
console(NULL),
 
288
controls(NULL),
 
289
user_agent(NULL),
 
290
page_url(NULL),
 
291
player_backend(NULL),
 
292
disable_context_menu(FALSE),
 
293
disable_fullscreen(FALSE),
 
294
post_dom_events(FALSE),
 
295
event_mediacomplete(NULL),
 
296
event_destroy(NULL),
 
297
event_mousedown(NULL),
 
298
event_mouseup(NULL),
 
299
event_mouseclicked(NULL), event_enterwindow(NULL), event_leavewindow(NULL), debug(FALSE),
 
300
tv_driver(NULL), tv_device(NULL), tv_input(NULL), tv_width(0), tv_height(0)
 
301
{
 
302
    GRand *rand;
 
303
    GmPrefStore *store;
 
304
    gboolean b;
 
305
 
 
306
    NPN_GetValue(mInstance, NPNVWindowNPObject, &sWindowObj);
 
307
    
 
308
    // get the page plugin was called from 
 
309
    // found at https://developer.mozilla.org/en/Getting_the_page_URL_in_NPAPI_plugin
 
310
    NPIdentifier identifier = NPN_GetStringIdentifier( "location" );
 
311
    // Declare a local variant value.
 
312
    NPVariant variantValue;
 
313
    // Get the location property from the window object (which is another object).
 
314
    b = NPN_GetProperty( mInstance, sWindowObj, identifier, &variantValue );
 
315
    // Get a pointer to the "location" object.
 
316
    NPObject *locationObj = variantValue.value.objectValue;
 
317
    // Create a "href" identifier.
 
318
    identifier = NPN_GetStringIdentifier( "href" );
 
319
    // Get the location property from the location object.
 
320
    b = NPN_GetProperty( mInstance, locationObj, identifier, &variantValue );
 
321
#ifdef HAVE_NEW_XULRUNNER
 
322
    page_url = g_strdup_printf("%s",NPVARIANT_TO_STRING(variantValue).UTF8Characters);
 
323
#else
 
324
    page_url = g_strdup_printf("%s",NPVARIANT_TO_STRING(variantValue).utf8characters);
 
325
#endif    
 
326
 
 
327
    // register methods
 
328
    Play_id = NPN_GetStringIdentifier("Play");
 
329
    PlayAt_id = NPN_GetStringIdentifier("PlayAt");
 
330
    Pause_id = NPN_GetStringIdentifier("Pause");
 
331
    PlayPause_id = NPN_GetStringIdentifier("PlayPause");
 
332
    Stop_id = NPN_GetStringIdentifier("Stop");
 
333
    quit_id = NPN_GetStringIdentifier("quit");
 
334
    DoPlay_id = NPN_GetStringIdentifier("DoPlay");
 
335
    DoPause_id = NPN_GetStringIdentifier("DoPause");
 
336
    FastForward_id = NPN_GetStringIdentifier("FastForward");
 
337
    FastReverse_id = NPN_GetStringIdentifier("FastReverse");
 
338
    ff_id = NPN_GetStringIdentifier("ff");
 
339
    rew_id = NPN_GetStringIdentifier("rew");
 
340
    rewind_id = NPN_GetStringIdentifier("rewind");
 
341
    Seek_id = NPN_GetStringIdentifier("Seek");
 
342
    Open_id = NPN_GetStringIdentifier("Open");
 
343
    SetVolume_id = NPN_GetStringIdentifier("SetVolume");
 
344
    GetVolume_id = NPN_GetStringIdentifier("GetVolume");
 
345
    SetFileName_id = NPN_GetStringIdentifier("SetFileName");
 
346
    GetFileName_id = NPN_GetStringIdentifier("GetFileName");
 
347
    SetIsLooping_id = NPN_GetStringIdentifier("SetIsLooping");
 
348
    GetIsLooping_id = NPN_GetStringIdentifier("GetIsLooping");
 
349
    SetAutoPlay_id = NPN_GetStringIdentifier("SetAutoPlay");
 
350
    GetAutoPlay_id = NPN_GetStringIdentifier("GetAutoPlay");
 
351
    SetHREF_id = NPN_GetStringIdentifier("SetHREF");
 
352
    GetHREF_id = NPN_GetStringIdentifier("GetHREF");
 
353
    SetURL_id = NPN_GetStringIdentifier("SetURL");
 
354
    GetURL_id = NPN_GetStringIdentifier("GetURL");
 
355
    GetMIMEType_id = NPN_GetStringIdentifier("GetMIMEType");
 
356
    getTime_id = NPN_GetStringIdentifier("getTime");
 
357
    getDuration_id = NPN_GetStringIdentifier("getDuration");
 
358
    getPercent_id = NPN_GetStringIdentifier("getPercent");
 
359
    isplaying_id = NPN_GetStringIdentifier("isplaying");
 
360
    playlistAppend_id = NPN_GetStringIdentifier("playlistAppend");
 
361
    playlistClear_id = NPN_GetStringIdentifier("playlistClear");
 
362
    onClick_id = NPN_GetStringIdentifier("onClick");
 
363
    onMediaComplete_id = NPN_GetStringIdentifier("onMediaComplete");
 
364
    onMouseUp_id = NPN_GetStringIdentifier("onMouseUp");
 
365
    onMouseDown_id = NPN_GetStringIdentifier("onMouseDown");
 
366
    onMouseOut_id = NPN_GetStringIdentifier("onMouseOut");
 
367
    onMouseOver_id = NPN_GetStringIdentifier("onMouseOver");
 
368
    onDestroy_id = NPN_GetStringIdentifier("onDestroy");
 
369
 
 
370
    controls_play_id = NPN_GetStringIdentifier("play");
 
371
    controls_pause_id = NPN_GetStringIdentifier("pause");
 
372
    controls_stop_id = NPN_GetStringIdentifier("stop");
 
373
    controls_fastForward_id = NPN_GetStringIdentifier("fastForward");
 
374
    controls_fastReverse_id = NPN_GetStringIdentifier("fastReverse");
 
375
    controls_step_id = NPN_GetStringIdentifier("step");
 
376
 
 
377
    // register properties
 
378
    filename_id = NPN_GetStringIdentifier("filename");
 
379
    src_id = NPN_GetStringIdentifier("src");
 
380
    ShowControls_id = NPN_GetStringIdentifier("ShowControls");
 
381
    fullscreen_id = NPN_GetStringIdentifier("fullscreen");
 
382
    showlogo_id = NPN_GetStringIdentifier("showlogo");
 
383
    playState_id = NPN_GetStringIdentifier("playState");
 
384
    controls_id = NPN_GetStringIdentifier("controls");
 
385
 
 
386
    controls_currentPosition_id = NPN_GetStringIdentifier("currentPosition");
 
387
 
 
388
    // generate a random controlid
 
389
    rand = g_rand_new();
 
390
    controlid = g_rand_int_range(rand, 0, 65535);
 
391
    g_rand_free(rand);
 
392
 
 
393
    if (path == NULL) {
 
394
        path = g_strdup_printf("/control/%i", controlid);
 
395
        // printf("using path %s\n",path);
 
396
    }
 
397
#ifdef ENABLE_NLS
 
398
    bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
 
399
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
 
400
    textdomain(GETTEXT_PACKAGE);
 
401
#endif
 
402
   printf(_("gecko mediaplayer v%s\n"), VERSION);
 
403
 
 
404
    g_type_init();
 
405
    store = gm_pref_store_new("gecko-mediaplayer");
 
406
    if (store != NULL) {
 
407
        debug_level = gm_pref_store_get_int(store, DEBUG_LEVEL);
 
408
        player_backend = gm_pref_store_get_string(store, PLAYER_BACKEND);
 
409
        printf("Using player backend of '%s'\n", player_backend);
 
410
        gm_pref_store_free(store);
 
411
    }
 
412
 
 
413
 
 
414
    if (connection == NULL) {
 
415
        connection = dbus_hookup(this);
 
416
    }
 
417
    pluginSpecific(this);
 
418
 
 
419
    mInitialized = TRUE;
 
420
}
 
421
 
 
422
CPlugin::~CPlugin()
 
423
{
 
424
    // mScriptablePeer may be also held by the browser 
 
425
    // so releasing it here does not guarantee that it is over
 
426
    // we should take precaution in case it will be called later
 
427
    // and zero its mPlugin member
 
428
    // mScriptablePeer->SetInstance(NULL);
 
429
    // NS_IF_RELEASE(mScriptablePeer);
 
430
 
 
431
    if (mInitialized)
 
432
        shut();
 
433
    if (sWindowObj)
 
434
        NPN_ReleaseObject(sWindowObj);
 
435
 
 
436
    mInstance = NULL;
 
437
/*
 
438
    if (mControlsScriptablePeer != NULL) {
 
439
        mControlsScriptablePeer->SetInstance(NULL);
 
440
        mControlsScriptablePeer->Release();
 
441
        NS_IF_RELEASE(mControlsScriptablePeer);
 
442
    }
 
443
*/
 
444
    clearPreference(this, "general.useragent.override");
 
445
 
 
446
    if (m_pScriptableObjectControls) {
 
447
        NPN_ReleaseObject(m_pScriptableObjectControls);
 
448
    }
 
449
 
 
450
    if (m_pScriptableObject) {
 
451
        NPN_ReleaseObject(m_pScriptableObject);
 
452
    }
 
453
}
 
454
 
 
455
 
 
456
 
 
457
NPBool CPlugin::init(NPWindow * pNPWindow)
 
458
{
 
459
    if (pNPWindow == NULL)
 
460
        return FALSE;
 
461
 
 
462
    m_Window = pNPWindow;
 
463
    mInitialized = TRUE;
 
464
 
 
465
    return mInitialized;
 
466
}
 
467
 
 
468
int16 CPlugin::handleEvent(void *event)
 
469
{
 
470
#ifdef XP_MAC
 
471
    NPEvent *ev = (NPEvent *) event;
 
472
    if (m_Window) {
 
473
        Rect box = { m_Window->y, m_Window->x,
 
474
            m_Window->y + m_Window->height, m_Window->x + m_Window->width
 
475
        };
 
476
        if (ev->what == updateEvt) {
 
477
            ::TETextBox(m_String, strlen(m_String), &box, teJustCenter);
 
478
        }
 
479
    }
 
480
#endif
 
481
    return 0;
 
482
}
 
483
 
 
484
NPError CPlugin::SetWindow(NPWindow * aWindow)
 
485
{
 
486
    GError *error = NULL;
 
487
    gchar *argvn[255];
 
488
    gint arg = 0;
 
489
    gint ok;
 
490
    ListItem *item;
 
491
    gchar *app_name;
 
492
 
 
493
    if (!acceptdata)
 
494
        return NPERR_NO_ERROR;
 
495
 
 
496
    if (aWindow == NULL)
 
497
        return NPERR_NO_ERROR;
 
498
 
 
499
    mX = aWindow->x;
 
500
    mY = aWindow->y;
 
501
    mWidth = aWindow->width;
 
502
    mHeight = aWindow->height;
 
503
    if (mWindow != (Window) aWindow->window) {
 
504
        mWindow = (Window) aWindow->window;
 
505
        NPSetWindowCallbackStruct *ws_info = (NPSetWindowCallbackStruct *) aWindow->ws_info;
 
506
    }
 
507
 
 
508
 
 
509
    if (player_launched && mWidth > 0 && mHeight > 0) {
 
510
        resize_window(this, NULL, mWidth, mHeight);
 
511
    }
 
512
 
 
513
    if (!player_launched && mWidth > 0 && mHeight > 0) {
 
514
        app_name = NULL;
 
515
        if (player_backend != NULL) {
 
516
            app_name = g_find_program_in_path(player_backend);
 
517
        }
 
518
        if (app_name == NULL) {
 
519
            app_name = g_find_program_in_path("gnome-mplayer");
 
520
            if (app_name == NULL)
 
521
                app_name = g_find_program_in_path("gnome-mplayer-minimal");
 
522
        }
 
523
 
 
524
        argvn[arg++] = g_strdup_printf("%s", app_name);
 
525
        argvn[arg++] = g_strdup_printf("--window=%i", (gint) mWindow);
 
526
        argvn[arg++] = g_strdup_printf("--controlid=%i", controlid);
 
527
        argvn[arg++] = g_strdup_printf("--width=%i", mWidth);
 
528
        argvn[arg++] = g_strdup_printf("--height=%i", mHeight);
 
529
        argvn[arg++] = g_strdup_printf("--autostart=%i", autostart);
 
530
        argvn[arg++] = g_strdup_printf("--showcontrols=%i", show_controls);
 
531
        if (disable_context_menu == TRUE)
 
532
            argvn[arg++] = g_strdup_printf("--disablecontextmenu");
 
533
        if (disable_fullscreen == TRUE)
 
534
            argvn[arg++] = g_strdup_printf("--disablefullscreen");
 
535
        if (debug == TRUE)
 
536
            argvn[arg++] = g_strdup_printf("--verbose");
 
537
        if (name != NULL)
 
538
            argvn[arg++] = g_strdup_printf("--rpname=%s", name);
 
539
        if (console != NULL)
 
540
            argvn[arg++] = g_strdup_printf("--rpconsole=%s", console);
 
541
        if (controls != NULL) {
 
542
            argvn[arg++] = g_strdup_printf("--rpcontrols=%s", controls);
 
543
        }
 
544
        if (tv_device != NULL) {
 
545
            argvn[arg++] = g_strdup_printf("--tvdevice=%s", tv_device);
 
546
        }
 
547
        if (tv_driver != NULL) {
 
548
            argvn[arg++] = g_strdup_printf("--tvdriver=%s", tv_driver);
 
549
        }
 
550
        if (tv_input != NULL) {
 
551
            argvn[arg++] = g_strdup_printf("--tvinput=%s", tv_input);
 
552
        }
 
553
        if (tv_width > 0) {
 
554
            argvn[arg++] = g_strdup_printf("--tvwidth=%i", tv_width);
 
555
        }
 
556
        if (tv_height > 0) {
 
557
            argvn[arg++] = g_strdup_printf("--tvheight=%i", tv_height);
 
558
        }
 
559
 
 
560
        argvn[arg] = NULL;
 
561
        playerready = FALSE;
 
562
        ok = g_spawn_async(NULL, argvn, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error);
 
563
 
 
564
        if (ok) {
 
565
            player_launched = TRUE;
 
566
        } else {
 
567
            printf("Unable to launch %s: %s\n", app_name, error->message);
 
568
            g_error_free(error);
 
569
            error = NULL;
 
570
        }
 
571
 
 
572
        g_free(app_name);
 
573
 
 
574
        if (post_dom_events && id != NULL) {
 
575
            postDOMEvent(mInstance, id, "qt_begin");
 
576
        }
 
577
    }
 
578
 
 
579
    if (playlist != NULL) {
 
580
        item = (ListItem *) playlist->data;
 
581
        if (!item->play)
 
582
            item = list_find_next_playable(playlist);
 
583
        if (!item->requested) {
 
584
            item->cancelled = FALSE;
 
585
            if (item->streaming) {
 
586
                printf("Calling open_location with item = %p src = %s\n", item, item->src);
 
587
                open_location(this, item, FALSE);
 
588
                item->requested = 1;
 
589
            } else {
 
590
                item->requested = 1;
 
591
                printf("Calling GetURLNotify with item = %p src = %s\n", item, item->src);
 
592
                NPN_GetURLNotify(mInstance, item->src, NULL, item);
 
593
            }
 
594
        }
 
595
    }
 
596
    return NPERR_NO_ERROR;
 
597
}
 
598
 
 
599
 
 
600
void CPlugin::shut()
 
601
{
 
602
    ListItem *item;
 
603
    GList *iter;
 
604
 
 
605
    acceptdata = FALSE;
 
606
    mInitialized = FALSE;
 
607
 
 
608
    if (playlist != NULL) {
 
609
        for (iter = playlist; iter != NULL; iter = g_list_next(iter)) {
 
610
            item = (ListItem *) iter->data;
 
611
            if (item != NULL) {
 
612
                if (item->controlid != 0) {
 
613
                    send_signal_when_ready(this, item, "Terminate");
 
614
                }
 
615
            }
 
616
        }
 
617
    }
 
618
    send_signal_when_ready(this, NULL, "Terminate");
 
619
    playerready = FALSE;
 
620
    playlist = list_clear(playlist);
 
621
 
 
622
    if (event_destroy != NULL) {
 
623
        NPN_GetURL(mInstance, event_destroy, NULL);
 
624
    }
 
625
 
 
626
    if (connection != NULL) {
 
627
        connection = dbus_unhook(connection, this);
 
628
    }
 
629
}
 
630
 
 
631
NPBool CPlugin::isInitialized()
 
632
{
 
633
    return mInitialized;
 
634
}
 
635
 
 
636
NPError CPlugin::NewStream(NPMIMEType type, NPStream * stream, NPBool seekable, uint16 * stype)
 
637
{
 
638
    if (g_strrstr(stream->url, "javascript") == NULL) {
 
639
        printf("New Stream Requested - %s\n", stream->url);
 
640
    }
 
641
 
 
642
    if (g_strrstr(stream->url, "javascript") == NULL && stream->notifyData == NULL) {
 
643
        printf("item is NULL for %s\n", stream->url);
 
644
    }
 
645
 
 
646
    return NPERR_NO_ERROR;
 
647
}
 
648
 
 
649
NPError CPlugin::DestroyStream(NPStream * stream, NPError reason)
 
650
{
 
651
    ListItem *item;
 
652
    gint id;
 
653
    gchar *path;
 
654
    gchar *text;
 
655
    gboolean ready;
 
656
    gboolean newwindow;
 
657
 
 
658
    if (g_strrstr(stream->url, "javascript") == NULL)
 
659
        printf("Entering destroy stream reason = %i for %s\n", reason, stream->url);
 
660
 
 
661
    if (reason == NPERR_NO_ERROR) {
 
662
        item = (ListItem *) stream->notifyData;
 
663
        // item = list_find(playlist, (gchar*)stream->url);
 
664
 
 
665
        if (item == NULL) {
 
666
            printf("Leaving destroy stream - item not found\n");
 
667
            return NPERR_NO_ERROR;
 
668
        }
 
669
 
 
670
        if (item->localfp) {
 
671
            fclose(item->localfp);
 
672
            item->retrieved = TRUE;
 
673
            item->localfp = 0;
 
674
            send_signal_with_double(this, item, "SetCachePercent", 1.0);
 
675
            text = g_strdup_printf(_("Cache fill: %2.2f%%"), 100.0);
 
676
            send_signal_with_string(this, item, "SetProgressText", text);
 
677
            g_free(text);
 
678
            if (post_dom_events && this->id != NULL) {
 
679
                postDOMEvent(mInstance, this->id, "qt_progress");
 
680
            }
 
681
        }
 
682
 
 
683
        if (!item->opened && item->play) {
 
684
            id = item->controlid;
 
685
            path = g_strdup(item->path);
 
686
            ready = item->playerready;
 
687
            newwindow = item->newwindow;
 
688
            if (!item->streaming)
 
689
                item->streaming = streaming(item->src);
 
690
            if (!item->streaming) {
 
691
                printf("in Destroy Stream\n");
 
692
                playlist = list_parse_qt(playlist, item);
 
693
                playlist = list_parse_qt2(playlist, item);
 
694
                playlist = list_parse_asx(playlist, item);
 
695
                playlist = list_parse_qml(playlist, item);
 
696
                playlist = list_parse_ram(playlist, item);
 
697
            }
 
698
            // printf("item->play = %i\n",item->play);
 
699
            // printf("item->src = %s\n", item->src);
 
700
            // printf("item->streaming = %i\n", item->streaming);
 
701
            // printf("calling open_location from DestroyStream\n");
 
702
            if (item->play) {
 
703
                item->requested = TRUE;
 
704
                open_location(this, item, TRUE);
 
705
                if (post_dom_events && this->id != NULL) {
 
706
                    postDOMEvent(mInstance, this->id, "qt_play");
 
707
                }
 
708
            } else {
 
709
                item = list_find_next_playable(playlist);
 
710
                if (item != NULL) {
 
711
                    // printf("item->play = %i\n",item->play);
 
712
                    // printf("item->src = %s\n", item->src);
 
713
                    // printf("item->streaming = %i\n", item->streaming);
 
714
                    if (!item->streaming) {
 
715
                        item->controlid = id;
 
716
                        g_strlcpy(item->path, path, 1024);
 
717
                        item->playerready = ready;
 
718
                        item->newwindow = newwindow;
 
719
                        item->cancelled = FALSE;
 
720
                        item->requested = TRUE;
 
721
                        if (item != NULL)
 
722
                            NPN_GetURLNotify(mInstance, item->src, NULL, item);
 
723
                    } else {
 
724
                        open_location(this, item, FALSE);
 
725
                        item->requested = TRUE;
 
726
                        if (post_dom_events && this->id != NULL) {
 
727
                            postDOMEvent(mInstance, this->id, "qt_play");
 
728
                        }
 
729
                    }
 
730
                }
 
731
            }
 
732
            g_free(path);
 
733
        }
 
734
        //printf("Leaving destroy stream src = %s\n", item->src);
 
735
 
 
736
    } else if (reason == NPERR_INVALID_URL) {
 
737
        item = (ListItem *) stream->notifyData;
 
738
        if (item) {
 
739
            printf("Destroy Stream, invalid url, item is %s\n", item->src);
 
740
        } else {
 
741
            if (g_strrstr(stream->url, "javascript") == NULL) {
 
742
                printf("Destroy Stream, network error, item is NULL\n");
 
743
            }
 
744
        }
 
745
    } else {
 
746
        item = (ListItem *) stream->notifyData;
 
747
        // item = list_find(playlist, (gchar*)stream->url);
 
748
        if (g_strrstr(stream->url, "javascript") == NULL)
 
749
            printf("Exiting destroy stream reason = %i for %s\n", reason, stream->url);
 
750
        if (item == NULL) {
 
751
            return NPERR_NO_ERROR;
 
752
        } else {
 
753
            if (post_dom_events && this->id != NULL) {
 
754
                postDOMEvent(mInstance, this->id, "qt_load");
 
755
            }
 
756
        }
 
757
 
 
758
        if (item->localfp) {
 
759
            fclose(item->localfp);
 
760
            item->retrieved = FALSE;
 
761
            item->localfp = 0;
 
762
        }
 
763
    }
 
764
 
 
765
    // list_dump(playlist);
 
766
    return NPERR_NO_ERROR;
 
767
}
 
768
 
 
769
void CPlugin::URLNotify(const char *url, NPReason reason, void *notifyData)
 
770
{
 
771
    ListItem *item = (ListItem *) notifyData;
 
772
    //DBusMessage *message;
 
773
    //const char *file;
 
774
 
 
775
    printf("URL Notify url = '%s'\nreason = %i\n%s\n%s\n%s\n", url, reason, item->src, item->local,
 
776
           path);
 
777
    if (reason == NPRES_DONE) {
 
778
 
 
779
        if (!item->opened) {
 
780
            // open_location(this,item, TRUE);
 
781
 
 
782
            /*
 
783
               file = g_strdup(item->local);
 
784
               while (!playerready) {
 
785
               // printf("waiting for player\n");
 
786
               g_main_context_iteration(NULL,FALSE);   
 
787
               }
 
788
               message = dbus_message_new_signal(path,"com.gnome.mplayer","Open");
 
789
               dbus_message_append_args(message, DBUS_TYPE_STRING, &file, DBUS_TYPE_INVALID);
 
790
               dbus_connection_send(connection,message,NULL);
 
791
               dbus_message_unref(message);
 
792
             */
 
793
        }
 
794
 
 
795
        if (item) {
 
796
            item->played = TRUE;
 
797
            if (!item->streaming) {
 
798
                item = list_find_next_playable(playlist);
 
799
                if (item) {
 
800
                    if (item->retrieved || item->streaming) {
 
801
                        open_location(this, item, TRUE);
 
802
                        item->requested = TRUE;
 
803
                    } else {
 
804
                        NPN_GetURLNotify(mInstance, item->src, NULL, item);
 
805
                        item->requested = TRUE;
 
806
                    }
 
807
                }
 
808
            }
 
809
        } else {
 
810
            item = list_find_next_playable(playlist);
 
811
            if (item) {
 
812
                if (item->retrieved || item->streaming) {
 
813
                    open_location(this, item, TRUE);
 
814
                    item->requested = TRUE;
 
815
                } else {
 
816
                    NPN_GetURLNotify(mInstance, item->src, NULL, item);
 
817
                    item->requested = TRUE;
 
818
                }
 
819
            }
 
820
        }
 
821
 
 
822
 
 
823
 
 
824
    } else if (reason == NPRES_NETWORK_ERR) {
 
825
        printf("URL Notify result is Network Error\n");
 
826
    } else if (reason == NPRES_USER_BREAK) {
 
827
        printf("URL Notify result is User Break\n");
 
828
    } else {
 
829
        printf("%i is an invalid reason code in URLNotify\n", reason);
 
830
    }
 
831
}
 
832
 
 
833
int32 CPlugin::WriteReady(NPStream * stream)
 
834
{
 
835
    ListItem *item;
 
836
    gchar *path;
 
837
    gchar *tmp;
 
838
 
 
839
    // printf("WriteReady called\n");
 
840
    if (!acceptdata) {
 
841
        printf("Not accepting data\n");
 
842
        NPN_DestroyStream(mInstance, stream, NPERR_GENERIC_ERROR);
 
843
        return -1;
 
844
    }
 
845
 
 
846
 
 
847
    item = (ListItem *) stream->notifyData;
 
848
    // item = list_find(playlist, (gchar*)stream->url);
 
849
    if (item == NULL) {
 
850
 
 
851
        if (mode == NP_FULL) {
 
852
            // printf("adding new item %s\n",stream->url);
 
853
            item = g_new0(ListItem, 1);
 
854
            g_strlcpy(item->src, stream->url, 1024);
 
855
            item->requested = TRUE;
 
856
            item->play = TRUE;
 
857
            if (!item->streaming)
 
858
                item->streaming = streaming(item->src);
 
859
            playlist = g_list_append(playlist, item);
 
860
            stream->notifyData = item;
 
861
        } else {
 
862
            printf("item is null\nstream url %s\n", stream->url);
 
863
            NPN_DestroyStream(mInstance, stream, NPERR_NO_ERROR);
 
864
            return -1;
 
865
        }
 
866
    } else {
 
867
        if (g_ascii_strcasecmp(item->src, stream->url) != 0) {
 
868
            g_strlcpy(item->src, stream->url, 4096);
 
869
        }
 
870
    }
 
871
 
 
872
    // printf("Write Ready item url = %s\n%s\n",item->src,stream->url);
 
873
 
 
874
    if (item->cancelled) {
 
875
        printf("cancelling WriteReady\n");
 
876
        NPN_DestroyStream(mInstance, stream, NPERR_NO_ERROR);
 
877
        return -1;
 
878
    }
 
879
 
 
880
    if (strlen(item->local) == 0) {
 
881
        path = g_strdup_printf("%s/gnome-mplayer/plugin", g_get_user_cache_dir());
 
882
        if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
 
883
            g_mkdir_with_parents(path, 0775);
 
884
        }
 
885
        tmp = gm_tempname(path, "gecko-mediaplayerXXXXXX");
 
886
        g_snprintf(item->local, 1024, "%s", tmp);
 
887
        g_free(tmp);
 
888
        g_free(path);
 
889
        if (strstr(mimetype, "midi") != NULL) {
 
890
            g_strlcat(item->local, ".mid", 1024);
 
891
        }
 
892
        if (strstr(mimetype, "mp3") != NULL) {
 
893
            g_strlcat(item->local, ".mp3", 1024);
 
894
        }
 
895
        if (strstr(mimetype, "audio/mpeg") != NULL) {
 
896
            g_strlcat(item->local, ".mp3", 1024);
 
897
        }
 
898
        if (strstr(mimetype, "audio/x-mod") != NULL) {
 
899
            g_strlcat(item->local, ".mod", 1024);
 
900
        }
 
901
        if (strstr(mimetype, "flac") != NULL) {
 
902
            g_strlcat(item->local, ".flac", 1024);
 
903
        }
 
904
 
 
905
    }
 
906
 
 
907
    if (item->retrieved) {
 
908
        printf("Duplicate request, item already retrieved\n");
 
909
        NPN_DestroyStream(mInstance, stream, NPERR_NO_ERROR);
 
910
        return -1;
 
911
    }
 
912
 
 
913
    return STREAMBUFSIZE;
 
914
}
 
915
 
 
916
int32 CPlugin::Write(NPStream * stream, int32 offset, int32 len, void *buffer)
 
917
{
 
918
    ListItem *item;
 
919
    int32 wrotebytes = -1;
 
920
    gchar *text;
 
921
    gdouble percent = 0.0;
 
922
    gdouble rate = 0.0;
 
923
    gint id;
 
924
    gchar *path;
 
925
    gboolean ready;
 
926
    gboolean newwindow;
 
927
    gboolean ok_to_play = FALSE;
 
928
 
 
929
    // printf("Write Called\n");
 
930
    if (!acceptdata) {
 
931
        printf("not accepting data\n");
 
932
        NPN_DestroyStream(mInstance, stream, NPERR_NO_ERROR);
 
933
        return -1;
 
934
    }
 
935
 
 
936
    item = (ListItem *) stream->notifyData;
 
937
 
 
938
    if (item == NULL) {
 
939
        printf("item is NULL\n");
 
940
        printf(_("Write unable to write because item is NULL\n"));
 
941
        NPN_DestroyStream(mInstance, stream, NPERR_GENERIC_ERROR);
 
942
        return -1;
 
943
    }
 
944
 
 
945
    if (item->cancelled || item->retrieved) {
 
946
        printf("cancelled\n");
 
947
        NPN_DestroyStream(mInstance, stream, NPERR_NO_ERROR);
 
948
        return -1;
 
949
    }
 
950
 
 
951
    if (strstr((char *) buffer, "ICY 200 OK") != NULL 
 
952
        || strstr((char *) buffer, "Content-length:") != NULL     // If item is a block of jpeg images, just stream it
 
953
        || strstr((char *) buffer, "<HTML>") != NULL 
 
954
        || item->streaming == TRUE ) {
 
955
        //   || stream->lastmodified == 0) {    this is not valid for many sites
 
956
 
 
957
        // printf("BUFFER='%s'\n", buffer);
 
958
 
 
959
        // printf("item->streaming = %i\n", item->streaming);
 
960
        // printf("stream->lastmodified = %i\n", stream->lastmodified);
 
961
        // printf("stream->end = %i\n", stream->end);
 
962
 
 
963
        item->streaming = TRUE;
 
964
        open_location(this, item, FALSE);
 
965
        if (post_dom_events && this->id != NULL) {
 
966
            postDOMEvent(mInstance, this->id, "qt_play");
 
967
        }
 
968
        item->requested = TRUE;
 
969
        if (item->localfp) {
 
970
            fclose(item->localfp);
 
971
        }
 
972
        printf("Got IceCast Stream, let mplayer stream it\n");
 
973
        NPN_DestroyStream(mInstance, stream, NPERR_NO_ERROR);
 
974
        return -1;
 
975
    }
 
976
 
 
977
    if ((!item->localfp) && (!item->retrieved)) {
 
978
        // printf("opening %s for localcache\n", item->local);
 
979
        item->localfp = fopen(item->local, "w+");
 
980
    }
 
981
    // printf("Write item url = %s\n",item->src);
 
982
 
 
983
    if (item->localfp == NULL) {
 
984
        printf("Local cache file is not open, cannot write data\n");
 
985
        NPN_DestroyStream(mInstance, stream, NPERR_NO_ERROR);
 
986
        return -1;
 
987
    }
 
988
    fseek(item->localfp, offset, SEEK_SET);
 
989
    wrotebytes = fwrite(buffer, 1, len, item->localfp);
 
990
    item->localsize += wrotebytes;
 
991
 
 
992
    if (item->mediasize != stream->end)
 
993
        item->mediasize = stream->end;
 
994
 
 
995
    if (playerready) {
 
996
        percent = 0.0;
 
997
        if (item->mediasize > 0) {
 
998
 
 
999
            percent = (gdouble) item->localsize / (gdouble) item->mediasize;
 
1000
            if (difftime(time(NULL), lastupdate) > 0.5) {
 
1001
                send_signal_with_double(this, item, "SetCachePercent", percent);
 
1002
                rate =
 
1003
                    (gdouble) ((item->localsize -
 
1004
                                item->lastsize) / 1024.0) / (gdouble) difftime(time(NULL),
 
1005
                                                                               lastupdate);
 
1006
                if (percent > 0.99) {
 
1007
                    text =
 
1008
                        g_strdup_printf(_("Caching %iK (%0.1f K/s)"), item->mediasize / 1024, rate);
 
1009
                } else {
 
1010
                    text =
 
1011
                        g_strdup_printf(_("Cache fill: %2.2f%% (%0.1f K/s)"), percent * 100.0,
 
1012
                                        rate);
 
1013
                }
 
1014
                send_signal_with_string(this, item, "SetProgressText", text);
 
1015
                if (!item->opened)
 
1016
                    send_signal_with_string(this, item, "SetURL", item->src);
 
1017
                if (post_dom_events && this->id != NULL) {
 
1018
                    postDOMEvent(mInstance, this->id, "qt_progress");
 
1019
                    postDOMEvent(mInstance, this->id, "qt_durationchange");
 
1020
                }
 
1021
 
 
1022
                time(&lastupdate);
 
1023
                item->lastsize = item->localsize;
 
1024
            }
 
1025
        }
 
1026
        if (!item->opened) {
 
1027
            if ((item->localsize >= (cache_size * 1024)) && (percent >= 0.2)) {
 
1028
                printf("Setting to play because %i > %i\n", item->localsize, cache_size * 1024);
 
1029
                ok_to_play = TRUE;
 
1030
            }
 
1031
            if (ok_to_play == FALSE && (item->localsize > (cache_size * 2 * 1024))
 
1032
                && (cache_size >= 512)) {
 
1033
                printf("Setting to play because %i > %i (double cache)\n", item->localsize,
 
1034
                       cache_size * 2 * 1024);
 
1035
                ok_to_play = TRUE;
 
1036
            }
 
1037
            if (ok_to_play == FALSE) {
 
1038
                if (item->bitrate == 0 && item->bitrate_requests < 5
 
1039
                    && ((gint) (percent * 100) > item->bitrate_requests)) {
 
1040
                    item->bitrate = request_bitrate(this, item, item->local);
 
1041
                    item->bitrate_requests++;
 
1042
                }
 
1043
                if (item->bitrate > 0) {
 
1044
                    if (item->localsize / item->bitrate >= 10 && (percent >= 0.2)) {
 
1045
                        printf("Setting to play becuase %i >= 10\n",
 
1046
                               item->localsize / item->bitrate);
 
1047
                        ok_to_play = TRUE;
 
1048
                        if (post_dom_events && this->id != NULL) {
 
1049
                            postDOMEvent(mInstance, this->id, "qt_canplay");
 
1050
                        }
 
1051
                    }
 
1052
                }
 
1053
            }
 
1054
 
 
1055
        }
 
1056
        // if not opened, over cache level and not an href target then try and open it
 
1057
        if ((!item->opened) && ok_to_play == TRUE) {
 
1058
            id = item->controlid;
 
1059
            path = g_strdup(item->path);
 
1060
            ready = item->playerready;
 
1061
            newwindow = item->newwindow;
 
1062
            if (!item->streaming)
 
1063
                item->streaming = streaming(item->src);
 
1064
            if (!item->streaming) {
 
1065
                printf("in Write\n");
 
1066
                playlist = list_parse_qt(playlist, item);
 
1067
                playlist = list_parse_asx(playlist, item);
 
1068
                playlist = list_parse_qml(playlist, item);
 
1069
                playlist = list_parse_ram(playlist, item);
 
1070
            }
 
1071
            // printf("item->play = %i\n",item->play);
 
1072
            // printf("item->src = %i\n", item->src);
 
1073
            // printf("calling open_location from Write\n"); 
 
1074
            if (item->play) {
 
1075
                send_signal_with_integer(this, item, "SetGUIState", PLAYING);
 
1076
                open_location(this, item, TRUE);
 
1077
                item->requested = TRUE;
 
1078
                if (post_dom_events && this->id != NULL) {
 
1079
                    postDOMEvent(mInstance, this->id, "qt_loadedfirstframe");
 
1080
                    postDOMEvent(mInstance, this->id, "qt_canplay");
 
1081
                    postDOMEvent(mInstance, this->id, "qt_play");
 
1082
                }
 
1083
            } else {
 
1084
                item = list_find_next_playable(playlist);
 
1085
                if (item != NULL) {
 
1086
                    item->controlid = id;
 
1087
                    g_strlcpy(item->path, path, 1024);
 
1088
                    item->playerready = ready;
 
1089
                    item->newwindow = newwindow;
 
1090
                    item->cancelled = FALSE;
 
1091
                    // printf("opening next playable items on the playlist\n");
 
1092
                    if (item->streaming) {
 
1093
                        open_location(this, item, FALSE);
 
1094
                        item->requested = TRUE;
 
1095
                    } else {
 
1096
                        NPN_GetURLNotify(mInstance, item->src, NULL, item);
 
1097
                        item->requested = TRUE;
 
1098
                    }
 
1099
                }
 
1100
            }
 
1101
            g_free(path);
 
1102
        }
 
1103
 
 
1104
    }
 
1105
 
 
1106
    return wrotebytes;
 
1107
}
 
1108
 
 
1109
 
 
1110
void CPlugin::Play()
 
1111
{
 
1112
    send_signal(this, this->lastopened, "Play");
 
1113
    if (post_dom_events && this->id != NULL) {
 
1114
        postDOMEvent(mInstance, this->id, "qt_play");
 
1115
    }
 
1116
}
 
1117
 
 
1118
void CPlugin::Pause()
 
1119
{
 
1120
    send_signal(this, this->lastopened, "Pause");
 
1121
    if (post_dom_events && this->id != NULL) {
 
1122
        postDOMEvent(mInstance, this->id, "qt_pause");
 
1123
    }
 
1124
}
 
1125
 
 
1126
void CPlugin::Stop()
 
1127
{
 
1128
    send_signal(this, this->lastopened, "Stop");
 
1129
}
 
1130
 
 
1131
void CPlugin::FastForward()
 
1132
{
 
1133
    send_signal(this, this->lastopened, "FastForward");
 
1134
}
 
1135
 
 
1136
void CPlugin::FastReverse()
 
1137
{
 
1138
    send_signal(this, this->lastopened, "FastReverse");
 
1139
}
 
1140
 
 
1141
void CPlugin::Seek(double counter)
 
1142
{
 
1143
    send_signal_with_double(this, this->lastopened, "Seek", counter);
 
1144
}
 
1145
 
 
1146
void CPlugin::SetShowControls(bool value)
 
1147
{
 
1148
    send_signal_with_boolean(this, this->lastopened, "SetShowControls", value);
 
1149
}
 
1150
 
 
1151
void CPlugin::SetFullScreen(bool value)
 
1152
{
 
1153
    send_signal_with_boolean(this, this->lastopened, "SetFullScreen", value);
 
1154
}
 
1155
 
 
1156
void CPlugin::SetVolume(double value)
 
1157
{
 
1158
    send_signal_with_double(this, this->lastopened, "Volume", value);
 
1159
    if (post_dom_events && this->id != NULL) {
 
1160
        postDOMEvent(mInstance, this->id, "qt_volumechange");
 
1161
    }
 
1162
}
 
1163
 
 
1164
void CPlugin::GetVolume(double *_retval)
 
1165
{
 
1166
    *_retval = request_double_value(this, this->lastopened, "GetVolume");
 
1167
}
 
1168
 
 
1169
void CPlugin::GetFullScreen(bool * _retval)
 
1170
{
 
1171
    *_retval = request_boolean_value(this, this->lastopened, "GetFullScreen");
 
1172
}
 
1173
 
 
1174
void CPlugin::GetShowControls(bool * _retval)
 
1175
{
 
1176
    *_retval = request_boolean_value(this, this->lastopened, "GetShowControls");
 
1177
}
 
1178
 
 
1179
void CPlugin::GetTime(double *_retval)
 
1180
{
 
1181
    *_retval = request_double_value(this, this->lastopened, "GetTime");
 
1182
}
 
1183
 
 
1184
void CPlugin::GetDuration(double *_retval)
 
1185
{
 
1186
    *_retval = request_double_value(this, this->lastopened, "GetDuration");
 
1187
}
 
1188
 
 
1189
void CPlugin::GetPercent(double *_retval)
 
1190
{
 
1191
    *_retval = request_double_value(this, this->lastopened, "GetPercent");
 
1192
}
 
1193
 
 
1194
void CPlugin::SetFilename(const char *filename)
 
1195
{
 
1196
    ListItem *item;
 
1197
 
 
1198
    if (filename == NULL)
 
1199
        return;
 
1200
 
 
1201
    item = g_new0(ListItem, 1);
 
1202
    g_strlcpy(item->src, filename, 1024);
 
1203
    item->streaming = streaming(item->src);
 
1204
    item->play = TRUE;
 
1205
    item->id = nextid++;
 
1206
    playlist = g_list_append(playlist, item);
 
1207
 
 
1208
    send_signal(this, this->lastopened, "Quit");
 
1209
 
 
1210
    if (item->streaming) {
 
1211
        open_location(this, item, FALSE);
 
1212
        item->requested = 1;
 
1213
    } else {
 
1214
        item->requested = 1;
 
1215
        NPN_GetURLNotify(mInstance, item->src, NULL, item);
 
1216
    }
 
1217
}
 
1218
 
 
1219
void CPlugin::GetFilename(char **filename)
 
1220
{
 
1221
    ListItem *item = NULL;
 
1222
 
 
1223
    if (this->lastopened != NULL) {
 
1224
        *filename = g_strdup(this->lastopened->src);
 
1225
    } else {
 
1226
        if (playlist != NULL)
 
1227
            item = (ListItem *) playlist->data;
 
1228
        if (item != NULL) {
 
1229
            *filename = g_strdup(item->src);
 
1230
        } else {
 
1231
            *filename = NULL;
 
1232
        }
 
1233
    }
 
1234
}
 
1235
 
 
1236
void CPlugin::GetMIMEType(char **_retval)
 
1237
{
 
1238
    *_retval = g_strdup(mimetype);
 
1239
}
 
1240
 
 
1241
void CPlugin::GetPlayState(PRInt32 * playstate)
 
1242
{
 
1243
    *playstate = request_int_value(this, this->lastopened, "GetPlayState");
 
1244
}
 
1245
 
 
1246
void CPlugin::GetLoop(bool * _retval)
 
1247
{
 
1248
    if (lastopened != NULL) {
 
1249
        *_retval = (bool) lastopened->loop;
 
1250
    } else {
 
1251
        *_retval = FALSE;
 
1252
    }
 
1253
 
 
1254
}
 
1255
 
 
1256
void CPlugin::SetLoop(bool value)
 
1257
{
 
1258
    if (lastopened != NULL) {
 
1259
        lastopened->loop = (int) value;
 
1260
        lastopened->loopcount = -1;
 
1261
    }
 
1262
}
 
1263
 
 
1264
void CPlugin::PlayPause()
 
1265
{
 
1266
    gint state;
 
1267
 
 
1268
    state = request_int_value(this, this->lastopened, "GetPlayState");
 
1269
    if (state == STATE_PAUSED) {
 
1270
        send_signal(this, this->lastopened, "Play");
 
1271
    }
 
1272
 
 
1273
    if (state == STATE_PLAYING) {
 
1274
        send_signal(this, this->lastopened, "Pause");
 
1275
    }
 
1276
}
 
1277
 
 
1278
 
 
1279
void CPlugin::SetOnClick(const char *event)
 
1280
{
 
1281
    if (event_mouseclicked != NULL) {
 
1282
        g_free(event_mouseclicked);
 
1283
    }
 
1284
 
 
1285
    if (g_ascii_strncasecmp(event, "javascript:", 11) == 0) {
 
1286
        event_mouseclicked = g_strdup_printf("%s", event);
 
1287
    } else {
 
1288
        event_mouseclicked = g_strdup_printf("javascript:%s", event);
 
1289
    }
 
1290
}
 
1291
 
 
1292
void CPlugin::SetOnMediaComplete(const char *event)
 
1293
{
 
1294
    if (event_mediacomplete != NULL) {
 
1295
        g_free(event_mediacomplete);
 
1296
    }
 
1297
 
 
1298
    if (g_ascii_strncasecmp(event, "javascript:", 11) == 0) {
 
1299
        event_mediacomplete = g_strdup_printf("%s", event);
 
1300
    } else {
 
1301
        event_mediacomplete = g_strdup_printf("javascript:%s", event);
 
1302
    }
 
1303
}
 
1304
 
 
1305
void CPlugin::SetOnMouseUp(const char *event)
 
1306
{
 
1307
    if (event_mouseup != NULL) {
 
1308
        g_free(event_mouseup);
 
1309
    }
 
1310
 
 
1311
    if (g_ascii_strncasecmp(event, "javascript:", 11) == 0) {
 
1312
        event_mouseup = g_strdup_printf("%s", event);
 
1313
    } else {
 
1314
        event_mouseup = g_strdup_printf("javascript:%s", event);
 
1315
    }
 
1316
}
 
1317
 
 
1318
void CPlugin::SetOnMouseDown(const char *event)
 
1319
{
 
1320
    if (event_mousedown != NULL) {
 
1321
        g_free(event_mousedown);
 
1322
    }
 
1323
 
 
1324
    if (g_ascii_strncasecmp(event, "javascript:", 11) == 0) {
 
1325
        event_mousedown = g_strdup_printf("%s", event);
 
1326
    } else {
 
1327
        event_mousedown = g_strdup_printf("javascript:%s", event);
 
1328
    }
 
1329
}
 
1330
 
 
1331
void CPlugin::SetOnMouseOut(const char *event)
 
1332
{
 
1333
    if (event_leavewindow != NULL) {
 
1334
        g_free(event_leavewindow);
 
1335
    }
 
1336
 
 
1337
    if (g_ascii_strncasecmp(event, "javascript:", 11) == 0) {
 
1338
        event_leavewindow = g_strdup_printf("%s", event);
 
1339
    } else {
 
1340
        event_leavewindow = g_strdup_printf("javascript:%s", event);
 
1341
    }
 
1342
}
 
1343
 
 
1344
void CPlugin::SetOnMouseOver(const char *event)
 
1345
{
 
1346
    if (event_enterwindow != NULL) {
 
1347
        g_free(event_enterwindow);
 
1348
    }
 
1349
 
 
1350
    if (g_ascii_strncasecmp(event, "javascript:", 11) == 0) {
 
1351
        event_enterwindow = g_strdup_printf("%s", event);
 
1352
    } else {
 
1353
        event_enterwindow = g_strdup_printf("javascript:%s", event);
 
1354
    }
 
1355
}
 
1356
 
 
1357
void CPlugin::SetOnDestroy(const char *event)
 
1358
{
 
1359
    if (event_destroy != NULL) {
 
1360
        g_free(event_destroy);
 
1361
    }
 
1362
 
 
1363
    if (g_ascii_strncasecmp(event, "javascript:", 11) == 0) {
 
1364
        event_destroy = g_strdup_printf("%s", event);
 
1365
    } else {
 
1366
        event_destroy = g_strdup_printf("javascript:%s", event);
 
1367
    }
 
1368
}
 
1369
 
 
1370
// ==============================
 
1371
// ! Scriptability related code !
 
1372
// ==============================
 
1373
//
 
1374
// here the plugin is asked by Mozilla to tell if it is scriptable
 
1375
// we should return a valid interface id and a pointer to 
 
1376
// nsScriptablePeer interface which we should have implemented
 
1377
// and which should be defined in the corressponding *.xpt file
 
1378
// in the bin/components folder
 
1379
class ScriptablePluginObjectBase:public NPObject {
 
1380
  public:
 
1381
    ScriptablePluginObjectBase(NPP npp)
 
1382
    :mNpp(npp) {
 
1383
    } virtual ~ ScriptablePluginObjectBase() {
 
1384
    }
 
1385
 
 
1386
    // Virtual NPObject hooks called through this base class. Override
 
1387
    // as you see fit.
 
1388
 
 
1389
    virtual void Invalidate();
 
1390
    virtual bool HasMethod(NPIdentifier name);
 
1391
    virtual bool Invoke(NPIdentifier name, const NPVariant * args,
 
1392
                        uint32_t argCount, NPVariant * result);
 
1393
    virtual bool InvokeDefault(const NPVariant * args, uint32_t argCount, NPVariant * result);
 
1394
    virtual bool HasProperty(NPIdentifier name);
 
1395
    virtual bool GetProperty(NPIdentifier name, NPVariant * result);
 
1396
    virtual bool SetProperty(NPIdentifier name, const NPVariant * value);
 
1397
    virtual bool RemoveProperty(NPIdentifier name);
 
1398
 
 
1399
  public:
 
1400
 
 
1401
    static void _Deallocate(NPObject * npobj);
 
1402
    static void _Invalidate(NPObject * npobj);
 
1403
    static bool _HasMethod(NPObject * npobj, NPIdentifier name);
 
1404
    static bool _Invoke(NPObject * npobj, NPIdentifier name,
 
1405
                        const NPVariant * args, uint32_t argCount, NPVariant * result);
 
1406
    static bool _InvokeDefault(NPObject * npobj, const NPVariant * args,
 
1407
                               uint32_t argCount, NPVariant * result);
 
1408
    static bool _HasProperty(NPObject * npobj, NPIdentifier name);
 
1409
    static bool _GetProperty(NPObject * npobj, NPIdentifier name, NPVariant * result);
 
1410
    static bool _SetProperty(NPObject * npobj, NPIdentifier name, const NPVariant * value);
 
1411
    static bool _RemoveProperty(NPObject * npobj, NPIdentifier name);
 
1412
 
 
1413
  protected:
 
1414
    NPP mNpp;
 
1415
};
 
1416
 
 
1417
#define DECLARE_NPOBJECT_CLASS_WITH_BASE(_class, ctor)                        \
 
1418
static NPClass s##_class##_NPClass = {                                        \
 
1419
  NP_CLASS_STRUCT_VERSION,                                                    \
 
1420
  ctor,                                                                       \
 
1421
  ScriptablePluginObjectBase::_Deallocate,                                    \
 
1422
  ScriptablePluginObjectBase::_Invalidate,                                    \
 
1423
  ScriptablePluginObjectBase::_HasMethod,                                     \
 
1424
  ScriptablePluginObjectBase::_Invoke,                                        \
 
1425
  ScriptablePluginObjectBase::_InvokeDefault,                                 \
 
1426
  ScriptablePluginObjectBase::_HasProperty,                                   \
 
1427
  ScriptablePluginObjectBase::_GetProperty,                                   \
 
1428
  ScriptablePluginObjectBase::_SetProperty,                                   \
 
1429
  ScriptablePluginObjectBase::_RemoveProperty                                 \
 
1430
}
 
1431
 
 
1432
#define GET_NPOBJECT_CLASS(_class) &s##_class##_NPClass
 
1433
 
 
1434
void ScriptablePluginObjectBase::Invalidate()
 
1435
{
 
1436
}
 
1437
 
 
1438
bool ScriptablePluginObjectBase::HasMethod(NPIdentifier name)
 
1439
{
 
1440
    return false;
 
1441
}
 
1442
 
 
1443
bool ScriptablePluginObjectBase::Invoke(NPIdentifier name, const NPVariant * args,
 
1444
                                        uint32_t argCount, NPVariant * result)
 
1445
{
 
1446
    return false;
 
1447
}
 
1448
 
 
1449
bool ScriptablePluginObjectBase::InvokeDefault(const NPVariant * args,
 
1450
                                               uint32_t argCount, NPVariant * result)
 
1451
{
 
1452
    return false;
 
1453
}
 
1454
 
 
1455
bool ScriptablePluginObjectBase::HasProperty(NPIdentifier name)
 
1456
{
 
1457
    return false;
 
1458
}
 
1459
 
 
1460
bool ScriptablePluginObjectBase::GetProperty(NPIdentifier name, NPVariant * result)
 
1461
{
 
1462
 
 
1463
    VOID_TO_NPVARIANT(*result);
 
1464
 
 
1465
    return false;
 
1466
}
 
1467
 
 
1468
bool ScriptablePluginObjectBase::SetProperty(NPIdentifier name, const NPVariant * value)
 
1469
{
 
1470
    return false;
 
1471
}
 
1472
 
 
1473
bool ScriptablePluginObjectBase::RemoveProperty(NPIdentifier name)
 
1474
{
 
1475
    return false;
 
1476
}
 
1477
 
 
1478
// static
 
1479
void ScriptablePluginObjectBase::_Deallocate(NPObject * npobj)
 
1480
{
 
1481
    // Call the virtual destructor.
 
1482
    delete(ScriptablePluginObjectBase *) npobj;
 
1483
}
 
1484
 
 
1485
// static
 
1486
void ScriptablePluginObjectBase::_Invalidate(NPObject * npobj)
 
1487
{
 
1488
    ((ScriptablePluginObjectBase *) npobj)->Invalidate();
 
1489
}
 
1490
 
 
1491
// static
 
1492
bool ScriptablePluginObjectBase::_HasMethod(NPObject * npobj, NPIdentifier name)
 
1493
{
 
1494
    return ((ScriptablePluginObjectBase *) npobj)->HasMethod(name);
 
1495
}
 
1496
 
 
1497
// static
 
1498
bool ScriptablePluginObjectBase::_Invoke(NPObject * npobj, NPIdentifier name,
 
1499
                                         const NPVariant * args, uint32_t argCount,
 
1500
                                         NPVariant * result)
 
1501
{
 
1502
    return ((ScriptablePluginObjectBase *) npobj)->Invoke(name, args, argCount, result);
 
1503
}
 
1504
 
 
1505
// static
 
1506
bool ScriptablePluginObjectBase::_InvokeDefault(NPObject * npobj,
 
1507
                                                const NPVariant * args,
 
1508
                                                uint32_t argCount, NPVariant * result)
 
1509
{
 
1510
    return ((ScriptablePluginObjectBase *) npobj)->InvokeDefault(args, argCount, result);
 
1511
}
 
1512
 
 
1513
// static
 
1514
bool ScriptablePluginObjectBase::_HasProperty(NPObject * npobj, NPIdentifier name)
 
1515
{
 
1516
    return ((ScriptablePluginObjectBase *) npobj)->HasProperty(name);
 
1517
}
 
1518
 
 
1519
// static
 
1520
bool ScriptablePluginObjectBase::_GetProperty(NPObject * npobj, NPIdentifier name,
 
1521
                                              NPVariant * result)
 
1522
{
 
1523
    return ((ScriptablePluginObjectBase *) npobj)->GetProperty(name, result);
 
1524
}
 
1525
 
 
1526
// static
 
1527
bool ScriptablePluginObjectBase::_SetProperty(NPObject * npobj, NPIdentifier name,
 
1528
                                              const NPVariant * value)
 
1529
{
 
1530
    return ((ScriptablePluginObjectBase *) npobj)->SetProperty(name, value);
 
1531
}
 
1532
 
 
1533
// static
 
1534
bool ScriptablePluginObjectBase::_RemoveProperty(NPObject * npobj, NPIdentifier name)
 
1535
{
 
1536
    return ((ScriptablePluginObjectBase *) npobj)->RemoveProperty(name);
 
1537
}
 
1538
 
 
1539
class ScriptablePluginObjectControls:public ScriptablePluginObjectBase {
 
1540
  public:
 
1541
    ScriptablePluginObjectControls(NPP npp)
 
1542
    :ScriptablePluginObjectBase(npp) {
 
1543
    } virtual bool HasMethod(NPIdentifier name);
 
1544
    virtual bool Invoke(NPIdentifier name, const NPVariant * args,
 
1545
                        uint32_t argCount, NPVariant * result);
 
1546
    virtual bool InvokeDefault(const NPVariant * args, uint32_t argCount, NPVariant * result);
 
1547
    virtual bool HasProperty(NPIdentifier name);
 
1548
    virtual bool GetProperty(NPIdentifier name, NPVariant * result);
 
1549
    virtual bool SetProperty(NPIdentifier name, const NPVariant * value);
 
1550
 
 
1551
};
 
1552
 
 
1553
static NPObject *AllocateScriptablePluginObjectControls(NPP npp, NPClass * aClass)
 
1554
{
 
1555
    return new ScriptablePluginObjectControls(npp);
 
1556
}
 
1557
 
 
1558
DECLARE_NPOBJECT_CLASS_WITH_BASE(ScriptablePluginObjectControls,
 
1559
                                 AllocateScriptablePluginObjectControls);
 
1560
 
 
1561
bool ScriptablePluginObjectControls::HasMethod(NPIdentifier name)
 
1562
{
 
1563
    if (name == controls_play_id ||
 
1564
        name == controls_pause_id ||
 
1565
        name == controls_stop_id ||
 
1566
        name == controls_fastForward_id ||
 
1567
        name == controls_fastReverse_id || name == controls_step_id) {
 
1568
        return true;
 
1569
    } else {
 
1570
        return false;
 
1571
    }
 
1572
}
 
1573
 
 
1574
bool ScriptablePluginObjectControls::Invoke(NPIdentifier name, const NPVariant * args,
 
1575
                                            uint32_t argCount, NPVariant * result)
 
1576
{
 
1577
    CPlugin *pPlugin = (CPlugin *) mNpp->pdata;
 
1578
    if (pPlugin == NULL) {
 
1579
        printf("Can't find plugin pointer\n");
 
1580
        return PR_FALSE;
 
1581
    }
 
1582
 
 
1583
    if (name == controls_play_id) {
 
1584
        pPlugin->Play();
 
1585
        return PR_TRUE;
 
1586
    }
 
1587
 
 
1588
    if (name == controls_pause_id) {
 
1589
        pPlugin->Pause();
 
1590
        return PR_TRUE;
 
1591
    }
 
1592
 
 
1593
    if (name == controls_stop_id) {
 
1594
        pPlugin->Stop();
 
1595
        return PR_TRUE;
 
1596
    }
 
1597
 
 
1598
    return false;
 
1599
}
 
1600
 
 
1601
bool ScriptablePluginObjectControls::InvokeDefault(const NPVariant * args, uint32_t argCount,
 
1602
                                                   NPVariant * result)
 
1603
{
 
1604
    printf("ScriptablePluginObject default method called!\n");
 
1605
 
 
1606
    STRINGZ_TO_NPVARIANT(strdup("default method return val"), *result);
 
1607
 
 
1608
    return PR_TRUE;
 
1609
}
 
1610
 
 
1611
bool ScriptablePluginObjectControls::HasProperty(NPIdentifier name)
 
1612
{
 
1613
    if (name == controls_currentPosition_id) {
 
1614
        return true;
 
1615
    } else {
 
1616
        return false;
 
1617
    }
 
1618
}
 
1619
 
 
1620
bool ScriptablePluginObjectControls::GetProperty(NPIdentifier name, NPVariant * result)
 
1621
{
 
1622
    double position;
 
1623
 
 
1624
    CPlugin *pPlugin = (CPlugin *) mNpp->pdata;
 
1625
    if (pPlugin == NULL) {
 
1626
        printf("Can't find plugin pointer\n");
 
1627
        VOID_TO_NPVARIANT(*result);
 
1628
        return false;
 
1629
    }
 
1630
 
 
1631
    if (name == controls_currentPosition_id) {
 
1632
        pPlugin->GetTime(&position);
 
1633
        DOUBLE_TO_NPVARIANT(position, *result);
 
1634
        return true;
 
1635
    }
 
1636
 
 
1637
    VOID_TO_NPVARIANT(*result);
 
1638
    return false;
 
1639
}
 
1640
 
 
1641
bool ScriptablePluginObjectControls::SetProperty(NPIdentifier name, const NPVariant * value)
 
1642
{
 
1643
    CPlugin *pPlugin = (CPlugin *) mNpp->pdata;
 
1644
    if (pPlugin == NULL) {
 
1645
        printf("Can't find plugin pointer\n");
 
1646
        return false;
 
1647
    }
 
1648
 
 
1649
    if (name == controls_currentPosition_id) {
 
1650
        pPlugin->Seek(NPVARIANT_TO_DOUBLE(*value));
 
1651
        return true;
 
1652
    }
 
1653
 
 
1654
    return false;
 
1655
}
 
1656
 
 
1657
 
 
1658
class ScriptablePluginObject:public ScriptablePluginObjectBase {
 
1659
  public:
 
1660
    ScriptablePluginObject(NPP npp)
 
1661
    :ScriptablePluginObjectBase(npp) {
 
1662
    } virtual bool HasMethod(NPIdentifier name);
 
1663
    virtual bool Invoke(NPIdentifier name, const NPVariant * args,
 
1664
                        uint32_t argCount, NPVariant * result);
 
1665
    virtual bool InvokeDefault(const NPVariant * args, uint32_t argCount, NPVariant * result);
 
1666
    virtual bool HasProperty(NPIdentifier name);
 
1667
    virtual bool GetProperty(NPIdentifier name, NPVariant * result);
 
1668
    virtual bool SetProperty(NPIdentifier name, const NPVariant * value);
 
1669
 
 
1670
};
 
1671
 
 
1672
static NPObject *AllocateScriptablePluginObject(NPP npp, NPClass * aClass)
 
1673
{
 
1674
    return new ScriptablePluginObject(npp);
 
1675
}
 
1676
 
 
1677
DECLARE_NPOBJECT_CLASS_WITH_BASE(ScriptablePluginObject, AllocateScriptablePluginObject);
 
1678
 
 
1679
 
 
1680
bool ScriptablePluginObject::HasMethod(NPIdentifier name)
 
1681
{
 
1682
    if (name == Play_id ||
 
1683
        name == PlayAt_id ||
 
1684
        name == Pause_id ||
 
1685
        name == PlayPause_id ||
 
1686
        name == Stop_id ||
 
1687
        name == quit_id ||
 
1688
        name == DoPlay_id ||
 
1689
        name == DoPause_id ||
 
1690
        name == FastForward_id ||
 
1691
        name == FastReverse_id ||
 
1692
        name == ff_id ||
 
1693
        name == rew_id ||
 
1694
        name == rewind_id ||
 
1695
        name == Seek_id ||
 
1696
        name == Open_id ||
 
1697
        name == SetVolume_id ||
 
1698
        name == GetVolume_id ||
 
1699
        name == SetFileName_id ||
 
1700
        name == GetFileName_id ||
 
1701
        name == SetIsLooping_id ||
 
1702
        name == GetIsLooping_id ||
 
1703
        name == SetAutoPlay_id ||
 
1704
        name == GetAutoPlay_id ||
 
1705
        name == SetHREF_id ||
 
1706
        name == GetHREF_id ||
 
1707
        name == SetURL_id ||
 
1708
        name == GetURL_id ||
 
1709
        name == GetMIMEType_id ||
 
1710
        name == getTime_id ||
 
1711
        name == getDuration_id ||
 
1712
        name == getPercent_id ||
 
1713
        name == isplaying_id ||
 
1714
        name == playlistAppend_id ||
 
1715
        name == playlistClear_id ||
 
1716
        name == onClick_id ||
 
1717
        name == onMediaComplete_id ||
 
1718
        name == onMouseUp_id ||
 
1719
        name == onMouseDown_id ||
 
1720
        name == onMouseOut_id || name == onMouseOver_id || name == onDestroy_id) {
 
1721
        return true;
 
1722
    } else {
 
1723
        return false;
 
1724
    }
 
1725
}
 
1726
 
 
1727
bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant * args,
 
1728
                                    uint32_t argCount, NPVariant * result)
 
1729
{
 
1730
    double d;
 
1731
    char *s;
 
1732
    bool b;
 
1733
 
 
1734
    CPlugin *pPlugin = (CPlugin *) mNpp->pdata;
 
1735
    if (pPlugin == NULL) {
 
1736
        printf("Can't find plugin pointer\n");
 
1737
        return PR_FALSE;
 
1738
    }
 
1739
 
 
1740
    if (name == Play_id || name == DoPlay_id) {
 
1741
        pPlugin->Play();
 
1742
        return PR_TRUE;
 
1743
    }
 
1744
 
 
1745
    if (name == PlayAt_id) {
 
1746
        pPlugin->Play();
 
1747
        if ((int)NPVARIANT_TO_DOUBLE(args[0]) == 0 && NPVARIANT_TO_INT32(args[0]) > 0) {
 
1748
            pPlugin->Seek((double)NPVARIANT_TO_INT32(args[0]));
 
1749
        } else {
 
1750
            pPlugin->Seek(NPVARIANT_TO_DOUBLE(args[0]));
 
1751
        }
 
1752
        return PR_TRUE;
 
1753
    }
 
1754
 
 
1755
    if (name == Pause_id || name == DoPause_id) {
 
1756
        pPlugin->Pause();
 
1757
        return PR_TRUE;
 
1758
    }
 
1759
 
 
1760
    if (name == PlayPause_id) {
 
1761
        pPlugin->PlayPause();
 
1762
        return PR_TRUE;
 
1763
    }
 
1764
 
 
1765
    if (name == Stop_id) {
 
1766
        pPlugin->Stop();
 
1767
        return PR_TRUE;
 
1768
    }
 
1769
 
 
1770
    if (name == FastForward_id || name == ff_id) {
 
1771
        pPlugin->FastForward();
 
1772
        return PR_TRUE;
 
1773
    }
 
1774
 
 
1775
    if (name == FastReverse_id || name == rew_id || name == rewind_id) {
 
1776
        pPlugin->FastReverse();
 
1777
        return PR_TRUE;
 
1778
    }
 
1779
 
 
1780
    if (name == Seek_id) {
 
1781
        
 
1782
        if ((int)NPVARIANT_TO_DOUBLE(args[0]) == 0 && NPVARIANT_TO_INT32(args[0]) > 0) {
 
1783
            pPlugin->Seek((double)NPVARIANT_TO_INT32(args[0]));
 
1784
        } else {
 
1785
            pPlugin->Seek(NPVARIANT_TO_DOUBLE(args[0]));
 
1786
        }
 
1787
        return PR_TRUE;
 
1788
    }
 
1789
 
 
1790
    if (name == Open_id || name == SetFileName_id || name == SetHREF_id || name == SetURL_id) {
 
1791
#ifdef HAVE_NEW_XULRUNNER
 
1792
        pPlugin->SetFilename(NPVARIANT_TO_STRING(args[0]).UTF8Characters);
 
1793
#else
 
1794
        pPlugin->SetFilename(NPVARIANT_TO_STRING(args[0]).utf8characters);
 
1795
#endif
 
1796
        return PR_TRUE;
 
1797
    }
 
1798
 
 
1799
    if (name == GetFileName_id || name == GetHREF_id || name == GetURL_id) {
 
1800
        pPlugin->GetFilename(&s);
 
1801
        STRINGZ_TO_NPVARIANT(s, *result);
 
1802
        g_free(s);
 
1803
        return PR_TRUE;
 
1804
    }
 
1805
 
 
1806
    if (name == SetVolume_id) {
 
1807
        pPlugin->SetVolume(NPVARIANT_TO_DOUBLE(args[0]));
 
1808
        return PR_TRUE;
 
1809
    }
 
1810
 
 
1811
    if (name == GetVolume_id) {
 
1812
        pPlugin->GetVolume(&d);
 
1813
        DOUBLE_TO_NPVARIANT(d, *result);
 
1814
        return PR_TRUE;
 
1815
    }
 
1816
 
 
1817
    if (name == SetIsLooping_id) {
 
1818
        pPlugin->SetLoop(NPVARIANT_TO_BOOLEAN(args[0]));
 
1819
        return PR_TRUE;
 
1820
    }
 
1821
 
 
1822
    if (name == GetIsLooping_id) {
 
1823
        pPlugin->GetLoop(&b);
 
1824
        BOOLEAN_TO_NPVARIANT(b, *result);
 
1825
        return PR_TRUE;
 
1826
    }
 
1827
 
 
1828
    if (name == SetAutoPlay_id) {
 
1829
        pPlugin->autostart = NPVARIANT_TO_BOOLEAN(args[0]);
 
1830
        return PR_TRUE;
 
1831
    }
 
1832
 
 
1833
    if (name == GetAutoPlay_id) {
 
1834
        BOOLEAN_TO_NPVARIANT(pPlugin->autostart, *result);
 
1835
        return PR_TRUE;
 
1836
    }
 
1837
 
 
1838
    if (name == GetMIMEType_id) {
 
1839
        pPlugin->GetMIMEType(&s);
 
1840
        STRINGZ_TO_NPVARIANT(s, *result);
 
1841
        g_free(s);
 
1842
        return PR_TRUE;
 
1843
    }
 
1844
 
 
1845
    if (name == getTime_id) {
 
1846
        pPlugin->GetTime(&d);
 
1847
        DOUBLE_TO_NPVARIANT(d, *result);
 
1848
        return PR_TRUE;
 
1849
    }
 
1850
 
 
1851
    if (name == getDuration_id) {
 
1852
        pPlugin->GetDuration(&d);
 
1853
        DOUBLE_TO_NPVARIANT(d, *result);
 
1854
        return PR_TRUE;
 
1855
    }
 
1856
 
 
1857
    if (name == getPercent_id) {
 
1858
        pPlugin->GetPercent(&d);
 
1859
        DOUBLE_TO_NPVARIANT(d, *result);
 
1860
        return PR_TRUE;
 
1861
    }
 
1862
 
 
1863
    if (name == isplaying_id || name == playlistAppend_id || name == playlistClear_id) {
 
1864
        return PR_TRUE;
 
1865
    }
 
1866
 
 
1867
    if (name == onClick_id) {
 
1868
#ifdef HAVE_NEW_XULRUNNER
 
1869
        pPlugin->SetOnClick(NPVARIANT_TO_STRING(args[0]).UTF8Characters);
 
1870
#else
 
1871
        pPlugin->SetOnClick(NPVARIANT_TO_STRING(args[0]).utf8characters);
 
1872
#endif
 
1873
        return PR_TRUE;
 
1874
    }
 
1875
 
 
1876
    if (name == onMediaComplete_id) {
 
1877
#ifdef HAVE_NEW_XULRUNNER
 
1878
        pPlugin->SetOnMediaComplete(NPVARIANT_TO_STRING(args[0]).UTF8Characters);
 
1879
#else
 
1880
        pPlugin->SetOnMediaComplete(NPVARIANT_TO_STRING(args[0]).utf8characters);
 
1881
#endif
 
1882
        return PR_TRUE;
 
1883
    }
 
1884
 
 
1885
    if (name == onMouseUp_id) {
 
1886
#ifdef HAVE_NEW_XULRUNNER
 
1887
        pPlugin->SetOnMouseUp(NPVARIANT_TO_STRING(args[0]).UTF8Characters);
 
1888
#else
 
1889
        pPlugin->SetOnMouseUp(NPVARIANT_TO_STRING(args[0]).utf8characters);
 
1890
#endif
 
1891
        return PR_TRUE;
 
1892
    }
 
1893
 
 
1894
    if (name == onMouseDown_id) {
 
1895
#ifdef HAVE_NEW_XULRUNNER
 
1896
        pPlugin->SetOnMouseDown(NPVARIANT_TO_STRING(args[0]).UTF8Characters);
 
1897
#else
 
1898
        pPlugin->SetOnMouseDown(NPVARIANT_TO_STRING(args[0]).utf8characters);
 
1899
#endif
 
1900
        return PR_TRUE;
 
1901
    }
 
1902
 
 
1903
    if (name == onMouseOut_id) {
 
1904
#ifdef HAVE_NEW_XULRUNNER
 
1905
        pPlugin->SetOnMouseOut(NPVARIANT_TO_STRING(args[0]).UTF8Characters);
 
1906
#else
 
1907
        pPlugin->SetOnMouseOut(NPVARIANT_TO_STRING(args[0]).utf8characters);
 
1908
#endif
 
1909
        return PR_TRUE;
 
1910
    }
 
1911
 
 
1912
    if (name == onMouseOver_id) {
 
1913
#ifdef HAVE_NEW_XULRUNNER
 
1914
        pPlugin->SetOnMouseOver(NPVARIANT_TO_STRING(args[0]).UTF8Characters);
 
1915
#else
 
1916
        pPlugin->SetOnMouseOver(NPVARIANT_TO_STRING(args[0]).utf8characters);
 
1917
#endif
 
1918
        return PR_TRUE;
 
1919
    }
 
1920
 
 
1921
    if (name == onDestroy_id) {
 
1922
#ifdef HAVE_NEW_XULRUNNER
 
1923
        pPlugin->SetOnDestroy(NPVARIANT_TO_STRING(args[0]).UTF8Characters);
 
1924
#else
 
1925
        pPlugin->SetOnDestroy(NPVARIANT_TO_STRING(args[0]).utf8characters);
 
1926
#endif
 
1927
        return PR_TRUE;
 
1928
    }
 
1929
 
 
1930
    return PR_FALSE;
 
1931
}
 
1932
 
 
1933
bool ScriptablePluginObject::InvokeDefault(const NPVariant * args, uint32_t argCount,
 
1934
                                           NPVariant * result)
 
1935
{
 
1936
    printf("ScriptablePluginObject default method called!\n");
 
1937
 
 
1938
    STRINGZ_TO_NPVARIANT(strdup("default method return val"), *result);
 
1939
 
 
1940
    return PR_TRUE;
 
1941
}
 
1942
 
 
1943
bool ScriptablePluginObject::HasProperty(NPIdentifier name)
 
1944
{
 
1945
    if (name == filename_id ||
 
1946
        name == src_id ||
 
1947
        name == ShowControls_id ||
 
1948
        name == fullscreen_id ||
 
1949
        name == showlogo_id || name == playState_id || name == controls_id) {
 
1950
        return true;
 
1951
    } else {
 
1952
        return false;
 
1953
    }
 
1954
}
 
1955
 
 
1956
bool ScriptablePluginObject::GetProperty(NPIdentifier name, NPVariant * result)
 
1957
{
 
1958
    char *filename;
 
1959
    bool setting;
 
1960
    PRInt32 state;
 
1961
 
 
1962
    CPlugin *pPlugin = (CPlugin *) mNpp->pdata;
 
1963
    if (pPlugin == NULL) {
 
1964
        printf("Can't find plugin pointer\n");
 
1965
        VOID_TO_NPVARIANT(*result);
 
1966
        return false;
 
1967
    }
 
1968
 
 
1969
    if (name == filename_id || name == src_id) {
 
1970
        pPlugin->GetFilename(&filename);
 
1971
        if (filename != NULL)
 
1972
            STRINGZ_TO_NPVARIANT(filename, *result);
 
1973
        return true;
 
1974
    }
 
1975
 
 
1976
    if (name == ShowControls_id) {
 
1977
        pPlugin->GetShowControls(&setting);
 
1978
        BOOLEAN_TO_NPVARIANT(setting, *result);
 
1979
        return true;
 
1980
    }
 
1981
 
 
1982
    if (name == fullscreen_id) {
 
1983
        pPlugin->GetFullScreen(&setting);
 
1984
        BOOLEAN_TO_NPVARIANT(setting, *result);
 
1985
        return true;
 
1986
    }
 
1987
 
 
1988
    if (name == showlogo_id) {
 
1989
        setting = true;
 
1990
        BOOLEAN_TO_NPVARIANT(setting, *result);
 
1991
        return true;
 
1992
    }
 
1993
 
 
1994
    if (name == playState_id) {
 
1995
        pPlugin->GetPlayState(&state);
 
1996
        INT32_TO_NPVARIANT(state, *result);
 
1997
        return true;
 
1998
    }
 
1999
 
 
2000
    if (name == controls_id) {
 
2001
        OBJECT_TO_NPVARIANT(pPlugin->GetScriptableObjectControls(), *result);
 
2002
        return true;
 
2003
    }
 
2004
 
 
2005
    VOID_TO_NPVARIANT(*result);
 
2006
    return false;
 
2007
}
 
2008
 
 
2009
bool ScriptablePluginObject::SetProperty(NPIdentifier name, const NPVariant * value)
 
2010
{
 
2011
    CPlugin *pPlugin = (CPlugin *) mNpp->pdata;
 
2012
    if (pPlugin == NULL) {
 
2013
        printf("Can't find plugin pointer\n");
 
2014
        return false;
 
2015
    }
 
2016
 
 
2017
    if (name == filename_id || name == src_id) {
 
2018
#ifdef HAVE_NEW_XULRUNNER
 
2019
        pPlugin->SetFilename(NPVARIANT_TO_STRING(*value).UTF8Characters);
 
2020
#else
 
2021
        pPlugin->SetFilename(NPVARIANT_TO_STRING(*value).utf8characters);
 
2022
#endif
 
2023
        return true;
 
2024
    }
 
2025
 
 
2026
    if (name == ShowControls_id) {
 
2027
        pPlugin->SetShowControls(NPVARIANT_TO_BOOLEAN(*value));
 
2028
        return true;
 
2029
    }
 
2030
 
 
2031
    if (name == fullscreen_id) {
 
2032
        pPlugin->SetFullScreen(NPVARIANT_TO_BOOLEAN(*value));
 
2033
        return true;
 
2034
    }
 
2035
 
 
2036
    if (name == showlogo_id) {
 
2037
        return true;
 
2038
    }
 
2039
 
 
2040
    if (name == playState_id) {
 
2041
        // readonly property
 
2042
        return true;
 
2043
    }
 
2044
 
 
2045
    return false;
 
2046
}
 
2047
 
 
2048
NPObject *CPlugin::GetScriptableObject()
 
2049
{
 
2050
    if (!m_pScriptableObject) {
 
2051
        m_pScriptableObject =
 
2052
            NPN_CreateObject(mInstance, GET_NPOBJECT_CLASS(ScriptablePluginObject));
 
2053
    }
 
2054
 
 
2055
    if (m_pScriptableObject) {
 
2056
        NPN_RetainObject(m_pScriptableObject);
 
2057
    }
 
2058
 
 
2059
    return m_pScriptableObject;
 
2060
}
 
2061
 
 
2062
NPObject *CPlugin::GetScriptableObjectControls()
 
2063
{
 
2064
    if (!m_pScriptableObjectControls) {
 
2065
        m_pScriptableObjectControls =
 
2066
            NPN_CreateObject(mInstance, GET_NPOBJECT_CLASS(ScriptablePluginObjectControls));
 
2067
    }
 
2068
 
 
2069
    if (m_pScriptableObjectControls) {
 
2070
        NPN_RetainObject(m_pScriptableObjectControls);
 
2071
    }
 
2072
 
 
2073
    return m_pScriptableObjectControls;
 
2074
}