From 53200f8efabd577fa90d0d472842a7e90fd4e72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Tue, 19 Aug 2008 15:53:37 +0000 Subject: [PATCH] Icon-O-Matic will now fall back to trying to open the icon attribute of any node it is requested to open. That this would happen was an assumption of the FileTypes preflet. Adding an icon to a node from the FileTypes add-on will now work. Previously, Icon-O-Matic thought this was supposed to be an icon file. Perhaps it would be better if Icon-O-Matic would ask the user if adding an icon was the intention. FileTypes could put a flag into the message to supress this alert. On the other hand, if adding an icon was not the intention, the user may as well simply close Icon-O-Matic. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27065 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/icon-o-matic/IconEditorApp.cpp | 51 +++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/src/apps/icon-o-matic/IconEditorApp.cpp b/src/apps/icon-o-matic/IconEditorApp.cpp index 36e546be3e..74aae54c48 100644 --- a/src/apps/icon-o-matic/IconEditorApp.cpp +++ b/src/apps/icon-o-matic/IconEditorApp.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -368,6 +369,7 @@ IconEditorApp::_Open(const entry_ref& ref, bool append) REF_NONE = 0, REF_MESSAGE, REF_FLAT, + REF_FLAT_ATTR, REF_SVG }; uint32 refMode = REF_NONE; @@ -387,8 +389,47 @@ IconEditorApp::_Open(const entry_ref& ref, bool append) file.Seek(0, SEEK_SET); SVGImporter svgImporter; ret = svgImporter.Import(icon, &ref); - if (ret >= B_OK) + if (ret >= B_OK) { refMode = REF_SVG; + } else { + // fall back to flat icon format but use the icon attribute + ret = B_OK; + attr_info attrInfo; + if (file.GetAttrInfo(kVectorAttrNodeName, &attrInfo) == B_OK) { + if (attrInfo.type != B_VECTOR_ICON_TYPE) + ret = B_ERROR; + // If the attribute is there, we must succeed in reading + // an icon! Otherwise we may overwrite an existing icon + // when the user saves. + uint8* buffer = NULL; + if (ret == B_OK) { + buffer = new(nothrow) uint8[attrInfo.size]; + if (buffer == NULL) + ret = B_NO_MEMORY; + } + if (ret == B_OK) { + ssize_t bytesRead = file.ReadAttr(kVectorAttrNodeName, + B_VECTOR_ICON_TYPE, 0, buffer, attrInfo.size); + if (bytesRead != (ssize_t)attrInfo.size) { + ret = bytesRead < 0 ? (status_t)bytesRead + : B_IO_ERROR; + } + } + if (ret == B_OK) { + ret = flatImporter.Import(icon, buffer, attrInfo.size); + if (ret == B_OK) + refMode = REF_FLAT_ATTR; + } + + delete[] buffer; + } else { + // If there is no icon attribute, simply fall back + // to creating an icon for this file. TODO: We may or may + // not want to display an alert asking the user if that is + // what he wants to do. + refMode = REF_FLAT_ATTR; + } + } } } @@ -430,6 +471,10 @@ IconEditorApp::_Open(const entry_ref& ref, bool append) fDocument->SetExportSaver( new SimpleFileSaver(new FlatIconExporter(), ref)); break; + case REF_FLAT_ATTR: + fDocument->SetNativeSaver( + new AttributeSaver(ref, kVectorAttrNodeName)); + break; case REF_SVG: fDocument->SetExportSaver( new SimpleFileSaver(new SVGExporter(), ref)); @@ -449,8 +494,8 @@ IconEditorApp::_Open(const entry_ref& ref, bool append) // _Open void -IconEditorApp::_Open(const BMessenger& externalObserver, - const uint8* data, size_t size) +IconEditorApp::_Open(const BMessenger& externalObserver, const uint8* data, + size_t size) { if (!_CheckSaveIcon(CurrentMessage())) return;