Commit | Line | Data |
---|---|---|
b0e88ebd NR |
1 | package be.nikiroo.fanfix; |
2 | ||
68e2c6d2 | 3 | import java.awt.image.BufferedImage; |
b0e88ebd NR |
4 | import java.io.File; |
5 | import java.io.IOException; | |
68e2c6d2 NR |
6 | import java.util.ArrayList; |
7 | import java.util.List; | |
b0e88ebd NR |
8 | |
9 | import be.nikiroo.fanfix.data.MetaData; | |
10 | import be.nikiroo.fanfix.data.Story; | |
11 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
12 | import be.nikiroo.utils.Progress; | |
13 | import be.nikiroo.utils.Version; | |
14 | import be.nikiroo.utils.serial.ConnectActionClient; | |
15 | ||
68e2c6d2 NR |
16 | /** |
17 | * This {@link BasicLibrary} will access a remote server to list the available | |
18 | * stories, and download the one you try to load to the local directory | |
19 | * specified in the configuration. | |
20 | * | |
21 | * @author niki | |
22 | */ | |
23 | public 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 | ||
68e2c6d2 NR |
46 | this.lib = new LocalLibrary(baseDir, OutputType.INFO_TEXT, |
47 | OutputType.CBZ); | |
b0e88ebd NR |
48 | } |
49 | ||
50 | @Override | |
68e2c6d2 NR |
51 | protected List<MetaData> getMetas(Progress pg) { |
52 | // TODO: progress | |
b0e88ebd | 53 | |
68e2c6d2 NR |
54 | if (metas == null) { |
55 | metas = new ArrayList<MetaData>(); | |
b0e88ebd | 56 | |
b0e88ebd NR |
57 | try { |
58 | new ConnectActionClient(host, port, true, null) { | |
59 | public void action(Version serverVersion) throws Exception { | |
60 | try { | |
61 | Object rep = send("GET_METADATA *"); | |
62 | for (MetaData meta : (MetaData[]) rep) { | |
68e2c6d2 | 63 | metas.add(meta); |
b0e88ebd NR |
64 | } |
65 | } catch (Exception e) { | |
68e2c6d2 | 66 | Instance.syserr(e); |
b0e88ebd NR |
67 | } |
68 | } | |
69 | }.connect(); | |
70 | } catch (IOException e) { | |
71 | Instance.syserr(e); | |
72 | } | |
73 | } | |
74 | ||
68e2c6d2 | 75 | return metas; |
b0e88ebd NR |
76 | } |
77 | ||
78 | @Override | |
79 | public synchronized File getFile(final String luid) { | |
80 | File file = lib.getFile(luid); | |
81 | if (file == null) { | |
82 | final File[] tmp = new File[1]; | |
83 | try { | |
84 | new ConnectActionClient(host, port, true, null) { | |
85 | public void action(Version serverVersion) throws Exception { | |
86 | try { | |
87 | Object rep = send("GET_STORY " + luid); | |
88 | Story story = (Story) rep; | |
89 | if (story != null) { | |
90 | lib.save(story, luid, null); | |
91 | tmp[0] = lib.getFile(luid); | |
92 | } | |
93 | } catch (Exception e) { | |
94 | Instance.syserr(e); | |
95 | } | |
96 | } | |
97 | }.connect(); | |
98 | } catch (IOException e) { | |
99 | Instance.syserr(e); | |
100 | } | |
101 | ||
102 | file = tmp[0]; | |
103 | } | |
104 | ||
105 | if (file != null) { | |
106 | MetaData meta = getInfo(luid); | |
68e2c6d2 | 107 | metas.add(meta); |
b0e88ebd NR |
108 | } |
109 | ||
110 | return file; | |
111 | } | |
68e2c6d2 NR |
112 | |
113 | @Override | |
114 | public BufferedImage getCover(String luid) { | |
115 | // Retrieve it from the network if needed: | |
116 | if (lib.getInfo(luid) == null) { | |
117 | getFile(luid); | |
118 | } | |
119 | ||
120 | return lib.getCover(luid); | |
121 | } | |
122 | ||
123 | @Override | |
124 | protected void clearCache() { | |
125 | metas = null; | |
126 | lib.clearCache(); | |
127 | } | |
128 | ||
129 | @Override | |
130 | public synchronized Story save(Story story, String luid, Progress pg) | |
131 | throws IOException { | |
132 | throw new java.lang.InternalError( | |
133 | "No write support allowed on remote Libraries"); | |
134 | } | |
135 | ||
136 | @Override | |
137 | protected int getNextId() { | |
138 | throw new java.lang.InternalError( | |
139 | "No write support allowed on remote Libraries"); | |
140 | } | |
141 | ||
142 | @Override | |
143 | protected void doDelete(String luid) throws IOException { | |
144 | throw new java.lang.InternalError( | |
145 | "No write support allowed on remote Libraries"); | |
146 | } | |
147 | ||
148 | @Override | |
149 | protected Story doSave(Story story, Progress pg) throws IOException { | |
150 | throw new java.lang.InternalError( | |
151 | "No write support allowed on remote Libraries"); | |
152 | } | |
b0e88ebd | 153 | } |