sbIPDSysDevice.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //=BEGIN SONGBIRD GPL
5 //
6 // This file is part of the Songbird web player.
7 //
8 // Copyright(c) 2005-2009 POTI, Inc.
9 // http://www.songbirdnest.com
10 //
11 // This file may be licensed under the terms of of the
12 // GNU General Public License Version 2 (the GPL).
13 //
14 // Software distributed under the License is distributed
15 // on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either
16 // express or implied. See the GPL for the specific language
17 // governing rights and limitations.
18 //
19 // You should have received a copy of the GPL along with this
20 // program. If not, go to http://www.gnu.org/licenses/gpl.html
21 // or write to the Free Software Foundation, Inc.,
22 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 //
24 //=END SONGBIRD GPL
25 */
26 
27 //------------------------------------------------------------------------------
28 //
29 // iPod system dependent device services.
30 //
31 //------------------------------------------------------------------------------
32 
38 //------------------------------------------------------------------------------
39 //
40 // iPod system dependent device imported services.
41 //
42 //------------------------------------------------------------------------------
43 
44 // Self imports.
45 #include "sbIPDSysDevice.h"
46 
47 // Local imports.
48 #include "sbIPDLog.h"
49 
50 // Mozilla imports.
51 #include <nsIPropertyBag.h>
52 #include <nsIPropertyBag2.h>
53 #include <nsIWritablePropertyBag.h>
54 
55 // Songbird imports
57 #include <sbDebugUtils.h>
58 
59 // System imports.
60 #include <ctype.h> // for isxdigit
61 
62 
63 //------------------------------------------------------------------------------
64 //
65 // iPod system dependent device sbIDevice services.
66 //
67 //------------------------------------------------------------------------------
68 
73 NS_IMETHODIMP
75 {
76  nsresult rv;
77 
78  // call the parent
79  rv = sbIPDDevice::Eject();
80  NS_ENSURE_SUCCESS(rv, rv);
81 
82  // Unmount and eject.
83  rv = mSBLibHalCtx->DeviceVolumeUnmount(mMediaPartUDI);
84  NS_ENSURE_SUCCESS(rv, rv);
85  rv = mSBLibHalCtx->DeviceVolumeEject(mMediaPartUDI);
86  NS_ENSURE_SUCCESS(rv, rv);
87 
88  return NS_OK;
89 }
90 
91 
92 //------------------------------------------------------------------------------
93 //
94 // iPod system dependent device services
95 //
96 //------------------------------------------------------------------------------
97 
105 sbIPDSysDevice::sbIPDSysDevice(const nsID& aControllerID,
106  nsIPropertyBag* aProperties) :
107  sbIPDDevice(aControllerID, aProperties),
108  mProperties(aProperties),
109  mSBLibHalCtx(nsnull)
110 {
112 
113  // Log progress.
114  LOG("Enter: sbIPDSysDevice::sbIPDSysDevice\n");
115 
116  // Validate parameters.
117  NS_ASSERTION(aProperties, "aProperties is null");
118 }
119 
120 
126 {
127  // Log progress.
128  LOG("Enter: sbIPDSysDevice::~sbIPDSysDevice\n");
129 
130  // Finalize the iPod system dependent device object.
131  Finalize();
132 }
133 
134 
139 nsresult
141 {
142  nsresult rv;
143 
144  // Initialize the HAL library context.
145  mSBLibHalCtx = new sbLibHalCtx();
146  NS_ENSURE_TRUE(mSBLibHalCtx, NS_ERROR_OUT_OF_MEMORY);
147  rv = mSBLibHalCtx->Initialize();
148  NS_ENSURE_SUCCESS(rv, rv);
149 
150  // Get the device properties.
151  nsCOMPtr<nsIPropertyBag2> properties = do_QueryInterface(mProperties, &rv);
152  NS_ENSURE_SUCCESS(rv, rv);
153  nsCOMPtr<nsIWritablePropertyBag> writeProperties =
154  do_QueryInterface(mProperties, &rv);
155  NS_ENSURE_SUCCESS(rv, rv);
156 
157  // Get the device and media partition UDIs.
158  rv = properties->GetPropertyAsACString(NS_LITERAL_STRING("DeviceUDI"),
159  mDeviceUDI);
160  NS_ENSURE_SUCCESS(rv, rv);
161  rv = properties->GetPropertyAsACString(NS_LITERAL_STRING("MediaPartitionUDI"),
162  mMediaPartUDI);
163  NS_ENSURE_SUCCESS(rv, rv);
164 
165  // Add the device mount path property.
166  nsAutoString mountPath;
167  rv = GetMountPath(mountPath);
168  NS_ENSURE_SUCCESS(rv, rv);
169  rv = writeProperties->SetProperty(NS_LITERAL_STRING("MountPath"),
170  sbIPDVariant(mountPath).get());
171  NS_ENSURE_SUCCESS(rv, rv);
172 
173  // Add the device manufacturer and model number properties.
174  rv = writeProperties->SetProperty
175  (NS_LITERAL_STRING(SB_DEVICE_PROPERTY_MANUFACTURER),
176  sbIPDVariant("Apple").get());
177  NS_ENSURE_SUCCESS(rv, rv);
178  rv = writeProperties->SetProperty(NS_LITERAL_STRING(SB_DEVICE_PROPERTY_MODEL),
179  sbIPDVariant("iPod").get());
180  NS_ENSURE_SUCCESS(rv, rv);
181 
182  // Get the Firewire GUID property.
183  nsAutoString firewireGUID;
184  rv = GetFirewireGUID(firewireGUID);
185  NS_ENSURE_SUCCESS(rv, rv);
186  rv = writeProperties->SetProperty(NS_LITERAL_STRING("FirewireGUID"),
187  sbIPDVariant(firewireGUID).get());
188  NS_ENSURE_SUCCESS(rv, rv);
189 
190  // Add the device serial number property.
191  //XXXeps use Firewire GUID for now.
192  rv = writeProperties->SetProperty
193  (NS_LITERAL_STRING(SB_DEVICE_PROPERTY_SERIAL_NUMBER),
194  sbIPDVariant(firewireGUID).get());
195  NS_ENSURE_SUCCESS(rv, rv);
196 
197  // Hey, if it's a read-only hfsplus filesystem then it's almost certainly
198  // journalled
199  nsCAutoString fstype;
200  rv = mSBLibHalCtx->DeviceGetPropertyString(mMediaPartUDI,
201  "volume.fstype",
202  fstype);
203  NS_ENSURE_SUCCESS(rv, rv);
204  PRBool readonly;
205  rv = mSBLibHalCtx->DeviceGetPropertyBool(mMediaPartUDI,
206  "volume.is_mounted_read_only",
207  &readonly);
208  NS_ENSURE_SUCCESS(rv, rv);
209  if (fstype.EqualsLiteral("hfsplus") && readonly) {
210  rv = writeProperties->SetProperty(NS_LITERAL_STRING("HFSPlusReadOnly"),
211  sbIPDVariant(PR_TRUE).get());
212  }
213 
214  // Initialize the iPod device object.
216  NS_ENSURE_SUCCESS(rv, rv);
217 
218  return NS_OK;
219 }
220 
221 
226 void
228 {
229  // Finalize the iPod device object.
231 
232  // Dispose of the HAL library context.
233  if (mSBLibHalCtx)
234  delete mSBLibHalCtx;
235  mSBLibHalCtx = nsnull;
236 }
237 
238 
239 //------------------------------------------------------------------------------
240 //
241 // Internal iPod system dependent device services
242 //
243 //------------------------------------------------------------------------------
244 
251 nsresult
252 sbIPDSysDevice::GetMountPath(nsAString& aMountPath)
253 {
254  nsresult rv;
255 
256  // Get the media partition mount point.
257  nsCAutoString mountPoint;
258  rv = mSBLibHalCtx->DeviceGetPropertyString(mMediaPartUDI,
259  "volume.mount_point",
260  mountPoint);
261  NS_ENSURE_SUCCESS(rv, rv);
262 
263  // Return results.
264  aMountPath.Assign(NS_ConvertUTF8toUTF16(mountPoint));
265 
266  return NS_OK;
267 }
268 
269 
276 nsresult
277 sbIPDSysDevice::GetFirewireGUID(nsAString& aFirewireGUID)
278 {
279  nsresult rv;
280 
281  // Get the device storage serial number.
282  nsCAutoString storageSerial;
283  rv = mSBLibHalCtx->DeviceGetPropertyString(mDeviceUDI,
284  "storage.serial",
285  storageSerial);
286  NS_ENSURE_SUCCESS(rv, rv);
287 
288  // Parse the Firewire GUID from the device storage serial number.
289  nsCString firewireGUID;
290  for (PRUint32 i = 0;
291  (i < storageSerial.Length()) && (firewireGUID.Length() < 16);
292  i++) {
293  char guidChar = storageSerial.CharAt(i);
294  if (isxdigit(guidChar))
295  firewireGUID += guidChar;
296  else
297  firewireGUID.Truncate();
298  }
299  NS_ENSURE_TRUE(firewireGUID.Length() == 16, NS_ERROR_FAILURE);
300 
301  // Return results.
302  aFirewireGUID.Assign(NS_ConvertUTF8toUTF16(firewireGUID));
303 
304  return NS_OK;
305 }
306 
307 
#define SB_PRLOG_SETUP(x)
Definition: sbDebugUtils.h:115
return NS_OK
#define LOG(args)
nsresult DeviceVolumeEject(nsCString &aDeviceUDI)
Definition: sbLibHal.cpp:679
nsCOMPtr< nsIArray > mProperties
#define SB_DEVICE_PROPERTY_MANUFACTURER
sbIPDSysDevice(const nsID &aControllerID, nsIPropertyBag *aProperties)
NS_IMETHOD Eject(void)
Songbird iPod Device Logging Definitions.
nsresult DeviceVolumeUnmount(nsCString &aDeviceUDI)
Definition: sbLibHal.cpp:697
readonly attribute sbIDeviceProperties properties
Definition: sbIDevice.idl:168
nsresult Eject()
#define SB_DEVICE_PROPERTY_SERIAL_NUMBER
nsresult DeviceGetPropertyBool(const nsACString &aUDI, const char *aKey, PRBool *aProperty)
Definition: sbLibHal.cpp:399
nsresult Initialize()
nsresult Initialize()
#define SB_DEVICE_PROPERTY_MODEL
nsresult Initialize()
Definition: sbLibHal.cpp:101
_getSelectedPageStyle s i
nsresult DeviceGetPropertyString(const nsACString &aUDI, const char *aKey, nsCString &aProperty)
Definition: sbLibHal.cpp:306