mainframe.cpp

来自「一款LINUX下的下载软件」· C++ 代码 · 共 2,143 行 · 第 1/5 页

CPP
2,143
字号
/***************************************************************************
*  mainframe.cpp
*
*  Wed Sep  6 22:19:52 2006
*  Copyright  2006  liubin,China
*  Email multiget@gmail.com
****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


#include "mgapp.h"
#include "mainframe.h"
#include "listenpipe.h"
#include <list>
#include <wx/image.h>
#include <locale.h>
#include <wx/aboutdlg.h>
#include "rightdownsplitter.h"
#include "taskmanager.h"
#include "lefttree.h"
#include "tasklist.h"
#include "mgnewtaskdlg.h"
#include "dropurl.h"
#include "mgurlparser.h"
#include "filemd5.h"

#include "meterctrl.h"
#include "systray.h"
#include "floatwin.h"
#include "hidewin.h"
#include "proxydlg.h"
#include "configwin.h"
#include "mirroradmin.h"
#include "propertydlg.h"
#include "batch.h"

#include "./newicons/16/new.xpm"
#include "./newicons/16/run.xpm"
#include "./newicons/16/stop.xpm"
#include "./newicons/16/delete.xpm"
#include "./newicons/16/up.xpm"
#include "./newicons/16/down.xpm"
#include "./newicons/16/plus.xpm"
#include "./newicons/16/sub.xpm"
#include "./newicons/16/reload.xpm"
#include "./newicons/16/option.xpm"
#include "./newicons/16/quit.xpm"
#include "./icons/about.xpm"
#include "./newicons/16/proxy.xpm"
#include "./newicons/16/save.xpm"
#include "./newicons/16/msgsystem_16.xpm"

#include "./newicons/32/tool_new.xpm"
#include "./newicons/32/tool_run.xpm"
#include "./newicons/32/tool_pause.xpm"
#include "./newicons/32/tool_delete.xpm"
#include "./newicons/32/tool_up.xpm"
#include "./newicons/32/tool_down.xpm"
#include "./newicons/32/tool_option.xpm"
#include "./newicons/32/tool_sub.xpm"
#include "./newicons/32/tool_plus.xpm"
#include "./newicons/32/tool_reload.xpm"
#include "./newicons/32/tool_proxy.xpm"
#include "./newicons/32/tool_property.xpm"
#include "./newicons/baby.xpm"
#include "./newicons/16/logo_16.xpm" 
//#include "./newicons/24/logo_24.xpm"
#include "./newicons/32/logo_32.xpm"
#include "./newicons/48/logo_48.xpm"
#include "./newicons/24/logo_22.xpm"

#include <fstream>

using namespace std;

DEFINE_EVENT_TYPE( mgID_TIMER )
DEFINE_EVENT_TYPE( mgEVT_TASK_FINISH )
DEFINE_EVENT_TYPE( mgEVT_URL_NOTICE )
DEFINE_EVENT_TYPE( mgEVT_CMD_NOTICE )
DEFINE_EVENT_TYPE( mgID_NEW_TASK )
DEFINE_EVENT_TYPE( mgID_START_TASK )
DEFINE_EVENT_TYPE( mgID_STOP_TASK )
DEFINE_EVENT_TYPE( mgID_CANCEL_TASK )
DEFINE_EVENT_TYPE( mgID_MOVEUP_TASK )
DEFINE_EVENT_TYPE( mgID_MOVEDOWN_TASK )
DEFINE_EVENT_TYPE( mgID_OPTION_CONFIG )
DEFINE_EVENT_TYPE( mgID_SUBTHREAD )
DEFINE_EVENT_TYPE( mgID_ADDTHREAD )
DEFINE_EVENT_TYPE( mgID_RELOAD )

DEFINE_EVENT_TYPE( mgID_VIEW_LANG )
DEFINE_EVENT_TYPE( mgID_VIEW_LANG_EN_UTF8 )
DEFINE_EVENT_TYPE( mgID_VIEW_LANG_CN_UTF8 )
DEFINE_EVENT_TYPE( mgID_VIEW_LANG_GB18030 )
DEFINE_EVENT_TYPE( mgID_VIEW_LANG_TW_UTF8 )
DEFINE_EVENT_TYPE( mgID_VIEW_LANG_TW_BIG5 )DEFINE_EVENT_TYPE( mgID_VIEW_LANG_DE_UTF8 )
DEFINE_EVENT_TYPE( mgID_VIEW_LANG_PT_UTF8 )

DEFINE_EVENT_TYPE( mgID_VIEW_TOOLBAR )
DEFINE_EVENT_TYPE( mgID_VIEW_TOOLBAR_SIZE16 )
DEFINE_EVENT_TYPE( mgID_VIEW_TOOLBAR_SIZE24 )
DEFINE_EVENT_TYPE( mgID_VIEW_TOOLBAR_SIZE32 )
DEFINE_EVENT_TYPE( mgID_VIEW_STATUSBAR )
DEFINE_EVENT_TYPE( mgID_OPTION_PROXY )
DEFINE_EVENT_TYPE( mgID_OPTION_PROPERTY )

DEFINE_EVENT_TYPE( mgID_FILE_SAVECONFIG );
DEFINE_EVENT_TYPE( mgID_FILE_SAVETASK );
DEFINE_EVENT_TYPE( mgID_FILE_SAVEPROXY );
DEFINE_EVENT_TYPE( mgID_HELP_VISITHOME );

// Event table for MainFrame
BEGIN_EVENT_TABLE( MainFrame, wxFrame )
EVT_MENU( wxID_ABOUT, MainFrame::OnAbout )
EVT_MENU( wxID_EXIT, MainFrame::OnQuit )
EVT_TIMER( mgID_TIMER, MainFrame::OnTimer )
EVT_SIZE( MainFrame::OnSize )
EVT_CLOSE( MainFrame::OnClose )
//EVT_WINDOW_DESTROY( MainFrame::OnDestroy )
EVT_COMMAND( wxID_ANY, mgEVT_TASK_FINISH, MainFrame::OnTaskFinish )
EVT_COMMAND( wxID_ANY, mgEVT_URL_NOTICE, MainFrame::OnUrlNotice )
EVT_COMMAND( wxID_ANY, mgEVT_CMD_NOTICE, MainFrame::OnCmdNotice )
EVT_ICONIZE( MainFrame::OnIconize )
EVT_MENU( mgID_NEW_TASK, MainFrame::OnNewTask )
EVT_MENU( mgID_START_TASK, MainFrame::OnStartTask )
EVT_MENU( mgID_STOP_TASK, MainFrame::OnStopTask )
EVT_MENU( mgID_CANCEL_TASK, MainFrame::OnCancelTask )
EVT_MENU( mgID_SUBTHREAD, MainFrame::OnDecreaseThread )
EVT_MENU( mgID_ADDTHREAD, MainFrame::OnIncreaseThread )
EVT_MENU( mgID_RELOAD, MainFrame::OnReloadFile )
EVT_MENU( mgID_OPTION_CONFIG, MainFrame::OnConfig )
EVT_MENU( mgID_MOVEDOWN_TASK, MainFrame::OnMoveDown )
EVT_MENU( mgID_MOVEUP_TASK, MainFrame::OnMoveUp )
EVT_MENU( mgID_VIEW_LANG_EN_UTF8, MainFrame::OnLangEnUtf8 )
EVT_MENU( mgID_VIEW_LANG_CN_UTF8, MainFrame::OnLangCnUtf8 )
EVT_MENU( mgID_VIEW_LANG_GB18030, MainFrame::OnLangCn18030 )
EVT_MENU( mgID_VIEW_LANG_TW_UTF8, MainFrame::OnLangTwUtf8 )
EVT_MENU( mgID_VIEW_LANG_TW_BIG5, MainFrame::OnLangTwBig5 )EVT_MENU( mgID_VIEW_LANG_DE_UTF8, MainFrame::OnLangDeUtf8 )
EVT_MENU( mgID_VIEW_LANG_PT_UTF8, MainFrame::OnLangPtUtf8 )
EVT_MENU( mgID_VIEW_STATUSBAR, MainFrame::OnStatusBar )
EVT_MENU( mgID_VIEW_TOOLBAR_SIZE16, MainFrame::OnToolbarSize16 )
EVT_MENU( mgID_VIEW_TOOLBAR_SIZE24, MainFrame::OnToolbarSize24 )
EVT_MENU( mgID_VIEW_TOOLBAR_SIZE32, MainFrame::OnToolbarSize32 )
EVT_MENU( mgID_OPTION_PROXY, MainFrame::OnProxySetting )
EVT_MENU( mgID_OPTION_PROPERTY, MainFrame::OnProperty )

EVT_MENU( mgID_FILE_SAVECONFIG, MainFrame::OnSaveConfig )
EVT_MENU( mgID_FILE_SAVEPROXY, MainFrame::OnSaveProxy )
EVT_MENU( mgID_FILE_SAVETASK, MainFrame::OnSaveTask )
EVT_MENU( mgID_HELP_VISITHOME, MainFrame::OnVisitHome )

EVT_WINDOW_CREATE( MainFrame::OnWindowCreate )
EVT_MAXIMIZE( MainFrame::OnMaximize )
//EVT_SET_FOCUS( MainFrame::OnFocus )
//EVT_KILL_FOCUS( MainFrame::OnLostFocus )
END_EVENT_TABLE()

#define  _MGSTR(s) wxGetApp().GetWxStr(s)

extern int gRunTaskNum;
extern int gConnectTimeOut;
extern int gReceiveTimeOut;
extern int gTaskDefThread;
extern int gRetryTime;
extern int gRetryWait;
extern std::list<std::string> gSavePathHistory;
extern std::string gDefSavePath, gDefFtpPass, gsMonitorExt, gsIgnoreExt;

extern bool gbIgnoreExt;
extern bool gbMonitorExt;
extern bool gbShowDropWin;
extern bool gbMonitorClip;
extern bool gbSoundTip;
extern bool gbAutoMd5;
extern int gDropWinX;
extern int gDropWinY;
extern int gSpeedMode;

//a simple mirror admin,because filemanager need it.
//extern CMirrorAdmin gMirrorAdmin;

void MainFrame::OnIconize( wxIconizeEvent& event )
{
    if ( event.Iconized() )
    {
        Iconize( true );
        Hide();
    }
}

void MainFrame::OnWindowCreate( wxWindowCreateEvent& event )
{
#ifndef WIN32
//in windows ,this is called before ctor. so only use in linux
    if ( m_FloatWin )
    {
		m_FloatWin->Show(); 
	}

    if ( m_SysTray )
    {
		m_SysTray->SetIcon( wxIcon( logo_22_xpm ), _MGSTR( _S_APP_NAME ) );
	}
#else
	event.Skip();
#endif
}

MainFrame::MainFrame( const wxString& title )
        : wxFrame( NULL, wxID_ANY, title, wxDefaultPosition, wxSize( 800, 600 ),
                   wxRESIZE_BORDER | wxDEFAULT_FRAME_STYLE | wxMAXIMIZE | wxFULL_REPAINT_ON_RESIZE )
{


    m_nCurrentShowType = ( _STATUS_ROOT );
    m_nTaskID = 0;
    m_nCurrentSelectTaskID = -1;
    m_lefttree = NULL;
    m_tasklist = NULL;
    m_vspliter = NULL;
    m_hspliter = NULL;
    m_vsubspliter = NULL;
    m_ToolBar = NULL;
    m_MenuBar = NULL;
	m_MeterBar = NULL;
    m_HideWin = NULL;
    m_NewTaskDlg = NULL;
    m_PropertyDlg = NULL;	m_lang = NULL;
    //m_bMonitorClip = true;
    m_bShowStatusBar = true;
    TOOLBAR_SIZE = 32;
    m_nWinSizeW = 800;
    m_nWinSizeH = 600;
    pthread_mutex_init ( &m_TaskMutex, NULL );
    //change to SetIcons
    wxIconBundle bund;
    bund.AddIcon( wxIcon( logo_16_xpm ) );
    bund.AddIcon( wxIcon( logo_22_xpm ) );
    bund.AddIcon( wxIcon( logo_32_xpm ) );
    bund.AddIcon( wxIcon( logo_48_xpm ) );

    SetIcons( bund );


    m_TaskManager = new CTaskManager( this );


    ReadTaskDB();

    ReadProxyDB();

    if ( !ReadConfig() )
    {
        GetUserHome( gDefSavePath );
    }

    InitFileTypeList();

    InitMeterBar(); //call before onsize
    InitSpliter();	//call before onsize
    InitMenuBar(); //make menu generate a size event in win32
	
    InitToolBar();
    InitStatusBar();



    m_Timer.SetOwner( this, mgID_TIMER );
    m_Timer.Start( 1000 );
	
    if ( gbShowDropWin )
        m_FloatWin = new CFloatWin( this );
    else
        m_FloatWin = NULL;

    m_SysTray = new CSysTray( this );
	
#ifdef WIN32
//in windows 
    if ( m_FloatWin )
    {//float win have trouble currently
		m_FloatWin->Show(); 
	}

    if ( m_SysTray )
    {
		m_SysTray->SetIcon( wxIcon( logo_22_xpm ), _MGSTR( _S_APP_NAME ) );
	}
#endif
	
    m_HideWin = new CHideWin( this );

    m_HideWin->MonitorClip( gbMonitorClip );

    UpdateToolBar();

    ScheduleTask();

#ifndef WIN32
    CListenPipe *tpi = new CListenPipe( this );

    tpi->Create();

    tpi->Run();
	
#endif
}


MainFrame::~MainFrame()
{
    delete m_TaskManager;
    if ( m_FloatWin )
        m_FloatWin->Destroy();
    m_HideWin->Destroy();
}


void MainFrame::InitMenuBar()
{


    wxMenu *fileMenu = new wxMenu;

    wxMenu *taskMenu = new wxMenu;

    wxMenu *viewMenu = new wxMenu;

    wxMenu *setMenu = new wxMenu;

    wxMenu *helpMenu = new wxMenu;

    wxMenuItem *visit =
        new wxMenuItem( helpMenu, mgID_HELP_VISITHOME, _MGSTR( _S_MENU_HELP_VISITHOME ), wxT( "Visit homepage" ) );
    visit->SetBitmap( wxBitmap( msgsystem_16_xpm ) );
    helpMenu->Append( visit );
	
    wxMenuItem *about =
        new wxMenuItem( helpMenu, wxID_ABOUT, _MGSTR( _S_MENU_HELP_ABOUT ), wxT( "About program" ) );
    about->SetBitmap( wxBitmap( about_xpm ) );
    helpMenu->Append( about );

    wxMenuItem *option =
        new wxMenuItem( setMenu, mgID_OPTION_CONFIG, _MGSTR( _S_MENU_OPTION_CONFIG ), wxT( "sys setting" ) );
    option->SetBitmap( wxBitmap( option_xpm ) );
    setMenu->Append( option );

    wxMenuItem *proxy =
        new wxMenuItem( setMenu, mgID_OPTION_PROXY, _MGSTR( _S_MENU_OPTION_PROXYCONFIG ), wxT( "proxy setting" ) );
    proxy->SetBitmap( wxBitmap( proxy_xpm ) );
    setMenu->Append( proxy );

	
    wxMenuItem *saveconfig =
        new wxMenuItem( fileMenu, mgID_FILE_SAVECONFIG, _MGSTR( _S_MENU_FILE_SAVECONFIG ), wxT( "Save Config" ) );
    saveconfig->SetBitmap( wxBitmap( save_xpm ) );
    fileMenu->Append( saveconfig );	

    wxMenuItem *savetask =
        new wxMenuItem( fileMenu, mgID_FILE_SAVETASK, _MGSTR( _S_MENU_FILE_SAVETASK ), wxT( "Save Task" ) );
    savetask->SetBitmap( wxBitmap( save_xpm ) );
    fileMenu->Append( savetask );	
	
    wxMenuItem *saveproxy =
        new wxMenuItem( fileMenu, mgID_FILE_SAVEPROXY, _MGSTR( _S_MENU_FILE_SAVEPROXY ), wxT( "Save Proxy" ) );
    saveproxy->SetBitmap( wxBitmap( save_xpm ) );
    fileMenu->Append( saveproxy );		

	fileMenu->AppendSeparator();
	
    wxMenuItem *quit =
        new wxMenuItem( fileMenu, wxID_EXIT, _MGSTR( _S_MENU_FILE_QUIT ), wxT( "Quit program" ) );
    quit->SetBitmap( wxBitmap( quit_xpm ) );
    fileMenu->Append( quit );	
	
    //view

    wxMenu *lang = new wxMenu;
    lang->AppendRadioItem( mgID_VIEW_LANG_EN_UTF8, wxT( "English UTF-8" ), wxT( "change to english." ) );
    lang->AppendSeparator();
    lang->AppendRadioItem( mgID_VIEW_LANG_CN_UTF8, wxT( "Chinese UTF-8" ), wxT( "change to chinese" ) );
    lang->AppendRadioItem( mgID_VIEW_LANG_GB18030, wxT( "Chinese GB18030/GB2312/GBK" ), wxT( "change to chinese" ) );    lang->AppendSeparator();
    lang->AppendRadioItem( mgID_VIEW_LANG_TW_UTF8, wxT( "Taiwan UTF-8" ), wxT( "change to taiwan utf-8" ) );
    lang->AppendRadioItem( mgID_VIEW_LANG_TW_BIG5, wxT( "Taiwan BIG5" ), wxT( "change to taiwan big5" ) );    lang->AppendSeparator();    lang->AppendRadioItem( mgID_VIEW_LANG_DE_UTF8, wxT( "German UTF-8" ), wxT( "change to German" ) );
    lang->AppendSeparator();
    lang->AppendRadioItem( mgID_VIEW_LANG_PT_UTF8, wxT( "Portuguese UTF-8" ), wxT( "change to Portuguese" ) );	m_lang=lang;	CheckLangMenu( wxGetApp().GetLang() );


    viewMenu->Append( mgID_VIEW_LANG, _MGSTR( _S_MENU_VIEW_LANG ), lang );
    viewMenu->AppendSeparator();

    wxMenu *tool = new wxMenu;
    tool->AppendRadioItem( mgID_VIEW_TOOLBAR_SIZE16, _MGSTR( _S_MENU_VIEW_TOOLBAR_SIZE16 ), wxT( "size 16" ) );
    tool->AppendRadioItem( mgID_VIEW_TOOLBAR_SIZE24, _MGSTR( _S_MENU_VIEW_TOOLBAR_SIZE24 ), wxT( "size 24" ) );
    tool->AppendRadioItem( mgID_VIEW_TOOLBAR_SIZE32, _MGSTR( _S_MENU_VIEW_TOOLBAR_SIZE32 ), wxT( "size 32" ) );
    viewMenu->Append( mgID_VIEW_TOOLBAR, _MGSTR( _S_MENU_VIEW_TOOLBAR ), tool );

    switch ( TOOLBAR_SIZE )
    {

        case 16:
        tool->Check( mgID_VIEW_TOOLBAR_SIZE16, true );
        break;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?