summaryrefslogtreecommitdiffstats
path: root/juk/coverdialog.cpp
blob: 9c81126b773f34a11ea17b5fc6696fbcf1ce51bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/***************************************************************************
    begin                : Sun May 15 2005 
    copyright            : (C) 2005 by Michael Pyne
    email                : [email protected]
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tdelistview.h>
#include <kiconview.h>
#include <kiconviewsearchline.h>
#include <kiconloader.h>
#include <tdeapplication.h>
#include <tdepopupmenu.h>
#include <tdelocale.h>

#include <tqtimer.h>
#include <tqtoolbutton.h>

#include "coverdialog.h"
#include "covericonview.h"
#include "covermanager.h"
#include "collectionlist.h"

using CoverUtility::CoverIconViewItem;

class AllArtistsListViewItem : public TDEListViewItem
{
public:
    AllArtistsListViewItem(TQListView *parent) :
        TDEListViewItem(parent, i18n("<All Artists>"))
    {
    }

    int compare(TQListViewItem *, int, bool) const
    {
        return -1; // Always be at the top.
    }
};

class CaseInsensitiveItem : public TDEListViewItem
{
public:
    CaseInsensitiveItem(TQListView *parent, const TQString &text) :
        TDEListViewItem(parent, text)
    {
    }

    int compare(TQListViewItem *item, int column, bool ascending) const
    {
        Q_UNUSED(ascending);
        return text(column).lower().localeAwareCompare(item->text(column).lower());
    }
};

CoverDialog::CoverDialog(TQWidget *parent) :
    CoverDialogBase(parent, "juk_cover_dialog", WType_Dialog)
{
    m_covers->setResizeMode(TQIconView::Adjust);
    m_covers->setGridX(140);
    m_covers->setGridY(150);

    m_searchLine->setIconView(m_covers);
    m_clearSearch->setIconSet(SmallIconSet("locationbar_erase"));
}

CoverDialog::~CoverDialog()
{
}

void CoverDialog::show()
{
    m_artists->clear();
    m_covers->clear();

    TQStringList artists = CollectionList::instance()->uniqueSet(CollectionList::Artists);

    m_artists->setSorting(-1);
    new AllArtistsListViewItem(m_artists);
    for(TQStringList::ConstIterator it = artists.begin(); it != artists.end(); ++it)
        new CaseInsensitiveItem(m_artists, *it);

    m_artists->setSorting(0);

    TQTimer::singleShot(0, this, TQT_SLOT(loadCovers()));
    CoverDialogBase::show();
}

// Here we try to keep the GUI from freezing for too long while we load the
// covers.
void CoverDialog::loadCovers()
{
    TQValueList<coverKey> keys = CoverManager::keys();
    TQValueList<coverKey>::ConstIterator it;
    int i = 0;

    for(it = keys.begin(); it != keys.end(); ++it) {
        new CoverIconViewItem(*it, m_covers);

        if(++i == 10) {
            i = 0;
            kapp->processEvents();
        }
    }
}

// TODO: Add a way to show cover art for tracks with no artist.
void CoverDialog::slotArtistClicked(TQListViewItem *item)
{
    m_covers->clear();

    if(dynamic_cast<AllArtistsListViewItem *>(item)) {
        // All artists.
        loadCovers();
    }
    else {
        TQString artist = item->text(0).lower();
        TQValueList<coverKey> keys = CoverManager::keys();
        TQValueList<coverKey>::ConstIterator it;

        for(it = keys.begin(); it != keys.end(); ++it) {
            CoverDataPtr data = CoverManager::coverInfo(*it);
            if(data->artist == artist)
                new CoverIconViewItem(*it, m_covers);
        }
    }
}

void CoverDialog::slotContextRequested(TQIconViewItem *item, const TQPoint &pt)
{
    static TDEPopupMenu *menu = 0;

    if(!item)
        return;

    if(!menu) {
        menu = new TDEPopupMenu(this);
        menu->insertItem(i18n("Remove Cover"), this, TQT_SLOT(removeSelectedCover()));
    }

    menu->popup(pt);
}

void CoverDialog::removeSelectedCover()
{
    CoverIconViewItem *coverItem = m_covers->currentItem();

    if(!coverItem || !coverItem->isSelected()) {
        kdWarning(65432) << "No item selected for removeSelectedCover.\n";
        return;
    }

    if(!CoverManager::removeCover(coverItem->id()))
        kdError(65432) << "Unable to remove selected cover: " << coverItem->id() << endl;
    else
        delete coverItem;
}

#include "coverdialog.moc"