diff --git a/src/org/geometerplus/fbreader/book/Author.java b/src/org/geometerplus/fbreader/book/Author.java index 3cafc1d18..23326e8b7 100644 --- a/src/org/geometerplus/fbreader/book/Author.java +++ b/src/org/geometerplus/fbreader/book/Author.java @@ -20,6 +20,8 @@ package org.geometerplus.fbreader.book; public final class Author implements Comparable { + public static final Author NULL = new Author("", ""); + public final String DisplayName; public final String SortKey; diff --git a/src/org/geometerplus/fbreader/book/Book.java b/src/org/geometerplus/fbreader/book/Book.java index a83dfa073..fb9c4f815 100644 --- a/src/org/geometerplus/fbreader/book/Book.java +++ b/src/org/geometerplus/fbreader/book/Book.java @@ -34,7 +34,6 @@ import org.geometerplus.zlibrary.core.image.ZLImage; import org.geometerplus.zlibrary.text.view.ZLTextPosition; import org.geometerplus.fbreader.Paths; -import org.geometerplus.fbreader.book.*; import org.geometerplus.fbreader.bookmodel.BookReadingException; import org.geometerplus.fbreader.formats.*; import org.geometerplus.fbreader.library.Library; @@ -141,6 +140,18 @@ public class Book { readMetaInfo(plugin); } + public void updateFrom(Book book) { + if (myId != book.myId) { + return; + } + myTitle = book.myTitle; + myEncoding = book.myEncoding; + myLanguage = book.myLanguage; + myAuthors = book.myAuthors != null ? new ArrayList(book.myAuthors) : null; + myTags = book.myTags != null ? new ArrayList(book.myTags) : null; + mySeriesInfo = book.mySeriesInfo; + } + public void reloadInfoFromFile() { try { readMetaInfo(); diff --git a/src/org/geometerplus/fbreader/library/AuthorListTree.java b/src/org/geometerplus/fbreader/library/AuthorListTree.java new file mode 100644 index 000000000..049cb8513 --- /dev/null +++ b/src/org/geometerplus/fbreader/library/AuthorListTree.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2009-2013 Geometer Plus + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +package org.geometerplus.fbreader.library; + +import java.util.Collections; +import java.util.List; + +import org.geometerplus.fbreader.book.*; + +public class AuthorListTree extends FirstLevelTree { + AuthorListTree(RootTree root) { + super(root, ROOT_BY_AUTHOR); + } + + @Override + public Status getOpeningStatus() { + return Status.ALWAYS_RELOAD_BEFORE_OPENING; + } + + @Override + public void waitForOpening() { + clear(); + for (Author a : Collection.authors()) { + createAuthorSubTree(a); + } + } + + @Override + public boolean onBookEvent(BookEvent event, Book book) { + switch (event) { + default: + case Added: + { + final List bookAuthors = book.authors(); + boolean changed = false; + if (bookAuthors.isEmpty()) { + changed &= createAuthorSubTree(Author.NULL); + } else for (Author a : Collection.authors()) { + changed &= createAuthorSubTree(a); + } + return changed; + } + case Removed: + // TODO: implement + return false; + case Updated: + // TODO: implement + return false; + } + } + + private boolean createAuthorSubTree(Author author) { + final AuthorTree temp = new AuthorTree(Collection, author); + int position = Collections.binarySearch(subTrees(), temp); + if (position >= 0) { + return false; + } else { + new AuthorTree(this, author, - position - 1); + return true; + } + } +} diff --git a/src/org/geometerplus/fbreader/library/LibraryTree.java b/src/org/geometerplus/fbreader/library/LibraryTree.java index 425b19327..0d4795bf6 100644 --- a/src/org/geometerplus/fbreader/library/LibraryTree.java +++ b/src/org/geometerplus/fbreader/library/LibraryTree.java @@ -142,6 +142,30 @@ public abstract class LibraryTree extends FBTree { return !toRemove.isEmpty(); } + public boolean onBookEvent(BookEvent event, Book book) { + switch (event) { + default: + case Added: + return false; + case Removed: + return removeBook(book, true); + case Updated: + { + boolean changed = false; + for (FBTree tree : this) { + if (tree instanceof BookTree) { + final Book b = ((BookTree)tree).Book; + if (b.equals(book)) { + b.updateFrom(book); + changed = true; + } + } + } + return changed; + } + } + } + @Override public int compareTo(FBTree tree) { final int cmp = super.compareTo(tree);