Back to index

lightning-sunbird  0.9+nobinonly
nsJSWinProfile.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "jsapi.h"
00039 #include "nscore.h"
00040 #include "nsIScriptContext.h"
00041 
00042 #include "nsString.h"
00043 #include "nsInstall.h"
00044 #include "nsWinProfile.h"
00045 #include "nsJSWinProfile.h"
00046 
00047 extern void ConvertJSValToStr(nsString&  aString,
00048                              JSContext* aContext,
00049                              jsval      aValue);
00050 
00051 extern void ConvertStrToJSVal(const nsString& aProp,
00052                              JSContext* aContext,
00053                              jsval* aReturn);
00054 
00055 extern PRBool ConvertJSValToBool(PRBool* aProp,
00056                                 JSContext* aContext,
00057                                 jsval aValue);
00058 
00059 
00060 static void PR_CALLBACK
00061 WinProfileCleanup(JSContext *cx, JSObject *obj);
00062 
00063 /***********************************************************************/
00064 //
00065 // class for WinProfile
00066 //
00067 JSClass WinProfileClass = {
00068   "WinProfile",
00069   JSCLASS_HAS_PRIVATE,
00070   JS_PropertyStub,
00071   JS_PropertyStub,
00072   JS_PropertyStub,
00073   JS_PropertyStub,
00074   JS_EnumerateStub,
00075   JS_ResolveStub,
00076   JS_ConvertStub,
00077   WinProfileCleanup
00078 };
00079 
00080 
00081 static void PR_CALLBACK WinProfileCleanup(JSContext *cx, JSObject *obj)
00082 {
00083     nsWinProfile *nativeThis = (nsWinProfile*)JS_GetPrivate(cx, obj);
00084     delete nativeThis;
00085 }
00086 
00087 /***********************************************************************************/
00088 // Native mothods for WinProfile functions
00089 
00090 //
00091 // Native method GetString
00092 //
00093 PR_STATIC_CALLBACK(JSBool)
00094 WinProfileGetString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00095 {
00096   nsWinProfile *nativeThis =
00097     (nsWinProfile*)JS_GetInstancePrivate(cx, obj, &WinProfileClass, argv);
00098   if (!nativeThis)
00099     return JS_FALSE;
00100 
00101   nsString     nativeRet;
00102   nsAutoString b0;
00103   nsAutoString b1;
00104 
00105   *rval = JSVAL_NULL;
00106 
00107   if(argc >= 2)                             
00108   {
00109     //  public string getString ( String section,
00110     //                         String key);
00111 
00112     ConvertJSValToStr(b0, cx, argv[0]);
00113     ConvertJSValToStr(b1, cx, argv[1]);
00114 
00115     nativeThis->GetString(b0, b1, &nativeRet);
00116 
00117     ConvertStrToJSVal(nativeRet, cx, rval);
00118   }
00119   else
00120   {
00121     JS_ReportWarning(cx, "WinProfile.getString() parameters error");
00122   }
00123 
00124   return JS_TRUE;
00125 }
00126 
00127 
00128 //
00129 // Native method WriteString
00130 //
00131 PR_STATIC_CALLBACK(JSBool)
00132 WinProfileWriteString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00133 {
00134   nsWinProfile *nativeThis =
00135     (nsWinProfile*)JS_GetInstancePrivate(cx, obj, &WinProfileClass, argv);
00136   if (!nativeThis)
00137     return JS_FALSE;
00138 
00139   PRInt32 nativeRet;
00140   nsAutoString b0;
00141   nsAutoString b1;
00142   nsAutoString b2;
00143 
00144   *rval = JSVAL_ZERO;
00145 
00146   if(argc >= 3)
00147   {
00148     //  public int writeString ( String section,
00149     //                           String key,
00150     //                           String value);
00151 
00152     ConvertJSValToStr(b0, cx, argv[0]);
00153     ConvertJSValToStr(b1, cx, argv[1]);
00154     ConvertJSValToStr(b2, cx, argv[2]);
00155 
00156     if(NS_OK == nativeThis->WriteString(b0, b1, b2, &nativeRet))
00157     {
00158       *rval = INT_TO_JSVAL(nativeRet);
00159     }
00160   }
00161   else
00162   {
00163     JS_ReportWarning(cx, "WinProfile.writeString() parameters error");
00164   }
00165 
00166   return JS_TRUE;
00167 }
00168 
00169 //
00170 // WinProfile constructor
00171 //
00172 PR_STATIC_CALLBACK(JSBool)
00173 WinProfile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00174 {
00175   return JS_FALSE;
00176 }
00177 
00178 static JSConstDoubleSpec winprofile_constants[] = 
00179 {
00180     {0}
00181 };
00182 
00183 //
00184 // WinProfile class methods
00185 //
00186 static JSFunctionSpec WinProfileMethods[] = 
00187 {
00188   {"getString",                  WinProfileGetString,                 2},
00189   {"writeString",                WinProfileWriteString,               3},
00190   {0}
00191 };
00192 
00193 PRInt32
00194 InitWinProfilePrototype(JSContext *jscontext, JSObject *global, JSObject **winProfilePrototype)
00195 {
00196   *winProfilePrototype = JS_InitClass( jscontext,          // context
00197                                        global,             // global object
00198                                        nsnull,             // parent proto 
00199                                        &WinProfileClass,   // JSClass
00200                                        nsnull,             // JSNative ctor
00201                                        0,                  // ctor args
00202                                        nsnull,             // proto props
00203                                        nsnull,             // proto funcs
00204                                        nsnull,             // ctor props (static)
00205                                        WinProfileMethods); // ctor funcs (static)
00206 
00207   if(nsnull == *winProfilePrototype) 
00208   {
00209     return NS_ERROR_FAILURE;
00210   }
00211 
00212   if(PR_FALSE == JS_DefineConstDoubles(jscontext, *winProfilePrototype, winprofile_constants))
00213     return NS_ERROR_FAILURE;
00214 
00215   return NS_OK;
00216 }
00217