2 * Copyright 2006-2010, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Stephan Aßmus <superstippi@gmx.de>
10 #include "MessageImporter.h"
15 #include <Archivable.h>
16 #include <ByteOrder.h>
22 #include "PathContainer.h"
25 #include "StyleContainer.h"
26 #include "VectorPath.h"
32 MessageImporter::MessageImporter()
40 MessageImporter::~MessageImporter()
46 MessageImporter::Import(Icon
* icon
, BPositionIO
* stream
)
49 status_t ret
= Init(icon
);
51 printf("MessageImporter::Import() - "
52 "Init() error: %s\n", strerror(ret
));
60 ssize_t size
= sizeof(magic
);
61 off_t position
= stream
->Position();
62 ssize_t read
= stream
->Read(&magic
, size
);
71 if (B_BENDIAN_TO_HOST_INT32(magic
) != kNativeIconMagicNumber
) {
72 // this might be an old native icon file, where
73 // we didn't prepend the magic number yet, seek back
74 if (stream
->Seek(position
, SEEK_SET
) != position
) {
75 printf("MessageImporter::Import() - "
76 "failed to seek back to beginning of stream\n");
82 ret
= archive
.Unflatten(stream
);
87 PathContainer
* paths
= icon
->Paths();
88 ret
= _ImportPaths(&archive
, paths
);
90 printf("MessageImporter::Import() - "
91 "error importing paths: %s\n", strerror(ret
));
96 StyleContainer
* styles
= icon
->Styles();
97 ret
= _ImportStyles(&archive
, styles
);
99 printf("MessageImporter::Import() - "
100 "error importing styles: %s\n", strerror(ret
));
105 ret
= _ImportShapes(&archive
, paths
, styles
, icon
->Shapes());
107 printf("MessageImporter::Import() - "
108 "error importing shapes: %s\n", strerror(ret
));
120 MessageImporter::_ImportPaths(const BMessage
* archive
,
121 PathContainer
* paths
) const
124 status_t ret
= archive
->FindMessage("paths", &allPaths
);
128 BMessage pathArchive
;
130 allPaths
.FindMessage("path", i
, &pathArchive
) == B_OK
; i
++) {
131 VectorPath
* path
= new (nothrow
) VectorPath(&pathArchive
);
132 if (!path
|| !paths
->AddPath(path
)) {
145 MessageImporter::_ImportStyles(const BMessage
* archive
,
146 StyleContainer
* styles
) const
149 status_t ret
= archive
->FindMessage("styles", &allStyles
);
153 BMessage styleArchive
;
155 allStyles
.FindMessage("style", i
, &styleArchive
) == B_OK
; i
++) {
156 Style
* style
= new (nothrow
) Style(&styleArchive
);
157 if (!style
|| !styles
->AddStyle(style
)) {
170 MessageImporter::_ImportShapes(const BMessage
* archive
, PathContainer
* paths
,
171 StyleContainer
* styles
, ShapeContainer
* shapes
) const
174 status_t ret
= archive
->FindMessage("shapes", &allShapes
);
178 BMessage shapeArchive
;
180 allShapes
.FindMessage("shape", i
, &shapeArchive
) == B_OK
; i
++) {
181 // find the right style
183 if (shapeArchive
.FindInt32("style ref", &styleIndex
) < B_OK
) {
184 printf("MessageImporter::_ImportShapes() - "
185 "Shape %" B_PRId32
" doesn't reference a Style!", i
);
189 Style
* style
= styles
->StyleAt(StyleIndexFor(styleIndex
));
191 Style
* style
= styles
->StyleAt(styleIndex
);
194 printf("MessageImporter::_ImportShapes() - "
195 "Shape %" B_PRId32
" wants Style %" B_PRId32
", which does not exist\n",
201 Shape
* shape
= new (nothrow
) Shape(style
);
202 if (!shape
|| shape
->InitCheck() < B_OK
|| !shapes
->AddShape(shape
)) {
209 // find the referenced paths
212 shapeArchive
.FindInt32("path ref", j
, &pathIndex
) == B_OK
;
215 VectorPath
* path
= paths
->PathAt(PathIndexFor(pathIndex
));
217 VectorPath
* path
= paths
->PathAt(pathIndex
);
220 printf("MessageImporter::_ImportShapes() - "
221 "Shape %" B_PRId32
" referenced path %" B_PRId32
", "
222 "which does not exist\n", i
, pathIndex
);
225 shape
->Paths()->AddPath(path
);
230 shape
->Unarchive(&shapeArchive
);