/** Export one playlist at a time. */
void PlaylistDialog::exportSelectedPlaylist()
{
	QString exportedPlaylistLocation;
	SettingsPrivate *settings = SettingsPrivate::instance();
	if (settings->value("locationForExportedPlaylist").isNull()) {
		exportedPlaylistLocation = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
	} else {
		exportedPlaylistLocation = settings->value("locationForExportedPlaylist").toString();
	}
	auto indexes = savedPlaylists->selectionModel()->selectedIndexes();
	if (indexes.isEmpty()) {
		return;
	}
	QModelIndex index = indexes.first();
	QStandardItem *item = _savedPlaylistModel->itemFromIndex(index);
	/// FIXME
	//QString pPath = item->data(PlaylistPath).toString();
	QString pName = item->data(Qt::DisplayRole).toString();

	// Open a file dialog and ask the user to choose a location
	QString newName = QFileDialog::getSaveFileName(this, tr("Export playlist"), exportedPlaylistLocation + QDir::separator() + pName, tr("Playlist (*.m3u8)"));
	if (QFile::exists(newName)) {
		QFile removePreviousOne(newName);
		if (!removePreviousOne.remove()) {
			qDebug() << Q_FUNC_INFO << "Cannot remove" << newName;
		}
	}
	if (newName.isEmpty()) {
		return;
	}
}
/** Export one playlist at a time. */
void PlaylistDialog::exportSelectedPlaylist()
{
	QString exportedPlaylistLocation;
	SettingsPrivate *settings = SettingsPrivate::instance();
	if (settings->value("locationForExportedPlaylist").isNull()) {
		exportedPlaylistLocation = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
	} else {
		exportedPlaylistLocation = settings->value("locationForExportedPlaylist").toString();
	}
	auto indexes = savedPlaylists->selectionModel()->selectedIndexes();
	if (indexes.isEmpty()) {
		return;
	}

	QStandardItem *item = _savedPlaylistModel->itemFromIndex(indexes.first());
	uint playlistId = item->data(PlaylistID).toUInt();
	auto db = SqlDatabase::instance();
	PlaylistDAO dao = db->selectPlaylist(playlistId);
	QString title = this->convertNameToValidFileName(dao.title());

	// Open a file dialog and ask the user to choose a location
	QString newName = QFileDialog::getSaveFileName(this, tr("Export playlist"), exportedPlaylistLocation + QDir::separator() + title, tr("Playlist (*.m3u8)"));
	if (QFile::exists(newName)) {
		QFile removePreviousOne(newName);
		if (!removePreviousOne.remove()) {
			qDebug() << Q_FUNC_INFO << "Cannot remove" << newName;
		}
	}
	if (newName.isEmpty()) {
		return;
	} else {
		QFile f(newName);
		if (f.open(QIODevice::ReadWrite | QIODevice::Text)) {
			QTextStream stream(&f);
			QList<TrackDAO> tracks = db->selectPlaylistTracks(playlistId);
			for (TrackDAO t : tracks) {
				stream << t.uri();
				endl(stream);
			}
		}
		f.close();
	}
}