sbiTunesImporterJob.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-2009 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 "sbiTunesImporterJob.h"
28 
29 #include <nsThreadUtils.h>
30 
32 #include <sbDebugUtils.h>
33 
37 
39  mCanCancel(PR_TRUE),
40  mCancelRequested(PR_FALSE),
41  mStatus(0) {
42 }
43 
45 }
46 
48  return new sbiTunesImporterJob;
49 }
50 
51 nsresult
52 sbiTunesImporterJob::SetStatus(PRUint32 aStatus) {
53  mStatus = aStatus;
54 
55  nsresult rv = UpdateProgress();
56  NS_ENSURE_SUCCESS(rv, rv);
57 
58  return NS_OK;
59 }
60 
61 nsresult
62 sbiTunesImporterJob::SetStatusText(nsAString const & aStatusText) {
63  mStatusText = aStatusText;
64 
65  nsresult rv = UpdateProgress();
66  NS_ENSURE_SUCCESS(rv, rv);
67 
68  return NS_OK;
69 }
70 
71 nsresult
72 sbiTunesImporterJob::SetTitleText(nsAString const & aTitleText) {
73  mTitleText = aTitleText;
74 
75  nsresult rv = UpdateProgress();
76  NS_ENSURE_SUCCESS(rv, rv);
77 
78  return NS_OK;
79 }
80 
81 nsresult
82 sbiTunesImporterJob::SetProgress(PRUint32 aProgress) {
83  mProgress = aProgress;
84 
85  nsresult rv = UpdateProgress();
86  NS_ENSURE_SUCCESS(rv, rv);
87 
88  return NS_OK;
89 }
90 
91 nsresult
93  mTotal = aTotal;
94 
95  nsresult rv = UpdateProgress();
96  NS_ENSURE_SUCCESS(rv, rv);
97 
98  return NS_OK;
99 }
100 
101 nsresult
102 sbiTunesImporterJob::AddErrorMessage(nsAString const & aErrorMessage) {
103  mErrorMessages.AppendElement(aErrorMessage);
104 
105  nsresult rv = UpdateProgress();
106  NS_ENSURE_SUCCESS(rv, rv);
107 
108  return NS_OK;
109 }
110 
111 NS_IMETHODIMP
112 sbiTunesImporterJob::GetStatus(PRUint16 *aStatus)
113 {
114  *aStatus = mStatus;
115  return NS_OK;
116 }
117 
118 NS_IMETHODIMP
119 sbiTunesImporterJob::GetBlocked(PRBool *aBlocked)
120 {
121  NS_ENSURE_ARG_POINTER(aBlocked);
122  *aBlocked = PR_FALSE;
123  return NS_OK;
124 }
125 
126 /* readonly attribute AString statusText; */
127 NS_IMETHODIMP
128 sbiTunesImporterJob::GetStatusText(nsAString & aStatusText)
129 {
130  aStatusText = mStatusText;
131  return NS_OK;
132 }
133 
134 /* readonly attribute AString titleText; */
135 NS_IMETHODIMP
136 sbiTunesImporterJob::GetTitleText(nsAString & aTitleText)
137 {
138  aTitleText = mTitleText;
139  return NS_OK;
140 }
141 
142 /* readonly attribute unsigned long progress; */
143 NS_IMETHODIMP
144 sbiTunesImporterJob::GetProgress(PRUint32 *aProgress)
145 {
146  *aProgress = mProgress;
147  return NS_OK;
148 }
149 
150 /* readonly attribute unsigned long total; */
151 NS_IMETHODIMP
152 sbiTunesImporterJob::GetTotal(PRUint32 *aTotal)
153 {
154  *aTotal = mTotal;
155  return NS_OK;
156 }
157 
158 /* readonly attribute unsigned long errorCount; */
159 NS_IMETHODIMP
160 sbiTunesImporterJob::GetErrorCount(PRUint32 *aErrorCount)
161 {
162  *aErrorCount = mErrorMessages.Length();
163  return NS_OK;
164 }
165 
171 NS_IMETHODIMP
172 sbiTunesImporterJob::GetErrorMessages(nsIStringEnumerator ** aEnumerator)
173 {
174  *aEnumerator = sbTArrayStringEnumerator::New(mErrorMessages);
175  NS_ENSURE_TRUE(*aEnumerator, NS_ERROR_OUT_OF_MEMORY);
176  NS_ADDREF(*aEnumerator);
177  return NS_OK;
178 }
179 
180 /* void addJobProgressListener (in sbIJobProgressListener aListener); */
181 NS_IMETHODIMP
182 sbiTunesImporterJob::AddJobProgressListener(sbIJobProgressListener *aListener)
183 {
184  if (mListeners.IndexOfObject(aListener) == -1) {
185  PRBool const inserted = mListeners.AppendObject(aListener);
186  NS_ENSURE_TRUE(inserted, NS_ERROR_OUT_OF_MEMORY);
187  }
188  else {
189  NS_WARNING("sbiTunesImporterJob::AddJobProgressListener listener already added");
190  }
191  return NS_OK;
192 }
193 
194 /* void removeJobProgressListener (in sbIJobProgressListener aListener); */
195 NS_IMETHODIMP
196 sbiTunesImporterJob::RemoveJobProgressListener(sbIJobProgressListener *aListener)
197 {
198  PRInt32 index = mListeners.IndexOfObject(aListener);
199  mListeners.RemoveObjectAt(index);
200  return NS_OK;
201 }
202 
203 NS_IMETHODIMP
204 sbiTunesImporterJob::GetCanCancel(PRBool *aCanCancel)
205 {
206  *aCanCancel = mCanCancel;
207  return NS_OK;
208 }
209 
210 /* void cancel (); */
211 NS_IMETHODIMP
212 sbiTunesImporterJob::Cancel()
213 {
214  mCancelRequested = PR_TRUE;
215  return NS_OK;
216 }
217 
218 nsresult
219 sbiTunesImporterJob::UpdateProgress() {
220  PRUint32 const listenerCount = mListeners.Count();
221  for (PRUint32 index = 0; index < listenerCount; ++index) {
222  nsresult SB_UNUSED_IN_RELEASE(rv) = mListeners[index]->OnJobProgress(this);
223  NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "iTunes Import listener reported error");
224  }
225  return NS_OK;
226 }
NS_IMPL_THREADSAFE_ISUPPORTS2(sbiTunesImporterJob, sbIJobProgress, sbIJobCancelable) sbiTunesImporterJob
return NS_OK
Generic interface for exposing long running jobs to the UI.
nsresult AddErrorMessage(nsAString const &aErrorMessage)
NS_DECL_ISUPPORTS static NS_DECL_NSISTRINGENUMERATOR sbTArrayStringEnumerator * New(const sbStringArray &aStringArray)
nsresult SetStatusText(nsAString const &aStatusText)
NS_DECL_ISUPPORTS NS_DECL_SBIJOBPROGRESS static NS_DECL_SBIJOBCANCELABLE sbiTunesImporterJob * New()
nsresult SetStatus(PRUint32 aStatus)
nsresult SetTitleText(nsAString const &aTitleText)
Implemented to receive notifications from sbIJobProgress interfaces.
nsresult SetProgress(PRUint32 aProgress)
#define SB_UNUSED_IN_RELEASE(decl)
Definition: sbDebugUtils.h:55
nsresult SetTotal(PRUint32 aTotal)