Set the description of some parameters
[synfig.git] / synfig-core / src / synfig / importer.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file importer.cpp
3 **      \brief It is the base class for all the importers.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "canvas.h"
34 #include "importer.h"
35 #include "surface.h"
36 #include <algorithm>
37 #include "string.h"
38 #include <map>
39 #include <ctype.h>
40 #include <functional>
41
42 #endif
43
44 /* === M A C R O S ========================================================= */
45
46 /* === G L O B A L S ======================================================= */
47
48 using namespace etl;
49 using namespace std;
50 using namespace synfig;
51
52 Importer::Book* synfig::Importer::book_;
53
54 map<String,Importer::LooseHandle> *__open_importers;
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 bool
61 Importer::subsys_init()
62 {
63         book_=new Book();
64         __open_importers=new map<String,Importer::LooseHandle>();
65         return true;
66 }
67
68 bool
69 Importer::subsys_stop()
70 {
71         delete book_;
72         delete __open_importers;
73         return true;
74 }
75
76 Importer::Book&
77 Importer::book()
78 {
79         return *book_;
80 }
81
82 Importer::Handle
83 Importer::open(const String &filename)
84 {
85         if(filename.empty())
86         {
87                 synfig::error(_("Importer::open(): Cannot open empty filename"));
88                 return 0;
89         }
90
91         // If we already have an importer open under that filename,
92         // then use it instead.
93         if(__open_importers->count(filename))
94         {
95                 //synfig::info("Found importer already open, using it...");
96                 return (*__open_importers)[filename];
97         }
98
99         if(filename_extension(filename) == "")
100         {
101                 synfig::error(_("Importer::open(): Couldn't find extension"));
102                 return 0;
103         }
104
105         String ext(filename_extension(filename));
106         if (ext.size()) ext = ext.substr(1); // skip initial '.'
107         std::transform(ext.begin(),ext.end(),ext.begin(),&::tolower);
108
109
110         if(!Importer::book().count(ext))
111         {
112                 synfig::error(_("Importer::open(): Unknown file type -- ")+ext);
113                 return 0;
114         }
115
116         try {
117                 Importer::Handle importer;
118                 importer=Importer::book()[ext](filename.c_str());
119                 (*__open_importers)[filename]=importer;
120                 return importer;
121         }
122         catch (String str)
123         {
124                 synfig::error(str);
125         }
126         return 0;
127 }
128
129 Importer::Importer():
130         gamma_(2.2)
131 {
132 }
133
134
135 Importer::~Importer()
136 {
137         // Remove ourselves from the open importer list
138         map<String,Importer::LooseHandle>::iterator iter;
139         for(iter=__open_importers->begin();iter!=__open_importers->end();++iter)
140                 if(iter->second==this)
141                 {
142                         __open_importers->erase(iter);
143                 }
144 }