Ticket #13952: 0001-Icon-O-Matic-convert-IOM-to-HVIF-SVG-from-command-li.patch

File 0001-Icon-O-Matic-convert-IOM-to-HVIF-SVG-from-command-li.patch, 5.0 KB (added by owenca, 7 years ago)
  • src/apps/icon-o-matic/IconEditorApp.cpp

    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  
    66
    77#include "IconEditorApp.h"
    88
     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
    916#include <new>
    1017#include <stdio.h>
    1118#include <string.h>
     
    3239#define B_TRANSLATION_CONTEXT "Icon-O-Matic-Main"
    3340
    3441
     42extern int exitCode;
     43
     44
    3545using std::nothrow;
    3646
    3747static const char* kAppSig = "application/x-vnd.haiku-icon_o_matic";
    3848
    3949static const float kWindowOffset = 20;
    4050
     51static bool isCLI;
     52
    4153
    4254IconEditorApp::IconEditorApp()
    4355    :
    4456    BApplication(kAppSig),
    45     fWindowCount(0),
    46     fLastWindowFrame(50, 50, 900, 750),
    4757
    4858    fOpenPanel(NULL),
    49     fSavePanel(NULL),
     59    fSavePanel(NULL)
     60{
     61}
    5062
    51     fLastOpenPath(""),
    52     fLastSavePath(""),
    53     fLastExportPath("")
     63
     64void
     65IconEditorApp::_Init()
    5466{
     67    fWindowCount = 0;
     68    fLastWindowFrame.Set(50, 50, 900, 750);
     69
     70    fLastOpenPath = "";
     71    fLastSavePath = "";
     72    fLastExportPath = "";
     73
    5574    // create file panels
    5675    BMessenger messenger(this, this);
    5776    BMessage message(B_REFS_RECEIVED);
    IconEditorApp::IconEditorApp()  
    6887
    6988IconEditorApp::~IconEditorApp()
    7089{
    71     delete fOpenPanel;
    72     delete fSavePanel;
     90    if (fOpenPanel != NULL)
     91        delete fOpenPanel;
     92
     93    if (fSavePanel != NULL)
     94        delete fSavePanel;
    7395}
    7496
    7597
    IconEditorApp::~IconEditorApp()  
    79101bool
    80102IconEditorApp::QuitRequested()
    81103{
     104    if (isCLI)
     105        return true;
     106
    82107    // Run the QuitRequested() hook in each window's own thread. Otherwise
    83108    // the BAlert which a window shows when an icon is not saved will not
    84109    // repaint the window. (BAlerts check which thread is running Go() and
    IconEditorApp::MessageReceived(BMessage* message)  
    202227void
    203228IconEditorApp::ReadyToRun()
    204229{
     230    if (isCLI)
     231        return;
     232
     233    _Init();
     234
    205235    // create main window
    206236    if (fWindowCount == 0)
    207237        _NewWindow()->Show();
    IconEditorApp::RefsReceived(BMessage* message)  
    248278}
    249279
    250280
     281static status_t
     282ConvertIcon(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
    251346void
    252347IconEditorApp::ArgvReceived(int32 argc, char** argv)
    253348{
    254349    if (argc < 2)
    255350        return;
    256351
     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
    257360    entry_ref ref;
    258361
    259362    for (int32 i = 1; i < argc; i++) {
  • src/apps/icon-o-matic/IconEditorApp.h

    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:  
    6060    // IconEditorApp
    6161
    6262private:
     63            void                _Init();
     64
    6365            MainWindow*         _NewWindow();
    6466
    6567            void                _SyncPanels(BFilePanel* from,
  • src/apps/icon-o-matic/main.cpp

    diff --git a/src/apps/icon-o-matic/main.cpp b/src/apps/icon-o-matic/main.cpp
    index c7b6492..0f997b3 100644
    a b  
    88
    99#include "IconEditorApp.h"
    1010
     11int exitCode;
     12
    1113int
    1214main(int argc, char** argv)
    1315{
    main(int argc, char** argv)  
    1618
    1719    delete app;
    1820
    19     return 0;
     21    return exitCode;
    2022}