1 /************************************************************************** 2 * Otter Browser: Web browser controlled by the user, not vice-versa. 3 * Copyright (C) 2013 - 2018 Michal Dutkiewicz aka Emdek <michal@emdek.pl> 4 * Copyright (C) 2014 - 2016 Piotr Wójcik <chocimier@tlen.pl> 5 * Copyright (C) 2016 Jan Bajer aka bajasoft <jbajer@gmail.com> 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * 20 **************************************************************************/ 21 22 #ifndef OTTER_SETTINGSMANAGER_H 23 #define OTTER_SETTINGSMANAGER_H 24 25 #include <QtCore/QObject> 26 #include <QtCore/QVariant> 27 #include <QtGui/QIcon> 28 29 namespace Otter 30 { 31 32 class SettingsManager final : public QObject 33 { 34 Q_OBJECT 35 Q_ENUMS(OptionIdentifier) 36 Q_ENUMS(OptionType) 37 38 public: 39 enum OptionIdentifier 40 { 41 AddressField_CompletionDisplayModeOption = 0, 42 AddressField_CompletionModeOption, 43 AddressField_DropActionOption, 44 AddressField_HostLookupTimeoutOption, 45 AddressField_LayoutOption, 46 AddressField_PasteAndGoOnMiddleClickOption, 47 AddressField_SelectAllOnFocusOption, 48 AddressField_ShowCompletionCategoriesOption, 49 AddressField_SuggestBookmarksOption, 50 AddressField_SuggestHistoryOption, 51 AddressField_SuggestLocalPathsOption, 52 AddressField_SuggestSearchOption, 53 AddressField_SuggestSpecialPagesOption, 54 Backends_PasswordsOption, 55 Backends_WebOption, 56 Browser_AlwaysAskWhereToSaveDownloadOption, 57 Browser_EnableMouseGesturesOption, 58 Browser_EnableSingleKeyShortcutsOption, 59 Browser_EnableSpellCheckOption, 60 Browser_EnableTrayIconOption, 61 Browser_HomePageOption, 62 Browser_InactiveTabTimeUntilSuspendOption, 63 Browser_KeyboardShortcutsProfilesOrderOption, 64 Browser_LocaleOption, 65 Browser_MigrationsOption, 66 Browser_MouseProfilesOrderOption, 67 Browser_OfflineStorageLimitOption, 68 Browser_OfflineWebApplicationCacheLimitOption, 69 Browser_OpenLinksInNewTabOption, 70 Browser_PrintElementBackgroundsOption, 71 Browser_PrivateModeOption, 72 Browser_RememberPasswordsOption, 73 Browser_ReuseCurrentTabOption, 74 Browser_ShowSelectionContextMenuOnDoubleClickOption, 75 Browser_SpellCheckDictionaryOption, 76 Browser_StartupBehaviorOption, 77 Browser_ToolTipsModeOption, 78 Browser_TransferStartingActionOption, 79 Browser_ValidatorsOrderOption, 80 Cache_DiskCacheLimitOption, 81 Cache_PagesInMemoryLimitOption, 82 Choices_WarnFormResendOption, 83 Choices_WarnLowDiskSpaceOption, 84 Choices_WarnOpenBookmarkFolderOption, 85 Choices_WarnOpenMultipleDroppedUrlsOption, 86 Choices_WarnQuitOption, 87 Choices_WarnQuitTransfersOption, 88 Content_BackgroundColorOption, 89 Content_CursiveFontOption, 90 Content_DefaultCharacterEncodingOption, 91 Content_DefaultFixedFontSizeOption, 92 Content_DefaultFontSizeOption, 93 Content_DefaultZoomOption, 94 Content_FantasyFontOption, 95 Content_FixedFontOption, 96 Content_LinkColorOption, 97 Content_MinimumFontSizeOption, 98 Content_PageReloadTimeOption, 99 Content_SansSerifFontOption, 100 Content_SerifFontOption, 101 Content_StandardFontOption, 102 Content_TextColorOption, 103 Content_UserStyleSheetOption, 104 Content_VisitedLinkColorOption, 105 Content_ZoomTextOnlyOption, 106 ContentBlocking_CosmeticFiltersModeOption, 107 ContentBlocking_EnableContentBlockingOption, 108 ContentBlocking_EnableWildcardsOption, 109 ContentBlocking_IgnoreHostsOption, 110 ContentBlocking_ProfilesOption, 111 History_BrowsingLimitAmountGlobalOption, 112 History_BrowsingLimitAmountWindowOption, 113 History_BrowsingLimitPeriodOption, 114 History_ClearOnCloseOption, 115 History_ClosedTabsLimitAmountOption, 116 History_ClosedWindowsLimitAmountOption, 117 History_DownloadsLimitPeriodOption, 118 History_ExpandBranchesOption, 119 History_ManualClearOptionsOption, 120 History_ManualClearPeriodOption, 121 History_RememberBrowsingOption, 122 History_RememberClosedPrivateTabsOption, 123 History_RememberDownloadsOption, 124 History_StoreFaviconsOption, 125 Interface_DateTimeFormatOption, 126 Interface_EnableSmoothScrollingOption, 127 Interface_IconThemePathOption, 128 Interface_LastTabClosingActionOption, 129 Interface_LockToolBarsOption, 130 Interface_NewTabOpeningActionOption, 131 Interface_NotificationVisibilityDurationOption, 132 Interface_ShowScrollBarsOption, 133 Interface_StyleSheetOption, 134 Interface_TabCrashingActionOption, 135 Interface_UseFancyDateTimeFormatOption, 136 Interface_UseNativeNotificationsOption, 137 Interface_UseSystemIconThemeOption, 138 Interface_WidgetStyleOption, 139 Network_AcceptLanguageOption, 140 Network_CookiesKeepModeOption, 141 Network_CookiesPolicyOption, 142 Network_DoNotTrackPolicyOption, 143 Network_EnableReferrerOption, 144 Network_ProxyOption, 145 Network_ThirdPartyCookiesAcceptedHostsOption, 146 Network_ThirdPartyCookiesPolicyOption, 147 Network_ThirdPartyCookiesRejectedHostsOption, 148 Network_UserAgentOption, 149 Network_WorkOfflineOption, 150 Paths_DownloadsOption, 151 Paths_OpenFileOption, 152 Paths_SaveFileOption, 153 Permissions_EnableFullScreenOption, 154 Permissions_EnableGeolocationOption, 155 Permissions_EnableImagesOption, 156 Permissions_EnableJavaScriptOption, 157 Permissions_EnableLocalStorageOption, 158 Permissions_EnableMediaCaptureAudioOption, 159 Permissions_EnableMediaCaptureVideoOption, 160 Permissions_EnableMediaPlaybackAudioOption, 161 Permissions_EnableNotificationsOption, 162 Permissions_EnableOfflineStorageDatabaseOption, 163 Permissions_EnableOfflineWebApplicationCacheOption, 164 Permissions_EnablePluginsOption, 165 Permissions_EnablePointerLockOption, 166 Permissions_EnableWebglOption, 167 Permissions_ScriptsCanAccessClipboardOption, 168 Permissions_ScriptsCanChangeWindowGeometryOption, 169 Permissions_ScriptsCanCloseSelfOpenedWindowsOption, 170 Permissions_ScriptsCanCloseWindowsOption, 171 Permissions_ScriptsCanOpenWindowsOption, 172 Permissions_ScriptsCanReceiveRightClicksOption, 173 Permissions_ScriptsCanShowStatusMessagesOption, 174 Search_DefaultQuickSearchEngineOption, 175 Search_DefaultSearchEngineOption, 176 Search_EnableFindInPageAsYouTypeOption, 177 Search_EnableFindInPageHighlightAllOption, 178 Search_ReuseLastQuickFindQueryOption, 179 Search_SearchEnginesOrderOption, 180 Search_SearchEnginesSuggestionsOption, 181 Security_AllowMixedContentOption, 182 Security_CiphersOption, 183 Security_EnableFraudCheckingOption, 184 Security_IgnoreSslErrorsOption, 185 Sessions_DeferTabsLoadingOption, 186 Sessions_OpenInExistingWindowOption, 187 Sessions_OptionsExludedFromInheritingOption, 188 Sessions_OptionsExludedFromSavingOption, 189 SourceViewer_ShowLineNumbersOption, 190 SourceViewer_WrapLinesOption, 191 StartPage_BackgroundColorOption, 192 StartPage_BackgroundModeOption, 193 StartPage_BackgroundPathOption, 194 StartPage_BookmarksFolderOption, 195 StartPage_EnableStartPageOption, 196 StartPage_ShowAddTileOption, 197 StartPage_ShowSearchFieldOption, 198 StartPage_TileBackgroundModeOption, 199 StartPage_TileHeightOption, 200 StartPage_TileWidthOption, 201 StartPage_TilesPerRowOption, 202 StartPage_ZoomLevelOption, 203 TabBar_EnablePreviewsOption, 204 TabBar_EnableThumbnailsOption, 205 TabBar_MaximumTabHeightOption, 206 TabBar_MinimumTabHeightOption, 207 TabBar_MaximumTabWidthOption, 208 TabBar_MinimumTabWidthOption, 209 TabBar_OpenNextToActiveOption, 210 TabBar_PrependPinnedTabOption, 211 TabBar_PreviewsAnimationDurationOption, 212 TabBar_RequireModifierToSwitchTabOnScrollOption, 213 TabBar_ShowCloseButtonOption, 214 TabBar_ShowUrlIconOption, 215 TabSwitcher_IgnoreMinimizedTabsOption, 216 TabSwitcher_OrderByLastActivityOption, 217 TabSwitcher_ShowListOption, 218 Updates_ActiveChannelsOption, 219 Updates_AutomaticInstallOption, 220 Updates_CheckIntervalOption, 221 Updates_LastCheckOption, 222 Updates_ServerUrlOption 223 }; 224 225 enum OptionType 226 { 227 UnknownType = 0, 228 BooleanType, 229 ColorType, 230 EnumerationType, 231 FontType, 232 IconType, 233 IntegerType, 234 ListType, 235 PathType, 236 PasswordType, 237 StringType 238 }; 239 240 struct OptionDefinition final 241 { 242 struct Choice final 243 { 244 QString title; 245 QString value; 246 QIcon icon; 247 getTitlefinal::final248 QString getTitle() const 249 { 250 return (title.isEmpty() ? value : title); 251 } 252 isValidfinal::final253 bool isValid() const 254 { 255 return (!title.isEmpty() || !value.isEmpty()); 256 } 257 }; 258 259 enum OptionFlag 260 { 261 NoFlags = 0, 262 IsEnabledFlag = 1, 263 IsVisibleFlag = 2, 264 IsBuiltInFlag = 4, 265 RequiresRestartFlag = 8 266 }; 267 268 Q_DECLARE_FLAGS(OptionFlags, OptionFlag) 269 270 QVariant defaultValue; 271 QVector<Choice> choices; 272 OptionType type = UnknownType; 273 OptionFlags flags = static_cast<OptionFlags>(IsEnabledFlag | IsVisibleFlag); 274 int identifier = -1; 275 setChoicesfinal276 void setChoices(const QStringList &choicesValue) 277 { 278 choices.reserve(choicesValue.count()); 279 280 for (int i = 0; i < choicesValue.count(); ++i) 281 { 282 choices.append({{}, choicesValue.at(i), {}}); 283 } 284 } 285 hasIconsfinal286 bool hasIcons() const 287 { 288 for (int i = 0; i < choices.count(); ++i) 289 { 290 if (!choices.at(i).icon.isNull()) 291 { 292 return true; 293 } 294 } 295 296 return false; 297 } 298 }; 299 300 static void createInstance(const QString &path); 301 static void removeOverride(const QString &host, int identifier = -1); 302 static void updateOptionDefinition(int identifier, const OptionDefinition &definition); 303 static void setOption(int identifier, const QVariant &value, const QString &host = {}); 304 static SettingsManager* getInstance(); 305 static QString createDisplayValue(int identifier, const QVariant &value); 306 static QString createReport(); 307 static QString getGlobalPath(); 308 static QString getOverridePath(); 309 static QString getOptionName(int identifier); 310 static QVariant getOption(int identifier, const QString &host = {}); 311 static QStringList getOptions(); 312 static QStringList getOverrideHosts(); 313 static OptionDefinition getOptionDefinition(int identifier); 314 static int registerOption(const QString &name, OptionType type, const QVariant &defaultValue = {}, const QStringList &choices = {}, OptionDefinition::OptionFlags flags = static_cast<OptionDefinition::OptionFlags>(OptionDefinition::IsEnabledFlag | OptionDefinition::IsVisibleFlag)); 315 static int getOptionIdentifier(const QString &name); 316 static bool hasOverride(const QString &host, int identifier = -1); 317 318 protected: 319 explicit SettingsManager(QObject *parent); 320 321 static void registerOption(int identifier, OptionType type, const QVariant &defaultValue = {}, const QStringList &choices = {}, OptionDefinition::OptionFlags flags = static_cast<OptionDefinition::OptionFlags>(OptionDefinition::IsEnabledFlag |OptionDefinition:: IsVisibleFlag | OptionDefinition::IsBuiltInFlag)); 322 static void saveOption(const QString &path, const QString &key, const QVariant &value, OptionType type); 323 324 private: 325 static SettingsManager *m_instance; 326 static QString m_globalPath; 327 static QString m_overridePath; 328 static QVector<OptionDefinition> m_definitions; 329 static QHash<QString, int> m_customOptions; 330 static int m_identifierCounter; 331 static int m_optionIdentifierEnumerator; 332 static bool m_hasWildcardedOverrides; 333 334 signals: 335 void optionChanged(int identifier, const QVariant &value); 336 void hostOptionChanged(int identifier, const QVariant &value, const QString &host); 337 }; 338 339 } 340 341 Q_DECLARE_OPERATORS_FOR_FLAGS(Otter::SettingsManager::OptionDefinition::OptionFlags) 342 343 #endif 344