view src/main.cpp @ 6:9a8b581c1993

improve performance.
author pyon@macmini
date Tue, 23 Oct 2018 19:15:22 +0900
parents e3b10fb860b3
children 29829e98d510
line wrap: on
line source

// Filename   : main.cpp
// Last Change: 2018-10-23 火 10:09:42.
//
#include "id.h"
#include "main.h"
#include "auth.h"
#include "rsearcher.h"

IMPLEMENT_APP( MyApp )

IMPLEMENT_CLASS( MyApp, wxApp )

MyApp::MyApp()
{
    develop = true;
}
MyApp::~MyApp()
{
}

bool MyApp::OnInit()
{
    if ( !wxApp::OnInit() ) return false;

    wxImage::AddHandler( new wxJPEGHandler );
    wxImage::AddHandler( new wxPNGHandler  );
	InitSetting();

	// Login Dialog
    AuthDialog *authdlg = new AuthDialog( NULL, wxID_ANY, wxT( "Who are you ?" ), wxDefaultPosition, wxDefaultSize, wxCAPTION );
	authdlg->SetServer( m_server );
	authdlg->GetDB();

	// Splash Screen
	wxFileSystem::AddHandler( new wxZipFSHandler );
	wxFileSystem* fs = new wxFileSystem();
	wxString archive = wxT( "file:///./myapp.bin" );
	for ( int i = 0; i < 11; i++ ) {
		wxFSFile* file = fs->OpenFile( archive + wxString::Format( wxT( "#zip:startup%02d.png" ), i ) );
		if ( file ) {
			wxInputStream* s = file->GetStream();
			wxImage image( *s, wxBITMAP_TYPE_PNG );
			wxBitmap bmp = wxBitmap( image );
			wxSplashScreen* splash = new wxSplashScreen( bmp, wxSPLASH_CENTRE_ON_SCREEN|wxSPLASH_TIMEOUT, 2500, NULL, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE|wxSTAY_ON_TOP );
			delete file;
		}
		wxMilliSleep( 200 );
	}
	delete fs;

	if ( !authdlg->LoadDB() ) {
		authdlg->Destroy();
		return false;
	}

    if ( authdlg->ShowModal() == wxID_OK ) {
		wxString ui = wxString::Format( wxT( "./image/" ) + authdlg->GetUser() + wxT( ".jpg" ) ); 
		if ( wxFileExists( ui ) ) {
			wxCopyFile( ui, wxT( "./image/hello.jpg" ), true );
		} else {
			unsigned int seed = (unsigned int)time( 0 );
			srand( seed );
			int n = rand() % 13;
			wxFileSystem* fs = new wxFileSystem();
			wxFSFile* file = fs->OpenFile( archive + wxString::Format( wxT( "#zip:%02d.jpg" ), n ) );
			if ( file ) {
				wxInputStream* s = file->GetStream();
				wxFileOutputStream of( wxT( "./image/hello.jpg" ) );
				s->Read( of );
				delete file;
			}
			delete fs;
		}

		// Main Window
		MainFrame *mainframe = new MainFrame( NULL, ID_MAIN, wxEmptyString, wxPoint( rect.x, rect.y ), rect.GetSize(), wxDEFAULT_FRAME_STYLE );
		mainframe->InDevelop( true );
		mainframe->SetServer( m_server );
		mainframe->SetUser( authdlg->GetUser() );
		mainframe->LoadDB();
		mainframe->SetTitle( wxT( "Re:Searcher - " ) + authdlg->GetUser() );
		mainframe->Show( true );
		//mainframe->Destroy();
	}
	authdlg->Destroy();

    return true;
}

int MyApp::OnExit()
{
	RemoveFile( wxT( "auth.db" ) );
	RemoveFile( wxT( "hhs.db" ) );
	RemoveFile( wxT( ".cache/*" )  );
	SaveSetting();
    return 0;
}

void MyApp::InitSetting()
{
    conf_file = wxGetCwd() + wxFILE_SEP_PATH + wxT( "app.conf" );
    config = new wxFileConfig( wxT( "MyApp" ), wxT( "T.Mutoh" ), conf_file, wxEmptyString, wxCONFIG_USE_LOCAL_FILE );

    config->SetPath( wxT( "/Geometry" ) );
    config->Read( wxT( "x" ), &rect.x );
    config->Read( wxT( "y" ), &rect.y );
    config->Read( wxT( "w" ), &rect.width );
    config->Read( wxT( "h" ), &rect.height );

	wxString proxy;
    config->SetPath( wxT( "/Server" ) );
    config->Read( wxT( "server" ), &m_server );
    config->Read( wxT( "proxy" ), &proxy );
    delete config;

    if ( !proxy.IsSameAs( wxEmptyString, false ) ) m_server = proxy;
	//m_server = wxT( "192.168.79.124:80" );
    if ( m_server.IsSameAs( wxEmptyString, false ) ) 
        m_server = wxT( "192.168.21.151:80" );	// nginx
}

void MyApp::SaveSetting()
{
    config = new wxFileConfig( wxT( "MyApp" ), wxT( "T.Mutoh" ), conf_file, wxEmptyString, wxCONFIG_USE_LOCAL_FILE );

    config->SetPath( wxT( "/Geometry" ) );
    config->Write( wxT( "x" ), rect.x );
    config->Write( wxT( "y" ), rect.y );
    config->Write( wxT( "w" ), rect.width );
    config->Write( wxT( "h" ), rect.height );
    delete config;
}

void MyApp::RemoveFile( wxString pattern )
{
	wxString file = wxFindFirstFile( pattern );
	while ( !file.empty() ) {
		wxRemoveFile( file  );
		file = wxFindNextFile();
	}
}