Back to index

lightning-sunbird  0.9+nobinonly
nsWinProfile.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 "nsWinReg.h"
00039 #include "nsWinProfile.h"
00040 #include "nsWinProfileItem.h"
00041 #include "nspr.h"
00042 #include <windows.h>
00043 #include "nsNativeCharsetUtils.h"
00044 
00045 /* Public Methods */
00046 
00047 MOZ_DECL_CTOR_COUNTER(nsWinProfile)
00048 
00049 nsWinProfile::nsWinProfile( nsInstall* suObj, const nsString& folder, const nsString& file )
00050   : mFilename(folder)
00051 {
00052   MOZ_COUNT_CTOR(nsWinProfile);
00053 
00054   if(mFilename.Last() != '\\')
00055   {
00056       mFilename.AppendLiteral("\\");
00057   }
00058   mFilename.Append(file);
00059 
00060        mInstallObject = suObj;
00061 }
00062 
00063 nsWinProfile::~nsWinProfile()
00064 {
00065   MOZ_COUNT_DTOR(nsWinProfile);
00066 }
00067 
00068 PRInt32
00069 nsWinProfile::GetString(nsString section, nsString key, nsString* aReturn)
00070 {
00071   return NativeGetString(section, key, aReturn);
00072 }
00073 
00074 PRInt32
00075 nsWinProfile::WriteString(nsString section, nsString key, nsString value, PRInt32* aReturn)
00076 {
00077   *aReturn = NS_OK;
00078   
00079   nsWinProfileItem* wi = new nsWinProfileItem(this, section, key, value, aReturn);
00080 
00081   if(wi == nsnull)
00082   {
00083     *aReturn = nsInstall::OUT_OF_MEMORY;
00084     return NS_OK;
00085   }
00086 
00087   if(*aReturn != nsInstall::SUCCESS)
00088   {
00089     if(wi)
00090     {
00091       delete wi;
00092       return NS_OK;
00093     }
00094   }
00095 
00096   if (mInstallObject)
00097     mInstallObject->ScheduleForInstall(wi);
00098   
00099   return NS_OK;
00100 }
00101 
00102 nsString& nsWinProfile::GetFilename()
00103 {
00104        return mFilename;
00105 }
00106 
00107 nsInstall* nsWinProfile::InstallObject()
00108 {
00109        return mInstallObject;
00110 }
00111 
00112 PRInt32
00113 nsWinProfile::FinalWriteString( nsString section, nsString key, nsString value )
00114 {
00115        /* do we need another security check here? */
00116        return NativeWriteString(section, key, value);
00117 }
00118 
00119 /* Private Methods */
00120 
00121 #define STRBUFLEN 255
00122   
00123 PRInt32
00124 nsWinProfile::NativeGetString(nsString aSection, nsString aKey, nsString* aReturn )
00125 {
00126   int       numChars = 0;
00127   char      valbuf[STRBUFLEN];
00128 
00129   /* make sure conversions worked */
00130   if(aSection.First() != '\0' && aKey.First() != '\0' && mFilename.First() != '\0')
00131   {
00132     nsCAutoString section;
00133     nsCAutoString key;
00134     nsCAutoString filename;
00135 
00136     if(NS_FAILED(NS_CopyUnicodeToNative(aSection, section)) ||
00137        NS_FAILED(NS_CopyUnicodeToNative(aKey, key)) ||
00138        NS_FAILED(NS_CopyUnicodeToNative(mFilename, filename)))
00139       return 0;
00140 
00141     valbuf[0] = 0;
00142     numChars = GetPrivateProfileString( section.get(), key.get(), "", valbuf,
00143                                         STRBUFLEN, filename.get());
00144 
00145     nsCAutoString cStrValue(valbuf);
00146     nsAutoString value;
00147     if(NS_SUCCEEDED(NS_CopyNativeToUnicode(cStrValue, value)))
00148       aReturn->Assign(value);
00149   }
00150 
00151   return numChars;
00152 }
00153 
00154 PRInt32
00155 nsWinProfile::NativeWriteString( nsString aSection, nsString aKey, nsString aValue )
00156 {
00157   int   success = 0;
00158 
00159        /* make sure conversions worked */
00160   if(aSection.First() != '\0' && aKey.First() != '\0' && mFilename.First() != '\0')
00161   {
00162     nsCAutoString section;
00163     nsCAutoString key;
00164     nsCAutoString value;
00165     nsCAutoString filename;
00166 
00167     if(NS_FAILED(NS_CopyUnicodeToNative(aSection, section)) ||
00168        NS_FAILED(NS_CopyUnicodeToNative(aKey, key)) ||
00169        NS_FAILED(NS_CopyUnicodeToNative(aValue, value)) ||
00170        NS_FAILED(NS_CopyUnicodeToNative(mFilename, filename)))
00171       return 0;
00172 
00173     success = WritePrivateProfileString( section.get(), key.get(), value.get(), filename.get() );
00174   }
00175 
00176   return success;
00177 }
00178