use more smart pointers

This commit is contained in:
Muki 2022-04-25 16:04:06 +02:00
parent cc49123236
commit 56e40e097d
No known key found for this signature in database
GPG key ID: BC8D1811CB9F9932
5 changed files with 87 additions and 214 deletions

View file

@ -436,10 +436,10 @@ void Fetcher::taskEnd()
void Fetcher::copyImage(const QString &path, const QString &contentType) void Fetcher::copyImage(const QString &path, const QString &contentType)
{ {
QString dpath = QDir(Settings::instance()->getImagesDir()) auto dpath = QDir(Settings::instance()->getImagesDir())
.absoluteFilePath("kaktus_" + QFileInfo(path).fileName()); .absoluteFilePath("kaktus_" + QFileInfo(path).fileName());
Utils::addExtension(contentType, dpath); Utils::addExtension(contentType, &dpath);
if (QFile::exists(dpath)) { if (QFile::exists(dpath)) {
emit error(801); // image already exists emit error(801); // image already exists

View file

@ -34,13 +34,13 @@ static void registerTypes() {
QStringLiteral("Settings is a singleton")); QStringLiteral("Settings is a singleton"));
} }
static void installTranslator() { static void installTranslator(const QString &locale) {
auto *translator = new QTranslator{qApp}; auto *translator = new QTranslator{qApp};
auto transDir = auto transDir =
SailfishApp::pathTo(QStringLiteral("translations")).toLocalFile(); SailfishApp::pathTo(QStringLiteral("translations")).toLocalFile();
if (!translator->load(QLocale{}, QStringLiteral("kaktus"), if (!translator->load(locale.isEmpty() ? QLocale{} : QLocale{locale},
QStringLiteral("_"), transDir, QStringLiteral("kaktus"), QStringLiteral("_"),
QStringLiteral(".qm"))) { transDir, QStringLiteral(".qm"))) {
qDebug() << "cannot load translation:" << QLocale::system().name() qDebug() << "cannot load translation:" << QLocale::system().name()
<< transDir; << transDir;
if (!translator->load(QStringLiteral("kaktus_en"), transDir)) { if (!translator->load(QStringLiteral("kaktus_en"), transDir)) {
@ -82,16 +82,16 @@ Q_DECL_EXPORT int main(int argc, char **argv) {
context->setContextProperty(QStringLiteral("LICENSE"), Kaktus::LICENSE); context->setContextProperty(QStringLiteral("LICENSE"), Kaktus::LICENSE);
context->setContextProperty(QStringLiteral("LICENSE_URL"), context->setContextProperty(QStringLiteral("LICENSE_URL"),
Kaktus::LICENSE_URL); Kaktus::LICENSE_URL);
installTranslator();
auto *settings = Settings::instance(); auto *settings = Settings::instance();
installTranslator(settings->getLocale());
view->engine()->addImageProvider(QStringLiteral("icons"), view->engine()->addImageProvider(QStringLiteral("icons"),
new IconProvider{}); new IconProvider{});
view->engine()->addImageProvider(QStringLiteral("nvicons"), view->engine()->addImageProvider(QStringLiteral("nvicons"),
new NvIconProvider{}); new NvIconProvider{});
settings->context = context; settings->setContext(context);
NetworkAccessManagerFactory namfactory{settings->getDmUserAgent()}; NetworkAccessManagerFactory namfactory{settings->getDmUserAgent()};
view->engine()->setNetworkAccessManagerFactory(&namfactory); view->engine()->setNetworkAccessManagerFactory(&namfactory);

View file

@ -10,12 +10,12 @@
#include <QList> #include <QList>
#include <QObject> #include <QObject>
#include <QQmlContext>
#include <QSettings> #include <QSettings>
#include <QString> #include <QString>
#include <QUrl> #include <QUrl>
#include <QVariant> #include <QVariant>
class QQmlContext;
class DatabaseManager; class DatabaseManager;
class DownloadManager; class DownloadManager;
class CacheServer; class CacheServer;
@ -111,7 +111,11 @@ class Settings : public QSettings {
static Settings *instance(); static Settings *instance();
Fetcher *fetcher = nullptr; Fetcher *fetcher = nullptr;
QQmlContext *context = nullptr;
inline void setContext(QQmlContext *context) { m_context = context; }
inline void setContextProperty(const QString &name, QObject *value) {
if (m_context) m_context->setContextProperty(name, value);
}
void setOfflineMode(bool value); void setOfflineMode(bool value);
bool getOfflineMode() const; bool getOfflineMode() const;
@ -350,6 +354,7 @@ class Settings : public QSettings {
static constexpr const float maxZoom = 2.0; static constexpr const float maxZoom = 2.0;
static constexpr const float minZoom = 0.5; static constexpr const float minZoom = 0.5;
static Settings *m_instance; static Settings *m_instance;
QQmlContext *m_context = nullptr;
Settings(); Settings();
static QString settingsFilepath(); static QString settingsFilepath();
}; };

View file

@ -1,21 +1,9 @@
/* /* Copyright (C) 2014-2022 Michal Kosciesza <michal@mkiol.net>
Copyright (C) 2014-2022 Michal Kosciesza <michal@mkiol.net> *
* This Source Code Form is subject to the terms of the Mozilla Public
This file is part of Kaktus. * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
Kaktus is free software: you can redistribute it and/or modify */
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Kaktus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kaktus. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils.h" #include "utils.h"
@ -42,27 +30,18 @@
#include "oldreaderfetcher.h" #include "oldreaderfetcher.h"
#include "ttrssfetcher.h" #include "ttrssfetcher.h"
Utils::Utils(QObject *parent) : QObject(parent) { Utils::Utils(QObject *parent) : QObject{parent} {}
dashboardModel = nullptr;
entryModel = nullptr;
tabModel = nullptr;
feedModel = nullptr;
}
QString Utils::readAsset(const QString &path) { QString Utils::readAsset(const QString &path) {
QFile file(SailfishApp::pathTo(path).toLocalFile()); QFile file{SailfishApp::pathTo(path).toLocalFile()};
if (!file.open(QIODevice::ReadOnly)) { if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Could not open" << path qWarning() << "Could not open" << path
<< "for reading: " << file.errorString(); << "for reading: " << file.errorString();
file.close(); return {};
return "";
} }
QString data = QString(file.readAll()); return QString::fromUtf8(file.readAll());
file.close();
return data;
} }
void Utils::copyToClipboard(const QString &text) { void Utils::copyToClipboard(const QString &text) {
@ -73,7 +52,7 @@ void Utils::copyToClipboard(const QString &text) {
void Utils::resetQtWebKit() { void Utils::resetQtWebKit() {
QStringList dataDirs = QStringList dataDirs =
QStandardPaths::standardLocations(QStandardPaths::DataLocation); QStandardPaths::standardLocations(QStandardPaths::DataLocation);
if (dataDirs.size() > 0) { if (!dataDirs.isEmpty()) {
QDir dir(QDir(dataDirs.at(0)).filePath(".QtWebKit")); QDir dir(QDir(dataDirs.at(0)).filePath(".QtWebKit"));
qDebug() << dir.path(); qDebug() << dir.path();
if (dir.exists()) dir.removeRecursively(); if (dir.exists()) dir.removeRecursively();
@ -156,153 +135,73 @@ bool Utils::removeDir(const QString &dirName) {
} }
void Utils::setRootModel() { void Utils::setRootModel() {
TabModel *oldTabModel = tabModel; switch (Settings::instance()->getViewMode()) {
FeedModel *oldFeedModel = feedModel;
EntryModel *oldEntryModel = entryModel;
// qDebug() << "utils tid:" << QThread::currentThreadId();
Settings *s = Settings::instance();
auto mode = s->getViewMode();
switch (mode) {
case Settings::ViewMode::TabsFeedsEntries: case Settings::ViewMode::TabsFeedsEntries:
tabModel = new TabModel();
tabModel->init(s->getDashboardInUse());
s->context->setContextProperty("tabModel", tabModel);
if (oldTabModel != nullptr) {
delete oldTabModel;
}
if (feedModel != nullptr) {
delete feedModel;
feedModel = nullptr;
}
if (entryModel != nullptr) {
delete entryModel;
entryModel = nullptr;
}
break;
case Settings::ViewMode::TabsEntries: case Settings::ViewMode::TabsEntries:
tabModel = new TabModel(); setTabModel(Settings::instance()->getDashboardInUse());
tabModel->init(s->getDashboardInUse()); feedModel.reset();
s->context->setContextProperty("tabModel", tabModel); entryModel.reset();
if (oldTabModel != nullptr) {
delete oldTabModel;
}
if (feedModel != nullptr) {
delete feedModel;
feedModel = nullptr;
}
if (entryModel != nullptr) {
delete entryModel;
entryModel = nullptr;
}
break; break;
case Settings::ViewMode::FeedsEntries: case Settings::ViewMode::FeedsEntries:
// View mode: Feeds->Entries setFeedModel(QStringLiteral("root"));
feedModel = new FeedModel(); tabModel.reset();
feedModel->init("root"); entryModel.reset();
s->context->setContextProperty("feedModel", feedModel);
if (tabModel != nullptr) {
delete tabModel;
tabModel = nullptr;
}
if (oldFeedModel != nullptr) {
delete oldFeedModel;
}
if (entryModel != nullptr) {
delete entryModel;
entryModel = nullptr;
}
break; break;
case Settings::ViewMode::AllEntries: case Settings::ViewMode::AllEntries:
case Settings::ViewMode::SavedEntries: case Settings::ViewMode::SavedEntries:
case Settings::ViewMode::SlowEntries: case Settings::ViewMode::SlowEntries:
case Settings::ViewMode::LikedEntries: case Settings::ViewMode::LikedEntries:
case Settings::ViewMode::BroadcastedEntries: case Settings::ViewMode::BroadcastedEntries:
entryModel = new EntryModel(); setEntryModel(QStringLiteral("root"));
entryModel->init("root"); tabModel.reset();
s->context->setContextProperty("entryModel", entryModel); feedModel.reset();
if (tabModel != nullptr) {
delete tabModel;
tabModel = nullptr;
}
if (feedModel != nullptr) {
delete feedModel;
feedModel = nullptr;
}
if (oldEntryModel != nullptr) {
delete oldEntryModel;
}
break; break;
} }
} }
void Utils::setTabModel(const QString &dashboardId) {
auto model = std::make_unique<TabModel>();
std::swap(model, tabModel);
tabModel->init(dashboardId);
Settings::instance()->setContextProperty(QStringLiteral("tabModel"),
tabModel.get());
}
void Utils::setFeedModel(const QString &tabId) { void Utils::setFeedModel(const QString &tabId) {
FeedModel *oldFeedModel = feedModel; auto model = std::make_unique<FeedModel>();
Settings *s = Settings::instance(); std::swap(model, feedModel);
feedModel = new FeedModel();
feedModel->init(tabId); feedModel->init(tabId);
Settings::instance()->setContextProperty(QStringLiteral("feedModel"),
s->context->setContextProperty("feedModel", feedModel); feedModel.get());
if (oldFeedModel != nullptr) {
delete oldFeedModel;
}
} }
void Utils::setEntryModel(const QString &feedId) { void Utils::setEntryModel(const QString &feedId) {
EntryModel *oldEntryModel = entryModel; auto model = std::make_unique<EntryModel>();
Settings *s = Settings::instance(); std::swap(model, entryModel);
entryModel = new EntryModel();
entryModel->initInThread(feedId); entryModel->initInThread(feedId);
Settings::instance()->setContextProperty(QStringLiteral("entryModel"),
s->context->setContextProperty("entryModel", entryModel); entryModel.get());
if (oldEntryModel != nullptr) {
delete oldEntryModel;
}
} }
void Utils::setDashboardModel() { void Utils::setDashboardModel() {
DashboardModel *oldDashboardModel = dashboardModel; auto model = std::make_unique<DashboardModel>();
Settings *s = Settings::instance(); std::swap(model, dashboardModel);
dashboardModel = new DashboardModel();
dashboardModel->init(); dashboardModel->init();
Settings::instance()->setContextProperty(QStringLiteral("dashboardModel"),
s->context->setContextProperty("dashboardModel", dashboardModel); dashboardModel.get());
if (oldDashboardModel != nullptr) delete oldDashboardModel;
} }
void Utils::updateModels() { void Utils::updateModels() {
if (dashboardModel != nullptr) dashboardModel->init(); if (dashboardModel) dashboardModel->init();
if (tabModel) tabModel->init();
if (tabModel != nullptr) tabModel->init(); if (feedModel) feedModel->init();
if (entryModel) entryModel->init();
if (feedModel != nullptr) feedModel->init();
if (entryModel != nullptr) entryModel->init();
}
Utils::~Utils() {
if (entryModel != nullptr) delete entryModel;
if (feedModel != nullptr) delete feedModel;
if (tabModel != nullptr) delete tabModel;
if (dashboardModel != nullptr) delete dashboardModel;
} }
QList<QString> Utils::dashboards() { QList<QString> Utils::dashboards() {
auto db = DatabaseManager::instance();
QList<QString> simpleList; QList<QString> simpleList;
QList<DatabaseManager::Dashboard> list = db->readDashboards(); auto list = DatabaseManager::instance()->readDashboards();
QList<DatabaseManager::Dashboard>::iterator i = list.begin(); auto i = list.begin();
while (i != list.end()) { while (i != list.end()) {
simpleList.append((*i).title); simpleList.append((*i).title);
++i; ++i;
@ -311,18 +210,14 @@ QList<QString> Utils::dashboards() {
} }
QString Utils::defaultDashboardName() { QString Utils::defaultDashboardName() {
auto s = Settings::instance(); return DatabaseManager::instance()
auto db = DatabaseManager::instance(); ->readDashboard(Settings::instance()->getDashboardInUse())
.title;
DatabaseManager::Dashboard d = db->readDashboard(s->getDashboardInUse());
return d.title;
} }
int Utils::countUnread() { int Utils::countUnread() {
auto s = Settings::instance(); return DatabaseManager::instance()->countEntriesUnreadByDashboard(
auto db = DatabaseManager::instance(); Settings::instance()->getDashboardInUse());
return db->countEntriesUnreadByDashboard(s->getDashboardInUse());
} }
QString Utils::getHumanFriendlySizeString(int size) { QString Utils::getHumanFriendlySizeString(int size) {
@ -345,11 +240,6 @@ QString Utils::getHumanFriendlyTimeString(int date) {
QDateTime qdate = QDateTime::fromTime_t(date); QDateTime qdate = QDateTime::fromTime_t(date);
int secs = qdate.secsTo(QDateTime::currentDateTimeUtc()); int secs = qdate.secsTo(QDateTime::currentDateTimeUtc());
// qDebug() << ">>>>>>>>date" << date << "QDateTime::fromTime_t(date)" <<
// qdate; qDebug() << "QDateTime::currentDateTimeUtc()" <<
// QDateTime::currentDateTimeUtc(); qDebug() <<
// "qdate.secsTo(QDateTime::currentDateTimeUtc())" << secs;
if (secs <= -18000) { if (secs <= -18000) {
return tr("unknown date"); return tr("unknown date");
} }
@ -385,7 +275,6 @@ QString Utils::hash(const QString &url) {
int Utils::monthsTo(const QDate &from, const QDate &to) { int Utils::monthsTo(const QDate &from, const QDate &to) {
int result = 12 * (to.year() - from.year()); int result = 12 * (to.year() - from.year());
result += (to.month() - from.month()); result += (to.month() - from.month());
return result; return result;
} }
@ -397,9 +286,7 @@ bool Utils::isSameWeek(const QDate &date1, const QDate &date2) {
int y1, y2; int y1, y2;
int w1 = date1.weekNumber(&y1); int w1 = date1.weekNumber(&y1);
int w2 = date2.weekNumber(&y2); int w2 = date2.weekNumber(&y2);
// qDebug() << date1 << date2 << y1 << y2 << w1 << w2; return w1 == w2 && y1 == y2 && w1 != 0 && w2 != 0;
if (w1 == w2 && y1 == y2 && w1 != 0 && w2 != 0) return true;
return false;
} }
void Utils::resetFetcher(int type) { void Utils::resetFetcher(int type) {
@ -426,16 +313,11 @@ void Utils::resetFetcher(int type) {
s->fetcher = new TTRssFetcher(); s->fetcher = new TTRssFetcher();
} }
if (s->fetcher != nullptr) if (s->fetcher != nullptr) s->setContextProperty("fetcher", s->fetcher);
s->context->setContextProperty("fetcher", s->fetcher);
} }
QString Utils::nameFromPath(const QString &path) { void Utils::addExtension(const QString &contentType, QString *path) {
return QFileInfo(path).fileName(); auto orig_ext = QFileInfo{*path}.suffix();
}
void Utils::addExtension(const QString &contentType, QString &path) {
auto orig_ext = QFileInfo(path).suffix();
QString new_ext; QString new_ext;
if (contentType == "image/jpeg") { if (contentType == "image/jpeg") {
@ -451,6 +333,6 @@ void Utils::addExtension(const QString &contentType, QString &path) {
} }
if (new_ext != orig_ext) { if (new_ext != orig_ext) {
path.append("." + new_ext); path->append("." + new_ext);
} }
} }

View file

@ -1,21 +1,9 @@
/* /* Copyright (C) 2014-2022 Michal Kosciesza <michal@mkiol.net>
Copyright (C) 2014-2022 Michal Kosciesza <michal@mkiol.net> *
* This Source Code Form is subject to the terms of the Mozilla Public
This file is part of Kaktus. * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
Kaktus is free software: you can redistribute it and/or modify */
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Kaktus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kaktus. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UTILS_H #ifndef UTILS_H
#define UTILS_H #define UTILS_H
@ -24,6 +12,7 @@
#include <QList> #include <QList>
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <memory>
#include "dashboardmodel.h" #include "dashboardmodel.h"
#include "entrymodel.h" #include "entrymodel.h"
@ -36,12 +25,11 @@ class Utils : public QObject {
public: public:
explicit Utils(QObject *parent = nullptr); explicit Utils(QObject *parent = nullptr);
~Utils();
Q_INVOKABLE void setEntryModel(const QString &feedId); Q_INVOKABLE void setEntryModel(const QString &feedId);
Q_INVOKABLE void setFeedModel(const QString &tabId); Q_INVOKABLE void setFeedModel(const QString &tabId);
Q_INVOKABLE void setRootModel(); Q_INVOKABLE void setRootModel();
Q_INVOKABLE void setDashboardModel(); Q_INVOKABLE void setDashboardModel();
Q_INVOKABLE void setTabModel(const QString &dashboardId);
Q_INVOKABLE QList<QString> dashboards(); Q_INVOKABLE QList<QString> dashboards();
Q_INVOKABLE void copyToClipboard(const QString &text); Q_INVOKABLE void copyToClipboard(const QString &text);
Q_INVOKABLE QString defaultDashboardName(); Q_INVOKABLE QString defaultDashboardName();
@ -51,25 +39,23 @@ class Utils : public QObject {
Q_INVOKABLE void resetQtWebKit(); Q_INVOKABLE void resetQtWebKit();
Q_INVOKABLE void resetFetcher(int type); Q_INVOKABLE void resetFetcher(int type);
Q_INVOKABLE QString formatHtml(const QString &data, bool offline, Q_INVOKABLE QString formatHtml(const QString &data, bool offline,
const QString &style = QString()); const QString &style = {});
Q_INVOKABLE QString readAsset(const QString &path); Q_INVOKABLE QString readAsset(const QString &path);
Q_INVOKABLE QString nameFromPath(const QString &path);
static QString hash(const QString &url); static QString hash(const QString &url);
static int monthsTo(const QDate &from, const QDate &to); static int monthsTo(const QDate &from, const QDate &to);
static int yearsTo(const QDate &from, const QDate &to); static int yearsTo(const QDate &from, const QDate &to);
static bool isSameWeek(const QDate &date1, const QDate &date2); static bool isSameWeek(const QDate &date1, const QDate &date2);
static void addExtension(const QString &contentType, QString &path); static void addExtension(const QString &contentType, QString *path);
Q_INVOKABLE static void log(const QString &data); Q_INVOKABLE static void log(const QString &data);
public slots: public slots:
void updateModels(); void updateModels();
private: private:
EntryModel *entryModel; std::unique_ptr<EntryModel> entryModel;
FeedModel *feedModel; std::unique_ptr<FeedModel> feedModel;
TabModel *tabModel; std::unique_ptr<TabModel> tabModel;
DashboardModel *dashboardModel; std::unique_ptr<DashboardModel> dashboardModel;
bool removeDir(const QString &dirName); bool removeDir(const QString &dirName);
}; };