Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "added-since" filter expression #1893

Merged
merged 1 commit into from
Nov 25, 2023
Merged
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
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ver 0.24 (not yet released)
- apply Unicode normalization to case-insensitive filter expressions
- stickers on playlists and some tag types
- new command "stickernames"
- new "search"/"find" filter "added-since"
* database
- attribute "added" shows when each song was added to the database
- proxy: require MPD 0.21 or later
Expand Down
19 changes: 19 additions & 0 deletions src/song/AddedSinceSongFilter.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#include "AddedSinceSongFilter.hxx"
#include "LightSong.hxx"
#include "time/ISO8601.hxx"
#include "util/StringBuffer.hxx"

std::string
AddedSinceSongFilter::ToExpression() const noexcept
{
return std::string("(added-since \"") + FormatISO8601(value).c_str() + "\")";
}

bool
AddedSinceSongFilter::Match(const LightSong &song) const noexcept
{
return song.added >= value;
}
26 changes: 26 additions & 0 deletions src/song/AddedSinceSongFilter.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#ifndef MPD_ADDED_SINCE_SONG_FILTER_HXX
#define MPD_ADDED_SINCE_SONG_FILTER_HXX

#include "ISongFilter.hxx"

#include <chrono>

class AddedSinceSongFilter final : public ISongFilter {
std::chrono::system_clock::time_point value;

public:
explicit AddedSinceSongFilter(std::chrono::system_clock::time_point _value) noexcept
:value(_value) {}

ISongFilterPtr Clone() const noexcept override {
return std::make_unique<AddedSinceSongFilter>(*this);
}

std::string ToExpression() const noexcept override;
bool Match(const LightSong &song) const noexcept override;
};

#endif
15 changes: 15 additions & 0 deletions src/song/Filter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "BaseSongFilter.hxx"
#include "TagSongFilter.hxx"
#include "ModifiedSinceSongFilter.hxx"
#include "AddedSinceSongFilter.hxx"
#include "AudioFormatSongFilter.hxx"
#include "PrioritySongFilter.hxx"
#include "pcm/AudioParser.hxx"
Expand Down Expand Up @@ -40,6 +41,7 @@ enum {
LOCATE_TAG_PRIORITY,
LOCATE_TAG_FILE_TYPE,
LOCATE_TAG_ANY_TYPE,
LOCATE_TAG_ADDED_SINCE,
};

/**
Expand All @@ -62,6 +64,9 @@ locate_parse_type(const char *str) noexcept
if (strcmp(str, "modified-since") == 0)
return LOCATE_TAG_MODIFIED_SINCE;

if (strcmp(str, "added-since") == 0)
return LOCATE_TAG_ADDED_SINCE;

if (StringEqualsCaseASCII(str, "AudioFormat"))
return LOCATE_TAG_AUDIO_FORMAT;

Expand Down Expand Up @@ -322,6 +327,12 @@ SongFilter::ParseExpression(const char *&s, bool fold_case)
throw std::runtime_error("')' expected");
s = StripLeft(s + 1);
return std::make_unique<ModifiedSinceSongFilter>(ParseTimeStamp(value_s.c_str()));
} else if (type == LOCATE_TAG_ADDED_SINCE) {
const auto value_s = ExpectQuoted(s);
if (*s != ')')
throw std::runtime_error("')' expected");
s = StripLeft(s + 1);
return std::make_unique<AddedSinceSongFilter>(ParseTimeStamp(value_s.c_str()));
} else if (type == LOCATE_TAG_BASE_TYPE) {
auto value = ExpectQuoted(s);
if (*s != ')')
Expand Down Expand Up @@ -406,6 +417,10 @@ SongFilter::Parse(const char *tag_string, const char *value, bool fold_case)
case LOCATE_TAG_MODIFIED_SINCE:
and_filter.AddItem(std::make_unique<ModifiedSinceSongFilter>(ParseTimeStamp(value)));
break;

case LOCATE_TAG_ADDED_SINCE:
and_filter.AddItem(std::make_unique<AddedSinceSongFilter>(ParseTimeStamp(value)));
break;

case LOCATE_TAG_FILE_TYPE:
/* for compatibility with MPD 0.20 and older,
Expand Down
1 change: 1 addition & 0 deletions src/song/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ song = static_library(
'BaseSongFilter.cxx',
'TagSongFilter.cxx',
'ModifiedSinceSongFilter.cxx',
'AddedSinceSongFilter.cxx',
'PrioritySongFilter.cxx',
'AudioFormatSongFilter.cxx',
'AndSongFilter.cxx',
Expand Down
Loading