* Icon-O-Matic prepends a magic number when writing native files, this way

we can associate these native files with Icon-O-Matic (they also get their
  own mime type)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21295 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2007-06-02 18:35:29 +00:00
parent 81fb129880
commit 1518e4101b
11 changed files with 158 additions and 11 deletions

View File

@ -18,6 +18,8 @@
#include <File.h>
#include <FilePanel.h>
#include <IconEditorProtocol.h>
#include <Message.h>
#include <Mime.h>
#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");
}

View File

@ -78,6 +78,7 @@ class IconEditorApp : public BApplication {
void _StoreSettings();
void _RestoreSettings();
void _InstallDocumentMimeType();
MainWindow* fMainWindow;
Document* fDocument;

View File

@ -106,6 +106,7 @@ Application Icon-O-Matic :
######## Icon-O-Matic ########
# document
Defines.cpp
Document.cpp
IconObject.cpp
SetPropertiesCommand.cpp

View File

@ -0,0 +1,13 @@
/*
* Copyright 2007, Haiku. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "Defines.h"
const uint32 kNativeIconMagicNumber = 'GSMI';
const char* kNativeIconMimeType = "application/x-vnd.Haiku-icon";

View File

@ -0,0 +1,14 @@
/*
* Copyright 2007, Haiku. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#ifndef DEFINES_H
#define DEFINES_H
extern const uint32 kNativeIconMagicNumber;
extern const char* kNativeIconMimeType;
#endif // DEFINES_H

View File

@ -5,7 +5,6 @@
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "NativeSaver.h"
#include "FlatIconFormat.h"

View File

@ -5,7 +5,6 @@
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#ifndef NATIVE_SAVER_H
#define NATIVE_SAVER_H

View File

@ -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 <superstippi@gmx.de>
*/
#include "SimpleFileSaver.h"
#include "Exporter.h"

View File

@ -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 <superstippi@gmx.de>
*/
#ifndef SIMPLE_FILE_SAVER_H
#define SIMPLE_FILE_SAVER_H

View File

@ -8,10 +8,12 @@
#include "MessageExporter.h"
#include <ByteOrder.h>
#include <DataIO.h>
#include <Message.h>
#include <TypeConstants.h>
#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 -

View File

@ -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 <superstippi@gmx.de>
*/
#include "MessageImporter.h"
#include <new>
#include <stdio.h>
#include <Archivable.h>
#include <ByteOrder.h>
#include <DataIO.h>
#include <Message.h>
#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) {