File indexing completed on 2024-10-27 04:24:10

0001 #include "screenmanager.h"
0002 
0003 #include "settingsstore.h"
0004 #include "mauimanutils.h"
0005 
0006 #include <QDebug>
0007 
0008 #if !defined Q_OS_ANDROID
0009 #include <QDBusInterface>
0010 #endif
0011 
0012 using namespace MauiMan;
0013 
0014 ScreenManager::ScreenManager(QObject * parent ) : QObject(parent)
0015   ,m_settings(new MauiMan::SettingsStore(this))
0016 {
0017     qDebug( " INIT SCREEN MANAGER");
0018 
0019 #if !defined Q_OS_ANDROID
0020     auto server = new MauiManUtils(this);
0021     if(server->serverRunning())
0022     {
0023         this->setConnections();
0024     }
0025 
0026     connect(server, &MauiManUtils::serverRunningChanged, [this](bool state)
0027     {
0028         if(state)
0029         {
0030             this->setConnections();
0031         }
0032     });
0033 #endif
0034 
0035     loadSettings();
0036 }
0037 
0038 double ScreenManager::scaleFactor() const
0039 {
0040     return m_scaleFactor;
0041 }
0042 
0043 void ScreenManager::setScaleFactor(double scaleFactor)
0044 {
0045     if (m_scaleFactor == scaleFactor)
0046         return;
0047 
0048     m_scaleFactor = scaleFactor;
0049     sync(QStringLiteral("setScaleFactor"), m_scaleFactor);
0050     m_settings->save(QStringLiteral("ScaleFactor"), m_scaleFactor);
0051     Q_EMIT scaleFactorChanged(m_scaleFactor);
0052 }
0053 
0054 uint ScreenManager::orientation() const
0055 {
0056     return m_orientation;
0057 }
0058 
0059 void ScreenManager::setOrientation(uint orientation)
0060 {
0061     if (m_orientation == orientation)
0062         return;
0063 
0064     m_orientation = orientation;
0065     sync(QStringLiteral("setOrientation"), m_orientation);
0066     m_settings->save(QStringLiteral("Orientation"), m_orientation);
0067     Q_EMIT orientationChanged(m_orientation);
0068 }
0069 
0070 void ScreenManager::onScaleFactorChanged(double scale)
0071 {
0072     if (m_scaleFactor == scale)
0073         return;
0074 
0075     m_scaleFactor = scale;
0076     Q_EMIT scaleFactorChanged(m_scaleFactor);
0077 }
0078 
0079 void ScreenManager::onOrientationChanged(uint orientation)
0080 {
0081     if (m_orientation == orientation)
0082         return;
0083 
0084     m_orientation = orientation;
0085     Q_EMIT orientationChanged(m_orientation);
0086 }
0087 
0088 void ScreenManager::sync(const QString &key, const QVariant &value)
0089 {
0090 #if !defined Q_OS_ANDROID
0091     if (m_interface && m_interface->isValid())
0092     {
0093         m_interface->call(key, value);
0094     }
0095 #else
0096     Q_UNUSED(key)
0097     Q_UNUSED(value)
0098 #endif
0099 }
0100 
0101 void ScreenManager::setConnections()
0102 {
0103 #if !defined Q_OS_ANDROID
0104     if(m_interface)
0105     {
0106         m_interface->disconnect();
0107         m_interface->deleteLater();
0108         m_interface = nullptr;
0109     }
0110 
0111     m_interface = new QDBusInterface (QStringLiteral("org.mauiman.Manager"),
0112                                       QStringLiteral("/Screen"),
0113                                       QStringLiteral("org.mauiman.Screen"),
0114                                       QDBusConnection::sessionBus(), this);
0115     if (m_interface->isValid())
0116     {
0117         connect(m_interface, SIGNAL(scaleFactorChanged(double)), this, SLOT(onScaleFactorChanged(double)));
0118         connect(m_interface, SIGNAL(orientationChanged(uint)), this, SLOT(onOrientationChanged(uint)));
0119 
0120     }
0121 #endif
0122 }
0123 
0124 void ScreenManager::loadSettings()
0125 {
0126     m_settings->beginModule(QStringLiteral("Screen"));
0127 
0128 #if !defined Q_OS_ANDROID
0129 
0130     if(m_interface && m_interface->isValid())
0131     {
0132         m_scaleFactor = m_interface->property("scaleFactor").toDouble();
0133         m_orientation = m_interface->property("orientation").toUInt();
0134         return;
0135     }
0136 #endif
0137 
0138     m_scaleFactor = m_settings->load(QStringLiteral("ScaleFactor"), m_scaleFactor).toDouble();
0139     m_orientation = m_settings->load(QStringLiteral("Orientation"), m_orientation).toUInt();
0140 }