File: mpdsearchmodel.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (128 lines) | stat: -rw-r--r-- 3,482 bytes parent folder | download
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
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "mpdsearchmodel.h"
#include "gui/covers.h"
#include "mpd-interface/mpdconnection.h"
#include "roles.h"

MpdSearchModel::MpdSearchModel(QObject* parent)
	: SearchModel(parent), currentId(0)
{
	connect(this, SIGNAL(getRating(QString)), MPDConnection::self(), SLOT(getRating(QString)));
	connect(this, SIGNAL(search(QString, QString, int)), MPDConnection::self(), SLOT(search(QString, QString, int)));
	connect(MPDConnection::self(), SIGNAL(searchResponse(int, QList<Song>)), this, SLOT(searchFinished(int, QList<Song>)));
	connect(MPDConnection::self(), SIGNAL(rating(QString, quint8)), SLOT(ratingResult(QString, quint8)));
	connect(Covers::self(), SIGNAL(loaded(Song, int)), this, SLOT(coverLoaded(Song, int)));
}

MpdSearchModel::~MpdSearchModel()
{
}

QVariant MpdSearchModel::data(const QModelIndex& index, int role) const
{
	if (!index.isValid() && Cantata::Role_RatingCol == role) {
		return COL_RATING;
	}

	const Song* song = toSong(index);

	if (!song) {
		return QVariant();
	}

	switch (role) {
	case Cantata::Role_SongWithRating: {
		QVariant var;
		if (Song::Standard == song->type && Song::Rating_Null == song->rating) {
			emit getRating(song->file);
			song->rating = Song::Rating_Requested;
		}
		var.setValue(*song);
		return var;
	}
	default:
		return SearchModel::data(index, role);
	}
	return QVariant();
}

void MpdSearchModel::clear()
{
	SearchModel::clear();
	currentId++;
}

void MpdSearchModel::search(const QString& key, const QString& value)
{
	if (key == currentKey && value == currentValue) {
		return;
	}
	emit searching();
	clear();
	currentKey = key;
	currentValue = value;
	currentId++;
	emit search(key, value, currentId);
}

void MpdSearchModel::searchFinished(int id, const QList<Song>& result)
{
	if (id != currentId) {
		return;
	}

	results(result);
}

void MpdSearchModel::coverLoaded(const Song& song, int s)
{
	Q_UNUSED(s)
	if (!song.isArtistImageRequest() && !song.isComposerImageRequest()) {
		int row = 0;
		for (const Song& s : songList) {
			if (s.albumArtist() == song.albumArtist() && s.album == song.album) {
				QModelIndex idx = index(row, 0, QModelIndex());
				emit dataChanged(idx, idx);
			}
			row++;
		}
	}
}

void MpdSearchModel::ratingResult(const QString& file, quint8 r)
{
	QList<Song>::iterator it = songList.begin();
	QList<Song>::iterator end = songList.end();
	int numCols = columnCount(QModelIndex()) - 1;

	for (int row = 0; it != end; ++it, ++row) {
		if (Song::Standard == (*it).type && r != (*it).rating && (*it).file == file) {
			(*it).rating = r;
			emit dataChanged(index(row, 0), index(row, numCols));
		}
	}
}

#include "moc_mpdsearchmodel.cpp"