Add more warnings source to 1.6) and fix warnings
[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;
12import be.nikiroo.fanfix.output.BasicOutput.OutputType;
13import be.nikiroo.utils.Progress;
14import be.nikiroo.utils.Version;
15import be.nikiroo.utils.serial.ConnectActionClient;
16
68e2c6d2
NR
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 */
24public class RemoteLibrary extends BasicLibrary {
b0e88ebd
NR
25 private String host;
26 private int port;
68e2c6d2
NR
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) {
b0e88ebd
NR
41 this.host = host;
42 this.port = port;
43
b0e88ebd
NR
44 this.baseDir = Instance.getRemoteDir(host);
45 this.baseDir.mkdirs();
46
68e2c6d2
NR
47 this.lib = new LocalLibrary(baseDir, OutputType.INFO_TEXT,
48 OutputType.CBZ);
b0e88ebd
NR
49 }
50
51 @Override
68e2c6d2
NR
52 protected List<MetaData> getMetas(Progress pg) {
53 // TODO: progress
b0e88ebd 54
68e2c6d2
NR
55 if (metas == null) {
56 metas = new ArrayList<MetaData>();
b0e88ebd 57
b0e88ebd
NR
58 try {
59 new ConnectActionClient(host, port, true, null) {
211f7ddb 60 @Override
b0e88ebd
NR
61 public void action(Version serverVersion) throws Exception {
62 try {
63 Object rep = send("GET_METADATA *");
64 for (MetaData meta : (MetaData[]) rep) {
68e2c6d2 65 metas.add(meta);
b0e88ebd
NR
66 }
67 } catch (Exception e) {
68e2c6d2 68 Instance.syserr(e);
b0e88ebd
NR
69 }
70 }
71 }.connect();
72 } catch (IOException e) {
73 Instance.syserr(e);
74 }
75 }
76
68e2c6d2 77 return metas;
b0e88ebd
NR
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) {
211f7ddb 87 @Override
b0e88ebd
NR
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);
68e2c6d2 110 metas.add(meta);
b0e88ebd
NR
111 }
112
113 return file;
114 }
68e2c6d2
NR
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 }
b0e88ebd 156}