1 /*
2  *  Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 2 or (at your option)
7  *  version 3 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef KEEPASSX_GROUP_H
19 #define KEEPASSX_GROUP_H
20 
21 #include <QImage>
22 #include <QPixmap>
23 #include <QPixmapCache>
24 #include <QPointer>
25 
26 #include "core/Database.h"
27 #include "core/Entry.h"
28 #include "core/TimeInfo.h"
29 #include "core/Uuid.h"
30 
31 class Group : public QObject
32 {
33     Q_OBJECT
34 
35 public:
36     enum TriState { Inherit, Enable, Disable };
37 
38     struct GroupData
39     {
40         QString name;
41         QString notes;
42         int iconNumber;
43         Uuid customIcon;
44         TimeInfo timeInfo;
45         bool isExpanded;
46         QString defaultAutoTypeSequence;
47         Group::TriState autoTypeEnabled;
48         Group::TriState searchingEnabled;
49     };
50 
51     Group();
52     ~Group();
53 
54     static Group* createRecycleBin();
55 
56     Uuid uuid() const;
57     QString name() const;
58     QString notes() const;
59     QImage icon() const;
60     QPixmap iconPixmap() const;
61     QPixmap iconScaledPixmap() const;
62     int iconNumber() const;
63     Uuid iconUuid() const;
64     TimeInfo timeInfo() const;
65     bool isExpanded() const;
66     QString defaultAutoTypeSequence() const;
67     Group::TriState autoTypeEnabled() const;
68     Group::TriState searchingEnabled() const;
69     bool resolveSearchingEnabled() const;
70     bool resolveAutoTypeEnabled() const;
71     Entry* lastTopVisibleEntry() const;
72     bool isExpired() const;
73 
74     static const int DefaultIconNumber;
75     static const int RecycleBinIconNumber;
76 
77     void setUuid(const Uuid& uuid);
78     void setName(const QString& name);
79     void setNotes(const QString& notes);
80     void setIcon(int iconNumber);
81     void setIcon(const Uuid& uuid);
82     void setTimeInfo(const TimeInfo& timeInfo);
83     void setExpanded(bool expanded);
84     void setDefaultAutoTypeSequence(const QString& sequence);
85     void setAutoTypeEnabled(TriState enable);
86     void setSearchingEnabled(TriState enable);
87     void setLastTopVisibleEntry(Entry* entry);
88     void setExpires(bool value);
89     void setExpiryTime(const QDateTime& dateTime);
90 
91     void setUpdateTimeinfo(bool value);
92 
93     Group* parentGroup();
94     const Group* parentGroup() const;
95     void setParent(Group* parent, int index = -1);
96 
97     Database* database();
98     const Database* database() const;
99     QList<Group*> children();
100     const QList<Group*>& children() const;
101     QList<Entry*> entries();
102     const QList<Entry*>& entries() const;
103     QList<Entry*> entriesRecursive(bool includeHistoryItems = false) const;
104     QList<const Group*> groupsRecursive(bool includeSelf) const;
105     QList<Group*> groupsRecursive(bool includeSelf);
106     QSet<Uuid> customIconsRecursive() const;
107     /**
108      * Creates a duplicate of this group including all child entries and groups.
109      * The exceptions are that the returned group doesn't have a parent group
110      * and all TimeInfo attributes are set to the current time.
111      * Note that you need to copy the custom icons manually when inserting the
112      * new group into another database.
113      */
114     Group* clone(Entry::CloneFlags entryFlags = Entry::CloneNewUuid | Entry::CloneResetTimeInfo) const;
115     void copyDataFrom(const Group* other);
116 
117 Q_SIGNALS:
118     void dataChanged(Group* group);
119 
120     void aboutToAdd(Group* group, int index);
121     void added();
122     void aboutToRemove(Group* group);
123     void removed();
124     /**
125      * Group moved within the database.
126      */
127     void aboutToMove(Group* group, Group* toGroup, int index);
128     void moved();
129 
130     void entryAboutToAdd(Entry* entry);
131     void entryAdded(Entry* entry);
132     void entryAboutToRemove(Entry* entry);
133     void entryRemoved(Entry* entry);
134 
135     void entryDataChanged(Entry* entry);
136 
137     void modified();
138 
139 private:
140     template <class P, class V> bool set(P& property, const V& value);
141 
142     void addEntry(Entry* entry);
143     void removeEntry(Entry* entry);
144     void setParent(Database* db);
145 
146     void recSetDatabase(Database* db);
147     void cleanupParent();
148     void recCreateDelObjects();
149     void updateTimeinfo();
150 
151     QPointer<Database> m_db;
152     Uuid m_uuid;
153     GroupData m_data;
154     QPointer<Entry> m_lastTopVisibleEntry;
155     QList<Group*> m_children;
156     QList<Entry*> m_entries;
157 
158     QPointer<Group> m_parent;
159 
160     bool m_updateTimeinfo;
161 
162     friend void Database::setRootGroup(Group* group);
163     friend Entry::~Entry();
164     friend void Entry::setGroup(Group* group);
165 };
166 
167 #endif // KEEPASSX_GROUP_H
168