Back to index

lightning-sunbird  0.9+nobinonly
nsWinRegItem.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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 "nsWinRegItem.h"
00039 #include "nspr.h"
00040 #include "nsWinReg.h"
00041 
00042 #ifdef WIN32
00043 #include <windows.h> /* is this needed? */
00044 #endif
00045 
00046 /* Public Methods */
00047 
00048 MOZ_DECL_CTOR_COUNTER(nsWinRegItem)
00049 
00050 nsWinRegItem::nsWinRegItem(nsWinReg* regObj, PRInt32 root, PRInt32 action, const nsAString& sub, const nsAString& valname, const nsAString& val, PRInt32 *aReturn)
00051 : nsInstallObject(regObj->InstallObject())
00052 {
00053     MOZ_COUNT_CTOR(nsWinRegItem);
00054 
00055     mReg     = regObj;
00056        mCommand = action;
00057        mRootkey = root;
00058 
00059   *aReturn = nsInstall::SUCCESS;
00060 
00061   /* I'm assuming we need to copy these */
00062        mSubkey  = new nsString(sub);
00063        mName    = new nsString(valname);
00064        mValue   = new nsString(val);
00065 
00066   if((mSubkey == nsnull) ||
00067      (mName   == nsnull) ||
00068      (mValue  == nsnull))
00069   {
00070     *aReturn = nsInstall::OUT_OF_MEMORY;
00071   }
00072 }
00073 
00074 nsWinRegItem::nsWinRegItem(nsWinReg* regObj, PRInt32 root, PRInt32 action, const nsAString& sub, const nsAString& valname, PRInt32 val, PRInt32 *aReturn)
00075 : nsInstallObject(regObj->InstallObject())
00076 {
00077     MOZ_COUNT_CTOR(nsWinRegItem);
00078 
00079        mReg     = regObj;
00080        mCommand = action;
00081        mRootkey = root;
00082 
00083   *aReturn = nsInstall::SUCCESS;
00084 
00085   /* I'm assuming we need to copy these */
00086        mSubkey  = new nsString(sub);
00087        mName    = new nsString(valname);
00088        mValue   = new PRInt32(val);
00089 
00090   if((mSubkey == nsnull) ||
00091      (mName   == nsnull) ||
00092      (mValue  == nsnull))
00093   {
00094     *aReturn = nsInstall::OUT_OF_MEMORY;
00095   }
00096 }
00097 
00098 nsWinRegItem::~nsWinRegItem()
00099 {
00100   if (mSubkey)  delete mSubkey;
00101   if (mName)    delete mName;
00102   if (mValue)   delete mValue;
00103   MOZ_COUNT_DTOR(nsWinRegItem);
00104 }
00105 
00106 PRInt32 nsWinRegItem::Complete()
00107 {
00108   PRInt32 aReturn = NS_OK;
00109   
00110   if (mReg == nsnull)
00111       return nsInstall::OUT_OF_MEMORY;
00112 
00113   switch (mCommand)
00114   {
00115     case NS_WIN_REG_CREATE:
00116         mReg->FinalCreateKey(mRootkey, *mSubkey, *mName, &aReturn);
00117         break;
00118     
00119     case NS_WIN_REG_DELETE:
00120         mReg->FinalDeleteKey(mRootkey, *mSubkey, &aReturn);
00121         break;
00122     
00123     case NS_WIN_REG_DELETE_VAL:
00124         mReg->FinalDeleteValue(mRootkey, *mSubkey, *mName, &aReturn);
00125         break;
00126     
00127     case NS_WIN_REG_SET_VAL_STRING:
00128         mReg->FinalSetValueString(mRootkey, *mSubkey, *mName, *(nsString*)mValue, &aReturn);
00129         break;
00130 
00131     case NS_WIN_REG_SET_VAL_NUMBER:
00132         mReg->FinalSetValueNumber(mRootkey, *mSubkey, *mName, *(PRInt32*)mValue, &aReturn);
00133         break;
00134     
00135     case NS_WIN_REG_SET_VAL:
00136         mReg->FinalSetValue(mRootkey, *mSubkey, *mName, (nsWinRegValue*)mValue, &aReturn);
00137         break;
00138   }
00139        return aReturn;
00140 }
00141   
00142 #define kCRK  "Create Registry Key: "
00143 #define kDRK  "Delete Registry Key: "
00144 #define kDRV  "Delete Registry Value: "
00145 #define kSRVS "Store Registry Value String: "
00146 #define kSRVN "Store Registry Value Number: "
00147 #define kSRV  "Store Registry Value: "
00148 #define kUNK  "Unknown "
00149 
00150 char* nsWinRegItem::toString()
00151 {
00152        nsString*  keyString     = nsnull;
00153        nsString*  result        = nsnull;
00154   char*      resultCString = nsnull;
00155 
00156        switch(mCommand)
00157        {
00158     case NS_WIN_REG_CREATE:
00159       keyString = keystr(mRootkey, mSubkey, nsnull);
00160       result    = new nsString;
00161       result->AssignWithConversion(kCRK);
00162       break;
00163 
00164     case NS_WIN_REG_DELETE:
00165       keyString = keystr(mRootkey, mSubkey, nsnull);
00166       result    = new nsString;
00167       result->AssignWithConversion(kDRK);
00168       break;
00169 
00170     case NS_WIN_REG_DELETE_VAL:
00171       keyString = keystr(mRootkey, mSubkey, mName);
00172       result    = new nsString;
00173       result->AssignWithConversion(kDRV);
00174      break;
00175 
00176     case NS_WIN_REG_SET_VAL_STRING:
00177       keyString = keystr(mRootkey, mSubkey, mName);
00178       result    = new nsString;
00179       result->AssignWithConversion(kSRVS);
00180       break;
00181 
00182     case NS_WIN_REG_SET_VAL_NUMBER:
00183       keyString = keystr(mRootkey, mSubkey, mName);
00184       result    = new nsString;
00185       result->AssignWithConversion(kSRVN);
00186       break;
00187 
00188     case NS_WIN_REG_SET_VAL:
00189       keyString = keystr(mRootkey, mSubkey, mName);
00190       result    = new nsString;
00191       result->AssignWithConversion(kSRV);
00192       break;
00193 
00194     default:
00195       keyString = keystr(mRootkey, mSubkey, mName);
00196       result    = new nsString;
00197       result->AssignWithConversion(kUNK);
00198       break;
00199        }
00200     
00201   if (result)
00202   {
00203       result->Append(*keyString);
00204       resultCString = new char[result->Length() + 1];
00205       if(resultCString != nsnull)
00206           result->ToCString(resultCString, result->Length() + 1);
00207   }
00208   
00209   if (keyString) delete keyString;
00210   if (result)    delete result;
00211   
00212   return resultCString;
00213 }
00214 
00215 PRInt32 nsWinRegItem::Prepare()
00216 {
00217   PRInt32 aReturn = NS_OK;
00218   
00219   if (mReg == nsnull)
00220       return nsInstall::OUT_OF_MEMORY;
00221 
00222   switch (mCommand)
00223   {
00224     case NS_WIN_REG_CREATE:
00225         mReg->PrepareCreateKey(mRootkey, *mSubkey, &aReturn);
00226         break;
00227     
00228     case NS_WIN_REG_DELETE:
00229         mReg->PrepareDeleteKey(mRootkey, *mSubkey, &aReturn);
00230         break;
00231     
00232     case NS_WIN_REG_DELETE_VAL:
00233         mReg->PrepareDeleteValue(mRootkey, *mSubkey, *mName, &aReturn);
00234         break;
00235     
00236     case NS_WIN_REG_SET_VAL_STRING:
00237         mReg->PrepareSetValueString(mRootkey, *mSubkey, &aReturn);
00238         break;
00239 
00240     case NS_WIN_REG_SET_VAL_NUMBER:
00241         mReg->PrepareSetValueNumber(mRootkey, *mSubkey, &aReturn);
00242         break;
00243     
00244     case NS_WIN_REG_SET_VAL:
00245         mReg->PrepareSetValue(mRootkey, *mSubkey, &aReturn);
00246         break;
00247 
00248     default:
00249         break;
00250   }
00251        return aReturn;
00252 }
00253 
00254 void nsWinRegItem::Abort()
00255 {
00256 }
00257 
00258 /* Private Methods */
00259 
00260 nsString* nsWinRegItem::keystr(PRInt32 root, nsString* mSubkey, nsString* mName)
00261 {
00262        nsString  rootstr;
00263        nsString* finalstr = nsnull;
00264   char*     istr     = nsnull;
00265 
00266        switch(root)
00267        {
00268          case nsWinReg::NS_HKEY_CLASSES_ROOT:
00269                 rootstr.AssignLiteral("HKEY_CLASSES_ROOT\\");
00270                 break;
00271 
00272          case nsWinReg::NS_HKEY_CURRENT_USER:
00273                 rootstr.AssignLiteral("HKEY_CURRENT_USER\\");
00274                 break;
00275 
00276          case nsWinReg::NS_HKEY_LOCAL_MACHINE:
00277                 rootstr.AssignLiteral("HKEY_LOCAL_MACHINE\\");
00278                 break;
00279 
00280          case nsWinReg::NS_HKEY_USERS:
00281                 rootstr.AssignLiteral("HKEY_USERS\\");
00282                 break;
00283 
00284     default:
00285       istr = itoa(root);
00286       if (istr)
00287       {
00288         rootstr.AssignLiteral("#");
00289         rootstr.AppendWithConversion(istr);
00290         rootstr.AppendLiteral("\\");
00291         
00292         PR_DELETE(istr);
00293       }
00294       break;
00295        }
00296 
00297   finalstr = new nsString(rootstr);
00298        if(finalstr != nsnull)
00299        {
00300     finalstr->Append(*mSubkey);
00301     finalstr->AppendLiteral(" [");
00302 
00303     if(mName != nsnull)
00304       finalstr->Append(*mName);
00305 
00306     finalstr->AppendLiteral("]");
00307        }
00308 
00309   return finalstr;
00310 }
00311 
00312 
00313 char* nsWinRegItem::itoa(PRInt32 n)
00314 {
00315        char* s;
00316        int i, sign;
00317        if((sign = n) < 0)
00318               n = -n;
00319        i = 0;
00320        
00321        s = (char*)PR_CALLOC(sizeof(char));
00322 
00323        do
00324               {
00325               s = (char*)PR_REALLOC(s, (i+1)*sizeof(char));
00326               s[i++] = n%10 + '0';
00327               s[i] = '\0';
00328               } while ((n/=10) > 0);
00329               
00330        if(sign < 0)
00331        {
00332               s = (char*)PR_REALLOC(s, (i+1)*sizeof(char));
00333               s[i++] = '-';
00334        }
00335        s[i]  = '\0';
00336        reverseString(s);
00337        return s;
00338 }
00339 
00340 void nsWinRegItem::reverseString(char* s)
00341 {
00342        int c, i, j;
00343        
00344        for(i=0, j=strlen(s)-1; i<j; i++, j--)
00345               {
00346               c = s[i];
00347               s[i] = s[j];
00348               s[j] = c;
00349               }
00350 }
00351 
00352 /* CanUninstall
00353 * WinRegItem() does not install any files which can be uninstalled,
00354 * hence this function returns false. 
00355 */
00356 PRBool
00357 nsWinRegItem:: CanUninstall()
00358 {
00359     return PR_FALSE;
00360 }
00361 
00362 /* RegisterPackageNode
00363 * WinRegItem() installs files which need to be registered,
00364 * hence this function returns true.
00365 */
00366 PRBool
00367 nsWinRegItem:: RegisterPackageNode()
00368 {
00369     return PR_TRUE;
00370 }
00371