view src/main.cpp @ 16:b651aa41b9d4 default tip

hhsinfo method (server)
author pyon@macmini
date Mon, 15 Jul 2019 07:03:05 +0900
parents c262e17de9b1
children
line wrap: on
line source

// Filename   : main.cpp
// Last Change: 2019-05-29 水 15:04:09.
//

#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;
	}

	// Validation Host
	bool valid_host = false;
	if ( !m_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 ( m_unlock_key.IsSameAs( output[0] ) ) valid_host = true;
	}

    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->SetDBdir( m_dbdir );
	mainframe->InDevelop( true );

	bool dlhhs = false;
	if ( valid_host && !m_qdlhhs ) {
		dlhhs = true;
	} else {
		wxMessageDialog dlg( NULL, wxT("download h-data ?"), wxT("Ask"), wxYES_NO );
		if ( wxID_YES == dlg.ShowModal() ) dlhhs = true;
	}

	if ( !mainframe->GetDB( true, dlhhs, false ) ) {
		mainframe->Destroy();
		return true;
	}

	// Splash Screen
	SplashScreen( m_splash );

	if ( valid_host ) {
		mainframe->SetUser( wxT( "root" ) );
		mainframe->SetTitle( wxT( "Re:Searcher - root" ) );
		mainframe->LoadDB( dlhhs );
		mainframe->Show( true );
		mainframe->Raise();
		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( dlhhs );
		mainframe->Show( true );
	} else {
		mainframe->Destroy();
	}
	authdlg->Destroy();

    return true;
}

int MyApp::OnExit()
{
	RemoveFile( wxT( "hhs.csv.tmp" ) );
	RemoveFile( wxT( "hhs.csv" ) );
	RemoveFile( wxT( "index.db.tmp" ) );
	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( "/DBManage" ) );
    config->Read( wxT( "dbdir" ), &m_dbdir );
    config->Read( wxT( "qdlhhs" ), &m_qdlhhs );

    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( "/Print" ) );
    config->Read( wxT( "zoom" ), &pzoom );

    config->SetPath( wxT( "/Misc" ) );
    config->Read( wxT( "splash" ), &m_splash );
    config->Read( wxT( "unlock_key" ), &m_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 );

    config->SetPath( wxT( "/Print" ) );
    config->Write( wxT( "zoom" ), pzoom );

    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* splashwin = 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();
	}
}