FileView.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:11k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // FileView.cpp : implementation file
- //
- #include "stdafx.h"
- #include "File.h"
- #include "FileView.h"
- #include "MainFrame.h"
- #include "SelectDialog.h"
- #include "ReceiveDialog.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern CFileApp theApp;
- /////////////////////////////////////////////////////////////////////////////
- // CFileView
- IMPLEMENT_DYNCREATE(CFileView, CFormView)
- CFileView::CFileView() : CFormView( CFileView::IDD )
- {
- //{{AFX_DATA_INIT(CFileView)
- //}}AFX_DATA_INIT
- this->mode = 0;
- this->frame = NULL;
- }
- CFileView::~CFileView()
- {
- }
- void CFileView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CFileView)
- DDX_Control(pDX, IDC_LIST, m_list);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CFileView, CFormView)
- //{{AFX_MSG_MAP(CFileView)
- ON_WM_SIZE()
- //}}AFX_MSG_MAP
- ON_MESSAGE( WM_RECEIVE , OnReceive )
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFileView diagnostics
- void CFileView::OnSize(UINT nType, int cx, int cy)
- {
- CFormView::OnSize( nType , cx , cy );
- CFormView::ShowScrollBar( SB_BOTH , FALSE );
- if( this->m_list )
- this->m_list.MoveWindow( 0 , 0 , cx , cy );
- }
- void CFileView::SetMode( CFrameWnd * mainFrame , int mode )
- {
- this->frame = ( CMainFrame * )mainFrame;
- this->mode = mode;
- if( this->mode == SEND )
- {
- this->m_list.InsertColumn( 0 , "文件名" , LVCFMT_LEFT , this->m_list.GetStringWidth( " 文件名 " ) );
- this->m_list.InsertColumn( 1 , "大小" , LVCFMT_RIGHT , this->m_list.GetStringWidth( " 大小 " ) );
- this->m_list.InsertColumn( 2 , "已发送" , LVCFMT_RIGHT , this->m_list.GetStringWidth( " 已发送 " ) );
- this->m_list.InsertColumn( 3 , "当前状态" , LVCFMT_CENTER , this->m_list.GetStringWidth( " 当前状态 " ) );
- }
- else if( this->mode == RECEIVE )
- {
- this->m_list.InsertColumn( 0 , "文件名" , LVCFMT_LEFT , this->m_list.GetStringWidth( " 文件名 " ) );
- this->m_list.InsertColumn( 1 , "大小" , LVCFMT_RIGHT , this->m_list.GetStringWidth( " 大小 " ) );
- this->m_list.InsertColumn( 2 , "来自用户" , LVCFMT_LEFT , this->m_list.GetStringWidth( " 来自用户 " ) );
- this->m_list.InsertColumn( 3 , "已接收" , LVCFMT_RIGHT , this->m_list.GetStringWidth( " 已接收 " ) );
- }
- this->m_list.SetExtendedStyle( LVS_EX_FULLROWSELECT );
- //设定系统图标
- SHFILEINFO shFinfo;
- HIMAGELIST imglist = ( HIMAGELIST )SHGetFileInfo( "C:\" , 0 , &shFinfo , sizeof( shFinfo ) , SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
- this->m_list.SendMessage( LVM_SETIMAGELIST , ( WPARAM )LVSIL_SMALL , ( LPARAM )imglist );
- // ScrollBar( this->m_list.GetSafeHwnd( ) );
- }
- void CFileView::Insert( const char * filename )
- {
- LVFINDINFO info;
- info.flags = LVFI_STRING;
- info.psz = filename;
- if( this->m_list.FindItem( &info ) != -1 )
- {
- MessageBox( "文件 " + ( CString )filename + " 已经存在" , "文件传输" , MB_ICONSTOP | MB_OK );
- return;
- }
- CFileStatus status;
- if( CFile::GetStatus( filename , status ) && status.m_size )
- {
- SHFILEINFO shFinfo;
- ::SHGetFileInfo( filename , 0 , &shFinfo , sizeof( shFinfo ) , SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
- int index = this->m_list.InsertItem( this->m_list.GetItemCount( ), filename , shFinfo.iIcon );
- CString s;
- s.Format( "%d 个字节" , status.m_size );
- this->m_list.SetItemText( index , 1 , s );
- this->m_list.SetItemText( index , 2 , "0 个字节" );
- this->m_list.SetItemText( index , 3 , "待发送" );
- }
- }
- void CFileView::Delete( void )
- {
- CString text;
- int index;
- if( this->mode == SEND )
- {
- POSITION pos = this->m_list.GetFirstSelectedItemPosition( );
- while( pos )
- {
- index = this->m_list.GetNextSelectedItem( pos );
- text = this->m_list.GetItemText( index , 3 );
- //等待发送
- if( ! text.CollateNoCase( "待发送" ) )
- {
- this->m_list.DeleteItem( index );
- pos = this->m_list.GetFirstSelectedItemPosition( );
- }
- else if( MessageBox( "文件 " + this->m_list.GetItemText( index , 0 ) + " 正在发送n是否停止发送并删除?" , "文件传输" , MB_ICONINFORMATION | MB_YESNO ) == IDYES )
- {
- this->m_list.DeleteItem( index );
- pos = this->m_list.GetFirstSelectedItemPosition( );
- }
- }
- }
- else
- {
- POSITION pos = this->m_list.GetFirstSelectedItemPosition( );
- while( pos )
- {
- index = this->m_list.GetNextSelectedItem( pos );
- if( ! this->m_list.GetItemData( index ) )
- {
- this->m_list.DeleteItem( index );
- pos = this->m_list.GetFirstSelectedItemPosition( );
- }
- else
- MessageBox( "文件" + this->m_list.GetItemText( index , 0 ) + "还没下载完毕nn不可删除该文件" , "文件传输" , MB_ICONSTOP | MB_OK );
- }
- }
- }
- void CFileView::Send( void )
- { //发送
- CSelectDialog dlg;
- if( dlg.DoModal( ) == IDOK )
- {
- POSITION pos = this->m_list.GetFirstSelectedItemPosition( );
- int index;
- CString filename;
- PStartSendFile sf;
- while( pos )
- {
- index = this->m_list.GetNextSelectedItem( pos );
- filename = this->m_list.GetItemText( index , 0 );
- CInfo * info = new CInfo( this , dlg.id_list , dlg.id_size );
- if( ! info->File.Open( filename , CFile::modeRead ) )
- {
- MessageBox( "打开文件" + filename + "失败nn无法发送该文件" , "文件传输" , MB_ICONSTOP | MB_OK );
- delete info;
- }
- else //启动线程发送
- {
- this->m_list.SetItemData( index , ( int )info );
- ::AfxBeginThread( CFileView::OnSend , info );
- }
- }
- }
- }
- void CFileView::Cancel( void )
- {
- CString text;
- int index;
- if( this->mode == SEND )
- {
- POSITION pos = this->m_list.GetFirstSelectedItemPosition( );
- while( pos )
- {
- index = this->m_list.GetNextSelectedItem( pos );
- text = this->m_list.GetItemText( index , 3 );
- //等待发送
- if( text.CollateNoCase( "待发送" ) )
- {//停止线程
- }
- }
- }
- }
- UINT CFileView::OnSend( void * pContext )
- {
- CInfo * info = ( CInfo * )pContext;
- PStartSendFile startSendFile;
- startSendFile.assembleData( info->pThis->frame->userName , info->File.GetFileName( ) , ( int )info , info->File.GetLength( ) , info->id_list , info->id_size );
- //通知外部开始发送文件
- info->pThis->frame->OnFile( info->pThis->frame->pContext , info->pThis->frame , startSendFile.buffer.GetBuffer( ) , startSendFile.buffer.GetSize( ) );
- char buffer[ 1000 ];
- int size;
- PSendFile sf;
- LVFINDINFO li;
- li.flags = LVFI_STRING | LVFI_PARAM ;
- li.psz = info->File.GetFilePath( );
- li.lParam = ( int )info;
- int sendSize = 0;
- int index = 0;
- while( ( size = info->File.Read( buffer , 1000 ) ) > 0 )
- { //组合数据包
- sf.assembleData( info->pThis->frame->userName , ( int )info , buffer , size , info->id_list , info->id_size );
- //发送文件
- info->pThis->frame->OnFile( info->pThis->frame->pContext , info->pThis->frame , sf.buffer.GetBuffer( ) , sf.buffer.GetSize( ) );
- //查找
- index = info->pThis->m_list.FindItem( &li );
- sendSize += size;
- sprintf( buffer , "%d个字节" , sendSize );
- info->pThis->m_list.SetItemText( index , 2 , buffer );
- sprintf( buffer , "%d%%" , int ( ( double )sendSize / info->File.GetLength( ) * 100 ) );
- info->pThis->m_list.SetItemText( index , 3 , buffer );
- }//停止发送
- sf.assembleData( info->pThis->frame->userName , ( int )info , 0 , 0 , info->id_list , info->id_size );
- info->pThis->frame->OnFile( info->pThis->frame->pContext , info->pThis->frame , sf.buffer.GetBuffer( ) , sf.buffer.GetSize( ) );
- //删除列表
- info->pThis->m_list.DeleteItem( index );
- //关闭文件
- info->File.Close( );
- delete info;
- return 0;
- }
- void CFileView::OnReceive( WPARAM wParam , LPARAM lParam )
- {
- char * buffer = ( char * )wParam;
- int size = lParam;
- switch( *( int * )buffer )
- {//发送文件请求
- case PSendFileREQTAG :
- {
- PSendFileREQ req;
- req.parseData( buffer , size );
- CReceiveDialog dlg( &req );
- //只有都接收或者都不接收
- PSendFileRES res;
- res.assembleData( req.user_id , req.recv_id , dlg.DoModal( ) == IDOK );
- //回复信息
- this->frame->OnFile( this->frame->pContext , this->frame , res.buffer.GetBuffer( ) , res.buffer.GetSize( ) );
- }break;
- //开始接收文件
- case PStartSendFileTAG :
- {
- PStartSendFile sf;
- sf.parseData( buffer , size );
- //更新文件名
- this->UpdateFileName( sf.filename );
- CFile * file = new CFile( );
- if( ! file->Open( sf.filename , CFile::modeCreate | CFile::modeWrite ) )
- {
- MessageBox( "创建文件" + sf.filename + "失败" , "文件传输" , MB_ICONSTOP | MB_OK );
- delete file;
- return;
- }
- SHFILEINFO shFinfo;
- ::SHGetFileInfo( sf.filename , 0 , &shFinfo , sizeof( shFinfo ) , SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
- int index = this->m_list.InsertItem( this->m_list.GetItemCount( ) , sf.filename , shFinfo.iIcon );
- sf.filename.Format( "%d个字节" , sf.file_size );
- this->m_list.SetItemText( index , 1 , sf.filename );
- this->m_list.SetItemText( index , 2 , sf.user_name );
- this->m_list.SetItemText( index , 3 , "0个字节" );
- this->m_list.SetItemData( index , sf.file_id );
- sf.filename.Format( "user : %s , file_id : %d" , sf.user_name , sf.file_id );
- //通过发送者的用户名和发送文件的id可以唯一确定本地的文件
- ::SetProp( this->m_list.GetSafeHwnd( ) , sf.filename , file );
- }break;
- //接收文件
- case PSendFileTAG :
- {
- PSendFile sf;
- sf.parseData( buffer , size );
- CString filename;
- filename.Format( "user : %s , file_id : %d" , sf.user_name , sf.file_id );
- CFile * file = ( CFile * )::GetProp( this->m_list.GetSafeHwnd( ) , filename );
- if( file )
- {
- LVFINDINFO li;
- li.flags = LVFI_STRING | LVFI_PARAM ;
- li.psz = file->GetFilePath( );
- li.lParam = sf.file_id;
- int index = this->m_list.FindItem( &li );
- if( index != -1 && sf.fbuffer && sf.fsize )
- {
- file->Write( sf.fbuffer , sf.fsize );
- filename.Format( "%d个字节" , ::atoi( this->m_list.GetItemText( index , 3 ) ) + sf.fsize );
- this->m_list.SetItemText( index , 3 , filename );
- }
- else
- {
- ::RemoveProp( this->m_list.GetSafeHwnd( ) , filename );
- SHFILEINFO shFinfo;
- ::SHGetFileInfo( file->GetFilePath( ) , 0 , &shFinfo , sizeof( shFinfo ) , SHGFI_SYSICONINDEX | SHGFI_SMALLICON );
- this->m_list.SetItem( index , 0 , LVIF_IMAGE | LVIF_PARAM , NULL , shFinfo.iIcon , 0 , 0 , 0 );
- file->Close( );
- delete file;
- }
- }
- }break;
- }
- }
- void CFileView::UpdateFileName( CString & filename )
- {
- ::CreateDirectory( theApp.appName + "接收的文件" , NULL );
- filename = theApp.appName + "接收的文件\" + filename;
- CFileFind find;
- int i = 0;
- CString s , f = filename;
- while( find.FindFile( f ) )
- {
- s.Format( "(%d)" , i ++ );
- f = filename;
- f.Insert( f.ReverseFind( '.' ) , s );
- }
- filename = f;
- }