From e97a38b102e338dae016837e301dd7537825de7f Mon Sep 17 00:00:00 2001
From: Owen <owenca@users.noreply.github.com>
Date: Thu, 11 Jan 2018 16:55:29 +0000
Subject: [PATCH] Icon-O-Matic: convert IOM to HVIF/SVG from command line
Usage: Icon-O-Matic -[h|s] -o output_file IOM_file
Returns: 0 if successful; 1 otherwise
---
src/apps/icon-o-matic/IconEditorApp.cpp | 119 +++++++++++++++++++++++++++++---
src/apps/icon-o-matic/IconEditorApp.h | 2 +
src/apps/icon-o-matic/main.cpp | 4 +-
3 files changed, 116 insertions(+), 9 deletions(-)
diff --git a/src/apps/icon-o-matic/IconEditorApp.cpp b/src/apps/icon-o-matic/IconEditorApp.cpp
index d667ce9..8a6cb06 100644
a
|
b
|
|
6 | 6 | |
7 | 7 | #include "IconEditorApp.h" |
8 | 8 | |
| 9 | #include "Document.h" |
| 10 | #include "FlatIconExporter.h" |
| 11 | #include "Icon.h" |
| 12 | #include "MessageImporter.h" |
| 13 | #include "SimpleFileSaver.h" |
| 14 | #include "SVGExporter.h" |
| 15 | |
9 | 16 | #include <new> |
10 | 17 | #include <stdio.h> |
11 | 18 | #include <string.h> |
… |
… |
|
32 | 39 | #define B_TRANSLATION_CONTEXT "Icon-O-Matic-Main" |
33 | 40 | |
34 | 41 | |
| 42 | extern int exitCode; |
| 43 | |
| 44 | |
35 | 45 | using std::nothrow; |
36 | 46 | |
37 | 47 | static const char* kAppSig = "application/x-vnd.haiku-icon_o_matic"; |
38 | 48 | |
39 | 49 | static const float kWindowOffset = 20; |
40 | 50 | |
| 51 | static bool isCLI; |
| 52 | |
41 | 53 | |
42 | 54 | IconEditorApp::IconEditorApp() |
43 | 55 | : |
44 | 56 | BApplication(kAppSig), |
45 | | fWindowCount(0), |
46 | | fLastWindowFrame(50, 50, 900, 750), |
47 | 57 | |
48 | 58 | fOpenPanel(NULL), |
49 | | fSavePanel(NULL), |
| 59 | fSavePanel(NULL) |
| 60 | { |
| 61 | } |
50 | 62 | |
51 | | fLastOpenPath(""), |
52 | | fLastSavePath(""), |
53 | | fLastExportPath("") |
| 63 | |
| 64 | void |
| 65 | IconEditorApp::_Init() |
54 | 66 | { |
| 67 | fWindowCount = 0; |
| 68 | fLastWindowFrame.Set(50, 50, 900, 750); |
| 69 | |
| 70 | fLastOpenPath = ""; |
| 71 | fLastSavePath = ""; |
| 72 | fLastExportPath = ""; |
| 73 | |
55 | 74 | // create file panels |
56 | 75 | BMessenger messenger(this, this); |
57 | 76 | BMessage message(B_REFS_RECEIVED); |
… |
… |
IconEditorApp::IconEditorApp()
|
68 | 87 | |
69 | 88 | IconEditorApp::~IconEditorApp() |
70 | 89 | { |
71 | | delete fOpenPanel; |
72 | | delete fSavePanel; |
| 90 | if (fOpenPanel != NULL) |
| 91 | delete fOpenPanel; |
| 92 | |
| 93 | if (fSavePanel != NULL) |
| 94 | delete fSavePanel; |
73 | 95 | } |
74 | 96 | |
75 | 97 | |
… |
… |
IconEditorApp::~IconEditorApp()
|
79 | 101 | bool |
80 | 102 | IconEditorApp::QuitRequested() |
81 | 103 | { |
| 104 | if (isCLI) |
| 105 | return true; |
| 106 | |
82 | 107 | // Run the QuitRequested() hook in each window's own thread. Otherwise |
83 | 108 | // the BAlert which a window shows when an icon is not saved will not |
84 | 109 | // repaint the window. (BAlerts check which thread is running Go() and |
… |
… |
IconEditorApp::MessageReceived(BMessage* message)
|
202 | 227 | void |
203 | 228 | IconEditorApp::ReadyToRun() |
204 | 229 | { |
| 230 | if (isCLI) |
| 231 | return; |
| 232 | |
| 233 | _Init(); |
| 234 | |
205 | 235 | // create main window |
206 | 236 | if (fWindowCount == 0) |
207 | 237 | _NewWindow()->Show(); |
… |
… |
IconEditorApp::RefsReceived(BMessage* message)
|
248 | 278 | } |
249 | 279 | |
250 | 280 | |
| 281 | static status_t |
| 282 | ConvertIcon(const char* from, const char* to, const char* format) |
| 283 | { |
| 284 | uint32 exportMode; |
| 285 | if (strcmp(format, "-h") == 0) |
| 286 | exportMode = EXPORT_MODE_FLAT_ICON; |
| 287 | else if (strcmp(format, "-s") == 0) |
| 288 | exportMode = EXPORT_MODE_SVG; |
| 289 | else |
| 290 | return B_ERROR; |
| 291 | |
| 292 | entry_ref ref; |
| 293 | status_t ret = get_ref_for_path(from, &ref); |
| 294 | if (ret != B_OK) |
| 295 | return ret; |
| 296 | |
| 297 | BFile file(&ref, B_READ_ONLY); |
| 298 | ret = file.InitCheck(); |
| 299 | if (ret < B_OK) |
| 300 | return ret; |
| 301 | |
| 302 | ret = get_ref_for_path(to, &ref); |
| 303 | if (ret != B_OK) |
| 304 | return ret; |
| 305 | |
| 306 | Icon* icon = new (nothrow) Icon(); |
| 307 | if (icon == NULL) |
| 308 | return B_ERROR; |
| 309 | |
| 310 | MessageImporter msgImporter; |
| 311 | ret = msgImporter.Import(icon, &file); |
| 312 | if (ret < B_OK) { |
| 313 | delete icon; |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | DocumentSaver* saver = NULL; |
| 318 | switch (exportMode) { |
| 319 | case EXPORT_MODE_FLAT_ICON: |
| 320 | saver = new SimpleFileSaver(new FlatIconExporter(), ref); |
| 321 | break; |
| 322 | case EXPORT_MODE_SVG: |
| 323 | saver = new SimpleFileSaver(new SVGExporter(), ref); |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | if (saver == NULL) { |
| 328 | delete icon; |
| 329 | return B_ERROR; |
| 330 | } |
| 331 | |
| 332 | Document* document = new Document(to); |
| 333 | if (document == NULL) { |
| 334 | delete icon; |
| 335 | return B_ERROR; |
| 336 | } |
| 337 | |
| 338 | document->SetIcon(icon); |
| 339 | document->SetExportSaver(saver); |
| 340 | saver->Save(document); |
| 341 | |
| 342 | return B_OK; |
| 343 | } |
| 344 | |
| 345 | |
251 | 346 | void |
252 | 347 | IconEditorApp::ArgvReceived(int32 argc, char** argv) |
253 | 348 | { |
254 | 349 | if (argc < 2) |
255 | 350 | return; |
256 | 351 | |
| 352 | // Usage: Icon-O-Matic -[h|s] -o output_file IOM_file |
| 353 | if (argc > 4 && strcmp(argv[2], "-o") == 0) { |
| 354 | isCLI = true; |
| 355 | exitCode = ConvertIcon(argv[4], argv[3], argv[1]); |
| 356 | PostMessage(B_QUIT_REQUESTED); |
| 357 | return; |
| 358 | } |
| 359 | |
257 | 360 | entry_ref ref; |
258 | 361 | |
259 | 362 | for (int32 i = 1; i < argc; i++) { |
diff --git a/src/apps/icon-o-matic/IconEditorApp.h b/src/apps/icon-o-matic/IconEditorApp.h
index 44df93a..f6ecc88 100644
a
|
b
|
public:
|
60 | 60 | // IconEditorApp |
61 | 61 | |
62 | 62 | private: |
| 63 | void _Init(); |
| 64 | |
63 | 65 | MainWindow* _NewWindow(); |
64 | 66 | |
65 | 67 | void _SyncPanels(BFilePanel* from, |
diff --git a/src/apps/icon-o-matic/main.cpp b/src/apps/icon-o-matic/main.cpp
index c7b6492..0f997b3 100644
a
|
b
|
|
8 | 8 | |
9 | 9 | #include "IconEditorApp.h" |
10 | 10 | |
| 11 | int exitCode; |
| 12 | |
11 | 13 | int |
12 | 14 | main(int argc, char** argv) |
13 | 15 | { |
… |
… |
main(int argc, char** argv)
|
16 | 18 | |
17 | 19 | delete app; |
18 | 20 | |
19 | | return 0; |
| 21 | return exitCode; |
20 | 22 | } |