2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
22 #include "UserAgentHelper.h"
24 // KDE Framework headers.
25 #include <KLocalizedString>
27 // Define the public database constants.
28 const QString UserAgentHelper::SYSTEM_DEFAULT_DATABASE = QStringLiteral("System Default");
29 const QString UserAgentHelper::PRIVACY_BROWSER_DATABASE = QStringLiteral("Privacy Browser");
30 const QString UserAgentHelper::FIREFOX_LINUX_DATABASE = QStringLiteral("Firefox Linux");
31 const QString UserAgentHelper::CHROMIUM_LINUX_DATABASE = QStringLiteral("Chromium Linux");
32 const QString UserAgentHelper::FIREFOX_WINDOWS_DATABASE = QStringLiteral("Firefox Windows");
33 const QString UserAgentHelper::CHROME_WINDOWS_DATABASE = QStringLiteral("Chrome Windows");
34 const QString UserAgentHelper::EDGE_WINDOWS_DATABASE = QStringLiteral("Edge Windows");
35 const QString UserAgentHelper::SAFARI_MACOS_DATABASE = QStringLiteral("Safari macOS");
37 // Define the public translated constants.
38 const QString UserAgentHelper::SYSTEM_DEFAULT_TRANSLATED = i18n("System default");
39 const QString UserAgentHelper::PRIVACY_BROWSER_TRANSLATED = i18n("Privacy Browser");
40 const QString UserAgentHelper::FIREFOX_LINUX_TRANSLATED = i18n("Firefox on Linux");
41 const QString UserAgentHelper::CHROMIUM_LINUX_TRANSLATED = i18n("Chromium on Linux");
42 const QString UserAgentHelper::FIREFOX_WINDOWS_TRANSLATED = i18n("Firefox on Windows");
43 const QString UserAgentHelper::CHROME_WINDOWS_TRANSLATED = i18n("Chrome on Windows");
44 const QString UserAgentHelper::EDGE_WINDOWS_TRANSLATED = i18n("Edge on Windows");
45 const QString UserAgentHelper::SAFARI_MACOS_TRANSLATED = i18n("Safari on macOS");
47 // Define the public user agent constants.
48 const QString UserAgentHelper::PRIVACY_BROWSER_USER_AGENT = QStringLiteral("PrivacyBrowser/1.0");
49 const QString UserAgentHelper::FIREFOX_LINUX_USER_AGENT = QStringLiteral("Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0");
50 const QString UserAgentHelper::CHROMIUM_LINUX_USER_AGENT = QStringLiteral("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36");
51 const QString UserAgentHelper::FIREFOX_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0");
52 const QString UserAgentHelper::CHROME_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36");
53 const QString UserAgentHelper::EDGE_WINDOWS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.56");
54 const QString UserAgentHelper::SAFARI_MACOS_USER_AGENT = QStringLiteral("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15");
56 // The default constructor.
57 UserAgentHelper::UserAgentHelper() {};
59 QString UserAgentHelper::getUserAgentFromDatabaseName(const QString &userAgentDatabaseName)
61 if (userAgentDatabaseName == PRIVACY_BROWSER_DATABASE) return PRIVACY_BROWSER_USER_AGENT; // Privacy Browser.
62 else if (userAgentDatabaseName == FIREFOX_LINUX_DATABASE) return FIREFOX_LINUX_USER_AGENT; // Firefox Linux.
63 else if (userAgentDatabaseName == CHROMIUM_LINUX_DATABASE) return CHROMIUM_LINUX_USER_AGENT; // Chromium Linux.
64 else if (userAgentDatabaseName == FIREFOX_WINDOWS_DATABASE) return FIREFOX_WINDOWS_USER_AGENT; // Firefox Windows.
65 else if (userAgentDatabaseName == CHROME_WINDOWS_DATABASE) return CHROME_WINDOWS_USER_AGENT; // Chrome Windows.
66 else if (userAgentDatabaseName == EDGE_WINDOWS_DATABASE) return EDGE_WINDOWS_USER_AGENT; // Edge Windows.
67 else if (userAgentDatabaseName == SAFARI_MACOS_DATABASE) return SAFARI_MACOS_USER_AGENT; // Safari macOS.
68 else return userAgentDatabaseName; // Return the custom user agent.
71 QString UserAgentHelper::getUserAgentFromTranslatedName(const QString &userAgentTranslatedName)
73 if (userAgentTranslatedName == PRIVACY_BROWSER_TRANSLATED) return PRIVACY_BROWSER_USER_AGENT; // Privacy Browser.
74 else if (userAgentTranslatedName == FIREFOX_LINUX_TRANSLATED) return FIREFOX_LINUX_USER_AGENT; // Firefox on Linux.
75 else if (userAgentTranslatedName == CHROMIUM_LINUX_TRANSLATED) return CHROMIUM_LINUX_USER_AGENT; // Chromium on Linux.
76 else if (userAgentTranslatedName == FIREFOX_WINDOWS_TRANSLATED) return FIREFOX_WINDOWS_USER_AGENT; // Firefox on Windows.
77 else if (userAgentTranslatedName == CHROME_WINDOWS_TRANSLATED) return CHROME_WINDOWS_USER_AGENT; // Chrome on Windows.
78 else if (userAgentTranslatedName == EDGE_WINDOWS_TRANSLATED) return EDGE_WINDOWS_USER_AGENT; // Edge on Windows.
79 else if (userAgentTranslatedName == SAFARI_MACOS_TRANSLATED) return SAFARI_MACOS_USER_AGENT; // Safari on macOS.
80 else return userAgentTranslatedName; // Return the custom user agent.
83 QString UserAgentHelper::getDatabaseUserAgentName(const QString &translatedUserAgentName)
85 // Return the database user agent name.
86 if (translatedUserAgentName == SYSTEM_DEFAULT_TRANSLATED) return SYSTEM_DEFAULT_DATABASE; // System Default.
87 else if (translatedUserAgentName == PRIVACY_BROWSER_TRANSLATED) return PRIVACY_BROWSER_DATABASE; // Privacy Browser.
88 else if (translatedUserAgentName == FIREFOX_LINUX_TRANSLATED) return FIREFOX_LINUX_DATABASE; // Firefox Linux.
89 else if (translatedUserAgentName == CHROMIUM_LINUX_TRANSLATED) return CHROMIUM_LINUX_DATABASE; // Chromium Linux.
90 else if (translatedUserAgentName == FIREFOX_WINDOWS_TRANSLATED) return FIREFOX_WINDOWS_DATABASE; // Firefox Windows.
91 else if (translatedUserAgentName == CHROME_WINDOWS_TRANSLATED) return CHROME_WINDOWS_DATABASE; // Chrome Windows.
92 else if (translatedUserAgentName == EDGE_WINDOWS_TRANSLATED) return EDGE_WINDOWS_DATABASE; // Edge Windows.
93 else if (translatedUserAgentName == SAFARI_MACOS_TRANSLATED) return SAFARI_MACOS_DATABASE; // Safari macOS.
94 else return translatedUserAgentName; // Return the custom user agent.
97 int UserAgentHelper::getDomainSettingsUserAgentIndex(const QString &userAgentName)
99 // Return the domain settings user agent index.
100 if (userAgentName == SYSTEM_DEFAULT_DATABASE) return 0; // System Default.
101 else if (userAgentName == PRIVACY_BROWSER_DATABASE) return 1; // Privacy Browser.
102 else if (userAgentName == FIREFOX_LINUX_DATABASE) return 2; // Firefox Linux.
103 else if (userAgentName == CHROMIUM_LINUX_DATABASE) return 3; // Chromium Linux.
104 else if (userAgentName == FIREFOX_WINDOWS_DATABASE) return 4; // Firefox Windows.
105 else if (userAgentName == CHROME_WINDOWS_DATABASE) return 5; // Chrome Windows.
106 else if (userAgentName == EDGE_WINDOWS_DATABASE) return 6; // Edge Windows.
107 else if (userAgentName == SAFARI_MACOS_DATABASE) return 7; // Safari macOS.
108 else return -1; // Custom user agent.
111 QString UserAgentHelper::getResultingDomainSettingsUserAgent(const QString &rawUserAgent)
113 // Return the resulting user agent.
114 if (rawUserAgent == SYSTEM_DEFAULT_DATABASE) return getUserAgentFromDatabaseName(Settings::userAgent()); // Return the default user agent.
115 else return getUserAgentFromDatabaseName(rawUserAgent); // Return the domain user agent.
118 QString UserAgentHelper::getTranslatedUserAgentName(const QString &userAgentName)
120 // Return the translated user agent name.
121 if (userAgentName == PRIVACY_BROWSER_DATABASE) return PRIVACY_BROWSER_TRANSLATED; // Privacy Browser.
122 else if (userAgentName == FIREFOX_LINUX_DATABASE) return FIREFOX_LINUX_TRANSLATED; // Firefox on Linux.
123 else if (userAgentName == CHROMIUM_LINUX_DATABASE) return CHROMIUM_LINUX_TRANSLATED; // Chromium on Linux.
124 else if (userAgentName == FIREFOX_WINDOWS_DATABASE) return FIREFOX_WINDOWS_TRANSLATED; // Firefox on Windows.
125 else if (userAgentName == CHROME_WINDOWS_DATABASE) return CHROME_WINDOWS_TRANSLATED; // Chrome on Windows.
126 else if (userAgentName == EDGE_WINDOWS_DATABASE) return EDGE_WINDOWS_TRANSLATED; // Edge on Windows.
127 else if (userAgentName == SAFARI_MACOS_DATABASE) return SAFARI_MACOS_TRANSLATED; // Safari on macOS.
128 else return userAgentName; // Return the custom user agent.