sbDeviceStatus.cpp
Go to the documentation of this file.
1 /* vim: set sw=2 :miv */
2 /*
3 //
4 // BEGIN SONGBIRD GPL
5 //
6 // This file is part of the Songbird web player.
7 //
8 // Copyright(c) 2005-2008 POTI, Inc.
9 // http://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 #include "sbDeviceStatus.h"
29 
30 #include <nsComponentManagerUtils.h>
31 #include <sbIDataRemote.h>
32 #include <sbIDevice.h>
33 #include <nsIProxyObjectManager.h>
34 #include <nsServiceManagerUtils.h>
35 #include <nsThreadUtils.h>
36 
38 
40 {
41  /* member initializers and constructor code */
42  mCurrentState = sbIDevice::STATE_IDLE;
43  mCurrentSubState = sbIDevice::STATE_IDLE;
44  mTimestamp = 0;
45  mCurrentProgress = -1;
46  mNewBatch = false;
47 }
48 
49 sbDeviceStatus::~sbDeviceStatus()
50 {
51  /* destructor code */
52 }
53 
54 /* attribute unsigned long currentState; */
55 NS_IMETHODIMP
56 sbDeviceStatus::GetCurrentState(PRUint32 *aCurrentState)
57 {
58  NS_ENSURE_ARG_POINTER(aCurrentState);
59  *aCurrentState = mCurrentState;
60  return NS_OK;
61 }
62 NS_IMETHODIMP
63 sbDeviceStatus::SetCurrentState(PRUint32 aCurrentState)
64 {
65  // edge transition from IDLE to any other state
66  if (aCurrentState != mCurrentState && mCurrentState == sbIDevice::STATE_IDLE)
67  {
68  mTimestamp = PR_IntervalNow();
69  }
70 
71  mCurrentState = aCurrentState;
72 
73  // If we're idle we want to set the current index to zero so the JS code
74  // doesn't erroneously display counts from the previous batch
75  if (aCurrentState == sbIDevice::STATE_IDLE) {
76  nsresult rv = SetWorkItemProgress(0);
77  NS_ENSURE_SUCCESS(rv, rv);
78  }
79  return NS_OK;
80 }
81 
82 /* attribute unsigned long currentSubState; */
83 NS_IMETHODIMP
84 sbDeviceStatus::GetCurrentSubState(PRUint32 *aCurrentSubState)
85 {
86  NS_ENSURE_ARG_POINTER(aCurrentSubState);
87  *aCurrentSubState = mCurrentSubState;
88  return NS_OK;
89 }
90 NS_IMETHODIMP
91 sbDeviceStatus::SetCurrentSubState(PRUint32 aCurrentSubState)
92 {
93  mCurrentSubState = aCurrentSubState;
94  return NS_OK;
95 }
96 
97 /* attribute AString stateMessage; */
98 NS_IMETHODIMP
99 sbDeviceStatus::GetStateMessage(nsAString & aStateMessage)
100 {
101  return mStatusRemote->GetStringValue(aStateMessage);
102 }
103 NS_IMETHODIMP
104 sbDeviceStatus::SetStateMessage(const nsAString & aStateMessage)
105 {
106  return mStatusRemote->SetStringValue(aStateMessage);
107 }
108 
109 /* attribute AString currentOperation; */
110 NS_IMETHODIMP
111 sbDeviceStatus::GetCurrentOperation(nsAString & aCurrentOperation)
112 {
113  return mOperationRemote->GetStringValue(aCurrentOperation);
114 }
115 NS_IMETHODIMP
116 sbDeviceStatus::SetCurrentOperation(const nsAString & aCurrentOperation)
117 {
118  return mOperationRemote->SetStringValue(aCurrentOperation);
119 }
120 
121 /* attribute double progress; */
122 NS_IMETHODIMP
123 sbDeviceStatus::GetProgress(double *aProgress)
124 {
125  PRInt64 curProgress;
126  nsresult rv;
127  rv = mProgressRemote->GetIntValue(&curProgress);
128  NS_ENSURE_SUCCESS(rv, rv);
129 
130  *aProgress = (curProgress / 100.0 - 0.5);
131  return NS_OK;
132 }
133 
134 NS_IMETHODIMP
135 sbDeviceStatus::SetProgress(double aProgress)
136 {
137  PRInt64 const newProgress = (PRUint64)(aProgress * 100.00 + 0.5);
138  if (newProgress != mCurrentProgress) {
139  mCurrentProgress = newProgress;
140  return mProgressRemote->SetIntValue(newProgress);
141  }
142  return NS_OK;
143 }
144 
145 /* attribute PRInt64 workItemProgress; */
146 NS_IMETHODIMP
147 sbDeviceStatus::GetWorkItemProgress(PRInt64 *aWorkItemProgress)
148 {
149  NS_ENSURE_ARG_POINTER(aWorkItemProgress);
150  return mWorkCurrentCountRemote->GetIntValue(aWorkItemProgress);
151 }
152 NS_IMETHODIMP
153 sbDeviceStatus::SetWorkItemProgress(PRInt64 aWorkItemProgress)
154 {
155  return mWorkCurrentCountRemote->SetIntValue(aWorkItemProgress);
156 }
157 
158 /* attribute PRInt32 workItemType; */
159 NS_IMETHODIMP
160 sbDeviceStatus::GetWorkItemType(PRInt32 *aCurrentItemType)
161 {
162  NS_ENSURE_ARG_POINTER(aCurrentItemType);
163  mWorkCurrentTypeRemote->GetIntValue((PRInt64 *)aCurrentItemType);
164  return NS_OK;
165 }
166 NS_IMETHODIMP
167 sbDeviceStatus::SetWorkItemType(PRInt32 aCurrentItemType)
168 {
169  mWorkCurrentTypeRemote->SetIntValue(aCurrentItemType);
170  return NS_OK;
171 }
172 
173 /* attribute PRInt64 workItemProgressEndCount; */
174 NS_IMETHODIMP
175 sbDeviceStatus::GetWorkItemProgressEndCount(PRInt64 *aWorkItemProgressEndCount)
176 {
177  NS_ENSURE_ARG_POINTER(aWorkItemProgressEndCount);
178  return mWorkTotalCountRemote->GetIntValue(aWorkItemProgressEndCount);
179 }
180 NS_IMETHODIMP
181 sbDeviceStatus::SetWorkItemProgressEndCount(PRInt64 aWorkItemProgressEndCount)
182 {
183  return mWorkTotalCountRemote->SetIntValue(aWorkItemProgressEndCount);
184 }
185 
186 /* attribute sbIMediaItem mediaItem; */
187 NS_IMETHODIMP
188 sbDeviceStatus::GetMediaItem(sbIMediaItem * *aMediaItem)
189 {
190  if (mItem) {
191  NS_IF_ADDREF(*aMediaItem = mItem);
192  }
193  return NS_OK;
194 }
195 NS_IMETHODIMP
196 sbDeviceStatus::SetMediaItem(sbIMediaItem * aMediaItem)
197 {
198  mItem = aMediaItem;
199  return NS_OK;
200 }
201 
202 /* attribute sbIMediaList mediaList; */
203 NS_IMETHODIMP
204 sbDeviceStatus::GetMediaList(sbIMediaList * *aMediaList)
205 {
206  if (mList) {
207  NS_IF_ADDREF(*aMediaList = mList);
208  }
209  return NS_OK;
210 }
211 NS_IMETHODIMP
212 sbDeviceStatus::SetMediaList(sbIMediaList * aMediaList)
213 {
214  mList = aMediaList;
215  return NS_OK;
216 }
217 
218 /* readonly attribute PRUint32 elapsedTime; */
219 NS_IMETHODIMP sbDeviceStatus::GetElapsedTime(PRUint32 *aElapsedTime)
220 {
221  NS_ENSURE_ARG_POINTER(aElapsedTime);
222  *aElapsedTime = PR_IntervalToMilliseconds((PRIntervalTime)(PR_IntervalNow() - mTimestamp));
223  return NS_OK;
224 }
225 
226 /* attribute boolean isNewBatch; */
227 NS_IMETHODIMP sbDeviceStatus::GetIsNewBatch(PRBool *aIsNewBatch)
228 {
229  *aIsNewBatch = mNewBatch;
230  return NS_OK;
231 }
232 NS_IMETHODIMP sbDeviceStatus::SetIsNewBatch(PRBool aIsNewBatch)
233 {
234  mNewBatch = aIsNewBatch;
235  if (aIsNewBatch)
236  mTimestamp = PR_IntervalNow();
237  return NS_OK;
238 }
239 
240 NS_IMETHODIMP sbDeviceStatus::Init(const nsAString& aDeviceID)
241 {
242  nsresult rv;
243  mDeviceID.Assign(aDeviceID);
244  mTimestamp = PR_IntervalNow();
245 
246  NS_NAMED_LITERAL_STRING(STATE, "status.state");
247  NS_NAMED_LITERAL_STRING(OPERATION, "status.operation");
248  NS_NAMED_LITERAL_STRING(PROGRESS, "status.progress");
249  NS_NAMED_LITERAL_STRING(WORK_CURRENT_TYPE, "status.type");
250  NS_NAMED_LITERAL_STRING(WORK_CURRENT_COUNT, "status.workcount");
251  NS_NAMED_LITERAL_STRING(WORK_TOTAL_COUNT, "status.totalcount");
252 
253  /* the data remotes need the POM */
254  nsCOMPtr<nsIProxyObjectManager> pom =
255  do_GetService("@mozilla.org/xpcomproxy;1", &rv);
256  NS_ENSURE_SUCCESS(rv, rv);
257 
258  rv = GetDataRemote(pom,
259  STATE,
260  mDeviceID,
261  getter_AddRefs(mStatusRemote));
262  NS_ENSURE_SUCCESS(rv, rv);
263 
264  rv = GetDataRemote(pom,
265  OPERATION,
266  mDeviceID,
267  getter_AddRefs(mOperationRemote));
268  NS_ENSURE_SUCCESS(rv, rv);
269 
270  rv = GetDataRemote(pom,
271  PROGRESS,
272  mDeviceID,
273  getter_AddRefs(mProgressRemote));
274  NS_ENSURE_SUCCESS(rv, rv);
275 
276  rv = GetDataRemote(pom,
277  WORK_CURRENT_TYPE,
278  mDeviceID,
279  getter_AddRefs(mWorkCurrentTypeRemote));
280  NS_ENSURE_SUCCESS(rv, rv);
281 
282  rv = GetDataRemote(pom,
283  WORK_CURRENT_COUNT,
284  mDeviceID,
285  getter_AddRefs(mWorkCurrentCountRemote));
286  NS_ENSURE_SUCCESS(rv, rv);
287 
288  rv = GetDataRemote(pom,
289  WORK_TOTAL_COUNT,
290  mDeviceID,
291  getter_AddRefs(mWorkTotalCountRemote));
292  NS_ENSURE_SUCCESS(rv, rv);
293 
294  return NS_OK;
295 }
296 
297 nsresult sbDeviceStatus::GetDataRemote(nsIProxyObjectManager* aProxyObjectManager,
298  const nsAString& aDataRemoteName,
299  const nsAString& aDataRemotePrefix,
300  void** appDataRemote)
301 {
302  nsAutoString fullDataRemoteName;
303  nsCOMPtr<sbIDataRemote> pDataRemote;
304  nsString nullString;
305  nsresult rv;
306 
307  nullString.SetIsVoid(PR_TRUE);
308 
309  /* Get the full data remote name. */
310  if (!aDataRemotePrefix.IsEmpty())
311  {
312  fullDataRemoteName.Assign(aDataRemotePrefix);
313  fullDataRemoteName.AppendLiteral(".");
314  }
315  fullDataRemoteName.Append(aDataRemoteName);
316 
317  /* Get the data remote. */
318  pDataRemote = do_CreateInstance("@songbirdnest.com/Songbird/DataRemote;1",
319  &rv);
320  NS_ENSURE_SUCCESS(rv, rv);
321  rv = pDataRemote->Init(fullDataRemoteName, nullString);
322  NS_ENSURE_SUCCESS(rv, rv);
323 
324 
325  /* Make a proxy for the status data remote. */
326  rv = aProxyObjectManager->GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
327  NS_GET_IID(sbIDataRemote),
328  pDataRemote,
329  nsIProxyObjectManager::INVOKE_ASYNC |
330  nsIProxyObjectManager::FORCE_PROXY_CREATION,
331  appDataRemote);
332  NS_ENSURE_SUCCESS(rv, rv);
333 
334  return (NS_OK);
335 }
return NS_OK
A brief description of the contents of this interface.
An interface for accessing, and binding to, stored data.
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
function Init()
const unsigned long STATE_IDLE
Definition: sbIDevice.idl:220
PRUint64 mTimestamp
Interface that defines a single item of media in the system.