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

0001 /****************************************************************************************
0002  * Copyright (c) 2007-2008 Leo Franchi <lfranchi@gmail.com>                             *
0003  * Copyright (c) 2008 William Viana Soares <vianasw@gmail.com>                          *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #define DEBUG_PREFIX "ContextView"
0019 
0020 #include "ContextView.h"
0021 
0022 #include "AppletLoader.h"
0023 #include "AppletModel.h"
0024 #include "PaletteHandler.h"
0025 #include "SvgHandler.h"
0026 #include "amarokurls/AmarokUrlHandler.h"
0027 #include "amarokurls/ContextUrlRunner.h"
0028 #include "core/support/Amarok.h"
0029 #include "core/support/Debug.h"
0030 #include "core/meta/Meta.h"
0031 
0032 #include <QDesktopServices>
0033 #include <QFile>
0034 #include <QGuiApplication>
0035 #include <QQmlContext>
0036 #include <QQmlError>
0037 #include <QQmlPropertyMap>
0038 #include <QQuickWindow>
0039 
0040 #include <KDeclarative/KDeclarative>
0041 #include <KI18n/KLocalizedContext>
0042 #include <KIconThemes/KIconLoader>
0043 #include <KPackage/PackageLoader>
0044 
0045 
0046 namespace Context
0047 {
0048 
0049 ContextView* ContextView::s_self = nullptr;
0050 
0051 
0052 ContextView::ContextView( QWidget *parent )
0053     : QQuickWidget( parent )
0054     , m_urlRunner( nullptr )
0055     , m_loader( new AppletLoader( this ) )
0056     , m_appletModel( new AppletModel( m_loader, this ) )
0057     , m_proxyModel( new AppletProxyModel( m_appletModel, this ) )
0058 {
0059     DEBUG_BLOCK
0060 
0061     KDeclarative::KDeclarative decl;
0062     decl.setDeclarativeEngine( engine() );
0063     decl.setupBindings();
0064 
0065     connect( this, &QQuickWidget::statusChanged, this, &ContextView::slotStatusChanged );
0066     connect( The::paletteHandler(), &PaletteHandler::newPalette, this, &ContextView::updatePalette );
0067 
0068     m_urlRunner = new ContextUrlRunner();
0069     The::amarokUrlHandler()->registerRunner( m_urlRunner, QStringLiteral("context") );
0070 
0071     rootContext()->setContextProperty( QStringLiteral( "AppletModel" ), m_appletModel );
0072     rootContext()->setContextProperty( QStringLiteral( "AppletProxyModel" ), m_proxyModel );
0073     rootContext()->setContextProperty( QStringLiteral( "Context" ), this );
0074     rootContext()->setContextProperty( QStringLiteral( "Svg" ), The::svgHandler() );
0075 
0076     quickWindow()->setColor( The::paletteHandler()->palette().color( QPalette::Window ) );
0077 
0078     auto qmlPackage = KPackage::PackageLoader::self()->loadPackage( QStringLiteral( "KPackage/GenericQML" ),
0079                                                                     QStringLiteral( "org.kde.amarok.context" ) );
0080     Q_ASSERT( qmlPackage.isValid() );
0081 
0082     const QUrl sourceUrl = qmlPackage.fileUrl( "mainscript" );
0083 
0084     ::debug() << "Loading context qml mainscript:" << sourceUrl;
0085 
0086     setSource( sourceUrl );
0087     setResizeMode( SizeRootObjectToView );
0088 
0089     // keep this assignment at bottom so that premature usage of ::self() asserts out
0090     s_self = this;
0091 }
0092 
0093 ContextView::~ContextView()
0094 {
0095     DEBUG_BLOCK
0096 
0097     delete m_urlRunner;
0098     s_self = nullptr;
0099 }
0100 
0101 QStringList
0102 ContextView::currentApplets() const
0103 {
0104     QStringList appletNames;
0105     
0106     auto applets = m_loader->enabledApplets();
0107     for( const auto &applet : applets )
0108     {
0109         appletNames << applet.pluginId();
0110     }
0111 
0112     ::debug() << "Current applets: " << appletNames;
0113 
0114     return appletNames;
0115 }
0116 
0117 QStringList
0118 ContextView::currentAppletNames() const
0119 {
0120     QStringList appletNames;
0121 
0122     auto applets = m_loader->enabledApplets();
0123     for( const auto &applet : applets )
0124     {
0125         appletNames << applet.name();
0126     }
0127 
0128     ::debug() << "Current applet names: " << appletNames;
0129 
0130     return appletNames;
0131 }
0132 
0133 void
0134 ContextView::runLink( const QUrl& link ) const
0135 {
0136     if( link.scheme() == QStringLiteral( "amarok" ) )
0137     {
0138         AmarokUrl aUrl( link.toString() );
0139         aUrl.run();
0140     }
0141     else
0142         QDesktopServices::openUrl( link );
0143 }
0144 
0145 void
0146 ContextView::slotStatusChanged( Status status )
0147 {
0148     if( status == Error )
0149         for( const auto &e : errors() )
0150             error( e.description() );
0151 }
0152 
0153 void
0154 ContextView::updatePalette( const QPalette &palette )
0155 {
0156     quickWindow()->setColor( palette.color( QPalette::Window ) );
0157 }
0158 
0159 void
0160 ContextView::debug( const QString &error ) const
0161 {
0162     ::debug() << error;
0163 }
0164 
0165 void
0166 ContextView::warning( const QString &error ) const
0167 {
0168     ::warning() << error;
0169 }
0170 
0171 void
0172 ContextView::error( const QString &error ) const
0173 {
0174     ::error() << error;
0175 }
0176 
0177 } // Context namespace