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

MyRuLibApp.cpp « MyRuLib « sources - github.com/lintest/myrulib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d63b56a4cccd01b0fbbd3ade9e74376d15c6ba28 (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
#include "MyRuLibApp.h"
#include <wx/app.h>
#include <wx/fs_inet.h>
#include <wx/fs_mem.h>
#include <wx/curl/http.h>
#include "FbDataPath.h"
#include "FbMainFrame.h"
#include "FbLogStream.h"
#include "FbLocale.h"
#include "FbParams.h"
#include "FbGenres.h"
#include "ZipReader.h"
#include "FbDataOpenDlg.h"
#include "FbCollection.h"

IMPLEMENT_APP(MyRuLibApp)

BEGIN_EVENT_TABLE(MyRuLibApp, wxApp)
	EVT_FB_IMAGE(wxID_ANY, MyRuLibApp::OnImageEvent)
END_EVENT_TABLE()

wxCriticalSection MyRuLibApp::sm_section;

MyRuLibApp::MyRuLibApp()
    :m_locale(NULL), m_collection(NULL), m_downloader(NULL)
{
}

void MyRuLibApp::StartDownload()
{
	if (m_downloader) {
		m_downloader->Signal();
	} else {
		m_downloader = new FbDownloader;
		m_downloader->Execute();
		m_downloader->Signal();
	}
}

void MyRuLibApp::StopDownload()
{
	if (m_downloader) {
		m_downloader->Close();
		m_downloader->Signal();
		m_downloader->Delete();
		m_downloader = NULL;
	}
}

void MyRuLibApp::Localize()
{
	wxLanguage language = (wxLanguage) FbParams::GetInt(FB_LANG_LOCALE);
	if (m_locale && m_locale->GetLanguage() == language) return;

	wxDELETE(m_locale);
    m_locale = new FbLocale;
    m_locale->Init(language);

    FbMainFrame * frame = wxDynamicCast(wxGetApp().GetTopWindow(), FbMainFrame);
    if (frame) frame->Localize(language);

	FbGenres::Init();
}

bool MyRuLibApp::OnInit()
{
	FbCollection::LoadConfig();
    Localize();

	OpenLog();
	#ifdef __WXDEBUG__
	wxLog::SetVerbose(true);
	#endif // __WXDEBUG__

	wxFileName filename = GetDatabaseFile();
	if (!filename.IsOk()) {
		wxString datafile;
		bool ok = FbDataOpenDlg::Execute(NULL, datafile);
		if (!ok) return false;
		filename = datafile;
	}

	filename.Normalize();
	OpenDatabase(filename.GetFullPath());

	::wxInitAllImageHandlers();
	wxFileSystem::AddHandler(new wxMemoryFSHandler);
	wxFileSystem::AddHandler(new wxInternetFSHandler);
	wxCurlHTTP::Init();

	LoadBlankImage();

	FbMainFrame * frame = new FbMainFrame;
	SetTopWindow(frame);
	frame->Show();

	return true;
}

void MyRuLibApp::LoadBlankImage()
{
	/* XPM */
	static const char * blank_xpm[] = {
	/* columns rows colors chars-per-pixel */
	"1 1 1 1",
	"- c None",
	/* pixels */
	"-",
	};

	wxBitmap bitmap(blank_xpm);
	wxMemoryFSHandler::AddFile(wxT("blank"), bitmap, wxBITMAP_TYPE_PNG);
}

wxFileName MyRuLibApp::GetDatabaseFile()
{
	wxFileName filename = FbStandardPaths().GetExecutablePath();
	filename.SetExt(wxT("db"));

	if (wxGetApp().argc > 1) {
		wxString arg = wxGetApp().argv[1];
		if (wxFileName::DirExists(arg)) {
			filename.SetPath(arg);
			return filename;
		}
		return wxFileName(arg);
	}

	if (filename.FileExists()) return filename;

	wxString recent = FbCollection::GetParamStr(FB_RECENT_0);
	if (!recent.IsEmpty()) {
		wxFileName filename = recent;
		if (filename.FileExists()) return filename;
	}

	return wxFileName();
}

int MyRuLibApp::OnExit()
{
	StopDownload();
    wxDELETE(m_locale);
	wxDELETE(m_collection);
	return wxApp::OnExit();
}

void MyRuLibApp::OpenLog()
{
	wxFileName logname = FbDatabase::GetConfigName();
	logname.SetExt(wxT("log"));
	wxLog * logger = new FbLogStream(logname.GetFullPath());
	wxLog::SetActiveTarget(logger);
}

bool MyRuLibApp::OpenDatabase(const wxString &filename)
{
	FbCollection * collection = new FbCollection(filename);
	bool ok = collection->IsOk();
	if (ok) {
		SetLibFile(filename);
		wxCriticalSectionLocker locker(sm_section);
		wxDELETE(m_collection);
		m_collection = collection;
	} else {
		delete collection;
	}
	if (ok) {
		FbParams::AddRecent(filename, FbParams::GetStr(DB_LIBRARY_TITLE));
		UpdateLibPath();
	}
	return ok;
}

FbCollection * MyRuLibApp::GetCollection()
{
	wxCriticalSectionLocker locker(sm_section);
	return m_collection;
}

const wxString MyRuLibApp::GetLibFile() const
{
	wxCriticalSectionLocker locker(sm_section);
	return m_LibFile;
}

const wxString MyRuLibApp::GetLibPath() const
{
	wxCriticalSectionLocker locker(sm_section);
	return m_LibPath;
}

void MyRuLibApp::SetLibFile(const wxString & filename)
{
	{
		wxCriticalSectionLocker locker(sm_section);
		m_LibFile = filename;
	}
}

void MyRuLibApp::UpdateLibPath()
{
	wxFileName dirname = GetLibFile();
	dirname.SetPath(FbParams::GetStr(DB_LIBRARY_DIR));
	if (dirname.IsRelative()) {
		wxFileName filename = GetLibFile();
		dirname.MakeAbsolute(filename.GetPath());
	}
	wxCriticalSectionLocker locker(sm_section);
	m_LibPath = dirname.GetPath();
}

void MyRuLibApp::OnImageEvent(FbImageEvent & event)
{
	FbViewData::Push(event.GetString(), event.GetImage());
}