2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2019 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
32 #include <QCoreApplication>
35 #include "base/bittorrent/torrentcontentlayout.h"
36 #include "base/global.h"
37 #include "base/logger.h"
38 #include "base/net/proxyconfigurationmanager.h"
39 #include "base/preferences.h"
40 #include "base/profile.h"
41 #include "base/settingsstorage.h"
42 #include "base/settingvalue.h"
43 #include "base/utils/io.h"
44 #include "base/utils/string.h"
48 const int MIGRATION_VERSION
= 6;
49 const QString MIGRATION_VERSION_KEY
= u
"Meta/MigrationVersion"_s
;
51 void exportWebUIHttpsFiles()
53 const auto migrate
= [](const QString
&oldKey
, const QString
&newKey
, const Path
&savePath
)
55 SettingsStorage
*settingsStorage
{SettingsStorage::instance()};
56 const auto oldData
{settingsStorage
->loadValue
<QByteArray
>(oldKey
)};
57 const auto newData
{settingsStorage
->loadValue
<QString
>(newKey
)};
58 const QString errorMsgFormat
{QCoreApplication::translate("Upgrade", "Migrate preferences failed: WebUI https, file: \"%1\", error: \"%2\"")};
60 if (!newData
.isEmpty() || oldData
.isEmpty())
63 const nonstd::expected
<void, QString
> result
= Utils::IO::saveToFile(savePath
, oldData
);
66 LogMsg(errorMsgFormat
.arg(savePath
.toString(), result
.error()) , Log::WARNING
);
70 settingsStorage
->storeValue(newKey
, savePath
);
71 settingsStorage
->removeValue(oldKey
);
73 LogMsg(QCoreApplication::translate("Upgrade", "Migrated preferences: WebUI https, exported data to file: \"%1\"").arg(savePath
.toString())
77 const Path configPath
= specialFolderLocation(SpecialFolder::Config
);
78 migrate(u
"Preferences/WebUI/HTTPS/Certificate"_s
79 , u
"Preferences/WebUI/HTTPS/CertificatePath"_s
80 , (configPath
/ Path(u
"WebUICertificate.crt"_s
)));
81 migrate(u
"Preferences/WebUI/HTTPS/Key"_s
82 , u
"Preferences/WebUI/HTTPS/KeyPath"_s
83 , (configPath
/ Path(u
"WebUIPrivateKey.pem"_s
)));
86 void upgradeTorrentContentLayout()
88 const QString oldKey
= u
"BitTorrent/Session/CreateTorrentSubfolder"_s
;
89 const QString newKey
= u
"BitTorrent/Session/TorrentContentLayout"_s
;
91 SettingsStorage
*settingsStorage
{SettingsStorage::instance()};
92 const auto oldData
{settingsStorage
->loadValue
<QVariant
>(oldKey
)};
93 const auto newData
{settingsStorage
->loadValue
<QString
>(newKey
)};
95 if (!newData
.isEmpty() || !oldData
.isValid())
98 const bool createSubfolder
= oldData
.toBool();
99 const BitTorrent::TorrentContentLayout torrentContentLayout
=
100 (createSubfolder
? BitTorrent::TorrentContentLayout::Original
: BitTorrent::TorrentContentLayout::NoSubfolder
);
102 settingsStorage
->storeValue(newKey
, Utils::String::fromEnum(torrentContentLayout
));
103 settingsStorage
->removeValue(oldKey
);
106 void upgradeListenPortSettings()
108 const auto oldKey
= u
"BitTorrent/Session/UseRandomPort"_s
;
109 const auto newKey
= u
"Preferences/Connection/PortRangeMin"_s
;
110 auto *settingsStorage
= SettingsStorage::instance();
112 if (settingsStorage
->hasKey(oldKey
))
114 if (settingsStorage
->loadValue
<bool>(oldKey
))
115 settingsStorage
->storeValue(newKey
, 0);
117 settingsStorage
->removeValue(oldKey
);
121 void upgradeSchedulerDaysSettings()
123 auto *settingsStorage
= SettingsStorage::instance();
124 const auto key
= u
"Preferences/Scheduler/days"_s
;
125 const auto value
= settingsStorage
->loadValue
<QString
>(key
);
128 const auto number
= value
.toInt(&ok
);
135 settingsStorage
->storeValue(key
, Scheduler::Days::EveryDay
);
138 settingsStorage
->storeValue(key
, Scheduler::Days::Weekday
);
141 settingsStorage
->storeValue(key
, Scheduler::Days::Weekend
);
144 settingsStorage
->storeValue(key
, Scheduler::Days::Monday
);
147 settingsStorage
->storeValue(key
, Scheduler::Days::Tuesday
);
150 settingsStorage
->storeValue(key
, Scheduler::Days::Wednesday
);
153 settingsStorage
->storeValue(key
, Scheduler::Days::Thursday
);
156 settingsStorage
->storeValue(key
, Scheduler::Days::Friday
);
159 settingsStorage
->storeValue(key
, Scheduler::Days::Saturday
);
162 settingsStorage
->storeValue(key
, Scheduler::Days::Sunday
);
165 LogMsg(QCoreApplication::translate("Upgrade", "Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".")
166 .arg(key
, QString::number(number
)), Log::WARNING
);
167 settingsStorage
->removeValue(key
);
173 void upgradeDNSServiceSettings()
175 auto *settingsStorage
= SettingsStorage::instance();
176 const auto key
= u
"Preferences/DynDNS/Service"_s
;
177 const auto value
= settingsStorage
->loadValue
<QString
>(key
);
180 const auto number
= value
.toInt(&ok
);
187 settingsStorage
->storeValue(key
, DNS::Service::None
);
190 settingsStorage
->storeValue(key
, DNS::Service::DynDNS
);
193 settingsStorage
->storeValue(key
, DNS::Service::NoIP
);
196 LogMsg(QCoreApplication::translate("Upgrade", "Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".")
197 .arg(key
, QString::number(number
)), Log::WARNING
);
198 settingsStorage
->removeValue(key
);
204 void upgradeTrayIconStyleSettings()
206 auto *settingsStorage
= SettingsStorage::instance();
207 const auto key
= u
"Preferences/Advanced/TrayIconStyle"_s
;
208 const auto value
= settingsStorage
->loadValue
<QString
>(key
);
211 const auto number
= value
.toInt(&ok
);
218 settingsStorage
->storeValue(key
, TrayIcon::Style::Normal
);
221 settingsStorage
->storeValue(key
, TrayIcon::Style::MonoDark
);
224 settingsStorage
->storeValue(key
, TrayIcon::Style::MonoLight
);
227 LogMsg(QCoreApplication::translate("Upgrade", "Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".")
228 .arg(key
, QString::number(number
)), Log::WARNING
);
229 settingsStorage
->removeValue(key
);
235 void migrateSettingKeys()
243 const KeyMapping mappings
[] =
245 {u
"AddNewTorrentDialog/Enabled"_s
, u
"Preferences/Downloads/NewAdditionDialog"_s
},
246 {u
"AddNewTorrentDialog/Expanded"_s
, u
"AddNewTorrentDialog/expanded"_s
},
247 {u
"AddNewTorrentDialog/Position"_s
, u
"AddNewTorrentDialog/y"_s
},
248 {u
"AddNewTorrentDialog/SavePathHistory"_s
, u
"TorrentAdditionDlg/save_path_history"_s
},
249 {u
"AddNewTorrentDialog/TopLevel"_s
, u
"Preferences/Downloads/NewAdditionDialogFront"_s
},
250 {u
"AddNewTorrentDialog/TreeHeaderState"_s
, u
"AddNewTorrentDialog/qt5/treeHeaderState"_s
},
251 {u
"AddNewTorrentDialog/Width"_s
, u
"AddNewTorrentDialog/width"_s
},
252 {u
"BitTorrent/Session/AddExtensionToIncompleteFiles"_s
, u
"Preferences/Downloads/UseIncompleteExtension"_s
},
253 {u
"BitTorrent/Session/AdditionalTrackers"_s
, u
"Preferences/Bittorrent/TrackersList"_s
},
254 {u
"BitTorrent/Session/AddTorrentPaused"_s
, u
"Preferences/Downloads/StartInPause"_s
},
255 {u
"BitTorrent/Session/AddTrackersEnabled"_s
, u
"Preferences/Bittorrent/AddTrackers"_s
},
256 {u
"BitTorrent/Session/AlternativeGlobalDLSpeedLimit"_s
, u
"Preferences/Connection/GlobalDLLimitAlt"_s
},
257 {u
"BitTorrent/Session/AlternativeGlobalUPSpeedLimit"_s
, u
"Preferences/Connection/GlobalUPLimitAlt"_s
},
258 {u
"BitTorrent/Session/AnnounceIP"_s
, u
"Preferences/Connection/InetAddress"_s
},
259 {u
"BitTorrent/Session/AnnounceToAllTrackers"_s
, u
"Preferences/Advanced/AnnounceToAllTrackers"_s
},
260 {u
"BitTorrent/Session/AnonymousModeEnabled"_s
, u
"Preferences/Advanced/AnonymousMode"_s
},
261 {u
"BitTorrent/Session/BandwidthSchedulerEnabled"_s
, u
"Preferences/Scheduler/Enabled"_s
},
262 {u
"BitTorrent/Session/DefaultSavePath"_s
, u
"Preferences/Downloads/SavePath"_s
},
263 {u
"BitTorrent/Session/DHTEnabled"_s
, u
"Preferences/Bittorrent/DHT"_s
},
264 {u
"BitTorrent/Session/DiskCacheSize"_s
, u
"Preferences/Downloads/DiskWriteCacheSize"_s
},
265 {u
"BitTorrent/Session/DiskCacheTTL"_s
, u
"Preferences/Downloads/DiskWriteCacheTTL"_s
},
266 {u
"BitTorrent/Session/Encryption"_s
, u
"Preferences/Bittorrent/Encryption"_s
},
267 {u
"BitTorrent/Session/FinishedTorrentExportDirectory"_s
, u
"Preferences/Downloads/FinishedTorrentExportDir"_s
},
268 {u
"BitTorrent/Session/ForceProxy"_s
, u
"Preferences/Connection/ProxyForce"_s
},
269 {u
"BitTorrent/Session/GlobalDLSpeedLimit"_s
, u
"Preferences/Connection/GlobalDLLimit"_s
},
270 {u
"BitTorrent/Session/GlobalMaxRatio"_s
, u
"Preferences/Bittorrent/MaxRatio"_s
},
271 {u
"BitTorrent/Session/GlobalUPSpeedLimit"_s
, u
"Preferences/Connection/GlobalUPLimit"_s
},
272 {u
"BitTorrent/Session/IgnoreLimitsOnLAN"_s
, u
"Preferences/Advanced/IgnoreLimitsLAN"_s
},
273 {u
"BitTorrent/Session/IgnoreSlowTorrentsForQueueing"_s
, u
"Preferences/Queueing/IgnoreSlowTorrents"_s
},
274 {u
"BitTorrent/Session/IncludeOverheadInLimits"_s
, u
"Preferences/Advanced/IncludeOverhead"_s
},
275 {u
"BitTorrent/Session/Interface"_s
, u
"Preferences/Connection/Interface"_s
},
276 {u
"BitTorrent/Session/InterfaceAddress"_s
, u
"Preferences/Connection/InterfaceAddress"_s
},
277 {u
"BitTorrent/Session/InterfaceName"_s
, u
"Preferences/Connection/InterfaceName"_s
},
278 {u
"BitTorrent/Session/IPFilter"_s
, u
"Preferences/IPFilter/File"_s
},
279 {u
"BitTorrent/Session/IPFilteringEnabled"_s
, u
"Preferences/IPFilter/Enabled"_s
},
280 {u
"BitTorrent/Session/LSDEnabled"_s
, u
"Preferences/Bittorrent/LSD"_s
},
281 {u
"BitTorrent/Session/MaxActiveDownloads"_s
, u
"Preferences/Queueing/MaxActiveDownloads"_s
},
282 {u
"BitTorrent/Session/MaxActiveTorrents"_s
, u
"Preferences/Queueing/MaxActiveTorrents"_s
},
283 {u
"BitTorrent/Session/MaxActiveUploads"_s
, u
"Preferences/Queueing/MaxActiveUploads"_s
},
284 {u
"BitTorrent/Session/MaxConnections"_s
, u
"Preferences/Bittorrent/MaxConnecs"_s
},
285 {u
"BitTorrent/Session/MaxConnectionsPerTorrent"_s
, u
"Preferences/Bittorrent/MaxConnecsPerTorrent"_s
},
286 {u
"BitTorrent/Session/MaxHalfOpenConnections"_s
, u
"Preferences/Connection/MaxHalfOpenConnec"_s
},
287 {u
"BitTorrent/Session/MaxRatioAction"_s
, u
"Preferences/Bittorrent/MaxRatioAction"_s
},
288 {u
"BitTorrent/Session/MaxUploads"_s
, u
"Preferences/Bittorrent/MaxUploads"_s
},
289 {u
"BitTorrent/Session/MaxUploadsPerTorrent"_s
, u
"Preferences/Bittorrent/MaxUploadsPerTorrent"_s
},
290 {u
"BitTorrent/Session/OutgoingPortsMax"_s
, u
"Preferences/Advanced/OutgoingPortsMax"_s
},
291 {u
"BitTorrent/Session/OutgoingPortsMin"_s
, u
"Preferences/Advanced/OutgoingPortsMin"_s
},
292 {u
"BitTorrent/Session/PeXEnabled"_s
, u
"Preferences/Bittorrent/PeX"_s
},
293 {u
"BitTorrent/Session/Port"_s
, u
"Preferences/Connection/PortRangeMin"_s
},
294 {u
"BitTorrent/Session/Preallocation"_s
, u
"Preferences/Downloads/PreAllocation"_s
},
295 {u
"BitTorrent/Session/ProxyPeerConnections"_s
, u
"Preferences/Connection/ProxyPeerConnections"_s
},
296 {u
"BitTorrent/Session/QueueingSystemEnabled"_s
, u
"Preferences/Queueing/QueueingEnabled"_s
},
297 {u
"BitTorrent/Session/RefreshInterval"_s
, u
"Preferences/General/RefreshInterval"_s
},
298 {u
"BitTorrent/Session/SaveResumeDataInterval"_s
, u
"Preferences/Downloads/SaveResumeDataInterval"_s
},
299 {u
"BitTorrent/Session/SuperSeedingEnabled"_s
, u
"Preferences/Advanced/SuperSeeding"_s
},
300 {u
"BitTorrent/Session/TempPath"_s
, u
"Preferences/Downloads/TempPath"_s
},
301 {u
"BitTorrent/Session/TempPathEnabled"_s
, u
"Preferences/Downloads/TempPathEnabled"_s
},
302 {u
"BitTorrent/Session/TorrentExportDirectory"_s
, u
"Preferences/Downloads/TorrentExportDir"_s
},
303 {u
"BitTorrent/Session/TrackerFilteringEnabled"_s
, u
"Preferences/IPFilter/FilterTracker"_s
},
304 {u
"BitTorrent/Session/UseAlternativeGlobalSpeedLimit"_s
, u
"Preferences/Connection/alt_speeds_on"_s
},
305 {u
"BitTorrent/Session/UseOSCache"_s
, u
"Preferences/Advanced/osCache"_s
},
306 {u
"BitTorrent/Session/UseRandomPort"_s
, u
"Preferences/General/UseRandomPort"_s
},
307 {u
"BitTorrent/Session/uTPEnabled"_s
, u
"Preferences/Bittorrent/uTP"_s
},
308 {u
"BitTorrent/Session/uTPRateLimited"_s
, u
"Preferences/Bittorrent/uTP_rate_limited"_s
},
309 {u
"BitTorrent/TrackerEnabled"_s
, u
"Preferences/Advanced/trackerEnabled"_s
},
310 {u
"Network/PortForwardingEnabled"_s
, u
"Preferences/Connection/UPnP"_s
},
311 {u
"Network/Proxy/Authentication"_s
, u
"Preferences/Connection/Proxy/Authentication"_s
},
312 {u
"Network/Proxy/IP"_s
, u
"Preferences/Connection/Proxy/IP"_s
},
313 {u
"Network/Proxy/OnlyForTorrents"_s
, u
"Preferences/Connection/ProxyOnlyForTorrents"_s
},
314 {u
"Network/Proxy/Password"_s
, u
"Preferences/Connection/Proxy/Password"_s
},
315 {u
"Network/Proxy/Port"_s
, u
"Preferences/Connection/Proxy/Port"_s
},
316 {u
"Network/Proxy/Type"_s
, u
"Preferences/Connection/ProxyType"_s
},
317 {u
"Network/Proxy/Username"_s
, u
"Preferences/Connection/Proxy/Username"_s
},
318 {u
"State/BannedIPs"_s
, u
"Preferences/IPFilter/BannedIPs"_s
}
321 auto *settingsStorage
= SettingsStorage::instance();
322 for (const KeyMapping
&mapping
: mappings
)
324 if (settingsStorage
->hasKey(mapping
.oldKey
))
326 const auto value
= settingsStorage
->loadValue
<QVariant
>(mapping
.oldKey
);
327 settingsStorage
->storeValue(mapping
.newKey
, value
);
328 // TODO: Remove oldKey after ~v4.4.3 and bump migration version
333 void migrateProxySettingsEnum()
335 auto *settingsStorage
= SettingsStorage::instance();
336 const auto key
= u
"Network/Proxy/Type"_s
;
337 const auto value
= settingsStorage
->loadValue
<QString
>(key
);
340 const auto number
= value
.toInt(&ok
);
347 settingsStorage
->storeValue(key
, Net::ProxyType::None
);
350 settingsStorage
->storeValue(key
, Net::ProxyType::HTTP
);
353 settingsStorage
->storeValue(key
, Net::ProxyType::SOCKS5
);
356 settingsStorage
->storeValue(key
, u
"HTTP_PW"_s
);
359 settingsStorage
->storeValue(key
, u
"SOCKS5_PW"_s
);
362 settingsStorage
->storeValue(key
, Net::ProxyType::SOCKS4
);
365 LogMsg(QCoreApplication::translate("Upgrade", "Invalid value found in configuration file, reverting it to default. Key: \"%1\". Invalid value: \"%2\".")
366 .arg(key
, QString::number(number
)), Log::WARNING
);
367 settingsStorage
->removeValue(key
);
373 void migrateProxySettings()
375 auto *settingsStorage
= SettingsStorage::instance();
376 const auto proxyType
= settingsStorage
->loadValue
<QString
>(u
"Network/Proxy/Type"_s
, u
"None"_s
);
377 const auto onlyForTorrents
= settingsStorage
->loadValue
<bool>(u
"Network/Proxy/OnlyForTorrents"_s
)
378 || (proxyType
== u
"SOCKS4");
380 settingsStorage
->storeValue(u
"Network/Proxy/Profiles/BitTorrent"_s
, true);
381 settingsStorage
->storeValue(u
"Network/Proxy/Profiles/RSS"_s
, !onlyForTorrents
);
382 settingsStorage
->storeValue(u
"Network/Proxy/Profiles/Misc"_s
, !onlyForTorrents
);
384 if (proxyType
== u
"HTTP_PW"_s
)
386 settingsStorage
->storeValue(u
"Network/Proxy/Type"_s
, Net::ProxyType::HTTP
);
387 settingsStorage
->storeValue(u
"Network/Proxy/AuthEnabled"_s
, true);
389 else if (proxyType
== u
"SOCKS5_PW"_s
)
391 settingsStorage
->storeValue(u
"Network/Proxy/Type"_s
, Net::ProxyType::SOCKS5
);
392 settingsStorage
->storeValue(u
"Network/Proxy/AuthEnabled"_s
, true);
395 settingsStorage
->removeValue(u
"Network/Proxy/OnlyForTorrents"_s
);
397 const auto proxyHostnameLookup
= settingsStorage
->loadValue
<bool>(u
"BitTorrent/Session/ProxyHostnameLookup"_s
);
398 settingsStorage
->storeValue(u
"Network/Proxy/HostnameLookupEnabled"_s
, proxyHostnameLookup
);
399 settingsStorage
->removeValue(u
"BitTorrent/Session/ProxyHostnameLookup"_s
);
403 void migrateMemoryPrioritySettings()
405 auto *settingsStorage
= SettingsStorage::instance();
406 const QString oldKey
= u
"BitTorrent/OSMemoryPriority"_s
;
407 const QString newKey
= u
"Application/ProcessMemoryPriority"_s
;
409 if (settingsStorage
->hasKey(oldKey
))
411 const auto value
= settingsStorage
->loadValue
<QVariant
>(oldKey
);
412 settingsStorage
->storeValue(newKey
, value
);
417 void migrateStartupWindowState()
419 auto *settingsStorage
= SettingsStorage::instance();
420 if (settingsStorage
->hasKey(u
"Preferences/General/StartMinimized"_s
))
422 const auto startMinimized
= settingsStorage
->loadValue
<bool>(u
"Preferences/General/StartMinimized"_s
);
423 const auto minimizeToTray
= settingsStorage
->loadValue
<bool>(u
"Preferences/General/MinimizeToTray"_s
);
424 const QString windowState
= startMinimized
? (minimizeToTray
? u
"Hidden"_s
: u
"Minimized"_s
) : u
"Normal"_s
;
425 settingsStorage
->storeValue(u
"GUI/StartUpWindowState"_s
, windowState
);
429 void migrateChineseLocale()
431 auto *settingsStorage
= SettingsStorage::instance();
432 const auto key
= u
"Preferences/General/Locale"_s
;
433 if (settingsStorage
->hasKey(key
))
435 const auto locale
= settingsStorage
->loadValue
<QString
>(key
);
436 if (locale
.compare(u
"zh"_s
, Qt::CaseInsensitive
) == 0)
437 settingsStorage
->storeValue(key
, u
"zh_CN"_s
);
444 CachedSettingValue
<int> version
{MIGRATION_VERSION_KEY
, 0};
446 if (version
!= MIGRATION_VERSION
)
450 exportWebUIHttpsFiles();
451 upgradeTorrentContentLayout();
452 upgradeListenPortSettings();
453 upgradeSchedulerDaysSettings();
454 upgradeDNSServiceSettings();
455 upgradeTrayIconStyleSettings();
459 migrateSettingKeys();
462 migrateProxySettingsEnum();
466 migrateMemoryPrioritySettings();
471 migrateStartupWindowState();
472 migrateChineseLocale();
476 migrateProxySettings();
478 version
= MIGRATION_VERSION
;
484 void setCurrentMigrationVersion()
486 SettingsStorage::instance()->storeValue(MIGRATION_VERSION_KEY
, MIGRATION_VERSION
);
489 void handleChangedDefaults(const DefaultPreferencesMode mode
)
498 const DefaultValue changedDefaults
[] =
500 {u
"BitTorrent/Session/QueueingSystemEnabled"_s
, true, false}
503 auto *settingsStorage
= SettingsStorage::instance();
504 for (const DefaultValue
&value
: changedDefaults
)
506 if (!settingsStorage
->hasKey(value
.name
))
508 settingsStorage
->storeValue(value
.name
509 , (mode
== DefaultPreferencesMode::Legacy
? value
.legacy
: value
.current
));