Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / synfig / loadcanvas.cpp
index 28dff3e..3da37e9 100644 (file)
@@ -7,6 +7,7 @@
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
 **     Copyright (c) 2007, 2008 Chris Moore
+**     Copyright (c) 2009 Carlos A. Sosa Navarro
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -1075,7 +1076,7 @@ CanvasParser::parse_animated(xmlpp::Element *element,Canvas::Handle canvas)
        // when loading a version 0.1 canvas, modify constant angle
        // waypoints to that they are within 180 degrees of the previous
        // waypoint's value
-       if (type == ValueBase::TYPE_ANGLE) 
+       if (type == ValueBase::TYPE_ANGLE)
        {
                if (canvas->get_version() == "0.1")
                {
@@ -1319,6 +1320,12 @@ CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle c
                                value_node->link_name(i) == "loop")
                                continue;
 
+                       // 'loop' was added while canvas version 0.6 was in use; the 'random' node was added back when 0.1 was in use
+                       if ((version == "0.1" || version == "0.2" || version == "0.3" || version == "0.4" || version == "0.5" || version == "0.6") &&
+                               element->get_name() == "random" &&
+                               value_node->link_name(i) == "loop")
+                               continue;
+
                        error(element, strprintf(_("<%s> is missing link %d (%s)"),
                                                                         element->get_name().c_str(),
                                                                         i,
@@ -2128,6 +2135,24 @@ CanvasParser::register_canvas_in_map(Canvas::Handle canvas, String as)
        canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
 }
 
+#ifdef _DEBUG
+void
+CanvasParser::show_canvas_map(String file, int line, String text)
+{
+       return;
+       printf("  .-----\n  |  %s:%d %s\n", file.c_str(), line, text.c_str());
+       std::map<synfig::String, etl::loose_handle<Canvas> > canvas_map(synfig::get_open_canvas_map());
+       std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
+       for (iter = canvas_map.begin(); iter != canvas_map.end(); iter++)
+       {
+               synfig::String first(iter->first);
+               etl::loose_handle<Canvas> second(iter->second);
+               printf("  |    %40s : %lx (%d)\n", first.c_str(), ulong(&*second), second->count());
+       }
+       printf("  `-----\n\n");
+}
+#endif // _DEBUG
+
 Canvas::Handle
 CanvasParser::parse_from_file_as(const String &file_,const String &as_,String &errors)
 {
@@ -2192,3 +2217,85 @@ CanvasParser::parse_from_file_as(const String &file_,const String &as_,String &e
        }
        return Canvas::Handle();
 }
+
+Canvas::Handle
+CanvasParser::parse_as(xmlpp::Element* node,String &errors)
+{
+       ChangeLocale change_locale(LC_NUMERIC, "C");
+       try
+       {
+               total_warnings_=0;
+               if(node)
+               {
+                       Canvas::Handle canvas(parse_canvas(node,0,false,""));
+                       if (!canvas) return canvas;
+
+                       const ValueNodeList& value_node_list(canvas->value_node_list());
+
+                       again:
+                       ValueNodeList::const_iterator iter;
+                       for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
+                       {
+                               ValueNode::Handle value_node(*iter);
+                               if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
+                               {
+                                       canvas->remove_value_node(value_node);
+                                       goto again;
+                               }
+                       }
+
+                       return canvas;
+               }
+       }
+       catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
+       catch(Exception::BadType) { synfig::error("BadType Thrown"); }
+       catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
+       catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
+       catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
+       catch(xmlpp::internal_error x)
+       {
+               if (!strcmp(x.what(), "Couldn't create parsing context"))
+                       throw runtime_error(String("  * ") + _("Can't open file") + " \"" + "\"");
+               throw;
+       }
+       catch(const std::exception& ex)
+       {
+               synfig::error("Standard Exception: "+String(ex.what()));
+               errors = ex.what();
+               return Canvas::Handle();
+       }
+       catch(const String& str)
+       {
+               cerr<<str<<endl;
+               errors = str;
+               return Canvas::Handle();
+       }
+       return Canvas::Handle();
+}
+//extern
+Canvas::Handle
+synfig::open_canvas(xmlpp::Element* node,String &errors,String &warnings){
+       Canvas::Handle canvas;
+       CanvasParser parser;
+       parser.set_allow_errors(true);
+       try
+       {
+               canvas=parser.parse_as(node,errors);
+       }
+       catch (...)
+       {
+               synfig::error("open canvas of a node");
+               throw;
+       }
+
+       warnings = parser.get_warnings_text();
+
+       if(parser.error_count())
+       {
+               errors = parser.get_errors_text();
+               return Canvas::Handle();
+       }
+       return canvas;
+}
+
+