Some fixes: output types, libraries, remote
[nikiroo-utils.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.utils.Progress;
13 import be.nikiroo.utils.Version;
14 import be.nikiroo.utils.serial.ConnectActionClient;
15
16 /**
17 * This {@link BasicLibrary} will access a remote server to list the available
18 * stories, and download the ones you try to load to the local directory
19 * specified in the configuration.
20 *
21 * @author niki
22 */
23 public class RemoteLibrary extends BasicLibrary {
24 private String host;
25 private int port;
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) {
40 this.host = host;
41 this.port = port;
42
43 this.baseDir = Instance.getRemoteDir(host);
44 this.baseDir.mkdirs();
45
46 this.lib = new LocalLibrary(baseDir);
47 }
48
49 @Override
50 public String getLibraryName() {
51 return host + ":" + port;
52 }
53
54 @Override
55 protected List<MetaData> getMetas(Progress pg) {
56 // TODO: progress
57
58 if (metas == null) {
59 metas = new ArrayList<MetaData>();
60
61 try {
62 new ConnectActionClient(host, port, true) {
63 @Override
64 public void action(Version serverVersion) throws Exception {
65 try {
66 Object rep = send("GET_METADATA *");
67 for (MetaData meta : (MetaData[]) rep) {
68 metas.add(meta);
69 }
70 } catch (Exception e) {
71 Instance.syserr(e);
72 }
73 }
74 }.connect();
75 } catch (IOException e) {
76 Instance.syserr(e);
77 }
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 }
86 }
87
88 return metas;
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 {
97 new ConnectActionClient(host, port, true) {
98 @Override
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);
121 metas.add(meta);
122 }
123
124 return file;
125 }
126
127 @Override
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);
149 }
150
151 return result[0];
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
168 public synchronized void delete(String luid) throws IOException {
169 throw new java.lang.InternalError(
170 "No write support allowed on remote Libraries");
171 }
172
173 @Override
174 public void setSourceCover(String source, String luid) {
175 throw new java.lang.InternalError(
176 "No write support allowed on remote Libraries");
177 }
178
179 // All the following methods are only used by Save and Delete in
180 // BasicLibrary:
181
182 @Override
183 protected int getNextId() {
184 throw new java.lang.InternalError("Should not have been called");
185 }
186
187 @Override
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");
195 }
196
197 /**
198 * Return the backing local library.
199 *
200 * @return the library
201 */
202 LocalLibrary getLocalLibrary() {
203 return lib;
204 }
205 }