library: Add callbacks for adding and removing tracks
Useful for keeping a library playqueue up to date. Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
parent
2535a2fe56
commit
25ee2e945c
|
@ -13,10 +13,12 @@ Callbacks: (lib/callback.cpp)
|
||||||
with no-op default values and filled in by the user through the
|
with no-op default values and filled in by the user through the
|
||||||
get_callbacks() function.
|
get_callbacks() function.
|
||||||
|
|
||||||
|
|
||||||
- Callback functions:
|
- Callback functions:
|
||||||
struct Callbacks {
|
struct Callbacks {
|
||||||
void (*on_library_add)(unsigned int, library :: Library *);
|
void (*on_library_add)(unsigned int, library :: Library *);
|
||||||
void (*on_library_update)(unsigned int, library :: Library *);
|
void (*on_library_update)(unsigned int, library :: Library *);
|
||||||
|
void (*on_library_track_add)();
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct Callbacks callbacks;
|
static struct Callbacks callbacks;
|
||||||
|
|
|
@ -10,6 +10,8 @@
|
||||||
struct Callbacks {
|
struct Callbacks {
|
||||||
void (*on_library_add)(unsigned int, library :: Library *);
|
void (*on_library_add)(unsigned int, library :: Library *);
|
||||||
void (*on_library_update)(unsigned int, library :: Library *);
|
void (*on_library_update)(unsigned int, library :: Library *);
|
||||||
|
void (*on_library_track_add)(unsigned int);
|
||||||
|
void (*on_library_track_del)(unsigned int);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,15 @@
|
||||||
#include <callback.h>
|
#include <callback.h>
|
||||||
|
|
||||||
|
|
||||||
|
static void no_op(unsigned int) {}
|
||||||
static void no_op(unsigned int id, library :: Library *path) {}
|
static void no_op(unsigned int id, library :: Library *path) {}
|
||||||
|
|
||||||
|
|
||||||
static struct Callbacks callbacks = {
|
static struct Callbacks callbacks = {
|
||||||
.on_library_add = no_op,
|
.on_library_add = no_op,
|
||||||
.on_library_update = no_op,
|
.on_library_update = no_op,
|
||||||
|
.on_library_track_add = no_op,
|
||||||
|
.on_library_track_del = no_op,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -279,7 +279,7 @@ static void read_tags(unsigned int lib_id, const std :: string &path)
|
||||||
TagLib :: Tag *tag;
|
TagLib :: Tag *tag;
|
||||||
TagLib :: AudioProperties *audio;
|
TagLib :: AudioProperties *audio;
|
||||||
TagLib :: FileRef ref(path.c_str(), true, TagLib :: AudioProperties :: Fast);
|
TagLib :: FileRef ref(path.c_str(), true, TagLib :: AudioProperties :: Fast);
|
||||||
unsigned int artist_id, album_id, genre_id;
|
unsigned int artist_id, album_id, genre_id, track_id;
|
||||||
|
|
||||||
if (ref.isNull()) {
|
if (ref.isNull()) {
|
||||||
print("ERROR: Could not read tags for file %s", path.c_str());
|
print("ERROR: Could not read tags for file %s", path.c_str());
|
||||||
|
@ -293,9 +293,11 @@ static void read_tags(unsigned int lib_id, const std :: string &path)
|
||||||
artist_id = artist_db.insert(library :: AGInfo(library :: DB_ARTIST, tag));
|
artist_id = artist_db.insert(library :: AGInfo(library :: DB_ARTIST, tag));
|
||||||
album_id = album_db.insert(library :: Album(tag, artist_id));
|
album_id = album_db.insert(library :: Album(tag, artist_id));
|
||||||
genre_id = genre_db.insert(library :: AGInfo(library :: DB_GENRE, tag));
|
genre_id = genre_db.insert(library :: AGInfo(library :: DB_GENRE, tag));
|
||||||
track_db.insert(library :: Track(tag, audio, lib_id, artist_id,
|
track_id = track_db.insert(library :: Track(tag, audio, lib_id,
|
||||||
album_id, genre_id, path));
|
artist_id, album_id, genre_id, path));
|
||||||
library_db[lib_id].size++;
|
library_db[lib_id].size++;
|
||||||
|
get_callbacks()->on_library_track_add(track_id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool process_path(unsigned int lib_id, const std :: string &dir,
|
static bool process_path(unsigned int lib_id, const std :: string &dir,
|
||||||
|
@ -387,7 +389,7 @@ static void do_import_track(File &f, unsigned int lib_id)
|
||||||
{
|
{
|
||||||
struct ImportData data;
|
struct ImportData data;
|
||||||
std::string artist, album, genre;
|
std::string artist, album, genre;
|
||||||
unsigned int artist_id, album_id, genre_id, year, tmp;
|
unsigned int artist_id, album_id, genre_id, track_id, year, tmp;
|
||||||
|
|
||||||
data.filepath = f.getline();
|
data.filepath = f.getline();
|
||||||
data.title = f.getline();
|
data.title = f.getline();
|
||||||
|
@ -405,8 +407,10 @@ static void do_import_track(File &f, unsigned int lib_id)
|
||||||
artist_id = artist_db.insert(library :: AGInfo(library :: DB_ARTIST, artist));
|
artist_id = artist_db.insert(library :: AGInfo(library :: DB_ARTIST, artist));
|
||||||
album_id = album_db.insert(library :: Album(album, year, artist_id));
|
album_id = album_db.insert(library :: Album(album, year, artist_id));
|
||||||
genre_id = genre_db.insert(library :: AGInfo(library :: DB_GENRE, genre));
|
genre_id = genre_db.insert(library :: AGInfo(library :: DB_GENRE, genre));
|
||||||
track_db.insert(library :: Track(&data, lib_id, artist_id, album_id, genre_id));
|
track_id = track_db.insert(library :: Track(&data, lib_id, artist_id,
|
||||||
|
album_id, genre_id));
|
||||||
library_db[lib_id].size++;
|
library_db[lib_id].size++;
|
||||||
|
get_callbacks()->on_library_track_add(track_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void do_import_library(std::string &s)
|
static void do_import_library(std::string &s)
|
||||||
|
@ -475,7 +479,18 @@ void library :: add_path(const std::string &dir)
|
||||||
|
|
||||||
void library :: del_path(unsigned int id)
|
void library :: del_path(unsigned int id)
|
||||||
{
|
{
|
||||||
|
unsigned int t;
|
||||||
|
|
||||||
|
for (t = track_db.first(); t < track_db.num_rows(); t = track_db.next(t)) {
|
||||||
|
if (track_db[t].library_id == id) {
|
||||||
|
get_callbacks()->on_library_track_del(t);
|
||||||
|
track_db.remove(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
library_db.remove(id);
|
library_db.remove(id);
|
||||||
|
|
||||||
|
track_db.save();
|
||||||
library_db.save();
|
library_db.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue