File indexing completed on 2024-07-21 04:35:17

0001 /*
0002     SPDX-FileCopyrightText: 2018 Ralf Habacker ralf.habacker @freenet.de
0003 
0004     This file is part of libalkimia.
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "alkonlinequotesprofilemanager.h"
0010 #include "alkonlinequotesprofile.h"
0011 #include "alkwebpage.h"
0012 
0013 #include <QPointer>
0014 
0015 #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
0016 #include <QLocale>
0017 #define initLocale() QLocale()
0018 #else
0019 #include <KGlobal>
0020 #define initLocale() KGlobal::locale()
0021 #endif
0022 
0023 class AlkOnlineQuotesProfileManager::Private
0024 {
0025 public:
0026     AlkOnlineQuotesProfileList m_profiles;
0027     QPointer<AlkWebPage> m_page;
0028     bool m_withPage;
0029     Private()
0030       : m_withPage(false)
0031     {
0032     }
0033 
0034     ~Private()
0035     {
0036         m_page.data()->deleteLater();
0037     }
0038 };
0039 
0040 AlkOnlineQuotesProfileManager::AlkOnlineQuotesProfileManager()
0041   : d(new Private)
0042 {
0043 }
0044 
0045 AlkOnlineQuotesProfileManager::~AlkOnlineQuotesProfileManager()
0046 {
0047     delete d;
0048 }
0049 
0050 bool AlkOnlineQuotesProfileManager::webPageEnabled()
0051 {
0052     return d->m_withPage;
0053 }
0054 
0055 void AlkOnlineQuotesProfileManager::setWebPageEnabled(bool enable)
0056 {
0057     d->m_withPage = enable;
0058 }
0059 
0060 void AlkOnlineQuotesProfileManager::addProfile(AlkOnlineQuotesProfile *profile)
0061 {
0062     if (!d->m_profiles.contains(profile)) {
0063         d->m_profiles.append(profile);
0064         profile->setManager(this);
0065         connect(profile, SIGNAL(updateAvailable(const QString &, const QString &)), this, SIGNAL(updateAvailable(const QString &, const QString &)));
0066     }
0067 }
0068 
0069 AlkOnlineQuotesProfileList AlkOnlineQuotesProfileManager::profiles()
0070 {
0071     return d->m_profiles;
0072 }
0073 
0074 AlkOnlineQuotesProfile *AlkOnlineQuotesProfileManager::profile(const QString &name)
0075 {
0076     for (AlkOnlineQuotesProfile *profile : profiles()) {
0077         if (name == profile->name()) {
0078             return profile;
0079         }
0080     }
0081     return nullptr;
0082 }
0083 
0084 QStringList AlkOnlineQuotesProfileManager::profileNames()
0085 {
0086     QStringList profiles;
0087     for (AlkOnlineQuotesProfile *profile : d->m_profiles) {
0088         profiles.append(profile->name());
0089     }
0090     return profiles;
0091 }
0092 
0093 AlkWebPage *AlkOnlineQuotesProfileManager::webPage()
0094 {
0095     if (!d->m_page) {
0096     // make sure that translations are installed on windows
0097         initLocale();
0098         d->m_page = new AlkWebPage;
0099     }
0100     return d->m_page;
0101 }
0102 
0103 AlkOnlineQuotesProfileManager &AlkOnlineQuotesProfileManager::instance()
0104 {
0105     static AlkOnlineQuotesProfileManager manager;
0106     return manager;
0107 }
0108 
0109 AlkOnlineQuotesProfileManager *AlkOnlineQuotesProfileManager::instancePointer()
0110 {
0111     return &instance();
0112 }