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