view src/main.cpp @ 11:799b6008db8e

auto-upgrade.
author pyon@macmini
date Mon, 12 Nov 2018 19:35:50 +0900
parents 36811fd22bd2
children f5ffc34f045a
line wrap: on
line source

// Filename   : main.cpp
// Last Change: 2018-11-09 金 11:54:20.
//

#include <wx/socket.h>
#include <wx/sstream.h>
#include <wx/protocol/http.h>
#include "id.h"
#include "main.h"
#include "auth.h"
#include "net.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;

	InitSetting();

	// Check New-Version
	wxString newver = GetVersion();
	if ( newver.Cmp( RSVER ) == 1 ) {	// 1.9 -> 1.91 -> 1.92 ...
		wxString sv = wxString::Format( wxT( "%s:%d" ), m_serveraddr, m_serverport );

		wxArrayString args;
		args.Add( wxT( "upgrade.bat" ) );
		args.Add( sv );
		args.Add( RSVER );
		args.Add( newver );

		wxExecute( wxJoin( args, ' ', '\\' ) );
		return false;
	}

    wxImage::AddHandler( new wxJPEGHandler );
    wxImage::AddHandler( new wxPNGHandler  );
	wxFileSystem::AddHandler( new wxZipFSHandler );

	// Main Window
	MainFrame *mainframe = new MainFrame( NULL, ID_MAIN, wxEmptyString, wxPoint( rect.x, rect.y ), rect.GetSize(), wxDEFAULT_FRAME_STYLE );
	mainframe->SetServer( m_serveraddr, m_serverport );
	mainframe->InDevelop( true );
	if ( !mainframe->GetDB() ) {
		mainframe->Destroy();
		return true;
	}

	// Splash Screen
	SplashScreen( splash );

	// Mode
	if ( !unlock_key.IsEmpty() ) {
		wxIPV4address addr;
		addr.Hostname( wxGetFullHostName() );

		wxString key = wxT( "re:searcher" ) + addr.IPAddress();
		wxArrayString args;
		args.Add( wxT( "crypto.exe" ) );
		args.Add( wxT( "-a" ) );
		args.Add( key );

		wxArrayString output, errors;
		wxExecute( wxJoin( args, ' ', '\\' ), output, errors );

		if ( unlock_key.IsSameAs( output[0] ) ) {
			mainframe->SetUser( wxT( "root" ) );
			mainframe->SetTitle( wxT( "Re:Searcher - root" ) );
			mainframe->LoadDB();
			mainframe->Show( true );
			return true;
		}
	}

	// User Dialog
    AuthDialog *authdlg = new AuthDialog( NULL, wxID_ANY, wxT( "Who are you ?" ), wxDefaultPosition, wxDefaultSize, wxCAPTION );
	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();
			wxString archive = wxT( "file:///./myapp.bin" );
			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;
		}

		mainframe->SetUser( authdlg->GetUser() );
		mainframe->SetTitle( wxT( "Re:Searcher - " ) + authdlg->GetUser() );
		mainframe->LoadDB();
		mainframe->Show( true );
	} else {
		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 );

    config->SetPath( wxT( "/Server" ) );
    config->Read( wxT( "proxy_address" ), &m_serveraddr );
    config->Read( wxT( "proxy_port" ),    &m_serverport );
    if ( m_serveraddr.IsSameAs( wxEmptyString, false ) ) {
		config->Read( wxT( "address" ), &m_serveraddr );
		config->Read( wxT( "port" ),    &m_serverport );
	}

    if ( m_serveraddr.IsSameAs( wxEmptyString, false ) ) {
        m_serveraddr = wxT( "192.168.21.151" );	// nginx
        m_serverport = 80;
	}

    config->SetPath( wxT( "/Misc" ) );
    config->Read( wxT( "splash" ), &splash );
    config->Read( wxT( "unlock_key" ), &unlock_key );

    delete config;
}

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::SplashScreen( int ms )
{
	if ( ms < 0 ) return;

	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, 2000, NULL, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE|wxSTAY_ON_TOP );
			delete file;
		}
		wxMilliSleep( ms );
	}
	delete fs;
}

wxString MyApp::GetVersion( void )
{
	wxHTTP get;
	get.SetTimeout( 30 );
	get.SetFlags( wxSOCKET_WAITALL|wxSOCKET_BLOCK );
	while ( !get.Connect( m_serveraddr, m_serverport ) )
		wxSleep( 1 );

	wxString version;
	wxString url = wxT( "/release/version" );
	wxInputStream *http_istream = get.GetInputStream( url );
	if ( get.GetError() == wxPROTO_NOERR ) {
		wxStringOutputStream out_stream( &version );
		http_istream->Read( out_stream );
	}

	wxDELETE( http_istream );
	get.Close();

	return version;
}

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