~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/MediaDeviceConfirmDialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Devid Filoni
  • Date: 2008-07-14 16:46:20 UTC
  • mfrom: (1.1.7 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080714164620-67hoz9fs177wpgmr
Tags: 1:1.5.1.31879.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #248100), remaining changes:
  - debian/rules: add dh_icons call
  + debian/control:
    - switch iceweasel to firefox in Recommends field
    - modify debhelper version to >= 5.0.51~
    - modify Maintainer to Ubuntu MOTU Developers

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2005 - 2007 by                                          *
3
 
 *      Christian Muehlhaeuser, Last.fm Ltd <chris@last.fm>                *
4
 
 *      Erik Jaelevik, Last.fm Ltd <erik@last.fm>                          *
5
 
 *                                                                         *
6
 
 *   This program is free software; you can redistribute it and/or modify  *
7
 
 *   it under the terms of the GNU General Public License as published by  *
8
 
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 
 *   (at your option) any later version.                                   *
10
 
 *                                                                         *
11
 
 *   This program is distributed in the hope that it will be useful,       *
12
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 
 *   GNU General Public License for more details.                          *
15
 
 *                                                                         *
16
 
 *   You should have received a copy of the GNU General Public License     *
17
 
 *   along with this program; if not, write to the                         *
18
 
 *   Free Software Foundation, Inc.,                                       *
19
 
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02110-1301, USA.          *
20
 
 ***************************************************************************/
21
 
 
22
 
#include "MediaDeviceConfirmDialog.h"
23
 
 
24
 
#include "container.h"
25
 
#include "MooseCommon.h"
26
 
#include "logger.h"
27
 
#include "Scrobbler-1.2.h"
28
 
#include "User.h"
29
 
 
30
 
#include <QString>
31
 
#include <QDebug>
32
 
#include <QDesktopWidget>
33
 
 
34
 
 
35
 
MediaDeviceConfirmDialog::MediaDeviceConfirmDialog( const QString& username, QWidget *parent )
36
 
        : QDialog( parent, Qt::Sheet ),
37
 
          m_username( username )
38
 
{
39
 
    m_tracks = ScrobbleCache::mediaDeviceCache( m_username ).tracks();
40
 
}
41
 
 
42
 
 
43
 
void
44
 
MediaDeviceConfirmDialog::setupUi()
45
 
{
46
 
    ui.setupUi( this );
47
 
 
48
 
    addTracksToView();
49
 
    
50
 
    QPixmap p( MooseUtils::dataPath( "app_55.png" ) );
51
 
//     p = p.scaled( 32, 32, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
52
 
    ui.iconLabel->setPixmap( p );
53
 
 
54
 
////// This section sizes the dialog as best as possible, it is a shame but
55
 
    // Qt sizeHints usually ignore the view contents size, so we do it manually.
56
 
 
57
 
    //this magnificent hack bought to you by teh mxcl
58
 
    // @short allow access to this protected function so we can determine the
59
 
    // ideal width for the confirm dialog
60
 
    struct NoProtection : QAbstractItemView
61
 
    {
62
 
        using QAbstractItemView::sizeHintForColumn;
63
 
    };
64
 
 
65
 
    int w = 0, desiredwidth = 0;
66
 
    for (int x = 0; x < ui.tracksWidget->columnCount(); ++x) {
67
 
        desiredwidth = reinterpret_cast<NoProtection*>(ui.tracksWidget)->sizeHintForColumn( x );
68
 
        w += desiredwidth;
69
 
    }
70
 
    
71
 
    ui.tracksWidget->setMinimumWidth( w );
72
 
    if (m_tracks.count() > 10 )
73
 
        ui.tracksWidget->setMinimumHeight( ui.tracksWidget->sizeHint().height() * 2 );
74
 
 
75
 
    //make us always the right size
76
 
    layout()->setSizeConstraint( QLayout::SetMinimumSize );
77
 
    
78
 
    int const W = QDesktopWidget().availableGeometry().width();
79
 
    if (sizeHint().width() > W)
80
 
        ui.tracksWidget->setMinimumWidth( W
81
 
                                          - (sizeHint().width() - ui.tracksWidget->width()) 
82
 
                                          - 10 /*small aesthetic gap*/ );
83
 
 
84
 
///////
85
 
    connect( ui.scrobble, SIGNAL(clicked()), SLOT(accept()) );
86
 
    connect( ui.cancel,   SIGNAL(clicked()), SLOT(reject()) );
87
 
    connect( ui.toggle,   SIGNAL(clicked()), SLOT(toggleChecked()) );
88
 
    
89
 
///////
90
 
    activateWindow();
91
 
}
92
 
 
93
 
 
94
 
void
95
 
MediaDeviceConfirmDialog::addTracksToView()
96
 
{
97
 
    QList<QTreeWidgetItem*> items;
98
 
    int index = 0;
99
 
    foreach (TrackInfo t, m_tracks)
100
 
    {
101
 
        QDateTime dt = QDateTime::fromTime_t( t.timeStamp() );
102
 
        QTreeWidgetItem* widget = new QTreeWidgetItem;
103
 
        widget->setData( 0, Qt::DisplayRole, t.artist() );
104
 
        widget->setData( 1, Qt::DisplayRole, t.track() );
105
 
        widget->setData( 2, Qt::DisplayRole, dt );
106
 
        widget->setData( 3, Qt::DisplayRole, QString::number(t.playCount()) );
107
 
 
108
 
        widget->setFlags( widget->flags() | Qt::ItemIsUserCheckable );
109
 
        widget->setCheckState( 0, Qt::Checked );
110
 
 
111
 
        widget->setData( 0, Qt::UserRole, index++ );
112
 
 
113
 
        items += widget;
114
 
    }
115
 
 
116
 
    ui.tracksWidget->insertTopLevelItems( 0, items );
117
 
    ui.tracksWidget->resizeColumnToContents( 0 );
118
 
    ui.tracksWidget->resizeColumnToContents( 1 );
119
 
    ui.tracksWidget->resizeColumnToContents( 2 );
120
 
    ui.tracksWidget->resizeColumnToContents( 4 );
121
 
 
122
 
    ui.tracksWidget->setSortingEnabled( true );
123
 
    ui.tracksWidget->sortByColumn( 2, Qt::DescendingOrder );
124
 
 
125
 
    QString text = tr( "<p>Last.fm found %n new track(s) since the last time you synced your iPod.<br/>"
126
 
                       "Please select the tracks that you want to be scrobbled from your iPod.",
127
 
                       "", items.count() );
128
 
 
129
 
    // avoid lawsuits
130
 
    if (The::user().name() != m_username)
131
 
        text += "<p><b>" + tr("This iPod scrobbles to %1's profile!").arg( m_username );
132
 
    
133
 
    ui.messageLabel->setText( text );
134
 
}
135
 
 
136
 
 
137
 
void
138
 
MediaDeviceConfirmDialog::toggleChecked()
139
 
{
140
 
    for (int x = 0; x < ui.tracksWidget->topLevelItemCount(); ++x) 
141
 
    {
142
 
        QTreeWidgetItem* i = ui.tracksWidget->topLevelItem( x );
143
 
        i->setCheckState( 0, i->checkState( 0 ) == Qt::Checked ? Qt::Unchecked : Qt::Checked );
144
 
    }
145
 
}
146
 
 
147
 
 
148
 
int
149
 
MediaDeviceConfirmDialog::exec()
150
 
{
151
 
    if (m_tracks.count())
152
 
    {
153
 
        setupUi();
154
 
        return QDialog::exec();
155
 
    }
156
 
    else
157
 
        return QDialog::Rejected;
158
 
}
159
 
 
160
 
 
161
 
QList<TrackInfo>
162
 
MediaDeviceConfirmDialog::tracks() const
163
 
{
164
 
    QList<TrackInfo> tracks;
165
 
    for (int x = 0; x < ui.tracksWidget->topLevelItemCount(); ++x)
166
 
    {
167
 
        QTreeWidgetItem* item = ui.tracksWidget->topLevelItem( x );
168
 
        if (item->checkState( 0 ) == Qt::Checked)
169
 
        {
170
 
            int const index = item->data( 0, Qt::UserRole ).toInt();
171
 
            tracks += m_tracks[index];
172
 
        }
173
 
    }
174
 
    return tracks;
175
 
}