Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FbCollection.cpp « MyRuLib « sources - github.com/lintest/myrulib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6f1e5c89ddb454a5675a703f35e804b37c835833 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include "FbCollection.h"
#include "FbColumns.h"
#include "MyRuLibApp.h"
#include "FbConst.h"
#include "FbParams.h"
#include "FbGenres.h"
#include "FbDateTime.h"

#include <wx/filename.h>
#include <wx/fs_mem.h>

#include "res/ico_pdf.xpm"
#include "res/ico_djvu.xpm"

#ifdef __WXMSW__
#include <wx/mimetype.h>
#endif // __WXMSW__

//-----------------------------------------------------------------------------
//  FbBookAuths
//-----------------------------------------------------------------------------

IMPLEMENT_CLASS(FbBookAuths, wxObject)

FbBookAuths::FbBookAuths(int code, FbDatabase & database)
	: m_code(code)
{
	m_name = database.Str(code, wxT("SELECT AGGREGATE(DISTINCT full_name) FROM authors WHERE id IN(SELECT id_author FROM books WHERE id=?)"));
}

wxString FbBookAuths::operator[](size_t col) const
{
	switch (col) {
		case BF_AUTH: return m_name;
		default: return wxEmptyString;
	}
}

#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(FbBookAuthsArray);

//-----------------------------------------------------------------------------
//  FbBookSeqns
//-----------------------------------------------------------------------------

IMPLEMENT_CLASS(FbBookSeqns, wxObject)

FbBookSeqns::FbBookSeqns(int code, wxSQLite3Database &database)
	: m_code(code)
{
	wxString sql = wxT("SELECT AGGREGATE(DISTINCT value), MAX(bookseq.number) FROM bookseq LEFT JOIN sequences ON id_seq=id WHERE id_book=?");
	wxSQLite3Statement stmt = database.PrepareStatement(sql);
	stmt.Bind(1, code);
	wxSQLite3ResultSet result = stmt.ExecuteQuery();
	if (result.NextRow()) {
		m_name = result.GetString(0);
		m_numb = result.GetInt(1);
	}
}

wxString FbBookSeqns::operator[](size_t col) const
{
	switch (col) {
		case BF_SEQN: return m_name;
		case BF_NUMB: return m_numb ? wxString::Format(wxT("%d"), m_numb) : wxString();
		default: return wxEmptyString;
	}
}

#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(FbBookSeqnsArray);

//-----------------------------------------------------------------------------
//  FbParamData
//-----------------------------------------------------------------------------

IMPLEMENT_CLASS(FbParamData, wxObject)

FbParamData & FbParamData::operator=(wxSQLite3ResultSet & result)
{
	m_int = result.GetInt(1);
	m_str = result.GetString(2);
	return *this;
}

//-----------------------------------------------------------------------------
//  FbCollection
//-----------------------------------------------------------------------------

IMPLEMENT_CLASS(FbCollection, wxObject)

wxCriticalSection FbCollection::sm_section;

FbParamHash FbCollection::sm_params;

FbCollection::FbCollection(const wxString &filename)
	: m_thread(NULL)
{
	m_database.Open(filename);
	m_database.AttachConfig();
	m_database.CreateFunction(wxT("AGGREGATE"), 1, m_aggregate);
	m_database.CreateFullText();
	LoadParams();
}

FbCollection::~FbCollection()
{
	if (m_thread) m_thread->Close();
}

bool FbCollection::IsOk() const
{
	return m_database.IsOpen();
}

wxString FbCollection::Format(int number)
{
	int hi = number / 1000;
	int lo = number % 1000;
	if (hi)
		return Format(hi) << wxChar(0xA0) << wxString::Format(wxT("%03d"), lo);
	else
		return wxString::Format(wxT("%d"), lo);
}

FbCollection * FbCollection::GetCollection()
{
	return wxGetApp().GetCollection();
}

wxString FbCollection::GetSeqn(int code, size_t col)
{
	if (code == 0 && col == 0) return _("(Misc.)");

	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection == NULL) return wxEmptyString;

	if (collection->m_seqns.count(code)) {
		return collection->m_seqns[code];
	} else {
		wxString sql = wxT("SELECT value FROM sequences WHERE id="); sql << code;
		wxSQLite3ResultSet result = collection->m_database.ExecuteQuery(sql);
		wxString name = result.NextRow() ? result.GetString(0) : wxString();
		collection->m_seqns[code] = name;
		return name;
	}
}

wxString FbCollection::GetAuth(int code, size_t col)
{
	if (code == 0 && col == 0) return _("(no Author)");

	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection == NULL) return wxEmptyString;

	if (collection->m_auths.count(code)) {
		return collection->m_auths[code];
	} else {
		wxString sql = wxT("SELECT full_name, number FROM authors WHERE id="); sql << code;
		wxSQLite3ResultSet result = collection->m_database.ExecuteQuery(sql);
		wxString name = result.NextRow() ? result.GetString(0) : wxString();
		collection->m_auths[code] = name;
		return name;
	}
}

void FbCollection::AddSeqn(int code, const wxString &name)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection) collection->m_seqns[code] = name;
}

void FbCollection::AddAuth(int code, const wxString &name)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection) collection->m_auths[code] = name;
}

void FbCollection::AddInfo(FbViewData * info)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection) collection->AddBookInfo(info);
}

FbCacheBook FbCollection::AddBook(const FbCacheBook & book)
{
	size_t count = m_books.Count();
	m_books.Insert(book, 0);
	if (count > DATA_CACHE_SIZE) m_books.RemoveAt(DATA_CACHE_SIZE, count - DATA_CACHE_SIZE);
	return book;
}

void FbCollection::AddBookInfo(FbViewData * info)
{
	size_t count = m_infos.Count();
	m_infos.Insert(info, 0);
	if (count > HTML_CACHE_SIZE) m_infos.RemoveAt(HTML_CACHE_SIZE, count - HTML_CACHE_SIZE);
}

void FbCollection::ResetSeqn(int code)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection) collection->m_seqns.erase(code);
}

void FbCollection::ResetAuth(int code)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection) collection->m_seqns.erase(code);
}

void FbCollection::ResetInfo(int code)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection) collection->DoResetInfo(code);
}

void FbCollection::ResetBook(int code)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection) collection->DoResetBook(code);
}

void FbCollection::ResetBook(const wxArrayInt &books)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection) collection->DoResetBook(books);
}

void FbCollection::DoResetInfo(int code)
{
	size_t count = m_infos.Count();
	for (size_t i = 0; i < count; i++) {
		if (m_infos[i].GetCode() == code) {
			m_infos.RemoveAt(i);
			break;
		}
	}
}

void FbCollection::DoResetBook(int code)
{
	size_t count = m_books.Count();
	for (size_t i = 0; i < count; i++) {
		if (m_books[i].GetCode() == code) {
			m_books.RemoveAt(i);
			break;
		}
	}
}

void FbCollection::DoResetBook(const wxArrayInt &books)
{
	size_t count = m_books.Count();
	for (size_t i = 0; i < count; i++) {
		size_t index = count - i - 1;
		int code = m_books[index].GetCode();
		if (books.Index(code) != wxNOT_FOUND) m_books.RemoveAt(index);
	}
}

wxString FbCollection::GetBook(int code, size_t col)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection == NULL) return wxEmptyString;
	switch (col) {
		case BF_AUTH:
			return collection->GetBookAuths(code, col);
		case BF_SEQN:
			return collection->GetBookSeqns(code, col);
		case BF_NUMB:
			return collection->GetBookSeqns(code, col);
		default:
			return collection->GetCacheBook(code).GetValue(col);

	}
}

void FbCollection::EmptyInfo()
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection) collection->m_infos.Empty();
}

FbCacheBook FbCollection::GetBookData(int code)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection == NULL) return 0;
	return collection->GetCacheBook(code);
}

wxString FbCollection::GetBookHTML(const FbViewContext &ctx, const FbCacheBook &book, int code)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();
	if (collection == NULL) return wxEmptyString;
	FbViewData * info = collection->GetCacheInfo(code);
	return info ? info->GetHTML(ctx, book) : wxString();
}

FbCacheBook FbCollection::GetCacheBook(int code)
{
	size_t count = m_books.Count();
	for (size_t i = 0; i < count; i++) {
		FbCacheBook & book = m_books[i];
		if (book.GetCode() == code) return book;
	}
	return AddBook(FbCacheBook::Get(code, m_database));
}

wxString FbCollection::GetBookAuths(int code, size_t col)
{
	size_t count = m_book_auth.Count();
	for (size_t i = 0; i < count; i++) {
		FbBookAuths & auth = m_book_auth[i];
		if (auth.GetCode() == code) return auth[col];
	}

	FbBookAuths * auth = new FbBookAuths(code, m_database);

	m_book_auth.Insert(auth, 0);
	if (count > DATA_CACHE_SIZE) m_book_auth.RemoveAt(DATA_CACHE_SIZE, count - DATA_CACHE_SIZE);
	return (*auth)[col];
}

wxString FbCollection::GetBookSeqns(int code, size_t col)
{
	size_t count = m_book_seqn.Count();
	for (size_t i = 0; i < count; i++) {
		FbBookSeqns & seqn = m_book_seqn[i];
		if (seqn.GetCode() == code) return seqn[col];
	}

	FbBookSeqns * seqn = new FbBookSeqns(code, m_database);

	m_book_seqn.Insert(seqn, 0);
	if (count > DATA_CACHE_SIZE) m_book_seqn.RemoveAt(DATA_CACHE_SIZE, count - DATA_CACHE_SIZE);
	return (*seqn)[col];
}

FbViewData * FbCollection::GetCacheInfo(int code)
{
	size_t count = m_infos.Count();
	for (size_t i = 0; i < count; i++) {
		FbViewData & info = m_infos[i];
		if (info.GetCode() == code) return &info;
	}
	return NULL;
}

wxArrayString FbCollection::sm_icons;

wxArrayString FbCollection::sm_noico;

void FbCollection::LoadIcon(const wxString &extension)
{
	wxCriticalSectionLocker locker(sm_section);

	if (!sm_icons.Count()) {
		AddIcon((wxString)wxT("djvu"), wxBitmap(ico_djvu_xpm));
		AddIcon((wxString)wxT("pdf"), wxBitmap(ico_pdf_xpm));
	}

	if (extension.IsEmpty() || extension == wxT("fb2")) return;
	if (sm_icons.Index(extension) != wxNOT_FOUND) return;
	if (sm_noico.Index(extension) != wxNOT_FOUND) return;

	#ifdef __WXMSW__
	wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(extension);
	if ( ft ) {
		wxIconLocation location;
		if ( ft->GetIcon(&location) && location.IsOk() ) {
			wxLogNull log;
			wxIcon icon(location);
			if (icon.IsOk()) {
				wxBitmap bitmap;
				bitmap.CopyFromIcon(icon);
				wxString filename = wxT("icon.") + extension;
				wxMemoryFSHandler::AddFile(filename, bitmap, wxBITMAP_TYPE_PNG);
				sm_icons.Add(extension);
				return;
			}
		}
	}
	#endif // __WXMSW__
	sm_noico.Add(extension);
}

wxString FbCollection::GetIcon(const wxString &extension)
{
	wxCriticalSectionLocker locker(sm_section);
	wxString filename = wxT("icon.") + extension;
	if (sm_icons.Index(extension) != wxNOT_FOUND)
		return filename;
	else
		return wxEmptyString;
}

void FbCollection::AddIcon(wxString extension, wxBitmap bitmap)
{
	wxString filename = wxT("icon.") + extension;
	wxMemoryFSHandler::AddFile(filename, bitmap, wxBITMAP_TYPE_PNG);
	sm_icons.Add(extension);
}

void FbCollection::LoadConfig()
{
	FbConfigDatabase database;
	database.Open();
	wxString sql = wxT("SELECT id, value, text FROM config WHERE id>=100");
	wxSQLite3ResultSet result = database.ExecuteQuery(sql);
	while (result.NextRow()) {
		int id = result.GetInt(0);
		sm_params[id] = result;
		if (id == FB_TEMP_DEL) FbTempEraser::sm_erase = result.GetInt(1);
	}
}

void FbCollection::LoadParams()
{
	wxString sql = wxT("SELECT id, value, text FROM params WHERE id<100");
	wxSQLite3ResultSet result = m_database.ExecuteQuery(sql);
	while (result.NextRow()) {
		int id = result.GetInt(0);
		m_params[id] = result;
		if (id == FB_TEMP_DEL) FbTempEraser::sm_erase = result.GetInt(1);
	}
}

int FbCollection::GetParamInt(int param)
{
	if (param >= 100) {
		wxCriticalSectionLocker locker(sm_section);
		FbParamHash::const_iterator it = sm_params.find(param);
		if (it != sm_params.end()) return it->second.m_int;
	} else {
		wxCriticalSectionLocker locker(sm_section);
		FbCollection * collection = GetCollection();
		if (collection) {
			FbParamHash::const_iterator it = collection->m_params.find(param);
			if (it != collection->m_params.end()) return it->second.m_int;
		}
	}
	return FbParamItem::DefaultInt(param);
}

wxString FbCollection::GetParamStr(int param)
{
	if (param >= 100) {
		wxCriticalSectionLocker locker(sm_section);
		FbParamHash::const_iterator it = sm_params.find(param);
		if (it != sm_params.end()) return it->second.m_str;
	} else {
		wxCriticalSectionLocker locker(sm_section);
		FbCollection * collection = GetCollection();
		if (collection) {
			FbParamHash::const_iterator it = collection->m_params.find(param);
			if (it != collection->m_params.end()) return it->second.m_str;
		}
	}
	return FbParamItem::DefaultStr(param);
}

void FbCollection::SetParamInt(int param, int value)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();

	if (param == FB_TEMP_DEL) FbTempEraser::sm_erase = value;

	if (param >= 100) {
		sm_params[param].m_int = value;
	} else {
		if (collection) collection->m_params[param].m_int = value;
	}

	if (collection == NULL) return;

	const wxChar * table = param < 100 ? wxT("params") : wxT("config");
	if (value == FbParamItem::DefaultInt(param)) {
		wxString sql = wxString::Format( wxT("DELETE FROM %s WHERE id=?"), table);
		wxSQLite3Statement stmt = collection->m_database.PrepareStatement(sql);
		stmt.Bind(1, param);
		stmt.ExecuteUpdate();
	} else {
		wxString sql = wxString::Format( wxT("INSERT OR REPLACE INTO %s (id, value) VALUES (?,?)"), table);
		wxSQLite3Statement stmt = collection->m_database.PrepareStatement(sql);
		stmt.Bind(1, param);
		stmt.Bind(2, value);
		stmt.ExecuteUpdate();
	}
}

void FbCollection::SetParamStr(int param, const wxString &value)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();

	if (param >= 100) {
		sm_params[param].m_str = value;
	} else {
		if (collection) collection->m_params[param].m_str = value;
	}

	if (collection == NULL) return;

	const wxChar * table = param < 100 ? wxT("params") : wxT("config");
	if (value == FbParamItem::DefaultStr(param)) {
		wxString sql = wxString::Format( wxT("DELETE FROM %s WHERE id=?"), table);
		wxSQLite3Statement stmt = collection->m_database.PrepareStatement(sql);
		stmt.Bind(1, param);
		stmt.ExecuteUpdate();
	} else {
		wxString sql = wxString::Format( wxT("INSERT OR REPLACE INTO %s (id, text) VALUES (?,?)"), table);
		wxSQLite3Statement stmt = collection->m_database.PrepareStatement(sql);
		stmt.Bind(1, param);
		stmt.Bind(2, value);
		stmt.ExecuteUpdate();
	}
}

void FbCollection::ResetParam(int param)
{
	wxCriticalSectionLocker locker(sm_section);
	FbCollection * collection = GetCollection();

	if (param >= 100) {
		sm_params.erase(param);
	} else {
		if (collection) collection->m_params.erase(param);
	}

	if (collection == NULL) return;

	const wxChar * table = param < 100 ? wxT("params") : wxT("config");
	wxString sql = wxString::Format( wxT("DELETE FROM %s WHERE id=?"), table);
	wxSQLite3Statement stmt = collection->m_database.PrepareStatement(sql);
	stmt.Bind(1, param);
	stmt.ExecuteUpdate();
}