Initial commit
[keepassx] / src / Database.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-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
22
23 KpxUuid::KpxUuid() : Data(16,0){
24 }
25
26 KpxUuid::KpxUuid(const void* src){
27         fromRaw(src);
28 }
29
30 void KpxUuid::generate(){
31         char uuid[16];
32         randomize(uuid,16);
33         quint32 Secs=QDateTime::currentDateTime().toTime_t();
34         quint16 mSecs=QTime::currentTime().msec();
35         mSecs=(mSecs & 0x3FF) | (*((quint16*)(uuid+4)) & 0xFC00); //msec has only 10 Bits, filling the rest with random data
36         memcpy((void*)uuid,&Secs,4);
37         memcpy((void*)(uuid+4),&mSecs,2);
38         Data=QByteArray(uuid,16);
39 }
40
41 QString KpxUuid::toString()const{
42         QString hex;
43         Q_ASSERT(Data.length()==16);
44         for(int i=0;i<16;i++){
45                 QString HexByte;
46                 HexByte.setNum((unsigned char)*(Data.data()+i),16);
47                 if(HexByte.length()<2)HexByte="0"+HexByte;
48                 hex+=HexByte;
49         }
50         return QString("{%1-%2-%3-%4-%5}")
51                         .arg(hex.mid(0,8))
52                         .arg(hex.mid(8,4))
53                         .arg(hex.mid(12,4))
54                         .arg(hex.mid(16,4))
55                         .arg(hex.mid(20,12));
56 }
57
58 void KpxUuid::toRaw(void* dst)const{
59         memcpy(dst,Data.data(),16);
60 }
61
62 void KpxUuid::fromRaw(const void* src){
63         Data=QByteArray((char*)src,16);
64 }
65
66 bool KpxUuid::operator==(const KpxUuid& other)const{
67         return other.Data==Data;
68 }
69
70 bool KpxUuid::operator!=(const KpxUuid& other)const{
71         return other.Data!=Data;
72 }
73
74
75
76 QString KpxDateTime::toString(Qt::DateFormat format) const{
77         if (*this==Date_Never)
78                 return QCoreApplication::translate("Database","Never");
79         else if (format==Qt::SystemLocaleDate){
80                 QString strFormat = QLocale::system().dateFormat(QLocale::ShortFormat);
81                 if (!strFormat.contains("dd")) strFormat.replace("d", "dd");
82                 if (!strFormat.contains("MM")) strFormat.replace("M", "MM");
83                 if (!strFormat.contains("yyyy")) strFormat.replace("yy", "yyyy");
84                 if (!strFormat.contains("hh")) strFormat.replace("h", "hh");
85                 if (!strFormat.contains("HH")) strFormat.replace("H", "HH");
86                 if (!strFormat.contains("mm")) strFormat.replace("m", "mm");
87                 if (!strFormat.contains("ss")) strFormat.replace("s", "ss");
88                 return date().toString(strFormat);
89         }
90         else
91                 return QDateTime::toString(format);
92 }
93
94 QString KpxDateTime::dateToString(Qt::DateFormat format) const{
95         if (*this==Date_Never)
96                 return QCoreApplication::translate("Database","Never");
97         else if (format==Qt::SystemLocaleDate){
98                 QString strFormat = QLocale::system().dateFormat(QLocale::ShortFormat);
99                 if (!strFormat.contains("dd")) strFormat.replace("d", "dd");
100                 if (!strFormat.contains("MM")) strFormat.replace("M", "MM");
101                 if (!strFormat.contains("yyyy")) strFormat.replace("yy", "yyyy");
102                 return date().toString(strFormat);
103         }
104         else
105                 return date().toString(format);
106 }
107
108
109 KpxDateTime KpxDateTime::fromString(const QString& string,Qt::DateFormat format){
110         if(string.toLower()=="never")
111                 return Date_Never;
112         else return QDateTime::fromString(string,format);       
113 }
114
115 CEntry::CEntry(){
116         Image=0;
117         GroupId=0;
118         Creation=QDateTime::currentDateTime();
119         LastMod=QDateTime::currentDateTime();
120         LastAccess=QDateTime::currentDateTime();
121         Expire=QDateTime(QDate(2999,12,28),QTime(23,59,59)); //Never
122         Binary=QByteArray();
123 }
124
125 bool KpxDateTime::operator<(const QDateTime& other){
126         if(*this!=Date_Never && other!=Date_Never)return ((QDateTime)(*this)<other);
127         if(*this==Date_Never && other==Date_Never)return false;
128         if(*this==Date_Never)return false;
129         if(other==Date_Never)return true;
130         
131         return false;
132 }
133
134
135 CGroup::CGroup(){
136         Image=0;
137         IsExpanded=false;
138 }
139
140