/src/libtomahawk/jobview/TransferStatusItem.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 129 lines · 87 code · 25 blank · 17 comment · 15 complexity · f946f6b37fdfb2c1da4c0c3b3ba7f30f MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 "TransferStatusItem.h"
  19. #include "network/StreamConnection.h"
  20. #include "network/Servent.h"
  21. #include "utils/TomahawkUtils.h"
  22. #include "utils/TomahawkUtilsGui.h"
  23. #include "Artist.h"
  24. #include "JobStatusModel.h"
  25. #include "JobStatusView.h"
  26. #include "Result.h"
  27. #include "Source.h"
  28. #include "Track.h"
  29. TransferStatusItem::TransferStatusItem( TransferStatusManager* p, StreamConnection* sc )
  30. : m_parent( p )
  31. , m_stream( QPointer< StreamConnection >( sc ) )
  32. {
  33. if ( m_stream.data()->type() == StreamConnection::RECEIVING )
  34. m_type = "receive";
  35. else
  36. m_type = "send";
  37. connect( m_stream.data(), SIGNAL( updated() ), SLOT( onTransferUpdate() ) );
  38. connect( Servent::instance(), SIGNAL( streamFinished( StreamConnection* ) ), SLOT( streamFinished( StreamConnection* ) ) );
  39. }
  40. TransferStatusItem::~TransferStatusItem()
  41. {
  42. }
  43. QString
  44. TransferStatusItem::mainText() const
  45. {
  46. if ( m_stream.isNull() )
  47. return QString();
  48. if ( m_stream.data()->source().isNull() && !m_stream.data()->track().isNull() )
  49. return QString( "%1" ).arg( QString( "%1 - %2" ).arg( m_stream.data()->track()->track()->artist() ).arg( m_stream.data()->track()->track()->track() ) );
  50. else if ( !m_stream.data()->source().isNull() && !m_stream.data()->track().isNull() )
  51. return QString( "%1 %2 %3" ).arg( QString( "%1 - %2" ).arg( m_stream.data()->track()->track()->artist() ).arg( m_stream.data()->track()->track()->track() ) )
  52. .arg( m_stream.data()->type() == StreamConnection::RECEIVING ? tr( "from", "streaming artist - track from friend" ) : tr( "to", "streaming artist - track to friend" ) )
  53. .arg( m_stream.data()->source()->friendlyName() );
  54. else
  55. return QString();
  56. }
  57. QString
  58. TransferStatusItem::rightColumnText() const
  59. {
  60. if ( m_stream.isNull() )
  61. return QString();
  62. return QString( "%1 kB/s" ).arg( m_stream.data()->transferRate() / 1000 );
  63. }
  64. void
  65. TransferStatusItem::streamFinished( StreamConnection* sc )
  66. {
  67. if ( m_stream.data() == sc )
  68. emit finished();
  69. }
  70. QPixmap
  71. TransferStatusItem::icon() const
  72. {
  73. if ( m_stream.isNull() )
  74. return QPixmap();
  75. if ( m_stream.data()->type() == StreamConnection::SENDING )
  76. return m_parent->txPixmap();
  77. else
  78. return m_parent->rxPixmap();
  79. }
  80. void
  81. TransferStatusItem::onTransferUpdate()
  82. {
  83. emit statusChanged();
  84. }
  85. TransferStatusManager::TransferStatusManager( QObject* parent )
  86. : QObject( parent )
  87. {
  88. connect( Servent::instance(), SIGNAL( streamStarted( StreamConnection* ) ), SLOT( streamRegistered( StreamConnection* ) ) );
  89. }
  90. void
  91. TransferStatusManager::streamRegistered( StreamConnection* sc )
  92. {
  93. JobStatusView::instance()->model()->addJob( new TransferStatusItem( this, sc ) );
  94. }
  95. QPixmap
  96. TransferStatusManager::rxPixmap() const
  97. {
  98. return TomahawkUtils::defaultPixmap( TomahawkUtils::Downloading, TomahawkUtils::Original, QSize( 128, 128 ) );
  99. }
  100. QPixmap
  101. TransferStatusManager::txPixmap() const
  102. {
  103. return TomahawkUtils::defaultPixmap( TomahawkUtils::Uploading, TomahawkUtils::Original, QSize( 128, 128 ) );
  104. }