1 /***************************************************************************
2 * Copyright (C) 2007 by Tarek Saidi *
3 * tarek.saidi@arcor.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; version 2 of the License. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include "Import_KeePassX_Xml.h"
24 bool Import_KeePassX_Xml::importDatabase(QWidget* Parent, IDatabase* database){
27 QFile* file=openFile(GuiParent,identifier(),QStringList()<<tr("KeePass XML Files (*.xml)")<<tr("All Files (*)"));
28 if(!file)return false;
33 if(!doc.setContent(file,false,&ErrMsg,&ErrLine,&ErrCol)){
34 QMessageBox::critical(GuiParent,tr("Import Failed"),tr("XML parsing error on line %1 column %2:\n%3").arg(ErrLine).arg(ErrCol).arg(ErrMsg));
39 QDomElement root=doc.documentElement();
40 if(root.tagName()!="database"){
41 QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Parsing error: File is no valid KeePassX XML file."));
44 QDomNodeList TopLevelGroupNodes=root.childNodes();
45 QStringList GroupNames;
46 for(int i=0;i<TopLevelGroupNodes.count();i++){
47 if(TopLevelGroupNodes.at(i).toElement().tagName()!="group"){
48 qWarning("Import_KeePassX_Xml: Error: Unknow tag '%s'",CSTR(TopLevelGroupNodes.at(i).toElement().tagName()));
49 QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Parsing error: File is no valid KeePassX XML file."));
52 if(!parseGroup(TopLevelGroupNodes.at(i).toElement(),NULL)){
53 QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Parsing error: File is no valid KeePassX XML file."));
61 bool Import_KeePassX_Xml::parseGroup(const QDomElement& GroupElement,IGroupHandle* ParentGroup){
63 QDomNodeList ChildNodes=GroupElement.childNodes();
64 for(int i=0;i<ChildNodes.size();i++){
65 if(!ChildNodes.item(i).isElement()){
66 qWarning("Import_KeePassX_Xml: Error: Invalid node.");
69 if(ChildNodes.item(i).toElement().tagName()=="icon")
70 Group.Image=ChildNodes.item(i).toElement().text().toInt();
71 else if(ChildNodes.item(i).toElement().tagName()=="title")
72 Group.Title=ChildNodes.item(i).toElement().text();
75 IGroupHandle* GroupHandle=db->addGroup(&Group,ParentGroup);
77 for(int i=0;i<ChildNodes.size();i++){
78 if(ChildNodes.item(i).toElement().tagName()=="entry"){
79 if(!parseEntry(ChildNodes.item(i).toElement(), GroupHandle))return false;
80 }else if(ChildNodes.item(i).toElement().tagName()=="group"){
81 if(!parseGroup(ChildNodes.item(i).toElement(),GroupHandle))return false;
90 bool Import_KeePassX_Xml::parseEntry(const QDomElement& EntryElement,IGroupHandle* Group){
91 if(EntryElement.isNull()){
92 qWarning("Import_KeePassX_Xml: Error: Entry element is null.");
95 IEntryHandle* entry=db->newEntry(Group);
96 QDomNodeList ChildNodes=EntryElement.childNodes();
97 for(int i=0;i<ChildNodes.size();i++){
98 if(!ChildNodes.item(i).isElement()){
99 qWarning("Import_KeePassX_Xml: Error: Invalid node.");
102 if(ChildNodes.item(i).toElement().tagName()=="title")
103 entry->setTitle(ChildNodes.item(i).toElement().text());
104 else if(ChildNodes.item(i).toElement().tagName()=="username")
105 entry->setUsername(ChildNodes.item(i).toElement().text());
106 else if(ChildNodes.item(i).toElement().tagName()=="password"){
108 QString cpw=ChildNodes.item(i).toElement().text();
109 pw.setString(cpw,true);
110 entry->setPassword(pw);
112 else if(ChildNodes.item(i).toElement().tagName()=="url")
113 entry->setUrl(ChildNodes.item(i).toElement().text());
114 else if(ChildNodes.item(i).toElement().tagName()=="icon")
115 entry->setImage(ChildNodes.item(i).toElement().text().toInt());
116 else if(ChildNodes.item(i).toElement().tagName()=="creation")
117 entry->setCreation(KpxDateTime::fromString(ChildNodes.item(i).toElement().text(),Qt::ISODate));
118 else if(ChildNodes.item(i).toElement().tagName()=="lastaccess")
119 entry->setLastAccess(KpxDateTime::fromString(ChildNodes.item(i).toElement().text(),Qt::ISODate));
120 else if(ChildNodes.item(i).toElement().tagName()=="lastmod")
121 entry->setLastMod(KpxDateTime::fromString(ChildNodes.item(i).toElement().text(),Qt::ISODate));
122 else if(ChildNodes.item(i).toElement().tagName()=="expire")
123 entry->setExpire(KpxDateTime::fromString(ChildNodes.item(i).toElement().text(),Qt::ISODate));
124 else if(ChildNodes.item(i).toElement().tagName()=="bindesc")
125 entry->setBinaryDesc(ChildNodes.item(i).toElement().text());
126 else if(ChildNodes.item(i).toElement().tagName()=="bin")
127 entry->setBinary(QByteArray::fromBase64(ChildNodes.item(i).toElement().text().toAscii()));
128 else if(ChildNodes.item(i).toElement().tagName()=="comment"){
129 QDomNodeList Lines=ChildNodes.item(i).childNodes();
131 for(int i=0;i<Lines.size();i++){
132 if(Lines.item(i).isText())
133 comment+=Lines.item(i).toText().data();
134 else if(Lines.item(i).toElement().tagName()=="br")
137 qWarning("Import_KeePassX_Xml: Error: Comment element contains invalid nodes.");
141 entry->setComment(comment);