/src/Preferences.mm

http://firefox-mac-pdf.googlecode.com/ · Objective C++ · 95 lines · 65 code · 9 blank · 21 comment · 2 complexity · 272ba0d65ccbf17a94a745bde92c7a9c MD5 · raw file

  1. /*
  2. * Copyright (c) 2008 Samuel Gross.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #import "Preferences.h"
  23. #include "xpcom-config.h"
  24. #include "nsIPrefBranch.h"
  25. #include "nsIPrefService.h"
  26. #include "nsCOMPtr.h"
  27. #include "nsDirectoryServiceUtils.h"
  28. @implementation Preferences
  29. + (BOOL)getBoolPreference:(const char*)key
  30. {
  31. nsCOMPtr<nsIPrefService> prefsService;
  32. prefsService = do_GetService("@mozilla.org/preferences-service;1");
  33. nsCOMPtr<nsIPrefBranch> prefs;
  34. prefsService->GetBranch("extensions.firefox-pdf-mac.", getter_AddRefs(prefs));
  35. PRBool value = false;
  36. prefs->GetBoolPref(key, &value);
  37. return value;
  38. }
  39. + (float)getFloatPreference:(const char*)key
  40. {
  41. nsCOMPtr<nsIPrefService> prefsService;
  42. prefsService = do_GetService("@mozilla.org/preferences-service;1");
  43. nsCOMPtr<nsIPrefBranch> prefs;
  44. prefsService->GetBranch("extensions.firefox-pdf-mac.", getter_AddRefs(prefs));
  45. char* value = NULL;
  46. prefs->GetCharPref(key, &value);
  47. if (value == NULL) {
  48. return 1.0;
  49. }
  50. return [[NSString stringWithCString:value encoding:NSASCIIStringEncoding] floatValue];
  51. }
  52. + (int)getIntPreference:(const char*)key
  53. {
  54. nsCOMPtr<nsIPrefService> prefsService;
  55. prefsService = do_GetService("@mozilla.org/preferences-service;1");
  56. nsCOMPtr<nsIPrefBranch> prefs;
  57. prefsService->GetBranch("extensions.firefox-pdf-mac.", getter_AddRefs(prefs));
  58. int value = 1;
  59. prefs->GetIntPref(key, &value);
  60. return value;
  61. }
  62. + (void)setBoolPreference:(const char*)key value:(BOOL)value
  63. {
  64. nsCOMPtr<nsIPrefService> prefsService;
  65. prefsService = do_GetService("@mozilla.org/preferences-service;1");
  66. nsCOMPtr<nsIPrefBranch> prefs;
  67. prefsService->GetBranch("extensions.firefox-pdf-mac.", getter_AddRefs(prefs));
  68. prefs->SetBoolPref(key, value);
  69. }
  70. + (void)setFloatPreference:(const char*)key value:(float)value
  71. {
  72. nsCOMPtr<nsIPrefService> prefsService;
  73. prefsService = do_GetService("@mozilla.org/preferences-service;1");
  74. nsCOMPtr<nsIPrefBranch> prefs;
  75. prefsService->GetBranch("extensions.firefox-pdf-mac.", getter_AddRefs(prefs));
  76. prefs->SetCharPref(key, [[NSString stringWithFormat:@"%f", value] cStringUsingEncoding:NSASCIIStringEncoding]);
  77. }
  78. + (void)setIntPreference:(const char*)key value:(int)value
  79. {
  80. nsCOMPtr<nsIPrefService> prefsService;
  81. prefsService = do_GetService("@mozilla.org/preferences-service;1");
  82. nsCOMPtr<nsIPrefBranch> prefs;
  83. prefsService->GetBranch("extensions.firefox-pdf-mac.", getter_AddRefs(prefs));
  84. prefs->SetIntPref(key, value);
  85. }
  86. @end