diff --git a/xpinstall/src/nsInstall.cpp b/xpinstall/src/nsInstall.cpp index a28bf9afdf2..56683b774f3 100644 --- a/xpinstall/src/nsInstall.cpp +++ b/xpinstall/src/nsInstall.cpp @@ -1973,6 +1973,63 @@ nsInstall::FileOpFileRename(nsInstallFolder& aSrc, nsString& aTarget, PRInt32* a return NS_OK; } +#ifdef _WINDOWS +#include +#endif + + +PRInt32 +nsInstall::FileOpFileWindowsGetShortName(nsInstallFolder& aTarget, nsString& aShortPathName) +{ +#ifdef _WINDOWS + + PRInt32 err; + PRBool flagExists; + nsString tmpNsString; + nsXPIDLCString nativeTargetPath; + char nativeShortPathName[MAX_PATH]; + nsCOMPtr localTarget(aTarget.GetFileSpec()); + + if(localTarget == nsnull) + return NS_OK; + + localTarget->Exists(&flagExists); + if(flagExists) + { + memset(nativeShortPathName, 0, MAX_PATH); + localTarget->GetPath(getter_Copies(nativeTargetPath)); + + err = GetShortPathName(nativeTargetPath, nativeShortPathName, MAX_PATH); + if((err > 0) && (*nativeShortPathName == '\0')) + { + // NativeShortPathName buffer not big enough. + // Reallocate and try again. + // err will have the required size. + char *nativeShortPathNameTmp = new char[err + 1]; + if(nativeShortPathNameTmp == nsnull) + return NS_OK; + + err = GetShortPathName(nativeTargetPath, nativeShortPathNameTmp, err + 1); + // Is it safe to assume that the second time around the buffer is big enough + // and not to worry about it unless it's a different problem? + + // if err is 0, it's not a buffer size problem. It's something else unexpected. + if(err != 0) + aShortPathName.AssignWithConversion(nativeShortPathNameTmp); + + if(nativeShortPathNameTmp) + delete [] nativeShortPathNameTmp; + } + else if(err != 0) + // if err is 0, it's not a buffer size problem. It's something else unexpected. + aShortPathName.AssignWithConversion(nativeShortPathName); + } + +#endif + + return NS_OK; +} + PRInt32 nsInstall::FileOpFileWindowsShortcut(nsIFile* aTarget, nsIFile* aShortcutPath, nsString& aDescription, nsIFile* aWorkingPath, nsString& aParams, nsIFile* aIcon, PRInt32 aIconId, PRInt32* aReturn) { diff --git a/xpinstall/src/nsInstall.h b/xpinstall/src/nsInstall.h index 5820c437475..9e4b02676c6 100644 --- a/xpinstall/src/nsInstall.h +++ b/xpinstall/src/nsInstall.h @@ -252,6 +252,7 @@ class nsInstall PRInt32 FileOpFileModDateChanged(nsInstallFolder& aTarget, double aOldStamp, PRBool* aReturn); PRInt32 FileOpFileMove(nsInstallFolder& aSrc, nsInstallFolder& aTarget, PRInt32* aReturn); PRInt32 FileOpFileRename(nsInstallFolder& aSrc, nsString& aTarget, PRInt32* aReturn); + PRInt32 FileOpFileWindowsGetShortName(nsInstallFolder& aTarget, nsString& aShortPathName); PRInt32 FileOpFileWindowsShortcut(nsIFile* aTarget, nsIFile* aShortcutPath, nsString& aDescription, nsIFile* aWorkingPath, nsString& aParams, nsIFile* aIcon, PRInt32 aIconId, PRInt32* aReturn); PRInt32 FileOpFileMacAlias(nsIFile *aSourceFile, nsIFile *aAliasFile, PRInt32* aReturn); PRInt32 FileOpFileUnixLink(nsInstallFolder& aTarget, PRInt32 aFlags, PRInt32* aReturn); diff --git a/xpinstall/src/nsJSFile.cpp b/xpinstall/src/nsJSFile.cpp index 9ede072e9af..ac9697d6716 100644 --- a/xpinstall/src/nsJSFile.cpp +++ b/xpinstall/src/nsJSFile.cpp @@ -991,6 +991,54 @@ InstallFileOpFileRename(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, j return JS_TRUE; } +// +// Native method FileWindowsGetShortName +// +JSBool PR_CALLBACK +InstallFileOpFileWindowsGetShortName(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsAutoString shortPathName; + nsInstall* nativeThis = (nsInstall*)JS_GetPrivate(cx, obj); + JSObject *jsObj; + nsInstallFolder *longPathName; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if(nsnull == nativeThis) + { + return JS_TRUE; + } + + // public String windowsGetShortName (File NativeFolderPath); + + if ( argc == 0 || argv[0] == JSVAL_NULL || !JSVAL_IS_OBJECT(argv[0])) //argv[0] MUST be a jsval + { + // error, return NULL + return JS_TRUE; + } + + jsObj = JSVAL_TO_OBJECT(argv[0]); + + if (!JS_InstanceOf(cx, jsObj, &FileSpecObjectClass, nsnull)) + { + // error, return NULL + return JS_TRUE; + } + + longPathName = (nsInstallFolder*)JS_GetPrivate(cx, jsObj); + + if(!longPathName || NS_OK != nativeThis->FileOpFileWindowsGetShortName(*longPathName, shortPathName)) + { + return JS_TRUE; + } + + if(shortPathName.Length() != 0) + *rval = STRING_TO_JSVAL(JS_NewUCStringCopyN(cx, NS_STATIC_CAST(const jschar*, shortPathName.GetUnicode()), shortPathName.Length())); + + return JS_TRUE; +} + // // Native method FileWindowsShortcut // @@ -1362,6 +1410,7 @@ static JSFunctionSpec FileOpMethods[] = {"modDateChanged", InstallFileOpFileModDateChanged, 2}, {"move", InstallFileOpFileMove, 2}, {"rename", InstallFileOpFileRename, 2}, + {"windowsGetShortName", InstallFileOpFileWindowsGetShortName, 1}, {"windowsShortcut", InstallFileOpFileWindowsShortcut, 7}, {"macAlias", InstallFileOpFileMacAlias, 2}, {"unixLink", InstallFileOpFileUnixLink, 2},