Fix configure.sh again (--tui=no + jar was not ok)
[fanfix.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
1 package be.nikiroo.fanfix.library;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import be.nikiroo.fanfix.Instance;
10 import be.nikiroo.fanfix.data.MetaData;
11 import be.nikiroo.fanfix.data.Story;
12 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
13 import be.nikiroo.utils.Progress;
14 import be.nikiroo.utils.Version;
15 import be.nikiroo.utils.serial.ConnectActionClient;
16
17 /**
18 * This {@link BasicLibrary} will access a remote server to list the available
19 * stories, and download the one you try to load to the local directory
20 * specified in the configuration.
21 *
22 * @author niki
23 */
24 public class RemoteLibrary extends BasicLibrary {
25 private String host;
26 private int port;
27 private File baseDir;
28
29 private LocalLibrary lib;
30 private List<MetaData> metas;
31
32 /**
33 * Create a {@link RemoteLibrary} linked to the given server.
34 *
35 * @param host
36 * the host to contact or NULL for localhost
37 * @param port
38 * the port to contact it on
39 */
40 public RemoteLibrary(String host, int port) {
41 this.host = host;
42 this.port = port;
43
44 this.baseDir = Instance.getRemoteDir(host);
45 this.baseDir.mkdirs();
46
47 this.lib = new LocalLibrary(baseDir, OutputType.INFO_TEXT,
48 OutputType.CBZ);
49 }
50
51 @Override
52 protected List<MetaData> getMetas(Progress pg) {
53 // TODO: progress
54
55 if (metas == null) {
56 metas = new ArrayList<MetaData>();
57
58 try {
59 new ConnectActionClient(host, port, true, null) {
60 public void action(Version serverVersion) throws Exception {
61 try {
62 Object rep = send("GET_METADATA *");
63 for (MetaData meta : (MetaData[]) rep) {
64 metas.add(meta);
65 }
66 } catch (Exception e) {
67 Instance.syserr(e);
68 }
69 }
70 }.connect();
71 } catch (IOException e) {
72 Instance.syserr(e);
73 }
74 }
75
76 return metas;
77 }
78
79 @Override
80 public synchronized File getFile(final String luid) {
81 File file = lib.getFile(luid);
82 if (file == null) {
83 final File[] tmp = new File[1];
84 try {
85 new ConnectActionClient(host, port, true, null) {
86 public void action(Version serverVersion) throws Exception {
87 try {
88 Object rep = send("GET_STORY " + luid);
89 Story story = (Story) rep;
90 if (story != null) {
91 lib.save(story, luid, null);
92 tmp[0] = lib.getFile(luid);
93 }
94 } catch (Exception e) {
95 Instance.syserr(e);
96 }
97 }
98 }.connect();
99 } catch (IOException e) {
100 Instance.syserr(e);
101 }
102
103 file = tmp[0];
104 }
105
106 if (file != null) {
107 MetaData meta = getInfo(luid);
108 metas.add(meta);
109 }
110
111 return file;
112 }
113
114 @Override
115 public BufferedImage getCover(String luid) {
116 // Retrieve it from the network if needed:
117 if (lib.getInfo(luid) == null) {
118 getFile(luid);
119 }
120
121 return lib.getCover(luid);
122 }
123
124 @Override
125 protected void clearCache() {
126 metas = null;
127 lib.clearCache();
128 }
129
130 @Override
131 public synchronized Story save(Story story, String luid, Progress pg)
132 throws IOException {
133 throw new java.lang.InternalError(
134 "No write support allowed on remote Libraries");
135 }
136
137 @Override
138 protected int getNextId() {
139 throw new java.lang.InternalError(
140 "No write support allowed on remote Libraries");
141 }
142
143 @Override
144 protected void doDelete(String luid) throws IOException {
145 throw new java.lang.InternalError(
146 "No write support allowed on remote Libraries");
147 }
148
149 @Override
150 protected Story doSave(Story story, Progress pg) throws IOException {
151 throw new java.lang.InternalError(
152 "No write support allowed on remote Libraries");
153 }
154 }