sbiTunesImporterStatus.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 "sbiTunesImporterStatus.h"
28 
29 #include <nsIProxyObjectManager.h>
30 #include <nsIObserverService.h>
31 
33 #include <sbStringBundle.h>
34 
35 #include "sbiTunesImporterJob.h"
36 
37 sbiTunesImporterStatus::sbiTunesImporterStatus(sbiTunesImporterJob * aJobProgress) :
38  mDone(PR_FALSE),
39  mJobProgress(aJobProgress),
40  mProgress(0),
41  mTotalSet(PR_FALSE) {
42 
43 }
44 
46  // TODO Auto-generated destructor stub
47 }
48 
51  return new sbiTunesImporterStatus(aJobProgress);
52 }
53 
55  // If there's no job progress, then we've been canceled
56  PRBool canceled = PR_TRUE;
57  if (mJobProgress) {
58  canceled = mJobProgress->CancelRequested();
59  }
60  return canceled;
61 }
62 
64 
65  nsresult rv;
66 
67  mProgress = 0;
68  // Create and proxy the status data remote object
69  mStatusDataRemote = do_CreateInstance(SB_DATAREMOTE_CONTRACTID, &rv);
70  NS_ENSURE_SUCCESS(rv, rv);
71 
72  rv = mStatusDataRemote->Init(NS_LITERAL_STRING("faceplate.status.text"), nsString());
73  NS_ENSURE_SUCCESS(rv, rv);
74 
75 
76  if (mJobProgress) {
78  nsTArray<nsString> params;
79 
80  nsString * newElement = params.AppendElement(NS_LITERAL_STRING("iTunes"));
81  NS_ENSURE_TRUE(newElement, NS_ERROR_OUT_OF_MEMORY);
82 
83  nsString titleText = bundle.Format(NS_LITERAL_STRING("import_library.job.title_text"),
84  params);
85  rv = mJobProgress->SetTitleText(titleText);
86  NS_ENSURE_SUCCESS(rv, rv);
87 
88  rv = mJobProgress->SetStatusText(SBLocalizedString("import_library.job.status_text"));
89  NS_ENSURE_SUCCESS(rv, rv);
90 
91  rv = mJobProgress->SetStatus(sbIJobProgress::STATUS_RUNNING);
92  NS_ENSURE_SUCCESS(rv, rv);
93  }
94  return NS_OK;
95 }
96 
98  mStatusDataRemote = nsnull;
99  mJobProgress = nsnull;
100 }
101 
103  mStatusText.Truncate();
104  mDone = PR_FALSE;
105  return NS_OK;
106 }
107 
108 void sbiTunesImporterStatus::SetProgress(PRInt64 aProgress) {
109  mProgress = (PRUint32)((aProgress * 100L) / mProgressMax);
110  Update();
111 }
112 
113 void sbiTunesImporterStatus::SetProgressMax(PRInt64 aProgressMax) {
114  mProgressMax = aProgressMax;
115 }
116 
117 void sbiTunesImporterStatus::SetStatusText(nsAString const & aMsg) {
118  mStatusText = aMsg;
119 }
120 
122  nsresult rv;
123 
124  // Check if finalized so just return an error
125  if (!mStatusDataRemote || !mJobProgress) {
126  return NS_ERROR_FAILURE;
127  }
128  nsString status(mStatusText);
129  // Round the value
130  if (!mLastStatusText.Equals(mStatusText) || mLastProgress != mProgress) {
131  if (!mDone) {
132  status.AppendLiteral(" ");
133  status.AppendInt(mProgress, 10);
134  status.AppendLiteral("%");
135  }
136  rv = mStatusDataRemote->SetStringValue(status);
137  NS_ENSURE_SUCCESS(rv, rv);
138  if (mJobProgress) {
139  if (mLastProgress != mProgress) {
140  rv = mJobProgress->SetProgress(mProgress);
141  NS_ENSURE_SUCCESS(rv, rv);
142 
143  rv = mJobProgress->SetTotal(100);
144  NS_ENSURE_SUCCESS(rv, rv);
145  }
146  }
147  if (mDone) {
148  rv = mJobProgress->SetStatus(sbIJobProgress::STATUS_SUCCEEDED);
149 
150  // Also send an observer notification with the job progress object in
151  // case anyone is interested.
152  nsCOMPtr<nsIObserverService> obsSvc = do_GetService(
153  NS_OBSERVERSERVICE_CONTRACTID, &rv);
154  NS_ENSURE_SUCCESS(rv, rv);
155 
156  nsCOMPtr<nsISupports> job = NS_ISUPPORTS_CAST(sbIJobProgress*, mJobProgress);
157  rv = obsSvc->NotifyObservers(job,
159  nsnull);
160  NS_ENSURE_SUCCESS(rv, rv);
161 
162  }
163  mLastProgress = mProgress;
164  mLastStatusText = mStatusText;
165  }
166  return NS_OK;
167 }
168 
return NS_OK
Generic interface for exposing long running jobs to the UI.
nsString Format(const nsAString &aKey, nsTArray< nsString > &aParams, const nsAString &aDefault=SBVoidString())
const unsigned short STATUS_SUCCEEDED
Constant indicating that the job has completed.
const unsigned short STATUS_RUNNING
Constant indicating that the job is active.
var bundle
static sbiTunesImporterStatus * New(sbiTunesImporterJob *aJobProgress)
#define SB_DATAREMOTE_CONTRACTID
Songbird String Bundle Definitions.
void SetProgress(PRInt64 aProgress)
void SetStatusText(nsAString const &aMsg)
#define SB_LIBRARY_IMPORT_ITUNES_COMPLETE
void SetProgressMax(PRInt64 aMaxProgress)