sbScreenSaverSuppressor.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2009 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
26 
27 // Include windows.h first because it's required for nsWindowsRegKey.h
28 #include <windows.h>
29 
30 #include <nsWindowsRegKey.h>
31 
32 #define SCREENSAVER_ENABLED "1"
33 #define SCREENSAVER_DISABLED "0"
34 
37 
39 {
40 }
41 
43 {
44 }
45 
46 /*virtual*/ nsresult
48 {
49  NS_NAMED_LITERAL_STRING(regKeyPath, "Control Panel\\Desktop");
50  NS_NAMED_LITERAL_STRING(regKeyValueName, "ScreenSaveActive");
51 
52  nsresult rv = NS_ERROR_UNEXPECTED;
53  nsCOMPtr<nsIWindowsRegKey> regKey =
54  do_CreateInstance(NS_WINDOWSREGKEY_CONTRACTID, &rv);
55  NS_ENSURE_SUCCESS(rv, rv);
56 
57  rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER,
58  regKeyPath,
59  nsIWindowsRegKey::ACCESS_QUERY_VALUE);
60  NS_ENSURE_SUCCESS(rv, rv);
61 
62  nsString currentUserValue;
63  rv = regKey->ReadStringValue(regKeyValueName, currentUserValue);
64  NS_ENSURE_SUCCESS(rv, rv);
65 
66  rv = regKey->Close();
67  NS_ENSURE_SUCCESS(rv, rv);
68 
69  if(aSuppress) {
70  // Only suppress if we need to.
71  if(currentUserValue.EqualsLiteral(SCREENSAVER_ENABLED)) {
72  BOOL success = SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE,
73  FALSE,
74  NULL,
75  0);
76  NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
77  }
78 
79  // Push user setting so we can restore it for the login session.
80  if(!mHasUserSetting) {
81  mHasUserSetting = PR_TRUE;
82  mUserSetting = currentUserValue.EqualsLiteral(SCREENSAVER_ENABLED);
83  }
84  }
85  else {
86  // No user setting, nothing to restore.
87  if(!mHasUserSetting) {
88  return NS_OK;
89  }
90 
91  // Only unsuppress if we need to.
92  if(mUserSetting) {
93  BOOL success = SystemParametersInfoW(SPI_SETSCREENSAVEACTIVE,
94  TRUE,
95  NULL,
96  0);
97  NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
98 
99  mHasUserSetting = PR_FALSE;
100  mUserSetting = PR_FALSE;
101  }
102  }
103 
104  return NS_OK;
105 }
return NS_OK
#define SCREENSAVER_ENABLED
NS_IMPL_ISUPPORTS_INHERITED0(sbScreenSaverSuppressor, sbBaseScreenSaverSuppressor) nsresult sbScreenSaverSuppressor
virtual NS_DECL_ISUPPORTS_INHERITED nsresult OnSuppress(PRBool aSuppress)