/src/context/ContextView.cpp

https://github.com/KDE/amarok · C++ · 177 lines · 123 code · 37 blank · 17 comment · 7 complexity · 9724ec69033e40facfaf66a2a2b60526 MD5 · raw file

  1. /****************************************************************************************
  2. * Copyright (c) 2007-2008 Leo Franchi <lfranchi@gmail.com> *
  3. * Copyright (c) 2008 William Viana Soares <vianasw@gmail.com> *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify it under *
  6. * the terms of the GNU General Public License as published by the Free Software *
  7. * Foundation; either version 2 of the License, or (at your option) any later *
  8. * version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
  12. * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License along with *
  15. * this program. If not, see <http://www.gnu.org/licenses/>. *
  16. ****************************************************************************************/
  17. #define DEBUG_PREFIX "ContextView"
  18. #include "ContextView.h"
  19. #include "AppletLoader.h"
  20. #include "AppletModel.h"
  21. #include "PaletteHandler.h"
  22. #include "SvgHandler.h"
  23. #include "amarokurls/AmarokUrlHandler.h"
  24. #include "amarokurls/ContextUrlRunner.h"
  25. #include "core/support/Amarok.h"
  26. #include "core/support/Debug.h"
  27. #include "core/meta/Meta.h"
  28. #include <QDesktopServices>
  29. #include <QFile>
  30. #include <QGuiApplication>
  31. #include <QQmlContext>
  32. #include <QQmlError>
  33. #include <QQmlPropertyMap>
  34. #include <QQuickWindow>
  35. #include <KDeclarative/KDeclarative>
  36. #include <KI18n/KLocalizedContext>
  37. #include <KIconThemes/KIconLoader>
  38. #include <KPackage/PackageLoader>
  39. namespace Context
  40. {
  41. ContextView* ContextView::s_self = nullptr;
  42. ContextView::ContextView( QWidget *parent )
  43. : QQuickWidget( parent )
  44. , m_urlRunner( nullptr )
  45. , m_loader( new AppletLoader( this ) )
  46. , m_appletModel( new AppletModel( m_loader, this ) )
  47. , m_proxyModel( new AppletProxyModel( m_appletModel, this ) )
  48. {
  49. DEBUG_BLOCK
  50. KDeclarative::KDeclarative decl;
  51. decl.setDeclarativeEngine( engine() );
  52. decl.setupBindings();
  53. connect( this, &QQuickWidget::statusChanged, this, &ContextView::slotStatusChanged );
  54. connect( The::paletteHandler(), &PaletteHandler::newPalette, this, &ContextView::updatePalette );
  55. m_urlRunner = new ContextUrlRunner();
  56. The::amarokUrlHandler()->registerRunner( m_urlRunner, QStringLiteral("context") );
  57. rootContext()->setContextProperty( QStringLiteral( "AppletModel" ), m_appletModel );
  58. rootContext()->setContextProperty( QStringLiteral( "AppletProxyModel" ), m_proxyModel );
  59. rootContext()->setContextProperty( QStringLiteral( "Context" ), this );
  60. rootContext()->setContextProperty( QStringLiteral( "Svg" ), The::svgHandler() );
  61. quickWindow()->setColor( The::paletteHandler()->palette().color( QPalette::Window ) );
  62. auto qmlPackage = KPackage::PackageLoader::self()->loadPackage( QStringLiteral( "KPackage/GenericQML" ),
  63. QStringLiteral( "org.kde.amarok.context" ) );
  64. Q_ASSERT( qmlPackage.isValid() );
  65. const QUrl sourceUrl = qmlPackage.fileUrl( "mainscript" );
  66. ::debug() << "Loading context qml mainscript:" << sourceUrl;
  67. setSource( sourceUrl );
  68. setResizeMode( SizeRootObjectToView );
  69. // keep this assignment at bottom so that premature usage of ::self() asserts out
  70. s_self = this;
  71. }
  72. ContextView::~ContextView()
  73. {
  74. DEBUG_BLOCK
  75. delete m_urlRunner;
  76. s_self = nullptr;
  77. }
  78. QStringList
  79. ContextView::currentApplets() const
  80. {
  81. QStringList appletNames;
  82. auto applets = m_loader->enabledApplets();
  83. for( const auto &applet : applets )
  84. {
  85. appletNames << applet.pluginId();
  86. }
  87. ::debug() << "Current applets: " << appletNames;
  88. return appletNames;
  89. }
  90. QStringList
  91. ContextView::currentAppletNames() const
  92. {
  93. QStringList appletNames;
  94. auto applets = m_loader->enabledApplets();
  95. for( const auto &applet : applets )
  96. {
  97. appletNames << applet.name();
  98. }
  99. ::debug() << "Current applet names: " << appletNames;
  100. return appletNames;
  101. }
  102. void
  103. ContextView::runLink( const QUrl& link ) const
  104. {
  105. if( link.scheme() == QStringLiteral( "amarok" ) )
  106. {
  107. AmarokUrl aUrl( link.toString() );
  108. aUrl.run();
  109. }
  110. else
  111. QDesktopServices::openUrl( link );
  112. }
  113. void
  114. ContextView::slotStatusChanged( Status status )
  115. {
  116. if( status == Error )
  117. for( const auto &e : errors() )
  118. error( e.description() );
  119. }
  120. void
  121. ContextView::updatePalette( const QPalette &palette )
  122. {
  123. quickWindow()->setColor( palette.color( QPalette::Window ) );
  124. }
  125. void
  126. ContextView::debug( const QString &error ) const
  127. {
  128. ::debug() << error;
  129. }
  130. void
  131. ContextView::warning( const QString &error ) const
  132. {
  133. ::warning() << error;
  134. }
  135. void
  136. ContextView::error( const QString &error ) const
  137. {
  138. ::error() << error;
  139. }
  140. } // Context namespace