1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
|
/*
* Copyright 2006 Timo Hirvonen
*/
#include "lib.h"
#include "search_mode.h"
#include "xmalloc.h"
#include "comment.h"
#include "utils.h"
#include "debug.h"
struct searchable *tree_searchable;
struct window *lib_tree_win;
struct window *lib_track_win;
struct window *lib_cur_win;
LIST_HEAD(lib_artist_head);
/* tree (search) iterators {{{ */
static int tree_search_get_prev(struct iter *iter)
{
struct list_head *head = iter->data0;
struct tree_track *track = iter->data1;
struct artist *artist;
struct album *album;
BUG_ON(iter->data2);
if (head == NULL)
return 0;
if (track == NULL) {
/* head, get last track */
if (head->prev == head) {
/* empty, iter points to the head already */
return 0;
}
artist = to_artist(head->prev);
album = to_album(artist->album_head.prev);
iter->data1 = to_tree_track(album->track_head.prev);
return 1;
}
/* prev track */
if (track->node.prev == &track->album->track_head || search_restricted) {
/* prev album */
if (track->album->node.prev == &track->album->artist->album_head) {
/* prev artist */
if (track->album->artist->node.prev == &lib_artist_head)
return 0;
artist = to_artist(track->album->artist->node.prev);
album = to_album(artist->album_head.prev);
track = to_tree_track(album->track_head.prev);
} else {
album = to_album(track->album->node.prev);
track = to_tree_track(album->track_head.prev);
}
} else {
track = to_tree_track(track->node.prev);
}
iter->data1 = track;
return 1;
}
static int tree_search_get_next(struct iter *iter)
{
struct list_head *head = iter->data0;
struct tree_track *track = iter->data1;
struct artist *artist;
struct album *album;
BUG_ON(iter->data2);
if (head == NULL)
return 0;
if (track == NULL) {
/* head, get first track */
if (head->next == head) {
/* empty, iter points to the head already */
return 0;
}
artist = to_artist(head->next);
album = to_album(artist->album_head.next);
iter->data1 = to_tree_track(album->track_head.next);
return 1;
}
/* next track */
if (track->node.next == &track->album->track_head || search_restricted) {
/* next album */
if (track->album->node.next == &track->album->artist->album_head) {
/* next artist */
if (track->album->artist->node.next == &lib_artist_head)
return 0;
artist = to_artist(track->album->artist->node.next);
album = to_album(artist->album_head.next);
track = to_tree_track(album->track_head.next);
} else {
album = to_album(track->album->node.next);
track = to_tree_track(album->track_head.next);
}
} else {
track = to_tree_track(track->node.next);
}
iter->data1 = track;
return 1;
}
/* }}} */
/* tree window iterators {{{ */
static int tree_get_prev(struct iter *iter)
{
struct list_head *head = iter->data0;
struct artist *artist = iter->data1;
struct album *album = iter->data2;
BUG_ON(head == NULL);
BUG_ON(artist == NULL && album != NULL);
if (artist == NULL) {
/* head, get last artist and/or album */
if (head->prev == head) {
/* empty, iter points to the head already */
return 0;
}
artist = to_artist(head->prev);
if (artist->expanded) {
album = to_album(artist->album_head.prev);
} else {
album = NULL;
}
iter->data1 = artist;
iter->data2 = album;
return 1;
}
if (artist->expanded && album) {
/* prev album */
if (album->node.prev == &artist->album_head) {
iter->data2 = NULL;
return 1;
} else {
iter->data2 = to_album(album->node.prev);
return 1;
}
}
/* prev artist */
if (artist->node.prev == &lib_artist_head) {
iter->data1 = NULL;
iter->data2 = NULL;
return 0;
}
artist = to_artist(artist->node.prev);
iter->data1 = artist;
iter->data2 = NULL;
if (artist->expanded) {
/* last album */
iter->data2 = to_album(artist->album_head.prev);
}
return 1;
}
static int tree_get_next(struct iter *iter)
{
struct list_head *head = iter->data0;
struct artist *artist = iter->data1;
struct album *album = iter->data2;
BUG_ON(head == NULL);
BUG_ON(artist == NULL && album != NULL);
if (artist == NULL) {
/* head, get first artist */
if (head->next == head) {
/* empty, iter points to the head already */
return 0;
}
iter->data1 = to_artist(head->next);
iter->data2 = NULL;
return 1;
}
if (artist->expanded) {
/* next album */
if (album == NULL) {
/* first album */
iter->data2 = to_album(artist->album_head.next);
return 1;
}
if (album->node.next != &artist->album_head) {
iter->data2 = to_album(album->node.next);
return 1;
}
}
/* next artist */
if (artist->node.next == head) {
iter->data1 = NULL;
iter->data2 = NULL;
return 0;
}
iter->data1 = to_artist(artist->node.next);
iter->data2 = NULL;
return 1;
}
/* }}} */
static GENERIC_ITER_PREV(tree_track_get_prev, struct tree_track, node)
static GENERIC_ITER_NEXT(tree_track_get_next, struct tree_track, node)
static inline void tree_search_track_to_iter(struct tree_track *track, struct iter *iter)
{
iter->data0 = &lib_artist_head;
iter->data1 = track;
iter->data2 = NULL;
}
static inline void album_to_iter(struct album *album, struct iter *iter)
{
iter->data0 = &lib_artist_head;
iter->data1 = album->artist;
iter->data2 = album;
}
static inline void artist_to_iter(struct artist *artist, struct iter *iter)
{
iter->data0 = &lib_artist_head;
iter->data1 = artist;
iter->data2 = NULL;
}
static inline void tree_track_to_iter(struct tree_track *track, struct iter *iter)
{
iter->data0 = &track->album->track_head;
iter->data1 = track;
iter->data2 = NULL;
}
/* search (tree) {{{ */
static int tree_search_get_current(void *data, struct iter *iter)
{
struct artist *artist;
struct album *album;
struct tree_track *track;
struct iter tmpiter;
if (list_empty(&lib_artist_head))
return 0;
if (window_get_sel(lib_track_win, &tmpiter)) {
track = iter_to_tree_track(&tmpiter);
tree_search_track_to_iter(track, iter);
return 1;
}
/* artist not expanded. track_win is empty
* set tmp to the first track of the selected artist */
window_get_sel(lib_tree_win, &tmpiter);
artist = iter_to_artist(&tmpiter);
album = to_album(artist->album_head.next);
track = to_tree_track(album->track_head.next);
tree_search_track_to_iter(track, iter);
return 1;
}
static inline struct tree_track *iter_to_tree_search_track(const struct iter *iter)
{
BUG_ON(iter->data0 != &lib_artist_head);
return iter->data1;
}
static int tree_search_matches(void *data, struct iter *iter, const char *text)
{
struct tree_track *track;
struct iter tmpiter;
unsigned int flags = TI_MATCH_ARTIST | TI_MATCH_ALBUM;
if (!search_restricted)
flags |= TI_MATCH_TITLE;
track = iter_to_tree_search_track(iter);
if (!track_info_matches(tree_track_info(track), text, flags))
return 0;
track->album->artist->expanded = 1;
album_to_iter(track->album, &tmpiter);
window_set_sel(lib_tree_win, &tmpiter);
tree_track_to_iter(track, &tmpiter);
window_set_sel(lib_track_win, &tmpiter);
return 1;
}
static const struct searchable_ops tree_search_ops = {
.get_prev = tree_search_get_prev,
.get_next = tree_search_get_next,
.get_current = tree_search_get_current,
.matches = tree_search_matches
};
/* search (tree) }}} */
static inline int album_selected(struct album *album)
{
struct iter sel;
if (window_get_sel(lib_tree_win, &sel))
return album == iter_to_album(&sel);
return 0;
}
static void tree_sel_changed(void)
{
struct iter sel;
struct album *album;
window_get_sel(lib_tree_win, &sel);
album = iter_to_album(&sel);
if (album == NULL) {
window_set_empty(lib_track_win);
} else {
window_set_contents(lib_track_win, &album->track_head);
}
}
static inline void tree_win_get_selected(struct artist **artist, struct album **album)
{
struct iter sel;
*artist = NULL;
*album = NULL;
if (window_get_sel(lib_tree_win, &sel)) {
*artist = iter_to_artist(&sel);
*album = iter_to_album(&sel);
}
}
static void artist_free(struct artist *artist)
{
free(artist->name);
free(artist);
}
static void album_free(struct album *album)
{
free(album->name);
free(album);
}
void tree_init(void)
{
struct iter iter;
list_init(&lib_artist_head);
lib_tree_win = window_new(tree_get_prev, tree_get_next);
lib_track_win = window_new(tree_track_get_prev, tree_track_get_next);
lib_cur_win = lib_tree_win;
lib_tree_win->sel_changed = tree_sel_changed;
window_set_empty(lib_track_win);
window_set_contents(lib_tree_win, &lib_artist_head);
iter.data0 = &lib_artist_head;
iter.data1 = NULL;
iter.data2 = NULL;
tree_searchable = searchable_new(NULL, &iter, &tree_search_ops);
}
struct track_info *tree_set_selected(void)
{
struct artist *artist;
struct album *album;
struct track_info *info;
struct iter sel;
if (list_empty(&lib_artist_head))
return NULL;
tree_win_get_selected(&artist, &album);
if (album == NULL) {
/* only artist selected, track window is empty
* => get first album of the selected artist and first track of that album
*/
album = to_album(artist->album_head.next);
lib_cur_track = to_tree_track(album->track_head.next);
} else {
window_get_sel(lib_track_win, &sel);
lib_cur_track = iter_to_tree_track(&sel);
}
lib_tree_win->changed = 1;
lib_track_win->changed = 1;
info = tree_track_info(lib_cur_track);
track_info_ref(info);
return info;
}
static void find_artist_and_album(const char *artist_name,
const char *album_name, struct artist **_artist,
struct album **_album)
{
struct artist *artist;
struct album *album;
list_for_each_entry(artist, &lib_artist_head, node) {
int res;
res = xstrcasecmp(artist->name, artist_name);
if (res == 0) {
*_artist = artist;
list_for_each_entry(album, &artist->album_head, node) {
res = xstrcasecmp(album->name, album_name);
if (res == 0) {
*_album = album;
return;
}
}
*_album = NULL;
return;
}
}
*_artist = NULL;
*_album = NULL;
return;
}
static struct artist *add_artist(const char *name)
{
struct list_head *item;
struct artist *artist;
artist = xnew(struct artist, 1);
artist->name = xxstrdup(name);
list_init(&artist->album_head);
artist->expanded = 0;
list_for_each(item, &lib_artist_head) {
struct artist *a = to_artist(item);
if (xstrcasecmp(name, a->name) < 0)
break;
}
/* add before item */
list_add_tail(&artist->node, item);
return artist;
}
static struct album *artist_add_album(struct artist *artist, const char *name, int date)
{
struct list_head *item;
struct album *album;
album = xnew(struct album, 1);
album->name = xxstrdup(name);
album->date = date;
list_init(&album->track_head);
album->artist = artist;
list_for_each(item, &artist->album_head) {
struct album *a = to_album(item);
if (date < a->date)
break;
if (date > a->date)
continue;
if (xstrcasecmp(name, a->name) < 0)
break;
}
/* add before item */
list_add_tail(&album->node, item);
return album;
}
static void album_add_track(struct album *album, struct tree_track *track)
{
/*
* NOTE: This is not perfect. You should ignore track numbers if
* either is unset and use filename instead, but usually you
* have all track numbers set or all unset (within one album
* of course).
*/
static const char * const album_track_sort_keys[] = {
"discnumber", "tracknumber", "filename", NULL
};
struct list_head *item;
track->album = album;
list_for_each(item, &album->track_head) {
const struct simple_track *a = (const struct simple_track *)track;
const struct simple_track *b = (const struct simple_track *)to_tree_track(item);
if (simple_track_cmp(&a->node, &b->node, album_track_sort_keys) < 0)
break;
}
/* add before item */
list_add_tail(&track->node, item);
}
void tree_add_track(struct tree_track *track)
{
const struct track_info *ti = tree_track_info(track);
const char *album_name, *artist_name;
struct artist *artist;
struct album *album;
int date;
album_name = comments_get_val(ti->comments, "album");
artist_name = comments_get_val(ti->comments, "artist");
if (artist_name == NULL && album_name == NULL && is_url(ti->filename)) {
artist_name = "<Stream>";
album_name = "<Stream>";
}
find_artist_and_album(artist_name, album_name, &artist, &album);
if (album) {
album_add_track(album, track);
/* is the album where we added the track selected? */
if (album_selected(album)) {
/* update track window */
window_changed(lib_track_win);
}
} else if (artist) {
date = comments_get_int(ti->comments, "date");
album = artist_add_album(artist, album_name, date);
album_add_track(album, track);
if (artist->expanded) {
/* update tree window */
window_changed(lib_tree_win);
/* album is not selected => no need to update track_win */
}
} else {
date = comments_get_int(ti->comments, "date");
artist = add_artist(artist_name);
album = artist_add_album(artist, album_name, date);
album_add_track(album, track);
window_changed(lib_tree_win);
}
}
static void remove_sel_artist(struct artist *artist)
{
struct list_head *aitem, *ahead;
ahead = &artist->album_head;
aitem = ahead->next;
while (aitem != ahead) {
struct list_head *titem, *thead;
struct list_head *anext = aitem->next;
struct album *album = to_album(aitem);
thead = &album->track_head;
titem = thead->next;
while (titem != thead) {
struct list_head *tnext = titem->next;
struct tree_track *track = to_tree_track(titem);
editable_remove_track(&lib_editable, (struct simple_track *)track);
titem = tnext;
}
/* all tracks removed => album removed
* if the last album was removed then the artist was removed too
*/
aitem = anext;
}
}
static void remove_sel_album(struct album *album)
{
struct list_head *item, *head;
head = &album->track_head;
item = head->next;
while (item != head) {
struct list_head *next = item->next;
struct tree_track *track = to_tree_track(item);
editable_remove_track(&lib_editable, (struct simple_track *)track);
item = next;
}
}
static void tree_win_remove_sel(void)
{
struct artist *artist;
struct album *album;
tree_win_get_selected(&artist, &album);
if (album) {
remove_sel_album(album);
} else if (artist) {
remove_sel_artist(artist);
}
}
static void track_win_remove_sel(void)
{
struct iter sel;
struct tree_track *track;
if (window_get_sel(lib_track_win, &sel)) {
track = iter_to_tree_track(&sel);
BUG_ON(track == NULL);
editable_remove_track(&lib_editable, (struct simple_track *)track);
}
}
void tree_toggle_active_window(void)
{
if (lib_cur_win == lib_tree_win) {
struct artist *artist;
struct album *album;
tree_win_get_selected(&artist, &album);
if (album) {
lib_cur_win = lib_track_win;
lib_tree_win->changed = 1;
lib_track_win->changed = 1;
}
} else if (lib_cur_win == lib_track_win) {
lib_cur_win = lib_tree_win;
lib_tree_win->changed = 1;
lib_track_win->changed = 1;
}
}
void tree_toggle_expand_artist(void)
{
struct iter sel;
struct artist *artist;
window_get_sel(lib_tree_win, &sel);
artist = iter_to_artist(&sel);
if (artist) {
if (artist->expanded) {
/* deselect album, select artist */
artist_to_iter(artist, &sel);
window_set_sel(lib_tree_win, &sel);
artist->expanded = 0;
lib_cur_win = lib_tree_win;
} else {
artist->expanded = 1;
}
window_changed(lib_tree_win);
}
}
static void remove_track(struct tree_track *track)
{
if (album_selected(track->album)) {
struct iter iter;
tree_track_to_iter(track, &iter);
window_row_vanishes(lib_track_win, &iter);
}
list_del(&track->node);
}
static void remove_album(struct album *album)
{
if (album->artist->expanded) {
struct iter iter;
album_to_iter(album, &iter);
window_row_vanishes(lib_tree_win, &iter);
}
list_del(&album->node);
}
static void remove_artist(struct artist *artist)
{
struct iter iter;
artist_to_iter(artist, &iter);
window_row_vanishes(lib_tree_win, &iter);
list_del(&artist->node);
}
void tree_remove(struct tree_track *track)
{
struct album *album = track->album;
struct artist *sel_artist;
struct album *sel_album;
tree_win_get_selected(&sel_artist, &sel_album);
remove_track(track);
/* don't free the track */
if (list_empty(&album->track_head)) {
struct artist *artist = album->artist;
if (sel_album == album)
lib_cur_win = lib_tree_win;
remove_album(album);
album_free(album);
if (list_empty(&artist->album_head)) {
artist->expanded = 0;
remove_artist(artist);
artist_free(artist);
}
}
}
void tree_remove_sel(void)
{
if (lib_cur_win == lib_tree_win) {
tree_win_remove_sel();
} else {
track_win_remove_sel();
}
}
void tree_sel_current(void)
{
if (lib_cur_track) {
struct iter iter;
CUR_ARTIST->expanded = 1;
if (lib_cur_win == lib_tree_win) {
lib_cur_win = lib_track_win;
lib_tree_win->changed = 1;
lib_track_win->changed = 1;
}
album_to_iter(CUR_ALBUM, &iter);
window_set_sel(lib_tree_win, &iter);
tree_track_to_iter(lib_cur_track, &iter);
window_set_sel(lib_track_win, &iter);
}
}
static int album_for_each_track(struct album *album, int (*cb)(void *data, struct track_info *ti),
void *data, int reverse)
{
struct tree_track *track;
int rc = 0;
if (reverse) {
list_for_each_entry_reverse(track, &album->track_head, node) {
rc = cb(data, tree_track_info(track));
if (rc)
break;
}
} else {
list_for_each_entry(track, &album->track_head, node) {
rc = cb(data, tree_track_info(track));
if (rc)
break;
}
}
return rc;
}
static int artist_for_each_track(struct artist *artist, int (*cb)(void *data, struct track_info *ti),
void *data, int reverse)
{
struct album *album;
int rc = 0;
if (reverse) {
list_for_each_entry_reverse(album, &artist->album_head, node) {
rc = album_for_each_track(album, cb, data, 1);
if (rc)
break;
}
} else {
list_for_each_entry(album, &artist->album_head, node) {
rc = album_for_each_track(album, cb, data, 0);
if (rc)
break;
}
}
return rc;
}
int __tree_for_each_sel(int (*cb)(void *data, struct track_info *ti), void *data, int reverse)
{
int rc = 0;
if (lib_cur_win == lib_tree_win) {
struct artist *artist;
struct album *album;
tree_win_get_selected(&artist, &album);
if (artist) {
if (album == NULL) {
rc = artist_for_each_track(artist, cb, data, reverse);
} else {
rc = album_for_each_track(album, cb, data, reverse);
}
}
} else {
struct iter sel;
struct tree_track *track;
if (window_get_sel(lib_track_win, &sel)) {
track = iter_to_tree_track(&sel);
rc = cb(data, tree_track_info(track));
}
}
return rc;
}
int tree_for_each_sel(int (*cb)(void *data, struct track_info *ti), void *data, int reverse)
{
int rc = __tree_for_each_sel(cb, data, reverse);
window_down(lib_cur_win, 1);
return rc;
}
|