0
|
1 // Filename : main.cpp
|
15
|
2 // Last Change: 2019-05-29 水 15:04:09.
|
0
|
3 //
|
9
|
4
|
10
|
5 #include <wx/socket.h>
|
11
|
6 #include <wx/sstream.h>
|
|
7 #include <wx/protocol/http.h>
|
4
|
8 #include "id.h"
|
0
|
9 #include "main.h"
|
|
10 #include "auth.h"
|
11
|
11 #include "net.h"
|
0
|
12 #include "rsearcher.h"
|
|
13
|
|
14 IMPLEMENT_APP( MyApp )
|
|
15
|
|
16 IMPLEMENT_CLASS( MyApp, wxApp )
|
|
17
|
|
18 MyApp::MyApp()
|
|
19 {
|
1
|
20 develop = true;
|
0
|
21 }
|
|
22 MyApp::~MyApp()
|
|
23 {
|
|
24 }
|
|
25
|
|
26 bool MyApp::OnInit()
|
|
27 {
|
|
28 if ( !wxApp::OnInit() ) return false;
|
|
29
|
11
|
30 InitSetting();
|
|
31
|
|
32 // Check New-Version
|
|
33 wxString newver = GetVersion();
|
|
34 if ( newver.Cmp( RSVER ) == 1 ) { // 1.9 -> 1.91 -> 1.92 ...
|
|
35 wxString sv = wxString::Format( wxT( "%s:%d" ), m_serveraddr, m_serverport );
|
|
36
|
|
37 wxArrayString args;
|
|
38 args.Add( wxT( "upgrade.bat" ) );
|
|
39 args.Add( sv );
|
|
40 args.Add( RSVER );
|
|
41 args.Add( newver );
|
|
42
|
|
43 wxExecute( wxJoin( args, ' ', '\\' ) );
|
|
44 return false;
|
|
45 }
|
|
46
|
15
|
47 // Validation Host
|
|
48 bool valid_host = false;
|
|
49 if ( !m_unlock_key.IsEmpty() ) {
|
10
|
50 wxIPV4address addr;
|
|
51 addr.Hostname( wxGetFullHostName() );
|
|
52
|
|
53 wxString key = wxT( "re:searcher" ) + addr.IPAddress();
|
|
54 wxArrayString args;
|
|
55 args.Add( wxT( "crypto.exe" ) );
|
|
56 args.Add( wxT( "-a" ) );
|
|
57 args.Add( key );
|
|
58
|
|
59 wxArrayString output, errors;
|
|
60 wxExecute( wxJoin( args, ' ', '\\' ), output, errors );
|
|
61
|
15
|
62 if ( m_unlock_key.IsSameAs( output[0] ) ) valid_host = true;
|
|
63 }
|
|
64
|
|
65 wxImage::AddHandler( new wxJPEGHandler );
|
|
66 wxImage::AddHandler( new wxPNGHandler );
|
|
67 wxFileSystem::AddHandler( new wxZipFSHandler );
|
|
68
|
|
69 // Main Window
|
|
70 MainFrame *mainframe = new MainFrame( NULL, ID_MAIN, wxEmptyString, wxPoint( rect.x, rect.y ), rect.GetSize(), wxDEFAULT_FRAME_STYLE );
|
|
71 mainframe->SetServer( m_serveraddr, m_serverport );
|
|
72 mainframe->SetDBdir( m_dbdir );
|
|
73 mainframe->InDevelop( true );
|
|
74
|
|
75 bool dlhhs = false;
|
|
76 if ( valid_host && !m_qdlhhs ) {
|
|
77 dlhhs = true;
|
|
78 } else {
|
|
79 wxMessageDialog dlg( NULL, wxT("download h-data ?"), wxT("Ask"), wxYES_NO );
|
|
80 if ( wxID_YES == dlg.ShowModal() ) dlhhs = true;
|
|
81 }
|
|
82
|
|
83 if ( !mainframe->GetDB( true, dlhhs, false ) ) {
|
|
84 mainframe->Destroy();
|
|
85 return true;
|
|
86 }
|
|
87
|
|
88 // Splash Screen
|
|
89 SplashScreen( m_splash );
|
|
90
|
|
91 if ( valid_host ) {
|
|
92 mainframe->SetUser( wxT( "root" ) );
|
|
93 mainframe->SetTitle( wxT( "Re:Searcher - root" ) );
|
|
94 mainframe->LoadDB( dlhhs );
|
|
95 mainframe->Show( true );
|
|
96 mainframe->Raise();
|
|
97 return true;
|
10
|
98 }
|
|
99
|
|
100 // User Dialog
|
7
|
101 AuthDialog *authdlg = new AuthDialog( NULL, wxID_ANY, wxT( "Who are you ?" ), wxDefaultPosition, wxDefaultSize, wxCAPTION );
|
5
|
102 if ( !authdlg->LoadDB() ) {
|
|
103 authdlg->Destroy();
|
|
104 return false;
|
|
105 }
|
1
|
106
|
3
|
107 if ( authdlg->ShowModal() == wxID_OK ) {
|
5
|
108 wxString ui = wxString::Format( wxT( "./image/" ) + authdlg->GetUser() + wxT( ".jpg" ) );
|
6
|
109 if ( wxFileExists( ui ) ) {
|
|
110 wxCopyFile( ui, wxT( "./image/hello.jpg" ), true );
|
|
111 } else {
|
|
112 unsigned int seed = (unsigned int)time( 0 );
|
|
113 srand( seed );
|
|
114 int n = rand() % 13;
|
|
115 wxFileSystem* fs = new wxFileSystem();
|
9
|
116 wxString archive = wxT( "file:///./myapp.bin" );
|
6
|
117 wxFSFile* file = fs->OpenFile( archive + wxString::Format( wxT( "#zip:%02d.jpg" ), n ) );
|
|
118 if ( file ) {
|
|
119 wxInputStream* s = file->GetStream();
|
|
120 wxFileOutputStream of( wxT( "./image/hello.jpg" ) );
|
|
121 s->Read( of );
|
|
122 delete file;
|
|
123 }
|
|
124 delete fs;
|
|
125 }
|
5
|
126
|
7
|
127 mainframe->SetUser( authdlg->GetUser() );
|
|
128 mainframe->SetTitle( wxT( "Re:Searcher - " ) + authdlg->GetUser() );
|
15
|
129 mainframe->LoadDB( dlhhs );
|
3
|
130 mainframe->Show( true );
|
7
|
131 } else {
|
|
132 mainframe->Destroy();
|
3
|
133 }
|
|
134 authdlg->Destroy();
|
0
|
135
|
|
136 return true;
|
|
137 }
|
|
138
|
|
139 int MyApp::OnExit()
|
|
140 {
|
13
|
141 RemoveFile( wxT( "hhs.csv.tmp" ) );
|
|
142 RemoveFile( wxT( "hhs.csv" ) );
|
|
143 RemoveFile( wxT( "index.db.tmp" ) );
|
6
|
144 RemoveFile( wxT( "auth.db" ) );
|
|
145 RemoveFile( wxT( "hhs.db" ) );
|
|
146 RemoveFile( wxT( ".cache/*" ) );
|
0
|
147 SaveSetting();
|
|
148 return 0;
|
|
149 }
|
|
150
|
|
151 void MyApp::InitSetting()
|
|
152 {
|
2
|
153 conf_file = wxGetCwd() + wxFILE_SEP_PATH + wxT( "app.conf" );
|
|
154 config = new wxFileConfig( wxT( "MyApp" ), wxT( "T.Mutoh" ), conf_file, wxEmptyString, wxCONFIG_USE_LOCAL_FILE );
|
|
155
|
|
156 config->SetPath( wxT( "/Geometry" ) );
|
|
157 config->Read( wxT( "x" ), &rect.x );
|
|
158 config->Read( wxT( "y" ), &rect.y );
|
|
159 config->Read( wxT( "w" ), &rect.width );
|
|
160 config->Read( wxT( "h" ), &rect.height );
|
0
|
161
|
13
|
162 config->SetPath( wxT( "/DBManage" ) );
|
|
163 config->Read( wxT( "dbdir" ), &m_dbdir );
|
15
|
164 config->Read( wxT( "qdlhhs" ), &m_qdlhhs );
|
13
|
165
|
2
|
166 config->SetPath( wxT( "/Server" ) );
|
7
|
167 config->Read( wxT( "proxy_address" ), &m_serveraddr );
|
|
168 config->Read( wxT( "proxy_port" ), &m_serverport );
|
|
169 if ( m_serveraddr.IsSameAs( wxEmptyString, false ) ) {
|
|
170 config->Read( wxT( "address" ), &m_serveraddr );
|
|
171 config->Read( wxT( "port" ), &m_serverport );
|
|
172 }
|
2
|
173
|
7
|
174 if ( m_serveraddr.IsSameAs( wxEmptyString, false ) ) {
|
|
175 m_serveraddr = wxT( "192.168.21.151" ); // nginx
|
|
176 m_serverport = 80;
|
|
177 }
|
9
|
178
|
15
|
179 config->SetPath( wxT( "/Print" ) );
|
|
180 config->Read( wxT( "zoom" ), &pzoom );
|
|
181
|
9
|
182 config->SetPath( wxT( "/Misc" ) );
|
15
|
183 config->Read( wxT( "splash" ), &m_splash );
|
|
184 config->Read( wxT( "unlock_key" ), &m_unlock_key );
|
9
|
185
|
|
186 delete config;
|
0
|
187 }
|
|
188
|
|
189 void MyApp::SaveSetting()
|
|
190 {
|
3
|
191 config = new wxFileConfig( wxT( "MyApp" ), wxT( "T.Mutoh" ), conf_file, wxEmptyString, wxCONFIG_USE_LOCAL_FILE );
|
0
|
192
|
3
|
193 config->SetPath( wxT( "/Geometry" ) );
|
|
194 config->Write( wxT( "x" ), rect.x );
|
|
195 config->Write( wxT( "y" ), rect.y );
|
|
196 config->Write( wxT( "w" ), rect.width );
|
|
197 config->Write( wxT( "h" ), rect.height );
|
15
|
198
|
|
199 config->SetPath( wxT( "/Print" ) );
|
|
200 config->Write( wxT( "zoom" ), pzoom );
|
|
201
|
0
|
202 delete config;
|
|
203 }
|
|
204
|
9
|
205 void MyApp::SplashScreen( int ms )
|
|
206 {
|
|
207 if ( ms < 0 ) return;
|
|
208
|
|
209 wxFileSystem* fs = new wxFileSystem();
|
|
210 wxString archive = wxT( "file:///./myapp.bin" );
|
|
211 for ( int i = 0; i < 11; i++ ) {
|
|
212 wxFSFile* file = fs->OpenFile( archive + wxString::Format( wxT( "#zip:startup%02d.png" ), i ) );
|
|
213 if ( file ) {
|
|
214 wxInputStream* s = file->GetStream();
|
|
215 wxImage image( *s, wxBITMAP_TYPE_PNG );
|
|
216 wxBitmap bmp = wxBitmap( image );
|
15
|
217 wxSplashScreen* splashwin = new wxSplashScreen( bmp, wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_TIMEOUT, 2000, NULL, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE|wxSTAY_ON_TOP );
|
9
|
218 delete file;
|
|
219 }
|
|
220 wxMilliSleep( ms );
|
|
221 }
|
|
222 delete fs;
|
|
223 }
|
|
224
|
11
|
225 wxString MyApp::GetVersion( void )
|
|
226 {
|
|
227 wxHTTP get;
|
|
228 get.SetTimeout( 30 );
|
|
229 get.SetFlags( wxSOCKET_WAITALL|wxSOCKET_BLOCK );
|
|
230 while ( !get.Connect( m_serveraddr, m_serverport ) )
|
|
231 wxSleep( 1 );
|
|
232
|
|
233 wxString version;
|
|
234 wxString url = wxT( "/release/version" );
|
|
235 wxInputStream *http_istream = get.GetInputStream( url );
|
|
236 if ( get.GetError() == wxPROTO_NOERR ) {
|
|
237 wxStringOutputStream out_stream( &version );
|
|
238 http_istream->Read( out_stream );
|
|
239 }
|
|
240
|
|
241 wxDELETE( http_istream );
|
|
242 get.Close();
|
|
243
|
|
244 return version;
|
|
245 }
|
|
246
|
6
|
247 void MyApp::RemoveFile( wxString pattern )
|
|
248 {
|
|
249 wxString file = wxFindFirstFile( pattern );
|
|
250 while ( !file.empty() ) {
|
|
251 wxRemoveFile( file );
|
|
252 file = wxFindNextFile();
|
|
253 }
|
|
254 }
|
|
255
|