0
|
1 // Filename : main.cpp
|
11
|
2 // Last Change: 2018-11-09 金 11:54:20.
|
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
|
0
|
47 wxImage::AddHandler( new wxJPEGHandler );
|
|
48 wxImage::AddHandler( new wxPNGHandler );
|
9
|
49 wxFileSystem::AddHandler( new wxZipFSHandler );
|
0
|
50
|
7
|
51 // Main Window
|
|
52 MainFrame *mainframe = new MainFrame( NULL, ID_MAIN, wxEmptyString, wxPoint( rect.x, rect.y ), rect.GetSize(), wxDEFAULT_FRAME_STYLE );
|
|
53 mainframe->SetServer( m_serveraddr, m_serverport );
|
10
|
54 mainframe->InDevelop( true );
|
9
|
55 if ( !mainframe->GetDB() ) {
|
|
56 mainframe->Destroy();
|
|
57 return true;
|
|
58 }
|
6
|
59
|
5
|
60 // Splash Screen
|
9
|
61 SplashScreen( splash );
|
3
|
62
|
10
|
63 // Mode
|
|
64 if ( !unlock_key.IsEmpty() ) {
|
|
65 wxIPV4address addr;
|
|
66 addr.Hostname( wxGetFullHostName() );
|
|
67
|
|
68 wxString key = wxT( "re:searcher" ) + addr.IPAddress();
|
|
69 wxArrayString args;
|
|
70 args.Add( wxT( "crypto.exe" ) );
|
|
71 args.Add( wxT( "-a" ) );
|
|
72 args.Add( key );
|
|
73
|
|
74 wxArrayString output, errors;
|
|
75 wxExecute( wxJoin( args, ' ', '\\' ), output, errors );
|
|
76
|
|
77 if ( unlock_key.IsSameAs( output[0] ) ) {
|
|
78 mainframe->SetUser( wxT( "root" ) );
|
|
79 mainframe->SetTitle( wxT( "Re:Searcher - root" ) );
|
|
80 mainframe->LoadDB();
|
|
81 mainframe->Show( true );
|
|
82 return true;
|
|
83 }
|
|
84 }
|
|
85
|
|
86 // User Dialog
|
7
|
87 AuthDialog *authdlg = new AuthDialog( NULL, wxID_ANY, wxT( "Who are you ?" ), wxDefaultPosition, wxDefaultSize, wxCAPTION );
|
5
|
88 if ( !authdlg->LoadDB() ) {
|
|
89 authdlg->Destroy();
|
|
90 return false;
|
|
91 }
|
1
|
92
|
3
|
93 if ( authdlg->ShowModal() == wxID_OK ) {
|
5
|
94 wxString ui = wxString::Format( wxT( "./image/" ) + authdlg->GetUser() + wxT( ".jpg" ) );
|
6
|
95 if ( wxFileExists( ui ) ) {
|
|
96 wxCopyFile( ui, wxT( "./image/hello.jpg" ), true );
|
|
97 } else {
|
|
98 unsigned int seed = (unsigned int)time( 0 );
|
|
99 srand( seed );
|
|
100 int n = rand() % 13;
|
|
101 wxFileSystem* fs = new wxFileSystem();
|
9
|
102 wxString archive = wxT( "file:///./myapp.bin" );
|
6
|
103 wxFSFile* file = fs->OpenFile( archive + wxString::Format( wxT( "#zip:%02d.jpg" ), n ) );
|
|
104 if ( file ) {
|
|
105 wxInputStream* s = file->GetStream();
|
|
106 wxFileOutputStream of( wxT( "./image/hello.jpg" ) );
|
|
107 s->Read( of );
|
|
108 delete file;
|
|
109 }
|
|
110 delete fs;
|
|
111 }
|
5
|
112
|
7
|
113 mainframe->SetUser( authdlg->GetUser() );
|
|
114 mainframe->SetTitle( wxT( "Re:Searcher - " ) + authdlg->GetUser() );
|
|
115 mainframe->LoadDB();
|
3
|
116 mainframe->Show( true );
|
7
|
117 } else {
|
|
118 mainframe->Destroy();
|
3
|
119 }
|
|
120 authdlg->Destroy();
|
0
|
121
|
|
122 return true;
|
|
123 }
|
|
124
|
|
125 int MyApp::OnExit()
|
|
126 {
|
6
|
127 RemoveFile( wxT( "auth.db" ) );
|
|
128 RemoveFile( wxT( "hhs.db" ) );
|
|
129 RemoveFile( wxT( ".cache/*" ) );
|
0
|
130 SaveSetting();
|
|
131 return 0;
|
|
132 }
|
|
133
|
|
134 void MyApp::InitSetting()
|
|
135 {
|
2
|
136 conf_file = wxGetCwd() + wxFILE_SEP_PATH + wxT( "app.conf" );
|
|
137 config = new wxFileConfig( wxT( "MyApp" ), wxT( "T.Mutoh" ), conf_file, wxEmptyString, wxCONFIG_USE_LOCAL_FILE );
|
|
138
|
|
139 config->SetPath( wxT( "/Geometry" ) );
|
|
140 config->Read( wxT( "x" ), &rect.x );
|
|
141 config->Read( wxT( "y" ), &rect.y );
|
|
142 config->Read( wxT( "w" ), &rect.width );
|
|
143 config->Read( wxT( "h" ), &rect.height );
|
0
|
144
|
2
|
145 config->SetPath( wxT( "/Server" ) );
|
7
|
146 config->Read( wxT( "proxy_address" ), &m_serveraddr );
|
|
147 config->Read( wxT( "proxy_port" ), &m_serverport );
|
|
148 if ( m_serveraddr.IsSameAs( wxEmptyString, false ) ) {
|
|
149 config->Read( wxT( "address" ), &m_serveraddr );
|
|
150 config->Read( wxT( "port" ), &m_serverport );
|
|
151 }
|
2
|
152
|
7
|
153 if ( m_serveraddr.IsSameAs( wxEmptyString, false ) ) {
|
|
154 m_serveraddr = wxT( "192.168.21.151" ); // nginx
|
|
155 m_serverport = 80;
|
|
156 }
|
9
|
157
|
|
158 config->SetPath( wxT( "/Misc" ) );
|
|
159 config->Read( wxT( "splash" ), &splash );
|
10
|
160 config->Read( wxT( "unlock_key" ), &unlock_key );
|
9
|
161
|
|
162 delete config;
|
0
|
163 }
|
|
164
|
|
165 void MyApp::SaveSetting()
|
|
166 {
|
3
|
167 config = new wxFileConfig( wxT( "MyApp" ), wxT( "T.Mutoh" ), conf_file, wxEmptyString, wxCONFIG_USE_LOCAL_FILE );
|
0
|
168
|
3
|
169 config->SetPath( wxT( "/Geometry" ) );
|
|
170 config->Write( wxT( "x" ), rect.x );
|
|
171 config->Write( wxT( "y" ), rect.y );
|
|
172 config->Write( wxT( "w" ), rect.width );
|
|
173 config->Write( wxT( "h" ), rect.height );
|
0
|
174 delete config;
|
|
175 }
|
|
176
|
9
|
177 void MyApp::SplashScreen( int ms )
|
|
178 {
|
|
179 if ( ms < 0 ) return;
|
|
180
|
|
181 wxFileSystem* fs = new wxFileSystem();
|
|
182 wxString archive = wxT( "file:///./myapp.bin" );
|
|
183 for ( int i = 0; i < 11; i++ ) {
|
|
184 wxFSFile* file = fs->OpenFile( archive + wxString::Format( wxT( "#zip:startup%02d.png" ), i ) );
|
|
185 if ( file ) {
|
|
186 wxInputStream* s = file->GetStream();
|
|
187 wxImage image( *s, wxBITMAP_TYPE_PNG );
|
|
188 wxBitmap bmp = wxBitmap( image );
|
|
189 wxSplashScreen* splash = new wxSplashScreen( bmp, wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_TIMEOUT, 2000, NULL, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE|wxSTAY_ON_TOP );
|
|
190 delete file;
|
|
191 }
|
|
192 wxMilliSleep( ms );
|
|
193 }
|
|
194 delete fs;
|
|
195 }
|
|
196
|
11
|
197 wxString MyApp::GetVersion( void )
|
|
198 {
|
|
199 wxHTTP get;
|
|
200 get.SetTimeout( 30 );
|
|
201 get.SetFlags( wxSOCKET_WAITALL|wxSOCKET_BLOCK );
|
|
202 while ( !get.Connect( m_serveraddr, m_serverport ) )
|
|
203 wxSleep( 1 );
|
|
204
|
|
205 wxString version;
|
|
206 wxString url = wxT( "/release/version" );
|
|
207 wxInputStream *http_istream = get.GetInputStream( url );
|
|
208 if ( get.GetError() == wxPROTO_NOERR ) {
|
|
209 wxStringOutputStream out_stream( &version );
|
|
210 http_istream->Read( out_stream );
|
|
211 }
|
|
212
|
|
213 wxDELETE( http_istream );
|
|
214 get.Close();
|
|
215
|
|
216 return version;
|
|
217 }
|
|
218
|
6
|
219 void MyApp::RemoveFile( wxString pattern )
|
|
220 {
|
|
221 wxString file = wxFindFirstFile( pattern );
|
|
222 while ( !file.empty() ) {
|
|
223 wxRemoveFile( file );
|
|
224 file = wxFindNextFile();
|
|
225 }
|
|
226 }
|
|
227
|