diff --git a/src/apps/icon-o-matic/IconEditorApp.cpp b/src/apps/icon-o-matic/IconEditorApp.cpp index 76c6cf0920..8aa3c82d71 100644 --- a/src/apps/icon-o-matic/IconEditorApp.cpp +++ b/src/apps/icon-o-matic/IconEditorApp.cpp @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include "support_settings.h" @@ -26,6 +28,7 @@ #include "BitmapExporter.h" #include "BitmapSetSaver.h" #include "CommandStack.h" +#include "Defines.h" #include "Document.h" #include "FlatIconExporter.h" #include "FlatIconFormat.h" @@ -46,9 +49,11 @@ using std::nothrow; +static const char* kAppSig = "application/x-vnd.haiku-icon_o_matic"; + // constructor IconEditorApp::IconEditorApp() - : BApplication("application/x-vnd.haiku-icon_o_matic"), + : BApplication(kAppSig), fMainWindow(NULL), fDocument(new Document("test")), @@ -227,6 +232,8 @@ IconEditorApp::ReadyToRun() _RestoreSettings(); fMainWindow->Show(); + + _InstallDocumentMimeType(); } // RefsReceived @@ -571,3 +578,80 @@ IconEditorApp::_RestoreSettings() fMainWindow->RestoreSettings(&settings); } +// _InstallDocumentMimeType +void +IconEditorApp::_InstallDocumentMimeType() +{ + // install mime type of documents + BMimeType mime(kNativeIconMimeType); + status_t ret = mime.InitCheck(); + if (ret < B_OK) { + fprintf(stderr, "Could not init native document mime type (%s): %s.\n", + kNativeIconMimeType, strerror(ret)); + return; + } + + if (mime.IsInstalled() && !(modifiers() & B_SHIFT_KEY)) { + // mime is already installed, and the user is not + // pressing the shift key to force a re-install + return; + } + + ret = mime.Install(); + if (ret < B_OK) { + fprintf(stderr, "Could not install native document mime type (%s): %s.\n", + kNativeIconMimeType, strerror(ret)); + return; + } + // set preferred app + ret = mime.SetPreferredApp(kAppSig); + if (ret < B_OK) + fprintf(stderr, "Could not set native document preferred app: %s\n", + strerror(ret)); + + // set descriptions + ret = mime.SetShortDescription("Haiku Icon"); + if (ret < B_OK) + fprintf(stderr, "Could not set short description of mime type: %s\n", + strerror(ret)); + ret = mime.SetLongDescription("Native Haiku vector icon"); + if (ret < B_OK) + fprintf(stderr, "Could not set long description of mime type: %s\n", + strerror(ret)); + + // set extensions + BMessage message('extn'); + message.AddString("extensions", "icon"); + ret = mime.SetFileExtensions(&message); + if (ret < B_OK) + fprintf(stderr, "Could not set extensions of mime type: %s\n", + strerror(ret)); + + // set sniffer rule + const char* snifferRule = "0.9 ('GSMI')"; + ret = mime.SetSnifferRule(snifferRule); + if (ret < B_OK) { + BString parseError; + BMimeType::CheckSnifferRule(snifferRule, &parseError); + fprintf(stderr, "Could not set sniffer rule of mime type: %s\n", + parseError.String()); + } + +// NOTE: Icon-O-Matic writes the icon being saved as icon of the file anyways +// therefor, the following code is not needed, it is also not tested and I am +// spotting an error with SetIcon() +// // set document icon +// BResources* resources = AppResources(); +// // does not need to be freed (belongs to BApplication base) +// if (resources) { +// size_t size; +// const void* iconData = resources->LoadResource('VICN', "IOM:DOC_ICON", &size); +// if (iconData && size > 0) { +// memcpy(largeIcon.Bits(), iconData, size); +// if (mime.SetIcon(&largeIcon, B_LARGE_ICON) < B_OK) +// fprintf(stderr, "Could not set large icon of mime type.\n"); +// } else +// fprintf(stderr, "Could not find icon in app resources (data: %p, size: %ld).\n", iconData, size); +// } else +// fprintf(stderr, "Could not find app resources.\n"); +} diff --git a/src/apps/icon-o-matic/IconEditorApp.h b/src/apps/icon-o-matic/IconEditorApp.h index 8708a3eb4f..0e4bac148c 100644 --- a/src/apps/icon-o-matic/IconEditorApp.h +++ b/src/apps/icon-o-matic/IconEditorApp.h @@ -78,6 +78,7 @@ class IconEditorApp : public BApplication { void _StoreSettings(); void _RestoreSettings(); + void _InstallDocumentMimeType(); MainWindow* fMainWindow; Document* fDocument; diff --git a/src/apps/icon-o-matic/Jamfile b/src/apps/icon-o-matic/Jamfile index de402bdd05..d2e6e99c3a 100644 --- a/src/apps/icon-o-matic/Jamfile +++ b/src/apps/icon-o-matic/Jamfile @@ -106,6 +106,7 @@ Application Icon-O-Matic : ######## Icon-O-Matic ######## # document + Defines.cpp Document.cpp IconObject.cpp SetPropertiesCommand.cpp diff --git a/src/apps/icon-o-matic/document/Defines.cpp b/src/apps/icon-o-matic/document/Defines.cpp new file mode 100644 index 0000000000..f6df1a8e94 --- /dev/null +++ b/src/apps/icon-o-matic/document/Defines.cpp @@ -0,0 +1,13 @@ +/* + * Copyright 2007, Haiku. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stephan Aßmus + */ +#include "Defines.h" + +const uint32 kNativeIconMagicNumber = 'GSMI'; +const char* kNativeIconMimeType = "application/x-vnd.Haiku-icon"; + + diff --git a/src/apps/icon-o-matic/document/Defines.h b/src/apps/icon-o-matic/document/Defines.h new file mode 100644 index 0000000000..0f82f75054 --- /dev/null +++ b/src/apps/icon-o-matic/document/Defines.h @@ -0,0 +1,14 @@ +/* + * Copyright 2007, Haiku. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stephan Aßmus + */ +#ifndef DEFINES_H +#define DEFINES_H + +extern const uint32 kNativeIconMagicNumber; +extern const char* kNativeIconMimeType; + +#endif // DEFINES_H diff --git a/src/apps/icon-o-matic/document/savers/NativeSaver.cpp b/src/apps/icon-o-matic/document/savers/NativeSaver.cpp index 28062ec0b0..8fdce21130 100644 --- a/src/apps/icon-o-matic/document/savers/NativeSaver.cpp +++ b/src/apps/icon-o-matic/document/savers/NativeSaver.cpp @@ -5,7 +5,6 @@ * Authors: * Stephan Aßmus */ - #include "NativeSaver.h" #include "FlatIconFormat.h" diff --git a/src/apps/icon-o-matic/document/savers/NativeSaver.h b/src/apps/icon-o-matic/document/savers/NativeSaver.h index 16ee8ce8a7..c7a21db7ad 100644 --- a/src/apps/icon-o-matic/document/savers/NativeSaver.h +++ b/src/apps/icon-o-matic/document/savers/NativeSaver.h @@ -5,7 +5,6 @@ * Authors: * Stephan Aßmus */ - #ifndef NATIVE_SAVER_H #define NATIVE_SAVER_H diff --git a/src/apps/icon-o-matic/document/savers/SimpleFileSaver.cpp b/src/apps/icon-o-matic/document/savers/SimpleFileSaver.cpp index aa97c25667..7b50e91976 100644 --- a/src/apps/icon-o-matic/document/savers/SimpleFileSaver.cpp +++ b/src/apps/icon-o-matic/document/savers/SimpleFileSaver.cpp @@ -1,11 +1,10 @@ /* - * Copyright 2006, Haiku. All rights reserved. + * Copyright 2006-2007, Haiku. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Stephan Aßmus */ - #include "SimpleFileSaver.h" #include "Exporter.h" diff --git a/src/apps/icon-o-matic/document/savers/SimpleFileSaver.h b/src/apps/icon-o-matic/document/savers/SimpleFileSaver.h index b1e2d299a4..245245fb1f 100644 --- a/src/apps/icon-o-matic/document/savers/SimpleFileSaver.h +++ b/src/apps/icon-o-matic/document/savers/SimpleFileSaver.h @@ -1,11 +1,10 @@ /* - * Copyright 2006, Haiku. All rights reserved. + * Copyright 2006-2007, Haiku. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Stephan Aßmus */ - #ifndef SIMPLE_FILE_SAVER_H #define SIMPLE_FILE_SAVER_H diff --git a/src/apps/icon-o-matic/import_export/message/MessageExporter.cpp b/src/apps/icon-o-matic/import_export/message/MessageExporter.cpp index 313c28a9d0..cd34ede0dc 100644 --- a/src/apps/icon-o-matic/import_export/message/MessageExporter.cpp +++ b/src/apps/icon-o-matic/import_export/message/MessageExporter.cpp @@ -8,10 +8,12 @@ #include "MessageExporter.h" +#include #include #include #include +#include "Defines.h" #include "Icon.h" #include "PathContainer.h" #include "Shape.h" @@ -98,6 +100,20 @@ MessageExporter::Export(const Icon* icon, BPositionIO* stream) ret = archive.AddMessage("shapes", &allShapes); } + // prepend the magic number to the file which + // later tells us that this file is one of us + if (ret == B_OK) { + ssize_t size = sizeof(uint32); + uint32 magic = B_HOST_TO_LENDIAN_INT32(kNativeIconMagicNumber); + ssize_t written = stream->Write(&magic, size); + if (written != size) { + if (written < 0) + ret = (status_t)written; + else + ret = B_IO_ERROR; + } + } + if (ret == B_OK) ret = archive.Flatten(stream); @@ -108,8 +124,7 @@ MessageExporter::Export(const Icon* icon, BPositionIO* stream) const char* MessageExporter::MIMEType() { - // TODO: come up with a MIME type - return NULL; + return kNativeIconMimeType; } // #pragma mark - diff --git a/src/apps/icon-o-matic/import_export/message/MessageImporter.cpp b/src/apps/icon-o-matic/import_export/message/MessageImporter.cpp index 1f0acb14dc..ede1dab285 100644 --- a/src/apps/icon-o-matic/import_export/message/MessageImporter.cpp +++ b/src/apps/icon-o-matic/import_export/message/MessageImporter.cpp @@ -1,20 +1,21 @@ /* - * Copyright 2006, Haiku. All rights reserved. + * Copyright 2006-2007, Haiku. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Stephan Aßmus */ - #include "MessageImporter.h" #include #include #include +#include #include #include +#include "Defines.h" #include "Icon.h" #include "PathContainer.h" #include "Shape.h" @@ -46,6 +47,28 @@ MessageImporter::Import(Icon* icon, BPositionIO* stream) return ret; } + uint32 magic = 0; + ssize_t size = sizeof(magic); + off_t position = stream->Position(); + ssize_t read = stream->Read(&magic, size); + if (read != size) { + if (read < 0) + ret = (status_t)read; + else + ret = B_IO_ERROR; + return ret; + } + + if (B_LENDIAN_TO_HOST_INT32(magic) != kNativeIconMagicNumber) { + // this might be an old native icon file, where + // we didn't prepend the magic number yet, seek back + if (stream->Seek(position, SEEK_SET) != position) { + printf("MessageImporter::Import() - " + "failed to seek back to beginning of stream\n"); + return B_IO_ERROR; + } + } + BMessage archive; ret = archive.Unflatten(stream); if (ret < B_OK) {