summaryrefslogtreecommitdiff
path: root/plugins/Dbx_tree/src/DataBase.h
blob: b19ceebb1cfc7b54db89d02e52a280d8104febb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*

dbx_tree: tree database driver for Miranda IM

Copyright 2007-2010 Michael "Protogenes" Kunz,

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.

*/

#pragma once

#include <stdint.h>
#include "MREWSync.h"

#include "Events.h"
#include "Settings.h"
#include "Entities.h"

#include "FileAccess.h"
#include "MappedMemory.h"
#include "DirectAccess.h"
#include "Blockmanager.h"

#include "sigslot.h"

#include "EncryptionManager.h"

typedef enum TDBFileType {
	DBFileSetting = 0,
	DBFilePrivate = 1,
	DBFileMax = 2
} TDBFileType;

static const uint8_t cFileSignature[DBFileMax][20] = {"Miranda IM Settings", "Miranda IM DataTree"};
static const uint32_t cDBVersion = 0x00000001;

static const uint32_t cHeaderBlockSignature = 0x7265491E;

#pragma pack(push, 1)  // push current alignment to stack, set alignment to 1 byte boundary

typedef struct TSettingsHeader {
	uint8_t Signature[20];          /// signature must be cSettingsHeader
	uint32_t Version;               /// internal DB version cDataBaseVersion
	uint32_t Obscure;
	TFileEncryption FileEncryption; /// Encryption Method
	uint32_t FileStructureBlock;    /// Offset of CBlockManager master block
	uint32_t FileSize;              /// Offset to the last used byte + 1		
	uint32_t Settings;              /// Offset to the SettingsBTree RootNode	
	uint8_t Reserved[256 - sizeof(TFileEncryption) - 20 - 5*sizeof(uint32_t)]; /// reserved storage
} TSettingsHeader;

typedef struct TPrivateHeader {
	uint8_t Signature[20];          /// signature must be CDataHeader
	uint32_t Version;               /// internal DB version cDataBaseVersion
	uint32_t Obscure;
	TFileEncryption FileEncryption; /// Encryption Method
	uint32_t FileStructureBlock;    /// Offset of CBlockManager master block
	uint32_t FileSize;              /// Offset to the last used byte + 1
	uint32_t RootEntity;            /// Offset to the Root CList Entity
	uint32_t Entities;              /// Offset to the EntityBTree RootNode
	uint32_t Virtuals;              /// Offset to the VirtualsBTree RootNode
	uint8_t Reserved[256 - sizeof(TFileEncryption) - 20 - 7*sizeof(uint32_t)]; /// reserved storage
} TPrivateHeader;


typedef union TGenericFileHeader {
	struct {
		uint8_t Signature[20];          /// signature must be cSettingsHeader
		uint32_t Version;               /// internal DB version cDataBaseVersion
		uint32_t Obscure;
		TFileEncryption FileEncryption; /// Encryption Method
		uint32_t FileStructureBlock;    /// Offset of CBlockManager master block
		uint32_t FileSize;              /// Offset to the last used byte + 1	
		uint8_t Reserved[256 - sizeof(TFileEncryption) - 20 - 4*sizeof(uint32_t)]; /// reserved storage
	} Gen;
	TSettingsHeader Set;
	TPrivateHeader Pri;
} TGenericFileHeader;

#pragma pack(pop)

class CDataBase : public sigslot::has_slots<>, public MIDatabase
{
private:
	TCHAR* m_FileName[DBFileMax];
	bool m_Opened;

	CBlockManager *m_BlockManager[DBFileMax];
	CFileAccess *m_FileAccess[DBFileMax];
	TGenericFileHeader * m_Header[DBFileMax];
	CEncryptionManager *m_EncryptionManager[DBFileMax];

	uint32_t m_HeaderBlock[DBFileMax];

	void onSettingsRootChanged(CSettings* Settings, CSettingsTree::TNodeRef NewRoot);
	void onVirtualsRootChanged(void* Virtuals, CVirtuals::TNodeRef NewRoot);
	void onEntitiesRootChanged(void* Entities, CEntities::TNodeRef NewRoot);
	void onFileSizeChanged(CFileAccess * File, uint32_t Size);

	bool PrivateFileExists();
	bool CreateNewFile(TDBFileType File);

	int CheckFile(TDBFileType Index);
	int LoadFile(TDBFileType Index);
protected:
	CEntities *m_Entities;
	CSettings *m_Settings;
	CEvents   *m_Events;

	void ReWriteHeader(TDBFileType Index);

public:
	CDataBase(const TCHAR* FileName);
	virtual ~CDataBase();

	int CreateDB();
	int CheckDB();
	int OpenDB();

	CEntities & getEntities()
	{
		return *m_Entities;
	}
	CSettings & getSettings()
	{
		return *m_Settings;
	}
	CEvents   & getEvents()
	{
		return *m_Events;
	}

	int getProfileName(int BufferSize, char * Buffer);
	int getProfilePath(int BufferSize, char * Buffer);

public:     // services 
	INT_PTR __cdecl  DBEntityGetRoot(WPARAM wParam, LPARAM lParam);
	INT_PTR __cdecl  DBEntityChildCount(WPARAM hEntity, LPARAM lParam);
	INT_PTR __cdecl  DBEntityGetParent(WPARAM hEntity, LPARAM lParam);
	INT_PTR __cdecl  DBEntityMove(WPARAM hEntity, LPARAM hParent);
	INT_PTR __cdecl  DBEntityGetFlags(WPARAM hEntity, LPARAM lParam);
	INT_PTR __cdecl  DBEntityIterInit(WPARAM pFilter, LPARAM hParent);
	INT_PTR __cdecl  DBEntityIterNext(WPARAM hIteration, LPARAM lParam);
	INT_PTR __cdecl  DBEntityIterClose(WPARAM hIteration, LPARAM lParam);
	INT_PTR __cdecl  DBEntityDelete(WPARAM hEntity, LPARAM lParam);
	INT_PTR __cdecl  DBEntityCreate(WPARAM pEntity, LPARAM lParam);
	INT_PTR __cdecl  DBEntityGetAccount(WPARAM hEntity, LPARAM lParam);

	INT_PTR __cdecl  DBVirtualEntityCreate(WPARAM hEntity, LPARAM hParent);
	INT_PTR __cdecl  DBVirtualEntityGetParent(WPARAM hVirtualEntity, LPARAM lParam);
	INT_PTR __cdecl  DBVirtualEntityGetFirst(WPARAM hEntity, LPARAM lParam);
	INT_PTR __cdecl  DBVirtualEntityGetNext(WPARAM hVirtualEntity, LPARAM lParam);

	INT_PTR __cdecl  DBSettingFind(WPARAM pSettingDescriptor, LPARAM lParam);
	INT_PTR __cdecl  DBSettingDelete(WPARAM pSettingDescriptor, LPARAM lParam);
	INT_PTR __cdecl  DBSettingDeleteHandle(WPARAM hSetting, LPARAM lParam);
	INT_PTR __cdecl  DBSettingWrite(WPARAM pSetting, LPARAM lParam);
	INT_PTR __cdecl  DBSettingWriteHandle(WPARAM pSetting, LPARAM hSetting);
	INT_PTR __cdecl  DBSettingRead(WPARAM pSetting, LPARAM lParam);
	INT_PTR __cdecl  DBSettingReadHandle(WPARAM pSetting, LPARAM hSetting);
	INT_PTR __cdecl  DBSettingIterInit(WPARAM pFilter, LPARAM lParam);
	INT_PTR __cdecl  DBSettingIterNext(WPARAM hIteration, LPARAM lParam);
	INT_PTR __cdecl  DBSettingIterClose(WPARAM hIteration, LPARAM lParam);

	INT_PTR __cdecl  DBEventGetBlobSize(WPARAM hEvent, LPARAM lParam);
	INT_PTR __cdecl  DBEventGet(WPARAM hEvent, LPARAM pEvent);
	INT_PTR __cdecl  DBEventGetCount(WPARAM hEntity, LPARAM lParam);
	INT_PTR __cdecl  DBEventDelete(WPARAM hEvent, LPARAM lParam);
	INT_PTR __cdecl  DBEventAdd(WPARAM hEntity, LPARAM pEvent);
	INT_PTR __cdecl  DBEventMarkRead(WPARAM hEvent, LPARAM lParam);
	INT_PTR __cdecl  DBEventWriteToDisk(WPARAM hEvent, LPARAM lParam);
	INT_PTR __cdecl  DBEventGetEntity(WPARAM hEvent, LPARAM lParam);
	INT_PTR __cdecl  DBEventIterInit(WPARAM pFilter, LPARAM lParam);
	INT_PTR __cdecl  DBEventIterNext(WPARAM hIteration, LPARAM lParam);
	INT_PTR __cdecl  DBEventIterClose(WPARAM hIteration, LPARAM lParam);

	bool RegisterServices();

	typedef INT_PTR ( __cdecl CDataBase::*DbServiceFunc )( WPARAM, LPARAM );
	void CreateDbService(const char *szServiceName, DbServiceFunc pFunc);

protected:  // to be compatible with the standard Miranda databases
	STDMETHODIMP_(void)   SetCacheSafetyMode(BOOL);

	STDMETHODIMP_(LONG)   GetContactCount(void);
	STDMETHODIMP_(MCONTACT) FindFirstContact(const char* szProto = NULL);
	STDMETHODIMP_(MCONTACT) FindNextContact(MCONTACT contactID, const char* szProto = NULL);
	STDMETHODIMP_(LONG)   DeleteContact(MCONTACT contactID);
	STDMETHODIMP_(HANDLE) AddContact(void);
	STDMETHODIMP_(BOOL)   IsDbContact(MCONTACT contactID);

	STDMETHODIMP_(LONG)   GetEventCount(MCONTACT contactID);
	STDMETHODIMP_(HANDLE) AddEvent(MCONTACT contactID, DBEVENTINFO *dbe);
	STDMETHODIMP_(BOOL)   DeleteEvent(MCONTACT contactID, HANDLE hDbEvent);
	STDMETHODIMP_(LONG)   GetBlobSize(HANDLE hDbEvent);
	STDMETHODIMP_(BOOL)   GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbe);
	STDMETHODIMP_(BOOL)   MarkEventRead(MCONTACT contactID, HANDLE hDbEvent);
	STDMETHODIMP_(HANDLE) GetEventContact(HANDLE hDbEvent);
	STDMETHODIMP_(HANDLE) FindFirstEvent(MCONTACT contactID);
	STDMETHODIMP_(HANDLE) FindFirstUnreadEvent(MCONTACT contactID);
	STDMETHODIMP_(HANDLE) FindLastEvent(MCONTACT contactID);
	STDMETHODIMP_(HANDLE) FindNextEvent(HANDLE hDbEvent);
	STDMETHODIMP_(HANDLE) FindPrevEvent(HANDLE hDbEvent);

	STDMETHODIMP_(BOOL)   EnumModuleNames(DBMODULEENUMPROC pFunc, void *pParam);

	STDMETHODIMP_(BOOL)   GetContactSetting(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv);
	STDMETHODIMP_(BOOL)   GetContactSettingStr(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv);
	STDMETHODIMP_(BOOL)   GetContactSettingStatic(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv);
	STDMETHODIMP_(BOOL)   FreeVariant(DBVARIANT *dbv);
	STDMETHODIMP_(BOOL)   WriteContactSetting(MCONTACT contactID, DBCONTACTWRITESETTING *dbcws);
	STDMETHODIMP_(BOOL)   DeleteContactSetting(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting);
	STDMETHODIMP_(BOOL)   EnumContactSettings(MCONTACT contactID, DBCONTACTENUMSETTINGS* dbces);
	STDMETHODIMP_(BOOL)   SetSettingResident(BOOL bIsResident, const char *pszSettingName);
	STDMETHODIMP_(BOOL)   EnumResidentSettings(DBMODULEENUMPROC pFunc, void *pParam);
	STDMETHODIMP_(BOOL)   IsSettingEncrypted(LPCSTR szModule, LPCSTR szSetting);

	int CheckProto(DBCachedContact *cc, const char *proto);
};