view src/myframe.cpp @ 1:20018a6f69a9

implement copy.
author pyon@macmini
date Wed, 31 Oct 2012 18:08:52 +0900
parents 7c3921bf511e
children deaf3f2d54b2
line wrap: on
line source

// Filename   : myframe.cpp
// Last Change: 20-Oct-2012.
//

#include "main.h"
#include "myframe.h"
#include "about.h"

// resources
#if !defined(__WXMSW__) && !defined(__WXPM__)
    #include "sample.xpm"
#endif

MyFrame::MyFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) 
    : wxFrame( parent, id, title, pos, size, style )
{
	this->SetSizeHints( wxDefaultSize, wxDefaultSize );
    this->SetBackgroundColour( wxColour(wxT("WHEAT")) );
    // set the frame icon
    SetIcon(wxICON(sample));
	
    // メニューバー Menu
	m_menubar = new wxMenuBar();
	m_menuFile = new wxMenu();

	wxMenuItem* m_menuItemAbout = new wxMenuItem( m_menuFile, ID_MNABOUT, wxString( wxT("&About...\tF1") ) , wxT("Show about dialog"), wxITEM_NORMAL );
	m_menuFile->Append( m_menuItemAbout );
	
	m_menubar->Append( m_menuFile, wxT("File(&F)") ); 
	
	this->SetMenuBar( m_menubar );
	
    // 
	wxFlexGridSizer* fgSizer = new wxFlexGridSizer( 0, 4, 0, 0 );
	fgSizer->SetFlexibleDirection( wxBOTH );
	fgSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
	
	m_staticText1 = new wxStaticText( this, wxID_ANY, wxT("Folder"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_staticText1, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
	
	m_dirPickerWatch = new wxDirPickerCtrl( this, ID_WATCHDIR, wxEmptyString, wxT("Select a folder"), wxDefaultPosition, wxDefaultSize, wxDIRP_DEFAULT_STYLE );
	fgSizer->Add( m_dirPickerWatch, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_staticText2 = new wxStaticText( this, wxID_ANY, wxT("Filename"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_staticText2, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
	
	m_textCtrlFile = new wxTextCtrl( this, ID_FILENAME, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_textCtrlFile, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
	
	m_staticText3 = new wxStaticText( this, wxID_ANY, wxT("Command"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_staticText3, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
	
	m_textCtrlCommand = new wxTextCtrl( this, ID_COMMAND, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 ); 
	fgSizer->Add( m_textCtrlCommand, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
	
	m_staticText4 = new wxStaticText( this, wxID_ANY, wxT("wait [sec]"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_staticText4, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );

	m_textCtrlWait = new wxTextCtrl( this, ID_WAIT, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 ); 
	fgSizer->Add( m_textCtrlWait, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );

	m_staticText5 = new wxStaticText( this, wxID_ANY, wxT("MoveTo"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_staticText5, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
	
	m_dirPickerMove = new wxDirPickerCtrl( this, ID_MOVEDIR, wxEmptyString, wxT("Select a folder"), wxDefaultPosition, wxDefaultSize, wxDIRP_DEFAULT_STYLE );
	fgSizer->Add( m_dirPickerMove, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_staticText6 = new wxStaticText( this, wxID_ANY, wxT("copy"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_staticText6, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );

    m_checkBoxCopy = new wxCheckBox( this, ID_COPY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_checkBoxCopy, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );

	m_staticTextDummy1 = new wxStaticText( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_staticTextDummy1, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );

	m_staticTextDummy2 = new wxStaticText( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_staticTextDummy2, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );

	m_staticTextDummy3 = new wxStaticText( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_staticTextDummy3, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
	
	m_toggleBtn = new wxToggleButton( this, ID_TOGGLEWATCH, wxT("Start Watching"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizer->Add( m_toggleBtn, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
	
	this->SetSizer( fgSizer );
	this->Layout();
	
	this->Centre( wxBOTH );

    //m_dirPickerWatch->SetPath( wxT("/Users/takayuki/wx/dw_test" ) );
    //m_dirPickerMove->SetPath( wxT("/Users/takayuki/wx/dw_test/log" ) );
    m_dirPickerWatch->SetPath( wxT("\\\\Kai-sv51\\Users\\Administrator\\Documents\\Fuji Xerox\\DocuWorks\\DWFolders\\ユーザーフォルダ" ) );
    m_dirPickerMove->SetPath( wxT("C:\\Users\\KAIGO\\Desktop\\dww\\" ) );
    m_textCtrlFile->SetValue( wxT("test.file") );
}

MyFrame::~MyFrame()
{
    delete m_watcher;
}

// Event Table
BEGIN_EVENT_TABLE( MyFrame, wxFrame )
    EVT_SIZE( MyFrame::OnWinSize )
    EVT_MOVE( MyFrame::OnWinMove )
    EVT_MENU( ID_MNABOUT, MyFrame::OnAbout )
    //EVT_MENU( wxID_EXIT,          MyFrame::OnQuit )
    EVT_TOGGLEBUTTON( ID_TOGGLEWATCH, MyFrame::OnWatch )
    EVT_CLOSE( MyFrame::SaveConfig )
END_EVENT_TABLE()

// Event Handlers & Functions
/* フォルダ監視開始/停止スイッチ */
void MyFrame::OnWatch( wxCommandEvent& WXUNUSED(event) )
{
    Connect( wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler( MyFrame::OnFileSystemEvent ) );

    static bool enabled = false;
    enabled = !enabled;

    if ( enabled ) {
        m_toggleBtn->SetLabelText( wxT("Stop Watching") );
        m_watcher = new wxFileSystemWatcher();
        m_watcher->SetOwner( this );

        m_watcher->Add( m_dirPickerWatch->GetPath(), wxFSW_EVENT_CREATE );
    }
    else {
        m_toggleBtn->SetLabelText( wxT("Start Watching") );
        wxDELETE( m_watcher );
    }
}

/* フォルダ監視 */
void MyFrame::OnFileSystemEvent( wxFileSystemWatcherEvent& event )
{
    wxMilliSleep( 1500 );

    wxString watchdir = m_dirPickerWatch->GetPath();
    wxString filename = m_textCtrlFile->GetValue();
    wxString command  = m_textCtrlCommand->GetValue();
    wxString movedir  = m_dirPickerMove->GetPath();

    wxString from;
    wxString to;

    wxString file;
    wxDir dir( watchdir );
    bool cont = dir.GetFirst( &file, filename, wxDIR_FILES );
    while ( cont ) {

        if ( !command.IsEmpty() ) {
            wxSleep( 2 );
            wxExecute( command );
        }

        if ( !movedir.IsEmpty() ) {
            from = watchdir + wxFILE_SEP_PATH + filename;
            wxFileName f( from );
            wxDateTime now = wxDateTime::Now();
            to = movedir + wxFILE_SEP_PATH + f.GetName() + wxT("_") + now.Format(wxT("%Y%m%d%H%M%S")) + wxT(".") + f.GetExt();
            if ( m_checkBoxCopy->IsChecked() ) {
                wxCopyFile( from, to, false );
            }
            else {
                wxRenameFile( from, to, false );
            }
        }

        cont = dir.GetNext( &file );
    }
}

/* サイズ変更 */
void MyFrame::OnWinSize( wxSizeEvent& event )
{
    this->Refresh( true, NULL );
    TellLocation();
    event.Skip();
}
/* ウィンドウ移動 */
void MyFrame::OnWinMove( wxMoveEvent& WXUNUSED(event) )
{
    TellLocation();
    return;
}
/* ウィンドウ位置とサイズを表示 */
void MyFrame::TellLocation( void ) 
{
    wxRect r = this->GetRect();
    int x = r.GetX();
    int y = r.GetY();
    int w = r.GetWidth();
    int h = r.GetHeight();
    //SetStatusText( wxString::Format(wxT("(%d,%d) %dx%d"),x,y,w,h), 3 );
}
/* 終了 */
void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) )
{
    Close(true);
}
/* 設定を保存 */
void MyFrame::SaveConfig( wxCloseEvent& WXUNUSED(event) )
{
    if ( !IsIconized() && !IsMaximized() ) {
        wxGetApp().rect = this->GetRect();
    }
    Destroy();
}
/* アバウトダイアログ */
void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
{
    AboutDialog* aboutDlg = new AboutDialog( this, wxID_ANY, wxT("About this Software"), wxDefaultPosition, wxSize(320,280), wxDEFAULT_DIALOG_STYLE|wxSTAY_ON_TOP ); 
    aboutDlg->ShowModal();
}