sbDeviceProperties.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 "sbDeviceProperties.h"
28 
30 
31 #include <nsAutoLock.h>
32 #include <nsComponentManagerUtils.h>
33 #include <nsISimpleEnumerator.h>
34 #include <nsIProperty.h>
35 #include <prlog.h>
36 #include <prprf.h>
37 #include <prtime.h>
38 
40 
45 #ifdef PR_LOGGING
46 static PRLogModuleInfo* gDevicePropertiesLog = nsnull;
47 #define TRACE(args) PR_LOG(gDevicePropertiesLog, PR_LOG_DEBUG, args)
48 #define LOG(args) PR_LOG(gDevicePropertiesLog, PR_LOG_WARN, args)
49 #else
50 #define TRACE(args) /* nothing */
51 #define LOG(args) /* nothing */
52 #endif /* PR_LOGGING */
53 
55 isInitialized(PR_FALSE)
56 {
57 #ifdef PR_LOGGING
58  if (!gDevicePropertiesLog) {
59  gDevicePropertiesLog = PR_NewLogModule("sbDeviceProperties");
60  }
61 #endif
62  mLock = nsAutoLock::NewLock("sbDevicePropertiesLock");
63  // Intialize our properties container
64  mProperties2 =
65  do_CreateInstance("@songbirdnest.com/moz/xpcom/sbpropertybag;1");
66  mProperties = do_QueryInterface(mProperties2);
67 
68  TRACE(("sbDeviceProperties[0x%.8x] - Constructed", this));
69 
70 }
71 
72 sbDeviceProperties::~sbDeviceProperties()
73 {
74  TRACE(("sbDeviceProperties[0x%.8x] - Destructed", this));
75  if (mLock) {
76  nsAutoLock::DestroyLock(mLock);
77  mLock = nsnull;
78  }
79 }
80 
81 NS_IMETHODIMP
82 sbDeviceProperties::InitFriendlyName(const nsAString & aFriendlyName)
83 {
84  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
85  nsresult rv =
86  mProperties2->SetPropertyAsAString(NS_LITERAL_STRING(SB_DEVICE_PROPERTY_NAME),
87  aFriendlyName);
88  NS_ENSURE_SUCCESS(rv, rv);
89 
90  return NS_OK;
91 }
92 
93 NS_IMETHODIMP
94 sbDeviceProperties::InitDefaultName(const nsAString & aDefaultName)
95 {
96  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
97  nsresult rv =
98  mProperties2->SetPropertyAsAString(
99  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_DEFAULT_NAME),
100  aDefaultName);
101  NS_ENSURE_SUCCESS(rv, rv);
102 
103  return NS_OK;
104 }
105 
106 NS_IMETHODIMP
107 sbDeviceProperties::InitVendorName(const nsAString & aVendorName)
108 {
109  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
110 
111  nsresult rv =
112  mProperties2->SetPropertyAsAString(NS_LITERAL_STRING(SB_DEVICE_PROPERTY_MANUFACTURER),
113  aVendorName);
114  NS_ENSURE_SUCCESS(rv, rv);
115 
116  return NS_OK;
117 }
118 
119 NS_IMETHODIMP
120 sbDeviceProperties::InitModelNumber(nsIVariant *aModelNumber)
121 {
122  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
123 
124  nsresult rv =
125  mProperties->SetProperty(NS_LITERAL_STRING(SB_DEVICE_PROPERTY_MODEL),
126  aModelNumber);
127  NS_ENSURE_SUCCESS(rv, rv);
128 
129  return NS_OK;
130 }
131 
132 NS_IMETHODIMP
133 sbDeviceProperties::InitSerialNumber(nsIVariant *aSerialNumber)
134 {
135  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
136 
137  nsresult rv =
138  mProperties->SetProperty(
139  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_SERIAL_NUMBER),
140  aSerialNumber);
141  NS_ENSURE_SUCCESS(rv, rv);
142 
143 return NS_OK;
144 }
145 
146 NS_IMETHODIMP
147 sbDeviceProperties::InitFirmwareVersion(const nsAString &aFirmwareVersion)
148 {
149  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
150 
151  nsresult rv =
152  mProperties2->SetPropertyAsAString(
153  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_FIRMWARE_VERSION),
154  aFirmwareVersion);
155  NS_ENSURE_SUCCESS(rv, rv);
156 
157  return NS_OK;
158 }
159 
160 NS_IMETHODIMP
161 sbDeviceProperties::InitDeviceLocation(nsIURI *aDeviceLocationUri)
162 {
163  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
164  mDeviceLocation = aDeviceLocationUri;
165  return NS_OK;
166 }
167 
168 NS_IMETHODIMP
169 sbDeviceProperties::InitDeviceIcon(nsIURI *aDeviceIconUri)
170 {
171  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
172  mDeviceIcon = aDeviceIconUri;
173  return NS_OK;
174 }
175 
176 NS_IMETHODIMP
177 sbDeviceProperties::InitDeviceProperties(nsIPropertyBag2 *aProperties)
178 {
179  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
180  NS_ENSURE_ARG_POINTER(aProperties);
181 
182  nsresult rv;
183 
184  // Copy the properties since the other init methods may have
185  // been called
186  nsCOMPtr<nsISimpleEnumerator> enumerator;
187  rv = aProperties->GetEnumerator(getter_AddRefs(enumerator));
188  NS_ENSURE_SUCCESS(rv, rv);
189 
190  nsCOMPtr<nsIProperty> property;
191  nsString name;
192  nsCOMPtr<nsIVariant> value;
193 
194  PRBool more;
195  while (NS_SUCCEEDED(enumerator->HasMoreElements(&more)) && more) {
196  rv = enumerator->GetNext(getter_AddRefs(property));
197  NS_ENSURE_SUCCESS(rv, rv);
198 
199  rv = property->GetName(name);
200  NS_ENSURE_SUCCESS(rv, rv);
201 
202  rv = property->GetValue(getter_AddRefs(value));
203  NS_ENSURE_SUCCESS(rv, rv);
204 
205  rv = mProperties->SetProperty(name, value);
206  NS_ENSURE_SUCCESS(rv, rv);
207  }
208  return NS_OK;
209 }
210 
211 NS_IMETHODIMP
212 sbDeviceProperties::InitDone()
213 {
214  NS_ENSURE_TRUE(!isInitialized, NS_ERROR_ALREADY_INITIALIZED);
215 
216  isInitialized = PR_TRUE;
217  return NS_OK;
218 }
219 
220 
221 static nsresult
222 GetProperty(nsIPropertyBag2 * aProperties,
223  nsAString const & aProp,
224  nsAString & aValue)
225 {
226  nsString value;
227  value.SetIsVoid(PR_TRUE);
228 
229  nsresult rv = aProperties->GetPropertyAsAString(aProp,
230  value);
231  if (rv != NS_ERROR_NOT_AVAILABLE) {
232  NS_ENSURE_SUCCESS(rv, rv);
233  }
234  aValue = value;
235  return NS_OK;
236 }
237 
238 static nsresult
239 GetProperty(nsIPropertyBag * aProperties,
240  nsAString const & aProp,
241  nsIVariant ** aValue)
242 {
243  nsresult rv = aProperties->GetProperty(aProp, aValue);
244  if (rv == NS_ERROR_NOT_AVAILABLE) {
245  *aValue = nsnull;
246  }
247  else {
248  NS_ENSURE_SUCCESS(rv, rv);
249  }
250  return NS_OK;
251 }
252 
253 NS_IMETHODIMP
254 sbDeviceProperties::GetFriendlyName(nsAString & aFriendlyName)
255 {
256  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
257 
258  nsAutoLock lock(mLock);
259  return GetProperty(mProperties2,
260  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_NAME),
261  aFriendlyName);
262 }
263 
264 NS_IMETHODIMP
265 sbDeviceProperties::SetFriendlyName(const nsAString & aFriendlyName)
266 {
267  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
268 
269  nsAutoLock lock(mLock);
270  nsresult rv =
271  mProperties2->SetPropertyAsAString(NS_LITERAL_STRING(SB_DEVICE_PROPERTY_NAME),
272  aFriendlyName);
273  NS_ENSURE_SUCCESS(rv, rv);
274 
275  return NS_OK;
276 }
277 
278 NS_IMETHODIMP
279 sbDeviceProperties::GetDefaultName(nsAString & aDefaultName)
280 {
281  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
282 
283  nsAutoLock lock(mLock);
284  return GetProperty(mProperties2,
285  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_DEFAULT_NAME),
286  aDefaultName);
287 }
288 
289 NS_IMETHODIMP
290 sbDeviceProperties::GetVendorName(nsAString & aVendorName)
291 {
292  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
293 
294  nsAutoLock lock(mLock);
295 
296  return GetProperty(mProperties2,
297  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_MANUFACTURER),
298  aVendorName);;
299 }
300 
301 NS_IMETHODIMP
302 sbDeviceProperties::GetModelNumber(nsIVariant * *aModelNumber)
303 {
304  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
305  NS_ENSURE_ARG_POINTER(aModelNumber);
306 
307  nsAutoLock lock(mLock);
308 
309  return GetProperty(mProperties,
310  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_MODEL),
311  aModelNumber);
312 }
313 
314 NS_IMETHODIMP
315 sbDeviceProperties::GetSerialNumber(nsIVariant * *aSerialNumber)
316 {
317  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
318  NS_ENSURE_ARG_POINTER(aSerialNumber);
319 
320  nsAutoLock lock(mLock);
321 
322  return GetProperty(mProperties,
323  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_SERIAL_NUMBER),
324  aSerialNumber);
325 }
326 
327 NS_IMETHODIMP
328 sbDeviceProperties::GetFirmwareVersion(nsAString &aFirmwareVersion)
329 {
330  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
331 
332  nsAutoLock lock(mLock);
333 
334  return GetProperty(mProperties2,
335  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_FIRMWARE_VERSION),
336  aFirmwareVersion);
337 }
338 
339 NS_IMETHODIMP
340 sbDeviceProperties::GetUri(nsIURI * *aUri)
341 {
342  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
343  NS_ENSURE_ARG_POINTER(aUri);
344 
345  nsAutoLock lock(mLock);
346 
347  NS_IF_ADDREF( *aUri = mDeviceLocation );
348  return NS_OK;
349 }
350 
351 NS_IMETHODIMP
352 sbDeviceProperties::GetIconUri(nsIURI * *aIconUri)
353 {
354  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
355  NS_ENSURE_ARG_POINTER(aIconUri);
356 
357  nsAutoLock lock(mLock);
358 
359  NS_IF_ADDREF( *aIconUri = mDeviceIcon );
360  return NS_OK;
361 }
362 
363 NS_IMETHODIMP
364 sbDeviceProperties::GetProperties(nsIPropertyBag2 * *aProperties)
365 {
366  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
367  NS_ENSURE_ARG_POINTER(aProperties);
368 
369  nsAutoLock lock(mLock);
370 
371  NS_IF_ADDREF( *aProperties = mProperties2 );
372  return NS_OK;
373 }
374 
375 NS_IMETHODIMP
376 sbDeviceProperties::SetHidden(PRBool aHidden)
377 {
378  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
379 
380  nsAutoLock lock(mLock);
381  nsresult rv =
382  mProperties2->SetPropertyAsBool(NS_LITERAL_STRING(SB_DEVICE_PROPERTY_HIDDEN),
383  aHidden);
384  NS_ENSURE_SUCCESS(rv, rv);
385 
386  return NS_OK;
387 }
388 
389 NS_IMETHODIMP
390 sbDeviceProperties::GetHidden(PRBool *aHidden)
391 {
392  NS_ENSURE_TRUE(isInitialized, NS_ERROR_NOT_INITIALIZED);
393  NS_ENSURE_ARG_POINTER(aHidden);
394 
395  nsAutoLock lock(mLock);
396  nsresult rv = mProperties2->GetPropertyAsBool(
397  NS_LITERAL_STRING(SB_DEVICE_PROPERTY_HIDDEN),
398  aHidden);
399  NS_ENSURE_SUCCESS(rv, rv);
400 
401  return NS_OK;
402 }
403 
return NS_OK
onPageChanged aValue
Definition: FeedWriter.js:1395
#define SB_DEVICE_PROPERTY_HIDDEN
#define TRACE(args)
#define SB_DEVICE_PROPERTY_MANUFACTURER
#define SB_DEVICE_PROPERTY_DEFAULT_NAME
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
static nsresult GetProperty(nsIPropertyBag2 *aProperties, nsAString const &aProp, nsAString &aValue)
nsCOMPtr< nsIURI > mDeviceLocation
#define SB_DEVICE_PROPERTY_FIRMWARE_VERSION
nsCOMPtr< nsIWritablePropertyBag > mProperties
#define SB_DEVICE_PROPERTY_NAME
nsCOMPtr< nsIURI > mDeviceIcon
nsCOMPtr< nsIWritablePropertyBag2 > mProperties2
countRef value
Definition: FeedWriter.js:1423
#define SB_DEVICE_PROPERTY_SERIAL_NUMBER
#define SB_DEVICE_PROPERTY_MODEL
PRPackedBool isInitialized
NS_DECL_ISUPPORTS NS_DECL_SBIDEVICEPROPERTIES sbDeviceProperties()