0
|
1 // Filename : main.cpp
|
9
|
2 // Last Change: 2018-10-31 水 15:18:12.
|
0
|
3 //
|
9
|
4
|
4
|
5 #include "id.h"
|
0
|
6 #include "main.h"
|
|
7 #include "auth.h"
|
|
8 #include "rsearcher.h"
|
|
9
|
|
10 IMPLEMENT_APP( MyApp )
|
|
11
|
|
12 IMPLEMENT_CLASS( MyApp, wxApp )
|
|
13
|
|
14 MyApp::MyApp()
|
|
15 {
|
1
|
16 develop = true;
|
0
|
17 }
|
|
18 MyApp::~MyApp()
|
|
19 {
|
|
20 }
|
|
21
|
|
22 bool MyApp::OnInit()
|
|
23 {
|
|
24 if ( !wxApp::OnInit() ) return false;
|
|
25
|
|
26 wxImage::AddHandler( new wxJPEGHandler );
|
|
27 wxImage::AddHandler( new wxPNGHandler );
|
9
|
28 wxFileSystem::AddHandler( new wxZipFSHandler );
|
0
|
29 InitSetting();
|
|
30
|
7
|
31 // Main Window
|
|
32 MainFrame *mainframe = new MainFrame( NULL, ID_MAIN, wxEmptyString, wxPoint( rect.x, rect.y ), rect.GetSize(), wxDEFAULT_FRAME_STYLE );
|
|
33 mainframe->SetServer( m_serveraddr, m_serverport );
|
9
|
34 if ( !mainframe->GetDB() ) {
|
|
35 mainframe->Destroy();
|
|
36 return true;
|
|
37 }
|
6
|
38
|
5
|
39 // Splash Screen
|
9
|
40 SplashScreen( splash );
|
3
|
41
|
7
|
42 // User Dialgo
|
|
43 AuthDialog *authdlg = new AuthDialog( NULL, wxID_ANY, wxT( "Who are you ?" ), wxDefaultPosition, wxDefaultSize, wxCAPTION );
|
5
|
44 if ( !authdlg->LoadDB() ) {
|
|
45 authdlg->Destroy();
|
|
46 return false;
|
|
47 }
|
1
|
48
|
3
|
49 if ( authdlg->ShowModal() == wxID_OK ) {
|
5
|
50 wxString ui = wxString::Format( wxT( "./image/" ) + authdlg->GetUser() + wxT( ".jpg" ) );
|
6
|
51 if ( wxFileExists( ui ) ) {
|
|
52 wxCopyFile( ui, wxT( "./image/hello.jpg" ), true );
|
|
53 } else {
|
|
54 unsigned int seed = (unsigned int)time( 0 );
|
|
55 srand( seed );
|
|
56 int n = rand() % 13;
|
|
57 wxFileSystem* fs = new wxFileSystem();
|
9
|
58 wxString archive = wxT( "file:///./myapp.bin" );
|
6
|
59 wxFSFile* file = fs->OpenFile( archive + wxString::Format( wxT( "#zip:%02d.jpg" ), n ) );
|
|
60 if ( file ) {
|
|
61 wxInputStream* s = file->GetStream();
|
|
62 wxFileOutputStream of( wxT( "./image/hello.jpg" ) );
|
|
63 s->Read( of );
|
|
64 delete file;
|
|
65 }
|
|
66 delete fs;
|
|
67 }
|
5
|
68
|
7
|
69 mainframe->SetUser( authdlg->GetUser() );
|
|
70 mainframe->SetTitle( wxT( "Re:Searcher - " ) + authdlg->GetUser() );
|
|
71 mainframe->LoadDB();
|
5
|
72 mainframe->InDevelop( true );
|
3
|
73 mainframe->Show( true );
|
7
|
74 } else {
|
|
75 mainframe->Destroy();
|
3
|
76 }
|
|
77 authdlg->Destroy();
|
0
|
78
|
|
79 return true;
|
|
80 }
|
|
81
|
|
82 int MyApp::OnExit()
|
|
83 {
|
6
|
84 RemoveFile( wxT( "auth.db" ) );
|
|
85 RemoveFile( wxT( "hhs.db" ) );
|
|
86 RemoveFile( wxT( ".cache/*" ) );
|
0
|
87 SaveSetting();
|
|
88 return 0;
|
|
89 }
|
|
90
|
|
91 void MyApp::InitSetting()
|
|
92 {
|
2
|
93 conf_file = wxGetCwd() + wxFILE_SEP_PATH + wxT( "app.conf" );
|
|
94 config = new wxFileConfig( wxT( "MyApp" ), wxT( "T.Mutoh" ), conf_file, wxEmptyString, wxCONFIG_USE_LOCAL_FILE );
|
|
95
|
|
96 config->SetPath( wxT( "/Geometry" ) );
|
|
97 config->Read( wxT( "x" ), &rect.x );
|
|
98 config->Read( wxT( "y" ), &rect.y );
|
|
99 config->Read( wxT( "w" ), &rect.width );
|
|
100 config->Read( wxT( "h" ), &rect.height );
|
0
|
101
|
2
|
102 config->SetPath( wxT( "/Server" ) );
|
7
|
103 config->Read( wxT( "proxy_address" ), &m_serveraddr );
|
|
104 config->Read( wxT( "proxy_port" ), &m_serverport );
|
|
105 if ( m_serveraddr.IsSameAs( wxEmptyString, false ) ) {
|
|
106 config->Read( wxT( "address" ), &m_serveraddr );
|
|
107 config->Read( wxT( "port" ), &m_serverport );
|
|
108 }
|
2
|
109
|
7
|
110 if ( m_serveraddr.IsSameAs( wxEmptyString, false ) ) {
|
|
111 m_serveraddr = wxT( "192.168.21.151" ); // nginx
|
|
112 m_serverport = 80;
|
|
113 }
|
9
|
114
|
|
115 config->SetPath( wxT( "/Misc" ) );
|
|
116 config->Read( wxT( "splash" ), &splash );
|
|
117
|
|
118 delete config;
|
0
|
119 }
|
|
120
|
|
121 void MyApp::SaveSetting()
|
|
122 {
|
3
|
123 config = new wxFileConfig( wxT( "MyApp" ), wxT( "T.Mutoh" ), conf_file, wxEmptyString, wxCONFIG_USE_LOCAL_FILE );
|
0
|
124
|
3
|
125 config->SetPath( wxT( "/Geometry" ) );
|
|
126 config->Write( wxT( "x" ), rect.x );
|
|
127 config->Write( wxT( "y" ), rect.y );
|
|
128 config->Write( wxT( "w" ), rect.width );
|
|
129 config->Write( wxT( "h" ), rect.height );
|
0
|
130 delete config;
|
|
131 }
|
|
132
|
9
|
133 void MyApp::SplashScreen( int ms )
|
|
134 {
|
|
135 if ( ms < 0 ) return;
|
|
136
|
|
137 wxFileSystem* fs = new wxFileSystem();
|
|
138 wxString archive = wxT( "file:///./myapp.bin" );
|
|
139 for ( int i = 0; i < 11; i++ ) {
|
|
140 wxFSFile* file = fs->OpenFile( archive + wxString::Format( wxT( "#zip:startup%02d.png" ), i ) );
|
|
141 if ( file ) {
|
|
142 wxInputStream* s = file->GetStream();
|
|
143 wxImage image( *s, wxBITMAP_TYPE_PNG );
|
|
144 wxBitmap bmp = wxBitmap( image );
|
|
145 wxSplashScreen* splash = new wxSplashScreen( bmp, wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_TIMEOUT, 2000, NULL, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE|wxSTAY_ON_TOP );
|
|
146 delete file;
|
|
147 }
|
|
148 wxMilliSleep( ms );
|
|
149 }
|
|
150 delete fs;
|
|
151 }
|
|
152
|
6
|
153 void MyApp::RemoveFile( wxString pattern )
|
|
154 {
|
|
155 wxString file = wxFindFirstFile( pattern );
|
|
156 while ( !file.empty() ) {
|
|
157 wxRemoveFile( file );
|
|
158 file = wxFindNextFile();
|
|
159 }
|
|
160 }
|
|
161
|