Initial commit
[keepassx] / src / import / Import_GnuKeyRing.cpp
1 /***************************************************************************
2  *   Copyright (C) 2006 by Brian Johnson                                   *
3  *   dev-keepassx@sherbang.com                                             *
4  *                                                                                                                                                 *
5  *   Copyright (C) 2007 by Tarek Saidi                                         *
6  *   tarek.saidi@arcor.de                                                  *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23
24 #include <iostream>
25 #include <QtXml>
26 #include <QtCore>
27
28 #include "IImport.h"
29 #include "Import_GnuKeyRing.h"
30 #include "lib/FileDialogs.h"
31 #include "main.h"
32
33 using namespace std;
34
35 QString Import_GnuKeyRing::importDatabase(QWidget* GuiParent, IDatabase* Database){
36         QString FileName=KpxFileDialogs::openExistingFile(GuiParent,
37                                                                                                          "Import_Keyring",
38                                                                                                          tr("Import Database..."),
39                                                                                                          QStringList()<<tr("All Files (*)"));
40         
41         if(FileName==QString())
42                 return QString();
43         
44         QFile file(FileName);
45         if(!file.exists()){
46                 return tr("File not found.");
47         }
48         if(!file.open(QIODevice::ReadOnly)){
49                 return decodeFileError(file.error());
50         }
51         int len=file.size();
52         if(len==0){
53                 return tr("File is empty.");
54         }
55         QTextStream ts(&file);
56         
57         uint entryNum = 0;
58         QRegExp newEntry("^#\\d*$");
59         QMap<QString, CGroup*> categories;
60         /*
61         CGroup* DefaultGroup=pwm->addGroup(NULL);
62         DefaultGroup->Name="def-group";
63         QString* field = NULL;
64         QString category;
65         QString name;
66         QString account;
67         QString password;
68         QString note;
69         while (!ts.atEnd()){
70                 QString line = ts.readLine(400);
71         if (newEntry.exactMatch(line)){
72                 //Save entry
73                 CGroup* newGroup;
74                 if (entryNum != 0){
75                 if (category != ""){
76                         if (categories.contains(category)){
77                         newGroup=categories[category];
78                         }else{
79                         newGroup=pwm->addGroup(NULL);
80                         newGroup->Name=category;
81                         categories.insert(category, newGroup);
82                         }
83                 }else{
84                         newGroup = DefaultGroup;
85                 }
86         
87                 CEntry* NewEntry=pwm->addEntry();
88                 NewEntry->GroupID=newGroup->ID;
89                 NewEntry->Title=name;
90                 NewEntry->UserName=account;
91                 NewEntry->Password.setString(password,true);
92                 NewEntry->Additional=note;
93                 }
94                 //New Entry
95                 category = "";
96                 name = "";
97                 account = "";
98                 password = "";
99                 note = "";
100                 entryNum = line.remove(0,1).toUInt();
101         }else if(entryNum == 0){
102                 //Do nothing (waiting for start of first entry)
103         }else if(line.startsWith("Category:")){
104                 category = line.remove(0,10);
105                 field = &category;
106         }else if(line.startsWith("Name:")){
107                 name = line.remove(0,6);
108                 field = &name;
109         }else if(line.startsWith("Account:")){
110                 account = line.remove(0,9);
111                 field = &account;
112         }else if(line.startsWith("Password:")){
113                 password = line.remove(0,10);
114                 field = &password;
115         }else if(line.startsWith("Notes:")){
116                 note = line.remove(0,7);
117                 field = &note;
118         }else if(field != NULL){
119                 field->append("\n");
120                 field->append(line);
121         }
122         }
123         file.close();
124         pwm->SearchGroupID=-1;
125         pwm->CryptoAlgorithmus=ALGO_AES;
126         pwm->KeyEncRounds=6000;
127         return true;
128         */
129         return QString();
130