Fix --help messages, fix trans step 1/2
[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.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 ones 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 public String getLibraryName() {
53 return host + ":" + port;
54 }
55
56 @Override
57 protected List<MetaData> getMetas(Progress pg) {
58 // TODO: progress
59
60 if (metas == null) {
61 metas = new ArrayList<MetaData>();
62
63 try {
64 new ConnectActionClient(host, port, true, null) {
65 @Override
66 public void action(Version serverVersion) throws Exception {
67 try {
68 Object rep = send("GET_METADATA *");
69 for (MetaData meta : (MetaData[]) rep) {
70 metas.add(meta);
71 }
72 } catch (Exception e) {
73 Instance.syserr(e);
74 }
75 }
76 }.connect();
77 } catch (IOException e) {
78 Instance.syserr(e);
79 }
80 }
81
82 return metas;
83 }
84
85 @Override
86 public synchronized File getFile(final String luid) {
87 File file = lib.getFile(luid);
88 if (file == null) {
89 final File[] tmp = new File[1];
90 try {
91 new ConnectActionClient(host, port, true, null) {
92 @Override
93 public void action(Version serverVersion) throws Exception {
94 try {
95 Object rep = send("GET_STORY " + luid);
96 Story story = (Story) rep;
97 if (story != null) {
98 lib.save(story, luid, null);
99 tmp[0] = lib.getFile(luid);
100 }
101 } catch (Exception e) {
102 Instance.syserr(e);
103 }
104 }
105 }.connect();
106 } catch (IOException e) {
107 Instance.syserr(e);
108 }
109
110 file = tmp[0];
111 }
112
113 if (file != null) {
114 MetaData meta = getInfo(luid);
115 metas.add(meta);
116 }
117
118 return file;
119 }
120
121 @Override
122 public BufferedImage getCover(String luid) {
123 // Retrieve it from the network if needed:
124 if (lib.getInfo(luid) == null) {
125 getFile(luid);
126 }
127
128 return lib.getCover(luid);
129 }
130
131 @Override
132 protected void clearCache() {
133 metas = null;
134 lib.clearCache();
135 }
136
137 @Override
138 public synchronized Story save(Story story, String luid, Progress pg)
139 throws IOException {
140 throw new java.lang.InternalError(
141 "No write support allowed on remote Libraries");
142 }
143
144 @Override
145 public synchronized void delete(String luid) throws IOException {
146 throw new java.lang.InternalError(
147 "No write support allowed on remote Libraries");
148 }
149
150 @Override
151 public void setSourceCover(String source, String luid) {
152 throw new java.lang.InternalError(
153 "No write support allowed on remote Libraries");
154 }
155
156 // All the following methods are only used by Save and Delete in
157 // BasicLibrary:
158
159 @Override
160 protected int getNextId() {
161 throw new java.lang.InternalError("Should not have been called");
162 }
163
164 @Override
165 protected void doDelete(String luid) throws IOException {
166 throw new java.lang.InternalError("Should not have been called");
167 }
168
169 @Override
170 protected Story doSave(Story story, Progress pg) throws IOException {
171 throw new java.lang.InternalError("Should not have been called");
172 }
173 }