File indexing completed on 2024-05-05 04:47:30

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "InfoProxy.h"
0018 
0019 #include "App.h"
0020 #include "core/support/Debug.h"
0021 #include "PaletteHandler.h"
0022 
0023 #include <QFile>
0024 #include <QStandardPaths>
0025 #include <QUrl>
0026 
0027 #include <KLocalizedString>
0028 
0029 InfoProxy * InfoProxy::m_instance = nullptr;
0030 
0031 InfoProxy * InfoProxy::instance()
0032 {
0033     if ( m_instance == nullptr )
0034         m_instance = new InfoProxy();
0035 
0036     return m_instance;
0037 }
0038 
0039 InfoProxy::InfoProxy()
0040 {
0041     DEBUG_BLOCK;
0042     //for testing
0043 
0044     QList<QVariant> strings;
0045     QList<QVariant> weights;
0046 
0047     strings << "This" << "is" << "just" << "a" << "very" << "small" << "and" << "quite" << "silly" << "default" << "text"
0048             << "as" << "I" << "currently" << "have" <<  "nothing" << "better" << "to" << "show";
0049 
0050     weights << 10 << 4 << 8 << 2 << 6 << 5 << 10 << 9 << 3 << 1 << 3 << 5 << 7 << 9 << 3 << 2 << 10 << 6 << 4;
0051 
0052     m_storedCloud[QStringLiteral("cloud_name")] = QVariant( "test cloud" );
0053     m_storedCloud[QStringLiteral("cloud_strings")] = QVariant( strings );
0054     m_storedCloud[QStringLiteral("cloud_weights")] = QVariant( weights );
0055 
0056     loadHomePage();
0057 }
0058 
0059 InfoProxy::~InfoProxy()
0060 {
0061 }
0062 
0063 
0064 void
0065 InfoProxy::subscribe( InfoObserver * observer )
0066 {
0067     DEBUG_BLOCK;
0068     if( observer )
0069     {
0070         m_observers.insert( observer );
0071         observer->infoChanged( m_storedInfo );
0072     }
0073 }
0074 
0075 void
0076 InfoProxy::subscribeForCloud( InfoObserver * observer )
0077 {
0078     DEBUG_BLOCK;
0079     if( observer )
0080     {
0081         m_cloudObservers.insert( observer );
0082         observer->infoChanged( m_storedCloud );
0083     }
0084 }
0085 
0086 void
0087 InfoProxy::unsubscribe( InfoObserver * observer )
0088 {
0089     m_observers.remove( observer );
0090     m_cloudObservers.remove( observer );
0091 }
0092 
0093 void
0094 InfoProxy::notifyObservers( const QVariantMap &infoMap ) const
0095 {
0096     foreach( InfoObserver *observer, m_observers )
0097         observer->infoChanged( infoMap );
0098 }
0099 
0100 void
0101 InfoProxy::notifyCloudObservers( const QVariantMap &cloudMap ) const
0102 {
0103     foreach( InfoObserver *observer, m_cloudObservers )
0104         observer->infoChanged( cloudMap );
0105 }
0106 
0107 void
0108 InfoProxy::setInfo( const QVariantMap &infoMap )
0109 {
0110     m_storedInfo = infoMap;
0111     notifyObservers( m_storedInfo );
0112 }
0113 
0114 void
0115 InfoProxy::setCloud( const QVariantMap &cloudMap )
0116 {
0117     m_storedCloud = cloudMap;
0118     notifyCloudObservers( m_storedCloud );
0119 }
0120 
0121 QVariantMap
0122 InfoProxy::info()
0123 {
0124     return m_storedInfo;
0125 }
0126 
0127 QVariantMap
0128 InfoProxy::cloud()
0129 {
0130     return m_storedCloud;
0131 }
0132 
0133 void
0134 InfoProxy::loadHomePage()
0135 {
0136     DEBUG_BLOCK
0137 
0138     QUrl dataUrl( QStandardPaths::locate( QStandardPaths::GenericDataLocation, QStringLiteral("amarok/data/") ) );
0139     QString dataPath = dataUrl.path();
0140 
0141     //load html
0142 
0143     QString htmlPath = dataPath + "info_frontpage.html";
0144     QFile file( htmlPath );
0145     if ( !file.open( QIODevice::ReadOnly | QIODevice::Text) )
0146     {
0147         debug() << "error opening file. Error: " << file.error();
0148         return;
0149     }
0150 
0151     QString html = file.readAll();
0152 
0153     QUrl imageUrl( QStandardPaths::locate( QStandardPaths::GenericDataLocation, QStringLiteral("amarok/images/") ) );
0154     QString imagePath = imageUrl.url();
0155 
0156     html.replace( QLatin1String("_PATH_"), imagePath );
0157 
0158     html.replace( QLatin1String("{background_color}"), The::paletteHandler()->highlightColor().lighter( 150 ).name() );
0159     html.replace( QLatin1String("{border_color}"), The::paletteHandler()->highlightColor().lighter( 150 ).name() );
0160     html.replace( QLatin1String("{text_color}"), pApp->palette().brush( QPalette::Text ).color().name() );
0161     QColor highlight( pApp->palette().highlight().color() );
0162     highlight.setHsvF( highlight.hueF(), 0.3, .95, highlight.alphaF() );
0163     html.replace( QLatin1String("{header_background_color}"), highlight.name() );
0164 
0165 
0166     m_storedInfo[QStringLiteral("service_name")] =  i18n( "Home" );
0167     m_storedInfo[QStringLiteral("main_info")] = html;
0168 
0169     notifyObservers( m_storedInfo );
0170 }
0171 
0172 
0173 namespace The {
0174     AMAROK_EXPORT InfoProxy* infoProxy() { return InfoProxy::instance(); }
0175 }
0176