fireModelChangedEvent(ChangeListener.Code.BookAdded);
}
<<<<<<< HEAD
=======
private void build() {
// Step 0: get database books marked as "existing"
final FileInfoSet fileInfos = new FileInfoSet(myDatabase);
final Map savedBooksByFileId = myDatabase.loadBooks(fileInfos, true);
final Map savedBooksByBookId = new HashMap();
for (Book b : savedBooksByFileId.values()) {
savedBooksByBookId.put(b.getId(), b);
}
// Step 2: check if files corresponding to "existing" books really exists;
// add books to library if yes (and reload book info if needed);
// remove from recent/favorites list if no;
// collect newly "orphaned" books
final Set orphanedBooks = new HashSet();
final Set physicalFiles = new HashSet();
int count = 0;
for (Book book : savedBooksByFileId.values()) {
synchronized (this) {
final ZLPhysicalFile file = book.File.getPhysicalFile();
if (file != null) {
physicalFiles.add(file);
}
if (file != book.File && file != null && file.getPath().endsWith(".epub")) {
continue;
}
if (book.File.exists()) {
boolean doAdd = true;
if (file == null) {
continue;
}
if (!fileInfos.check(file, true)) {
try {
book.readMetaInfo();
book.save();
} catch (BookReadingException e) {
doAdd = false;
}
file.setCached(false);
}
if (doAdd) {
addBookToLibrary(book);
if (++count % 16 == 0) {
fireModelChangedEvent(ChangeListener.Code.BookAdded);
}
}
} else {
fireModelChangedEvent(ChangeListener.Code.BookRemoved);
orphanedBooks.add(book);
}
}
}
fireModelChangedEvent(ChangeListener.Code.BookAdded);
myDatabase.setExistingFlag(orphanedBooks, false);
// Step 3: collect books from physical files; add new, update already added,
// unmark orphaned as existing again, collect newly added
final Map orphanedBooksByFileId = myDatabase.loadBooks(fileInfos, false);
final Set newBooks = new HashSet();
final List physicalFilesList = collectPhysicalFiles();
for (ZLPhysicalFile file : physicalFilesList) {
if (physicalFiles.contains(file)) {
continue;
}
collectBooks(
file, fileInfos,
savedBooksByFileId, orphanedBooksByFileId,
newBooks,
!fileInfos.check(file, true)
);
file.setCached(false);
}
// Step 4: add help file
try {
final ZLFile helpFile = getHelpFile();
Book helpBook = savedBooksByFileId.get(fileInfos.getId(helpFile));
if (helpBook == null) {
helpBook = new Book(helpFile);
}
addBookToLibrary(helpBook);
fireModelChangedEvent(ChangeListener.Code.BookAdded);
} catch (BookReadingException e) {
// that's impossible
e.printStackTrace();
}
// Step 5: save changes into database
fileInfos.save();
myDatabase.executeAsATransaction(new Runnable() {
public void run() {
for (Book book : newBooks) {
book.save();
}
}
});
myDatabase.setExistingFlag(newBooks, true);
}
private volatile boolean myBuildStarted = false;
public synchronized void startBuild() {
if (myBuildStarted) {
fireModelChangedEvent(ChangeListener.Code.StatusChanged);
return;
}
myBuildStarted = true;
setStatus(myStatusMask | STATUS_LOADING);
final Thread builder = new Thread("Library.build") {
public void run() {
try {
build();
} finally {
setStatus(myStatusMask & ~STATUS_LOADING);
}
}
};
builder.setPriority((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2);
builder.start();
}
>>>>>>> 2148db4a2dcb3cb8be13fb74f0797867b99e2620
public boolean isUpToDate() {
return myStatusMask == 0;
} |