home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmocha / lm_supdt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.0 KB  |  231 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /*
  19.  * Sofware Update object
  20.  * This object only exists to pass the data from SoftwareUpdate trigger
  21.  * to softupdate Java class. It has a single property, src, that is read-only
  22.  * It is created as a part of the scope for softupdate
  23.  *
  24.  * Aleks Totic 3/5/97
  25.  *
  26.  */
  27.  
  28. #include "lm.h"
  29.  
  30. /* Private object data */
  31. typedef struct JSSoftup    {
  32.     JSString * src;        /* Name of the JAR file */
  33.     JSBool silent;
  34.     JSBool force;
  35. } JSSoftup;
  36.  
  37. /* Properties */
  38. enum su_slot {
  39.     SU_SRC              = -1,
  40.     SU_SILENT           = -2,
  41.     SU_FORCE            = -3
  42. };
  43.  
  44. static JSPropertySpec su_props[] = {
  45.     { "src",    SU_SRC,    JSPROP_READONLY },
  46.     { "silent",    SU_SILENT,    JSPROP_READONLY },
  47.     { "force",    SU_FORCE,    JSPROP_READONLY },
  48.     {0}
  49. };
  50.  
  51. /* Prototypes */
  52. JSBool PR_CALLBACK
  53. su_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
  54. void PR_CALLBACK
  55. su_finalize(JSContext *cx, JSObject *obj);
  56. JSBool PR_CALLBACK
  57. SoftUpdate(JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval);
  58. static JSBool
  59. su_InitSoftUpClass( MochaDecoder * decoder, JSContext *cx );
  60. char *
  61. DecodeSoftUpJSArgs(const char * jsArgs, JSBool* force, JSBool* silent );
  62.  
  63. extern JSClass lm_softup_class;
  64.  
  65. /*
  66.  * su_getProperty
  67.  * 
  68.  */
  69. JSBool PR_CALLBACK
  70. su_getProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
  71. {
  72.     JSSoftup *softup;
  73.     jsint slot;
  74.  
  75.     if (!JSVAL_IS_INT(id))
  76.         return JS_TRUE;
  77.  
  78.     slot = JSVAL_TO_INT(id);
  79.  
  80.     softup = JS_GetInstancePrivate(cx, obj, &lm_softup_class, NULL);
  81.     XP_ASSERT( softup );
  82.     if (!softup )
  83.         return JS_TRUE;
  84.  
  85.     switch (slot)
  86.     {
  87.     case SU_SRC:
  88.         *vp = STRING_TO_JSVAL(softup->src);
  89.         return JS_TRUE;
  90.     case SU_SILENT:
  91.         *vp = BOOLEAN_TO_JSVAL(softup->silent);
  92.         return JS_TRUE;
  93.     case SU_FORCE:
  94.         *vp = BOOLEAN_TO_JSVAL(softup->force);
  95.         return JS_TRUE;
  96.     default:
  97.         return JS_TRUE;
  98.     }
  99.     return JS_TRUE;
  100. }
  101.  
  102. /*
  103.  * clean up
  104.  */
  105. void PR_CALLBACK
  106. su_finalize(JSContext *cx, JSObject *obj)
  107. {
  108.     JSSoftup *softup;
  109.     softup = JS_GetPrivate(cx, obj);
  110.     if (softup)
  111.     {
  112.         JS_UnlockGCThing( cx, softup->src );
  113.         XP_FREE(softup);
  114.     }
  115. }
  116.  
  117. /* Global, referenced in su_trigger.c to check if the object is the
  118.    correct class */
  119.  
  120. JSClass lm_softup_class = {
  121.     "SoftUpdate", JSCLASS_HAS_PRIVATE,
  122.     JS_PropertyStub, JS_PropertyStub, su_getProperty, JS_PropertyStub,
  123.     JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, su_finalize
  124. };
  125.  
  126. /*
  127.  * Constructor
  128.  * No one should be able to construct one of these, except as a scope object
  129.  * for softupdate.
  130.  */
  131. JSBool PR_CALLBACK
  132. SoftUpdate(JSContext *cx, JSObject *obj,
  133.        uint argc, jsval *argv, jsval *rval)
  134. {
  135.     return JS_TRUE;
  136. }
  137.  
  138. /* 
  139.  * Initialize the class
  140.  */
  141. static JSObject *prototype = NULL;    /* Using this as a class prototype */
  142.                                     /* and a signal if class has been loaded */
  143.  
  144. static JSBool
  145. su_InitSoftUpClass(MochaDecoder * decoder, JSContext *cx)
  146. {
  147.     JSObject *prototype;
  148.     prototype = JS_InitClass(    cx,                 /* JSContext *cx */
  149.                         decoder->window_object,     /* JSObject *obj */
  150.                         NULL, /* decoder->event_receiver_prototype,  JSObject *parent_proto */
  151.                         &lm_softup_class,            /* JSClass *clasp */
  152.                         SoftUpdate,                  /* JSNative constructor */
  153.                         0,                             /* PRUintn nargs */
  154.                         su_props,                     /* JSPropertySpec *ps */
  155.                         0,                             /* JSFunctionSpec *fs */
  156.                         0,                             /* JSPropertySpec *static_ps */
  157.                         0);                            /* JSFunctionSpec *static_fs */
  158.  
  159.     if (!prototype)
  160.         return JS_FALSE;
  161.         
  162.     /* JS_InitStandardClasses makes the "standard" JS functions available 
  163.     if ( !JS_InitStandardClasses(cx, prototype))
  164.         return JS_FALSE;
  165. */    return JS_TRUE;
  166. }
  167.  
  168. /* Decodes the JSArgs */
  169. /* see softupdt.c, EncodeSoftUpJSArgs for info on encoding */
  170. /* jsArgs do not have 'autoinstall:' prefix */
  171. char *
  172. DecodeSoftUpJSArgs(const char *jsArgs, JSBool *force, JSBool *silent )
  173. {
  174.     char * fileName;
  175.     int32 length;
  176.     int32 fileNameLength;
  177.  
  178.     *force = JS_FALSE;
  179.     *silent = JS_FALSE;
  180.     if (jsArgs == NULL)
  181.         return NULL;
  182.     length = XP_STRLEN(jsArgs);
  183.     fileNameLength = length - 4;
  184.     fileName = XP_ALLOC(fileNameLength +1);
  185.     XP_MEMCPY(fileName, jsArgs, fileNameLength);
  186.     fileName[fileNameLength] = 0;
  187.     *force = (JSBool)(jsArgs[length - 1] == 'T');
  188.     *silent = (JSBool)(jsArgs[length - 3] == 'T');
  189.     return fileName;
  190. }
  191.  
  192. /*
  193.  * Creates a new object
  194.  * Arguments: jarargs jar: followed by name of the JAR file
  195.  */
  196.  
  197. JSObject *
  198. lm_NewSoftupObject( JSContext *cx, MochaDecoder *decoder, const char *jarargs )
  199. {
  200.     JSObject * obj;
  201.     if (prototype == NULL)
  202.     {
  203.         if ( !su_InitSoftUpClass(decoder, cx ))
  204.             return NULL;
  205.     }
  206.  
  207.     obj = JS_NewObject(cx, &lm_softup_class, prototype, decoder->window_object);
  208.     if ( obj )
  209.     {
  210.         JSSoftup * su;
  211.         su = XP_ALLOC (sizeof (JSSoftup));
  212.         if ( su )
  213.         {
  214.             char * fileName;
  215.             JSBool silent;
  216.             JSBool force;
  217.             fileName = DecodeSoftUpJSArgs( jarargs, &force, &silent);
  218.             su->src = JS_NewStringCopyZ(cx, fileName);
  219.             if ( fileName )
  220.                 XP_FREE(fileName);
  221.             su->force = force;
  222.             su->silent = silent;
  223.             JS_LockGCThing( cx, su->src );
  224.             JS_SetPrivate( cx, obj, su );
  225.         }
  226.     }
  227.  
  228.     return obj;
  229. }
  230.  
  231.