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 <strings.h>
12
13#include <Alert.h>
14#include <Catalog.h>
15#include <Entry.h>
16#include <File.h>
17#include <Locale.h>
18#include <MimeType.h>
19#include <Path.h>
20
21#include "DocumentBuilder.h"
22#include "nanosvg.h"
23
24
25#undef B_TRANSLATION_CONTEXT
26#define B_TRANSLATION_CONTEXT "Icon-O-Matic-SVGImport"
27
28
29// constructor
30SVGImporter::SVGImporter()
31{
32}
33
34// destructor
35SVGImporter::~SVGImporter()
36{
37}
38
39// Import
40status_t
41SVGImporter::Import(Icon* icon, const entry_ref* ref)
42{
43	status_t ret = Init(icon);
44	if (ret < B_OK) {
45		printf("SVGImporter::Import() - "
46			   "Init() error: %s\n", strerror(ret));
47		return ret;
48	}
49
50	BPath path(ref);
51	ret = path.InitCheck();
52	if (ret < B_OK)
53		return ret;
54
55	// Check that it indeed looks like an SVG file
56	BMimeType type;
57	ret = BMimeType::GuessMimeType(ref, &type);
58	if (ret != B_OK || strcmp(type.Type(), "image/svg+xml") != 0) {
59		printf("not an svg file %s %s\n", strerror(ret), type.Type());
60		return B_ERROR;
61	}
62
63	NSVGimage* svg = nsvgParseFromFile(path.Path(), "px", 96);
64	if (svg == NULL) {
65		char error[1024];
66		sprintf(error, B_TRANSLATE("Failed to open the file '%s' as "
67			"an SVG document.\n\n"), ref->name);
68		BAlert* alert = new BAlert(B_TRANSLATE("Load error"),
69			error, B_TRANSLATE("OK"), NULL, NULL,
70			B_WIDTH_AS_USUAL, B_WARNING_ALERT);
71		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
72		alert->Go(NULL);
73		return B_ERROR;
74	}
75
76	DocumentBuilder builder(svg);
77	ret = builder.GetIcon(icon, ref->name);
78	nsvgDelete(svg);
79
80	return ret;
81}
82