Beispiel #1
0
void
AccountModel::loadData()
{
    beginResetModel();

    qDeleteAll( m_accounts );
    m_accounts.clear();

    // Add all factories
    QList< AccountFactory* > factories = AccountManager::instance()->factories();
    QList< Account* > allAccounts = AccountManager::instance()->accounts();
#if ACCOUNTMODEL_DEBUG
    qDebug() << Q_FUNC_INFO;
    qDebug() << "All accounts:";
    foreach ( Account* acct, allAccounts )
        qDebug() << acct->accountFriendlyName() << "\t" << acct->accountId();
#endif
    foreach ( AccountFactory* fac, factories )
    {
        if ( !fac->allowUserCreation() )
            continue;

        qDebug() << "Creating factory node:" << fac->prettyName();
        m_accounts << new AccountModelNode( fac );

        // remove the accounts we are dealing with
        foreach ( Account* acct, allAccounts )
        {
            if ( AccountManager::instance()->factoryForAccount( acct ) == fac )
                allAccounts.removeAll( acct );
        }
    }

    // add all attica resolvers (installed or uninstalled)
    Attica::Content::List fromAttica = AtticaManager::instance()->resolvers();
    foreach ( const Attica::Content& content, fromAttica )
    {
        qDebug() << "Loading ATTICA ACCOUNT with content:" << content.id() << content.name();
        if ( AtticaManager::instance()->hasCustomAccountForAttica( content.id() ) )
        {
            Account* acct = AtticaManager::instance()->customAccountForAttica( content.id() );
            Q_ASSERT( acct );
            if ( acct )
            {
                m_accounts << new AccountModelNode( acct );
                const int removed = allAccounts.removeAll( acct );
#if ACCOUNTMODEL_DEBUG
                qDebug() << "Removed custom account from misc accounts list, found:" << removed;
                qDebug() << "All accounts after remove:";
                foreach ( Account* acct, allAccounts )
                    qDebug() << acct->accountFriendlyName() << "\t" << acct->accountId();    // All other accounts we haven't dealt with yet
#else
                Q_UNUSED( removed );
#endif
            }
        } else
        {
            m_accounts << new AccountModelNode( content );

            foreach ( Account* acct, AccountManager::instance()->accounts( Accounts::ResolverType ) )
            {
#if ACCOUNTMODEL_DEBUG
                qDebug() << "Found ResolverAccount" << acct->accountFriendlyName();
#endif
                if ( AtticaResolverAccount* resolver = qobject_cast< AtticaResolverAccount* >( acct ) )
                {
#if ACCOUNTMODEL_DEBUG
                    qDebug() << "Which is an attica resolver with id:" << resolver->atticaId();
#endif
                    if ( resolver->atticaId() == content.id() )
                    {
                        allAccounts.removeAll( acct );
                    }
                }
            }
        }
void
AccountFactoryWrapperDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QStyleOptionViewItemV4 opt = option;
    initStyleOption( &opt, index );

    const int center = opt.rect.height() / 2 + opt.rect.top();
    const int topIcon = center - ICON_SIZE/2;

    // draw the background
    const QWidget* w = opt.widget;
    QStyle* style = w ? w->style() : QApplication::style();
    style->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter, w );

    Account* acc = qobject_cast< Account* >( index.data( AccountFactoryWrapper::AccountRole ).value< QObject* >() );
    Q_ASSERT( acc );

    // Checkbox on left edge, then text
    const QRect checkRect( PADDING/4, PADDING/4 + opt.rect.top(), opt.rect.height() - PADDING/4, opt.rect.height() - PADDING/4 );
    m_cachedCheckRects[ index ] = checkRect;
    QStyleOptionViewItemV4 opt2 = opt;
    opt2.rect = checkRect;
    opt.checkState == Qt::Checked ? opt2.state |= QStyle::State_On : opt2.state |= QStyle::State_Off;
    style->drawPrimitive( QStyle::PE_IndicatorViewItemCheck, &opt2, painter, w );

    // name on left
    painter->drawText( opt.rect.adjusted( checkRect.right() + PADDING, PADDING, -PADDING, -PADDING ), Qt::AlignLeft | Qt::AlignVCenter, acc->accountFriendlyName() );

    // remove, config, status on right
    const QRect pmRect( opt.rect.right() - PADDING - ICON_SIZE, topIcon, ICON_SIZE, ICON_SIZE );
    painter->drawPixmap( pmRect, TomahawkUtils::defaultPixmap( TomahawkUtils::ListRemove, TomahawkUtils::Original, pmRect.size() ) );
    m_cachedButtonRects[ index ] = pmRect;

    const QRect confRect( pmRect.left() - PADDING - CONFIG_WRENCH_SIZE, center - CONFIG_WRENCH_SIZE/2, CONFIG_WRENCH_SIZE, CONFIG_WRENCH_SIZE );

    QStyleOptionToolButton topt;
    topt.rect = confRect;
    topt.pos = confRect.topLeft();
    topt.font = opt.font;
    topt.icon = ImageRegistry::instance()->pixmap( RESPATH "images/configure.svg", QSize( CONFIG_WRENCH_SIZE - 8, CONFIG_WRENCH_SIZE - 8 ) );
    topt.iconSize = QSize( CONFIG_WRENCH_SIZE - 8, CONFIG_WRENCH_SIZE - 8 );
    topt.subControls = QStyle::SC_ToolButton;
    topt.activeSubControls = QStyle::SC_None;
    topt.features = QStyleOptionToolButton::None;
    bool pressed = ( m_configPressed == opt.index );
    topt.state = pressed ? QStyle::State_On : QStyle::State_Raised;
    if( opt.state & QStyle::State_MouseOver || pressed )
        topt.state |= QStyle::State_HasFocus;
    style->drawComplexControl( QStyle::CC_ToolButton, &topt, painter, w );
    m_cachedConfigRects[ index ] = confRect;

    QPixmap p;
    QString statusText;
    Account::ConnectionState state = acc->connectionState();
    const QRect connectIconRect( confRect.left() - PADDING - ICON_SIZE, topIcon, ICON_SIZE, ICON_SIZE );

    if ( state == Account::Connected )
    {
        p = TomahawkUtils::defaultPixmap( TomahawkUtils::SipPluginOnline, TomahawkUtils::Original, connectIconRect.size() );
        statusText = tr( "Online" );
    }
    else if ( state == Account::Connecting )
    {
        p = TomahawkUtils::defaultPixmap( TomahawkUtils::SipPluginOffline, TomahawkUtils::Original, connectIconRect.size() );
        statusText = tr( "Connecting..." );
    }
    else
    {
        p = TomahawkUtils::defaultPixmap( TomahawkUtils::SipPluginOffline, TomahawkUtils::Original, connectIconRect.size() );
        statusText = tr( "Offline" );
    }

    painter->drawPixmap( connectIconRect, p );

    int width = painter->fontMetrics().width( statusText );
    painter->drawText( QRect( connectIconRect.left() - PADDING - width, center - painter->fontMetrics().height()/2, width, painter->fontMetrics().height() ), statusText );

}