1 /* 2 * Copyright 2006-2009, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stephan Aßmus <superstippi@gmx.de> 7 */ 8 9 #include "SVGImporter.h" 10 11 #include <Alert.h> 12 #include <Entry.h> 13 #include <File.h> 14 #include <Path.h> 15 16 #include "DocumentBuilder.h" 17 #include "SVGParser.h" 18 19 // constructor 20 SVGImporter::SVGImporter() 21 { 22 } 23 24 // destructor 25 SVGImporter::~SVGImporter() 26 { 27 } 28 29 // Import 30 status_t 31 SVGImporter::Import(Icon* icon, const entry_ref* ref) 32 { 33 status_t ret = Init(icon); 34 if (ret < B_OK) { 35 printf("SVGImporter::Import() - " 36 "Init() error: %s\n", strerror(ret)); 37 return ret; 38 } 39 40 BPath path(ref); 41 ret = path.InitCheck(); 42 if (ret < B_OK) 43 return ret; 44 45 // peek into file to see if this could be an SVG file at all 46 BFile file(path.Path(), B_READ_ONLY); 47 ret = file.InitCheck(); 48 if (ret < B_OK) 49 return ret; 50 51 ssize_t size = 5; 52 char buffer[size + 1]; 53 if (file.Read(buffer, size) != size) 54 return B_ERROR; 55 56 // 0 terminate 57 buffer[size] = 0; 58 if (strcasecmp(buffer, "<?xml") != 0) { 59 // we might be stretching it a bit, but what the heck 60 return B_ERROR; 61 } 62 63 try { 64 agg::svg::DocumentBuilder builder; 65 agg::svg::Parser parser(builder); 66 parser.parse(path.Path()); 67 ret = builder.GetIcon(icon, this, ref->name); 68 } catch(agg::svg::exception& e) { 69 char error[1024]; 70 sprintf(error, "Failed to open the file '%s' as " 71 "an SVG document.\n\n" 72 "Error: %s", ref->name, e.msg()); 73 BAlert* alert = new BAlert("load error", 74 error, "OK", NULL, NULL, 75 B_WIDTH_AS_USUAL, B_WARNING_ALERT); 76 alert->Go(NULL); 77 ret = B_ERROR; 78 } 79 80 return ret; 81 } 82