📄 commands.cpp
字号:
// Commands.cpp : implementation file
//
#include "stdafx.h"
#include "ProjectZip.h"
#include "Commands.h"
#include "optionsdlg.h"
#include "selectfiledlg.h"
#include "..\shared\folderdialog.h"
#include "..\zipunzip\zipper.h"
#include "..\zipunzip\unzipper.h"
#include <COMDEF.H>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCommands
CCommands::CCommands()
{
m_pApplication = NULL;
m_pApplicationEventsObj = NULL;
m_pDebuggerEventsObj = NULL;
}
CCommands::~CCommands()
{
ASSERT (m_pApplication != NULL);
m_pApplication->Release();
}
void CCommands::SetApplicationObject(IApplication* pApplication)
{
// This function assumes pApplication has already been AddRef'd
// for us, which CDSAddIn did in its QueryInterface call
// just before it called us.
m_pApplication = pApplication;
// Create Application event handlers
XApplicationEventsObj::CreateInstance(&m_pApplicationEventsObj);
m_pApplicationEventsObj->AddRef();
m_pApplicationEventsObj->Connect(m_pApplication);
m_pApplicationEventsObj->m_pCommands = this;
// Create Debugger event handler
CComPtr<IDispatch> pDebugger;
if (SUCCEEDED(m_pApplication->get_Debugger(&pDebugger))
&& pDebugger != NULL)
{
XDebuggerEventsObj::CreateInstance(&m_pDebuggerEventsObj);
m_pDebuggerEventsObj->AddRef();
m_pDebuggerEventsObj->Connect(pDebugger);
m_pDebuggerEventsObj->m_pCommands = this;
}
}
void CCommands::UnadviseFromEvents()
{
ASSERT (m_pApplicationEventsObj != NULL);
m_pApplicationEventsObj->Disconnect(m_pApplication);
m_pApplicationEventsObj->Release();
m_pApplicationEventsObj = NULL;
if (m_pDebuggerEventsObj != NULL)
{
// Since we were able to connect to the Debugger events, we
// should be able to access the Debugger object again to
// unadvise from its events (thus the VERIFY_OK below--see stdafx.h).
CComPtr<IDispatch> pDebugger;
VERIFY_OK(m_pApplication->get_Debugger(&pDebugger));
ASSERT (pDebugger != NULL);
m_pDebuggerEventsObj->Disconnect(pDebugger);
m_pDebuggerEventsObj->Release();
m_pDebuggerEventsObj = NULL;
}
}
/////////////////////////////////////////////////////////////////////////////
// Event handlers
// TODO: Fill out the implementation for those events you wish handle
// Use m_pCommands->GetApplicationObject() to access the Developer
// Studio Application object
// Application events
HRESULT CCommands::XApplicationEvents::BeforeBuildStart()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::BuildFinish(long nNumErrors, long nNumWarnings)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::BeforeApplicationShutDown()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::DocumentOpen(IDispatch* theDocument)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::BeforeDocumentClose(IDispatch* theDocument)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::DocumentSave(IDispatch* theDocument)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::NewDocument(IDispatch* theDocument)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::WindowActivate(IDispatch* theWindow)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::WindowDeactivate(IDispatch* theWindow)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::WorkspaceOpen()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::WorkspaceClose()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
HRESULT CCommands::XApplicationEvents::NewWorkspace()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
// Debugger event
HRESULT CCommands::XDebuggerEvents::BreakpointHit(IDispatch* pBreakpoint)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
// CCommands methods
enum { PZIP_OK, PZIP_CREATEFILE, PZIP_CANCELLED };
STDMETHODIMP CCommands::PZQuickZipCommandMethod()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
CWaitCursor cursor;
ZipProject();
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
return S_OK;
}
CString CCommands::GetProjectPath()
{
CString sPath;
IGenericProject* pProject = NULL;
if (SUCCEEDED(m_pApplication->get_ActiveProject((IDispatch**)&pProject)))
{
BSTR bsName;
if (SUCCEEDED(pProject->get_FullName(&bsName)))
{
sPath = (LPCTSTR)_bstr_t(bsName);
}
pProject->Release();
}
return sPath;
}
STDMETHODIMP CCommands::PZOptionsZipCommandMethod()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
COptionsDlg dialog(!GetProjectPath().IsEmpty());
if (dialog.DoModal() == IDOK)
return PZQuickZipCommandMethod();
return S_OK;
}
void CCommands::ZipProject()
{
CString sProjectPath = GetProjectPath();
if (sProjectPath.IsEmpty())
{
MessageBox(NULL, "ProjectZip could not determine the active project.\n\nPlease ensure that a project is loaded and is active.", "ProjectZip", MB_OK);
return;
}
// else save all
m_pApplication->ExecuteCommand(L"FileSaveAll");
m_pApplication->ExecuteCommand(L"WorkspaceSave");
CString sZipPath;
CStringArray aFailures;
int nRes = ZipProject(sProjectPath, sZipPath, aFailures);
CString sMessage;
switch (nRes)
{
case PZIP_OK:
if (aFailures.GetSize())
{
int nFile = aFailures.GetSize();
CString sFiles;
while (nFile--)
{
sFiles += '\n';
sFiles += aFailures[nFile];
}
sMessage.Format("ProjectZip has successfully zipped the active project to '%s', \nbut the following files could not be added:\n%s", sZipPath, sFiles);
}
else
sMessage.Format("ProjectZip has successfully zipped the active project to '%s'", sZipPath);
break;
case PZIP_CREATEFILE:
sMessage.Format("ProjectZip was unable to create the zip file '%s'", sZipPath);
break;
case PZIP_CANCELLED:
break;
}
if (!sMessage.IsEmpty())
MessageBox(NULL, sMessage, "ProjectZip", MB_OK);
// open the zip
if (nRes == PZIP_OK && COptionsDlg::GetOpenZip())
{
ShellExecute(NULL, NULL, sZipPath, NULL, NULL, SW_SHOW);
}
}
int CCommands::ZipProject(CString sProjectFile, CString& sZipPath, CStringArray& aFailures)
{
// get file list
CStringArray aFiles;
BuildProjectFileList(sProjectFile, aFiles);
PrepareFilePaths(sProjectFile, aFiles, aFailures);
// get file root
CString sRootPath = GetFileRoot(sProjectFile, aFiles);
// get zip path
char szDrive[_MAX_DRIVE], szFolder[MAX_PATH], szName[_MAX_FNAME];
_splitpath(sProjectFile, szDrive, szFolder, szName, NULL);
_makepath(sZipPath.GetBuffer(MAX_PATH), szDrive, szFolder, szName, "zip");
sZipPath.ReleaseBuffer();
if (COptionsDlg::GetShowSaveAs())
{
CFileDialog dialog(FALSE, "zip", sZipPath, 0, "Zip Files (*.zip)|*.zip||");
dialog.m_ofn.lpstrTitle = "ProjectZip - Save Zip File";
if (dialog.DoModal() != IDOK)
return PZIP_CANCELLED;
sZipPath = dialog.GetPathName();
}
// check before overwriting
if (COptionsDlg::GetPromptOverwrite() && (GetFileAttributes(sZipPath) != 0xffffffff))
{
CString sMessage;
sMessage.Format("The file '%s' already exists.\n\nAre you sure that you want to overwrite it?", sZipPath);
if (IDNO == MessageBox(NULL, sMessage, "ProjectZip", MB_YESNO))
return PZIP_CANCELLED;
// else delete the file first
DeleteFile(sZipPath);
}
CZipper zip;
if (!zip.OpenZip(sZipPath, sRootPath))
return PZIP_CREATEFILE;
// else add the files
int nFile = aFiles.GetSize();
while (nFile--)
{
CString sFile = aFiles[nFile];
if (!zip.AddFileToZip(sFile))
aFailures.Add(sFile);
}
return PZIP_OK;
}
int CCommands::BuildProjectFileList(CString sProjectFile, CStringArray& aFiles)
{
aFiles.RemoveAll();
// add the dsp before we forget!
aFiles.Add(sProjectFile);
// and the dsw if requested
if (COptionsDlg::GetIncludeDsw())
{
char szDrive[_MAX_DRIVE], szFolder[MAX_PATH], szName[_MAX_FNAME];
_splitpath(sProjectFile, szDrive, szFolder, szName, NULL);
char szDsw[MAX_PATH];
_makepath(szDsw, szDrive, szFolder, szName, "dsw");
DWORD dwAttrib = GetFileAttributes(szDsw);
if (dwAttrib != 0xffffffff && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY))
aFiles.Add(szDsw);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -