/src/libtomahawk/jobview/LatchedStatusItem.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 120 lines · 84 code · 19 blank · 17 comment · 10 complexity · 10452f85b032b7226c9acd91ca0932d0 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 "LatchedStatusItem.h"
  19. #include "Source.h"
  20. #include "SourceList.h"
  21. #include "JobStatusView.h"
  22. #include "JobStatusModel.h"
  23. #include "utils/TomahawkUtilsGui.h"
  24. LatchedStatusItem::LatchedStatusItem( const Tomahawk::source_ptr& from, const Tomahawk::source_ptr& to, LatchedStatusManager* parent )
  25. : JobStatusItem()
  26. , m_from( from )
  27. , m_to( to )
  28. , m_parent( parent )
  29. {
  30. m_text = tr( "%1 is listening along with you!" ).arg( from->friendlyName() );
  31. }
  32. LatchedStatusItem::~LatchedStatusItem()
  33. {
  34. }
  35. QPixmap
  36. LatchedStatusItem::icon() const
  37. {
  38. return m_parent->pixmap();
  39. }
  40. QString
  41. LatchedStatusItem::mainText() const
  42. {
  43. return m_text;
  44. }
  45. QString
  46. LatchedStatusItem::type() const
  47. {
  48. return "latched";
  49. }
  50. void LatchedStatusItem::stop()
  51. {
  52. emit finished();
  53. }
  54. LatchedStatusManager::LatchedStatusManager( QObject* parent )
  55. : QObject( parent )
  56. {
  57. connect( SourceList::instance(), SIGNAL( sourceLatchedOn( Tomahawk::source_ptr, Tomahawk::source_ptr ) ), this, SLOT( latchedOn( Tomahawk::source_ptr, Tomahawk::source_ptr ) ) );
  58. connect( SourceList::instance(), SIGNAL( sourceLatchedOff( Tomahawk::source_ptr, Tomahawk::source_ptr ) ), this, SLOT( latchedOff( Tomahawk::source_ptr, Tomahawk::source_ptr ) ) );
  59. }
  60. void
  61. LatchedStatusManager::latchedOn( const Tomahawk::source_ptr& from, const Tomahawk::source_ptr& to )
  62. {
  63. if ( from.isNull() || to.isNull() )
  64. return;
  65. if ( to->isLocal() )
  66. {
  67. LatchedStatusItem* item = new LatchedStatusItem( from, to, this );
  68. m_jobs[ from->nodeId() ] = item;
  69. JobStatusView::instance()->model()->addJob( item );
  70. connect( from.data(), SIGNAL( offline() ), this, SLOT( sourceOffline() ), Qt::UniqueConnection );
  71. }
  72. }
  73. void
  74. LatchedStatusManager::sourceOffline()
  75. {
  76. Tomahawk::Source* s = qobject_cast< Tomahawk::Source* >( sender() );
  77. Q_ASSERT( s );
  78. if ( m_jobs.contains( s->nodeId() ) )
  79. {
  80. QPointer< LatchedStatusItem> job = m_jobs.take( s->nodeId() ).data();
  81. if ( !job.isNull() )
  82. job.data()->stop();
  83. }
  84. }
  85. void
  86. LatchedStatusManager::latchedOff( const Tomahawk::source_ptr& from, const Tomahawk::source_ptr& to )
  87. {
  88. if ( from.isNull() || to.isNull() )
  89. return;
  90. if ( to->isLocal() && m_jobs.contains( from->nodeId() ) )
  91. {
  92. QPointer< LatchedStatusItem > item = m_jobs.take( from->nodeId() );
  93. if ( !item.isNull() )
  94. item.data()->stop();
  95. }
  96. }
  97. QPixmap
  98. LatchedStatusManager::pixmap() const
  99. {
  100. return TomahawkUtils::defaultPixmap( TomahawkUtils::HeadphonesOn, TomahawkUtils::Original, QSize( 128, 128 ) );
  101. }