Ticket #13952: 0001-Icon-O-Matic-convert-IOM-to-HVIF-RDef-SVG-source-fro.patch

File 0001-Icon-O-Matic-convert-IOM-to-HVIF-RDef-SVG-source-fro.patch, 5.6 KB (added by owenca, 7 years ago)

Replacing previous patches.

  • src/apps/icon-o-matic/IconEditorApp.cpp

    From 626c454f4d1142310a0f1e1722a319705fe67337 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/RDef/SVG/source from
     command line
    
    It returns 0 if successful or nonzero otherwise.
    
    Fixes #13952
    ---
     src/apps/icon-o-matic/IconEditorApp.cpp | 150 ++++++++++++++++++++++++++++++--
     src/apps/icon-o-matic/IconEditorApp.h   |   2 +
     src/apps/icon-o-matic/main.cpp          |   4 +-
     3 files changed, 147 insertions(+), 9 deletions(-)
    
    diff --git a/src/apps/icon-o-matic/IconEditorApp.cpp b/src/apps/icon-o-matic/IconEditorApp.cpp
    index d667ce9..bd29b1e 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 "RDefExporter.h"
     14#include "SimpleFileSaver.h"
     15#include "SourceExporter.h"
     16#include "SVGExporter.h"
     17
    918#include <new>
    1019#include <stdio.h>
    1120#include <string.h>
     
    3241#define B_TRANSLATION_CONTEXT "Icon-O-Matic-Main"
    3342
    3443
     44extern int exitCode;
     45
     46
    3547using std::nothrow;
    3648
    3749static const char* kAppSig = "application/x-vnd.haiku-icon_o_matic";
    3850
    3951static const float kWindowOffset = 20;
    4052
     53static bool isCLI;
     54
    4155
    4256IconEditorApp::IconEditorApp()
    4357    :
    44     BApplication(kAppSig),
    45     fWindowCount(0),
    46     fLastWindowFrame(50, 50, 900, 750),
     58    BApplication(kAppSig)
     59{
     60}
    4761
    48     fOpenPanel(NULL),
    49     fSavePanel(NULL),
    5062
    51     fLastOpenPath(""),
    52     fLastSavePath(""),
    53     fLastExportPath("")
     63void
     64IconEditorApp::_Init()
    5465{
     66    fWindowCount = 0;
     67    fLastWindowFrame.Set(50, 50, 900, 750);
     68
     69    fOpenPanel = NULL;
     70    fSavePanel = NULL;
     71
     72    fLastOpenPath = "";
     73    fLastSavePath = "";
     74    fLastExportPath = "";
     75
    5576    // create file panels
    5677    BMessenger messenger(this, this);
    5778    BMessage message(B_REFS_RECEIVED);
    IconEditorApp::~IconEditorApp()  
    79100bool
    80101IconEditorApp::QuitRequested()
    81102{
     103    if (isCLI)
     104        return true;
     105
    82106    // Run the QuitRequested() hook in each window's own thread. Otherwise
    83107    // the BAlert which a window shows when an icon is not saved will not
    84108    // repaint the window. (BAlerts check which thread is running Go() and
    IconEditorApp::MessageReceived(BMessage* message)  
    202226void
    203227IconEditorApp::ReadyToRun()
    204228{
     229    if (isCLI)
     230        return;
     231
     232    _Init();
     233
    205234    // create main window
    206235    if (fWindowCount == 0)
    207236        _NewWindow()->Show();
    IconEditorApp::RefsReceived(BMessage* message)  
    248277}
    249278
    250279
     280static void
     281Help()
     282{
     283    puts("Usage: Icon-O-Matic OPTION -o OUTPUT_FILE IOM_FILE");
     284    puts("  -H  convert to HVIF");
     285    puts("  -R  convert to RDef");
     286    puts("  -S  convert to SVG");
     287    puts("  -s  convert to source");
     288    puts("  -h, --help");
     289    puts("      display this help");
     290}
     291
     292
     293static status_t
     294ConvertIcon(const char* from, const char* to, const char* format)
     295{
     296    uint32 exportMode;
     297    if (strcmp(format, "-H") == 0)
     298        exportMode = EXPORT_MODE_FLAT_ICON;
     299    else if (strcmp(format, "-R") == 0)
     300        exportMode = EXPORT_MODE_ICON_RDEF;
     301    else if (strcmp(format, "-S") == 0)
     302        exportMode = EXPORT_MODE_SVG;
     303    else if (strcmp(format, "-s") == 0)
     304        exportMode = EXPORT_MODE_ICON_SOURCE;
     305    else {
     306        Help();
     307        return B_ERROR;
     308    }
     309
     310    entry_ref ref;
     311    status_t ret = get_ref_for_path(from, &ref);
     312    if (ret != B_OK)
     313        return ret;
     314
     315    BFile file(&ref, B_READ_ONLY);
     316    ret = file.InitCheck();
     317    if (ret < B_OK)
     318        return ret;
     319
     320    ret = get_ref_for_path(to, &ref);
     321    if (ret != B_OK)
     322        return ret;
     323
     324    Icon* icon = new (nothrow) Icon();
     325    if (icon == NULL)
     326        return B_ERROR;
     327
     328    MessageImporter msgImporter;
     329    ret = msgImporter.Import(icon, &file);
     330    if (ret < B_OK) {
     331        delete icon;
     332        return ret;
     333    }
     334
     335    DocumentSaver* saver = NULL;
     336    switch (exportMode) {
     337        case EXPORT_MODE_FLAT_ICON:
     338            saver = new SimpleFileSaver(new FlatIconExporter(), ref);
     339            break;
     340        case EXPORT_MODE_ICON_RDEF:
     341            saver = new SimpleFileSaver(new RDefExporter(), ref);
     342            break;
     343        case EXPORT_MODE_ICON_SOURCE:
     344            saver = new SimpleFileSaver(new SourceExporter(), ref);
     345            break;
     346        case EXPORT_MODE_SVG:
     347            saver = new SimpleFileSaver(new SVGExporter(), ref);
     348            break;
     349    }
     350
     351    if (saver == NULL) {
     352        delete icon;
     353        return B_ERROR;
     354    }
     355
     356    Document* document = new Document(to);
     357    if (document == NULL) {
     358        delete icon;
     359        return B_ERROR;
     360    }
     361
     362    document->SetIcon(icon);
     363    document->SetExportSaver(saver);
     364    saver->Save(document);
     365
     366    return B_OK;
     367}
     368
     369
    251370void
    252371IconEditorApp::ArgvReceived(int32 argc, char** argv)
    253372{
    254373    if (argc < 2)
    255374        return;
    256375
     376    if (*argv[1] == '-') {
     377        if ((strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0))
     378            Help();
     379        else if (argc == 5 && strcmp(argv[2], "-o") == 0)
     380            exitCode = ConvertIcon(argv[4], argv[3], argv[1]);
     381        else {
     382            Help();
     383            exitCode = B_ERROR;
     384        }
     385
     386        isCLI = true;
     387        PostMessage(B_QUIT_REQUESTED);
     388        return;
     389    }
     390
    257391    entry_ref ref;
    258392
    259393    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}