home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmocha / lm_trggr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.8 KB  |  278 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.  * lm_trggr.c
  20.  * 
  21.  * Reflects AutoInstall trigger methods
  22.  * into the global JavaScript config object.
  23.  *
  24.  * See Trigger.java for related functions.
  25.  */
  26.  
  27. #include "lm.h"
  28. #include "prefapi.h"
  29. #include "VerReg.h"
  30. #include "softupdt.h"
  31. #include "gui.h"    /* XP_AppPlatform */
  32.  
  33. JSBool PR_CALLBACK asd_Version(JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval);
  34. JSBool PR_CALLBACK asd_get_version(JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval);
  35. JSBool PR_CALLBACK asd_start_update(JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval);
  36. JSBool PR_CALLBACK asd_conditional_update(JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval);
  37. JSBool PR_CALLBACK asd_alert(JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval);
  38. JSBool PR_CALLBACK asd_platform(JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval);
  39.  
  40. PRIVATE JSFunctionSpec autoinstall_methods[] = {
  41.     { "getVersion",    asd_get_version,    2 },
  42.     { "startUpdate",    asd_start_update,    2 },
  43.     { "conditionalUpdate",    asd_conditional_update, 4 },
  44.     { "startUpdateComponent",    asd_conditional_update,    4 },    /* old name */
  45.     { NULL,    NULL,    0 }
  46. };
  47.  
  48. PRIVATE JSFunctionSpec globalconfig_methods[] = {
  49.     { "alert",    asd_alert,    1 },
  50.     { "getPlatform", asd_platform, 0 },
  51.     { NULL,    NULL,    0 }
  52. };
  53.  
  54. PRIVATE JSClass autoinstall_class = {
  55.     "AutoInstall", 0,
  56.     JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
  57.     JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
  58. };
  59.  
  60. PRIVATE JSClass version_class = {
  61.     "VersionInfo", 0,
  62.     JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
  63.     JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
  64. };
  65.  
  66. JSBool lm_DefineTriggers()
  67. {
  68.     JSContext    *globalContext = NULL;
  69.     JSObject    *globalObject = NULL;
  70.     JSObject    *autoInstallObject, *versionObject;
  71.  
  72.     /* get global mocha context and object */
  73.     PREF_GetConfigContext(&globalContext);
  74.     PREF_GetGlobalConfigObject(&globalObject);
  75.  
  76.     if (globalContext == NULL || globalObject == NULL)    {
  77.         return JS_FALSE;
  78.     }
  79.  
  80.     /* define AutoInstall object in global object */
  81.     autoInstallObject = JS_DefineObject(globalContext, globalObject, 
  82.                         "AutoInstall",
  83.                         &autoinstall_class, 
  84.                         NULL, 
  85.                         JSPROP_ENUMERATE|JSPROP_READONLY);
  86.                         
  87.     if (!autoInstallObject)
  88.         return JS_FALSE;
  89.     
  90.     /* define Version class in AutoInstall */
  91.     versionObject = JS_InitClass(globalContext, autoInstallObject, NULL,
  92.                         &version_class, asd_Version, 0, NULL, NULL, NULL, NULL);
  93.     
  94.     if (!versionObject)
  95.         return JS_FALSE;
  96.     
  97.     /* define some global config utility functions */
  98.     JS_DefineFunctions(globalContext, globalObject, globalconfig_methods);
  99.     
  100.     return JS_DefineFunctions(globalContext, autoInstallObject, autoinstall_methods);
  101. }
  102.  
  103. /* Convert VERSION type to Version JS object */
  104. static void asd_versToObj(JSContext *cx, VERSION* vers, JSObject* versObj)
  105. {
  106.     jsval val = INT_TO_JSVAL(vers->major);
  107.     JS_SetProperty(cx, versObj, "major", &val);
  108.     val = INT_TO_JSVAL(vers->minor);
  109.     JS_SetProperty(cx, versObj, "minor", &val);
  110.     val = INT_TO_JSVAL(vers->release);
  111.     JS_SetProperty(cx, versObj, "release", &val);
  112.     val = INT_TO_JSVAL(vers->build);
  113.     JS_SetProperty(cx, versObj, "build", &val);
  114. }
  115.  
  116. /* Convert Version JS object to VERSION type */
  117. static void asd_objToVers(JSContext *cx, JSObject* versObj, VERSION* vers)
  118. {
  119.     jsval val;
  120.     JS_GetProperty(cx, versObj, "major", &val);
  121.     vers->major = JSVAL_TO_INT(val);
  122.     JS_GetProperty(cx, versObj, "minor", &val);
  123.     vers->minor = JSVAL_TO_INT(val);
  124.     JS_GetProperty(cx, versObj, "release", &val);
  125.     vers->release = JSVAL_TO_INT(val);
  126.     JS_GetProperty(cx, versObj, "build", &val);
  127.     vers->build = JSVAL_TO_INT(val);
  128. }
  129.  
  130. /*
  131.  * ?? -- move this to VR?
  132.  * Returns 0 if versions are equal; < 0 if vers2 is newer; > 0 if vers1 is newer
  133.  */
  134. static int asd_compareVersion(VERSION* vers1, VERSION* vers2)
  135. {
  136.     int diff;
  137.     if (vers1 == NULL)
  138.         diff = -4;
  139.     else if (vers2 == NULL)
  140.         diff = 4;
  141.     else if (vers1->major == vers2->major) {
  142.         if (vers1->minor == vers2->minor) {
  143.             if (vers1->release == vers2->release) {
  144.                 if (vers1->build == vers2->build) 
  145.                     diff = 0;
  146.                 else diff = (vers1->build > vers2->build) ? 1 : -1;
  147.             }
  148.             else diff = (vers1->release > vers2->release) ? 2 : -2;
  149.         }
  150.         else diff = (vers1->minor > vers2->minor) ? 3 : -3;
  151.     }
  152.     else diff = (vers1->major > vers2->major) ? 4 : -4;
  153.     return diff;
  154. }
  155.  
  156. /* Version constructor
  157.    (accepts up to four int params to initialize fields) */
  158. JSBool PR_CALLBACK asd_Version
  159.     (JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval)
  160. {
  161.     VERSION vers;
  162.     vers.major = (argc >= 1 && JSVAL_IS_INT(argv[0])) ? JSVAL_TO_INT(argv[0]) : 0;
  163.     vers.minor = (argc >= 2 && JSVAL_IS_INT(argv[1])) ? JSVAL_TO_INT(argv[1]) : 0;
  164.     vers.release = (argc >= 3 && JSVAL_IS_INT(argv[2])) ? JSVAL_TO_INT(argv[2]) : 0;
  165.     vers.build = (argc >= 4 && JSVAL_IS_INT(argv[3])) ? JSVAL_TO_INT(argv[3]) : 0;
  166.  
  167.     asd_versToObj(cx, &vers, obj);
  168.     return JS_TRUE;
  169. }
  170.  
  171. /* arguments:
  172.    0. component name
  173.    1. version no. to fill in
  174.    return:
  175.       error status */
  176. JSBool PR_CALLBACK asd_get_version
  177.     (JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval)
  178. {
  179.     REGERR status = 0;
  180.     if (argc >= 2 && JSVAL_IS_STRING(argv[0])
  181.         && JSVAL_IS_OBJECT(argv[1]))
  182.     {        
  183.         VERSION vers;
  184.         char* component_path = JS_GetStringBytes(JSVAL_TO_STRING(argv[0]));
  185.         status = VR_GetVersion(component_path, &vers);
  186.  
  187.         if (status == REGERR_OK) {
  188.             JSObject* versObj = JSVAL_TO_OBJECT(argv[1]);
  189.             asd_versToObj(cx, &vers, versObj);
  190.         }
  191.     }
  192.     *rval = INT_TO_JSVAL(status);
  193.     return JS_TRUE;
  194. }
  195.  
  196. /* arguments:
  197.    0. url
  198.    1. flags
  199.    return:
  200.       true if no errors */
  201. JSBool PR_CALLBACK asd_start_update
  202.     (JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval)
  203. {
  204.     if (argc >= 2 && JSVAL_IS_STRING(argv[0]) &&
  205.         JSVAL_IS_INT(argv[1])) {
  206.         char* url = JS_GetStringBytes(JSVAL_TO_STRING(argv[0]));
  207.         /* Bookmarks is a hack, you should really get some SmartUpdate context */
  208.         MWContext* cx = XP_FindContextOfType(NULL, MWContextBookmarks);
  209.         
  210.         XP_Bool result = SU_StartSoftwareUpdate( cx, url,
  211.             NULL, NULL, NULL, JSVAL_TO_INT(argv[1]) );
  212.         *rval = BOOLEAN_TO_JSVAL(result);
  213.         return JS_TRUE;
  214.     }
  215.     *rval = BOOLEAN_TO_JSVAL(JS_FALSE);
  216.     return JS_TRUE;
  217. }
  218.  
  219. /* arguments:
  220.    0. url
  221.    1. component name
  222.    2. version no. to compare
  223.    3. flags
  224.    return:
  225.       true if update triggered and no errors */
  226. JSBool PR_CALLBACK asd_conditional_update
  227.     (JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval)
  228. {
  229.     REGERR status = 0;
  230.     if (argc >= 4 && JSVAL_IS_STRING(argv[0]) &&
  231.         JSVAL_IS_STRING(argv[1]) &&
  232.         JSVAL_IS_OBJECT(argv[2]) &&
  233.         JSVAL_IS_INT(argv[3]))
  234.     {
  235.         VERSION curr_vers;
  236.         char* component_path = JS_GetStringBytes(JSVAL_TO_STRING(argv[1]));
  237.         status = VR_GetVersion(component_path, &curr_vers);
  238.  
  239.         if (status == REGERR_OK) {
  240.             JSObject* versObj = JSVAL_TO_OBJECT(argv[2]);
  241.             VERSION req_vers;
  242.             asd_objToVers(cx, versObj, &req_vers);
  243.             if ( asd_compareVersion(&req_vers, &curr_vers) > 0 ) {
  244.                 char* url = JS_GetStringBytes(JSVAL_TO_STRING(argv[0]));
  245.                 MWContext* cx = XP_FindContextOfType(NULL, MWContextBookmarks);
  246.  
  247.                 XP_Bool result = SU_StartSoftwareUpdate( cx, url,
  248.                     NULL, NULL, NULL, JSVAL_TO_INT(argv[3]) );
  249.                 *rval = BOOLEAN_TO_JSVAL(result);
  250.                 return JS_TRUE;
  251.             }
  252.         }
  253.     }
  254.     *rval = BOOLEAN_TO_JSVAL(JS_FALSE);    /*INT_TO_JSVAL(status);*/
  255.     return JS_TRUE;
  256. }
  257.  
  258. JSBool PR_CALLBACK asd_alert
  259.     (JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval)
  260. {
  261.     if (argc >= 1 && JSVAL_IS_STRING(argv[0])) {
  262.         char* msg = JS_GetStringBytes(JSVAL_TO_STRING(argv[0]));
  263.         MWContext* ctx = XP_FindSomeContext();
  264.         
  265.         if (ctx)
  266.             FE_Alert(ctx, msg);
  267.     }
  268.     return JS_TRUE;
  269. }
  270.  
  271. JSBool PR_CALLBACK asd_platform
  272.     (JSContext *cx, JSObject *obj, uint argc, jsval *argv, jsval *rval)
  273. {
  274.     *rval = STRING_TO_JSVAL( JS_NewStringCopyZ(cx, XP_AppPlatform) );
  275.     return JS_TRUE;
  276. }
  277.  
  278.