Added feature to delete a song. Now working on downloading the song in the background
This commit is contained in:
@@ -175,6 +175,24 @@ int retrieveShuffleMode(const std::string& path) {
|
||||
}
|
||||
|
||||
|
||||
template<class Song = model::Song, typename Str = std::string, typename B = bool>
|
||||
B deleteSong(Song& song, const Str& path) {
|
||||
manager::DirectoryManager dirMgr;
|
||||
if (!dirMgr.doesSongExist(song, path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto result = dirMgr.deleteSong(song, path);
|
||||
if (!result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
repository::local::SongRepository songRepo(path);
|
||||
songRepo.deleteSongFromTable(song, path);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool doesDatabaseExist(const std::string& dataPath) {
|
||||
repository::local::UserRepository userRepo;
|
||||
const auto result = userRepo.databaseExist(dataPath);
|
||||
@@ -286,6 +304,7 @@ void downloadSong(Song& song, const Token& token, const Str& path) {
|
||||
saveSong.close();
|
||||
|
||||
song.path = downloadedSong.path;
|
||||
song.downloaded = true;
|
||||
repository::local::SongRepository localSongRepo(path);
|
||||
localSongRepo.saveSong(downloadedSong, path);
|
||||
}
|
||||
@@ -703,6 +722,24 @@ Java_com_example_mear_repositories_TokenRepository_isTokenTableEmpty(
|
||||
return doesTokenExist(dataPath);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jboolean
|
||||
JNICALL
|
||||
Java_com_example_mear_repositories_TrackRepository_deleteSong(
|
||||
JNIEnv *env,
|
||||
jobject thisObj,
|
||||
jobject songObj,
|
||||
jstring pathStr
|
||||
) {
|
||||
auto songClass = env->GetObjectClass(songObj);
|
||||
auto path = env->GetStringUTFChars(pathStr, nullptr);
|
||||
auto song = ObjToSong(env, songObj);
|
||||
|
||||
const auto result = deleteSong(song, path);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jboolean
|
||||
JNICALL
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace manager {
|
||||
public:
|
||||
template<typename Str = std::string>
|
||||
std::string fullSongPath(const Song& song, const Str& path) {
|
||||
std::string s = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
|
||||
std::string s = rootMusicDirectory(path);
|
||||
s.append(song.albumArtist);
|
||||
s.append("/");
|
||||
s.append(song.album);
|
||||
@@ -51,7 +51,7 @@ namespace manager {
|
||||
|
||||
template<typename Str = std::string>
|
||||
Str albumPath(const Song& song, const Str& path) {
|
||||
std::string s = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
|
||||
auto s = rootMusicDirectory<std::string>(path);
|
||||
s.append(song.albumArtist);
|
||||
s.append("/");
|
||||
s.append(song.album);
|
||||
@@ -76,7 +76,7 @@ namespace manager {
|
||||
|
||||
template<typename Str = std::string>
|
||||
Str artistPath(const Song& song, const Str& path) {
|
||||
Str s = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
|
||||
Str s = rootMusicDirectory(path);
|
||||
s.append(song.albumArtist);
|
||||
|
||||
return s.c_str();
|
||||
@@ -101,6 +101,10 @@ namespace manager {
|
||||
|
||||
template<typename Str = std::string>
|
||||
void createSongDirectory(const Song& song, const Str& path) {
|
||||
if (!rootMusicDirectoryExists<std::string>(path)) {
|
||||
const std::string p(rootMusicDirectory<std::string>(path));
|
||||
auto status = mkdir(p.c_str(), 0777);
|
||||
}
|
||||
if (!artistDirectoryExists(song, path)) {
|
||||
auto status = mkdir(artistPath<std::string>(song, path).c_str(), 0777);
|
||||
}
|
||||
@@ -112,14 +116,20 @@ namespace manager {
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Str = std::string>
|
||||
void deleteSongDirectory(const Song& song, const Str& path) {
|
||||
|
||||
}
|
||||
private:
|
||||
template<typename Str = std::string>
|
||||
static auto rootMusicDirectory(const Str& path) {
|
||||
auto rootPath = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
|
||||
rootPath.append("music/");
|
||||
|
||||
return rootPath;
|
||||
}
|
||||
|
||||
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
B albumDirectoryExists(const Song song, const Str& path) {
|
||||
auto albumPath = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
|
||||
// auto albumPath = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
|
||||
auto albumPath = rootMusicDirectory<std::string>(path);
|
||||
albumPath.append(song.albumArtist);
|
||||
albumPath.append("/");
|
||||
albumPath.append(song.album);
|
||||
@@ -159,8 +169,9 @@ namespace manager {
|
||||
}
|
||||
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
B artistDirectoryExists(const Song song, const Str& path) {
|
||||
auto artistPath = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
|
||||
B artistDirectoryExists(const Song& song, const Str& path) {
|
||||
// auto artistPath = utility::GeneralUtility::appendForwardSlashToUri<std::string>(path);
|
||||
auto artistPath = rootMusicDirectory<std::string>(path);
|
||||
artistPath.append(song.albumArtist);
|
||||
DIR* dir = opendir(artistPath.c_str());
|
||||
|
||||
@@ -173,6 +184,21 @@ namespace manager {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
B rootMusicDirectoryExists(const Str& path) {
|
||||
auto rootPath = rootMusicDirectory<std::string>(path);
|
||||
DIR *dir = opendir(rootPath.c_str());
|
||||
|
||||
if (dir) {
|
||||
closedir(dir);
|
||||
return true;
|
||||
} else if (ENOENT == errno) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user