sbImageLabelLinkPropertyInfo.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 <nsIURI.h>
32 #include <nsNetUtil.h>
33 #include <nsServiceManagerUtils.h>
34 #include <nsUnicharUtils.h>
35 #include <sbHashtableUtils.h>
36 #include <sbStringUtils.h>
37 
41 
43  : sbImageLinkPropertyInfo(nsString(),
44  nsString(),
45  nsString(),
46  PR_FALSE,
47  PR_FALSE,
48  PR_TRUE,
49  PR_TRUE,
50  nsString()),
51  mImages(nsnull),
52  mLabels(nsnull),
53  mClickHandlers(nsnull)
54 {
55  mType.AssignLiteral("image");
56 }
57 
59 {
60  delete mImages;
61  delete mLabels;
62  delete mClickHandlers;
63 }
64 
65 nsresult
67 {
68  mImages = new nsClassHashtable<nsCStringHashKey, nsCString>();
69  NS_ENSURE_TRUE(mImages, NS_ERROR_OUT_OF_MEMORY);
70  mLabels = new nsClassHashtable<nsCStringHashKey, nsString>();
71  NS_ENSURE_TRUE(mLabels, NS_ERROR_OUT_OF_MEMORY);
72  mClickHandlers = new nsTHashtable<nsISupportsHashKey>();
73  NS_ENSURE_TRUE(mClickHandlers, NS_ERROR_OUT_OF_MEMORY);
74  mImages->Init();
75  mLabels->Init();
76  mClickHandlers->Init();
77 
78  nsresult rv = sbImageLinkPropertyInfo::Init();
79  NS_ENSURE_SUCCESS(rv, rv);
80 
81  return NS_OK;
82 }
83 
84 nsresult
86  LabelMap_t *&aLabels,
87  InterfaceSet_t *&aClickHandlers)
88 {
89  mImages = aImages;
90  aImages = nsnull;
91  mLabels = aLabels;
92  aLabels = nsnull;
93  mClickHandlers = aClickHandlers;
94  aClickHandlers = nsnull;
95 
96  nsresult rv = sbImageLinkPropertyInfo::Init();
97  NS_ENSURE_SUCCESS(rv, rv);
98 
99  return NS_OK;
100 }
101 
102 NS_IMETHODIMP
103 sbImageLabelLinkPropertyInfo::SetPropertyID(const nsAString& aPropertyID)
104 {
105  mID = aPropertyID;
106  return NS_OK;
107 }
108 
109 NS_IMETHODIMP
110 sbImageLabelLinkPropertyInfo::SetDisplayName(const nsAString& aDisplayName)
111 {
112  mDisplayName = aDisplayName;
113  return NS_OK;
114 }
115 
116 NS_IMETHODIMP
117 sbImageLabelLinkPropertyInfo::SetLocalizationKey(const nsAString& aLocalizationKey)
118 {
119  mLocalizationKey = aLocalizationKey;
120  return NS_OK;
121 }
122 
123 NS_IMETHODIMP
125 {
126  mRemoteReadable = aRemoteReadable;
127  return NS_OK;
128 }
129 
130 NS_IMETHODIMP
132 {
133  mRemoteWritable = aRemoteWritable;
134  return NS_OK;
135 }
136 
137 NS_IMETHODIMP
139 {
140  mUserViewable = aUserViewable;
141  return NS_OK;
142 }
143 
144 NS_IMETHODIMP
146 {
147  mUserEditable = aUserEditable;
148  return NS_OK;
149 }
150 
151 NS_IMETHODIMP
152 sbImageLabelLinkPropertyInfo::SetUrlPropertyID(const nsAString& aUrlPropertyID)
153 {
154  mUrlPropertyID = aUrlPropertyID;
155  return NS_OK;
156 }
157 
158 /***** sbIImageLinkPropertyInfo */
159 
160 NS_IMETHODIMP
161 sbImageLabelLinkPropertyInfo::GetUrlProperty(nsAString& _retval)
162 {
163  _retval = mUrlPropertyID;
164 
165  return NS_OK;
166 }
167 
168 NS_IMETHODIMP
169 sbImageLabelLinkPropertyInfo::GetPreventNavigation(const nsAString& aImageValue,
170  const nsAString& aUrlValue,
171  PRBool *_retval)
172 {
173  NS_ENSURE_ARG_POINTER(_retval);
174 
175  *_retval = aImageValue.IsEmpty() || aUrlValue.IsEmpty();
176 
177  return NS_OK;
178 }
179 
180 /***** sbIImageLabelLinkPropertyInfo */
181 
182 /* void addImage (in ACString aKey, in ACString aImageUrl); */
183 NS_IMETHODIMP
184 sbImageLabelLinkPropertyInfo::AddImage(const nsACString & aKey,
185  const nsACString & aImageUrl)
186 {
187  NS_ENSURE_TRUE(mImages, NS_ERROR_NOT_INITIALIZED);
188 
189  if (mImages->Get(aKey, nsnull)) {
190  NS_WARNING("found an existing image key");
191  return NS_OK;
192  }
193 
194  PRBool success = mImages->Put(aKey, new nsCString(aImageUrl));
195  NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
196  return NS_OK;
197 }
198 
199 /* void addLabel (in ACString aKey, in AString aLabel); */
200 NS_IMETHODIMP
201 sbImageLabelLinkPropertyInfo::AddLabel(const nsACString & aKey,
202  const nsAString & aLabel)
203 {
204  NS_ENSURE_TRUE(mLabels, NS_ERROR_NOT_INITIALIZED);
205 
206  if (mLabels->Get(aKey, nsnull)) {
207  NS_WARNING("found an existing label key");
208  return NS_OK;
209  }
210 
211  nsString value(aLabel);
212 
213  if (StringBeginsWith(aLabel, NS_LITERAL_STRING("&")) &&
214  StringEndsWith(aLabel, NS_LITERAL_STRING(";")))
215  {
216  // localize the string
217  value = SBLocalizedString(Substring(aLabel, 1, aLabel.Length() - 2));
218  }
219 
220  PRBool success = mLabels->Put(aKey, new nsString(value));
221  NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
222  return NS_OK;
223 }
224 
225 /***** sbITreeViewPropertyInfo */
226 
227 NS_IMETHODIMP
229  nsAString& _retval)
230 {
231  NS_ENSURE_TRUE(mImages, NS_ERROR_NOT_INITIALIZED);
232 
233  NS_LossyConvertUTF16toASCII key(aValue);
234  nsCString *result;
235 
236  // look for the key as given
237  if (mImages->Get(key, &result)) {
238  CopyASCIItoUTF16(*result, _retval);
239  return NS_OK;
240  }
241  // look for a default value
242  if (mImages->Get(nsCString(), &result)) {
243  CopyASCIItoUTF16(*result, _retval);
244  return NS_OK;
245  }
246  // nothing at all; give an empty string back
247  _retval.Truncate();
248  return NS_OK;
249 }
250 
251 NS_IMETHODIMP
253  nsAString& _retval)
254 {
255  if(aValue.IsEmpty()) {
256  _retval.AssignLiteral("image label noLink");
257  return NS_OK;
258  }
259 
260  _retval.AssignLiteral("image label link");
261 
262  return NS_OK;
263 }
264 
265 /***** sbIClickablePropertyInfo */
266 NS_IMETHODIMP
267 sbImageLabelLinkPropertyInfo::HitTest(const nsAString& aCurrentValue,
268  const nsAString& aPart,
269  PRUint32 aBoxWidth,
270  PRUint32 aBoxHeight,
271  PRUint32 aMouseX,
272  PRUint32 aMouseY,
273  PRBool* _retval)
274 {
275  NS_ENSURE_ARG_POINTER(_retval);
276  *_retval = aPart.EqualsLiteral("image") ||
277  aPart.EqualsLiteral("text");
278  return NS_OK;
279 }
280 
281 NS_IMETHODIMP
283  nsISupports *aEvent,
284  nsISupports *aContext,
285  PRBool *_retval NS_OUTPARAM)
286 {
287  NS_ENSURE_ARG_POINTER(aItem);
288  NS_ENSURE_ARG_POINTER(_retval);
289  *_retval = PR_FALSE;
290 
291  nsresult rv;
292 
293  if (!mClickHandlers) {
294  return NS_OK;
295  }
296 
297  // copy the handlers to an auxilary array to handle removing them in the call
298  nsCOMPtr<nsIMutableArray> handlers =
299  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1");
300  NS_ENSURE_TRUE(handlers, NS_ERROR_OUT_OF_MEMORY);
301  sbCopyHashtableToArray(*mClickHandlers, handlers);
302 
303  nsCOMPtr<nsISimpleEnumerator> enumerator;
304  rv = handlers->Enumerate(getter_AddRefs(enumerator));
305  NS_ENSURE_SUCCESS(rv, rv);
306 
307  PRBool hasMore, result;
308 
309  while (NS_SUCCEEDED(rv = enumerator->HasMoreElements(&hasMore)) && hasMore) {
310  nsCOMPtr<nsISupports> supports;
311  rv = enumerator->GetNext(getter_AddRefs(supports));
312  NS_ENSURE_SUCCESS(rv, rv);
313  nsCOMPtr<sbIClickablePropertyCallback> callback =
314  do_QueryInterface(supports);
315  if (!callback) {
316  NS_WARNING("found a onClick callback that is "
317  "not a sbIClickablePropertyCallback");
318  continue;
319  }
320  rv = callback->OnClick(this,
321  aItem,
322  aEvent,
323  aContext,
324  &result);
325  if (NS_SUCCEEDED(rv) && result) {
326  *_retval = PR_TRUE;
327  }
328  }
329  return NS_OK;
330 }
331 
332 
333 /***** sbIPropertyInfo */
334 
335 NS_IMETHODIMP
337  nsAString& _retval)
338 {
339  NS_ENSURE_TRUE(mLabels, NS_ERROR_NOT_INITIALIZED);
340 
341  NS_LossyConvertUTF16toASCII key(aValue);
342  nsString *result;
343 
344  // look for the key as given
345  if (mLabels->Get(key, &result)) {
346  _retval.Assign(*result);
347  return NS_OK;
348  }
349  // look for a default value
350  if (mLabels->Get(nsCString(), &result)) {
351  _retval.Assign(*result);
352  return NS_OK;
353  }
354  // nothing at all; give an empty string back
355  _retval.Truncate();
356  return NS_OK;
357 }
return NS_OK
onPageChanged aValue
Definition: FeedWriter.js:1395
var handlers
Definition: FeedWriter.js:1044
grep callback
nsresult sbCopyHashtableToArray(class nsTHashtable< T > &aSource, nsIMutableArray *aDest)
StringArrayEnumerator prototype hasMore
countRef value
Definition: FeedWriter.js:1423
Interface that defines a single item of media in the system.