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