mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 10:49:24 +02:00
NetworkDatabase implementation
git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@1520 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
parent
ba7b135ebf
commit
c7e52706ac
9 changed files with 332 additions and 68 deletions
|
@ -58,6 +58,8 @@ class NetworkView {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
|
new SQLiteNetworkDatabase();
|
||||||
|
|
||||||
NetworkLibrary.Instance().synchronize();
|
NetworkLibrary.Instance().synchronize();
|
||||||
|
|
||||||
myActions.add(new NetworkBookActions());
|
myActions.add(new NetworkBookActions());
|
||||||
|
|
|
@ -19,18 +19,43 @@
|
||||||
|
|
||||||
package org.geometerplus.android.fbreader.network;
|
package org.geometerplus.android.fbreader.network;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.database.sqlite.SQLiteStatement;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidApplication;
|
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidApplication;
|
||||||
|
|
||||||
|
import org.geometerplus.fbreader.network.ICustomNetworkLink;
|
||||||
import org.geometerplus.fbreader.network.NetworkDatabase;
|
import org.geometerplus.fbreader.network.NetworkDatabase;
|
||||||
|
|
||||||
public class SQLiteNetworkDatabase extends NetworkDatabase {
|
class SQLiteNetworkDatabase extends NetworkDatabase {
|
||||||
private final SQLiteDatabase myDatabase;
|
private final SQLiteDatabase myDatabase;
|
||||||
|
|
||||||
public SQLiteNetworkDatabase() {
|
SQLiteNetworkDatabase() {
|
||||||
myDatabase = ZLAndroidApplication.Instance().openOrCreateDatabase("network.db", Context.MODE_PRIVATE, null);
|
myDatabase = ZLAndroidApplication.Instance().openOrCreateDatabase("network.db", Context.MODE_PRIVATE, null);
|
||||||
|
migrate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void migrate() {
|
||||||
|
final int version = myDatabase.getVersion();
|
||||||
|
final int currentCodeVersion = 1;
|
||||||
|
if (version >= currentCodeVersion) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
myDatabase.beginTransaction();
|
||||||
|
switch (version) {
|
||||||
|
case 0:
|
||||||
|
createTables();
|
||||||
|
}
|
||||||
|
myDatabase.setTransactionSuccessful();
|
||||||
|
myDatabase.endTransaction();
|
||||||
|
|
||||||
|
myDatabase.execSQL("VACUUM");
|
||||||
|
myDatabase.setVersion(currentCodeVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void executeAsATransaction(Runnable actions) {
|
protected void executeAsATransaction(Runnable actions) {
|
||||||
|
@ -42,4 +67,141 @@ public class SQLiteNetworkDatabase extends NetworkDatabase {
|
||||||
myDatabase.endTransaction();
|
myDatabase.endTransaction();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void bindString(SQLiteStatement statement, int index, String value) {
|
||||||
|
if (value != null) {
|
||||||
|
statement.bindString(index, value);
|
||||||
|
} else {
|
||||||
|
statement.bindNull(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void loadCustomLinks(List<ICustomNetworkLink> links, ICustomLinksFactory factory) {
|
||||||
|
final Cursor cursor = myDatabase.rawQuery("SELECT link_id,title,site_name,summary,icon FROM CustomLinks", null);
|
||||||
|
final HashMap<String,String> linksMap = new HashMap<String,String>();
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
final int id = cursor.getInt(0);
|
||||||
|
final String title = cursor.getString(1);
|
||||||
|
final String siteName = cursor.getString(2);
|
||||||
|
final String summary = cursor.getString(3);
|
||||||
|
final String icon = cursor.getString(4);
|
||||||
|
|
||||||
|
linksMap.clear();
|
||||||
|
final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url FROM CustomLinkUrls WHERE link_id = " + id, null);
|
||||||
|
while (linksCursor.moveToNext()) {
|
||||||
|
linksMap.put(linksCursor.getString(0), linksCursor.getString(1));
|
||||||
|
}
|
||||||
|
linksCursor.close();
|
||||||
|
|
||||||
|
final ICustomNetworkLink newLink = factory.createCustomLink(id, siteName, title, summary, icon, linksMap);
|
||||||
|
if (newLink != null) {
|
||||||
|
links.add(newLink);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private SQLiteStatement myInsertCustomLinkStatement;
|
||||||
|
private SQLiteStatement myUpdateCustomLinkStatement;
|
||||||
|
private SQLiteStatement myInsertCustomLinkUrlStatement;
|
||||||
|
private SQLiteStatement myUpdateCustomLinkUrlStatement;
|
||||||
|
private SQLiteStatement myDeleteCustomLinkUrlStatement;
|
||||||
|
@Override
|
||||||
|
protected void saveCustomLink(final ICustomNetworkLink link) {
|
||||||
|
executeAsATransaction(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
final SQLiteStatement statement;
|
||||||
|
if (link.getId() == ICustomNetworkLink.INVALID_ID) {
|
||||||
|
if (myInsertCustomLinkStatement == null) {
|
||||||
|
myInsertCustomLinkStatement = myDatabase.compileStatement(
|
||||||
|
"INSERT INTO CustomLinks (title,site_name,summary,icon) VALUES (?,?,?,?)"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
statement = myInsertCustomLinkStatement;
|
||||||
|
} else {
|
||||||
|
if (myUpdateCustomLinkStatement == null) {
|
||||||
|
myUpdateCustomLinkStatement = myDatabase.compileStatement(
|
||||||
|
"UPDATE CustomLinks SET title = ?, site_name = ?, summary =?, icon = ? "
|
||||||
|
+ "WHERE link_id = ?"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
statement = myUpdateCustomLinkStatement;
|
||||||
|
}
|
||||||
|
|
||||||
|
statement.bindString(1, link.getTitle());
|
||||||
|
statement.bindString(2, link.getSiteName());
|
||||||
|
bindString(statement, 3, link.getSummary());
|
||||||
|
bindString(statement, 4, link.getIcon());
|
||||||
|
|
||||||
|
final long id;
|
||||||
|
final HashMap<String,String> linksMap = new HashMap<String,String>();
|
||||||
|
|
||||||
|
if (statement == myInsertCustomLinkStatement) {
|
||||||
|
id = statement.executeInsert();
|
||||||
|
link.setId((int) id);
|
||||||
|
} else {
|
||||||
|
id = link.getId();
|
||||||
|
statement.bindLong(5, id);
|
||||||
|
statement.execute();
|
||||||
|
|
||||||
|
final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url FROM CustomLinkUrls WHERE link_id = " + link.getId(), null);
|
||||||
|
while (linksCursor.moveToNext()) {
|
||||||
|
linksMap.put(linksCursor.getString(0), linksCursor.getString(1));
|
||||||
|
}
|
||||||
|
linksCursor.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String key: link.getLinkKeys()) {
|
||||||
|
final String value = link.getLink(key);
|
||||||
|
final String dbValue = linksMap.remove(key);
|
||||||
|
final SQLiteStatement urlStatement;
|
||||||
|
if (dbValue == null) {
|
||||||
|
if (myInsertCustomLinkUrlStatement == null) {
|
||||||
|
myInsertCustomLinkUrlStatement = myDatabase.compileStatement(
|
||||||
|
"INSERT INTO CustomLinkUrls(url,link_id,key) VALUES (?,?,?)");
|
||||||
|
}
|
||||||
|
urlStatement = myInsertCustomLinkUrlStatement;
|
||||||
|
} else if (!value.equals(dbValue)) {
|
||||||
|
if (myUpdateCustomLinkUrlStatement == null) {
|
||||||
|
myUpdateCustomLinkUrlStatement = myDatabase.compileStatement(
|
||||||
|
"UPDATE CustomLinkUrls SET url = ? WHERE link_id = ? AND key = ?");
|
||||||
|
}
|
||||||
|
urlStatement = myUpdateCustomLinkUrlStatement;
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
urlStatement.bindString(1, value);
|
||||||
|
urlStatement.bindLong(2, id);
|
||||||
|
urlStatement.bindString(3, key);
|
||||||
|
urlStatement.execute();
|
||||||
|
}
|
||||||
|
for (String key: linksMap.keySet()) {
|
||||||
|
if (myDeleteCustomLinkUrlStatement == null) {
|
||||||
|
myDeleteCustomLinkUrlStatement = myDatabase.compileStatement(
|
||||||
|
"DELETE FROM CustomLinkUrls WHERE link_id = ? AND key = ?");
|
||||||
|
}
|
||||||
|
myDeleteCustomLinkUrlStatement.bindLong(1, id);
|
||||||
|
myDeleteCustomLinkUrlStatement.bindString(2, key);
|
||||||
|
myDeleteCustomLinkUrlStatement.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createTables() {
|
||||||
|
myDatabase.execSQL(
|
||||||
|
"CREATE TABLE CustomLinks(" +
|
||||||
|
"link_id INTEGER PRIMARY KEY," +
|
||||||
|
"title TEXT UNIQUE NOT NULL," +
|
||||||
|
"site_name TEXT NOT NULL," +
|
||||||
|
"summary TEXT," +
|
||||||
|
"icon TEXT)");
|
||||||
|
myDatabase.execSQL(
|
||||||
|
"CREATE TABLE CustomLinkUrls(" +
|
||||||
|
"key TEXT NOT NULL," +
|
||||||
|
"link_id INTEGER NOT NULL REFERENCES CustomLinks(link_id)," +
|
||||||
|
"url TEXT NOT NULL," +
|
||||||
|
"CONSTRAINT CustomLinkUrls_PK PRIMARY KEY (key, link_id))");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
package org.geometerplus.fbreader.network;
|
package org.geometerplus.fbreader.network;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,4 +69,8 @@ public abstract class AbstractNetworkLink implements INetworkLink {
|
||||||
public String getLink(String urlKey) {
|
public String getLink(String urlKey) {
|
||||||
return myLinks.get(urlKey);
|
return myLinks.get(urlKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<String> getLinkKeys() {
|
||||||
|
return myLinks.keySet();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,18 @@ package org.geometerplus.fbreader.network;
|
||||||
|
|
||||||
public interface ICustomNetworkLink extends INetworkLink {
|
public interface ICustomNetworkLink extends INetworkLink {
|
||||||
|
|
||||||
|
public static final int INVALID_ID = -1;
|
||||||
|
|
||||||
|
int getId();
|
||||||
|
void setId(int id);
|
||||||
|
|
||||||
|
interface SaveLinkListener {
|
||||||
|
void onSaveLink(ICustomNetworkLink link);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setSaveLinkListener(SaveLinkListener listener);
|
||||||
|
void saveLink();
|
||||||
|
|
||||||
void setSiteName(String name);
|
void setSiteName(String name);
|
||||||
void setTitle(String title);
|
void setTitle(String title);
|
||||||
void setSummary(String summary);
|
void setSummary(String summary);
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
|
|
||||||
package org.geometerplus.fbreader.network;
|
package org.geometerplus.fbreader.network;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.network.ZLNetworkRequest;
|
import org.geometerplus.zlibrary.core.network.ZLNetworkRequest;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||||
|
@ -40,6 +42,7 @@ public interface INetworkLink {
|
||||||
String getIcon();
|
String getIcon();
|
||||||
String getLink(String urlKey);
|
String getLink(String urlKey);
|
||||||
|
|
||||||
|
Set<String> getLinkKeys();
|
||||||
|
|
||||||
ZLNetworkRequest simpleSearchRequest(String pattern, NetworkOperationData data);
|
ZLNetworkRequest simpleSearchRequest(String pattern, NetworkOperationData data);
|
||||||
ZLNetworkRequest resume(NetworkOperationData data);
|
ZLNetworkRequest resume(NetworkOperationData data);
|
||||||
|
|
|
@ -19,6 +19,9 @@
|
||||||
|
|
||||||
package org.geometerplus.fbreader.network;
|
package org.geometerplus.fbreader.network;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public abstract class NetworkDatabase {
|
public abstract class NetworkDatabase {
|
||||||
private static NetworkDatabase ourInstance;
|
private static NetworkDatabase ourInstance;
|
||||||
|
|
||||||
|
@ -32,4 +35,10 @@ public abstract class NetworkDatabase {
|
||||||
|
|
||||||
protected abstract void executeAsATransaction(Runnable actions);
|
protected abstract void executeAsATransaction(Runnable actions);
|
||||||
|
|
||||||
|
public interface ICustomLinksFactory {
|
||||||
|
ICustomNetworkLink createCustomLink(int id, String siteName, String title, String summary, String icon, Map<String, String> links);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void loadCustomLinks(List<ICustomNetworkLink> links, ICustomLinksFactory factory);
|
||||||
|
protected abstract void saveCustomLink(ICustomNetworkLink link);
|
||||||
}
|
}
|
||||||
|
|
|
@ -213,7 +213,6 @@ public class NetworkLibrary {
|
||||||
|
|
||||||
private final ArrayList<INetworkLink> myLoadedLinks = new ArrayList<INetworkLink>();
|
private final ArrayList<INetworkLink> myLoadedLinks = new ArrayList<INetworkLink>();
|
||||||
private final ArrayList<ICustomNetworkLink> myCustomLinks = new ArrayList<ICustomNetworkLink>();
|
private final ArrayList<ICustomNetworkLink> myCustomLinks = new ArrayList<ICustomNetworkLink>();
|
||||||
|
|
||||||
private final CompositeList myLinks;
|
private final CompositeList myLinks;
|
||||||
|
|
||||||
private final RootTree myRootTree = new RootTree();
|
private final RootTree myRootTree = new RootTree();
|
||||||
|
@ -223,7 +222,7 @@ public class NetworkLibrary {
|
||||||
|
|
||||||
private NetworkLibrary() {
|
private NetworkLibrary() {
|
||||||
LinkedList<String> catalogs = readCatalogFileNames();
|
LinkedList<String> catalogs = readCatalogFileNames();
|
||||||
OPDSLinkReader reader = new OPDSLinkReader();
|
final OPDSLinkReader reader = new OPDSLinkReader();
|
||||||
for (String fileName: catalogs) {
|
for (String fileName: catalogs) {
|
||||||
INetworkLink link = reader.readDocument(ZLResourceFile.createResourceFile("data/network/" + fileName));
|
INetworkLink link = reader.readDocument(ZLResourceFile.createResourceFile("data/network/" + fileName));
|
||||||
if (link != null) {
|
if (link != null) {
|
||||||
|
@ -231,17 +230,31 @@ public class NetworkLibrary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> links;
|
/*final HashMap<String, String> links = new HashMap<String, String>();
|
||||||
|
|
||||||
links = new TreeMap<String, String>();
|
|
||||||
links.put(INetworkLink.URL_MAIN, "http://bookserver.archive.org/catalog/");
|
links.put(INetworkLink.URL_MAIN, "http://bookserver.archive.org/catalog/");
|
||||||
myCustomLinks.add( reader.createCustomLink("archive.org",
|
NetworkDatabase.Instance().saveCustomLink(
|
||||||
"Internet Archive Catalog", null, null, links));
|
reader.createCustomLink(
|
||||||
|
ICustomNetworkLink.INVALID_ID,
|
||||||
links = new TreeMap<String, String>();
|
"archive.org", "Internet Archive Catalog", null, null, links
|
||||||
|
)
|
||||||
|
);
|
||||||
|
links.clear();
|
||||||
links.put(INetworkLink.URL_MAIN, "http://pragprog.com/magazines.opds");
|
links.put(INetworkLink.URL_MAIN, "http://pragprog.com/magazines.opds");
|
||||||
myCustomLinks.add( reader.createCustomLink("pragprog.com",
|
NetworkDatabase.Instance().saveCustomLink(
|
||||||
"PragPub Magazine", "The Pragmatic Bookshelf", null, links));
|
reader.createCustomLink(
|
||||||
|
ICustomNetworkLink.INVALID_ID,
|
||||||
|
"pragprog.com", "PragPub Magazine", "The Pragmatic Bookshelf", null, links
|
||||||
|
)
|
||||||
|
);*/
|
||||||
|
|
||||||
|
NetworkDatabase.Instance().loadCustomLinks(myCustomLinks,
|
||||||
|
new NetworkDatabase.ICustomLinksFactory() {
|
||||||
|
public ICustomNetworkLink createCustomLink(int id, String siteName,
|
||||||
|
String title, String summary, String icon, Map<String, String> links) {
|
||||||
|
return reader.createCustomLink(id, siteName, title, summary, icon, links);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
LinksComparator comparator = new LinksComparator();
|
LinksComparator comparator = new LinksComparator();
|
||||||
Collections.sort(myLoadedLinks, comparator);
|
Collections.sort(myLoadedLinks, comparator);
|
||||||
|
@ -275,9 +288,11 @@ public class NetworkLibrary {
|
||||||
|
|
||||||
public String rewriteUrl(String url, boolean externalUrl) {
|
public String rewriteUrl(String url, boolean externalUrl) {
|
||||||
final String host = ZLNetworkUtil.hostFromUrl(url).toLowerCase();
|
final String host = ZLNetworkUtil.hostFromUrl(url).toLowerCase();
|
||||||
for (INetworkLink link: myLinks) {
|
synchronized (myLinks) {
|
||||||
if (host.contains(link.getSiteName())) {
|
for (INetworkLink link: myLinks) {
|
||||||
url = link.rewriteUrl(url, externalUrl);
|
if (host.contains(link.getSiteName())) {
|
||||||
|
url = link.rewriteUrl(url, externalUrl);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return url;
|
return url;
|
||||||
|
@ -298,50 +313,52 @@ public class NetworkLibrary {
|
||||||
FBTree currentNode = null;
|
FBTree currentNode = null;
|
||||||
int nodeCount = 0;
|
int nodeCount = 0;
|
||||||
|
|
||||||
ListIterator<INetworkLink> it = myLinks.listIterator();
|
synchronized (myLinks) {
|
||||||
while (it.hasNext()) {
|
ListIterator<INetworkLink> it = myLinks.listIterator();
|
||||||
INetworkLink link = it.next();
|
while (it.hasNext()) {
|
||||||
/*if (!link.OnOption.getValue()) {
|
INetworkLink link = it.next();
|
||||||
continue;
|
/*if (!link.OnOption.getValue()) {
|
||||||
}*/
|
|
||||||
boolean processed = false;
|
|
||||||
while (currentNode != null || nodeIterator.hasNext()) {
|
|
||||||
if (currentNode == null) {
|
|
||||||
currentNode = nodeIterator.next();
|
|
||||||
}
|
|
||||||
if (!(currentNode instanceof NetworkCatalogTree)) {
|
|
||||||
currentNode = null;
|
|
||||||
++nodeCount;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}*/
|
||||||
final INetworkLink nodeLink = ((NetworkCatalogTree)currentNode).Item.Link;
|
boolean processed = false;
|
||||||
if (nodeLink == link) {
|
while (currentNode != null || nodeIterator.hasNext()) {
|
||||||
currentNode = null;
|
if (currentNode == null) {
|
||||||
++nodeCount;
|
currentNode = nodeIterator.next();
|
||||||
processed = true;
|
}
|
||||||
break;
|
if (!(currentNode instanceof NetworkCatalogTree)) {
|
||||||
} else {
|
currentNode = null;
|
||||||
boolean found = false;
|
++nodeCount;
|
||||||
ListIterator<INetworkLink> jt = myLinks.listIterator(it);
|
continue;
|
||||||
while (jt.hasNext()) {
|
}
|
||||||
if (nodeLink == jt.next()) {
|
final INetworkLink nodeLink = ((NetworkCatalogTree) currentNode).Item.Link;
|
||||||
found = true;
|
if (nodeLink == link) {
|
||||||
|
currentNode = null;
|
||||||
|
++nodeCount;
|
||||||
|
processed = true;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
boolean found = false;
|
||||||
|
ListIterator<INetworkLink> jt = myLinks.listIterator(it);
|
||||||
|
while (jt.hasNext()) {
|
||||||
|
if (nodeLink == jt.next()) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
toRemove.add(currentNode);
|
||||||
|
currentNode = null;
|
||||||
|
++nodeCount;
|
||||||
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!found) {
|
|
||||||
toRemove.add(currentNode);
|
|
||||||
currentNode = null;
|
|
||||||
++nodeCount;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
final int nextIndex = nodeIterator.nextIndex();
|
||||||
final int nextIndex = nodeIterator.nextIndex();
|
if (!processed) {
|
||||||
if (!processed) {
|
new NetworkCatalogRootTree(myRootTree, link, nodeCount++).Item.onDisplayItem();
|
||||||
new NetworkCatalogRootTree(myRootTree, link, nodeCount++).Item.onDisplayItem();
|
nodeIterator = myRootTree.subTrees().listIterator(nextIndex + 1);
|
||||||
nodeIterator = myRootTree.subTrees().listIterator(nextIndex + 1);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -351,7 +368,6 @@ public class NetworkLibrary {
|
||||||
}
|
}
|
||||||
toRemove.add(currentNode);
|
toRemove.add(currentNode);
|
||||||
currentNode = null;
|
currentNode = null;
|
||||||
//++nodeCount; // TODO: where to increment???
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (FBTree tree: toRemove) {
|
for (FBTree tree: toRemove) {
|
||||||
|
@ -396,15 +412,17 @@ public class NetworkLibrary {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (INetworkLink link: myLoadedLinks) {
|
synchronized (myLinks) {
|
||||||
//if (link.OnOption.getValue()) {
|
for (INetworkLink link: myLinks) {
|
||||||
// execute next code only if link is enabled
|
//if (link.OnOption.getValue()) {
|
||||||
//}
|
// execute next code only if link is enabled
|
||||||
NetworkOperationData data = new NetworkOperationData(link, synchronizedListener);
|
//}
|
||||||
ZLNetworkRequest request = link.simpleSearchRequest(pattern, data);
|
NetworkOperationData data = new NetworkOperationData(link, synchronizedListener);
|
||||||
if (request != null) {
|
ZLNetworkRequest request = link.simpleSearchRequest(pattern, data);
|
||||||
dataList.add(data);
|
if (request != null) {
|
||||||
requestList.add(request);
|
dataList.add(data);
|
||||||
|
requestList.add(request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -426,4 +444,34 @@ public class NetworkLibrary {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ICustomNetworkLink.SaveLinkListener myChangesListener = new ICustomNetworkLink.SaveLinkListener() {
|
||||||
|
public void onSaveLink(ICustomNetworkLink link) {
|
||||||
|
NetworkDatabase.Instance().saveCustomLink(link);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return <code>false</code> if this library already contains link with the specified title
|
||||||
|
* <code>true</code> if links has been inserted into this library
|
||||||
|
*/
|
||||||
|
public boolean addCustomLink(ICustomNetworkLink link) {
|
||||||
|
final int index = Collections.binarySearch(myCustomLinks, link, new LinksComparator());
|
||||||
|
if (index >= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final int insertAt = -index - 1;
|
||||||
|
myCustomLinks.add(insertAt, link);
|
||||||
|
link.setSaveLinkListener(myChangesListener);
|
||||||
|
link.saveLink();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCustomLinksNumber() {
|
||||||
|
return myCustomLinks.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICustomNetworkLink getCustomLink(int index) {
|
||||||
|
return myCustomLinks.get(index);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,30 @@ import org.geometerplus.fbreader.network.ICustomNetworkLink;
|
||||||
|
|
||||||
class OPDSCustomLink extends OPDSLink implements ICustomNetworkLink {
|
class OPDSCustomLink extends OPDSLink implements ICustomNetworkLink {
|
||||||
|
|
||||||
OPDSCustomLink(String siteName, String title, String summary, String icon, Map<String, String> links) {
|
private int myId;
|
||||||
|
private SaveLinkListener myListener;
|
||||||
|
|
||||||
|
OPDSCustomLink(int id, String siteName, String title, String summary, String icon, Map<String, String> links) {
|
||||||
super(siteName, title, summary, icon, links);
|
super(siteName, title, summary, icon, links);
|
||||||
|
myId = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return myId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
myId = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSaveLinkListener(SaveLinkListener listener) {
|
||||||
|
myListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveLink() {
|
||||||
|
if (myListener != null) {
|
||||||
|
myListener.onSaveLink(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void setIcon(String icon) {
|
public final void setIcon(String icon) {
|
||||||
|
|
|
@ -87,14 +87,15 @@ public class OPDSLinkReader extends ZLXMLReaderAdapter {
|
||||||
return opdsLink;
|
return opdsLink;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICustomNetworkLink createCustomLink(String siteName, String title, String summary, String icon, Map<String, String> links) {
|
public ICustomNetworkLink createCustomLink(int id, String siteName, String title, String summary, String icon, Map<String, String> links) {
|
||||||
if (siteName == null || title == null || links.get(INetworkLink.URL_MAIN) == null) {
|
if (siteName == null || title == null || links.get(INetworkLink.URL_MAIN) == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
OPDSCustomLink link = new OPDSCustomLink(siteName, title, summary, icon, links);
|
OPDSCustomLink link = new OPDSCustomLink(id, siteName, title, summary, icon, links);
|
||||||
|
|
||||||
// TODO: read common OPDSLink attributes from special custom.xml file
|
// TODO: read common OPDSLink attributes from special custom.xml file
|
||||||
|
// Does this additional info have to override duplicated settings, received from user???
|
||||||
|
|
||||||
return link;
|
return link;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue