link gui frame to search
[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] + ">");
225 exitCode = 255;
226 }
8b153400 227
91b82a5c
NR
228 if (BasicSearchable.getSearchable(searchOn) == null) {
229 Instance.getTraceHandler().error(
230 "Website not supported: " + searchOn);
231 exitCode = 255;
232 }
233 } else if (search == null) {
234 search = args[i];
8b153400 235 } else if (page != null && page == -1) {
91b82a5c
NR
236 try {
237 page = Integer.parseInt(args[i]);
8b153400
NR
238 } catch (Exception e) {
239 page = -2;
91b82a5c 240 }
8b153400 241 } else if (item != null && item == -1) {
91b82a5c
NR
242 try {
243 item = Integer.parseInt(args[i]);
8b153400
NR
244 } catch (Exception e) {
245 item = -2;
246 }
247 } else if (page == null || item == null) {
248 if (page == null && "page".equals(args[i])) {
249 page = -1;
250 } else if (item == null && "item".equals(args[i])) {
251 item = -1;
252 } else {
91b82a5c
NR
253 exitCode = 255;
254 }
255 } else {
256 exitCode = 255;
257 }
258 break;
8b153400 259 case TAG:
91b82a5c
NR
260 if (searchOn == null) {
261 searchOn = SupportType.valueOfAllOkUC(args[i]);
8b153400 262
91b82a5c
NR
263 if (searchOn == null) {
264 Instance.getTraceHandler().error(
265 "Website not known: <" + args[i] + ">");
266 exitCode = 255;
267 }
8b153400 268
91b82a5c
NR
269 if (BasicSearchable.getSearchable(searchOn) == null) {
270 Instance.getTraceHandler().error(
271 "Website not supported: " + searchOn);
272 exitCode = 255;
273 }
8b153400
NR
274 } else if (page == null && item == null) {
275 if ("page".equals(args[i])) {
276 page = -1;
277 } else if ("item".equals(args[i])) {
278 item = -1;
279 } else {
280 try {
281 int index = Integer.parseInt(args[i]);
282 tags.add(index);
283 } catch (NumberFormatException e) {
284 Instance.getTraceHandler().error(
285 "Invalid tag index: " + args[i]);
286 exitCode = 255;
287 }
288 }
289 } else if (page != null && page == -1) {
290 try {
291 page = Integer.parseInt(args[i]);
292 } catch (Exception e) {
293 page = -2;
294 }
295 } else if (item != null && item == -1) {
296 try {
297 item = Integer.parseInt(args[i]);
298 } catch (Exception e) {
299 item = -2;
300 }
301 } else if (page == null || item == null) {
302 if (page == null && "page".equals(args[i])) {
303 page = -1;
304 } else if (item == null && "item".equals(args[i])) {
305 item = -1;
306 } else {
307 exitCode = 255;
308 }
91b82a5c 309 } else {
8b153400 310 exitCode = 255;
91b82a5c
NR
311 }
312 break;
d0114000
NR
313 case HELP:
314 exitCode = 255;
315 break;
316 case SET_READER:
7de079f1 317 exitCode = setReaderType(args[i]);
c1873e56 318 action = MainAction.START;
d0114000 319 break;
333f0e7b
NR
320 case START:
321 exitCode = 255; // not supposed to be selected by user
322 break;
39c3c689
NR
323 case VERSION:
324 exitCode = 255; // no arguments for this option
b0e88ebd
NR
325 break;
326 case SERVER:
5e848e6a 327 case STOP_SERVER:
2070ced5
NR
328 if (key == null) {
329 key = args[i];
330 } else if (port == null) {
b0e88ebd
NR
331 port = Integer.parseInt(args[i]);
332 } else {
333 exitCode = 255;
334 }
335 break;
336 case REMOTE:
2070ced5
NR
337 if (key == null) {
338 key = args[i];
339 } else if (host == null) {
b0e88ebd
NR
340 host = args[i];
341 } else if (port == null) {
342 port = Integer.parseInt(args[i]);
ff05b828 343
2070ced5 344 BasicLibrary lib = new RemoteLibrary(key, host, port);
5895a958 345 lib = new CacheLibrary(Instance.getRemoteDir(host), lib);
ff05b828
NR
346
347 BasicReader.setDefaultLibrary(lib);
5e848e6a 348
b0e88ebd
NR
349 action = MainAction.START;
350 } else {
351 exitCode = 255;
352 }
353 break;
d0114000
NR
354 }
355 }
356
92fb0719
NR
357 final Progress mainProgress = new Progress(0, 80);
358 mainProgress.addProgressListener(new Progress.ProgressListener() {
359 private int current = mainProgress.getMin();
360
211f7ddb 361 @Override
92fb0719
NR
362 public void progress(Progress progress, String name) {
363 int diff = progress.getProgress() - current;
364 current += diff;
365
1822d603
NR
366 if (diff <= 0)
367 return;
368
92fb0719
NR
369 StringBuilder builder = new StringBuilder();
370 for (int i = 0; i < diff; i++) {
371 builder.append('.');
372 }
373
374 System.err.print(builder.toString());
375
376 if (progress.isDone()) {
377 System.err.println("");
378 }
379 }
380 });
381 Progress pg = new Progress();
382 mainProgress.addProgress(pg, mainProgress.getMax());
383
b42117f1
NR
384 VersionCheck updates = VersionCheck.check();
385 if (updates.isNewVersionAvailable()) {
386 // Sent to syserr so not to cause problem if one tries to capture a
387 // story content in text mode
388 System.err
389 .println("A new version of the program is available at https://github.com/nikiroo/fanfix/releases");
390 System.err.println("");
391 for (Version v : updates.getNewer()) {
392 System.err.println("\tVersion " + v);
393 System.err.println("\t-------------");
394 System.err.println("");
91b82a5c
NR
395 for (String it : updates.getChanges().get(v)) {
396 System.err.println("\t- " + it);
b42117f1
NR
397 }
398 System.err.println("");
399 }
400 }
401
d0114000
NR
402 if (exitCode != 255) {
403 switch (action) {
404 case IMPORT:
92fb0719 405 exitCode = imprt(urlString, pg);
b42117f1 406 updates.ok(); // we consider it read
d0114000
NR
407 break;
408 case EXPORT:
b0e88ebd 409 exitCode = export(luid, sourceString, target, pg);
b42117f1 410 updates.ok(); // we consider it read
d0114000
NR
411 break;
412 case CONVERT:
b0e88ebd 413 exitCode = convert(urlString, sourceString, target,
92fb0719 414 plusInfo == null ? false : plusInfo, pg);
b42117f1 415 updates.ok(); // we consider it read
d0114000
NR
416 break;
417 case LIST:
99ccbdf6 418 if (BasicReader.getReader() == null) {
62c63b07
NR
419 Instance.getTraceHandler()
420 .error(new Exception(
421 "No reader type has been configured"));
99ccbdf6
NR
422 exitCode = 10;
423 break;
424 }
b0e88ebd 425 exitCode = list(sourceString);
d0114000 426 break;
e10b51a2
NR
427 case SET_SOURCE:
428 try {
429 Instance.getLibrary().changeSource(luid, sourceString, pg);
430 } catch (IOException e1) {
431 Instance.getTraceHandler().error(e1);
432 exitCode = 21;
433 }
434 break;
435 case SET_TITLE:
436 try {
437 Instance.getLibrary().changeTitle(luid, titleString, pg);
438 } catch (IOException e1) {
439 Instance.getTraceHandler().error(e1);
440 exitCode = 22;
441 }
442 break;
443 case SET_AUTHOR:
444 try {
445 Instance.getLibrary().changeAuthor(luid, authorString, pg);
446 } catch (IOException e1) {
447 Instance.getTraceHandler().error(e1);
448 exitCode = 23;
449 }
450 break;
d0114000 451 case READ:
99ccbdf6 452 if (BasicReader.getReader() == null) {
62c63b07
NR
453 Instance.getTraceHandler()
454 .error(new Exception(
455 "No reader type has been configured"));
99ccbdf6
NR
456 exitCode = 10;
457 break;
458 }
d0114000
NR
459 exitCode = read(luid, chapString, true);
460 break;
461 case READ_URL:
99ccbdf6 462 if (BasicReader.getReader() == null) {
62c63b07
NR
463 Instance.getTraceHandler()
464 .error(new Exception(
465 "No reader type has been configured"));
99ccbdf6
NR
466 exitCode = 10;
467 break;
468 }
d0114000 469 exitCode = read(urlString, chapString, false);
91b82a5c
NR
470 break;
471 case SEARCH:
8b153400
NR
472 page = page == null ? 1 : page;
473 if (page < 0) {
474 Instance.getTraceHandler().error("Incorrect page number");
91b82a5c
NR
475 exitCode = 255;
476 break;
477 }
8b153400
NR
478
479 item = item == null ? 0 : item;
480 if (item < 0) {
481 Instance.getTraceHandler().error("Incorrect item number");
482 exitCode = 255;
483 break;
91b82a5c 484 }
8b153400 485
91b82a5c
NR
486 if (BasicReader.getReader() == null) {
487 Instance.getTraceHandler()
488 .error(new Exception(
489 "No reader type has been configured"));
490 exitCode = 10;
491 break;
492 }
8b153400
NR
493
494 if (searchOn == null || search == null) {
495 // TODO: do on reader!!!
496 for (SupportType type : SupportType.values()) {
497 if (BasicSearchable.getSearchable(type) != null) {
498 System.out.println(type);
499 }
500 }
501 } else {
502 try {
503 BasicReader.getReader().search(searchOn, search, page,
504 item);
505 } catch (IOException e1) {
506 Instance.getTraceHandler().error(e1);
507 }
91b82a5c 508 }
8b153400 509
91b82a5c 510 break;
8b153400 511 case TAG:
91b82a5c
NR
512 if (searchOn == null) {
513 exitCode = 255;
514 break;
515 }
91b82a5c 516
8b153400
NR
517 page = page == null ? 1 : page;
518 if (page < 0) {
519 Instance.getTraceHandler().error("Incorrect page number");
520 exitCode = 255;
521 break;
91b82a5c 522 }
8b153400
NR
523
524 item = item == null ? 0 : item;
525 if (item < 0) {
526 Instance.getTraceHandler().error("Incorrect item number");
527 exitCode = 255;
528 break;
529 }
530
91b82a5c
NR
531 if (BasicReader.getReader() == null) {
532 Instance.getTraceHandler()
533 .error(new Exception(
534 "No reader type has been configured"));
535 exitCode = 10;
536 break;
537 }
8b153400 538
91b82a5c 539 try {
8b153400
NR
540 BasicReader.getReader().searchTag(searchOn, page, item,
541 tags.toArray(new Integer[] {}));
91b82a5c
NR
542 } catch (IOException e1) {
543 Instance.getTraceHandler().error(e1);
544 }
8b153400 545
d0114000
NR
546 break;
547 case HELP:
548 syntax(true);
549 exitCode = 0;
550 break;
551 case SET_READER:
b0e88ebd 552 exitCode = 255;
d0114000 553 break;
39c3c689
NR
554 case VERSION:
555 System.out
556 .println(String.format("Fanfix version %s"
9fe3f177
NR
557 + "%nhttps://github.com/nikiroo/fanfix/"
558 + "%n\tWritten by Nikiroo",
39c3c689 559 Version.getCurrentVersion()));
b42117f1 560 updates.ok(); // we consider it read
39c3c689 561 break;
333f0e7b 562 case START:
99ccbdf6 563 if (BasicReader.getReader() == null) {
62c63b07
NR
564 Instance.getTraceHandler()
565 .error(new Exception(
566 "No reader type has been configured"));
99ccbdf6
NR
567 exitCode = 10;
568 break;
569 }
b0e88ebd
NR
570 BasicReader.getReader().browse(null);
571 break;
572 case SERVER:
573 if (port == null) {
574 exitCode = 255;
575 break;
576 }
577 try {
62c63b07 578 ServerObject server = new RemoteLibraryServer(key, port);
22b2b942 579 server.setTraceHandler(Instance.getTraceHandler());
edf79e5e 580 server.run();
b0e88ebd 581 } catch (IOException e) {
62c63b07 582 Instance.getTraceHandler().error(e);
b0e88ebd
NR
583 }
584 return;
5e848e6a
NR
585 case STOP_SERVER:
586 if (port == null) {
587 exitCode = 255;
588 break;
589 }
590
468b960b 591 new RemoteLibrary(key, host, port).exit();
5e848e6a 592 break;
b0e88ebd 593 case REMOTE:
99ccbdf6 594 exitCode = 255; // should not be reachable (REMOTE -> START)
333f0e7b 595 break;
08fe2e33
NR
596 }
597 }
598
350bc060
NR
599 try {
600 Instance.getTempFiles().close();
601 } catch (IOException e) {
602 Instance.getTraceHandler()
603 .error(new IOException(
604 "Cannot dispose of the temporary files", e));
2aac79c7
NR
605 }
606
08fe2e33 607 if (exitCode == 255) {
d0114000 608 syntax(false);
08fe2e33
NR
609 }
610
31e28683 611 System.exit(exitCode);
08fe2e33
NR
612 }
613
08fe2e33 614 /**
68e2c6d2 615 * Import the given resource into the {@link LocalLibrary}.
08fe2e33 616 *
d0114000 617 * @param urlString
08fe2e33 618 * the resource to import
92fb0719
NR
619 * @param pg
620 * the optional progress reporter
08fe2e33
NR
621 *
622 * @return the exit return code (0 = success)
623 */
92fb0719 624 public static int imprt(String urlString, Progress pg) {
08fe2e33 625 try {
3b2b638f
NR
626 Story story = Instance.getLibrary().imprt(
627 BasicReader.getUrl(urlString), pg);
08fe2e33
NR
628 System.out.println(story.getMeta().getLuid() + ": \""
629 + story.getMeta().getTitle() + "\" imported.");
630 } catch (IOException e) {
62c63b07 631 Instance.getTraceHandler().error(e);
08fe2e33
NR
632 return 1;
633 }
634
635 return 0;
636 }
637
638 /**
68e2c6d2
NR
639 * Export the {@link Story} from the {@link LocalLibrary} to the given
640 * target.
08fe2e33 641 *
73ce17ef 642 * @param luid
08fe2e33
NR
643 * the story LUID
644 * @param typeString
645 * the {@link OutputType} to use
646 * @param target
647 * the target
92fb0719
NR
648 * @param pg
649 * the optional progress reporter
08fe2e33
NR
650 *
651 * @return the exit return code (0 = success)
652 */
92fb0719
NR
653 public static int export(String luid, String typeString, String target,
654 Progress pg) {
e604986c 655 OutputType type = OutputType.valueOfNullOkUC(typeString, null);
08fe2e33 656 if (type == null) {
62c63b07
NR
657 Instance.getTraceHandler().error(
658 new Exception(trans(StringId.OUTPUT_DESC, typeString)));
08fe2e33
NR
659 return 1;
660 }
661
662 try {
92fb0719 663 Instance.getLibrary().export(luid, type, target, pg);
08fe2e33 664 } catch (IOException e) {
62c63b07 665 Instance.getTraceHandler().error(e);
08fe2e33
NR
666 return 4;
667 }
668
669 return 0;
670 }
671
672 /**
68e2c6d2
NR
673 * List the stories of the given source from the {@link LocalLibrary}
674 * (unless NULL is passed, in which case all stories will be listed).
08fe2e33 675 *
b0e88ebd
NR
676 * @param source
677 * the source to list the known stories of, or NULL to list all
333f0e7b 678 * stories
08fe2e33
NR
679 *
680 * @return the exit return code (0 = success)
681 */
b0e88ebd 682 private static int list(String source) {
f569d249
NR
683 List<MetaData> stories;
684 stories = BasicReader.getReader().getLibrary().getListBySource(source);
685
686 for (MetaData story : stories) {
687 String author = "";
688 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
689 author = " (" + story.getAuthor() + ")";
690 }
691
692 System.out.println(story.getLuid() + ": " + story.getTitle()
693 + author);
694 }
08fe2e33
NR
695 return 0;
696 }
697
698 /**
350bc060 699 * Start the current reader for this {@link Story}.
08fe2e33
NR
700 *
701 * @param story
68e2c6d2
NR
702 * the LUID of the {@link Story} in the {@link LocalLibrary}
703 * <b>or</b> the {@link Story} {@link URL}
d0114000 704 * @param chapString
08fe2e33
NR
705 * which {@link Chapter} to read (starting at 1), or NULL to get
706 * the {@link Story} description
707 * @param library
708 * TRUE if the source is the {@link Story} LUID, FALSE if it is a
709 * {@link URL}
710 *
711 * @return the exit return code (0 = success)
712 */
d0114000 713 private static int read(String story, String chapString, boolean library) {
08fe2e33 714 try {
e42573a0 715 Reader reader = BasicReader.getReader();
08fe2e33 716 if (library) {
bc2ea776 717 reader.setMeta(story);
08fe2e33 718 } else {
bc2ea776 719 reader.setMeta(BasicReader.getUrl(story), null);
08fe2e33
NR
720 }
721
d0114000
NR
722 if (chapString != null) {
723 try {
bc2ea776 724 reader.setChapter(Integer.parseInt(chapString));
350bc060 725 reader.read(true);
d0114000 726 } catch (NumberFormatException e) {
62c63b07
NR
727 Instance.getTraceHandler().error(
728 new IOException("Chapter number cannot be parsed: "
729 + chapString, e));
d0114000
NR
730 return 2;
731 }
08fe2e33 732 } else {
350bc060 733 reader.read(true);
08fe2e33
NR
734 }
735 } catch (IOException e) {
62c63b07 736 Instance.getTraceHandler().error(e);
08fe2e33
NR
737 return 1;
738 }
739
740 return 0;
741 }
742
743 /**
744 * Convert the {@link Story} into another format.
745 *
d0114000 746 * @param urlString
08fe2e33
NR
747 * the source {@link Story} to convert
748 * @param typeString
749 * the {@link OutputType} to convert to
d0114000 750 * @param target
08fe2e33
NR
751 * the target file
752 * @param infoCover
753 * TRUE to also export the cover and info file, even if the given
754 * {@link OutputType} does not usually save them
92fb0719
NR
755 * @param pg
756 * the optional progress reporter
08fe2e33
NR
757 *
758 * @return the exit return code (0 = success)
759 */
f7460e4c 760 public static int convert(String urlString, String typeString,
92fb0719 761 String target, boolean infoCover, Progress pg) {
08fe2e33
NR
762 int exitCode = 0;
763
826e4569 764 Instance.getTraceHandler().trace("Convert: " + urlString);
d0114000 765 String sourceName = urlString;
08fe2e33 766 try {
3b2b638f 767 URL source = BasicReader.getUrl(urlString);
08fe2e33
NR
768 sourceName = source.toString();
769 if (source.toString().startsWith("file://")) {
770 sourceName = sourceName.substring("file://".length());
771 }
772
e604986c 773 OutputType type = OutputType.valueOfAllOkUC(typeString, null);
08fe2e33 774 if (type == null) {
62c63b07
NR
775 Instance.getTraceHandler().error(
776 new IOException(trans(StringId.ERR_BAD_OUTPUT_TYPE,
777 typeString)));
08fe2e33
NR
778
779 exitCode = 2;
780 } else {
781 try {
782 BasicSupport support = BasicSupport.getSupport(source);
333f0e7b 783
08fe2e33 784 if (support != null) {
350bc060
NR
785 Instance.getTraceHandler().trace(
786 "Support found: " + support.getClass());
bee7dffe
NR
787 Progress pgIn = new Progress();
788 Progress pgOut = new Progress();
789 if (pg != null) {
790 pg.setMax(2);
791 pg.addProgress(pgIn, 1);
792 pg.addProgress(pgOut, 1);
793 }
08fe2e33 794
0ffa4754 795 Story story = support.process(pgIn);
08fe2e33 796 try {
d0114000 797 target = new File(target).getAbsolutePath();
925298fd
NR
798 BasicOutput.getOutput(type, infoCover, infoCover)
799 .process(story, target, pgOut);
08fe2e33 800 } catch (IOException e) {
62c63b07
NR
801 Instance.getTraceHandler().error(
802 new IOException(trans(StringId.ERR_SAVING,
803 target), e));
08fe2e33
NR
804 exitCode = 5;
805 }
806 } else {
62c63b07
NR
807 Instance.getTraceHandler().error(
808 new IOException(trans(
809 StringId.ERR_NOT_SUPPORTED, source)));
08fe2e33
NR
810
811 exitCode = 4;
812 }
813 } catch (IOException e) {
62c63b07
NR
814 Instance.getTraceHandler().error(
815 new IOException(trans(StringId.ERR_LOADING,
816 sourceName), e));
08fe2e33
NR
817 exitCode = 3;
818 }
819 }
820 } catch (MalformedURLException e) {
62c63b07
NR
821 Instance.getTraceHandler()
822 .error(new IOException(trans(StringId.ERR_BAD_URL,
823 sourceName), e));
08fe2e33
NR
824 exitCode = 1;
825 }
826
827 return exitCode;
828 }
829
830 /**
831 * Simple shortcut method to call {link Instance#getTrans()#getString()}.
832 *
833 * @param id
834 * the ID to translate
835 *
836 * @return the translated result
837 */
838 private static String trans(StringId id, Object... params) {
839 return Instance.getTrans().getString(id, params);
840 }
841
842 /**
d0114000
NR
843 * Display the correct syntax of the program to the user to stdout, or an
844 * error message if the syntax used was wrong on stderr.
845 *
846 * @param showHelp
847 * TRUE to show the syntax help, FALSE to show "syntax error"
08fe2e33 848 */
d0114000
NR
849 private static void syntax(boolean showHelp) {
850 if (showHelp) {
851 StringBuilder builder = new StringBuilder();
852 for (SupportType type : SupportType.values()) {
853 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
854 type.getDesc()));
855 builder.append('\n');
856 }
08fe2e33 857
d0114000
NR
858 String typesIn = builder.toString();
859 builder.setLength(0);
08fe2e33 860
d0114000
NR
861 for (OutputType type : OutputType.values()) {
862 builder.append(trans(StringId.ERR_SYNTAX_TYPE, type.toString(),
4d205683 863 type.getDesc(true)));
d0114000
NR
864 builder.append('\n');
865 }
08fe2e33 866
d0114000 867 String typesOut = builder.toString();
08fe2e33 868
d0114000
NR
869 System.out.println(trans(StringId.HELP_SYNTAX, typesIn, typesOut));
870 } else {
871 System.err.println(trans(StringId.ERR_SYNTAX));
872 }
873 }
874
875 /**
876 * Set the default reader type for this session only (it can be changed in
877 * the configuration file, too, but this value will override it).
878 *
879 * @param readerTypeString
880 * the type
881 */
882 private static int setReaderType(String readerTypeString) {
883 try {
7de079f1
NR
884 ReaderType readerType = ReaderType.valueOf(readerTypeString
885 .toUpperCase());
d0114000
NR
886 BasicReader.setDefaultReaderType(readerType);
887 return 0;
888 } catch (IllegalArgumentException e) {
62c63b07
NR
889 Instance.getTraceHandler().error(
890 new IOException("Unknown reader type: " + readerTypeString,
891 e));
d0114000
NR
892 return 1;
893 }
08fe2e33
NR
894 }
895}