sbDeviceProgressListener.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-2010 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 //
30 // Device progress listener.
31 //
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // Device progress listener imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Self imports.
48 
49 // Local imports.
50 #include "sbDeviceStatusHelper.h"
51 
52 // Mozilla imports.
53 #include <nsAutoLock.h>
54 #include <nsAutoPtr.h>
55 
56 
57 //------------------------------------------------------------------------------
58 //
59 // Device progress listener nsISupports implementation.
60 //
61 //------------------------------------------------------------------------------
62 
64 
65 
66 //------------------------------------------------------------------------------
67 //
68 // Device progress listener sbIJobProgressListener implementation.
69 //
70 //------------------------------------------------------------------------------
71 
72 //-------------------------------------
73 //
74 // OnJobProgress
75 //
76 
77 NS_IMETHODIMP
78 sbDeviceProgressListener::OnJobProgress(sbIJobProgress* aJobProgress)
79 {
80  // Validate arguments.
81  NS_ENSURE_ARG_POINTER(aJobProgress);
82 
83  // Function variables.
84  nsresult rv;
85 
86  // Update the device status.
87  if (mDeviceStatusHelper) {
88  // Get the job progress.
89  PRUint32 progress;
90  PRUint32 total;
91  rv = aJobProgress->GetProgress(&progress);
92  NS_ENSURE_SUCCESS(rv, rv);
93  rv = aJobProgress->GetTotal(&total);
94  NS_ENSURE_SUCCESS(rv, rv);
95 
96  // Update the device status.
97  if (total > 0) {
98  mDeviceStatusHelper->ItemProgress(static_cast<double>(progress) /
99  static_cast<double>(total));
100  }
101  }
102 
103  // Get the job status.
104  PRUint16 status;
105  rv = aJobProgress->GetStatus(&status);
106  NS_ENSURE_SUCCESS(rv, rv);
107 
108  // Check for job completion.
109  if (status != sbIJobProgress::STATUS_RUNNING) {
110  if (mCompleteNotifyMonitor) {
111  nsAutoMonitor monitor(mCompleteNotifyMonitor);
112  PR_AtomicSet(&mIsComplete, 1);
113  monitor.Notify();
114  }
115  else {
116  PR_AtomicSet(&mIsComplete, 1);
117  }
118  }
119 
120  return NS_OK;
121 }
122 
123 
124 //------------------------------------------------------------------------------
125 //
126 // Device progress listener public services.
127 //
128 //------------------------------------------------------------------------------
129 
130 //-------------------------------------
131 //
132 // New
133 //
134 
135 /* static */ nsresult
137  (sbDeviceProgressListener** aDeviceProgressListener,
138  PRMonitor* aCompleteNotifyMonitor,
139  sbDeviceStatusHelper* aDeviceStatusHelper)
140 {
141  // Validate arguments.
142  NS_ENSURE_ARG_POINTER(aDeviceProgressListener);
143 
144  // Create the device progress listener object.
145  nsRefPtr<sbDeviceProgressListener>
146  listener = new sbDeviceProgressListener(aCompleteNotifyMonitor,
147  aDeviceStatusHelper);
148  NS_ENSURE_TRUE(listener, NS_ERROR_OUT_OF_MEMORY);
149 
150  // Return results.
151  listener.forget(aDeviceProgressListener);
152 
153  return NS_OK;
154 }
155 
156 
157 //-------------------------------------
158 //
159 // IsComplete
160 //
161 
162 PRBool
164 {
165  return mIsComplete;
166 }
167 
168 
169 //-------------------------------------
170 //
171 // sbDeviceProgressListener
172 //
173 
175  (PRMonitor* aCompleteNotifyMonitor,
176  sbDeviceStatusHelper* aDeviceStatusHelper) :
177  mCompleteNotifyMonitor(aCompleteNotifyMonitor),
178  mDeviceStatusHelper(aDeviceStatusHelper),
179  mIsComplete(PR_FALSE)
180 {
181 }
182 
183 
184 //-------------------------------------
185 //
186 // ~sbDeviceProgressListener
187 //
188 
190 {
191 }
192 
var total
return NS_OK
NS_DECL_ISUPPORTS static NS_DECL_SBIJOBPROGRESSLISTENER nsresult New(sbDeviceProgressListener **aDeviceProgressListener, PRMonitor *aCompleteNotifyMonitor=nsnull, sbDeviceStatusHelper *aDeviceStatusHelper=nsnull)
Generic interface for exposing long running jobs to the UI.
const unsigned short STATUS_RUNNING
Constant indicating that the job is active.
Songbird Device Progress Listener Definitions.
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
sbDeviceProgressListener(PRMonitor *aCompleteNotifyMonitor, sbDeviceStatusHelper *aDeviceStatusHelper)
Songbird Device Status Services Definitions.
Implemented to receive notifications from sbIJobProgress interfaces.