PageRenderTime 31ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

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