sbRatingPropertyInfo.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #include "sbRatingPropertyInfo.h"
28 
29 #include <nsAutoPtr.h>
30 #include <sbIPropertyManager.h>
31 #include <nsITreeView.h>
32 
37 
38 #define STAR_WIDTH 14
39 #define MAX_RATING 5
40 #define ZERO_HIT_WIDTH 10
41 
42 sbRatingPropertyInfo::sbRatingPropertyInfo(const nsAString& aPropertyID,
43  const nsAString& aDisplayName,
44  const nsAString& aLocalizationKey,
45  const PRBool aRemoteReadable,
46  const PRBool aRemoteWritable,
47  const PRBool aUserViewable,
48  const PRBool aUserEditable)
49 {
50  mID = aPropertyID;
51  mDisplayName = aDisplayName;
52  mLocalizationKey = aLocalizationKey;
53  mUserViewable = aUserViewable;
54  mUserEditable = aUserEditable;
55  mRemoteReadable = aRemoteReadable;
56  mRemoteWritable = aRemoteWritable;
57  mType.AssignLiteral("rating");
58  mSuppressSelect = PR_TRUE;
59 }
60 
61 nsresult
63 {
64  nsresult rv;
65  nsAutoString op;
66  nsRefPtr<sbPropertyOperator> propOp;
67 
68  rv = sbImmutablePropertyInfo::GetOPERATOR_EQUALS(op);
69  NS_ENSURE_SUCCESS(rv, rv);
70  propOp = new sbPropertyOperator(op, NS_LITERAL_STRING("&smart.rating.equal"));
71  NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
72  rv = mOperators.AppendObject(propOp);
73  NS_ENSURE_SUCCESS(rv, rv);
74 
75  rv = sbImmutablePropertyInfo::GetOPERATOR_NOTEQUALS(op);
76  NS_ENSURE_SUCCESS(rv, rv);
77  propOp = new sbPropertyOperator(op, NS_LITERAL_STRING("&smart.rating.notequal"));
78  NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
79  rv = mOperators.AppendObject(propOp);
80  NS_ENSURE_SUCCESS(rv, rv);
81 
82  rv = sbImmutablePropertyInfo::GetOPERATOR_GREATER(op);
83  NS_ENSURE_SUCCESS(rv, rv);
84  propOp = new sbPropertyOperator(op, NS_LITERAL_STRING("&smart.rating.greater"));
85  NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
86  rv = mOperators.AppendObject(propOp);
87  NS_ENSURE_SUCCESS(rv, rv);
88 
89  rv = sbImmutablePropertyInfo::GetOPERATOR_GREATEREQUAL(op);
90  NS_ENSURE_SUCCESS(rv, rv);
91  propOp = new sbPropertyOperator(op, NS_LITERAL_STRING("&smart.rating.greaterequal"));
92  NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
93  rv = mOperators.AppendObject(propOp);
94  NS_ENSURE_SUCCESS(rv, rv);
95 
96  rv = sbImmutablePropertyInfo::GetOPERATOR_LESS(op);
97  NS_ENSURE_SUCCESS(rv, rv);
98  propOp = new sbPropertyOperator(op, NS_LITERAL_STRING("&smart.rating.less"));
99  NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
100  rv = mOperators.AppendObject(propOp);
101  NS_ENSURE_SUCCESS(rv, rv);
102 
103  rv = sbImmutablePropertyInfo::GetOPERATOR_LESSEQUAL(op);
104  NS_ENSURE_SUCCESS(rv, rv);
105  propOp = new sbPropertyOperator(op, NS_LITERAL_STRING("&smart.rating.lessequal"));
106  NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
107  rv = mOperators.AppendObject(propOp);
108  NS_ENSURE_SUCCESS(rv, rv);
109 
110  rv = sbImmutablePropertyInfo::GetOPERATOR_BETWEEN(op);
111  NS_ENSURE_SUCCESS(rv, rv);
112  propOp = new sbPropertyOperator(op, NS_LITERAL_STRING("&smart.rating.between"));
113  NS_ENSURE_TRUE(propOp, NS_ERROR_OUT_OF_MEMORY);
114  rv = mOperators.AppendObject(propOp);
115  NS_ENSURE_SUCCESS(rv, rv);
116 
117  return NS_OK;
118 }
119 
120 nsresult
122 {
123  nsresult rv;
124 
126  NS_ENSURE_SUCCESS(rv, rv);
127 
128  rv = InitializeOperators();
129  NS_ENSURE_SUCCESS(rv, rv);
130 
131  return NS_OK;
132 }
133 
134 // sbITreeViewPropertyInfo
135 
136 NS_IMETHODIMP
137 sbRatingPropertyInfo::GetImageSrc(const nsAString& aValue,
138  nsAString& _retval)
139 {
140  _retval.Truncate();
141  return NS_OK;
142 }
143 
144 NS_IMETHODIMP
145 sbRatingPropertyInfo::GetProgressMode(const nsAString& aValue,
146  PRInt32* _retval)
147 {
148  NS_ENSURE_ARG_POINTER(_retval);
149  *_retval = nsITreeView::PROGRESS_NONE;
150  return NS_OK;
151 }
152 
153 NS_IMETHODIMP
154 sbRatingPropertyInfo::GetCellValue(const nsAString& aValue,
155  nsAString& _retval)
156 {
157  _retval.Truncate();
158  return NS_OK;
159 }
160 
161 NS_IMETHODIMP
162 sbRatingPropertyInfo::GetRowProperties(const nsAString& aValue,
163  nsAString& _retval)
164 {
165  _retval.Truncate();
166  return NS_OK;
167 }
168 
169 NS_IMETHODIMP
170 sbRatingPropertyInfo::GetCellProperties(const nsAString& aValue,
171  nsAString& _retval)
172 {
173  _retval.AssignLiteral("rating rating");
174  _retval.Append(aValue);
175  return NS_OK;
176 }
177 
178 NS_IMETHODIMP
179 sbRatingPropertyInfo::GetColumnType(nsAString& _retval)
180 {
181  _retval.AssignLiteral("text");
182  return NS_OK;
183 }
184 
185 // sbIClickablePropertyInfo
186 
187 NS_IMETHODIMP
188 sbRatingPropertyInfo::GetSuppressSelect(PRBool* aSuppressSelect)
189 {
190  NS_ENSURE_ARG_POINTER(aSuppressSelect);
191  *aSuppressSelect = mSuppressSelect;
192  return NS_OK;
193 }
194 
195 NS_IMETHODIMP
196 sbRatingPropertyInfo::SetSuppressSelect(PRBool aSuppressSelect)
197 {
198  mSuppressSelect = aSuppressSelect;
199  return NS_OK;
200 }
201 
202 NS_IMETHODIMP
203 sbRatingPropertyInfo::IsDisabled(const nsAString& aCurrentValue,
204  PRBool* _retval)
205 {
206  NS_ENSURE_ARG_POINTER(_retval);
207  *_retval = PR_FALSE;
208  return NS_OK;
209 }
210 
211 NS_IMETHODIMP
212 sbRatingPropertyInfo::HitTest(const nsAString& aCurrentValue,
213  const nsAString& aPart,
214  PRUint32 aBoxWidth,
215  PRUint32 aBoxHeight,
216  PRUint32 aMouseX,
217  PRUint32 aMouseY,
218  PRBool* _retval)
219 {
220  NS_ENSURE_ARG_POINTER(_retval);
221  *_retval = PR_TRUE;
222  return NS_OK;
223 }
224 
225 NS_IMETHODIMP
226 sbRatingPropertyInfo::GetValueForClick(const nsAString& aCurrentValue,
227  PRUint32 aBoxWidth,
228  PRUint32 aBoxHeight,
229  PRUint32 aMouseX,
230  PRUint32 aMouseY,
231  nsAString& _retval)
232 {
233  PRUint32 rating;
234 
235  // Magical number that allows the user to click to the left of the first
236  // star to set the rating to zero
237  if (aMouseX < ZERO_HIT_WIDTH) {
238  rating = 0;
239  }
240  else {
241  rating = ((aMouseX - ZERO_HIT_WIDTH) / STAR_WIDTH) + 1;
242 
243  if (rating > MAX_RATING) {
244  rating = MAX_RATING;
245  }
246  }
247 
248  nsString ratingStr;
249  ratingStr.AppendInt(rating);
250 
251  if (ratingStr.Equals(aCurrentValue) || ratingStr.EqualsLiteral("0")) {
252  ratingStr.SetIsVoid(PR_TRUE);
253  }
254 
255  _retval = ratingStr;
256  return NS_OK;
257 }
258 
259 /* boolean onClick (in sbIMediaItem aItem,
260  [optional] in nsISupports aEvent,
261  [optional] in nsISupports aContext); */
262 NS_IMETHODIMP
263 sbRatingPropertyInfo::OnClick(sbIMediaItem *aItem,
264  nsISupports *aEvent,
265  nsISupports *aContext,
266  PRBool *_retval NS_OUTPARAM)
267 {
268  NS_ENSURE_ARG_POINTER(_retval);
269  *_retval = PR_FALSE;
270  return NS_OK;
271 }
272 
273 // sbIPropertyInfo
274 
275 NS_IMETHODIMP
276 sbRatingPropertyInfo::Format(const nsAString& aValue,
277  nsAString& _retval)
278 {
279  // XXXsteve Pretend that zeros are nulls. This should be removed when
280  // bug 8033 is fixed.
281  if (aValue.EqualsLiteral("0")) {
282  _retval.SetIsVoid(PR_TRUE);
283  }
284  else {
285  _retval = aValue;
286  }
287 
288  return NS_OK;
289 }
290 
291 NS_IMETHODIMP
292 sbRatingPropertyInfo::Validate(const nsAString& aValue, PRBool* _retval)
293 {
294  // Value can only be 1 through MAX_RATING or null
295  *_retval = PR_TRUE;
296  if (aValue.IsVoid()) {
297  return NS_OK;
298  }
299 
300  nsresult rv;
301  PRUint32 rating = aValue.ToInteger(&rv);
302  if (NS_SUCCEEDED(rv) && rating >= 0 && rating <= MAX_RATING) {
303  return NS_OK;
304  }
305 
306  *_retval = PR_FALSE;
307  return NS_OK;
308 }
return NS_OK
#define STAR_WIDTH
dataSBHighestRatedArtists SBProperties rating
Definition: tuner2.js:867
onPageChanged aValue
Definition: FeedWriter.js:1395
#define MAX_RATING
The property information specific to clickable properties.
NS_IMETHOD Validate(const nsAString &aValue, PRBool *_retval)
#define ZERO_HIT_WIDTH
nsCOMArray< sbIPropertyOperator > mOperators
NS_IMPL_ISUPPORTS_INHERITED2(sbRatingPropertyInfo, sbImmutablePropertyInfo, sbIClickablePropertyInfo, sbITreeViewPropertyInfo) sbRatingPropertyInfo
NS_DECL_ISUPPORTS_INHERITED NS_DECL_SBICLICKABLEPROPERTYINFO NS_DECL_SBITREEVIEWPROPERTYINFO sbRatingPropertyInfo(const nsAString &aPropertyID, const nsAString &aDisplayName, const nsAString &aLocalizationKey, const PRBool aRemoteReadable, const PRBool aRemoteWritable, const PRBool aUserViewable, const PRBool aUserEditable)
Interface that defines a single item of media in the system.
NS_IMETHOD Format(const nsAString &aValue, nsAString &_retval)