PageRenderTime 86ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libtomahawk/resolvers/ExternalResolverGui.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 165 lines | 105 code | 29 blank | 31 comment | 17 complexity | 0b9e59e0348d10b4d518f13e5133269f MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "ExternalResolverGui.h"
  19. #include "Source.h"
  20. #include "utils/Logger.h"
  21. #include "accounts/AccountConfigWidget.h"
  22. #include <QMetaProperty>
  23. #include <QBuffer>
  24. #include <QDir>
  25. #include <QIcon>
  26. #include <QWidget>
  27. #include <QUiLoader>
  28. #include <QBoxLayout>
  29. #include <QPushButton>
  30. #include <QDesktopServices>
  31. Tomahawk::ExternalResolverGui::ExternalResolverGui(const QString& filePath)
  32. : Tomahawk::ExternalResolver(filePath)
  33. {
  34. }
  35. QVariant
  36. Tomahawk::ExternalResolverGui::configMsgFromWidget( QWidget* w )
  37. {
  38. if( !w )
  39. return QVariant();
  40. // generate a qvariantmap of all the widgets in the hierarchy, and for each one include the list of properties and values
  41. QVariantMap widgetMap;
  42. addChildProperties( w, widgetMap );
  43. // qDebug() << "Generated widget variant:" << widgetMap;
  44. return widgetMap;
  45. }
  46. void
  47. Tomahawk::ExternalResolverGui::addChildProperties( QObject* widget, QVariantMap& m )
  48. {
  49. // recursively add all properties of this widget to the map, then repeat on all children.
  50. // bare QWidgets are boring---so skip them! They have no input that the user can set.
  51. if( !widget || !widget->isWidgetType() )
  52. return;
  53. if( qstrcmp( widget->metaObject()->className(), "QWidget" ) != 0 )
  54. {
  55. // qDebug() << "Adding properties for this:" << widget->metaObject()->className();
  56. // add this widget's properties
  57. QVariantMap props;
  58. for( int i = 0; i < widget->metaObject()->propertyCount(); i++ )
  59. {
  60. QString prop = widget->metaObject()->property( i ).name();
  61. QVariant val = widget->property( prop.toLatin1() );
  62. // clean up for QJson....
  63. if( val.canConvert< QPixmap >() || val.canConvert< QImage >() || val.canConvert< QIcon >() )
  64. continue;
  65. props[ prop ] = val.toString();
  66. // qDebug() << QString( "%1: %2" ).arg( prop ).arg( props[ prop ].toString() );
  67. }
  68. m[ widget->objectName() ] = props;
  69. }
  70. // and recurse
  71. foreach( QObject* child, widget->children() )
  72. addChildProperties( child, m );
  73. }
  74. void
  75. Tomahawk::ExternalResolverGui::setupClickHandlerOnUrlButtons( QObject* widget )
  76. {
  77. if( !widget || !widget->isWidgetType() )
  78. return;
  79. if( qstrcmp( widget->metaObject()->className(), "QPushButton" ) == 0 && !widget->property( "url" ).isNull() )
  80. {
  81. QPushButton* button = qobject_cast< QPushButton* >( widget );
  82. Q_ASSERT( button );
  83. connect( button, &QPushButton::clicked, [=]() {
  84. QDesktopServices::openUrl( widget->property( "url" ).toUrl() );
  85. });
  86. }
  87. // and recurse
  88. foreach( QObject* child, widget->children() )
  89. setupClickHandlerOnUrlButtons( child );
  90. }
  91. AccountConfigWidget*
  92. Tomahawk::ExternalResolverGui::widgetFromData( QByteArray& data, QWidget* parent )
  93. {
  94. if( data.isEmpty() )
  95. return 0;
  96. AccountConfigWidget* configWidget = new AccountConfigWidget( parent );
  97. QUiLoader l;
  98. QBuffer b( &data );
  99. QWidget* w = l.load( &b, configWidget );
  100. setupClickHandlerOnUrlButtons( w );
  101. // HACK: proper way would be to create a designer plugin for this widget type
  102. configWidget->setLayout( new QBoxLayout( QBoxLayout::TopToBottom ) );
  103. configWidget->layout()->addWidget( w );
  104. #ifdef Q_OS_MAC
  105. w->setContentsMargins( 12, 12, 12, 12 );
  106. #else
  107. w->setContentsMargins( 6, 6, 6, 6 );
  108. #endif
  109. return configWidget;
  110. }
  111. QByteArray
  112. Tomahawk::ExternalResolverGui::fixDataImagePaths( const QByteArray& data, bool compressed, const QVariantMap& images )
  113. {
  114. // with a list of images and image data, write each to a temp file, replace the path in the .ui file with the temp file path
  115. QString uiFile = QString::fromUtf8( data );
  116. foreach( const QString& filename, images.keys() )
  117. {
  118. if( !uiFile.contains( filename ) ) // make sure the image is used
  119. continue;
  120. QString fullPath = QDir::tempPath() + "/" + filename;
  121. QFile imgF( fullPath );
  122. if( !imgF.open( QIODevice::WriteOnly ) )
  123. {
  124. qWarning() << "Failed to write to temporary image in UI file:" << filename << fullPath;
  125. continue;
  126. }
  127. QByteArray data = images[ filename ].toByteArray();
  128. // qDebug() << "expanding data:" << data << compressed;
  129. data = compressed ? qUncompress( QByteArray::fromBase64( data ) ) : QByteArray::fromBase64( data );
  130. imgF.write( data );
  131. imgF.close();
  132. // replace the path to the image with the real path
  133. uiFile.replace( filename, fullPath );
  134. }
  135. return uiFile.toUtf8();
  136. }