Initial commit
[keepassx] / src / export / Export_KeePassX_Xml.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Tarek Saidi                                     *
3  *   tarek.saidi@arcor.de                                                  *
4  *                                                                         *
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.               *
8
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "Export_KeePassX_Xml.h"
22
23 bool Export_KeePassX_Xml::exportDatabase(QWidget* GuiParent,IDatabase* database){
24         db=database;    
25         QFile *file=openFile(GuiParent,identifier(),QStringList()<<tr("XML Files (*.xml)") << tr("All Files (*)"));
26         if(!file)return false;
27         QDomDocument doc("KEEPASSX_DATABASE");
28         QDomElement root=doc.createElement("database");
29         doc.appendChild(root);
30         QList<IGroupHandle*> Groups=db->sortedGroups();
31         for(int i=0;i<Groups.size();i++){
32                 if(Groups[i]->parent()==NULL){
33                         addGroup(Groups[i],root,doc);
34                 }
35         }
36         file->write(doc.toByteArray());
37         file->close();
38         delete file;
39         return true;
40 }
41
42 void Export_KeePassX_Xml::addGroup(IGroupHandle* group,QDomElement& parent,QDomDocument& doc){
43         QDomElement GroupElement=doc.createElement("group");
44         parent.appendChild(GroupElement);
45         QDomElement Title=doc.createElement("title");
46         QDomElement Icon=doc.createElement("icon");
47         Title.appendChild(doc.createTextNode(group->title()));
48         Icon.appendChild(doc.createTextNode(QString::number(group->image())));
49         GroupElement.appendChild(Title);
50         GroupElement.appendChild(Icon);
51         QList<IGroupHandle*> children=group->children();
52         for(int i=0;i<children.size();i++){
53                 addGroup(children[i],GroupElement,doc);
54         }
55         QList<IEntryHandle*> entries=db->entriesSortedStd(group);
56         for(int i=0;i<entries.size();i++){
57                 addEntry(entries[i],GroupElement,doc);
58         }
59         
60 }
61
62 void Export_KeePassX_Xml::addEntry(IEntryHandle* entry,QDomElement& parent,QDomDocument& doc){
63         QDomElement GroupElement=doc.createElement("entry");
64         parent.appendChild(GroupElement);
65         QDomElement Title=doc.createElement("title");
66         QDomElement Username=doc.createElement("username");
67         QDomElement Password=doc.createElement("password");
68         QDomElement Url=doc.createElement("url");
69         QDomElement Comment=doc.createElement("comment");
70         QDomElement BinaryDesc=doc.createElement("bindesc");
71         QDomElement Binary=doc.createElement("bin");    
72         QDomElement Icon=doc.createElement("icon");
73         QDomElement Creation=doc.createElement("creation");
74         QDomElement LastAccess=doc.createElement("lastaccess"); 
75         QDomElement LastMod=doc.createElement("lastmod");
76         QDomElement Expire=doc.createElement("expire"); 
77         
78         Title.appendChild(doc.createTextNode(entry->title()));
79         Username.appendChild(doc.createTextNode(entry->username()));
80         SecString password=entry->password();
81         password.unlock();
82         Password.appendChild(doc.createTextNode(password.string()));
83         password.lock();
84         Url.appendChild(doc.createTextNode(entry->url()));
85         QStringList CommentLines=entry->comment().split('\n');
86         for(int i=0;i<CommentLines.size();i++){
87                 Comment.appendChild(doc.createTextNode(CommentLines[i]));
88                 if(i==CommentLines.size()-1)break;
89                 Comment.appendChild(doc.createElement("br"));
90         }
91         bool HasAttachment=!entry->binary().isNull();
92         if(HasAttachment){
93                 BinaryDesc.appendChild(doc.createTextNode(entry->binaryDesc()));
94                 Binary.appendChild(doc.createTextNode(entry->binary().toBase64()));
95         }
96         Icon.appendChild(doc.createTextNode(QString::number(entry->image())));
97         Creation.appendChild(doc.createTextNode(entry->creation().toString(Qt::ISODate)));
98         LastAccess.appendChild(doc.createTextNode(entry->lastAccess().toString(Qt::ISODate)));
99         LastMod.appendChild(doc.createTextNode(entry->lastMod().toString(Qt::ISODate)));
100         Expire.appendChild(doc.createTextNode(entry->expire().toString(Qt::ISODate)));
101         GroupElement.appendChild(Title);
102         GroupElement.appendChild(Username);
103         GroupElement.appendChild(Password);
104         GroupElement.appendChild(Url);
105         GroupElement.appendChild(Comment);
106         if(HasAttachment){
107                 GroupElement.appendChild(BinaryDesc);
108                 GroupElement.appendChild(Binary);
109         }
110         GroupElement.appendChild(Icon);
111         GroupElement.appendChild(Creation);
112         GroupElement.appendChild(LastAccess);
113         GroupElement.appendChild(LastMod);
114         GroupElement.appendChild(Expire);
115 }