Back to index

lightning-sunbird  0.9+nobinonly
nsJSFileSpecObj.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org Code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "jsapi.h"
00040 #include "nscore.h"
00041 #include "nsIScriptContext.h"
00042 
00043 #include "nsString.h"
00044 #include "nsInstall.h"
00045 #include "nsJSFileSpecObj.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 
00061 static void PR_CALLBACK
00062 FileSpecObjectCleanup(JSContext *cx, JSObject *obj);
00063 
00064 /***********************************************************************/
00065 //
00066 // class for FileObj
00067 //
00068 JSClass FileSpecObjectClass = {
00069   "FileSpecObject",
00070   JSCLASS_HAS_PRIVATE,
00071   JS_PropertyStub,
00072   JS_PropertyStub,
00073   JS_PropertyStub,
00074   JS_PropertyStub,
00075   JS_EnumerateStub,
00076   JS_ResolveStub,
00077   JS_ConvertStub,
00078   FileSpecObjectCleanup
00079 };
00080 
00081 
00082 /***********************************************************************************/
00083 // Native methods for FileSpecObj functions
00084 
00085 /*
00086  * Native method fso_ToString
00087  */
00088 PR_STATIC_CALLBACK(JSBool)
00089 fso_ToString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00090 {
00091   nsInstallFolder *nativeThis =
00092     (nsInstallFolder*)JS_GetInstancePrivate(cx, obj, &FileSpecObjectClass,
00093                                             argv);
00094   if (!nativeThis)
00095     return JS_FALSE;
00096 
00097   nsAutoString stringReturned;
00098 
00099   *rval = JSVAL_NULL;
00100 
00101   if(NS_FAILED( nativeThis->ToString(&stringReturned)))
00102     return JS_TRUE;
00103 
00104 
00105   JSString *jsstring =
00106     JS_NewUCStringCopyN(cx, NS_REINTERPRET_CAST(const jschar*,
00107                                                 stringReturned.get()),
00108                         stringReturned.Length());
00109 
00110   // set the return value
00111   *rval = STRING_TO_JSVAL(jsstring);
00112 
00113   return JS_TRUE;
00114 }
00115 
00116 
00117 /*
00118  * Native method fso_AppendString
00119  */
00120 PR_STATIC_CALLBACK(JSBool)
00121 fso_AppendPath(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
00122 {
00123   return JS_TRUE;
00124 }
00125 
00126 
00127 /*
00128  * FileSpecObj destructor
00129  */
00130 static void PR_CALLBACK FileSpecObjectCleanup(JSContext *cx, JSObject *obj)
00131 {
00132   nsInstallFolder *nativeThis = (nsInstallFolder*)JS_GetPrivate(cx, obj);
00133   if (nativeThis != nsnull)
00134     delete nativeThis;
00135 }
00136 
00137 //
00138 // FileObj class methods
00139 //
00140 static JSFunctionSpec fileSpecObjMethods[] = 
00141 {
00142   {"appendPath",       fso_AppendPath,     1},
00143   {"toString",         fso_ToString,       0},
00144   {0}
00145 };
00146 
00147 
00148 PRInt32  InitFileSpecObjectPrototype(JSContext *jscontext, 
00149                                       JSObject *global, 
00150                                       JSObject **fileSpecObjectPrototype)
00151 {
00152   *fileSpecObjectPrototype  = JS_InitClass( jscontext,         // context
00153                                             global,            // global object
00154                                             nsnull,            // parent proto 
00155                                             &FileSpecObjectClass, // JSClass
00156                                             nsnull,            // JSNative ctor
00157                                             0,                 // ctor args
00158                                             nsnull,            // proto props
00159                                             fileSpecObjMethods,// proto funcs
00160                                             nsnull,            // ctor props (static)
00161                                             nsnull);           // ctor funcs (static)
00162 
00163   if (nsnull == *fileSpecObjectPrototype) 
00164   {
00165       return NS_ERROR_FAILURE;
00166   }
00167 
00168  
00169   return NS_OK;
00170 }
00171 
00172