view src/main.cpp @ 5:e3b10fb860b3

release v1.0.
author pyon@macmini
date Mon, 22 Oct 2018 22:17:02 +0900
parents 06342fc544e4
children 9a8b581c1993
line wrap: on
line source

// Filename   : main.cpp
// Last Change: 2018-10-22 月 16:14:28.
//
#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();

	// 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, 3000, NULL, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE|wxSTAY_ON_TOP );
			delete file;
		}
		wxMilliSleep( 250 );
	}

	unsigned int seed = (unsigned int)time( 0 );
	srand( seed );
	int n = rand() % 13;
	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;

	// Login Dialog
    AuthDialog *authdlg = new AuthDialog( NULL, wxID_ANY, wxT( "Who are you ?" ), wxDefaultPosition, wxDefaultSize, wxCAPTION );
	authdlg->SetServer( m_server );
	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 );

		// 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()
{
	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;
}