added missing files to svn.

git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@126 b624d157-de02-0410-bad0-e51aec6abb33
This commit is contained in:
tarek_saidi
2007-03-18 13:11:43 +00:00
parent 50a39d726a
commit 11b33969d8
20 changed files with 1817 additions and 24 deletions

43
src/import/Import.cpp Normal file
View File

@@ -0,0 +1,43 @@
/***************************************************************************
* Copyright (C) 2007 by Tarek Saidi *
* tarek.saidi@arcor.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QMessageBox>
#include "main.h"
#include "Import.h"
#include "lib/FileDialogs.h"
#include "dialogs/SimplePasswordDlg.h"
QFile* ImporterBase::openFile(QWidget* parent, QString id, QStringList Filters){
QString filename=KpxFileDialogs::openExistingFile(parent,id,tr("Import File..."),Filters);
if(filename==QString())return NULL;
QFile* file=new QFile(filename);
if(!file->open(QIODevice::ReadOnly)){
QMessageBox::critical(parent,tr("Import Failed"),decodeFileError(file->error()));
delete file;
return NULL;
}
return file;
}
QString ImporterBase::getPassword(QWidget* parent){
SimplePasswordDialog dlg(parent);
dlg.exec();
return dlg.password;
}

42
src/import/Import.h Normal file
View File

@@ -0,0 +1,42 @@
/***************************************************************************
* Copyright (C) 2007 by Tarek Saidi *
* tarek.saidi@arcor.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _IMPORT_H_
#define _IMPORT_H_
#include <QWidget>
#include <QFile>
#include "Database.h"
class IImport{
public:
virtual ~IImport(){};
virtual bool importDatabase(QWidget* GuiParent, IDatabase* Database)=0;
virtual QString identifier()=0;
virtual QString title()=0;
};
class ImporterBase:public QObject{
protected:
virtual QFile* openFile(QWidget* GuiParent,QString id,QStringList Filter);
virtual QString getPassword(QWidget* GuiParent);
};
#endif

View File

@@ -0,0 +1,130 @@
/***************************************************************************
* Copyright (C) 2006 by Brian Johnson *
* dev-keepassx@sherbang.com *
* *
* Copyright (C) 2007 by Tarek Saidi *
* tarek.saidi@arcor.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include <QtXml>
#include <QtCore>
#include "IImport.h"
#include "Import_GnuKeyRing.h"
#include "lib/FileDialogs.h"
#include "main.h"
using namespace std;
QString Import_GnuKeyRing::importDatabase(QWidget* GuiParent, IDatabase* Database){
QString FileName=KpxFileDialogs::openExistingFile(GuiParent,
"Import_Keyring",
tr("Import Database..."),
QStringList()<<tr("All Files (*)"));
if(FileName==QString())
return QString();
QFile file(FileName);
if(!file.exists()){
return tr("File not found.");
}
if(!file.open(QIODevice::ReadOnly)){
return decodeFileError(file.error());
}
int len=file.size();
if(len==0){
return tr("File is empty.");
}
QTextStream ts(&file);
uint entryNum = 0;
QRegExp newEntry("^#\\d*$");
QMap<QString, CGroup*> categories;
/*
CGroup* DefaultGroup=pwm->addGroup(NULL);
DefaultGroup->Name="def-group";
QString* field = NULL;
QString category;
QString name;
QString account;
QString password;
QString note;
while (!ts.atEnd()){
QString line = ts.readLine(400);
if (newEntry.exactMatch(line)){
//Save entry
CGroup* newGroup;
if (entryNum != 0){
if (category != ""){
if (categories.contains(category)){
newGroup=categories[category];
}else{
newGroup=pwm->addGroup(NULL);
newGroup->Name=category;
categories.insert(category, newGroup);
}
}else{
newGroup = DefaultGroup;
}
CEntry* NewEntry=pwm->addEntry();
NewEntry->GroupID=newGroup->ID;
NewEntry->Title=name;
NewEntry->UserName=account;
NewEntry->Password.setString(password,true);
NewEntry->Additional=note;
}
//New Entry
category = "";
name = "";
account = "";
password = "";
note = "";
entryNum = line.remove(0,1).toUInt();
}else if(entryNum == 0){
//Do nothing (waiting for start of first entry)
}else if(line.startsWith("Category:")){
category = line.remove(0,10);
field = &category;
}else if(line.startsWith("Name:")){
name = line.remove(0,6);
field = &name;
}else if(line.startsWith("Account:")){
account = line.remove(0,9);
field = &account;
}else if(line.startsWith("Password:")){
password = line.remove(0,10);
field = &password;
}else if(line.startsWith("Notes:")){
note = line.remove(0,7);
field = &note;
}else if(field != NULL){
field->append("\n");
field->append(line);
}
}
file.close();
pwm->SearchGroupID=-1;
pwm->CryptoAlgorithmus=ALGO_AES;
pwm->KeyEncRounds=6000;
return true;
*/
return QString();
}

View File

@@ -0,0 +1,34 @@
/***************************************************************************
* Copyright (C) 2007 by Tarek Saidi *
* tarek.saidi@arcor.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _IMPORT_KEYRING_H_
#define _IMPORT_KEYRING_H_
#include <QWidget>
#include "Database.h"
class Import_GnuKeyRing:public QObject,public IImport{
public:
virtual QString importDatabase(QWidget* GuiParent, IDatabase* Database);
virtual QString name(){return "GnuKeyRing";}
};
#endif

View File

@@ -0,0 +1,147 @@
/***************************************************************************
* Copyright (C) 2007 by Tarek Saidi *
* tarek.saidi@arcor.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QStringList>
#include <QMessageBox>
#include <QtXml>
#include "Import_KeePassX_Xml.h"
bool Import_KeePassX_Xml::importDatabase(QWidget* Parent, IDatabase* database){
db=database;
GuiParent=Parent;
QFile* file=openFile(GuiParent,identifier(),QStringList()<<tr("KeePass XML Files (*.xml)")<<tr("All Files (*)"));
if(!file)return false;
QDomDocument doc;
QString ErrMsg;
int ErrLine;
int ErrCol;
if(!doc.setContent(file,false,&ErrMsg,&ErrLine,&ErrCol)){
QMessageBox::critical(GuiParent,tr("Import Failed"),tr("XML parsing error on line %1 column %2:\n%3").arg(ErrLine).arg(ErrCol).arg(ErrMsg));
delete file;
return false;
}
delete file;
QDomElement root=doc.documentElement();
if(root.tagName()!="database"){
QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Parsing error: File is no valid KeePassX XML file."));
return false;
}
QDomNodeList TopLevelGroupNodes=root.childNodes();
QStringList GroupNames;
for(int i=0;i<TopLevelGroupNodes.count();i++){
if(TopLevelGroupNodes.at(i).toElement().tagName()!="group"){
qWarning("Import_KeePassX_Xml: Error: Unknow tag '%s'",TopLevelGroupNodes.at(i).toElement().tagName());
QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Parsing error: File is no valid KeePassX XML file."));
return false;
}
if(!parseGroup(TopLevelGroupNodes.at(i).toElement(),NULL)){
QMessageBox::critical(GuiParent,tr("Import Failed"),tr("Parsing error: File is no valid KeePassX XML file."));
return false;}
}
return true;
}
bool Import_KeePassX_Xml::parseGroup(const QDomElement& GroupElement,IGroupHandle* ParentGroup){
CGroup Group;
QDomNodeList ChildNodes=GroupElement.childNodes();
for(int i=0;i<ChildNodes.size();i++){
if(!ChildNodes.item(i).isElement()){
qWarning("Import_KeePassX_Xml: Error: Invalid node.");
return false;
}
if(ChildNodes.item(i).toElement().tagName()=="icon")
Group.Image=ChildNodes.item(i).toElement().text().toInt();
else if(ChildNodes.item(i).toElement().tagName()=="title")
Group.Title=ChildNodes.item(i).toElement().text();
}
IGroupHandle* GroupHandle=db->addGroup(&Group,ParentGroup);
for(int i=0;i<ChildNodes.size();i++){
if(ChildNodes.item(i).toElement().tagName()=="entry"){
if(!parseEntry(ChildNodes.item(i).toElement(), GroupHandle))return false;
}else if(ChildNodes.item(i).toElement().tagName()=="group"){
if(!parseGroup(ChildNodes.item(i).toElement(),GroupHandle))return false;
}
}
return true;
}
bool Import_KeePassX_Xml::parseEntry(const QDomElement& EntryElement,IGroupHandle* Group){
if(EntryElement.isNull()){
qWarning("Import_KeePassX_Xml: Error: Entry element is null.");
return false;
}
IEntryHandle* entry=db->newEntry(Group);
QDomNodeList ChildNodes=EntryElement.childNodes();
for(int i=0;i<ChildNodes.size();i++){
if(!ChildNodes.item(i).isElement()){
qWarning("Import_KeePassX_Xml: Error: Invalid node.");
return false;
}
if(ChildNodes.item(i).toElement().tagName()=="title")
entry->setTitle(ChildNodes.item(i).toElement().text());
else if(ChildNodes.item(i).toElement().tagName()=="username")
entry->setUsername(ChildNodes.item(i).toElement().text());
else if(ChildNodes.item(i).toElement().tagName()=="password"){
SecString pw;
QString cpw=ChildNodes.item(i).toElement().text();
pw.setString(cpw,true);
entry->setPassword(pw);
}
else if(ChildNodes.item(i).toElement().tagName()=="url")
entry->setUrl(ChildNodes.item(i).toElement().text());
else if(ChildNodes.item(i).toElement().tagName()=="icon")
entry->setImage(ChildNodes.item(i).toElement().text().toInt());
else if(ChildNodes.item(i).toElement().tagName()=="creation")
entry->setCreation(KpxDateTime::fromString(ChildNodes.item(i).toElement().text(),Qt::ISODate));
else if(ChildNodes.item(i).toElement().tagName()=="lastaccess")
entry->setLastAccess(KpxDateTime::fromString(ChildNodes.item(i).toElement().text(),Qt::ISODate));
else if(ChildNodes.item(i).toElement().tagName()=="lastmod")
entry->setLastMod(KpxDateTime::fromString(ChildNodes.item(i).toElement().text(),Qt::ISODate));
else if(ChildNodes.item(i).toElement().tagName()=="expire")
entry->setExpire(KpxDateTime::fromString(ChildNodes.item(i).toElement().text(),Qt::ISODate));
else if(ChildNodes.item(i).toElement().tagName()=="bindesc")
entry->setBinaryDesc(ChildNodes.item(i).toElement().text());
else if(ChildNodes.item(i).toElement().tagName()=="bin")
entry->setBinary(QByteArray::fromBase64(ChildNodes.item(i).toElement().text().toAscii()));
else if(ChildNodes.item(i).toElement().tagName()=="comment"){
QDomNodeList Lines=ChildNodes.item(i).childNodes();
QString comment;
for(int i=0;i<Lines.size();i++){
if(Lines.item(i).isText())
comment+=Lines.item(i).toText().data();
else if(Lines.item(i).toElement().tagName()=="br")
comment+="\n";
else{
qWarning("Import_KeePassX_Xml: Error: Comment element contains invalid nodes.");
return false;
}
}
entry->setComment(comment);
}
}
return true;
}

View File

@@ -0,0 +1,41 @@
/***************************************************************************
* Copyright (C) 2007 by Tarek Saidi *
* tarek.saidi@arcor.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _IMPORT_KPX_XML_H_
#define _IMPORT_KPX_XML_H_
#include <QDomElement>
#include "Import.h"
class Import_KeePassX_Xml:public IImport, public ImporterBase{
public:
virtual bool importDatabase(QWidget* GuiParent, IDatabase* Database);
virtual QString identifier(){return "KeePassX_Xml";}
virtual QString title(){return "KeePassX XML (*.xml)";}
private:
bool parseGroup(const QDomElement& GroupElement,IGroupHandle* ParentGroup);
bool parseEntry(const QDomElement& EntryElement,IGroupHandle* Group);
IDatabase* db;
QWidget* GuiParent;
};
#endif