aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-09-29 14:56:39 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-09-29 14:56:39 +0200
commit9abf7a9690163737d3e70b3b52d814135858d0d5 (patch)
tree037b1d3a12115221182cb283c14736229c52a8d8 /lib
parentUpdate repository path in license headers (diff)
downloadsmolbote-9abf7a9690163737d3e70b3b52d814135858d0d5.tar.xz
ProfileManager: move initial profile loading to ProfileManager
Diffstat (limited to 'lib')
-rw-r--r--lib/web/profilemanager.cpp54
-rw-r--r--lib/web/profilemanager.h10
2 files changed, 25 insertions, 39 deletions
diff --git a/lib/web/profilemanager.cpp b/lib/web/profilemanager.cpp
index 97d5f8d..75fd413 100644
--- a/lib/web/profilemanager.cpp
+++ b/lib/web/profilemanager.cpp
@@ -11,23 +11,25 @@
#include <QFileInfo>
#include <QWebEngineSettings>
-ProfileManager *ProfileManager::s_instance = nullptr;
-
-ProfileManager::ProfileManager(const QHash<QString, QString> &profileSection, QObject *parent) : QObject(parent)
- , defaults(profileSection)
+ProfileManager::ProfileManager(const QHash<QString, QString> &profileSection, const QString &defaultId, QObject *parent)
+ : QObject(parent)
+ , defaults(profileSection)
{
-}
+ // load profiles from path
+ const QDir profilesDir(defaults.value("profile.path"));
-void ProfileManager::setInstance(ProfileManager *instance)
-{
- Q_CHECK_PTR(instance);
- s_instance = instance;
-}
+ if(profilesDir.exists()) {
+ const auto entries = profilesDir.entryInfoList({ "*.profile" }, QDir::Files | QDir::Readable, QDir::Time);
-ProfileManager *ProfileManager::instance()
-{
- Q_CHECK_PTR(s_instance);
- return s_instance;
+ for(const QFileInfo &f : entries) {
+ loadProfile(f.absoluteFilePath());
+ }
+ }
+
+ const QString defaultProfile = defaults.value("profile.default", defaultId);
+ if(m_profiles.count(defaultProfile) == 0) {
+ loadProfile(defaultProfile);
+ }
}
WebProfile *ProfileManager::loadProfile(const QString &path)
@@ -100,9 +102,9 @@ void ProfileManager::deleteProfile(const QString &id)
QMenu *ProfileManager::createProfileMenu(std::function<void(WebProfile *)> callback, QWidget *parent) const
{
- QMenu *menu = new QMenu(parent);
- for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) {
- WebProfile *profile = it->second->profile;
+ auto *menu = new QMenu(parent);
+ for(const auto &m_profile : m_profiles) {
+ WebProfile *profile = m_profile.second->profile;
QAction *action = menu->addAction(profile->name());
connect(action, &QAction::triggered, [profile, callback]() {
callback(profile);
@@ -114,17 +116,17 @@ QMenu *ProfileManager::createProfileMenu(std::function<void(WebProfile *)> callb
const QStringList ProfileManager::idList() const
{
QStringList ids;
- for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) {
- ids.append(it->first);
+ for(const auto &m_profile : m_profiles) {
+ ids.append(m_profile.first);
}
return ids;
}
const QString ProfileManager::id(const WebProfile *profile) const
{
- for(auto it = m_profiles.cbegin(); it != m_profiles.cend(); ++it) {
- if(it->second->profile == profile)
- return it->first;
+ for(const auto &m_profile : m_profiles) {
+ if(m_profile.second->profile == profile)
+ return m_profile.first;
}
return QString();
}
@@ -136,11 +138,3 @@ WebProfile *ProfileManager::profile(const QString &id) const
}
return nullptr;
}
-
-const QString ProfileManager::configurationPath(const QString &id) const
-{
- if(m_profiles.count(id) > 0) {
- return m_profiles.at(id)->path;
- }
- return QString();
-}
diff --git a/lib/web/profilemanager.h b/lib/web/profilemanager.h
index 1cb5dd5..9cd2cbd 100644
--- a/lib/web/profilemanager.h
+++ b/lib/web/profilemanager.h
@@ -20,17 +20,12 @@
#include <map>
#include <memory>
-#define profileManager ProfileManager::instance()
-
class WebProfile;
class ProfileManager : public QObject
{
Q_OBJECT
public:
- explicit ProfileManager(const QHash<QString, QString> &profileSection, QObject *parent = nullptr);
-
- static void setInstance(ProfileManager *instance);
- static ProfileManager *instance();
+ explicit ProfileManager(const QHash<QString, QString> &profileSection, const QString &defaultId, QObject *parent = nullptr);
WebProfile *loadProfile(const QString &path);
void deleteProfile(const QString &id);
@@ -40,7 +35,6 @@ public:
const QStringList idList() const;
const QString id(const WebProfile *profile) const;
WebProfile *profile(const QString &id) const;
- const QString configurationPath(const QString &id) const;
private:
struct ProfileData {
@@ -68,8 +62,6 @@ private:
};
std::map<QString, std::unique_ptr<ProfileData>> m_profiles;
-
- static ProfileManager *s_instance;
const QHash<QString, QString> defaults;
};