File indexing completed on 2024-11-24 03:56:28

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "application.hpp"
0008 
0009 #include <QStandardPaths>
0010 #include <QMetaEnum>
0011 #include <QIcon>
0012 
0013 #include "app/log/log.hpp"
0014 
0015 
0016 void app::Application::initialize()
0017 {
0018     on_initialize();
0019     on_initialize_translations();
0020     on_initialize_settings();
0021     app::settings::Settings::instance().load();
0022 }
0023 
0024 QString app::Application::writable_data_path(const QString& name) const
0025 {
0026     QString search = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
0027 
0028     if ( !search.isEmpty() )
0029     {
0030         return QDir::cleanPath(QDir(search).absoluteFilePath(name));
0031     }
0032 
0033     return QString();
0034 }
0035 
0036 QStringList app::Application::data_paths(const QString& name) const
0037 {
0038     QStringList found;
0039 
0040     for ( const QDir& d: data_roots() )
0041     {
0042         if ( d.exists(name) )
0043             found << QDir::cleanPath(d.absoluteFilePath(name));
0044     }
0045     found.removeDuplicates();
0046 
0047     return found;
0048 }
0049 
0050 QStringList app::Application::data_paths_unchecked(const QString& name) const
0051 {
0052     QStringList filter;
0053     for ( const QDir& d: data_roots() )
0054     {
0055         filter << QDir::cleanPath(d.absoluteFilePath(name));
0056     }
0057     filter.removeDuplicates();
0058 
0059     return filter;
0060 }
0061 
0062 QList<QDir> app::Application::data_roots() const
0063 {
0064     QList<QDir> search;
0065     // std paths
0066     for ( const QString& str : QStandardPaths::standardLocations(QStandardPaths::AppDataLocation) )
0067         search.push_back(QDir(str));
0068     // executable dir
0069     QDir binpath(QCoreApplication::applicationDirPath());
0070 #ifdef Q_OS_WIN
0071     // some Windows apps do not use a bin subfolder
0072     search.push_back(binpath.filePath(QString("share/%1/%2").arg(organizationName()).arg(applicationName())));
0073 #endif
0074     binpath.cdUp();
0075     search.push_back(binpath.filePath(QString("share/%1/%2").arg(organizationName()).arg(applicationName())));
0076 #ifdef Q_OS_MAC
0077     // some macOS app bundles use a Resources subfolder
0078     search.push_back(binpath.filePath(QString("Resources/%1/%2").arg(organizationName()).arg(applicationName())));
0079 #endif
0080 
0081     return search;
0082 }
0083 
0084 QString app::Application::data_file(const QString& name) const
0085 {
0086     QStringList found;
0087 
0088     for ( const QDir& d: data_roots() )
0089     {
0090         if ( d.exists(name) )
0091             return QDir::cleanPath(d.absoluteFilePath(name));
0092     }
0093 
0094     return {};
0095 }
0096 
0097 QSettings app::Application::qsettings() const
0098 {
0099     return QSettings(writable_data_path("settings.ini"), QSettings::IniFormat);
0100 }
0101 
0102 bool app::Application::notify(QObject* receiver, QEvent* e)
0103 {
0104     try {
0105         return QApplication::notify(receiver, e);
0106     } catch ( const std::exception& exc ) {
0107         log::Log("Event", QMetaEnum::fromType<QEvent::Type>().valueToKey(e->type())).stream(log::Error) << "Exception:" << exc.what();
0108         return false;
0109     }
0110 }
0111 
0112 void app::Application::on_initialize_translations()
0113 {
0114     app::TranslationService::instance().initialize();
0115 }