xml.cpp
author insilmaril
Thu, 22 Sep 2005 12:14:23 +0000
changeset 164 d442a66e9121
parent 160 72cc3873306a
child 166 325958acb69b
permissions -rw-r--r--
new way to handle tmpdir, minor changes
     1 #include "xml.h"
     2 
     3 #include <qmessagebox.h>
     4 #include <qcolor.h>
     5 #include <qstylesheet.h>
     6 #include <iostream>
     7 
     8 #include "misc.h"
     9 #include "settings.h"
    10 #include "linkablemapobj.h"
    11 
    12 #include "version.h"
    13 
    14 static BranchObj *lastBranch;
    15 static FloatObj *lastFloat;
    16 static LinkableMapObj *lastLMO;
    17 
    18 extern Settings settings;
    19 
    20 mapBuilderHandler::mapBuilderHandler() {}
    21 
    22 mapBuilderHandler::~mapBuilderHandler() {}
    23 
    24 QString mapBuilderHandler::errorProtocol() { return errorProt; }
    25 
    26 bool mapBuilderHandler::startDocument()
    27 {
    28     errorProt = "";
    29     state = StateInit;
    30     laststate = StateInit;
    31     branchDepth=0;
    32 	htmldata="";
    33 	isVymPart=false;
    34     return true;
    35 }
    36 
    37 
    38 QString mapBuilderHandler::parseHREF(QString href)
    39 {
    40 	QString type=href.section(":",0,0);
    41 	QString path=href.section(":",1,1);
    42 	if (!tmpDir.endsWith("/"))
    43 		return tmpDir + "/" + path;
    44 	else	
    45 		return tmpDir + path;
    46 }
    47 
    48 bool mapBuilderHandler::startElement  ( const QString&, const QString&,
    49                     const QString& eName, const QXmlAttributes& atts ) 
    50 {
    51     QColor col;
    52 	//cout << "startElement <"<<eName<<">  state="<<state <<"  laststate="<<laststate<<"   loadMode="<<loadMode<<endl;
    53     if ( state == StateInit && (eName == "vymmap")  ) 
    54 	{
    55         state = StateMap;
    56 		if (!atts.value( "version").isEmpty() ) 
    57 		{
    58 			mc->setVersion(atts.value( "version" ));
    59 			if (!mc->checkVersion())
    60 				QMessageBox::warning( 0, "Warning: Version Problem" ,
    61 				   "<h3>Map is newer than VYM</h3>"
    62 				   "<p>The map you are just trying to load was "
    63 				   "saved using vym " +atts.value("version")+". "
    64 				   "The version of this vym is " __VYM_VERSION__
    65 				   ". If you run into problems after pressing "
    66 				   "the ok-button below, updating vym should help.");
    67 
    68 		}
    69 		if (loadMode==NewMap)
    70 		{
    71 			if (!atts.value( "author").isEmpty() )
    72 			{
    73 				mc->setAuthor(atts.value( "author" ) );
    74 			}
    75 			if (!atts.value( "comment").isEmpty() )
    76 			{
    77 				mc->setComment (atts.value( "comment" ) );
    78 			}
    79 			if (!atts.value( "backgroundColor").isEmpty() )
    80 			{
    81 				col.setNamedColor(atts.value("backgroundColor"));
    82 				mc->getCanvas()->setBackgroundColor(col);
    83 			}	    
    84 			if (!atts.value( "linkColorHint").isEmpty() ) 
    85 			{
    86 				if (atts.value("linkColorHint")=="HeadingColor")
    87 					me->setLinkColorHint(HeadingColor);
    88 				else
    89 					me->setLinkColorHint(DefaultColor);
    90 			}
    91 			if (!atts.value( "linkStyle").isEmpty() ) 
    92 			{
    93 				QString s=atts.value("linkStyle");
    94 				if (s=="StyleLine")
    95 					me->setLinkStyle(StyleLine);
    96 				else	
    97 					if (s=="StyleParabel")
    98 						me->setLinkStyle(StyleParabel);
    99 					else	
   100 						if (s=="StylePolyLine")
   101 							me->setLinkStyle(StylePolyLine);
   102 						else	
   103 							me->setLinkStyle(StylePolyParabel);
   104 			}	
   105 			if (!atts.value( "linkColor").isEmpty() ) 
   106 			{
   107 				col.setNamedColor(atts.value("linkColor"));
   108 				me->setLinkColor(col);
   109 			}	
   110 			if (!atts.value( "defXLinkColor").isEmpty() ) 
   111 			{
   112 				col.setNamedColor(atts.value("defXLinkColor"));
   113 				me->setDefXLinkColor(col);
   114 			}	
   115 			if (!atts.value( "defXLinkWidth").isEmpty() ) 
   116 			{
   117 				me->setDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
   118 			}	
   119 		}	
   120 	} else if ( eName == "select" && state == StateMap ) 
   121 	{
   122 		state=StateMapSelect;
   123 	} else if ( eName == "setting" && state == StateMap ) 
   124 	{
   125 		state=StateMapSetting;
   126 		if (loadMode==NewMap)
   127 			readSettingAttr (atts);
   128 	} else if ( eName == "mapcenter" && state == StateMap ) 
   129 	{
   130 		state=StateMapCenter;
   131 		if (loadMode==NewMap)
   132 		{	
   133 			// Really use the found mapcenter as MCO in a new map
   134 			lastBranch=mc;	// avoid empty pointer
   135 		} else
   136 		{
   137 			// Treat the found mapcenter as a branch 
   138 			// in an existing map
   139 			LinkableMapObj* lmo=me->getSelection();
   140 			if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   141 			        || (typeid(*lmo) == typeid(MapCenterObj) ) )
   142 			{
   143 				lastBranch=(BranchObj*)lmo;
   144 				if (loadMode==ImportAdd)
   145 				{
   146 					lastBranch->addBranch();
   147 					lastBranch=lastBranch->getLastBranch();
   148 				} else
   149 					lastBranch->clear();
   150 			} else
   151 				return false;
   152 		}
   153 		readBranchAttr (atts);
   154 	} else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter) 
   155 	{
   156 		state=StateMapCenterStandardFlag;
   157 	} else if ( eName == "heading" && state == StateMapCenter) 
   158 	{
   159 		state=StateMapCenterHeading;
   160 		if (!atts.value( "textColor").isEmpty() ) 
   161 		{
   162 			col.setNamedColor(atts.value("textColor"));
   163 			lastBranch->setColor(col ,false );
   164 		}	    
   165 	} else if ( eName == "note" && state == StateMapCenter) 
   166 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   167 		state=StateMapCenterNote;
   168 		if (!readNoteAttr (atts) ) return false;
   169 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   170 	{
   171 		laststate=state;
   172 		state=StateHtmlNote;
   173     } else if ( eName == "floatimage" && state == StateMapCenter ) 
   174 	{
   175 		state=StateMapCenterFloatImage;
   176         lastBranch->addFloatImage();
   177 		lastFloat=lastBranch->getLastFloatImage();
   178 		if (!readFloatImageAttr(atts)) return false;
   179 	} else if ( eName == "branch" && state == StateMap) 
   180 	{
   181 		// This is used in vymparts, which have no mapcenter!
   182 		isVymPart=true;
   183 		state=StateBranch;
   184 		LinkableMapObj* lmo=me->getSelection();
   185 		if (!lmo)
   186 		{
   187 			// If a vym part is _loaded_ (not imported), 
   188 			// selection==lmo==NULL
   189 			// Treat it like ImportAdd then...
   190 			loadMode=ImportAdd;
   191 			lmo=mc;
   192 		}	
   193 		if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   194 				|| (typeid(*lmo) == typeid(MapCenterObj) ) )
   195 		{
   196 			lastBranch=(BranchObj*)(lmo);
   197 			if (loadMode==ImportAdd)
   198 			{
   199 				lastBranch->addBranch();
   200 				lastBranch=lastBranch->getLastBranch();
   201 				
   202 			} else
   203 				lastBranch->clear();
   204 		} else
   205 			return false;
   206 		branchDepth=1;
   207 		readBranchAttr (atts);
   208 	} else if ( eName == "branch" && state == StateMapCenter) 
   209 	{
   210 		state=StateBranch;
   211 		branchDepth=1;
   212 		lastBranch->addBranch();
   213 		lastBranch=lastBranch->getLastBranch();
   214 		readBranchAttr (atts);
   215 	} else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch) 
   216 	{
   217 		state=StateBranchStandardFlag;
   218 	} else if ( eName == "heading" && state == StateBranch) 
   219 	{
   220 		state=StateBranchHeading;
   221 		if (!atts.value( "textColor").isEmpty() ) 
   222 		{
   223 			col.setNamedColor(atts.value("textColor"));
   224 			lastBranch->setColor(col ,false );
   225 		}	    
   226     } else if ( eName == "note" && state == StateBranch) 
   227 	{
   228         state=StateBranchNote;
   229 		if (!readNoteAttr (atts) ) return false;
   230 	} else if ( eName == "htmlnote" && state == StateBranch) 
   231 	{
   232 		laststate=state;
   233 		state=StateHtmlNote;
   234 		no.clear();
   235 		if (!atts.value( "fonthint").isEmpty() ) 
   236 			no.setFontHint(atts.value ("fonthint") );
   237     } else if ( eName == "floatimage" && state == StateBranch ) 
   238 	{
   239 		state=StateBranchFloatImage;
   240         lastBranch->addFloatImage();
   241 		lastFloat=lastBranch->getLastFloatImage();
   242 		if (!readFloatImageAttr(atts)) return false;
   243     } else if ( eName == "xlink" && state == StateBranch ) 
   244 	{
   245 		state=StateBranchXLink;
   246 		if (!readXLinkAttr (atts)) return false;
   247     } else if ( eName == "branch" && state == StateBranch ) 
   248 	{
   249         lastBranch->addBranch();
   250 		lastBranch=lastBranch->getLastBranch();		
   251         branchDepth++;
   252 		readBranchAttr (atts);
   253     } else if ( eName == "html" && state == StateHtmlNote ) 
   254 	{
   255 		state=StateHtml;
   256 		htmldata="<"+eName;
   257 		readHtmlAttr(atts);
   258 		htmldata+=">";
   259     } else if ( state == StateHtml ) 
   260 	{
   261 		// accept all while in html mode,
   262 		htmldata+="<"+eName;
   263 		readHtmlAttr(atts);
   264 		htmldata+=">";
   265     } else
   266         return false;   // Error
   267     return true;
   268 }
   269 
   270 bool mapBuilderHandler::endElement  ( const QString&, const QString&, const QString &eName)
   271 {
   272 //	cout << "endElement </"<<eName<<">  state="<<state <<"  laststate="<<laststate<<endl;
   273     switch ( state ) 
   274 	{
   275         case StateMapSelect: state=StateMap;  return true;
   276         case StateMapSetting: state=StateMap;  return true;
   277         case StateMapCenter: state=StateMap;  return true;
   278         case StateMapCenterStandardFlag: state=StateMapCenter;  return true;
   279         case StateMapCenterHeading: state=StateMapCenter;  return true;
   280         case StateMapCenterNote: state=StateMapCenter;  return true;
   281         case StateMapCenterFloatImage: state=StateMapCenter;  return true;
   282         case StateBranch: 
   283             if (branchDepth>1) 
   284 			{
   285                 branchDepth--;
   286                 state=StateBranch;
   287             } else  
   288 			{
   289                 branchDepth=0;
   290 				if (isVymPart)
   291 					state=StateMap;
   292 				else
   293 					state=StateMapCenter;
   294             }   
   295 			lastBranch=(BranchObj*)(lastBranch->getParObj());
   296              return true;
   297         case StateBranchStandardFlag: state=StateBranch;  return true;
   298         case StateBranchHeading: state=StateBranch;  return true;
   299         case StateBranchNote: state=StateBranch; return true;
   300         case StateBranchFloatImage: state=StateBranch;  return true;
   301         case StateBranchXLink: state=StateBranch;  return true;
   302         case StateHtmlNote: state=laststate; return true;
   303         case StateHtml: 
   304 			htmldata+="</"+eName+">";
   305 			if (eName=="html")
   306 			{
   307 				state=StateHtmlNote;  
   308 				htmldata.replace ("<br></br>","<br />");
   309 				no.setNote (htmldata);
   310 				lastBranch->setNote (no);
   311 				return true;
   312 			}	else
   313 			{
   314 				return true;
   315 			}	
   316         case StateMap: state=StateInit;  return true;
   317         default : 
   318 			// even for HTML includes, this should never be reached
   319 			return false;
   320     }   
   321 }
   322 
   323 bool mapBuilderHandler::characters   ( const QString& ch)
   324 {
   325 	//cout << "characters \""<<ch<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   326 
   327 	QString ch_org=quotemeta (ch);
   328     QString ch_simplified=ch.simplifyWhiteSpace();
   329     if ( ch_simplified.isEmpty() ) return true;
   330 
   331     switch ( state ) 
   332     {
   333         case StateInit: break;
   334         case StateMap: break; 
   335 		case StateMapSelect:
   336 			me->select(ch_simplified);
   337 			break;
   338 		case StateMapSetting:break;
   339         case StateMapCenter: break;
   340         case StateMapCenterStandardFlag: 
   341             lastBranch->activateStandardFlag(ch_simplified); 
   342             break;
   343         case StateMapCenterHeading: 
   344             lastBranch->setHeading(ch_simplified); 
   345             break;
   346         case StateMapCenterNote:
   347 			lastBranch->setNote(ch_simplified);
   348 			break;
   349         case StateBranch: break;
   350         case StateBranchStandardFlag: 
   351             lastBranch->activateStandardFlag(ch_simplified); 
   352             break;
   353         case StateBranchHeading: 
   354             lastBranch->setHeading(ch_simplified);
   355             break;
   356         case StateBranchNote: 
   357 			lastBranch->setNote(ch_simplified);
   358 			break;
   359         case StateBranchFloatImage: break;
   360         case StateHtmlNote: break;
   361         case StateHtml:
   362 			htmldata+=ch_org;
   363 			break;
   364         default: 
   365 			return false;
   366     }
   367     return true;
   368 }
   369 
   370 QString mapBuilderHandler::errorString() 
   371 {
   372     return "the document is not in the VYM file format";
   373 }
   374 
   375 bool mapBuilderHandler::fatalError( const QXmlParseException& exception ) 
   376 {
   377     errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n")
   378     .arg( exception.message() )
   379     .arg( exception.lineNumber() )
   380     .arg( exception.columnNumber() );
   381 
   382     return QXmlDefaultHandler::fatalError( exception );
   383 }
   384 
   385 void mapBuilderHandler::setMapEditor (MapEditor* e)
   386 {
   387     me=e;
   388 	mc=me->getMapCenter();
   389 }
   390 
   391 void mapBuilderHandler::setTmpDir (QString tp)
   392 {
   393 	tmpDir=tp;
   394 }
   395 
   396 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
   397 {
   398 	loadMode=lm;
   399 }
   400 
   401 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
   402 {
   403 	lastLMO=lastBranch;
   404 	if (!readLinkAttr(a)) return false;
   405 
   406 	bool okx,oky;
   407 	int x,y;
   408 	if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2) 
   409 	{
   410 		if (!a.value( "absPosY").isEmpty() ) 
   411 		{
   412 			x=a.value("absPosX").toInt (&okx, 10);
   413 			y=a.value("absPosY").toInt (&oky, 10);
   414 			if (okx && oky) 
   415 				lastBranch->move(x,y);
   416 			else
   417 				return false;   // Couldn't read absPos
   418 		}           
   419 	}           
   420 	if (!a.value( "scrolled").isEmpty() )
   421 		lastBranch->toggleScroll();
   422 	if (!a.value( "url").isEmpty() ) 
   423 		lastBranch->setURL (a.value ("url"));
   424 	if (!a.value( "vymLink").isEmpty() ) 
   425 		lastBranch->setVymLink (a.value ("vymLink"));
   426 	if (!a.value( "frameType").isEmpty() ) 
   427 		lastBranch->setFrameType (a.value("frameType"));
   428 	return true;	
   429 }
   430 
   431 bool mapBuilderHandler::readLinkAttr (const QXmlAttributes& a)
   432 {
   433 	if (!a.value( "hideLinkUnselected").isEmpty()) 
   434 	{
   435 		if (a.value ("hideLinkUnselected") =="true")
   436 			if (lastLMO) lastLMO->setHideLinkUnselected(true);
   437 	}	
   438 	return true;	
   439 }
   440 
   441 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
   442 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   443 	no.clear();
   444 	QString fn;
   445 	if (!a.value( "href").isEmpty() ) 
   446 	{
   447 		// Load note
   448 		fn=parseHREF(a.value ("href") );
   449 		QFile file (fn);
   450 		QString s;						// Reading a note
   451 
   452 		if ( !file.open( IO_ReadOnly) )
   453 		{
   454 			qWarning ("mapBuilderHandler::readNoteAttr:  Couldn't load "+fn);
   455 			return false;
   456 		}	
   457 		QTextStream stream( &file );
   458 		QString lines;
   459 		while ( !stream.eof() ) {
   460 			lines += stream.readLine()+"\n"; 
   461 		}
   462 		file.close();
   463 		// Convert to richtext
   464 		if ( !QStyleSheet::mightBeRichText( lines ) )
   465 		{
   466 			// Here we are workarounding the QT conversion method:
   467 			// convertFromPlainText does not generate valid xml, needed
   468 			// for the parser, but just <p> and <br> without closing tags.
   469 			// So we have to add those by ourselves
   470 			//lines=quotemeta (lines);
   471 			lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal );
   472 			lines.replace ("<br>","<br />");
   473 		}	
   474 
   475 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   476 		no.setNote (lines);
   477 	}		
   478 	if (!a.value( "fonthint").isEmpty() ) 
   479 		no.setFontHint(a.value ("fonthint") );
   480 	if (state == StateMapCenterNote) 	
   481 		mc->setNote(no);
   482 	else
   483 		lastBranch->setNote(no);
   484 	return true;
   485 }
   486 
   487 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
   488 {
   489 	lastLMO=lastFloat;
   490 	if (!readLinkAttr(a)) return false;
   491 	
   492 	if (!a.value( "useOrientation").isEmpty() ) 
   493 	{
   494 		if (a.value ("useOrientation") =="true")
   495 			lastFloat->setUseOrientation (true);
   496 		else	
   497 			lastFloat->setUseOrientation (false);
   498 	}	
   499 	if (!a.value( "href").isEmpty() )
   500 	{
   501 		// Load FloatImage
   502 		if (!lastFloat->load (parseHREF(a.value ("href") ) ))
   503 		{
   504 			QMessageBox::warning( 0, "Warning: " ,
   505 				"Couldn't load float image\n"+parseHREF(a.value ("href") ));
   506 			lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
   507 			lastFloat=NULL;
   508 			return true;
   509 		}
   510 		
   511 	}	
   512 	if (!a.value( "floatExport").isEmpty() ) 
   513 	{
   514 		if (a.value ("floatExpofrt") =="true")
   515 			lastFloat->setFloatExport (true);
   516 		else	
   517 			lastFloat->setFloatExport (false);
   518 	}	
   519 	if (!a.value( "zPlane").isEmpty() ) 
   520 		lastFloat->setZ (a.value("zPlane").toInt ());
   521     int x,y;
   522     bool okx,oky;
   523 	if (!a.value( "relPosX").isEmpty() ) 
   524 	{
   525 		if (!a.value( "relPosY").isEmpty() ) 
   526 		{
   527 			// read relPos
   528 			x=a.value("relPosX").toInt (&okx, 10);
   529 			y=a.value("relPosY").toInt (&oky, 10);
   530 			if (okx && oky) 
   531 				lastFloat->setRelPos (QPoint (x,y) );
   532 			else
   533 				// Couldn't read relPos
   534 				return false;  
   535 		}           
   536 	}	
   537 	return true;
   538 }
   539 
   540 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
   541 {
   542 	QColor col;
   543 	bool okx;
   544 	bool success=false;
   545 	XLinkObj *xlo=new XLinkObj (mc->getCanvas());
   546 	if (!a.value( "color").isEmpty() ) 
   547 	{
   548 		col.setNamedColor(a.value("color"));
   549 		xlo->setColor (col);
   550 	}
   551 
   552 	if (!a.value( "width").isEmpty() ) 
   553 	{
   554 		xlo->setWidth(a.value ("width").toInt (&okx, 10));
   555 	}
   556 
   557 	if (!a.value( "beginBranch").isEmpty() ) 
   558 	{
   559 		if (!a.value( "endBranch").isEmpty() ) 
   560 		{
   561 			LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
   562 			if (lmo && typeid (*lmo)==typeid (BranchObj))
   563 			{
   564 				xlo->setBegin ((BranchObj*)(lmo));
   565 				lmo=mc->findObjBySelect (a.value( "endBranch"));
   566 				if (lmo && typeid (*lmo)==typeid (BranchObj))
   567 				{
   568 					xlo->setEnd ((BranchObj*)(lmo));
   569 					xlo->activate();
   570 				}
   571 			}
   572 			success=true; // Not all branches there yet, no error
   573 		}           
   574 	}	
   575 	if (!success) delete (xlo);
   576 	return success;
   577 }
   578 
   579 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
   580 {
   581 	for (int i=1; i<=a.count(); i++)
   582 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   583 	return true;
   584 }
   585 
   586 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
   587 {
   588 	if (!a.value( "key").isEmpty() ) 
   589 	{
   590 		if (!a.value( "value").isEmpty() ) 
   591 			settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));
   592 		else
   593 			return false;
   594 		
   595 	} else
   596 		return false;
   597 	
   598 	return true;
   599 }