Continuing work on download functionality
This commit is contained in:
+27
-19
@@ -58,6 +58,9 @@ jobject songToObj(JNIEnv *env, const model::Song& song)
|
||||
jmethodID songDuration = env->GetMethodID( songClass, "setDuration", "(I)V" );
|
||||
jmethodID songYear = env->GetMethodID( songClass, "setYear", "(I)V" );
|
||||
jmethodID songCoverArtId = env->GetMethodID(songClass, "setCoverArtId", "(I)V");
|
||||
jmethodID songPathId = env->GetMethodID(songClass, "setPath", "(Ljava/lang/String;)V");
|
||||
jmethodID songFilenameId = env->GetMethodID(songClass, "setFilename", "(Ljava/lang/String;)V");
|
||||
jmethodID songDownloadedId = env->GetMethodID(songClass, "setDownloaded", "(B)V");
|
||||
|
||||
jint id = song.id;
|
||||
jstring title = env->NewStringUTF(song.title.c_str());
|
||||
@@ -68,6 +71,10 @@ jobject songToObj(JNIEnv *env, const model::Song& song)
|
||||
jint duration = song.duration;
|
||||
jint year = song.year;
|
||||
jint coverArtId = song.coverArtId;
|
||||
jstring songPath = env->NewStringUTF(song.path.c_str());
|
||||
jstring songFilename = env->NewStringUTF(song.filename.c_str());
|
||||
jboolean songDownloaded = song.downloaded;
|
||||
|
||||
|
||||
env->CallVoidMethod( songObj, songId, id );
|
||||
env->CallVoidMethod(songObj, songTitle, title);
|
||||
@@ -78,12 +85,17 @@ jobject songToObj(JNIEnv *env, const model::Song& song)
|
||||
env->CallVoidMethod(songObj, songDuration, duration);
|
||||
env->CallVoidMethod(songObj, songYear, year);
|
||||
env->CallVoidMethod(songObj, songCoverArtId, coverArtId);
|
||||
env->CallVoidMethod(songObj, songPathId, songPath);
|
||||
env->CallVoidMethod(songObj, songFilenameId, songFilename);
|
||||
env->CallVoidMethod(songObj, songDownloadedId, songDownloaded);
|
||||
|
||||
env->DeleteLocalRef(title);
|
||||
env->DeleteLocalRef(album);
|
||||
env->DeleteLocalRef(albumArtist);
|
||||
env->DeleteLocalRef(artist);
|
||||
env->DeleteLocalRef(genre);
|
||||
env->DeleteLocalRef(songPath);
|
||||
env->DeleteLocalRef(songFilename);
|
||||
|
||||
return songObj;
|
||||
}
|
||||
@@ -404,24 +416,6 @@ Java_com_example_mear_repositories_TrackRepository_retrieveSong(
|
||||
return fetchedSongObj;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jobject
|
||||
JNICALL
|
||||
Java_com_example_mear_repositories_TrackRepositories_downloadSong(
|
||||
JNIEnv *env,
|
||||
jobject thisObj,
|
||||
jobject tokenObj,
|
||||
jobject songObj,
|
||||
jstring uriStr
|
||||
) {
|
||||
// TODO: left off here
|
||||
model::Song downloadedSong;
|
||||
auto downloadedSongObj = songToObj(env, downloadedSong);
|
||||
|
||||
return downloadedSongObj;
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT jobject
|
||||
JNICALL
|
||||
@@ -648,7 +642,6 @@ Java_com_example_mear_repositories_APIRepository_saveAPIInfoRecord(
|
||||
saveAPIInfo(apiInfo, path);
|
||||
}
|
||||
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT void
|
||||
JNICALL
|
||||
@@ -669,6 +662,21 @@ Java_com_example_mear_repositories_TokenRepository_saveTokenRecord(
|
||||
saveToken(token, path);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT void
|
||||
JNICALL
|
||||
Java_com_example_mear_repositories_TrackRepositories_downloadSong(
|
||||
JNIEnv *env,
|
||||
jobject thisObj,
|
||||
jobject tokenObj,
|
||||
jobject songObj,
|
||||
jstring uriStr
|
||||
) {
|
||||
// TODO: left off here
|
||||
auto song = ObjToSong<jobject, JNIEnv>(env, songObj);
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
JNIEXPORT void
|
||||
JNICALL
|
||||
|
||||
@@ -24,6 +24,32 @@ std::string jstringToString(JNIEnv*, jstring&);
|
||||
|
||||
model::APIInfo retrieveAPIInfo(const std::string&);
|
||||
|
||||
template<typename SongObj, typename JE, class Song = model::Song>
|
||||
Song ObjToSong(JE *env, SongObj obj) {
|
||||
jclass songClass = env->GetObjectClass(obj);
|
||||
//jmethodID jconstructor = env->GetMethodID( songClass, "<init>", "()V" );
|
||||
//jobject songObj = env->NewObject( songClass, jconstructor );
|
||||
|
||||
auto songId = env->GetFieldID( songClass, "id", "I" );
|
||||
auto songTitle = env->GetFieldID( songClass, "title", "Ljava/lang/String;" );
|
||||
auto songAlbum = env->GetFieldID( songClass, "album", "Ljava/lang/String;" );
|
||||
auto songAlbumArtist = env->GetFieldID(songClass, "albumArtist", "Ljava/lang/String;");
|
||||
auto songArtist = env->GetFieldID( songClass, "artist", "Ljava/lang/String;" );
|
||||
auto songGenre = env->GetFieldID( songClass, "genre", "Ljava/lang/String;" );
|
||||
auto songDuration = env->GetFieldID( songClass, "duration", "I" );
|
||||
auto songYear = env->GetFieldID( songClass, "year", "I" );
|
||||
auto songCoverArtId = env->GetFieldID(songClass, "coverArtId", "I");
|
||||
auto songPathId = env->GetFieldID(songClass, "path", "Ljava/lang/String;");
|
||||
auto songFilenameId = env->GetFieldID(songClass, "filename", "Ljava/lang/String;");
|
||||
auto songDownloadedId = env->GetFieldID(songClass, "downloaded", "B");
|
||||
|
||||
auto titleVal = (jstring)env->GetObjectField(obj, songTitle);
|
||||
|
||||
Song song;
|
||||
|
||||
return song;
|
||||
}
|
||||
|
||||
model::Song retrieveSong(const model::Token&, const model::Song&, const std::string&);
|
||||
|
||||
model::Token fetchToken(const model::User&, const std::string&);
|
||||
@@ -44,6 +70,10 @@ void saveCredentials(const model::User&, const std::string&);
|
||||
void saveToken(const model::Token&, const std::string&);
|
||||
void updateRepeatMode(const std::string&);
|
||||
void updateShuffleMode(const std::string&);
|
||||
template<class Song = model::Song>
|
||||
void downloadSong(Song& song) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -6,48 +6,122 @@
|
||||
#define MEAR_DIRECTORYMANAGER_H
|
||||
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../model/Song.h"
|
||||
#include "../utility/GeneralUtility.h"
|
||||
|
||||
namespace manager {
|
||||
template<class Song = model::Song>
|
||||
class DirectoryManager {
|
||||
public:
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
template<typename Str = std::string>
|
||||
Str fullSongPath(const Song& song, const Str& path) {
|
||||
auto s = "";
|
||||
auto s = utility::GeneralUtility::appendForwardSlashToUri(path);
|
||||
s.append(song.albumArtist);
|
||||
s.append("/");
|
||||
s.append(song.album);
|
||||
s.append("/");
|
||||
s.append(song.filename);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
template<typename Str = std::string>
|
||||
Str albumPath(const Song& song, const Str& path) {
|
||||
auto ss = "";
|
||||
auto s = utility::GeneralUtility::appendForwardSlashToUri(path);
|
||||
s.append(song.albumArtist);
|
||||
s.append("/");
|
||||
s.append(song.album);
|
||||
|
||||
return ss;
|
||||
return s;
|
||||
}
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
template<typename Str = std::string>
|
||||
Str artistPath(const Song& song, const Str& path) {
|
||||
return "art";
|
||||
auto s = utility::GeneralUtility::appendForwardSlashToUri(path);
|
||||
s.append(song.albumArtist);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
bool doesSongExist(const Song& song, const Str& path) {
|
||||
return false;
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
B doesSongExist(const Song& song, const Str& path) {
|
||||
auto s = albumPath(song, path);
|
||||
s.append("/");
|
||||
s.append(song.filename);
|
||||
|
||||
stat buffer;
|
||||
return (stat (s.c_str(), &buffer) == 0);
|
||||
}
|
||||
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
B deleteSong(const Song& song, const Str& path) {
|
||||
auto s = fullSongPath(song, path);
|
||||
auto result = std::remove(s.c_str());
|
||||
|
||||
if (result == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
template<typename Str = std::string>
|
||||
void createSongDirectory(const Song& song, const Str& path) {
|
||||
|
||||
if (!artistDirectoryExists(song, path)) {
|
||||
auto status = mkdir(artistPath(song, path).c_str(), 0777);
|
||||
}
|
||||
if (!albumDirectoryExists(song, path)) {
|
||||
auto status = mkdir(albumPath(song, path), 0777);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Str = std::string, class Song = model::Song>
|
||||
template<typename Str = std::string>
|
||||
void deleteSongDirectory(const Song& song, const Str& path) {
|
||||
|
||||
}
|
||||
private:
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
B albumDirectoryExists(const Song song, const Str& path) {
|
||||
auto albumPath = utility::GeneralUtility::appendForwardSlashToUri(path);
|
||||
albumPath.append(song.albumArtist);
|
||||
albumPath.append("/");
|
||||
albumPath.append(song.album);
|
||||
DIR* dir = opendir(albumPath.c_str());
|
||||
|
||||
if (dir) {
|
||||
closedir(dir);
|
||||
return true;
|
||||
} else if (ENOENT == errno) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename Str = std::string, typename B = bool>
|
||||
B artistDirectoryExists(const Song song, const Str& path) {
|
||||
auto artistPath = utility::GeneralUtility::appendForwardSlashToUri(path);
|
||||
artistPath.append(song.albumArtist);
|
||||
DIR* dir = opendir(artistPath.c_str());
|
||||
|
||||
if (dir) {
|
||||
closedir(dir);
|
||||
return true;
|
||||
} else if (ENOENT == errno) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -10,36 +10,35 @@
|
||||
|
||||
|
||||
namespace model {
|
||||
class Song {
|
||||
public:
|
||||
Song() = default;
|
||||
Song(const int id) : id(id) { }
|
||||
Song(const int id, const std::string& title, const std::string& artist,
|
||||
const std::string& album, const std::string& genre,
|
||||
const int duration, const int year) :
|
||||
id(id), title(title), artist(artist), album(album),
|
||||
genre(genre), duration(duration), year(year) { }
|
||||
Song(const int id, const std::string& title, const std::string& artist,
|
||||
const std::string& album, const std::string& albumArtist, const std::string& genre,
|
||||
const int duration, const int year, const int coverArtId) :
|
||||
id(id), title(title), artist(artist), album(album), albumArtist(albumArtist),
|
||||
genre(genre), duration(duration), year(year), coverArtId(coverArtId) { }
|
||||
|
||||
class Song
|
||||
{
|
||||
public:
|
||||
Song() = default;
|
||||
Song(const int id) : id(id) { }
|
||||
Song(const int id, const std::string& title, const std::string& artist,
|
||||
const std::string& album, const std::string& genre,
|
||||
const int duration, const int year) :
|
||||
id(id), title(title), artist(artist), album(album),
|
||||
genre(genre), duration(duration), year(year) { }
|
||||
Song(const int id, const std::string& title, const std::string& artist,
|
||||
const std::string& album, const std::string& albumArtist, const std::string& genre,
|
||||
const int duration, const int year, const int coverArtId) :
|
||||
id(id), title(title), artist(artist), album(album), albumArtist(albumArtist),
|
||||
genre(genre), duration(duration), year(year), coverArtId(coverArtId) { }
|
||||
|
||||
int id;
|
||||
std::string title;
|
||||
std::string artist;
|
||||
std::string album;
|
||||
std::string albumArtist;
|
||||
std::string genre;
|
||||
int duration;
|
||||
int year;
|
||||
int coverArtId;
|
||||
std::vector<char> data;
|
||||
bool downloaded;
|
||||
std::string path;
|
||||
};
|
||||
int id;
|
||||
std::string title;
|
||||
std::string artist;
|
||||
std::string album;
|
||||
std::string albumArtist;
|
||||
std::string genre;
|
||||
int duration;
|
||||
int year;
|
||||
int coverArtId;
|
||||
std::vector<char> data;
|
||||
bool downloaded;
|
||||
std::string path;
|
||||
std::string filename;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MEAR_SONG_H
|
||||
|
||||
@@ -140,6 +140,8 @@ namespace repository {
|
||||
auto res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
downloadedSong.data = std::move(std::vector<char>(data.begin(), data.end()));
|
||||
downloadedSong.filename = downloadedSong.title;
|
||||
downloadedSong.filename.append(".mp3");
|
||||
|
||||
return downloadedSong;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user