Add more warnings source to 1.6) and fix warnings
[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 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 @Override
61 public void action(Version serverVersion) throws Exception {
62 try {
63 Object rep = send("GET_METADATA *");
64 for (MetaData meta : (MetaData[]) rep) {
65 metas.add(meta);
66 }
67 } catch (Exception e) {
68 Instance.syserr(e);
69 }
70 }
71 }.connect();
72 } catch (IOException e) {
73 Instance.syserr(e);
74 }
75 }
76
77 return metas;
78 }
79
80 @Override
81 public synchronized File getFile(final String luid) {
82 File file = lib.getFile(luid);
83 if (file == null) {
84 final File[] tmp = new File[1];
85 try {
86 new ConnectActionClient(host, port, true, null) {
87 @Override
88 public void action(Version serverVersion) throws Exception {
89 try {
90 Object rep = send("GET_STORY " + luid);
91 Story story = (Story) rep;
92 if (story != null) {
93 lib.save(story, luid, null);
94 tmp[0] = lib.getFile(luid);
95 }
96 } catch (Exception e) {
97 Instance.syserr(e);
98 }
99 }
100 }.connect();
101 } catch (IOException e) {
102 Instance.syserr(e);
103 }
104
105 file = tmp[0];
106 }
107
108 if (file != null) {
109 MetaData meta = getInfo(luid);
110 metas.add(meta);
111 }
112
113 return file;
114 }
115
116 @Override
117 public BufferedImage getCover(String luid) {
118 // Retrieve it from the network if needed:
119 if (lib.getInfo(luid) == null) {
120 getFile(luid);
121 }
122
123 return lib.getCover(luid);
124 }
125
126 @Override
127 protected void clearCache() {
128 metas = null;
129 lib.clearCache();
130 }
131
132 @Override
133 public synchronized Story save(Story story, String luid, Progress pg)
134 throws IOException {
135 throw new java.lang.InternalError(
136 "No write support allowed on remote Libraries");
137 }
138
139 @Override
140 protected int getNextId() {
141 throw new java.lang.InternalError(
142 "No write support allowed on remote Libraries");
143 }
144
145 @Override
146 protected void doDelete(String luid) throws IOException {
147 throw new java.lang.InternalError(
148 "No write support allowed on remote Libraries");
149 }
150
151 @Override
152 protected Story doSave(Story story, Progress pg) throws IOException {
153 throw new java.lang.InternalError(
154 "No write support allowed on remote Libraries");
155 }
156 }