sbMediaExportPrefController.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 
28 
29 #include "sbMediaExportDefines.h"
30 #include <nsIPrefBranch2.h>
31 #include <nsIObserverService.h>
32 #include <nsServiceManagerUtils.h>
33 #include <nsCOMPtr.h>
34 #include <nsStringAPI.h>
35 
36 #include <sbDebugUtils.h>
37 
38 
40 
42  : mShouldExportTracks(PR_FALSE)
43  , mShouldExportPlaylists(PR_FALSE)
44  , mShouldExportSmartPlaylists(PR_FALSE)
45  , mShouldStartExportAgent(PR_FALSE)
46  , mListener(nsnull)
47 {
49 }
50 
52 {
53 }
54 
55 nsresult
57 {
58  TRACE("%s: Initializing the mediaexport pref controller", __FUNCTION__);
59 
60  nsresult rv;
61  nsCOMPtr<nsIPrefBranch2> prefBranch =
62  do_GetService("@mozilla.org/preferences-service;1", &rv);
63  NS_ENSURE_SUCCESS(rv, rv);
64 
65  // Adding observer values for each pref will load the value in |Observe|.
66  rv = prefBranch->AddObserver(PREF_EXPORT_TRACKS,
67  this,
68  PR_FALSE);
69  NS_ENSURE_SUCCESS(rv, rv);
70  rv = prefBranch->AddObserver(PREF_EXPORT_PLAYLISTS,
71  this,
72  PR_FALSE);
73  NS_ENSURE_SUCCESS(rv, rv);
74  rv = prefBranch->AddObserver(PREF_EXPORT_SMARTPLAYLISTS,
75  this,
76  PR_FALSE);
77  NS_ENSURE_SUCCESS(rv, rv);
78  rv = prefBranch->AddObserver(PREF_EXPORT_STARTAGENT,
79  this,
80  PR_FALSE);
81  NS_ENSURE_SUCCESS(rv, rv);
82 
83  // Don't bother checking the result on this pref.
84  prefBranch->GetBoolPref(PREF_EXPORT_STARTAGENT, &mShouldStartExportAgent);
85 
86  mListener = aListener;
87  return NS_OK;
88 }
89 
90 nsresult
92 {
93  LOG("%s: Shutting down the mediaexport pref controller", __FUNCTION__);
94 
95  nsresult rv;
96  nsCOMPtr<nsIPrefBranch2> prefBranch =
97  do_GetService("@mozilla.org/preferences-service;1", &rv);
98  NS_ENSURE_SUCCESS(rv, rv);
99 
100  // Cleanup the pref observers
101  rv = prefBranch->RemoveObserver(PREF_EXPORT_TRACKS, this);
102  NS_ENSURE_SUCCESS(rv, rv);
103  rv = prefBranch->RemoveObserver(PREF_EXPORT_PLAYLISTS, this);
104  NS_ENSURE_SUCCESS(rv, rv);
105  rv = prefBranch->RemoveObserver(PREF_EXPORT_SMARTPLAYLISTS, this);
106  NS_ENSURE_SUCCESS(rv, rv);
107  rv = prefBranch->RemoveObserver(PREF_EXPORT_STARTAGENT, this);
108  NS_ENSURE_SUCCESS(rv, rv);
109 
110  mListener = nsnull;
111  return NS_OK;
112 }
113 
114 NS_IMETHODIMP
115 sbMediaExportPrefController::Observe(nsISupports *aSubject,
116  const char *aTopic,
117  const PRUnichar *aData)
118 {
119  if (strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) != 0) {
120  return NS_OK;
121  }
122 
123  nsresult rv;
124  nsCOMPtr<nsIPrefBranch2> prefBranch = do_QueryInterface(aSubject, &rv);
125  NS_ENSURE_SUCCESS(rv, rv);
126 
127  nsString modifiedPref(aData);
128 
129  PRBool modifiedValue = PR_FALSE;
130  rv = prefBranch->GetBoolPref(NS_ConvertUTF16toUTF8(modifiedPref).get(),
131  &modifiedValue);
132  NS_ENSURE_SUCCESS(rv, rv);
133 
134  LOG("%s: %s pref changed to %s",
135  __FUNCTION__,
136  NS_ConvertUTF16toUTF8(modifiedPref).get(),
137  (modifiedValue ? "true" : "false"));
138 
139  if (modifiedPref.EqualsLiteral(PREF_EXPORT_TRACKS)) {
140  mShouldExportTracks = modifiedValue;
141  }
142  else if (modifiedPref.EqualsLiteral(PREF_EXPORT_PLAYLISTS)) {
143  mShouldExportPlaylists = modifiedValue;
144  }
145  else if (modifiedPref.EqualsLiteral(PREF_EXPORT_SMARTPLAYLISTS)) {
146  mShouldExportSmartPlaylists = modifiedValue;
147  }
148  else if (modifiedPref.EqualsLiteral(PREF_EXPORT_STARTAGENT)) {
149  mShouldStartExportAgent = modifiedValue;
150  }
151 
152  if (mListener) {
153  rv = mListener->OnBoolPrefChanged(modifiedPref, modifiedValue);
154  NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
155  "Could not notify of mediaexport pref change!");
156  }
157 
158  return NS_OK;
159 }
160 
161 PRBool
163 {
164  return mShouldExportTracks ||
165  mShouldExportPlaylists ||
166  mShouldExportSmartPlaylists;
167 }
168 
169 PRBool
171 {
172  return mShouldExportPlaylists || mShouldExportSmartPlaylists;
173 }
174 
175 PRBool
177 {
178  return mShouldStartExportAgent;
179 }
180 
181 PRBool
183 {
184  return mShouldExportTracks;
185 }
186 
187 PRBool
189 {
190  return mShouldExportPlaylists;
191 }
192 
193 PRBool
195 {
196  return mShouldExportSmartPlaylists;
197 }
198 
#define SB_PRLOG_SETUP(x)
Definition: sbDebugUtils.h:115
return NS_OK
#define LOG(args)
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
nsresult Init(sbMediaExportPrefListener *aListener)
var PREF_EXPORT_STARTAGENT
var PREF_EXPORT_PLAYLISTS
var PREF_EXPORT_SMARTPLAYLISTS
function TRACE(s)
var PREF_EXPORT_TRACKS
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
NS_IMETHOD OnBoolPrefChanged(const nsAString &aPrefName, const PRBool aNewPrefValue)=0
_updateTextAndScrollDataForFrame aData