/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* * The contents of this file are subject to the Netscape Public License * Version 1.0 (the "NPL"); you may not use this file except in * compliance with the NPL. You may obtain a copy of the NPL at * http://www.mozilla.org/NPL/ * * Software distributed under the NPL is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL * for the specific language governing rights and limitations under the * NPL. * * The Initial Developer of this code under the NPL is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ #include "nsWinRegItem.h" #include "nspr.h" #ifdef WIN32 #include /* is this needed? */ #endif /* Public Methods */ nsWinRegItem::nsWinRegItem(nsWinReg* regObj, PRInt32 root, PRInt32 action, const nsString& sub, const nsString& valname, const nsString& val, PRInt32 *aReturn) : nsInstallObject(regObj->InstallObject()) { mReg = regObj; mCommand = action; mRootkey = root; *aReturn = nsInstall::SUCCESS; /* I'm assuming we need to copy these */ mSubkey = new nsString(sub); mName = new nsString(valname); mValue = new nsString(val); if((mSubkey == nsnull) || (mName == nsnull) || (mValue == nsnull)) { *aReturn = nsInstall::OUT_OF_MEMORY; } } nsWinRegItem::nsWinRegItem(nsWinReg* regObj, PRInt32 root, PRInt32 action, const nsString& sub, const nsString& valname, PRInt32 val, PRInt32 *aReturn) : nsInstallObject(regObj->InstallObject()) { mReg = regObj; mCommand = action; mRootkey = root; *aReturn = nsInstall::SUCCESS; /* I'm assuming we need to copy these */ mSubkey = new nsString(sub); mName = new nsString(valname); mValue = new PRInt32(val); if((mSubkey == nsnull) || (mName == nsnull) || (mValue == nsnull)) { *aReturn = nsInstall::OUT_OF_MEMORY; } } nsWinRegItem::~nsWinRegItem() { if (mSubkey) delete mSubkey; if (mName) delete mName; if (mValue) delete mValue; } PRInt32 nsWinRegItem::Complete() { PRInt32 aReturn = NS_OK; if (mReg == nsnull) return nsInstall::OUT_OF_MEMORY; switch (mCommand) { case NS_WIN_REG_CREATE: mReg->FinalCreateKey(mRootkey, *mSubkey, *mName, &aReturn); break; case NS_WIN_REG_DELETE: mReg->FinalDeleteKey(mRootkey, *mSubkey, &aReturn); break; case NS_WIN_REG_DELETE_VAL: mReg->FinalDeleteValue(mRootkey, *mSubkey, *mName, &aReturn); break; case NS_WIN_REG_SET_VAL_STRING: mReg->FinalSetValueString(mRootkey, *mSubkey, *mName, *(nsString*)mValue, &aReturn); break; case NS_WIN_REG_SET_VAL_NUMBER: mReg->FinalSetValueNumber(mRootkey, *mSubkey, *mName, *(PRInt32*)mValue, &aReturn); break; case NS_WIN_REG_SET_VAL: mReg->FinalSetValue(mRootkey, *mSubkey, *mName, (nsWinRegValue*)mValue, &aReturn); break; } return aReturn; } #define kCRK "Create Registry Key " #define kDRK "Delete Registry Key " #define kDRV "Delete Registry Value " #define kSRVS "Store Registry Value String " #define kSRVN "Store Registry Value Number " #define kSRV "Store Registry Value " #define kUNK "Unknown " char* nsWinRegItem::toString() { nsString* keyString = nsnull; nsString* result = nsnull; char* resultCString = nsnull; switch(mCommand) { case NS_WIN_REG_CREATE: keyString = keystr(mRootkey, mSubkey, nsnull); result = new nsString(kCRK); break; case NS_WIN_REG_DELETE: keyString = keystr(mRootkey, mSubkey, nsnull); result = new nsString(kDRK); break; case NS_WIN_REG_DELETE_VAL: keyString = keystr(mRootkey, mSubkey, mName); result = new nsString(kDRV); break; case NS_WIN_REG_SET_VAL_STRING: keyString = keystr(mRootkey, mSubkey, mName); result = new nsString(kSRVS); break; case NS_WIN_REG_SET_VAL_NUMBER: keyString = keystr(mRootkey, mSubkey, mName); result = new nsString(kSRVN); break; case NS_WIN_REG_SET_VAL: keyString = keystr(mRootkey, mSubkey, mName); result = new nsString(kSRV); break; default: keyString = keystr(mRootkey, mSubkey, mName); result = new nsString(kUNK); break; } if (result) { result->Append(*keyString); resultCString = result->ToNewCString(); } if (keyString) delete keyString; if (result) delete result; return resultCString; } PRInt32 nsWinRegItem::Prepare() { return nsnull; } void nsWinRegItem::Abort() { } /* Private Methods */ nsString* nsWinRegItem::keystr(PRInt32 root, nsString* mSubkey, nsString* mName) { nsString rootstr; nsString* finalstr = nsnull; char* istr = nsnull; switch(root) { case (int)(HKEY_CLASSES_ROOT) : rootstr = "HKEY_CLASSES_ROOT\\"; break; case (int)(HKEY_CURRENT_USER) : rootstr = "HKEY_CURRENT_USER\\"; break; case (int)(HKEY_LOCAL_MACHINE) : rootstr = "HKEY_LOCAL_MACHINE\\"; break; case (int)(HKEY_USERS) : rootstr = "HKEY_USERS\\"; break; default: istr = itoa(root); if (istr) { rootstr = "#"; rootstr.Append(istr); rootstr.Append("\\"); PR_DELETE(istr); } break; } finalstr = new nsString(rootstr); if(finalstr != nsnull) { finalstr->Append(*mSubkey); finalstr->Append(" ["); if(mName != nsnull) finalstr->Append(*mName); finalstr->Append("]"); } return finalstr; } char* nsWinRegItem::itoa(PRInt32 n) { char* s; int i, sign; if((sign = n) < 0) n = -n; i = 0; s = (char*)PR_CALLOC(sizeof(char)); do { s = (char*)PR_REALLOC(s, (i+1)*sizeof(char)); s[i++] = n%10 + '0'; s[i] = '\0'; } while ((n/=10) > 0); if(sign < 0) { s = (char*)PR_REALLOC(s, (i+1)*sizeof(char)); s[i++] = '-'; } s[i] = '\0'; reverseString(s); return s; } void nsWinRegItem::reverseString(char* s) { int c, i, j; for(i=0, j=strlen(s)-1; i