mirror of
https://github.com/zealdocs/zeal.git
synced 2026-08-29 08:34:50 +08:00
refactor: prefer stack objects over QScopedPointer (#1876)
This commit is contained in:
+35
-38
@@ -157,27 +157,27 @@ void registerProtocolHandler(const QString &scheme, const QString &description)
|
||||
const QString appPath = QDir::toNativeSeparators(QCoreApplication::applicationFilePath());
|
||||
const QString regPath = QStringLiteral("HKEY_CURRENT_USER\\Software\\Classes\\") + scheme;
|
||||
|
||||
QScopedPointer<QSettings> reg(new QSettings(regPath, QSettings::NativeFormat));
|
||||
QSettings registry(regPath, QSettings::NativeFormat);
|
||||
|
||||
reg->setValue(QStringLiteral("Default"), description);
|
||||
reg->setValue(QStringLiteral("URL Protocol"), QString());
|
||||
registry.setValue(QStringLiteral("Default"), description);
|
||||
registry.setValue(QStringLiteral("URL Protocol"), QString());
|
||||
|
||||
reg->beginGroup(QStringLiteral("DefaultIcon"));
|
||||
reg->setValue(QStringLiteral("Default"), QString("%1,1").arg(appPath));
|
||||
reg->endGroup();
|
||||
registry.beginGroup(QStringLiteral("DefaultIcon"));
|
||||
registry.setValue(QStringLiteral("Default"), QString("%1,1").arg(appPath));
|
||||
registry.endGroup();
|
||||
|
||||
reg->beginGroup(QStringLiteral("shell"));
|
||||
reg->beginGroup(QStringLiteral("open"));
|
||||
reg->beginGroup(QStringLiteral("command"));
|
||||
reg->setValue(QStringLiteral("Default"), QVariant(appPath + QLatin1String(" %1")));
|
||||
registry.beginGroup(QStringLiteral("shell"));
|
||||
registry.beginGroup(QStringLiteral("open"));
|
||||
registry.beginGroup(QStringLiteral("command"));
|
||||
registry.setValue(QStringLiteral("Default"), QVariant(appPath + QLatin1String(" %1")));
|
||||
}
|
||||
|
||||
void registerProtocolHandlers(const QHash<QString, QString> &protocols, bool force = false)
|
||||
{
|
||||
const QString regPath = QStringLiteral("HKEY_CURRENT_USER\\Software\\Classes");
|
||||
QScopedPointer<QSettings> reg(new QSettings(regPath, QSettings::NativeFormat));
|
||||
const QSettings registry(regPath, QSettings::NativeFormat);
|
||||
|
||||
const QStringList groups = reg->childGroups();
|
||||
const QStringList groups = registry.childGroups();
|
||||
for (auto it = protocols.cbegin(); it != protocols.cend(); ++it) {
|
||||
if (force || !groups.contains(it.key())) {
|
||||
registerProtocolHandler(it.key(), it.value());
|
||||
@@ -188,10 +188,10 @@ void registerProtocolHandlers(const QHash<QString, QString> &protocols, bool for
|
||||
void unregisterProtocolHandlers(const QHash<QString, QString> &protocols)
|
||||
{
|
||||
const QString regPath = QStringLiteral("HKEY_CURRENT_USER\\Software\\Classes");
|
||||
QScopedPointer<QSettings> reg(new QSettings(regPath, QSettings::NativeFormat));
|
||||
QSettings registry(regPath, QSettings::NativeFormat);
|
||||
|
||||
for (auto it = protocols.cbegin(); it != protocols.cend(); ++it) {
|
||||
reg->remove(it.key());
|
||||
registry.remove(it.key());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -211,10 +211,10 @@ int main(int argc, char *argv[])
|
||||
// Handle --version before creating QApplication to avoid
|
||||
// initializing the platform/graphics stack just to print a version string.
|
||||
{
|
||||
QCoreApplication coreApp(argc, argv);
|
||||
const QCoreApplication coreApp(argc, argv);
|
||||
QCommandLineParser parser;
|
||||
parser.addVersionOption();
|
||||
parser.parse(coreApp.arguments());
|
||||
parser.parse(QCoreApplication::arguments());
|
||||
if (parser.isSet(QStringLiteral("version"))) {
|
||||
parser.showVersion();
|
||||
}
|
||||
@@ -230,16 +230,16 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
#endif
|
||||
|
||||
QScopedPointer<QApplication> qapp(new QApplication(argc, argv));
|
||||
QApplication qapp(argc, argv);
|
||||
|
||||
#if defined(Q_OS_WINDOWS) && QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
DarkModeEraseFilter darkModeEraseFilter;
|
||||
if (qapp->styleHints()->colorScheme() == Qt::ColorScheme::Dark) {
|
||||
qapp->installNativeEventFilter(&darkModeEraseFilter);
|
||||
if (QApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark) {
|
||||
qapp.installNativeEventFilter(&darkModeEraseFilter);
|
||||
}
|
||||
#endif
|
||||
|
||||
const CommandLineParameters clParams = parseCommandLine(qapp->arguments());
|
||||
const CommandLineParameters clParams = parseCommandLine(QApplication::arguments());
|
||||
|
||||
#ifdef Q_OS_WINDOWS
|
||||
const static QHash<QString, QString> protocols = {{QStringLiteral("dash"),
|
||||
@@ -268,20 +268,20 @@ int main(int argc, char *argv[])
|
||||
#endif // Q_OS_WINDOWS
|
||||
|
||||
using Zeal::Core::ApplicationSingleton;
|
||||
QScopedPointer<ApplicationSingleton> appSingleton(new ApplicationSingleton());
|
||||
if (appSingleton->state() == ApplicationSingleton::State::Failed) {
|
||||
ApplicationSingleton appSingleton;
|
||||
if (appSingleton.state() == ApplicationSingleton::State::Failed) {
|
||||
QTextStream(stderr) << "Failed to initialize application singleton." << '\n';
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (appSingleton->state() == ApplicationSingleton::State::Secondary) {
|
||||
if (appSingleton.state() == ApplicationSingleton::State::Secondary) {
|
||||
#ifdef Q_OS_WINDOWS
|
||||
::AllowSetForegroundWindow(appSingleton->primaryPid());
|
||||
::AllowSetForegroundWindow(appSingleton.primaryPid());
|
||||
#endif
|
||||
QByteArray ba;
|
||||
QDataStream out(&ba, QIODevice::WriteOnly);
|
||||
out << clParams.query << clParams.preventActivation;
|
||||
if (!appSingleton->sendMessage(ba)) {
|
||||
if (!appSingleton.sendMessage(ba)) {
|
||||
QTextStream(stderr) << "Failed to send query to the primary instance." << '\n';
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
@@ -289,37 +289,34 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
// Set application-wide window icon. All message boxes and other windows will use it by default.
|
||||
qapp->setDesktopFileName(QStringLiteral("org.zealdocs.zeal"));
|
||||
qapp->setWindowIcon(QIcon::fromTheme(QStringLiteral("zeal"), QIcon(QStringLiteral(":/zeal.svg"))));
|
||||
QApplication::setDesktopFileName(QStringLiteral("org.zealdocs.zeal"));
|
||||
QApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("zeal"), QIcon(QStringLiteral(":/zeal.svg"))));
|
||||
|
||||
QDir::setSearchPaths(QStringLiteral("typeIcon"), {QStringLiteral(":/icons/type")});
|
||||
|
||||
using Zeal::Core::Application;
|
||||
QScopedPointer<Application> app(new Application());
|
||||
Application app;
|
||||
|
||||
using Zeal::WidgetUi::WindowManager;
|
||||
QScopedPointer<WindowManager> wm(new WindowManager(app.data()));
|
||||
WindowManager wm(&app);
|
||||
|
||||
QObject::connect(appSingleton.data(),
|
||||
&ApplicationSingleton::messageReceived,
|
||||
wm.data(),
|
||||
[&wm](const QByteArray &data) {
|
||||
QObject::connect(&appSingleton, &ApplicationSingleton::messageReceived, &wm, [&wm](const QByteArray &data) {
|
||||
Zeal::Registry::SearchQuery query;
|
||||
bool preventActivation = false;
|
||||
|
||||
QDataStream in(data);
|
||||
in >> query >> preventActivation;
|
||||
|
||||
wm->executeQuery(query, preventActivation);
|
||||
wm.executeQuery(query, preventActivation);
|
||||
});
|
||||
|
||||
wm->openWindow(clParams.forceMinimized);
|
||||
wm.openWindow(clParams.forceMinimized);
|
||||
|
||||
if (!clParams.query.isEmpty()) {
|
||||
QTimer::singleShot(0, wm.data(), [&wm, clParams] {
|
||||
wm->executeQuery(clParams.query, clParams.preventActivation);
|
||||
QTimer::singleShot(0, &wm, [&wm, clParams] {
|
||||
wm.executeQuery(clParams.query, clParams.preventActivation);
|
||||
});
|
||||
}
|
||||
|
||||
return qapp->exec();
|
||||
return QApplication::exec();
|
||||
}
|
||||
|
||||
@@ -187,7 +187,7 @@ void Application::checkForUpdates(bool quiet)
|
||||
{
|
||||
QNetworkReply *reply = download(QUrl(ReleasesApiUrl));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, quiet]() {
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(qobject_cast<QNetworkReply *>(sender()));
|
||||
const QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(qobject_cast<QNetworkReply *>(sender()));
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
if (!quiet) {
|
||||
|
||||
@@ -136,7 +136,7 @@ bool ApplicationSingleton::sendMessage(QByteArray &data, int timeout)
|
||||
return false;
|
||||
}
|
||||
|
||||
QScopedPointer<QLocalSocket, QScopedPointerDeleteLater> socket(new QLocalSocket);
|
||||
const QScopedPointer<QLocalSocket, QScopedPointerDeleteLater> socket(new QLocalSocket);
|
||||
socket->connectToServer(m_id);
|
||||
if (!socket->waitForConnected(timeout)) {
|
||||
qCWarning(log) << "Cannot connect to the local service:" << socket->errorString();
|
||||
|
||||
@@ -76,8 +76,8 @@ void Extractor::extract(const QString &sourceFile, const QString &destination, c
|
||||
// See: https://github.com/zealdocs/zeal/issues/1393
|
||||
QDir().mkpath(QFileInfo(filePath).absolutePath());
|
||||
|
||||
QScopedPointer<QFile> file(new QFile(filePath));
|
||||
if (!file->open(QIODevice::WriteOnly)) {
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::WriteOnly)) {
|
||||
qCWarning(log, "Cannot open file for writing at '%s'.", qPrintable(pathname));
|
||||
continue;
|
||||
}
|
||||
@@ -100,7 +100,7 @@ void Extractor::extract(const QString &sourceFile, const QString &destination, c
|
||||
return;
|
||||
}
|
||||
|
||||
file->write(static_cast<const char *>(buffer), size);
|
||||
file.write(static_cast<const char *>(buffer), size);
|
||||
}
|
||||
|
||||
emitProgress(info);
|
||||
|
||||
@@ -129,9 +129,9 @@ Settings::ColorScheme Settings::colorScheme()
|
||||
|
||||
void Settings::load()
|
||||
{
|
||||
QScopedPointer<QSettings> settings(qsettings());
|
||||
auto settings = qsettings();
|
||||
qCDebug(log, "Using settings file: %s", qPrintable(settings->fileName()));
|
||||
migrate(settings.data());
|
||||
migrate(settings.get());
|
||||
|
||||
// TODO: Put everything in groups
|
||||
startMinimized = settings->value(QStringLiteral("start_minimized"), false).toBool();
|
||||
@@ -288,7 +288,7 @@ void Settings::load()
|
||||
|
||||
void Settings::save()
|
||||
{
|
||||
QScopedPointer<QSettings> settings(qsettings());
|
||||
auto settings = qsettings();
|
||||
|
||||
// TODO: Put everything in groups
|
||||
settings->setValue(QStringLiteral("start_minimized"), startMinimized);
|
||||
@@ -412,14 +412,13 @@ void Settings::migrate(QSettings *settings) const
|
||||
* QSettings is initialized according to build options, e.g. standard vs portable.
|
||||
* Caller is responsible for deleting the returned object.
|
||||
*/
|
||||
QSettings *Settings::qsettings(QObject *parent)
|
||||
std::unique_ptr<QSettings> Settings::qsettings()
|
||||
{
|
||||
#ifndef PORTABLE_BUILD
|
||||
return new QSettings(parent);
|
||||
return std::make_unique<QSettings>();
|
||||
#else
|
||||
return new QSettings(QCoreApplication::applicationDirPath() + QLatin1String("/zeal.ini"),
|
||||
QSettings::IniFormat,
|
||||
parent);
|
||||
return std::make_unique<QSettings>(QCoreApplication::applicationDirPath() + QLatin1String("/zeal.ini"),
|
||||
QSettings::IniFormat);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <QKeySequence>
|
||||
#include <QObject>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class QSettings;
|
||||
|
||||
namespace Zeal::Core {
|
||||
@@ -134,7 +136,7 @@ signals:
|
||||
private:
|
||||
void migrate(QSettings *settings) const;
|
||||
|
||||
static QSettings *qsettings(QObject *parent = nullptr);
|
||||
static std::unique_ptr<QSettings> qsettings();
|
||||
};
|
||||
|
||||
} // namespace Zeal::Core
|
||||
|
||||
@@ -465,13 +465,13 @@ void Docset::loadMetadata()
|
||||
return;
|
||||
}
|
||||
|
||||
QScopedPointer<QFile> file(new QFile(dir.filePath(QStringLiteral("meta.json"))));
|
||||
if (!file->open(QIODevice::ReadOnly)) {
|
||||
QFile file(dir.filePath(QStringLiteral("meta.json")));
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonParseError jsonError;
|
||||
const QJsonObject jsonObject = QJsonDocument::fromJson(file->readAll(), &jsonError).object();
|
||||
const QJsonObject jsonObject = QJsonDocument::fromJson(file.readAll(), &jsonError).object();
|
||||
|
||||
if (jsonError.error != QJsonParseError::NoError) {
|
||||
return;
|
||||
|
||||
@@ -58,8 +58,8 @@ DocsetMetadata::DocsetMetadata(const QJsonObject &jsonObject)
|
||||
*/
|
||||
void DocsetMetadata::save(const QString &path, const QString &version)
|
||||
{
|
||||
QScopedPointer<QFile> file(new QFile(path + QLatin1String("/meta.json")));
|
||||
if (!file->open(QIODevice::WriteOnly)) {
|
||||
QFile file(path + QLatin1String("/meta.json"));
|
||||
if (!file.open(QIODevice::WriteOnly)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -93,28 +93,28 @@ void DocsetMetadata::save(const QString &path, const QString &version)
|
||||
jsonObject[QStringLiteral("extra")] = m_extra;
|
||||
}
|
||||
|
||||
file->write(QJsonDocument(jsonObject).toJson());
|
||||
file->close();
|
||||
file.write(QJsonDocument(jsonObject).toJson());
|
||||
file.close();
|
||||
|
||||
if (m_rawIcon.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
file->setFileName(path + QLatin1String("/icon.png"));
|
||||
if (file->open(QIODevice::WriteOnly)) {
|
||||
file->write(m_rawIcon);
|
||||
file.setFileName(path + QLatin1String("/icon.png"));
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
file.write(m_rawIcon);
|
||||
}
|
||||
file->close();
|
||||
file.close();
|
||||
|
||||
if (m_rawIcon2x.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
file->setFileName(path + QLatin1String("/icon@2x.png"));
|
||||
if (file->open(QIODevice::WriteOnly)) {
|
||||
file->write(m_rawIcon2x);
|
||||
file.setFileName(path + QLatin1String("/icon@2x.png"));
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
file.write(m_rawIcon2x);
|
||||
}
|
||||
file->close();
|
||||
file.close();
|
||||
}
|
||||
|
||||
QString DocsetMetadata::name() const
|
||||
|
||||
@@ -101,21 +101,21 @@ void DocsetListItemDelegate::paintProgressBar(QPainter *painter,
|
||||
styleOption.rect.setRight(styleOption.rect.right() - ProgressBarWidth);
|
||||
|
||||
// Size progress bar
|
||||
QScopedPointer<QProgressBar> renderer(new QProgressBar());
|
||||
renderer->resize(ProgressBarWidth, styleOption.rect.height());
|
||||
renderer->setRange(0, 100);
|
||||
renderer->setValue(value);
|
||||
QProgressBar renderer;
|
||||
renderer.resize(ProgressBarWidth, styleOption.rect.height());
|
||||
renderer.setRange(0, 100);
|
||||
renderer.setValue(value);
|
||||
|
||||
const QString format = index.model()->data(index, FormatRole).toString();
|
||||
if (!format.isEmpty()) {
|
||||
renderer->setFormat(format);
|
||||
renderer.setFormat(format);
|
||||
}
|
||||
|
||||
painter->save();
|
||||
|
||||
// Paint progress bar
|
||||
painter->translate(styleOption.rect.topRight());
|
||||
renderer->render(painter);
|
||||
renderer.render(painter);
|
||||
|
||||
painter->restore();
|
||||
|
||||
|
||||
@@ -240,7 +240,7 @@ void DocsetsDialog::downloadSelectedDocsets()
|
||||
*/
|
||||
void DocsetsDialog::downloadCompleted()
|
||||
{
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(qobject_cast<QNetworkReply *>(sender()));
|
||||
const QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(qobject_cast<QNetworkReply *>(sender()));
|
||||
|
||||
m_replies.removeOne(reply.data());
|
||||
|
||||
@@ -456,14 +456,14 @@ void DocsetsDialog::loadDocsetList()
|
||||
return;
|
||||
}
|
||||
|
||||
QScopedPointer<QFile> file(new QFile(fi.filePath()));
|
||||
if (!file->open(QIODevice::ReadOnly)) {
|
||||
QFile file(fi.filePath());
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
downloadDocsetList();
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonParseError jsonError;
|
||||
const QJsonDocument jsonDoc = QJsonDocument::fromJson(file->readAll(), &jsonError);
|
||||
const QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll(), &jsonError);
|
||||
|
||||
if (jsonError.error != QJsonParseError::NoError) {
|
||||
downloadDocsetList();
|
||||
@@ -733,11 +733,11 @@ void DocsetsDialog::processDocsetListReply(QNetworkReply *reply)
|
||||
return;
|
||||
}
|
||||
|
||||
QScopedPointer<QFile> file(new QFile(cacheLocation(DocsetListCacheFileName)));
|
||||
if (file->open(QIODevice::WriteOnly)) {
|
||||
file->write(replyData);
|
||||
file->close(); // Flush to ensure timestamp update on all systems.
|
||||
updateDocsetListDownloadTimeLabel(QFileInfo(file->fileName()).lastModified());
|
||||
QFile file(cacheLocation(DocsetListCacheFileName));
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
file.write(replyData);
|
||||
file.close(); // Flush to ensure timestamp update on all systems.
|
||||
updateDocsetListDownloadTimeLabel(QFileInfo(file.fileName()).lastModified());
|
||||
}
|
||||
|
||||
processDocsetList(jsonDoc.array());
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
#include <QKeyEvent>
|
||||
#include <QMenuBar>
|
||||
#include <QMouseEvent>
|
||||
#include <QScopedPointer>
|
||||
#include <QShortcut>
|
||||
#include <QSplitter>
|
||||
#include <QStackedWidget>
|
||||
@@ -320,8 +319,8 @@ void MainWindow::setupMainMenu()
|
||||
m_globalShortcut->setEnabled(false);
|
||||
}
|
||||
|
||||
QScopedPointer<SettingsDialog> dialog(new SettingsDialog(this));
|
||||
dialog->exec();
|
||||
SettingsDialog dialog(this);
|
||||
dialog.exec();
|
||||
|
||||
if (m_globalShortcut) {
|
||||
m_globalShortcut->setEnabled(true);
|
||||
@@ -420,8 +419,8 @@ void MainWindow::setupMainMenu()
|
||||
|
||||
// -> Docsets Action.
|
||||
m_showDocsetManagerAction = menu->addAction(tr("&Docsets…"), this, [this]() {
|
||||
QScopedPointer<DocsetsDialog> dialog(new DocsetsDialog(m_application, this));
|
||||
dialog->exec();
|
||||
DocsetsDialog dialog(m_application, this);
|
||||
dialog.exec();
|
||||
});
|
||||
|
||||
// Help Menu.
|
||||
@@ -441,8 +440,8 @@ void MainWindow::setupMainMenu()
|
||||
|
||||
// -> About Action.
|
||||
action = menu->addAction(QIcon::fromTheme(QStringLiteral("help-about")), tr("&About Zeal"), this, [this]() {
|
||||
QScopedPointer<AboutDialog> dialog(new AboutDialog(this));
|
||||
dialog->exec();
|
||||
AboutDialog dialog(this);
|
||||
dialog.exec();
|
||||
});
|
||||
addAction(action);
|
||||
action->setMenuRole(QAction::AboutRole);
|
||||
|
||||
@@ -15,14 +15,14 @@ Q_LOGGING_CATEGORY(log, "zeal.util.plist")
|
||||
|
||||
bool Plist::read(const QString &fileName)
|
||||
{
|
||||
QScopedPointer<QFile> file(new QFile(fileName));
|
||||
if (!file->open(QIODevice::ReadOnly)) {
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qCWarning(log, "Cannot open plist file '%s'.", qPrintable(fileName));
|
||||
m_hasError = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
QXmlStreamReader xml(file.data());
|
||||
QXmlStreamReader xml(&file);
|
||||
|
||||
while (!xml.atEnd()) {
|
||||
const QXmlStreamReader::TokenType token = xml.readNext();
|
||||
|
||||
Reference in New Issue
Block a user