DatabaseSong: new library merging duplicate code

This commit is contained in:
Max Kellermann 2014-01-15 18:25:58 +01:00
parent e2812f722d
commit 32ec672311
6 changed files with 91 additions and 35 deletions

View File

@ -107,6 +107,7 @@ src_mpd_SOURCES = \
src/DirectorySave.cxx src/DirectorySave.hxx \
src/DatabaseSimple.hxx \
src/DatabaseGlue.cxx src/DatabaseGlue.hxx \
src/DatabaseSong.cxx src/DatabaseSong.hxx \
src/DatabasePrint.cxx src/DatabasePrint.hxx \
src/DatabaseQueue.cxx src/DatabaseQueue.hxx \
src/DatabasePlaylist.cxx src/DatabasePlaylist.hxx \

40
src/DatabaseSong.cxx Normal file
View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "DatabaseSong.hxx"
#include "DatabaseGlue.hxx"
#include "DatabasePlugin.hxx"
#include "DetachedSong.hxx"
DetachedSong *
DatabaseDetachSong(const char *uri, Error &error)
{
const Database *db = GetDatabase(error);
if (db == nullptr)
return nullptr;
Song *tmp = db->GetSong(uri, error);
if (tmp == nullptr)
return nullptr;
DetachedSong *song = new DetachedSong(*tmp);
db->ReturnSong(tmp);
return song;
}

38
src/DatabaseSong.hxx Normal file
View File

@ -0,0 +1,38 @@
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_DATABASE_SONG_HXX
#define MPD_DATABASE_SONG_HXX
#include "Compiler.h"
class DetachedSong;
class Error;
/**
* Look up a song in the database and convert it to a #DetachedSong
* instance. The caller is responsible for freeing it.
*
* @return nullptr on error
*/
gcc_malloc gcc_nonnull_all
DetachedSong *
DatabaseDetachSong(const char *uri, Error &error);
#endif

View File

@ -32,8 +32,7 @@
#include "Song.hxx"
#include "DetachedSong.hxx"
#include "Idle.hxx"
#include "DatabaseGlue.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseSong.hxx"
#include "Log.hxx"
#include <stdlib.h>
@ -113,16 +112,9 @@ playlist::AppendURI(PlayerControl &pc,
if (uri_has_scheme(uri)) {
song = new DetachedSong(uri);
} else {
const Database *db = GetDatabase();
if (db == nullptr)
song = DatabaseDetachSong(uri, IgnoreError());
if (song == nullptr)
return PlaylistResult::NO_SUCH_SONG;
Song *tmp = db->GetSong(uri, IgnoreError());
if (tmp == nullptr)
return PlaylistResult::NO_SUCH_SONG;
song = new DetachedSong(*tmp);
db->ReturnSong(tmp);
}
PlaylistResult result = AppendSong(pc, std::move(*song), added_id);

View File

@ -22,8 +22,7 @@
#include "PlaylistSave.hxx"
#include "PlaylistInfo.hxx"
#include "PlaylistVector.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
#include "DatabaseSong.hxx"
#include "DetachedSong.hxx"
#include "Mapper.hxx"
#include "fs/TextFile.hxx"
@ -405,18 +404,13 @@ spl_append_uri(const char *url, const char *utf8file, Error &error)
return spl_append_song(utf8file, DetachedSong(url),
error);
} else {
const Database *db = GetDatabase(error);
if (db == nullptr)
DetachedSong *song = DatabaseDetachSong(url, error);
if (song == nullptr)
return false;
Song *tmp = db->GetSong(url, error);
if (tmp == nullptr)
return false;
const DetachedSong song(*tmp);
db->ReturnSong(tmp);
return spl_append_song(utf8file, song, error);
bool success = spl_append_song(utf8file, *song, error);
delete song;
return success;
}
}

View File

@ -20,8 +20,7 @@
#include "config.h"
#include "PlaylistSong.hxx"
#include "Mapper.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
#include "DatabaseSong.hxx"
#include "ls.hxx"
#include "tag/Tag.hxx"
#include "tag/TagBuilder.hxx"
@ -106,17 +105,9 @@ playlist_check_load_song(const DetachedSong *song, const char *uri, bool secure)
return nullptr;
}
} else {
const Database *db = GetDatabase();
if (db == nullptr)
dest = DatabaseDetachSong(uri, IgnoreError());
if (dest == nullptr)
return nullptr;
Song *tmp = db->GetSong(uri, IgnoreError());
if (tmp == nullptr)
/* not found in database */
return nullptr;
dest = new DetachedSong(*tmp);
db->ReturnSong(tmp);
}
return apply_song_metadata(dest, song);