2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2014 sledgehammer999 <hammered999@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program 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 this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "settingsstorage.h"
43 // Encapsulates serialization of settings in "atomic" way.
44 // write() does not leave half-written files,
45 // read() has a workaround for a case of power loss during a previous serialization
46 class TransactionalSettings
49 explicit TransactionalSettings(const QString
&name
)
54 QVariantHash
read() const;
55 bool write(const QVariantHash
&data
) const;
58 // we return actual file names used by QSettings because
59 // there is no other way to get that name except
60 // actually create a QSettings object.
61 // if serialization operation was not successful we return empty string
62 QString
deserialize(const QString
&name
, QVariantHash
&data
) const;
63 QString
serialize(const QString
&name
, const QVariantHash
&data
) const;
68 QString
mapKey(const QString
&key
)
70 static const QHash
<QString
, QString
> keyMapping
=
72 {"BitTorrent/Session/MaxRatioAction", "Preferences/Bittorrent/MaxRatioAction"},
73 {"BitTorrent/Session/DefaultSavePath", "Preferences/Downloads/SavePath"},
74 {"BitTorrent/Session/TempPath", "Preferences/Downloads/TempPath"},
75 {"BitTorrent/Session/TempPathEnabled", "Preferences/Downloads/TempPathEnabled"},
76 {"BitTorrent/Session/AddTorrentPaused", "Preferences/Downloads/StartInPause"},
77 {"BitTorrent/Session/RefreshInterval", "Preferences/General/RefreshInterval"},
78 {"BitTorrent/Session/Preallocation", "Preferences/Downloads/PreAllocation"},
79 {"BitTorrent/Session/AddExtensionToIncompleteFiles", "Preferences/Downloads/UseIncompleteExtension"},
80 {"BitTorrent/Session/TorrentExportDirectory", "Preferences/Downloads/TorrentExportDir"},
81 {"BitTorrent/Session/FinishedTorrentExportDirectory", "Preferences/Downloads/FinishedTorrentExportDir"},
82 {"BitTorrent/Session/GlobalUPSpeedLimit", "Preferences/Connection/GlobalUPLimit"},
83 {"BitTorrent/Session/GlobalDLSpeedLimit", "Preferences/Connection/GlobalDLLimit"},
84 {"BitTorrent/Session/AlternativeGlobalUPSpeedLimit", "Preferences/Connection/GlobalUPLimitAlt"},
85 {"BitTorrent/Session/AlternativeGlobalDLSpeedLimit", "Preferences/Connection/GlobalDLLimitAlt"},
86 {"BitTorrent/Session/UseAlternativeGlobalSpeedLimit", "Preferences/Connection/alt_speeds_on"},
87 {"BitTorrent/Session/BandwidthSchedulerEnabled", "Preferences/Scheduler/Enabled"},
88 {"BitTorrent/Session/Port", "Preferences/Connection/PortRangeMin"},
89 {"BitTorrent/Session/UseRandomPort", "Preferences/General/UseRandomPort"},
90 {"BitTorrent/Session/Interface", "Preferences/Connection/Interface"},
91 {"BitTorrent/Session/InterfaceName", "Preferences/Connection/InterfaceName"},
92 {"BitTorrent/Session/InterfaceAddress", "Preferences/Connection/InterfaceAddress"},
93 {"BitTorrent/Session/SaveResumeDataInterval", "Preferences/Downloads/SaveResumeDataInterval"},
94 {"BitTorrent/Session/Encryption", "Preferences/Bittorrent/Encryption"},
95 {"BitTorrent/Session/ForceProxy", "Preferences/Connection/ProxyForce"},
96 {"BitTorrent/Session/ProxyPeerConnections", "Preferences/Connection/ProxyPeerConnections"},
97 {"BitTorrent/Session/MaxConnections", "Preferences/Bittorrent/MaxConnecs"},
98 {"BitTorrent/Session/MaxUploads", "Preferences/Bittorrent/MaxUploads"},
99 {"BitTorrent/Session/MaxConnectionsPerTorrent", "Preferences/Bittorrent/MaxConnecsPerTorrent"},
100 {"BitTorrent/Session/MaxUploadsPerTorrent", "Preferences/Bittorrent/MaxUploadsPerTorrent"},
101 {"BitTorrent/Session/DHTEnabled", "Preferences/Bittorrent/DHT"},
102 {"BitTorrent/Session/LSDEnabled", "Preferences/Bittorrent/LSD"},
103 {"BitTorrent/Session/PeXEnabled", "Preferences/Bittorrent/PeX"},
104 {"BitTorrent/Session/AddTrackersEnabled", "Preferences/Bittorrent/AddTrackers"},
105 {"BitTorrent/Session/AdditionalTrackers", "Preferences/Bittorrent/TrackersList"},
106 {"BitTorrent/Session/IPFilteringEnabled", "Preferences/IPFilter/Enabled"},
107 {"BitTorrent/Session/TrackerFilteringEnabled", "Preferences/IPFilter/FilterTracker"},
108 {"BitTorrent/Session/IPFilter", "Preferences/IPFilter/File"},
109 {"BitTorrent/Session/GlobalMaxRatio", "Preferences/Bittorrent/MaxRatio"},
110 {"BitTorrent/Session/AnnounceToAllTrackers", "Preferences/Advanced/AnnounceToAllTrackers"},
111 {"BitTorrent/Session/DiskCacheSize", "Preferences/Downloads/DiskWriteCacheSize"},
112 {"BitTorrent/Session/DiskCacheTTL", "Preferences/Downloads/DiskWriteCacheTTL"},
113 {"BitTorrent/Session/UseOSCache", "Preferences/Advanced/osCache"},
114 {"BitTorrent/Session/AnonymousModeEnabled", "Preferences/Advanced/AnonymousMode"},
115 {"BitTorrent/Session/QueueingSystemEnabled", "Preferences/Queueing/QueueingEnabled"},
116 {"BitTorrent/Session/MaxActiveDownloads", "Preferences/Queueing/MaxActiveDownloads"},
117 {"BitTorrent/Session/MaxActiveUploads", "Preferences/Queueing/MaxActiveUploads"},
118 {"BitTorrent/Session/MaxActiveTorrents", "Preferences/Queueing/MaxActiveTorrents"},
119 {"BitTorrent/Session/IgnoreSlowTorrentsForQueueing", "Preferences/Queueing/IgnoreSlowTorrents"},
120 {"BitTorrent/Session/OutgoingPortsMin", "Preferences/Advanced/OutgoingPortsMin"},
121 {"BitTorrent/Session/OutgoingPortsMax", "Preferences/Advanced/OutgoingPortsMax"},
122 {"BitTorrent/Session/IgnoreLimitsOnLAN", "Preferences/Advanced/IgnoreLimitsLAN"},
123 {"BitTorrent/Session/IncludeOverheadInLimits", "Preferences/Advanced/IncludeOverhead"},
124 {"BitTorrent/Session/AnnounceIP", "Preferences/Connection/InetAddress"},
125 {"BitTorrent/Session/SuperSeedingEnabled", "Preferences/Advanced/SuperSeeding"},
126 {"BitTorrent/Session/MaxHalfOpenConnections", "Preferences/Connection/MaxHalfOpenConnec"},
127 {"BitTorrent/Session/uTPEnabled", "Preferences/Bittorrent/uTP"},
128 {"BitTorrent/Session/uTPRateLimited", "Preferences/Bittorrent/uTP_rate_limited"},
129 {"BitTorrent/TrackerEnabled", "Preferences/Advanced/trackerEnabled"},
130 {"Network/Proxy/OnlyForTorrents", "Preferences/Connection/ProxyOnlyForTorrents"},
131 {"Network/Proxy/Type", "Preferences/Connection/ProxyType"},
132 {"Network/Proxy/Authentication", "Preferences/Connection/Proxy/Authentication"},
133 {"Network/Proxy/Username", "Preferences/Connection/Proxy/Username"},
134 {"Network/Proxy/Password", "Preferences/Connection/Proxy/Password"},
135 {"Network/Proxy/IP", "Preferences/Connection/Proxy/IP"},
136 {"Network/Proxy/Port", "Preferences/Connection/Proxy/Port"},
137 {"Network/PortForwardingEnabled", "Preferences/Connection/UPnP"},
138 {"AddNewTorrentDialog/TreeHeaderState", "AddNewTorrentDialog/qt5/treeHeaderState"},
139 {"AddNewTorrentDialog/Width", "AddNewTorrentDialog/width"},
140 {"AddNewTorrentDialog/Position", "AddNewTorrentDialog/y"},
141 {"AddNewTorrentDialog/Expanded", "AddNewTorrentDialog/expanded"},
142 {"AddNewTorrentDialog/SavePathHistory", "TorrentAdditionDlg/save_path_history"},
143 {"AddNewTorrentDialog/Enabled", "Preferences/Downloads/NewAdditionDialog"},
144 {"AddNewTorrentDialog/TopLevel", "Preferences/Downloads/NewAdditionDialogFront"},
146 {"State/BannedIPs", "Preferences/IPFilter/BannedIPs"}
149 return keyMapping
.value(key
, key
);
153 SettingsStorage
*SettingsStorage::m_instance
= nullptr;
155 SettingsStorage::SettingsStorage()
156 : m_data
{TransactionalSettings(QLatin1String("qBittorrent")).read()}
158 m_timer
.setSingleShot(true);
159 m_timer
.setInterval(5 * 1000);
160 connect(&m_timer
, &QTimer::timeout
, this, &SettingsStorage::save
);
163 SettingsStorage::~SettingsStorage()
168 void SettingsStorage::initInstance()
171 m_instance
= new SettingsStorage
;
174 void SettingsStorage::freeInstance()
177 m_instance
= nullptr;
180 SettingsStorage
*SettingsStorage::instance()
185 bool SettingsStorage::save()
187 const QWriteLocker
locker(&m_lock
); // guard for `m_dirty` too
188 if (!m_dirty
) return true;
190 const TransactionalSettings
settings(QLatin1String("qBittorrent"));
191 if (!settings
.write(m_data
))
201 QVariant
SettingsStorage::loadValueImpl(const QString
&key
, const QVariant
&defaultValue
) const
203 const QString realKey
= mapKey(key
);
204 const QReadLocker
locker(&m_lock
);
205 return m_data
.value(realKey
, defaultValue
);
208 void SettingsStorage::storeValueImpl(const QString
&key
, const QVariant
&value
)
210 const QString realKey
= mapKey(key
);
211 const QWriteLocker
locker(&m_lock
);
213 QVariant
¤tValue
= m_data
[realKey
];
214 if (currentValue
!= value
)
217 currentValue
= value
;
222 void SettingsStorage::removeValue(const QString
&key
)
224 const QString realKey
= mapKey(key
);
225 const QWriteLocker
locker(&m_lock
);
226 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
227 if (m_data
.remove(realKey
))
229 if (m_data
.remove(realKey
) > 0)
237 bool SettingsStorage::hasKey(const QString
&key
) const
239 const QString realKey
= mapKey(key
);
240 const QReadLocker locker
{&m_lock
};
241 return m_data
.contains(realKey
);
244 QVariantHash
TransactionalSettings::read() const
248 const QString newPath
= deserialize(m_name
+ QLatin1String("_new"), res
);
249 if (!newPath
.isEmpty())
250 { // "_new" file is NOT empty
251 // This means that the PC closed either due to power outage
252 // or because the disk was full. In any case the settings weren't transferred
253 // in their final position. So assume that qbittorrent_new.ini/qbittorrent_new.conf
254 // contains the most recent settings.
255 Logger::instance()->addMessage(QObject::tr("Detected unclean program exit. Using fallback file to restore settings: %1")
256 .arg(Utils::Fs::toNativePath(newPath
))
259 QString finalPath
= newPath
;
260 int index
= finalPath
.lastIndexOf("_new", -1, Qt::CaseInsensitive
);
261 finalPath
.remove(index
, 4);
263 Utils::Fs::forceRemove(finalPath
);
264 QFile::rename(newPath
, finalPath
);
268 deserialize(m_name
, res
);
274 bool TransactionalSettings::write(const QVariantHash
&data
) const
276 // QSettings deletes the file before writing it out. This can result in problems
277 // if the disk is full or a power outage occurs. Those events might occur
278 // between deleting the file and recreating it. This is a safety measure.
279 // Write everything to qBittorrent_new.ini/qBittorrent_new.conf and if it succeeds
280 // replace qBittorrent.ini/qBittorrent.conf with it.
281 const QString newPath
= serialize(m_name
+ QLatin1String("_new"), data
);
282 if (newPath
.isEmpty())
284 Utils::Fs::forceRemove(newPath
);
288 QString finalPath
= newPath
;
289 int index
= finalPath
.lastIndexOf("_new", -1, Qt::CaseInsensitive
);
290 finalPath
.remove(index
, 4);
292 Utils::Fs::forceRemove(finalPath
);
293 return QFile::rename(newPath
, finalPath
);
296 QString
TransactionalSettings::deserialize(const QString
&name
, QVariantHash
&data
) const
298 SettingsPtr settings
= Profile::instance()->applicationSettings(name
);
300 if (settings
->allKeys().isEmpty())
303 // Copy everything into memory. This means even keys inserted in the file manually
304 // or that we don't touch directly in this code (eg disabled by ifdef). This ensures
305 // that they will be copied over when save our settings to disk.
306 for (const QString
&key
: asConst(settings
->allKeys()))
308 const QVariant value
= settings
->value(key
);
313 return settings
->fileName();
316 QString
TransactionalSettings::serialize(const QString
&name
, const QVariantHash
&data
) const
318 SettingsPtr settings
= Profile::instance()->applicationSettings(name
);
319 for (auto i
= data
.begin(); i
!= data
.end(); ++i
)
320 settings
->setValue(i
.key(), i
.value());
322 settings
->sync(); // Important to get error status
324 switch (settings
->status())
326 case QSettings::NoError
:
327 return settings
->fileName();
328 case QSettings::AccessError
:
329 Logger::instance()->addMessage(QObject::tr("An access error occurred while trying to write the configuration file."), Log::CRITICAL
);
331 case QSettings::FormatError
:
332 Logger::instance()->addMessage(QObject::tr("A format error occurred while trying to write the configuration file."), Log::CRITICAL
);
335 Logger::instance()->addMessage(QObject::tr("An unknown error occurred while trying to write the configuration file."), Log::CRITICAL
);