Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 36 additions & 43 deletions src/Media/Album.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,57 @@ bool Album::operator==(const Media &other) const {
return false;
}

std::vector<std::shared_ptr<Media>> Album::filter(const std::vector<std::shared_ptr<Album>>& input) const {
std::vector<std::shared_ptr<Media>> result;
std::unique_ptr<Media> Album::clone() const {
return std::make_unique<Album>(*this);
}

// Riutilizzo filtro base di Media
std::vector<std::shared_ptr<Media>> baseInput(input.begin(), input.end());
std::vector<std::shared_ptr<Media>> filteredBase = Media::filter(baseInput);

// Filtro specifico per Album
for (const auto& mediaPtr : filteredBase) {
auto albumPtr = std::dynamic_pointer_cast<Album>(mediaPtr);
if (!albumPtr) continue;
bool Album::filter(const Media& album) const {
// Riutilizzo filtro base di Media
if (!Media::filter(album))
return false;

bool match = true;
// Cast to Album to access Album-specific members
const Album* albumPtr = dynamic_cast<const Album*>(&album);
if (!albumPtr)
return false;

// Band
if (!band_.empty() && !stringContainsIgnoreCase(albumPtr->getBand(), band_))
match = false;
// Band
if (!band_.empty() && !stringContainsIgnoreCase(albumPtr->getBand(), band_))
return false;

// Band members (ogni membro richiesto deve matchare almeno uno esistente)
if (!band_members_.empty()) {
for (const auto& memberFilter : band_members_) {
bool found = false;
for (const auto& m : albumPtr->getBandMembers()) {
if (stringContainsIgnoreCase(m, memberFilter)) {
found = true;
break;
}
}
if (!found) {
match = false;
// Band members (ogni membro richiesto deve matchare almeno uno esistente)
if (!band_members_.empty()) {
for (const auto& memberFilter : band_members_) {
bool found = false;
for (const auto& m : albumPtr->getBandMembers()) {
if (stringContainsIgnoreCase(m, memberFilter)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
}

// Songs (ogni canzone richiesta deve matchare almeno una esistente)
if (!songs_.empty()) {
for (const auto& songFilter : songs_) {
bool found = false;
for (const auto& s : albumPtr->getSongs()) {
if (stringContainsIgnoreCase(s, songFilter)) {
found = true;
break;
}
}
if (!found) {
match = false;
// Songs (ogni canzone richiesta deve matchare almeno una esistente)
if (!songs_.empty()) {
for (const auto& songFilter : songs_) {
bool found = false;
for (const auto& s : albumPtr->getSongs()) {
if (stringContainsIgnoreCase(s, songFilter)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}

if (match)
result.push_back(albumPtr);
}

return result;
return true;
}


} // namespace media
5 changes: 4 additions & 1 deletion src/Media/Album.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class Album : public Media {
const std::vector<std::string> &getSongs() const;
bool operator==(const Media &other) const override;

std::vector<std::shared_ptr<Media>> filter(const std::vector<std::shared_ptr<Album>>& input) const;
std::unique_ptr<Media> clone() const override;

bool filter(const Media& album) const override;

};
} // namespace media

#endif
38 changes: 15 additions & 23 deletions src/Media/AudioBook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ bool AudioBook::operator==(const Media& other) const {
return false;
}

std::unique_ptr<Media> AudioBook::clone() const {
return std::make_unique<AudioBook>(*this);
}

std::string AudioBook::getNarrator() const {
return narrator_;
}
Expand All @@ -37,33 +41,21 @@ void AudioBook::setStreamingService(const std::string& service) {
streamingService_ = service;
}

std::vector<std::shared_ptr<Media>> AudioBook::filter(const std::vector<std::shared_ptr<AudioBook>>& input) const {
std::vector<std::shared_ptr<Media>> result;

// Riutilizza filtro base di Novel (che include filtro di Media)
std::vector<std::shared_ptr<Novel>> novels(input.begin(), input.end());
std::vector<std::shared_ptr<Media>> filteredNovels = Novel::filter(novels);
bool AudioBook::filter(const Media& input) const {
if (!Novel::filter(input))
return false;
const AudioBook* audiobookPtr = dynamic_cast<const AudioBook*>(&input);
if (!audiobookPtr)
return false; // Protegge da cast fallito

// Filtro specifico AudioBook
for (const auto& novelPtr : filteredNovels) {
auto audiobookPtr = std::dynamic_pointer_cast<AudioBook>(novelPtr);
if (!audiobookPtr) continue;

bool match = true;
if (!narrator_.empty() && !stringContainsIgnoreCase(audiobookPtr->getNarrator(), narrator_))
return false;

// File size
if (narrator_ != "" && audiobookPtr->getNarrator()!= narrator_)
match = false;

// DRM
if (streamingService_ != "" && audiobookPtr->getStreamingService() != streamingService_)
match = false;

if (match)
result.push_back(audiobookPtr);
}
if (!streamingService_.empty() && !stringContainsIgnoreCase(audiobookPtr->getStreamingService(), streamingService_))
return false;

return result;
return true;
}

}
4 changes: 3 additions & 1 deletion src/Media/AudioBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class AudioBook : public Novel {
void setNarrator(const std::string& narrator);
void setStreamingService(const std::string& service);

std::vector<std::shared_ptr<Media>> filter(const std::vector<std::shared_ptr<AudioBook>>& input) const;
std::unique_ptr<Media> clone() const override;

bool filter(const Media& audiobook) const override;
};

}
Expand Down
42 changes: 18 additions & 24 deletions src/Media/Ebook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,28 @@ void Ebook::setDrm(bool drm) {
drm_ = drm;
}

std::vector<std::shared_ptr<Media>> Ebook::filter(const std::vector<std::shared_ptr<Ebook>>& input) const {
std::vector<std::shared_ptr<Media>> result;

// Riutilizza filtro base di Novel (che include filtro di Media)
std::vector<std::shared_ptr<Novel>> novels(input.begin(), input.end());
std::vector<std::shared_ptr<Media>> filteredNovels = Novel::filter(novels);

// Filtro specifico Ebook
for (const auto& novelPtr : filteredNovels) {
auto ebookPtr = std::dynamic_pointer_cast<Ebook>(novelPtr);
if (!ebookPtr) continue;

bool match = true;
std::unique_ptr<Media> Ebook::clone() const {
return std::make_unique<Ebook>(*this);
}

// File size
if (fileSizeBytes_ > 0 && ebookPtr->getFileSizeBytes() != fileSizeBytes_)
match = false;
bool Ebook::filter(const Media& input) const {
// Riutilizzo filtro base di Novel
if (!Novel::filter(input))
return false;
// Cast to Ebook to access Ebook-specific members
const Ebook* ebookPtr = dynamic_cast<const Ebook*>(&input);
if (!ebookPtr)
return false;

// DRM
if (drm_ && ebookPtr->hasDrm() != drm_)
match = false;
// File size filter
if (fileSizeBytes_ > 0 && ebookPtr->getFileSizeBytes() != fileSizeBytes_)
return false;

if (match)
result.push_back(ebookPtr);
}
// DRM filter
if (drm_ && ebookPtr->hasDrm() != drm_)
return false;

return result;
return true;
}


}
4 changes: 3 additions & 1 deletion src/Media/Ebook.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class Ebook : public Novel {
void setFileSizeBytes(unsigned int size);
void setDrm(bool drm);

std::vector<std::shared_ptr<Media>> filter(const std::vector<std::shared_ptr<Ebook>>& input) const;
std::unique_ptr<Media> clone() const override;

bool filter(const Media& ebook) const override;

};

Expand Down
1 change: 1 addition & 0 deletions src/Media/IMedia.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace media {
class IMedia {
virtual void accept(IConstMediaVisitor &) const = 0;
virtual bool open() = 0;
virtual std::unique_ptr<Media> clone() const = 0;
};

}
Expand Down
78 changes: 35 additions & 43 deletions src/Media/Media.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Media::Media(const std::string &title, int release, const std::string &language,

void Media::accept(IConstMediaVisitor &v) const {}

std::unique_ptr<media::Media> media::Media::clone() const {
return std::make_unique<media::Media>(*this);
}


bool Media::operator==(const Media &other) const {
return title_ == other.title_ && release_ == other.release_ &&
language_ == other.language_ && favourite_ == other.favourite_ &&
Expand All @@ -31,55 +36,42 @@ bool Media::open() {
return false;
}

std::vector<std::shared_ptr<Media>> Media::filter(const std::vector<std::shared_ptr<Media>>& input) const {
std::vector<std::shared_ptr<Media>> result;

for (const auto& mediaPtr : input) {
if (!mediaPtr) continue;

const Media& media = *mediaPtr;
bool match = true;

bool Media::filter(const Media& media) const {
// Title (substring, case-insensitive)
if (!getTitle().empty() && !stringContainsIgnoreCase(media.getTitle(), getTitle()))
match = false;

// Release (confronto stretto)
if (getRelease() != std::numeric_limits<int>::min() &&
media.getRelease() != getRelease())
match = false;

// Language (substring, case-insensitive)
if (!getLanguage().empty() && media.getLanguage() != getLanguage())
match = false;

// Favourite (confronto booleano)
if (isFavourite() && media.isFavourite() != isFavourite())
match = false;

// Generi (match parziale case-insensitive su ogni genere richiesto)
if (!getGenres().empty()) {
const auto& mediaGenres = media.getGenres();
for (const auto& genreFilter : getGenres()) {
bool found = false;
for (const auto& g : mediaGenres) {
if (stringContainsIgnoreCase(g, genreFilter)) {
found = true;
break;
}
}
if (!found) {
match = false;
if (!getTitle().empty() && !stringContainsIgnoreCase(media.getTitle(), getTitle()))
return false;

// Release (confronto stretto)
if (getRelease() != std::numeric_limits<int>::min() &&
media.getRelease() != getRelease())
return false;

// Language (substring, case-insensitive)
if (!getLanguage().empty() && media.getLanguage() != getLanguage())
return false;

// Favourite (confronto booleano)
if (isFavourite() && media.isFavourite() != isFavourite())
return false;

// Generi (match parziale case-insensitive su ogni genere richiesto)
if (!getGenres().empty()) {
const auto& mediaGenres = media.getGenres();
for (const auto& genreFilter : getGenres()) {
bool found = false;
for (const auto& g : mediaGenres) {
if (stringContainsIgnoreCase(g, genreFilter)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
}

if (match)
result.push_back(mediaPtr);
}

return result;
return true;
}


Expand Down
5 changes: 4 additions & 1 deletion src/Media/Media.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ class Media : IMedia{
const std::string &getImgPath() const;
const std::string &getNotes() const;

std::vector<std::shared_ptr<Media>> filter(const std::vector<std::shared_ptr<Media>> &media) const;
virtual std::unique_ptr<Media> clone() const;


virtual bool filter(const Media &media) const;
};
} // namespace media
#endif
Loading
Loading