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

📄 listcontrol.cpp

📁 在SYMBIAN平台上用OPEN C实现的一个FTP客户程序
💻 CPP
字号:
/***************************************************************************/
/*                                                                         */
/* ListControl.cpp - Part of Open C FTP Client                             */
/*                                                                         */
/* Copyright (C) 2007 Nokia Corporation                                    */
/*                                                                         */
/* Open C FTP Client is based on ftplib and qftp by Thomas Pfau.           */
/*                                                                         */
/* Copyright (C) 1996-2001 Thomas Pfau, pfau@eclipse.net                   */
/* 1407 Thomas Ave, North Brunswick, NJ, 08902                             */
/*                                                                         */
/* This program is free software; you can redistribute it and/or           */
/* modify it under the terms of the GNU Library 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       */
/* Library General Public License for more details.                        */
/*                                                                         */
/* You should have received a copy of the GNU Library 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.                                             */
/*                                                                         */
/***************************************************************************/
 
/*
 * ============================================================================
 *  Name     : ListControl from ListControl.cpp
 *  Part of  : Open C FTP Client
 *  Created  : 03/12/2007 by Forum Nokia
 *  Version  : 1.0
 * ============================================================================
*/

#include <aknglobalnote.h>
#include <aknlists.h>           // listboxes
#include <aknnotewrappers.h>
#include <aknview.h>
#include <aknsfld.h>			// search field
#include <badesca.h>
#include <barsread.h>           // TResourceReader
#include <eikclbd.h>            // CColumnListBoxData
#include <eikapp.h>
#include <f32file.h>
#include <string.h>
#include <stringloader.h>

#include "ListControl.h"
#include "OpenCFTPClient.hrh"
#include <OpenCFTPClient.rsg>

extern "C" {
#include "qftp.h"
}

CEikTextListBox* listBox;

TBuf8<maxSize> ftpaddr;
TBuf8<maxSize> ftpuser;
TBuf8<maxSize> ftppass;

// ===========================================================

CListControl::CListControl(CAknView* aParent) : iParent(aParent)
{

}

// ===========================================================

void CListControl::ConstructL(const TRect& aRect)
{
    CreateWindowL();

    listBox = new (ELeave) CAknSingleNumberStyleListBox;
    listBox->ConstructL(this, EAknListBoxSelectionList);
    listBox->SetContainerWindowL(*this);

    _LIT(KDefaultText, "Open C FTP Client Example");
    listBox->View()->SetListEmptyTextL(KDefaultText);

    listBox->CreateScrollBarFrameL(ETrue);
    listBox->ScrollBarFrame()->SetScrollBarVisibilityL(
    	CEikScrollBarFrame::EAuto,
        CEikScrollBarFrame::EAuto);


    const TInt maxCharacters = 40;
    iSearchField = CAknSearchField::NewL(*this, CAknSearchField::EPopup, NULL, maxCharacters);
    iSearchField->MakeVisible(EFalse);

    CAknFilteredTextListBoxModel* model; 
    model = static_cast<CAknFilteredTextListBoxModel*>(listBox->Model());

    model->CreateFilterL(listBox, iSearchField);

    SetRect(aRect);

    ActivateL();

    ShowNoteL(R_WELCOME_TEXT);
}

// ===========================================================

CListControl::~CListControl()
{
    if(listBox)
    {
    	delete listBox;
    }
    if(iSearchField)
    {
    	delete iSearchField;
    }
}

// ===========================================================

void CListControl::SizeChanged()
{
    // layout both controls here
    AknFind::HandlePopupFindSizeChanged(
        this, static_cast<CAknColumnListBox*>(listBox), iSearchField);
}

// ===========================================================

TInt CListControl::CountComponentControls() const
{
    // listbox and the search field
    const TInt controlCount = 2;
    return controlCount;
}

// ===========================================================

CCoeControl* CListControl::ComponentControl(TInt aIndex) const
{
    switch ( aIndex )
    {
        case 0:
            return listBox;
        case 1:
        	return iSearchField;
        default:
            return NULL;
    }
}

// ===========================================================

TKeyResponse CListControl::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
{
	TKeyResponse keyResponse = EKeyWasNotConsumed;
    if (aType == EEventKey)
    {
        switch (aKeyEvent.iCode)
        {
            case EKeyOK:
                ChooseActionL();
                break;

            case EKeyUpArrow:
            case EKeyDownArrow:
            	keyResponse = listBox->OfferKeyEventL(aKeyEvent, aType);
            	break;

            default:
               	keyResponse = iSearchField->OfferKeyEventL(aKeyEvent, aType);
        }
    }
    this->DrawNow();
    return keyResponse;
}

// ===========================================================

void CListControl::Draw(const TRect& /*aRect*/) const
{
    CWindowGc& gc = SystemGc();
    gc.SetBrushColor(KRgbWhite);
    gc.Clear(Rect());
}

// ---------------------------------------------------------
// CListControl::HandleResourceChange()
// Called by framework when resource is changed.
// ---------------------------------------------------------
//
void CListControl::HandleResourceChange(TInt aType)
{
    CCoeControl::HandleResourceChange(aType);

    if ( aType==KEikDynamicLayoutVariantSwitch )
    {
        TRect rect;
        AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
        SetRect(rect);
    }
}

//	
// This implementation is according to /bin/ls format, works with most ftp servers but not all!	
//
void CListControl::UpdateListL(TDes& aRetVal)
{
    TChar objectType;
    TChar file = '-';
    TChar directory = 'd';
    TChar alias = 'l';
    _LIT(KDefaultSpaceToken, " ");
    _LIT(KDefaultLineChange, "\n");
    TBuf<100> item;
    TInt index = 0;
    TInt i;
    TInt isLastLine = 0;

    aRetVal.TrimAll();
    aRetVal.Length();

    MDesCArray* textArray = listBox->Model()->ItemTextArray();
    CDesCArray* array = static_cast<CDesCArray*>(textArray);
    array->Reset();
    listBox->Reset();

    do
    {	
        objectType = TChar(aRetVal[0]);
        const TInt spaceCount = 8; /* The number of spaces between items on the ftp file list line */
        for (i=0;i<spaceCount;i++)
        {
            index = aRetVal.Find(KDefaultSpaceToken);
            aRetVal.Delete(0,index+1);
            aRetVal.Length();
        }

        index = aRetVal.Find(KDefaultLineChange);

        if (index == -1 && isLastLine == 0)
        {
            isLastLine = 1;
            index = aRetVal.Length();
        }

        if (index > 0 || isLastLine == 1)
        {
            HBufC* objectName = HBufC::NewLC(index);
            TPtr tmp = objectName->Des();
				
            if (isLastLine == 1)
            {
                tmp.SetLength(aRetVal.Length());
                tmp = aRetVal.Left(aRetVal.Length());
            }
            else
            {
                tmp.SetLength(index);
                tmp = aRetVal.Left(index);
            }

            if (objectType == file)
            {
            	objectType = 'F';
            }
            else if (objectType == directory)
            {
            	objectType = 'D';
            }
            else if (objectType == alias)
            {
            	objectType = 'L';
            }

            _LIT(KItemFormat, "%c\t%S\t\t");
            item.Format(KItemFormat,objectType,objectName);
            array->AppendL(item);
            listBox->HandleItemAdditionL();
            listBox->SetCurrentItemIndexAndDraw(array->Count()-1);

			CleanupStack::PopAndDestroy(objectName);
				
            aRetVal.Delete(0,index+1);
			
            if (isLastLine == 1)
            {
                aRetVal.Delete(0,aRetVal.Length());
            }
        }
    } while (aRetVal.Length() > 1);
}

void CListControl::FtpCommandHandlerL(int argcount, char *argcommand[])
{
    char *returnValue;

	returnValue = qftp_main(argcount, argcommand);
    if (returnValue)
    {
        ShowNoteL(R_COMPLETED_TEXT);
    }
    else
    {
        ShowNoteL(R_ERROR_TEXT);
    }
}

void CListControl::DownloadFileListL(int argcount, char *argcommand[])
{
    char *returnValue;

	returnValue = qftp_main(argcount, argcommand);

    if (returnValue)
    {
        TInt j = strlen(returnValue);
        HBufC* destination = HBufC::NewLC(j);
        TPtr tmp = destination->Des();

        tmp.SetLength(j);

        if (returnValue)
        {
            TInt i = 0;
            while (i < j)
            {
                tmp[i]=TChar(returnValue[i]);
                i++;
            }
        }

        UpdateListL(tmp);

        CleanupStack::PopAndDestroy(destination);
    }

}

void CListControl::DownloadSelectedFileL()
{
    const TInt argumentCount = 8;
    
    TInt currentItem = listBox->CurrentItemIndex();

    if (currentItem >= 0)
    {
        CTextListBoxModel* model = listBox->Model();
   	    TPtrC16 selectedItem = model->ItemText(currentItem);

        /* Remove unneeded parts of line according to CAknSingleNumberStyleListBox */
        const TInt removeLineHeader = 2;
        const TInt removeLineFooter = 4;
        TPtrC tmp = selectedItem.Mid(removeLineHeader,selectedItem.Length()-removeLineFooter);    
	
        TBuf8<maxSize> aBuffer;
        aBuffer.Copy(ftpaddr);
        TBuf8<maxSize> uBuffer;
        uBuffer.Copy(ftpuser);
        TBuf8<maxSize> pBuffer;
        pBuffer.Copy(ftppass);
        TBuf8<maxSize> nBuffer;
        nBuffer.Fill('\0');
        nBuffer.Copy(tmp);
        nBuffer.Append('\0');

        char *command[] = {"get",
                            (char *)aBuffer.Ptr(),
                            "-l",
                	        (char *)uBuffer.Ptr(),
                            "-p",
                            (char *)pBuffer.Ptr(),
                            (char *)nBuffer.Ptr(),
                            "\n"};

        TRAPD(error, FtpCommandHandlerL(argumentCount, command));

        if (error == -1)
        {
            ShowNoteL(R_ERROR_TEXT);
        }

        RFs fs;
        fs.Connect();

        CFileMan* fileMan = CFileMan::NewL(fs);
        CleanupStack::PushL(fileMan);
        _LIT(KDefaultLocation, "C:\\Data\\Others\\");
        error = fileMan->Move(tmp, KDefaultLocation);
        CleanupStack::PopAndDestroy(fileMan);

        fs.Close();

        if (error != KErrNone)
        {
            ShowNoteL(R_ERROR_TEXT);
        }
    }
}

void CListControl::ChooseActionL()
{
    TInt currentItem = listBox->CurrentItemIndex();
    TChar file = 'F';
    TChar directory = 'D';
    TChar alias = 'L';

    if (currentItem >= 0)
    {
        CTextListBoxModel* model = listBox->Model();
   	    TPtrC16 selectedItem = model->ItemText(currentItem);

		if (selectedItem[0] == file)
		{
			DownloadSelectedFileL();
		}
		else if (selectedItem[0] == alias)
		{
			ShowNoteL(R_ERROR_LINK_TEXT);
		}
    }
}

void CListControl::ShowNoteL(TInt note)
{
    HBufC* textResource = StringLoader::LoadLC(note);
    CAknInformationNote* informationNote;
    informationNote = new ( ELeave ) CAknInformationNote;
    informationNote->ExecuteLD( *textResource);
    CleanupStack::PopAndDestroy( textResource );	
}

void CListControl::SetConnectionOptions(TBuf8<maxSize> addr, TBuf8<maxSize> user, TBuf8<maxSize> pass)
{
	ftpaddr = addr;
	ftpuser = user;
	ftppass = pass;
}

⌨️ 快捷键说明

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