sbIPDStatus.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-2011 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 device status services.
30 //
31 // These services may only be used within the request lock.
32 //
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // iPod device status imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Self imports.
47 #include "sbIPDStatus.h"
48 
49 // Local imports.
50 #include "sbIPDDevice.h"
51 #include "sbIPDLog.h"
52 #include "sbIPDUtils.h"
53 
54 // Songbird imports.
55 #include <sbIDeviceEvent.h>
56 #include "sbRequestItem.h"
57 
58 #include <sbDebugUtils.h>
59 
60 static const char* SB_UNUSED_IN_RELEASE(gStateStrings[]) =
61 { "IDLE", "SYNCING", "COPYING",
62  "DELETING", "UPDATING", "MOUNTING", "DOWNLOADING", "UPLOADING",
63  "DOWNLOAD_PAUSED", "UPLOAD_PAUSED", "DISCONNECTED", "BUSY" };
64 #define STATE_STRING(X) \
65  (((X)>=0 && (X)<(sizeof(gStateStrings)/sizeof(gStateStrings[0]))) ? \
66  gStateStrings[(X)] : "unknown")
67 
68 
69 //------------------------------------------------------------------------------
70 //
71 // iPod device status Constructors/destructors/initializers/finalizers.
72 //
73 // These services may be used outside of the request lock.
74 //
75 //------------------------------------------------------------------------------
76 
84  mDevice(aDevice),
85  mOperationType(OPERATION_TYPE_NONE)
86 {
87  // Validate arguments.
88  NS_ASSERTION(aDevice, "aDevice is null");
89 
91 }
92 
93 
99 {
100 }
101 
102 
107 nsresult
109 {
110  nsresult rv;
111 
112  // Get the device ID and set it up for auto-disposal.
113  nsID* deviceID;
114  rv = mDevice->GetId(&deviceID);
115  NS_ENSURE_SUCCESS(rv, rv);
116  sbAutoNSMemPtr autoDeviceID(deviceID);
117 
118  // Create the base device status object.
119  char deviceIDString[NSID_LENGTH];
120  deviceID->ToProvidedString(deviceIDString);
121  mStatus = do_CreateInstance("@songbirdnest.com/Songbird/Device/DeviceStatus;1", &rv);
122  NS_ENSURE_SUCCESS(rv, rv);
123 
124  // Initialize the base status object
125  char idString[NSID_LENGTH];
126  deviceID->ToProvidedString(idString);
127  rv = mStatus->Init(NS_ConvertASCIItoUTF16(idString, NSID_LENGTH-1));
128  NS_ENSURE_SUCCESS(rv, rv);
129 
130  // set the state to idle
132 
133  return NS_OK;
134 }
135 
136 
141 void
143 {
144  // Remove object references.
145  mDevice = nsnull;
146  mStatus = nsnull;
147  mMediaList = nsnull;
148  mMediaItem = nsnull;
149 }
150 
151 
152 //------------------------------------------------------------------------------
153 //
154 // iPod device status operation services.
155 //
156 //------------------------------------------------------------------------------
157 
167 void
168 sbIPDStatus::OperationStart(PRUint32 aOperationType,
170  PRUint32 aBatchCount)
171 {
172  // Validate arguments.
173  NS_ENSURE_TRUE(aRequest, /* void */);
174 
175  if (aRequest) {
176  // Update the current media item and list.
177  mMediaList = aRequest->list;
178  mMediaItem = aRequest->item;
179  }
180 
181  const PRInt32 batchIndex = aRequest->GetBatchIndex() + 1;
182 
183  // Update the current operation type.
184  mOperationType = aOperationType;
185 
186  // Dispatch operation dependent status processing.
187  switch (mOperationType)
188  {
189  case OPERATION_TYPE_MOUNT :
190  FIELD_LOG(("Starting mount operation.\n"));
191  UpdateStatus(sbIDevice::STATE_MOUNTING, NS_LITERAL_STRING("mounting"),
192  NS_LITERAL_STRING("mounting"), batchIndex, aBatchCount, 0.0);
193  mDevice->CreateAndDispatchEvent
195  sbIPDVariant(NS_ISUPPORTS_CAST(sbIDevice*, mDevice)).get());
196  break;
197 
198  case OPERATION_TYPE_WRITE :
199  FIELD_LOG(("Starting write track operation.\n"));
200  UpdateStatus(sbIDevice::STATE_COPYING, NS_LITERAL_STRING("copying"),
201  NS_LITERAL_STRING("copying"), batchIndex, aBatchCount, 0.0);
202  mDevice->CreateAndDispatchEvent
204  sbIPDVariant(mMediaItem).get());
205  break;
206 
207  case OPERATION_TYPE_DELETE :
208  FIELD_LOG(("Starting delete operation.\n"));
209  UpdateStatus(sbIDevice::STATE_DELETING, NS_LITERAL_STRING("deleting"),
210  NS_LITERAL_STRING("deleting"), batchIndex, aBatchCount, 0.0);
211  mDevice->CreateAndDispatchEvent
213  sbIPDVariant(mMediaItem).get());
214  break;
215 
216  default :
217  break;
218  }
219 }
220 
221 
229 void
231 {
232 
233  nsString stateMessage;
234  // Update device status.
235  if (NS_SUCCEEDED(aResult)) {
236  stateMessage.AssignLiteral("Completed");
237  } else {
238  stateMessage.AssignLiteral("Failed");
239  }
240 
241  // Dispatch operation dependent status processing.
242  switch(mOperationType)
243  {
244  case OPERATION_TYPE_MOUNT :
245  FIELD_LOG(("Completed mount operation.\n"));
246  UpdateStatus(sbIDevice::STATE_MOUNTING, NS_LITERAL_STRING("mount"), stateMessage,
247  0, 0, 100.0);
248  mDevice->CreateAndDispatchEvent
250  sbIPDVariant(NS_ISUPPORTS_CAST(sbIDevice*, mDevice)).get());
251  break;
252 
253  case OPERATION_TYPE_WRITE :
254  FIELD_LOG(("Completed write track operation.\n"));
255  UpdateStatus(sbIDevice::STATE_COPYING, NS_LITERAL_STRING("write"), stateMessage,
256  0, 0, 100.0);
257  mDevice->CreateAndDispatchEvent
259  sbIPDVariant(mMediaItem).get());
260  break;
261 
262  case OPERATION_TYPE_DELETE :
263  FIELD_LOG(("Completed delete track operation.\n"));
264  UpdateStatus(sbIDevice::STATE_DELETING, NS_LITERAL_STRING("delete"), stateMessage,
265  0, 0, 100.0);
266  mDevice->CreateAndDispatchEvent
268  sbIPDVariant(mMediaItem).get());
269  break;
270 
271  default :
272  break;
273  }
274 
275  // Clear the current operation state.
276  mOperationType = OPERATION_TYPE_NONE;
277  mMediaList = nsnull;
278  mMediaItem = nsnull;
279 }
280 
281 
282 //------------------------------------------------------------------------------
283 //
284 // iPod device status item services.
285 //
286 //------------------------------------------------------------------------------
287 
298 void
299 sbIPDStatus::ItemStart(PRInt32 aItemNum,
300  PRInt32 aItemCount)
301 {
302  // Dispatch operation dependent status processing.
303  switch(mOperationType)
304  {
305  case OPERATION_TYPE_MOUNT :
306  FIELD_LOG(("Starting mount item.\n"));
307  UpdateStatus(sbIDevice::STATE_MOUNTING, NS_LITERAL_STRING("mount"),
308  NS_LITERAL_STRING("mount"), aItemNum, aItemCount, 0.0);
309  mDevice->CreateAndDispatchEvent
311  sbIPDVariant(NS_ISUPPORTS_CAST(sbIDevice*, mDevice)).get());
312  break;
313 
314  case OPERATION_TYPE_WRITE :
315  FIELD_LOG(("Starting write track item %d %d.\n", aItemNum, aItemCount));
316  /*XXXeps what to do with "Starting" state?*/
317  UpdateStatus(sbIDevice::STATE_COPYING, NS_LITERAL_STRING("write"),
318  NS_LITERAL_STRING("InProgress"), aItemNum, aItemCount, 0.0);
319  mDevice->CreateAndDispatchEvent
321  sbIPDVariant(mMediaItem).get());
322  break;
323 
324  default :
325  break;
326  }
327 }
328 
329 
341 void
342 sbIPDStatus::ItemStart(Itdb_Track* aTrack,
343  PRInt32 aItemNum,
344  PRInt32 aItemCount)
345 {
346  // Validate arguments.
347  NS_ENSURE_TRUE(aTrack, /* void */);
348 
349  // Process item start status.
350  ItemStart(aItemNum, aItemCount);
351 }
352 
353 
367 void
369  Itdb_Track* aTrack,
370  PRInt32 aItemNum,
371  PRInt32 aItemCount)
372 {
373  // Validate arguments.
374  NS_ENSURE_TRUE(aPlaylist, /* void */);
375  NS_ENSURE_TRUE(aTrack, /* void */);
376 
377  // Process item start status.
378  ItemStart(aItemNum, aItemCount);
379 }
380 
381 
395 void
397  sbIMediaItem* aMediaItem,
398  PRInt32 aItemNum,
399  PRInt32 aItemCount)
400 {
401  // Validate arguments.
402  NS_ENSURE_TRUE(aMediaItem, /* void */);
403 
404  // Update the current media item and list.
405  mMediaList = aMediaList;
406  mMediaItem = aMediaItem;
407 
408  // Apply default status processing.
409  ItemStart(aItemNum, aItemCount);
410 }
411 
412 
420 void
421 sbIPDStatus::ItemProgress(double aProgress)
422 {
423  // Update progress status.
424  // XXX should we be calling UpdateStatus?
425  mStatus->SetProgress(aProgress);
426  mDevice->CreateAndDispatchEvent
428  sbIPDVariant(mMediaItem).get());
429 }
430 
431 
439 void
440 sbIPDStatus::ItemComplete(nsresult aResult)
441 {
442  // Dispatch operation dependent status processing.
443  switch(mOperationType)
444  {
445  case OPERATION_TYPE_WRITE :
446  FIELD_LOG(("Completed write track item.\n"));
447  mDevice->CreateAndDispatchEvent
449  sbIPDVariant(mMediaItem).get());
450  break;
451 
452  default :
453  break;
454  }
455 }
456 
457 nsresult
459 {
460  NS_ENSURE_ARG(aCurrentStatus);
461  NS_IF_ADDREF(*aCurrentStatus = mStatus);
462  return NS_OK;
463 }
464 
465 
466 nsresult sbIPDStatus::ChangeStatus(PRUint32 newState) {
467  PRUint32 oldState;
468  nsresult rv;
469  rv = mStatus->GetCurrentState(&oldState);
470  NS_ENSURE_SUCCESS(rv, rv);
471 
472  LOG("sbIPDStatus::ChangeStatus(%s) from %s\n", STATE_STRING(newState), STATE_STRING(oldState));
473 
474  PRUint32 currentState = newState;
475  PRUint32 currentSubState = sbIDevice::STATE_IDLE;
476 
477  // Reset item status
478  rv = mStatus->SetMediaItem(nsnull);
479  NS_ENSURE_SUCCESS(rv, rv);
480  rv = mStatus->SetMediaList(nsnull);
481  NS_ENSURE_SUCCESS(rv, rv);
482 
483  // Check first if we are updating the current or sub states
484  if (newState != sbIDevice::STATE_IDLE) {
485  // Keep the status from switching rapidly, keep a main status and let the
486  // extra stuff be sub status (like copy when syncing or updating while
487  // copying)
488  if (oldState == sbIDevice::STATE_SYNCING ||
489  oldState == sbIDevice::STATE_COPYING ||
490  oldState == sbIDevice::STATE_MOUNTING) {
491  currentSubState = newState;
492  currentState = oldState;
493  }
494  }
495 
496  // Only update if we are actually changing states
497  if (currentState != oldState) {
498  rv = mStatus->SetCurrentState(currentState);
499  NS_ENSURE_SUCCESS(rv, rv);
500  mDevice->SetState(currentState);
501  // XXX check result?
502  }
503 
504  LOG("sbIPDStatus::ChangeStatus to state=%s, substate=%s\n", STATE_STRING(currentState), STATE_STRING(currentSubState));
505 
506  // Always update the sub state
507  rv = mStatus->SetCurrentSubState(currentSubState);
508  NS_ENSURE_SUCCESS(rv, rv);
509 
510  return NS_OK;
511 }
512 
513 nsresult sbIPDStatus::UpdateStatus(PRUint32 forState,
514  const nsAString& currentOperation,
515  const nsAString& currentStateMessage,
516  PRInt32 currentIndex,
517  PRInt32 totalCount,
518  double currentProgress) {
519  LOG("sbIPDStatus::UpdateStatus(forState=%s, currentIndex=%d, totalCount=%d)\n", STATE_STRING(forState), currentIndex, totalCount);
520  PRUint32 currentState;
521  nsresult rv;
522  rv = mStatus->GetCurrentState(&currentState);
523  NS_ENSURE_SUCCESS(rv, rv);
524 
525  // If totalCount is zero that means we have no work item progress to report
526  if (totalCount != 0) {
527  rv = mStatus->SetWorkItemProgress(currentIndex + 1);
528  NS_ENSURE_SUCCESS(rv, rv);
529  rv = mStatus->SetWorkItemProgressEndCount(totalCount);
530  NS_ENSURE_SUCCESS(rv, rv);
531  }
532 
533  rv = mStatus->SetCurrentOperation(currentOperation);
534  NS_ENSURE_SUCCESS(rv, rv);
535  rv = mStatus->SetStateMessage(currentStateMessage);
536  NS_ENSURE_SUCCESS(rv, rv);
537  rv = mStatus->SetMediaItem(mMediaItem);
538  NS_ENSURE_SUCCESS(rv, rv);
539  rv = mStatus->SetMediaList(mMediaList);
540  NS_ENSURE_SUCCESS(rv, rv);
541  rv = mStatus->SetProgress(currentProgress);
542  NS_ENSURE_SUCCESS(rv, rv);
543 
544  return NS_OK;
545 }
546 
547 void
549 {
550  LOG("sbIPDStatus::Idle()\n");
551 
552  // set the device status state
553  mStatus->SetCurrentState(sbIDevice::STATE_IDLE);
554 
555  // set the device status sub-state
556  mStatus->SetCurrentSubState(sbIDevice::STATE_IDLE);
557 
558  // set the device state
560 }
nsresult CreateAndDispatchEvent(PRUint32 aType, nsIVariant *aData, PRBool aAsync=PR_TRUE, sbIDeviceEventTarget *aTarget=nsnull)
#define SB_PRLOG_SETUP(x)
Definition: sbDebugUtils.h:115
return NS_OK
void OperationComplete(nsresult aResult)
const unsigned long STATE_DELETING
Definition: sbIDevice.idl:224
#define LOG(args)
const unsigned long EVENT_DEVICE_MOUNTING_END
Songbird iPod Device Utility Definitions.
virtual ~sbIPDStatus()
Definition: sbIPDStatus.cpp:98
void ItemComplete(nsresult aResult)
const unsigned long EVENT_DEVICE_MOUNTING_PROGRESS
nsresult GetCurrentStatus(sbIDeviceStatus **aCurrentStatus)
nsresult ChangeStatus(PRUint32 newState)
void ItemStart(PRInt32 aItemNum=-1, PRInt32 aItemCount=-1)
Songbird iPod Device Status Definitions.
A brief description of the contents of this interface.
#define STATE_STRING(X)
Definition: sbIPDStatus.cpp:64
const unsigned long EVENT_DEVICE_TRANSFER_END
void Finalize()
static const char * SB_UNUSED_IN_RELEASE(gStateStrings[])
PRUint32 GetBatchIndex() const
Definition: sbRequestItem.h:90
const unsigned long STATE_COPYING
Definition: sbIDevice.idl:223
Songbird iPod Device Definitions.
sbIPDStatus(sbIPDDevice *aDevice)
Definition: sbIPDStatus.cpp:83
#define FIELD_LOG(args)
Definition: sbIPDLog.h:126
const unsigned long EVENT_DEVICE_MOUNTING_START
static const PRUint32 OPERATION_TYPE_DELETE
Definition: sbIPDStatus.h:99
const unsigned long STATE_IDLE
Definition: sbIDevice.idl:220
nsresult UpdateStatus(PRUint32 forState, const nsAString &currentOperation, const nsAString &currentStateMessage, PRInt32 currentIndex, PRInt32 totalCount, double currentProgress)
Songbird iPod Device Logging Definitions.
const unsigned long STATE_MOUNTING
Definition: sbIDevice.idl:226
const unsigned long EVENT_DEVICE_TRANSFER_START
void ItemProgress(double aProgress)
const unsigned long STATE_SYNCING
Definition: sbIDevice.idl:221
nsCOMPtr< sbIMediaItem > item
Definition: sbBaseDevice.h:155
Interface that defines a single item of media in the system.
const unsigned long EVENT_DEVICE_MEDIA_WRITE_END
static const PRUint32 OPERATION_TYPE_MOUNT
Definition: sbIPDStatus.h:97
const unsigned long EVENT_DEVICE_TRANSFER_PROGRESS
NS_IMETHOD SetState(PRUint32 aState)
const unsigned long EVENT_DEVICE_MEDIA_WRITE_START
static const PRUint32 OPERATION_TYPE_WRITE
Definition: sbIPDStatus.h:98
nsCOMPtr< sbIMediaList > list
Definition: sbBaseDevice.h:156
void OperationStart(PRUint32 aOperationType, sbBaseDevice::TransferRequest *aRequest, PRUint32 aBatchCount)
nsresult Initialize()
static const PRUint32 OPERATION_TYPE_NONE
Definition: sbIPDStatus.h:96