Skip to content

Commit

Permalink
cocoaui: fix autosort when adding folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Nov 2, 2024
1 parent dd4d711 commit 962fa50
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
6 changes: 3 additions & 3 deletions plugins/cocoaui/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ - (IBAction)addFoldersAction:(id)sender {
NSArray* files = openDlg.URLs;
ddb_playlist_t *plt = deadbeef->plt_alloc ("add-folders");
ddb_playlist_t *plt_curr = deadbeef->plt_get_curr ();
if (!deadbeef->plt_add_files_begin (plt, 0)) {
if (!deadbeef->plt_add_files_begin (plt_curr, 0)) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (NSUInteger i = 0; i < files.count; i++) {
NSString *fileName = [files[i] path];
Expand All @@ -527,13 +527,13 @@ - (IBAction)addFoldersAction:(id)sender {
}
deadbeef->pl_save_current();
ddb_undo->set_action_name ("Add Folders");
deadbeef->plt_add_files_end (plt, 0);
deadbeef->plt_add_files_end (plt_curr, 0);
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);
});
}
else {
deadbeef->plt_add_files_end (plt, 0);
deadbeef->plt_add_files_end (plt_curr, 0);
deadbeef->plt_unref (plt);
deadbeef->plt_unref (plt_curr);
}
Expand Down
3 changes: 0 additions & 3 deletions src/playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,6 @@ plt_search_process2 (playlist_t *plt, const char *text, int select_results);
void
plt_sort (playlist_t *plt, int iter, int id, const char *format, int order);

void
plt_sort_random (playlist_t *plt, int iter);

void
pl_items_copy_junk (struct playItem_s *from, struct playItem_s *first, struct playItem_s *last);

Expand Down
40 changes: 25 additions & 15 deletions src/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
#define trace(fmt,...)

static void
plt_sort_internal (playlist_t *playlist, int iter, int id, const char *format, int order, int version);
plt_sort_internal (playlist_t *playlist, int iter, int id, const char *format, int order, int version, int is_autosorting);

void
plt_sort_v2 (playlist_t *plt, int iter, int id, const char *format, int order) {
static void
plt_sort_v2_internal (playlist_t *plt, int iter, int id, const char *format, int order, int is_autosorting) {
ddb_undobuffer_t *undobuffer = ddb_undomanager_get_buffer (ddb_undomanager_shared ());
if (plt->undo_enabled) {
pl_lock ();
Expand All @@ -58,7 +58,7 @@ plt_sort_v2 (playlist_t *plt, int iter, int id, const char *format, int order) {
}
}

plt_sort_internal (plt, iter, id, format, order, 1);
plt_sort_internal (plt, iter, id, format, order, 1, is_autosorting);

if (plt->undo_enabled) {
int count = plt->count[PL_MAIN];
Expand All @@ -75,10 +75,15 @@ plt_sort_v2 (playlist_t *plt, int iter, int id, const char *format, int order) {
}
}

void
plt_sort_v2 (playlist_t *plt, int iter, int id, const char *format, int order) {
plt_sort_v2_internal(plt, iter, id, format, order, 0);
}

// sort for title formatting v1
void
plt_sort (playlist_t *playlist, int iter, int id, const char *format, int order) {
plt_sort_internal (playlist, iter, id, format, order, 0);
plt_sort_internal (playlist, iter, id, format, order, 0, 0);
}

static int pl_sort_is_duration;
Expand Down Expand Up @@ -171,9 +176,8 @@ qsort_cmp_func (const void *a, const void *b) {
return pl_sort_compare_str (aa, bb);
}

void
static void
plt_sort_random (playlist_t *playlist, int iter) {
plt_replace_meta (playlist, "autosort_mode", "random");
if (!playlist->head[iter] || !playlist->head[iter]->next[iter]) {
return;
}
Expand Down Expand Up @@ -225,17 +229,23 @@ plt_sort_random (playlist_t *playlist, int iter) {

// version 0: title formatting v1
// version 1: title formatting v2
void
plt_sort_internal (playlist_t *playlist, int iter, int id, const char *format, int order, int version) {
static void
plt_sort_internal (playlist_t *playlist, int iter, int id, const char *format, int order, int version, int is_autosorting) {
if (order == DDB_SORT_RANDOM) {
if (!is_autosorting) {
plt_replace_meta (playlist, "autosort_mode", "random");
}
plt_sort_random (playlist, iter);
return;
}
int ascending = order == DDB_SORT_DESCENDING ? 0 : 1;
plt_set_meta_int (playlist, "autosort_ascending", ascending);
if (version != 0 && format != NULL) {
plt_replace_meta (playlist, "autosort_mode", "tf");
plt_replace_meta (playlist, "autosort_tf", format);
if (!is_autosorting)
{
plt_set_meta_int (playlist, "autosort_ascending", ascending);
if (version != 0 && format != NULL) {
plt_replace_meta (playlist, "autosort_mode", "tf");
plt_replace_meta (playlist, "autosort_tf", format);
}
}

if (format == NULL || id == DB_COLUMN_FILENUMBER || !playlist->head[iter] || !playlist->head[iter]->next[iter]) {
Expand Down Expand Up @@ -409,10 +419,10 @@ plt_autosort (playlist_t *plt) {
if (!fmt) {
return;
}
plt_sort_v2 (plt, PL_MAIN, -1, fmt, ascending ? DDB_SORT_ASCENDING : DDB_SORT_DESCENDING);
plt_sort_v2_internal (plt, PL_MAIN, -1, fmt, ascending ? DDB_SORT_ASCENDING : DDB_SORT_DESCENDING, 1);
}
else if (!strcmp (autosort_mode, "random")) {
plt_sort_v2 (plt, PL_MAIN, -1, NULL, DDB_SORT_RANDOM);
plt_sort_v2_internal (plt, PL_MAIN, -1, NULL, DDB_SORT_RANDOM, 1);
}

plt_save_config (plt);
Expand Down

0 comments on commit 962fa50

Please sign in to comment.