File: syncdialog.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 (281 lines) | stat: -rw-r--r-- 7,855 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Cantata
 *
 * Copyright (c) 2011-2014 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 "syncdialog.h"
#include "actiondialog.h"
#include "devicepropertiesdialog.h"
#include "devicepropertieswidget.h"
#include "models/devicesmodel.h"
#include "models/mpdlibrarymodel.h"
#include "mpd-interface/mpdconnection.h"
#include "mpd-interface/song.h"
#include "support/messagebox.h"
#include "support/squeezedtextlabel.h"
#include "synccollectionwidget.h"
#include "widgets/icons.h"
#include <QFileInfo>
#include <QSplitter>

struct SyncSong : public Song {
	SyncSong(const Song& o)
		: Song(o)
	{
	}
	bool operator==(const SyncSong& o) const
	{
		return title == o.title && album == o.album && albumArtist() == o.albumArtist();
	}
	bool operator<(const SyncSong& o) const
	{
		int compare = albumArtist().compare(o.albumArtist());

		if (0 != compare) {
			return compare < 0;
		}
		compare = album.compare(o.album);
		if (0 != compare) {
			return compare < 0;
		}
		return title.compare(o.title) < 0;
	}
};

inline uint qHash(const SyncSong& key)
{
	return qHash(key.albumArtist() + key.artist + key.title);
}

static void getDiffs(const QSet<Song>& s1, const QSet<Song>& s2, QSet<Song>& in1, QSet<Song>& in2)
{
	QSet<SyncSong> a;
	QSet<SyncSong> b;

	for (const Song& s : s1) {
		a.insert(s);
	}

	for (const Song& s : s2) {
		b.insert(s);
	}

	QSet<SyncSong> r = a - b;

	for (const Song& s : r) {
		in1.insert(s);
	}

	r = b - a;

	for (const Song& s : r) {
		in2.insert(s);
	}
}

static int iCount = 0;

int SyncDialog::instanceCount()
{
	return iCount;
}

SyncDialog::SyncDialog(QWidget* parent)
	: Dialog(parent, "SyncDialog", QSize(680, 680)), currentDev(nullptr)
{
	iCount++;

	QWidget* mw = new QWidget(this);
	QVBoxLayout* l = new QVBoxLayout(mw);
	QSplitter* splitter = new QSplitter(mw);
	libWidget = new SyncCollectionWidget(splitter, tr("Library:"));
	devWidget = new SyncCollectionWidget(splitter, tr("Device:"));
	statusLabel = new SqueezedTextLabel(this);
	statusLabel->setText(tr("Loading all songs from library, please wait..."));
	splitter->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
	NoteLabel* noteLabel = new NoteLabel(this);
	noteLabel->setText(tr("<code>Library</code> lists only songs that are in your library, but not on the device. Likewise <code>Device</code> lists "
	                      "songs that are only on the device.<br/>"
	                      "Select songs from <code>Library</code> that you would like to copy to <code>Device</code>, "
	                      "and select songs from <code>Device</code> that you would like to copy to <code>Library</code>. "
	                      "Then press the <code>Synchronize</code> button."));
	l->addWidget(splitter);
	l->addWidget(statusLabel);
	l->addWidget(noteLabel);
	libWidget->setEnabled(false);
	devWidget->setEnabled(false);
	setMainWidget(mw);
	setButtons(Cancel | Ok);
	setButtonText(Ok, tr("Synchronize"));
	enableButtonOk(false);
	setAttribute(Qt::WA_DeleteOnClose);
	setCaption(tr("Synchronize"));
	connect(libWidget, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
	connect(devWidget, SIGNAL(selectionChanged()), SLOT(selectionChanged()));
	connect(libWidget, SIGNAL(configure()), SLOT(configure()));
	connect(devWidget, SIGNAL(configure()), SLOT(configure()));
	libOptions.save(MPDConnectionDetails::configGroupName(MPDConnection::self()->getDetails().name), true);
}

SyncDialog::~SyncDialog()
{
	iCount--;
}

void SyncDialog::sync(const QString& udi)
{
	devUdi = udi;
	connect(MpdLibraryModel::self(), SIGNAL(songListing(QList<Song>, double)), this, SLOT(librarySongs(QList<Song>, double)));
	MpdLibraryModel::self()->listSongs();
	show();
}

void SyncDialog::copy(const QList<Song>& songs)
{
	Device* dev = getDevice();

	if (!dev) {
		return;
	}

	bool fromDev = sender() == devWidget;
	ActionDialog* dlg = new ActionDialog(this);
	connect(dlg, SIGNAL(completed()), SLOT(updateSongs()));
	dlg->copy(fromDev ? dev->id() : QString(), fromDev ? QString() : dev->id(), songs);
}

void SyncDialog::updateSongs()
{
	Device* dev = getDevice();

	if (!dev) {
		deleteLater();
		hide();
		return;
	}

	QSet<Song> devSongs = dev->allSongs(dev->options().fixVariousArtists);
	QSet<Song> inDev;
	QSet<Song> inLib;
	getDiffs(devSongs, libSongs, inDev, inLib);

	if (0 == inDev.count() && 0 == inLib.count()) {
		MessageBox::information(isVisible() ? this : parentWidget(), tr("Device and library are in sync."));
		deleteLater();
		hide();
		return;
	}
	devWidget->setSupportsAlbumArtistTag(dev->supportsAlbumArtistTag());
	devWidget->update(inDev);
	libWidget->update(inLib);
	libWidget->setEnabled(true);
	devWidget->setEnabled(true);
	libSongs.clear();
}

void SyncDialog::librarySongs(const QList<Song>& songs, double pc)
{
	if (songs.isEmpty()) {
		statusLabel->hide();
		disconnect(MpdLibraryModel::self(), SIGNAL(songListing(QList<Song>, double)), this, SLOT(librarySongs(QList<Song>, double)));
		updateSongs();
	}
	else {
		libSongs += Utils::listToSet(songs);
		statusLabel->setText(tr("Loading all songs from library, please wait...%1%...").arg(pc));
	}
}

void SyncDialog::selectionChanged()
{
	enableButtonOk(libWidget->numCheckedSongs() || devWidget->numCheckedSongs());
}

void SyncDialog::configure()
{
	if (libWidget == sender()) {
		DevicePropertiesDialog* dlg = new DevicePropertiesDialog(this);
		connect(dlg, SIGNAL(updatedSettings(const QString&, const DeviceOptions&)), SLOT(saveProperties(const QString&, const DeviceOptions&)));
		dlg->setCaption(tr("Local Music Library Properties"));
		dlg->show(MPDConnection::self()->getDetails().dir, libOptions, DevicePropertiesWidget::Prop_Basic | DevicePropertiesWidget::Prop_FileName);
	}
	else {
		Device* dev = getDevice();
		if (dev) {
			dev->configure(this);
		}
	}
}

void SyncDialog::saveProperties(const QString& path, const DeviceOptions& opts)
{
	Q_UNUSED(path)
	libOptions = opts;
	libOptions.save(MPDConnectionDetails::configGroupName(MPDConnection::self()->getDetails().name), true, false);
}

void SyncDialog::slotButtonClicked(int button)
{
	switch (button) {
	case Ok: {
		Device* dev = getDevice();
		if (dev) {
			setVisible(false);
			ActionDialog* dlg = new ActionDialog(parentWidget());
			QList<Song> songs = libWidget->checkedSongs();
			QString devId;
			devId = dev->id();
			dlg->sync(devId, libWidget->checkedSongs(), devWidget->checkedSongs());
			Dialog::slotButtonClicked(button);
		}
		break;
	}
	case Cancel:
		MpdLibraryModel::self()->cancelListing();
		Dialog::slotButtonClicked(button);
		break;
	default:
		break;
	}
}

Device* SyncDialog::getDevice()
{
	Device* dev = DevicesModel::self()->device(devUdi);
	if (!dev) {
		MessageBox::error(isVisible() ? this : parentWidget(), tr("Device has been removed!"));
		return nullptr;
	}

	if (currentDev && dev != currentDev) {
		MessageBox::error(isVisible() ? this : parentWidget(), tr("Device has been changed?"));
		return nullptr;
	}

	if (dev->isIdle()) {
		return dev;
	}

	MessageBox::error(isVisible() ? this : parentWidget(), tr("Device is busy?"));
	return nullptr;
}

#include "moc_syncdialog.cpp"