GUI search: beta quality
[nikiroo-utils.git] / src / be / nikiroo / fanfix / Main.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix;
2
3import java.io.File;
4import java.io.IOException;
5import java.net.MalformedURLException;
6import java.net.URL;
91b82a5c 7import java.util.ArrayList;
f569d249 8import java.util.List;
08fe2e33
NR
9
10import be.nikiroo.fanfix.bundles.StringId;
11import be.nikiroo.fanfix.data.Chapter;
f569d249 12import be.nikiroo.fanfix.data.MetaData;
08fe2e33 13import be.nikiroo.fanfix.data.Story;
ff05b828
NR
14import be.nikiroo.fanfix.library.BasicLibrary;
15import be.nikiroo.fanfix.library.CacheLibrary;
e42573a0
NR
16import be.nikiroo.fanfix.library.LocalLibrary;
17import be.nikiroo.fanfix.library.RemoteLibrary;
18import be.nikiroo.fanfix.library.RemoteLibraryServer;
08fe2e33
NR
19import be.nikiroo.fanfix.output.BasicOutput;
20import be.nikiroo.fanfix.output.BasicOutput.OutputType;
3727aae2 21import be.nikiroo.fanfix.reader.BasicReader;
e42573a0
NR
22import be.nikiroo.fanfix.reader.Reader;
23import be.nikiroo.fanfix.reader.Reader.ReaderType;
91b82a5c 24import be.nikiroo.fanfix.searchable.BasicSearchable;
08fe2e33 25import be.nikiroo.fanfix.supported.BasicSupport;
0ffa4754 26import be.nikiroo.fanfix.supported.SupportType;
3b2b638f 27import be.nikiroo.utils.Progress;
39c3c689 28import be.nikiroo.utils.Version;
62c63b07 29import be.nikiroo.utils.serial.server.ServerObject;
08fe2e33
NR
30
31/**
32 * Main program entry point.
33 *
34 * @author niki
35 */
36public class Main {
d0114000 37 private enum MainAction {
8b153400 38 IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, SET_READER, START, VERSION, SERVER, STOP_SERVER, REMOTE, SET_SOURCE, SET_TITLE, SET_AUTHOR, SEARCH, TAG
d0114000
NR
39 }
40
08fe2e33
NR
41 /**
42 * Main program entry point.
43 * <p>
44 * Known environment variables:
45 * <ul>
d0114000 46 * <li>NOUTF: if set to 1 or 'true', the program will prefer non-unicode
08fe2e33
NR
47 * {@link String}s when possible</li>
48 * <li>CONFIG_DIR: a path where to look for the <tt>.properties</tt> files
edd46289
NR
49 * before taking the usual ones; they will also be saved/updated into this
50 * path when the program starts</li>
d0114000
NR
51 * <li>DEBUG: if set to 1 or 'true', the program will override the DEBUG_ERR
52 * configuration value with 'true'</li>
53 * </ul>
54 * <p>
55 * <ul>
56 * <li>--import [URL]: import into library</li>
57 * <li>--export [id] [output_type] [target]: export story to target</li>
58 * <li>--convert [URL] [output_type] [target] (+info): convert URL into
59 * target</li>
60 * <li>--read [id] ([chapter number]): read the given story from the library
61 * </li>
333f0e7b 62 * <li>--read-url [URL] ([chapter number]): convert on the fly and read the
d0114000 63 * story, without saving it</li>
8b153400
NR
64 * <li>--search: list the supported websites (where)</li>
65 * <li>--search [where] [keywords] (page [page]) (item [item]): search on
66 * the supported website and display the given results page of stories it
67 * found, or the story details if asked</li>
68 * <li>--tag [where]: list all the tags supported by this website</li>
69 * <li>--tag [index 1]... (page [page]) (item [item]): search for the given
70 * stories or subtags, tag by tag, and display information about a specific
71 * page of results or about a specific item if requested</li>
333f0e7b 72 * <li>--list ([type]): list the stories present in the library</li>
e10b51a2
NR
73 * <li>--set-source [id] [new source]: change the source of the given story</li>
74 * <li>--set-title [id] [new title]: change the title of the given story</li>
75 * <li>--set-author [id] [new author]: change the author of the given story</li>
c1873e56
NR
76 * <li>--set-reader [reader type]: set the reader type to CLI, TUI or LOCAL
77 * for this command</li>
39c3c689 78 * <li>--version: get the version of the program</li>
2070ced5
NR
79 * <li>--server [key] [port]: start a server on this port</li>
80 * <li>--stop-server [key] [port]: stop the running server on this port if
81 * any</li>
82 * <li>--remote [key] [host] [port]: use a the given remote library</li>
08fe2e33
NR
83 * </ul>
84 *
85 * @param args
d0114000 86 * see method description
08fe2e33
NR
87 */
88 public static void main(String[] args) {
d0114000
NR
89 String urlString = null;
90 String luid = null;
b0e88ebd 91 String sourceString = null;
e10b51a2
NR
92 String titleString = null;
93 String authorString = null;
d0114000
NR
94 String chapString = null;
95 String target = null;
2070ced5 96 String key = null;
333f0e7b 97 MainAction action = MainAction.START;
d0114000 98 Boolean plusInfo = null;
b0e88ebd
NR
99 String host = null;
100 Integer port = null;
91b82a5c
NR
101 SupportType searchOn = null;
102 String search = null;
8b153400 103 List<Integer> tags = new ArrayList<Integer>();
91b82a5c
NR
104 Integer page = null;
105 Integer item = null;
73ce17ef 106
d0114000
NR
107 boolean noMoreActions = false;
108
109 int exitCode = 0;
110 for (int i = 0; exitCode == 0 && i < args.length; i++) {
111 // Action (--) handling:
112 if (!noMoreActions && args[i].startsWith("--")) {
113 if (args[i].equals("--")) {
114 noMoreActions = true;
115 } else {
116 try {
117 action = MainAction.valueOf(args[i].substring(2)
118 .toUpperCase().replace("-", "_"));
119 } catch (Exception e) {
62c63b07
NR
120 Instance.getTraceHandler().error(
121 new IllegalArgumentException("Unknown action: "
122 + args[i], e));
d0114000
NR
123 exitCode = 255;
124 }
125 }
08fe2e33 126
d0114000
NR
127 continue;
128 }
129
130 switch (action) {
131 case IMPORT:
132 if (urlString == null) {
133 urlString = args[i];
134 } else {
135 exitCode = 255;
136 }
137 break;
138 case EXPORT:
139 if (luid == null) {
140 luid = args[i];
b0e88ebd
NR
141 } else if (sourceString == null) {
142 sourceString = args[i];
d0114000
NR
143 } else if (target == null) {
144 target = args[i];
145 } else {
146 exitCode = 255;
147 }
148 break;
149 case CONVERT:
150 if (urlString == null) {
151 urlString = args[i];
b0e88ebd
NR
152 } else if (sourceString == null) {
153 sourceString = args[i];
d0114000
NR
154 } else if (target == null) {
155 target = args[i];
156 } else if (plusInfo == null) {
157 if ("+info".equals(args[i])) {
158 plusInfo = true;
159 } else {
160 exitCode = 255;
161 }
162 } else {
163 exitCode = 255;
08fe2e33 164 }
d0114000
NR
165 break;
166 case LIST:
b0e88ebd
NR
167 if (sourceString == null) {
168 sourceString = args[i];
d0114000
NR
169 } else {
170 exitCode = 255;
08fe2e33 171 }
d0114000 172 break;
e10b51a2
NR
173 case SET_SOURCE:
174 if (luid == null) {
175 luid = args[i];
176 } else if (sourceString == null) {
177 sourceString = args[i];
178 } else {
179 exitCode = 255;
180 }
181 break;
182 case SET_TITLE:
183 if (luid == null) {
184 luid = args[i];
185 } else if (sourceString == null) {
186 titleString = args[i];
187 } else {
188 exitCode = 255;
189 }
190 break;
191 case SET_AUTHOR:
192 if (luid == null) {
193 luid = args[i];
194 } else if (sourceString == null) {
195 authorString = args[i];
196 } else {
197 exitCode = 255;
198 }
199 break;
d0114000
NR
200 case READ:
201 if (luid == null) {
202 luid = args[i];
203 } else if (chapString == null) {
204 chapString = args[i];
205 } else {
206 exitCode = 255;
08fe2e33 207 }
d0114000
NR
208 break;
209 case READ_URL:
210 if (urlString == null) {
211 urlString = args[i];
212 } else if (chapString == null) {
213 chapString = args[i];
214 } else {
215 exitCode = 255;
08fe2e33 216 }
d0114000 217 break;
91b82a5c
NR
218 case SEARCH:
219 if (searchOn == null) {
220 searchOn = SupportType.valueOfAllOkUC(args[i]);
8b153400 221
91b82a5c
NR
222 if (searchOn == null) {
223 Instance.getTraceHandler().error(
224 "Website not known: <" + args[i] + ">");
b31a0db0
NR
225 exitCode = 41;
226 break;
91b82a5c 227 }
8b153400 228
91b82a5c
NR
229 if (BasicSearchable.getSearchable(searchOn) == null) {
230 Instance.getTraceHandler().error(
231 "Website not supported: " + searchOn);
b31a0db0
NR
232 exitCode = 42;
233 break;
91b82a5c
NR
234 }
235 } else if (search == null) {
236 search = args[i];
8b153400 237 } else if (page != null && page == -1) {
91b82a5c
NR
238 try {
239 page = Integer.parseInt(args[i]);
8b153400
NR
240 } catch (Exception e) {
241 page = -2;
91b82a5c 242 }
8b153400 243 } else if (item != null && item == -1) {
91b82a5c
NR
244 try {
245 item = Integer.parseInt(args[i]);
8b153400
NR
246 } catch (Exception e) {
247 item = -2;
248 }
249 } else if (page == null || item == null) {
250 if (page == null && "page".equals(args[i])) {
251 page = -1;
252 } else if (item == null && "item".equals(args[i])) {
253 item = -1;
254 } else {
91b82a5c
NR
255 exitCode = 255;
256 }
257 } else {
258 exitCode = 255;
259 }
260 break;
8b153400 261 case TAG:
91b82a5c
NR
262 if (searchOn == null) {
263 searchOn = SupportType.valueOfAllOkUC(args[i]);
8b153400 264
91b82a5c
NR
265 if (searchOn == null) {
266 Instance.getTraceHandler().error(
267 "Website not known: <" + args[i] + ">");
268 exitCode = 255;
269 }
8b153400 270
91b82a5c
NR
271 if (BasicSearchable.getSearchable(searchOn) == null) {
272 Instance.getTraceHandler().error(
273 "Website not supported: " + searchOn);
274 exitCode = 255;
275 }
8b153400
NR
276 } else if (page == null && item == null) {
277 if ("page".equals(args[i])) {
278 page = -1;
279 } else if ("item".equals(args[i])) {
280 item = -1;
281 } else {
282 try {
283 int index = Integer.parseInt(args[i]);
284 tags.add(index);
285 } catch (NumberFormatException e) {
286 Instance.getTraceHandler().error(
287 "Invalid tag index: " + args[i]);
288 exitCode = 255;
289 }
290 }
291 } else if (page != null && page == -1) {
292 try {
293 page = Integer.parseInt(args[i]);
294 } catch (Exception e) {
295 page = -2;
296 }
297 } else if (item != null && item == -1) {
298 try {
299 item = Integer.parseInt(args[i]);
300 } catch (Exception e) {
301 item = -2;
302 }
303 } else if (page == null || item == null) {
304 if (page == null && "page".equals(args[i])) {
305 page = -1;
306 } else if (item == null && "item".equals(args[i])) {
307 item = -1;
308 } else {
309 exitCode = 255;
310 }
91b82a5c 311 } else {
8b153400 312 exitCode = 255;
91b82a5c
NR
313 }
314 break;
d0114000
NR
315 case HELP:
316 exitCode = 255;
317 break;
318 case SET_READER:
7de079f1 319 exitCode = setReaderType(args[i]);
c1873e56 320 action = MainAction.START;
d0114000 321 break;
333f0e7b
NR
322 case START:
323 exitCode = 255; // not supposed to be selected by user
324 break;
39c3c689
NR
325 case VERSION:
326 exitCode = 255; // no arguments for this option
b0e88ebd
NR
327 break;
328 case SERVER:
5e848e6a 329 case STOP_SERVER:
2070ced5
NR
330 if (key == null) {
331 key = args[i];
332 } else if (port == null) {
b0e88ebd
NR
333 port = Integer.parseInt(args[i]);
334 } else {
335 exitCode = 255;
336 }
337 break;
338 case REMOTE:
2070ced5
NR
339 if (key == null) {
340 key = args[i];
341 } else if (host == null) {
b0e88ebd
NR
342 host = args[i];
343 } else if (port == null) {
344 port = Integer.parseInt(args[i]);
ff05b828 345
2070ced5 346 BasicLibrary lib = new RemoteLibrary(key, host, port);
5895a958 347 lib = new CacheLibrary(Instance.getRemoteDir(host), lib);
ff05b828
NR
348
349 BasicReader.setDefaultLibrary(lib);
5e848e6a 350
b0e88ebd
NR
351 action = MainAction.START;
352 } else {
353 exitCode = 255;
354 }
355 break;
d0114000
NR
356 }
357 }
358
92fb0719
NR
359 final Progress mainProgress = new Progress(0, 80);
360 mainProgress.addProgressListener(new Progress.ProgressListener() {
361 private int current = mainProgress.getMin();
362
211f7ddb 363 @Override
92fb0719
NR
364 public void progress(Progress progress, String name) {
365 int diff = progress.getProgress() - current;
366 current += diff;
367
1822d603
NR
368 if (diff <= 0)
369 return;
370
92fb0719
NR
371 StringBuilder builder = new StringBuilder();
372 for (int i = 0; i < diff; i++) {
373 builder.append('.');
374 }
375
376 System.err.print(builder.toString());
377
378 if (progress.isDone()) {
379 System.err.println("");
380 }
381 }
382 });
383 Progress pg = new Progress();
384 mainProgress.addProgress(pg, mainProgress.getMax());
385
b42117f1
NR
386 VersionCheck updates = VersionCheck.check();
387 if (updates.isNewVersionAvailable()) {
388 // Sent to syserr so not to cause problem if one tries to capture a
389 // story content in text mode
390 System.err
391 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
392 System.err.println("");
393 for (Version v : updates.getNewer()) {
394 System.err.println("\tVersion " + v);
395 System.err.println("\t-------------");
396 System.err.println("");
91b82a5c
NR
397 for (String it : updates.getChanges().get(v)) {
398 System.err.println("\t- " + it);
b42117f1
NR
399 }
400 System.err.println("");
401 }
402 }
403
b31a0db0 404 if (exitCode == 0) {
d0114000
NR
405 switch (action) {
406 case IMPORT:
92fb0719 407 exitCode = imprt(urlString, pg);
b42117f1 408 updates.ok(); // we consider it read
d0114000
NR
409 break;
410 case EXPORT:
b0e88ebd 411 exitCode = export(luid, sourceString, target, pg);
b42117f1 412 updates.ok(); // we consider it read
d0114000
NR
413 break;
414 case CONVERT:
b0e88ebd 415 exitCode = convert(urlString, sourceString, target,
92fb0719 416 plusInfo == null ? false : plusInfo, pg);
b42117f1 417 updates.ok(); // we consider it read
d0114000
NR
418 break;
419 case LIST:
99ccbdf6 420 if (BasicReader.getReader() == null) {
62c63b07
NR
421 Instance.getTraceHandler()
422 .error(new Exception(
423 "No reader type has been configured"));
99ccbdf6
NR
424 exitCode = 10;
425 break;
426 }
b0e88ebd 427 exitCode = list(sourceString);
d0114000 428 break;
e10b51a2
NR
429 case SET_SOURCE:
430 try {
431 Instance.getLibrary().changeSource(luid, sourceString, pg);
432 } catch (IOException e1) {
433 Instance.getTraceHandler().error(e1);
434 exitCode = 21;
435 }
436 break;
437 case SET_TITLE:
438 try {
439 Instance.getLibrary().changeTitle(luid, titleString, pg);
440 } catch (IOException e1) {
441 Instance.getTraceHandler().error(e1);
442 exitCode = 22;
443 }
444 break;
445 case SET_AUTHOR:
446 try {
447 Instance.getLibrary().changeAuthor(luid, authorString, pg);
448 } catch (IOException e1) {
449 Instance.getTraceHandler().error(e1);
450 exitCode = 23;
451 }
452 break;
d0114000 453 case READ:
99ccbdf6 454 if (BasicReader.getReader() == null) {
62c63b07
NR
455 Instance.getTraceHandler()
456 .error(new Exception(
457 "No reader type has been configured"));
99ccbdf6
NR
458 exitCode = 10;
459 break;
460 }
d0114000
NR
461 exitCode = read(luid, chapString, true);
462 break;
463 case READ_URL:
99ccbdf6 464 if (BasicReader.getReader() == null) {
62c63b07
NR
465 Instance.getTraceHandler()
466 .error(new Exception(
467 "No reader type has been configured"));
99ccbdf6
NR
468 exitCode = 10;
469 break;
470 }
d0114000 471 exitCode = read(urlString, chapString, false);
91b82a5c
NR
472 break;
473 case SEARCH:
8b153400
NR
474 page = page == null ? 1 : page;
475 if (page < 0) {
476 Instance.getTraceHandler().error("Incorrect page number");
91b82a5c
NR
477 exitCode = 255;
478 break;
479 }
8b153400
NR
480
481 item = item == null ? 0 : item;
482 if (item < 0) {
483 Instance.getTraceHandler().error("Incorrect item number");
484 exitCode = 255;
485 break;
91b82a5c 486 }
8b153400 487
91b82a5c
NR
488 if (BasicReader.getReader() == null) {
489 Instance.getTraceHandler()
490 .error(new Exception(
491 "No reader type has been configured"));
492 exitCode = 10;
493 break;
494 }
8b153400 495
b31a0db0
NR
496 try {
497 if (searchOn == null) {
498 BasicReader.getReader().search(true);
499 } else if (search != null) {
500
8b153400 501 BasicReader.getReader().search(searchOn, search, page,
f76de465 502 item, true);
b31a0db0
NR
503 } else {
504 exitCode = 255;
8b153400 505 }
b31a0db0
NR
506 } catch (IOException e1) {
507 Instance.getTraceHandler().error(e1);
508 exitCode = 20;
91b82a5c 509 }
8b153400 510
91b82a5c 511 break;
8b153400 512 case TAG:
91b82a5c
NR
513 if (searchOn == null) {
514 exitCode = 255;
515 break;
516 }
91b82a5c 517
8b153400
NR
518 page = page == null ? 1 : page;
519 if (page < 0) {
520 Instance.getTraceHandler().error("Incorrect page number");
521 exitCode = 255;
522 break;
91b82a5c 523 }
8b153400
NR
524
525 item = item == null ? 0 : item;
526 if (item < 0) {
527 Instance.getTraceHandler().error("Incorrect item number");
528 exitCode = 255;
529 break;
530 }
531
91b82a5c
NR
532 if (BasicReader.getReader() == null) {
533 Instance.getTraceHandler()
534 .error(new Exception(
535 "No reader type has been configured"));
536 exitCode = 10;
537 break;
538 }
8b153400 539
91b82a5c 540 try {
8b153400 541 BasicReader.getReader().searchTag(searchOn, page, item,
f76de465 542 true, tags.toArray(new Integer[] {}));
91b82a5c
NR
543 } catch (IOException e1) {
544 Instance.getTraceHandler().error(e1);
545 }
8b153400 546
d0114000
NR
547 break;
548 case HELP:
549 syntax(true);
550 exitCode = 0;
551 break;
552 case SET_READER:
b0e88ebd 553 exitCode = 255;
d0114000 554 break;
39c3c689
NR
555 case VERSION:
556 System.out
557 .println(String.format("Fanfix version %s"
9fe3f177
NR
558 + "%nhttps://github.com/nikiroo/fanfix/"
559 + "%n\tWritten by Nikiroo",
39c3c689 560 Version.getCurrentVersion()));
b42117f1 561 updates.ok(); // we consider it read
39c3c689 562 break;
333f0e7b 563 case START:
99ccbdf6 564 if (BasicReader.getReader() == null) {
62c63b07
NR
565 Instance.getTraceHandler()
566 .error(new Exception(
567 "No reader type has been configured"));
99ccbdf6
NR
568 exitCode = 10;
569 break;
570 }
b0e88ebd
NR
571 BasicReader.getReader().browse(null);
572 break;
573 case SERVER:
574 if (port == null) {
575 exitCode = 255;
576 break;
577 }
578 try {
62c63b07 579 ServerObject server = new RemoteLibraryServer(key, port);
22b2b942 580 server.setTraceHandler(Instance.getTraceHandler());
edf79e5e 581 server.run();
b0e88ebd 582 } catch (IOException e) {
62c63b07 583 Instance.getTraceHandler().error(e);
b0e88ebd
NR
584 }
585 return;
5e848e6a
NR
586 case STOP_SERVER:
587 if (port == null) {
588 exitCode = 255;
589 break;
590 }
591
468b960b 592 new RemoteLibrary(key, host, port).exit();
5e848e6a 593 break;
b0e88ebd 594 case REMOTE:
99ccbdf6 595 exitCode = 255; // should not be reachable (REMOTE -> START)
333f0e7b 596 break;
08fe2e33
NR
597 }
598 }
599
350bc060
NR
600 try {
601 Instance.getTempFiles().close();
602 } catch (IOException e) {
603 Instance.getTraceHandler()
604 .error(new IOException(
605 "Cannot dispose of the temporary files", e));
2aac79c7
NR
606 }
607
08fe2e33 608 if (exitCode == 255) {
d0114000 609 syntax(false);
08fe2e33
NR
610 }
611
31e28683 612 System.exit(exitCode);
08fe2e33
NR
613 }
614
08fe2e33 615 /**
68e2c6d2 616 * Import the given resource into the {@link LocalLibrary}.
08fe2e33 617 *
d0114000 618 * @param urlString
08fe2e33 619 * the resource to import
92fb0719
NR
620 * @param pg
621 * the optional progress reporter
08fe2e33
NR
622 *
623 * @return the exit return code (0 = success)
624 */
92fb0719 625 public static int imprt(String urlString, Progress pg) {
08fe2e33 626 try {
3b2b638f
NR
627 Story story = Instance.getLibrary().imprt(
628 BasicReader.getUrl(urlString), pg);
08fe2e33
NR
629 System.out.println(story.getMeta().getLuid() + ": \""
630 + story.getMeta().getTitle() + "\" imported.");
631 } catch (IOException e) {
62c63b07 632 Instance.getTraceHandler().error(e);
08fe2e33
NR
633 return 1;
634 }
635
636 return 0;
637 }
638
639 /**
68e2c6d2
NR
640 * Export the {@link Story} from the {@link LocalLibrary} to the given
641 * target.
08fe2e33 642 *
73ce17ef 643 * @param luid
08fe2e33
NR
644 * the story LUID
645 * @param typeString
646 * the {@link OutputType} to use
647 * @param target
648 * the target
92fb0719
NR
649 * @param pg
650 * the optional progress reporter
08fe2e33
NR
651 *
652 * @return the exit return code (0 = success)
653 */
92fb0719
NR
654 public static int export(String luid, String typeString, String target,
655 Progress pg) {
e604986c 656 OutputType type = OutputType.valueOfNullOkUC(typeString, null);
08fe2e33 657 if (type == null) {
62c63b07
NR
658 Instance.getTraceHandler().error(
659 new Exception(trans(StringId.OUTPUT_DESC, typeString)));
08fe2e33
NR
660 return 1;
661 }
662
663 try {
92fb0719 664 Instance.getLibrary().export(luid, type, target, pg);
08fe2e33 665 } catch (IOException e) {
62c63b07 666 Instance.getTraceHandler().error(e);
08fe2e33
NR
667 return 4;
668 }
669
670 return 0;
671 }
672
673 /**
68e2c6d2
NR
674 * List the stories of the given source from the {@link LocalLibrary}
675 * (unless NULL is passed, in which case all stories will be listed).
08fe2e33 676 *
b0e88ebd
NR
677 * @param source
678 * the source to list the known stories of, or NULL to list all
333f0e7b 679 * stories
08fe2e33
NR
680 *
681 * @return the exit return code (0 = success)
682 */
b0e88ebd 683 private static int list(String source) {
f569d249
NR
684 List<MetaData> stories;
685 stories = BasicReader.getReader().getLibrary().getListBySource(source);
686
687 for (MetaData story : stories) {
688 String author = "";
689 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
690 author = " (" + story.getAuthor() + ")";
691 }
692
693 System.out.println(story.getLuid() + ": " + story.getTitle()
694 + author);
695 }
08fe2e33
NR
696 return 0;
697 }
698
699 /**
350bc060 700 * Start the current reader for this {@link Story}.
08fe2e33
NR
701 *
702 * @param story
68e2c6d2
NR
703 * the LUID of the {@link Story} in the {@link LocalLibrary}
704 * <b>or</b> the {@link Story} {@link URL}
d0114000 705 * @param chapString
08fe2e33
NR
706 * which {@link Chapter} to read (starting at 1), or NULL to get
707 * the {@link Story} description
708 * @param library
709 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
710 * {@link URL}
711 *
712 * @return the exit return code (0 = success)
713 */
d0114000 714 private static int read(String story, String chapString, boolean library) {
08fe2e33 715 try {
e42573a0 716 Reader reader = BasicReader.getReader();
08fe2e33 717 if (library) {
bc2ea776 718 reader.setMeta(story);
08fe2e33 719 } else {
bc2ea776 720 reader.setMeta(BasicReader.getUrl(story), null);
08fe2e33
NR
721 }
722
d0114000
NR
723 if (chapString != null) {
724 try {
bc2ea776 725 reader.setChapter(Integer.parseInt(chapString));
350bc060 726 reader.read(true);
d0114000 727 } catch (NumberFormatException e) {
62c63b07
NR
728 Instance.getTraceHandler().error(
729 new IOException("Chapter number cannot be parsed: "
730 + chapString, e));
d0114000
NR
731 return 2;
732 }
08fe2e33 733 } else {
350bc060 734 reader.read(true);
08fe2e33
NR
735 }
736 } catch (IOException e) {
62c63b07 737 Instance.getTraceHandler().error(e);
08fe2e33
NR
738 return 1;
739 }
740
741 return 0;
742 }
743
744 /**
745 * Convert the {@link Story} into another format.
746 *
d0114000 747 * @param urlString
08fe2e33
NR
748 * the source {@link Story} to convert
749 * @param typeString
750 * the {@link OutputType} to convert to
d0114000 751 * @param target
08fe2e33
NR
752 * the target file
753 * @param infoCover
754 * TRUE to also export the cover and info file, even if the given
755 * {@link OutputType} does not usually save them
92fb0719
NR
756 * @param pg
757 * the optional progress reporter
08fe2e33
NR
758 *
759 * @return the exit return code (0 = success)
760 */
f7460e4c 761 public static int convert(String urlString, String typeString,
92fb0719 762 String target, boolean infoCover, Progress pg) {
08fe2e33
NR
763 int exitCode = 0;
764
826e4569 765 Instance.getTraceHandler().trace("Convert: " + urlString);
d0114000 766 String sourceName = urlString;
08fe2e33 767 try {
3b2b638f 768 URL source = BasicReader.getUrl(urlString);
08fe2e33
NR
769 sourceName = source.toString();
770 if (source.toString().startsWith("file://")) {
771 sourceName = sourceName.substring("file://".length());
772 }
773
e604986c 774 OutputType type = OutputType.valueOfAllOkUC(typeString, null);
08fe2e33 775 if (type == null) {
62c63b07
NR
776 Instance.getTraceHandler().error(
777 new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE,
778 typeString)));
08fe2e33
NR
779
780 exitCode = 2;
781 } else {
782 try {
783 BasicSupport support = BasicSupport.getSupport(source);
333f0e7b 784
08fe2e33 785 if (support != null) {
350bc060
NR
786 Instance.getTraceHandler().trace(
787 "Support found: " + support.getClass());
bee7dffe
NR
788 Progress pgIn = new Progress();
789 Progress pgOut = new Progress();
790 if (pg != null) {
791 pg.setMax(2);
792 pg.addProgress(pgIn, 1);
793 pg.addProgress(pgOut, 1);
794 }
08fe2e33 795
0ffa4754 796 Story story = support.process(pgIn);
08fe2e33 797 try {
d0114000 798 target = new File(target).getAbsolutePath();
925298fd
NR
799 BasicOutput.getOutput(type, infoCover, infoCover)
800 .process(story, target, pgOut);
08fe2e33 801 } catch (IOException e) {
62c63b07
NR
802 Instance.getTraceHandler().error(
803 new IOException(trans(StringId.ERR_SAVING,
804 target), e));
08fe2e33
NR
805 exitCode = 5;
806 }
807 } else {
62c63b07
NR
808 Instance.getTraceHandler().error(
809 new IOException(trans(
810 StringId.ERR_NOT_SUPPORTED, source)));
08fe2e33
NR
811
812 exitCode = 4;
813 }
814 } catch (IOException e) {
62c63b07
NR
815 Instance.getTraceHandler().error(
816 new IOException(trans(StringId.ERR_LOADING,
817 sourceName), e));
08fe2e33
NR
818 exitCode = 3;
819 }
820 }
821 } catch (MalformedURLException e) {
62c63b07
NR
822 Instance.getTraceHandler()
823 .error(new IOException(trans(StringId.ERR_BAD_URL,
824 sourceName), e));
08fe2e33
NR
825 exitCode = 1;
826 }
827
828 return exitCode;
829 }
830
831 /**
832 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
833 *
834 * @param id
835 * the ID to translate
836 *
837 * @return the translated result
838 */
839 private static String trans(StringId id, Object... params) {
840 return Instance.getTrans().getString(id, params);
841 }
842
843 /**
d0114000
NR
844 * Display the correct syntax of the program to the user to stdout, or an
845 * error message if the syntax used was wrong on stderr.
846 *
847 * @param showHelp
848 * TRUE to show the syntax help, FALSE to show "syntax error"
08fe2e33 849 */
d0114000
NR
850 private static void syntax(boolean showHelp) {
851 if (showHelp) {
852 StringBuilder builder = new StringBuilder();
853 for (SupportType type : SupportType.values()) {
854 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
855 type.getDesc()));
856 builder.append('\n');
857 }
08fe2e33 858
d0114000
NR
859 String typesIn = builder.toString();
860 builder.setLength(0);
08fe2e33 861
d0114000
NR
862 for (OutputType type : OutputType.values()) {
863 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
4d205683 864 type.getDesc(true)));
d0114000
NR
865 builder.append('\n');
866 }
08fe2e33 867
d0114000 868 String typesOut = builder.toString();
08fe2e33 869
d0114000
NR
870 System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut));
871 } else {
872 System.err.println(trans(StringId.ERR_SYNTAX));
873 }
874 }
875
876 /**
877 * Set the default reader type for this session only (it can be changed in
878 * the configuration file, too, but this value will override it).
879 *
880 * @param readerTypeString
881 * the type
882 */
883 private static int setReaderType(String readerTypeString) {
884 try {
7de079f1
NR
885 ReaderType readerType = ReaderType.valueOf(readerTypeString
886 .toUpperCase());
d0114000
NR
887 BasicReader.setDefaultReaderType(readerType);
888 return 0;
889 } catch (IllegalArgumentException e) {
62c63b07
NR
890 Instance.getTraceHandler().error(
891 new IOException("Unknown reader type: " + readerTypeString,
892 e));
d0114000
NR
893 return 1;
894 }
08fe2e33
NR
895 }
896}