+
+ List<Object> list = new ArrayList<Object>();
+ for (Object obj = send(null); obj != null; obj = send(null)) {
+ list.add(obj);
+ pg.add(1);
+ }
+
+ result[0] = RemoteLibraryServer.rebuildStory(list);
+ pg.done();
+ }
+
+ @Override
+ protected void onError(Exception e) {
+ if (e instanceof SSLException) {
+ Instance.getTraceHandler().error(
+ "Connection refused (bad key)");
+ } else {
+ Instance.getTraceHandler().error(e);
+ }
+ }
+ }.connect();
+ } catch (Exception e) {
+ Instance.getTraceHandler().error(e);
+ }
+
+ return result[0];
+ }
+
+ @Override
+ public synchronized Story save(final Story story, final String luid,
+ Progress pg) throws IOException {
+ if (!check()) {
+ return null;
+ }
+
+ final String[] luidSaved = new String[1];
+ Progress pgSave = new Progress();
+ Progress pgRefresh = new Progress();
+ if (pg == null) {
+ pg = new Progress();
+ }
+
+ pg.setMinMax(0, 10);
+ pg.addProgress(pgSave, 9);
+ pg.addProgress(pgRefresh, 1);
+
+ final Progress pgF = pgSave;
+
+ new ConnectActionClientObject(host, port, key) {
+ @Override
+ public void action(Version serverVersion) throws Exception {
+ Progress pg = pgF;
+ if (story.getMeta().getWords() <= Integer.MAX_VALUE) {
+ pg.setMinMax(0, (int) story.getMeta().getWords());
+ }
+
+ send(new Object[] { subkey, "SAVE_STORY", luid });
+
+ List<Object> list = RemoteLibraryServer.breakStory(story);
+ for (Object obj : list) {
+ send(obj);
+ pg.add(1);
+ }
+
+ luidSaved[0] = (String) send(null);
+
+ pg.done();
+ }
+
+ @Override
+ protected void onError(Exception e) {
+ if (e instanceof SSLException) {
+ Instance.getTraceHandler().error(
+ "Connection refused (bad key)");
+ } else {
+ Instance.getTraceHandler().error(e);
+ }
+ }
+ }.connect();
+
+ // because the meta changed:
+ MetaData meta = getInfo(luidSaved[0]);
+ if (story.getMeta().getClass() != null) {
+ // If already available locally:
+ meta.setCover(story.getMeta().getCover());
+ } else {
+ // If required:
+ meta.setCover(getCover(meta.getLuid()));
+ }
+ story.setMeta(meta);
+
+ pg.done();
+
+ return story;
+ }
+
+ @Override
+ public synchronized void delete(final String luid) throws IOException {
+ if (!check()) {
+ throw new IOException("Library not ready");
+ }
+
+ new ConnectActionClientObject(host, port, key) {
+ @Override
+ public void action(Version serverVersion) throws Exception {
+ send(new Object[] { subkey, "DELETE_STORY", luid });
+ }
+
+ @Override
+ protected void onError(Exception e) {
+ if (e instanceof SSLException) {
+ Instance.getTraceHandler().error(
+ "Connection refused (bad key)");
+ } else {
+ Instance.getTraceHandler().error(e);
+ }