Back to index

lightning-sunbird  0.9+nobinonly
nsPresState.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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 "nsCOMPtr.h"
00039 #include "nsXPCOM.h"
00040 #include "nsISupportsPrimitives.h"
00041 #include "nsIComponentManager.h"
00042 #include "nsXPIDLString.h"
00043 #include "nsReadableUtils.h"
00044 #include "nsLayoutErrors.h"
00045 #include "nsPresState.h"
00046 #include "nsString.h"
00047 
00048 // Implementation /////////////////////////////////////////////////////////////////
00049 
00050 nsresult
00051 nsPresState::Init()
00052 {
00053   return mPropertyTable.Init(8) ? NS_OK : NS_ERROR_FAILURE;
00054 }
00055 
00056 nsresult
00057 nsPresState::GetStateProperty(const nsAString& aName, nsAString& aResult)
00058 {
00059   nsresult rv = NS_STATE_PROPERTY_NOT_THERE;
00060   aResult.Truncate();
00061 
00062   // Retrieve from hashtable.
00063   nsISupports *data = mPropertyTable.GetWeak(aName);
00064 
00065   // Strings are stored in the table as UTF-8, to save space.
00066   // XXX minimize conversions here...
00067 
00068   nsCOMPtr<nsISupportsCString> supportsStr = do_QueryInterface(data);
00069   if (supportsStr) {
00070     nsCAutoString data;
00071     supportsStr->GetData(data);
00072 
00073     CopyUTF8toUTF16(data, aResult);
00074     rv = NS_STATE_PROPERTY_EXISTS;
00075   }
00076 
00077   return rv;
00078 }
00079 
00080 nsresult
00081 nsPresState::SetStateProperty(const nsAString& aName, const nsAString& aValue)
00082 {
00083   // Add to hashtable
00084   nsCOMPtr<nsISupportsCString> supportsStr(do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID));
00085   NS_ENSURE_TRUE(supportsStr, NS_ERROR_OUT_OF_MEMORY);
00086 
00087   supportsStr->SetData(NS_ConvertUCS2toUTF8(aValue));
00088 
00089   mPropertyTable.Put(aName, supportsStr);
00090   return NS_OK;
00091 }
00092 
00093 nsresult
00094 nsPresState::RemoveStateProperty(const nsAString& aName)
00095 {
00096   mPropertyTable.Remove(aName);
00097   return NS_OK;
00098 }
00099 
00100 nsresult
00101 nsPresState::GetStatePropertyAsSupports(const nsAString& aName,
00102                                         nsISupports** aResult)
00103 {
00104   // Retrieve from hashtable.
00105   mPropertyTable.Get(aName, aResult);
00106   return NS_OK;
00107 }
00108 
00109 nsresult
00110 nsPresState::SetStatePropertyAsSupports(const nsAString& aName,
00111                                         nsISupports* aValue)
00112 {
00113   mPropertyTable.Put(aName, aValue);
00114   return NS_OK;
00115 }
00116 
00117 
00118 nsresult
00119 NS_NewPresState(nsPresState** aState)
00120 {
00121   nsPresState *state;
00122 
00123   *aState = nsnull;
00124   state = new nsPresState();
00125   if (!state)
00126     return NS_ERROR_OUT_OF_MEMORY;
00127 
00128   nsresult rv = state->Init();
00129   if (NS_SUCCEEDED(rv))
00130     *aState = state;
00131   else
00132     delete state;
00133 
00134   return rv;
00135 }
00136 
00137