sbWatchFolderPrefMgr.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 "sbWatchFolderPrefMgr.h"
28 
29 #include "sbWatchFolderDefines.h"
30 #include "sbWatchFolderService.h"
31 #include <nsCOMPtr.h>
32 #include <nsServiceManagerUtils.h>
33 #include <nsIObserverService.h>
34 
35 
37 
39 {
40 }
41 
43 {
44 }
45 
46 nsresult
48 {
49  NS_ENSURE_ARG_POINTER(aWFService);
50 
51  mWatchFolderService = aWFService;
52 
53  nsresult rv;
54  nsCOMPtr<nsIObserverService> observerService =
55  do_GetService("@mozilla.org/observer-service;1", &rv);
56  NS_ENSURE_SUCCESS(rv, rv);
57 
58  rv = observerService->AddObserver(this,
59  "final-ui-startup",
60  PR_FALSE);
61  NS_ENSURE_SUCCESS(rv, rv);
62 
63  rv = observerService->AddObserver(this,
64  "quit-application-granted",
65  PR_FALSE);
66  NS_ENSURE_SUCCESS(rv, rv);
67 
68  return NS_OK;
69 }
70 
71 nsresult
73 {
74  NS_ENSURE_ARG_POINTER(aOutIsRunning);
75  *aOutIsRunning = PR_FALSE;
76 
77  nsresult rv;
78  nsCOMPtr<nsIPrefBranch2> prefBranch =
79  do_GetService("@mozilla.org/preferences-service;1", &rv);
80  NS_ENSURE_SUCCESS(rv, rv);
81 
82  rv = prefBranch->GetBoolPref("songbird.__testmode__", aOutIsRunning);
83  NS_ENSURE_SUCCESS(rv, rv);
84 
85  return NS_OK;
86 }
87 
88 nsresult
89 sbWatchFolderPrefMgr::OnPrefChanged(const nsAString & aPrefName,
90  nsIPrefBranch2 *aPrefBranch)
91 {
92  LOG(("%s: aPrefName = %s",
93  __FUNCTION__,
94  NS_ConvertUTF16toUTF8(aPrefName).get()));
95 
96  NS_ENSURE_ARG_POINTER(aPrefBranch);
97 
98  nsresult rv;
99 
100  // Should enable watch folders pref.
101  if (aPrefName.EqualsLiteral(PREF_WATCHFOLDER_ENABLE)) {
102  PRBool shouldEnable;
103  rv = aPrefBranch->GetBoolPref(PREF_WATCHFOLDER_ENABLE, &shouldEnable);
104  NS_ENSURE_SUCCESS(rv, rv);
105 
106  rv = mWatchFolderService->OnEnableWatchFolderChanged(shouldEnable);
107  NS_ENSURE_SUCCESS(rv, rv);
108  }
109  // Watch folder path
110  else if (aPrefName.EqualsLiteral(PREF_WATCHFOLDER_PATH)) {
111  nsCOMPtr<nsISupportsString> supportsVal;
112  rv = aPrefBranch->GetComplexValue(PREF_WATCHFOLDER_PATH,
113  NS_GET_IID(nsISupportsString),
114  getter_AddRefs(supportsVal));
115  NS_ENSURE_SUCCESS(rv, rv);
116 
117  nsString newWatchPath;
118  rv = supportsVal->GetData(newWatchPath);
119  NS_ENSURE_SUCCESS(rv, rv);
120 
121  rv = mWatchFolderService->OnWatchFolderPathChanged(newWatchPath);
122  NS_ENSURE_SUCCESS(rv, rv);
123  }
124  else {
125  TRACE(("%s: Pref change not handled!", __FUNCTION__));
126  }
127 
128  return NS_OK;
129 }
130 
131 //------------------------------------------------------------------------------
132 // nsIObserver
133 
134 NS_IMETHODIMP
135 sbWatchFolderPrefMgr::Observe(nsISupports *aSubject,
136  const char *aTopic,
137  const PRUnichar *aData)
138 {
139  NS_ENSURE_ARG_POINTER(aTopic);
140 
141  nsresult rv;
142 
143  // Final UI startup:
144  if (strcmp("final-ui-startup", aTopic) == 0) {
145  nsCOMPtr<nsIObserverService> observerService =
146  do_GetService("@mozilla.org/observer-service;1", &rv);
147  NS_ENSURE_SUCCESS(rv, rv);
148 
149  rv = observerService->RemoveObserver(this, aTopic);
150  NS_ENSURE_SUCCESS(rv, rv);
151 
152  // Now is the time to start listening to the pref branch.
153  nsCOMPtr<nsIPrefBranch2> prefBranch =
154  do_GetService("@mozilla.org/preferences-service;1", &rv);
155  NS_ENSURE_SUCCESS(rv, rv);
156 
157  rv = prefBranch->AddObserver(PREF_WATCHFOLDER_ROOT, this, PR_FALSE);
158  NS_ENSURE_SUCCESS(rv, rv);
159 
160  rv = mWatchFolderService->OnAppStartup();
161  NS_ENSURE_SUCCESS(rv, rv);
162  }
163  // Library shutdown topic:
164  else if (strcmp("quit-application-granted", aTopic) == 0) {
165  nsCOMPtr<nsIObserverService> observerService =
166  do_GetService("@mozilla.org/observer-service;1", &rv);
167  NS_ENSURE_SUCCESS(rv, rv);
168 
169  rv = observerService->RemoveObserver(this, aTopic);
170  NS_ENSURE_SUCCESS(rv, rv);
171 
172  rv = mWatchFolderService->OnAppShutdown();
173  NS_ENSURE_SUCCESS(rv, rv);
174  }
175  // Pref changed topic:
176  else if (strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic) == 0) {
177  nsCOMPtr<nsIPrefBranch2> prefBranch = do_QueryInterface(aSubject, &rv);
178  NS_ENSURE_SUCCESS(rv, rv);
179 
180  rv = OnPrefChanged(nsDependentString(aData), prefBranch);
181  NS_ENSURE_SUCCESS(rv, rv);
182  }
183 
184  return NS_OK;
185 }
186 
return NS_OK
static nsCOMPtr< nsIObserverService > observerService
Definition: UnityProxy.cpp:6
nsresult OnPrefChanged(const nsAString &aPrefName, nsIPrefBranch2 *aPrefBranch)
#define LOG(args)
var nsIPrefBranch2
Definition: windowUtils.js:253
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
nsresult Init(sbWatchFolderService *aWFService)
#define PREF_WATCHFOLDER_ROOT
NS_DECL_ISUPPORTS NS_DECL_NSIOBSERVER nsresult GetIsUnitTestsRunning(PRBool *aOutIsRunning)
function TRACE(s)
#define PREF_WATCHFOLDER_PATH
#define PREF_WATCHFOLDER_ENABLE
const nsISupportsString
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
_updateTextAndScrollDataForFrame aData