⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpdhlp.cpp

📁 很好用的ftp源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************\
    FILE: ftpdhlp.cpp
    
    DESCRIPTION:
        Replace strings in a dialog template with attributes about an FTP
    item (ftp server, ftp dir, or ftp file).
\*****************************************************************************/

#include "priv.h"
#include "ftpurl.h"
#include "ftpdhlp.h"

#define SZ_WSPRINTFSTR_S            TEXT("%s")
#define SZ_WSPRINTFSTR_U            TEXT("%u")



class CSizeHolder
{
public:
    BOOL IsAllFolders(void) {return m_fAllFolders;};
    void FoundNonFolder(void) {m_fAllFolders = FALSE;};

    HRESULT GetError(void) {return m_hr;};
    void SetError(HRESULT hr) {m_hr = hr;};

    void AddSize(ULONGLONG ullSizeToAdd) { m_ullTotalSize += ullSizeToAdd;};
    ULONGLONG GetTotalSize(void) {return m_ullTotalSize;};

    CSizeHolder() {m_ullTotalSize = 0; m_fAllFolders = TRUE; m_hr = S_OK;};
    ~CSizeHolder() {};

private:
    BOOL    m_fAllFolders;
    HRESULT m_hr;
    ULONGLONG   m_ullTotalSize;
};



HRESULT CFtpDialogTemplate::_ReinsertDlgText(HWND hwnd, LPCVOID pv, LPCTSTR ptszFormat)
{
    TCHAR szDlgTemplate[256];
    TCHAR szFinalString[1024];            // wnsprintf maxes at 1024
    
    GetWindowText(hwnd, szDlgTemplate, ARRAYSIZE(szDlgTemplate));
    wnsprintf(szFinalString, ARRAYSIZE(szFinalString), szDlgTemplate, pv);
    
    // Are they the same?
    if (!StrCmp(szDlgTemplate, szFinalString))
        wnsprintf(szFinalString, ARRAYSIZE(szFinalString), ptszFormat, pv); // Yes
    
    SetWindowText(hwnd, szFinalString);
    return S_OK;
}


/*****************************************************************************\
    FUNCTION: _ReplaceIcon
    
    DESCRIPTION:
\*****************************************************************************/
HRESULT CFtpDialogTemplate::_ReplaceIcon(HWND hwnd, HICON hicon)
{
    if (hicon)
    {
        hicon = (HICON)SendMessage(hwnd, STM_SETICON, (WPARAM)hicon, 0L);
        if (hicon)
            DestroyIcon(hicon);
    }
    return S_OK;
}

/*****************************************************************************\
    FUNCTION: _InitIcon
    
    DESCRIPTION:
        _HACKHACK_  We go straight to CFtpIcon to get the pxi
    instead of going through CFtpFolder.  Same effect, but
    saves some memory allocations.  What's more important,
    we don't necessarily have a psf to play with, so we really
    have no choice.

    Yes, it's gross.
\*****************************************************************************/
HRESULT CFtpDialogTemplate::_InitIcon(HWND hwnd, CFtpFolder * pff, CFtpPidlList * pflHfpl)
{
    IExtractIcon * pxi;
    HRESULT hr;
    
    if (pflHfpl && pflHfpl->GetCount() == 1)
    {
        SHFILEINFO sfi;
        hr = FtpPidl_GetFileInfo(pflHfpl->GetPidl(0), &sfi, SHGFI_ICON | SHGFI_LARGEICON);
        if (SUCCEEDED(hr))
            hr = _ReplaceIcon(hwnd, sfi.hIcon);
    }
    else
    {
        hr = CFtpIcon_Create(pff, pflHfpl, IID_IExtractIcon, (LPVOID *)&pxi);
        if (EVAL(SUCCEEDED(hr)))
        {
            TCHAR szPath[MAX_PATH];
            int i;
            UINT ui;
            
            hr = pxi->GetIconLocation(0, szPath, ARRAYSIZE(szPath), &i, &ui);
            if (EVAL(SUCCEEDED(hr)))
            {
                CHAR szPathAnsi[MAX_PATH];
                
                SHTCharToAnsi(szPath, szPathAnsi, ARRAYSIZE(szPathAnsi));
                hr = _ReplaceIcon(hwnd, ExtractIconA(g_hinst, szPathAnsi, i));
            }
            
            ASSERT(pxi);
            pxi->Release();
        }
    }
    
    return hr;
}


void GetItemName(CFtpFolder * pff, CFtpPidlList * pflHfpl, LPWSTR pwzName, DWORD cchSize)
{
    // Are multiple items selected?
    if (1 < pflHfpl->GetCount())
        LoadString(HINST_THISDLL, IDS_SEVERAL_SELECTED, pwzName, cchSize);
    else
    {
        LPCITEMIDLIST pidl;
    
        if (0 == pflHfpl->GetCount())
            pidl = FtpID_GetLastIDReferense(pff->GetPrivatePidlReference());
        else
            pidl = FtpID_GetLastIDReferense(pflHfpl->GetPidl(0));

        if (EVAL(pidl))
            FtpPidl_GetDisplayName(pidl, pwzName, cchSize);
    }
}


BOOL CanEditName(CFtpFolder * pff, CFtpPidlList * pflHfpl)
{
    int nNumItems = pflHfpl->GetCount();
    BOOL fCanRename = TRUE;

    // we can edit except for multiply selected items
    if (2 <= nNumItems)
        fCanRename = FALSE;
    else
    {
        // If they chose the background properties for a server,
        // we won't let the change the server name.
        if (0 == nNumItems)
        {
            LPCITEMIDLIST pidlFolder = pff->GetPrivatePidlReference();

            if (pidlFolder && (ILIsEmpty(pidlFolder) || (ILIsEmpty(_ILNext(pidlFolder)))))
            {
                fCanRename = FALSE;
            }
        }
        else if (1 == nNumItems)
        {
            // Now I'm worried that pflHfpl->GetPidl(0) is a PIDL pointing to
            // an FTP Server.
            LPCITEMIDLIST pidl = pflHfpl->GetPidl(0);

            if (pidl && !ILIsEmpty(pidl) &&
                FtpID_IsServerItemID(pidl) && ILIsEmpty(_ILNext(pidl)))
            {
                fCanRename = FALSE;
            }
        }
    }

    return fCanRename;
}


/*****************************************************************************\
    FUNCTION: _InitName
    
    DESCRIPTION:
        Get the name of the object in the pflHfpl.  If there is more than one
    thing, use ellipses.
\*****************************************************************************/
HRESULT CFtpDialogTemplate::_InitName(HWND hwnd, CFtpFolder * pff, CFtpPidlList * pflHfpl)
{
    HRESULT hr = S_OK;
    WCHAR wzName[MAX_PATH];

    GetItemName(pff, pflHfpl, wzName, ARRAYSIZE(wzName));
    hr = _ReinsertDlgText(hwnd, wzName, SZ_WSPRINTFSTR_S);
    // We only use the static filename when more than one item is selected
    // because that is the case that we can't do a rename.  Are there
    // multiple items selected?
    if (m_fEditable && CanEditName(pff, pflHfpl))
    {
        // Hide because we will use IDC_FILENAME_EDITABLE instead.
        ShowEnableWindow(hwnd, FALSE);
    }

    return hr;
}


/*****************************************************************************\
    FUNCTION: _InitNameEditable
    
    DESCRIPTION:
        Get the name of the object in the pflHfpl.  If there is more than one
    thing, use ellipses.
\*****************************************************************************/
HRESULT CFtpDialogTemplate::_InitNameEditable(HWND hwnd, CFtpFolder * pff, CFtpPidlList * pflHfpl)
{
    HRESULT hr = S_OK;
    TCHAR szName[MAX_PATH];

    GetItemName(pff, pflHfpl, szName, ARRAYSIZE(szName));
    hr = _ReinsertDlgText(hwnd, szName, SZ_WSPRINTFSTR_S);

    // We only use the static filename when more than one item is selected
    // because that is the case that we can't do a rename.  Are there
    // multiple items selected?
    if (!m_fEditable || !CanEditName(pff, pflHfpl))
    {
        // Hide because we will use IDC_FILENAME_EDITABLE instead.
        ShowEnableWindow(hwnd, FALSE);
    }

    return hr;
}


void GetNameFromPidlList(CFtpFolder * pff, CFtpPidlList * pflHfpl, LPWSTR pwzName, DWORD cchSize)
{
    LPCITEMIDLIST pidl;
    
    if (0 == pflHfpl->GetCount())
        pidl = FtpID_GetLastIDReferense(pff->GetPrivatePidlReference());
    else
        pidl = FtpID_GetLastIDReferense(pflHfpl->GetPidl(0));

    if (EVAL(pidl))
        StrCpyNW(pwzName, FtpPidl_GetLastItemDisplayName(pidl), cchSize);
}


/*****************************************************************************\
    FUNCTION: _InitType
    
    DESCRIPTION:
        Get the type of the pidls identified by pflHfpl.
\*****************************************************************************/
HRESULT CFtpDialogTemplate::_InitType(HWND hwnd, CFtpFolder * pff, CFtpPidlList * pflHfpl)
{
    TCHAR szType[MAX_URL_STRING];
    
    szType[0] = 0;
    switch (pflHfpl->GetCount())
    {
    case 0:
        {
            // Find out if it's a folder or an ftp server root.
            LPCITEMIDLIST pidl = FtpID_GetLastIDReferense(pff->GetPrivatePidlReference());
            if (EVAL(pidl))
                LoadString(HINST_THISDLL, (FtpID_IsServerItemID(pidl) ? IDS_ITEMTYPE_SERVER : IDS_ITEMTYPE_FOLDER), szType, ARRAYSIZE(szType));
        }
        break;
        
    case 1:
        // Just one item is selected, so get it's type.
        FtpPidl_GetFileType(pflHfpl->GetPidl(0), szType, ARRAYSIZE(szType));
        break;
        
    default:
        // Display "Several Selected" because they can span 1 type.
        LoadString(HINST_THISDLL, IDS_SEVERAL_SELECTED, szType, ARRAYSIZE(szType));
        break;
    }
    
    return _ReinsertDlgText(hwnd, szType, SZ_WSPRINTFSTR_S);
}


/*****************************************************************************\
    FUNCTION: _InitLocation
    
    DESCRIPTION:
        Get the name of the folder identified by pidl.
\*****************************************************************************/
HRESULT CFtpDialogTemplate::_InitLocation(HWND hwnd, CFtpFolder * pff, CFtpPidlList * pidlList)
{
    HRESULT hr = E_FAIL;
    TCHAR szUrl[MAX_PATH];
    LPITEMIDLIST pidl = GetPidlFromFtpFolderAndPidlList(pff, pidlList);

    ASSERT(pidlList && pff);
    if (EVAL(pidl))
    {
        // If more than one items are selected, then we only want to
        // show the common location.
        if (1 < pidlList->GetCount())
            ILRemoveLastID(pidl);
        hr = UrlCreateFromPidl(pidl, SHGDN_FORADDRESSBAR, szUrl, ARRAYSIZE(szUrl), 0, TRUE);
        if (SUCCEEDED(hr))
        {
            hr = _ReinsertDlgText(hwnd, szUrl, SZ_WSPRINTFSTR_S);
        }
        ILFree(pidl);
    }

    return hr;
}


/*****************************************************************************\
    FUNCTION: _InitSizeTally
    
    DESCRIPTION:
        Total up the size of each file referred to in the pidl.
\*****************************************************************************/
int CFtpDialogTemplate::_InitSizeTally(LPVOID pvPidl, LPVOID pvSizeHolder)
{
    BOOL fSuccess = TRUE;
    LPCITEMIDLIST pidl = (LPCITEMIDLIST) pvPidl;
    CSizeHolder * pSizeHolder = (CSizeHolder *) pvSizeHolder;

    // Did we get a valid size and is pSizeHolder still valid?
    if (SUCCEEDED(pSizeHolder->GetError()))
    {
        // Yes, so keep accumulating if it's a file.
        if (!FtpID_IsServerItemID(pidl) && !FtpItemID_IsDirectory(pidl, FALSE))
        {
            ULARGE_INTEGER uliPidlFileSize;

⌨️ 快捷键说明

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