Some fixes: output types, libraries, remote
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
CommitLineData
e42573a0 1package be.nikiroo.fanfix.library;
b0e88ebd 2
68e2c6d2 3import java.awt.image.BufferedImage;
b0e88ebd
NR
4import java.io.File;
5import java.io.IOException;
68e2c6d2
NR
6import java.util.ArrayList;
7import java.util.List;
b0e88ebd 8
e42573a0 9import be.nikiroo.fanfix.Instance;
b0e88ebd
NR
10import be.nikiroo.fanfix.data.MetaData;
11import be.nikiroo.fanfix.data.Story;
b0e88ebd
NR
12import be.nikiroo.utils.Progress;
13import be.nikiroo.utils.Version;
14import be.nikiroo.utils.serial.ConnectActionClient;
15
68e2c6d2
NR
16/**
17 * This {@link BasicLibrary} will access a remote server to list the available
a85e8077 18 * stories, and download the ones you try to load to the local directory
68e2c6d2
NR
19 * specified in the configuration.
20 *
21 * @author niki
22 */
23public class RemoteLibrary extends BasicLibrary {
b0e88ebd
NR
24 private String host;
25 private int port;
68e2c6d2
NR
26 private File baseDir;
27
28 private LocalLibrary lib;
29 private List<MetaData> metas;
30
31 /**
32 * Create a {@link RemoteLibrary} linked to the given server.
33 *
34 * @param host
35 * the host to contact or NULL for localhost
36 * @param port
37 * the port to contact it on
38 */
39 public RemoteLibrary(String host, int port) {
b0e88ebd
NR
40 this.host = host;
41 this.port = port;
42
b0e88ebd
NR
43 this.baseDir = Instance.getRemoteDir(host);
44 this.baseDir.mkdirs();
45
e604986c 46 this.lib = new LocalLibrary(baseDir);
b0e88ebd
NR
47 }
48
99ccbdf6
NR
49 @Override
50 public String getLibraryName() {
51 return host + ":" + port;
52 }
53
b0e88ebd 54 @Override
68e2c6d2
NR
55 protected List<MetaData> getMetas(Progress pg) {
56 // TODO: progress
b0e88ebd 57
68e2c6d2
NR
58 if (metas == null) {
59 metas = new ArrayList<MetaData>();
b0e88ebd 60
b0e88ebd 61 try {
e604986c 62 new ConnectActionClient(host, port, true) {
211f7ddb 63 @Override
b0e88ebd
NR
64 public void action(Version serverVersion) throws Exception {
65 try {
66 Object rep = send("GET_METADATA *");
67 for (MetaData meta : (MetaData[]) rep) {
68e2c6d2 68 metas.add(meta);
b0e88ebd
NR
69 }
70 } catch (Exception e) {
68e2c6d2 71 Instance.syserr(e);
b0e88ebd
NR
72 }
73 }
74 }.connect();
75 } catch (IOException e) {
76 Instance.syserr(e);
77 }
e604986c
NR
78
79 List<String> test = new ArrayList<String>();
80 for (MetaData meta : metas) {
81 if (test.contains(meta.getLuid())) {
82 throw new RuntimeException("wwops");
83 }
84 test.add(meta.getLuid());
85 }
b0e88ebd
NR
86 }
87
68e2c6d2 88 return metas;
b0e88ebd
NR
89 }
90
91 @Override
92 public synchronized File getFile(final String luid) {
93 File file = lib.getFile(luid);
94 if (file == null) {
95 final File[] tmp = new File[1];
96 try {
e604986c 97 new ConnectActionClient(host, port, true) {
211f7ddb 98 @Override
b0e88ebd
NR
99 public void action(Version serverVersion) throws Exception {
100 try {
101 Object rep = send("GET_STORY " + luid);
102 Story story = (Story) rep;
103 if (story != null) {
104 lib.save(story, luid, null);
105 tmp[0] = lib.getFile(luid);
106 }
107 } catch (Exception e) {
108 Instance.syserr(e);
109 }
110 }
111 }.connect();
112 } catch (IOException e) {
113 Instance.syserr(e);
114 }
115
116 file = tmp[0];
117 }
118
119 if (file != null) {
120 MetaData meta = getInfo(luid);
68e2c6d2 121 metas.add(meta);
b0e88ebd
NR
122 }
123
124 return file;
125 }
68e2c6d2
NR
126
127 @Override
e604986c
NR
128 public BufferedImage getCover(final String luid) {
129 // Retrieve it from the cache if possible:
130 if (lib.getInfo(luid) != null) {
131 return lib.getCover(luid);
132 }
133
134 final BufferedImage[] result = new BufferedImage[1];
135 try {
136 new ConnectActionClient(host, port, true) {
137 @Override
138 public void action(Version serverVersion) throws Exception {
139 try {
140 Object rep = send("GET_COVER " + luid);
141 result[0] = (BufferedImage) rep;
142 } catch (Exception e) {
143 Instance.syserr(e);
144 }
145 }
146 }.connect();
147 } catch (IOException e) {
148 Instance.syserr(e);
68e2c6d2
NR
149 }
150
e604986c 151 return result[0];
68e2c6d2
NR
152 }
153
154 @Override
155 protected void clearCache() {
156 metas = null;
157 lib.clearCache();
158 }
159
160 @Override
161 public synchronized Story save(Story story, String luid, Progress pg)
162 throws IOException {
163 throw new java.lang.InternalError(
164 "No write support allowed on remote Libraries");
165 }
166
167 @Override
a85e8077 168 public synchronized void delete(String luid) throws IOException {
68e2c6d2
NR
169 throw new java.lang.InternalError(
170 "No write support allowed on remote Libraries");
171 }
172
173 @Override
a85e8077 174 public void setSourceCover(String source, String luid) {
68e2c6d2
NR
175 throw new java.lang.InternalError(
176 "No write support allowed on remote Libraries");
177 }
178
a85e8077
NR
179 // All the following methods are only used by Save and Delete in
180 // BasicLibrary:
181
68e2c6d2 182 @Override
a85e8077
NR
183 protected int getNextId() {
184 throw new java.lang.InternalError("Should not have been called");
68e2c6d2 185 }
14b57448
NR
186
187 @Override
a85e8077
NR
188 protected void doDelete(String luid) throws IOException {
189 throw new java.lang.InternalError("Should not have been called");
190 }
191
192 @Override
193 protected Story doSave(Story story, Progress pg) throws IOException {
194 throw new java.lang.InternalError("Should not have been called");
14b57448 195 }
e604986c
NR
196
197 /**
198 * Return the backing local library.
199 *
200 * @return the library
201 */
202 LocalLibrary getLocalLibrary() {
203 return lib;
204 }
b0e88ebd 205}