sbImageLabelLinkPropertyBuilder.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
26 
27 #include <sbIPropertyArray.h>
28 #include <sbIPropertyManager.h>
29 #include <nsITreeView.h>
30 
31 #include <nsAutoPtr.h>
32 #include <nsIURI.h>
33 #include <nsNetUtil.h>
34 #include <nsServiceManagerUtils.h>
35 #include <nsUnicharUtils.h>
36 #include <sbStringUtils.h>
37 
39 
44 
46  : mImages(nsnull),
47  mLabels(nsnull),
48  mClickHandlers(nsnull)
49 {
50 }
51 
53 {
54  delete mImages;
55  delete mLabels;
56  delete mClickHandlers;
57 }
58 
59 nsresult
61 {
62  nsresult rv;
63 
64  mImages = new nsClassHashtable<nsCStringHashKey, nsCString>();
65  NS_ENSURE_TRUE(mImages, NS_ERROR_OUT_OF_MEMORY);
66  mLabels = new nsClassHashtable<nsCStringHashKey, nsString>();
67  NS_ENSURE_TRUE(mLabels, NS_ERROR_OUT_OF_MEMORY);
68  mClickHandlers = new nsTHashtable<nsISupportsHashKey>();
69  NS_ENSURE_TRUE(mClickHandlers, NS_ERROR_OUT_OF_MEMORY);
70  mImages->Init();
71  mLabels->Init();
72  mClickHandlers->Init();
74  NS_ENSURE_SUCCESS(rv, rv);
75 
76  return NS_OK;
77 }
78 
79 /***** sbIPropertyBuilder */
80 
81 NS_IMETHODIMP
83 {
84  nsresult rv;
85  nsRefPtr<sbImageLabelLinkPropertyInfo> info =
87  rv = info->Init(mImages, mLabels, mClickHandlers);
88  NS_ENSURE_SUCCESS(rv, rv);
89  rv = info->SetPropertyID(mPropertyID);
90  NS_ENSURE_SUCCESS(rv, rv);
91  rv = info->SetDisplayName(mDisplayName);
92  NS_ENSURE_SUCCESS(rv, rv);
93  rv = info->SetLocalizationKey(mDisplayNameKey);
94  NS_ENSURE_SUCCESS(rv, rv);
95  rv = info->SetRemoteReadable(mRemoteReadable);
96  NS_ENSURE_SUCCESS(rv, rv);
97  rv = info->SetRemoteWritable(mRemoteWritable);
98  NS_ENSURE_SUCCESS(rv, rv);
99  rv = info->SetUserViewable(mUserViewable);
100  NS_ENSURE_SUCCESS(rv, rv);
101  rv = info->SetUserEditable(mUserEditable);
102  NS_ENSURE_SUCCESS(rv, rv);
103 
104  return CallQueryInterface(info.get(), _retval);
105 }
106 
107 /***** sbIImageLabelLinkPropertyBuilder */
108 
109 /* void addImage (in ACString aKey, in ACString aImageUrl); */
110 NS_IMETHODIMP
111 sbImageLabelLinkPropertyBuilder::AddImage(const nsACString & aKey,
112  const nsACString & aImageUrl)
113 {
114  NS_ENSURE_TRUE(mImages, NS_ERROR_NOT_INITIALIZED);
115  PRBool success = mImages->Put(aKey, new nsCString(aImageUrl));
116  NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
117  return NS_OK;
118 }
119 
120 /* ACString getImage (in ACString aKey); */
121 NS_IMETHODIMP
122 sbImageLabelLinkPropertyBuilder::GetImage(const nsACString & aKey,
123  nsACString & _retval NS_OUTPARAM)
124 {
125  NS_ENSURE_TRUE(mImages, NS_ERROR_NOT_INITIALIZED);
126 
127  nsCString key(aKey), *result;
128 
129  // look for the key as given
130  if (mImages->Get(aKey, &result)) {
131  _retval.Assign(*result);
132  return NS_OK;
133  }
134  // look for a default value
135  if (mImages->Get(nsCString(), &result)) {
136  _retval.Assign(*result);
137  return NS_OK;
138  }
139  // nothing at all; give an empty string back
140  _retval.Truncate();
141  return NS_OK;
142 }
143 
144 /* void addLabel (in ACString aKey, in AString aLabel); */
145 NS_IMETHODIMP
146 sbImageLabelLinkPropertyBuilder::AddLabel(const nsACString & aKey,
147  const nsAString & aLabel)
148 {
149  NS_ENSURE_TRUE(mLabels, NS_ERROR_NOT_INITIALIZED);
150 
151  nsString value(aLabel);
152 
153  if (StringBeginsWith(aLabel, NS_LITERAL_STRING("&")) &&
154  StringEndsWith(aLabel, NS_LITERAL_STRING(";")))
155  {
156  // localize the string
157  value = SBLocalizedString(Substring(aLabel, 1, aLabel.Length() - 2));
158  }
159 
160  PRBool success = mLabels->Put(aKey, new nsString(value));
161  NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
162  return NS_OK;
163 }
164 
165 /* AString getLabel (in ACString aKey); */
166 NS_IMETHODIMP
167 sbImageLabelLinkPropertyBuilder::GetLabel(const nsACString & aKey,
168  nsAString & _retval NS_OUTPARAM)
169 {
170  NS_ENSURE_TRUE(mLabels, NS_ERROR_NOT_INITIALIZED);
171 
172  nsString *result;
173 
174  // look for the key as given
175  if (mLabels->Get(aKey, &result)) {
176  _retval.Assign(*result);
177  return NS_OK;
178  }
179  // look for a default value
180  if (mLabels->Get(nsCString(), &result)) {
181  _retval.Assign(*result);
182  return NS_OK;
183  }
184  // nothing at all; give an empty string back
185  _retval.Truncate();
186  return NS_OK;
187 }
188 
189 /***** sbIClickablePropertyBuilder */
190 /* void addClickHandler (in sbIClickablePropertyCallback aCallback); */
191 NS_IMETHODIMP
192 sbImageLabelLinkPropertyBuilder::AddClickHandler(
193  sbIClickablePropertyCallback *aCallback)
194 {
195  NS_ENSURE_TRUE(mClickHandlers, NS_ERROR_NOT_INITIALIZED);
196  nsISupportsHashKey* entry = mClickHandlers->PutEntry(aCallback);
197  NS_ENSURE_TRUE(entry, NS_ERROR_OUT_OF_MEMORY);
198  return NS_OK;
199 }
200 
201 /* void removeClickHandler (in sbIClickablePropertyCallback aCallback); */
202 NS_IMETHODIMP
203 sbImageLabelLinkPropertyBuilder::RemoveClickHandler(
204  sbIClickablePropertyCallback *aCallback)
205 {
206  NS_ENSURE_TRUE(mClickHandlers, NS_ERROR_NOT_INITIALIZED);
207  mClickHandlers->RemoveEntry(aCallback);
208  return NS_OK;
209 }
classDescription entry
Definition: FeedWriter.js:1427
return NS_OK
An interface used to describe a metadata property for use by the UI and other sbILibrary interfaces (...
countRef value
Definition: FeedWriter.js:1423