chxavmisc.cpp

来自「著名的 helix realplayer 基于手机 symbian 系统的 播放」· C++ 代码 · 共 903 行 · 第 1/2 页

CPP
903
字号
/*****************************************************************************
 * chxavmisc.cpp
 * -------------
 *
 * Synopsis:
 * Misc helpers for s60 player only
 *
 *
 *
 * Target:
 * Symbian OS
 *
 *
 * (c) 1995-2003 RealNetworks, Inc. Patents pending. All rights reserved.
 *
 *****************************************************************************/

// Symbian includes...
#include <coeutils.h>
#include <aknenv.h>
#include <limits.h>
#include <apgwgnam.h>
#include <apsettingshandlerui.h>
#include <akntabgrp.h>
#include <akniconarray.h> 
#include <aputils.h> 
#include <apgcli.h>
#include <commdb.h>
#include <avkon.rsg>

// Helix includes...
#include "hxassert.h"
#include "hxstring.h"

// Includes from this project...
#include "chxavutil.h"
#include "chxavmisc.h"
#include "chxavfileutil.h"
#include "chxavinfolist.h"
#include "chxavmessagedialog.h"
#include "chxavcleanstring.h"
#include "chxavcleanupstack.h"
#include "chxavnamedisplaytrait.h"
#include "chxavurllist.h"

#include "realplayer.rsg"
#include "realplayer.hrh"

#include "comptr.h"
#include "ihxpckts.h"
#include "chxavplayer.h"
#include "hxdebug_hxapi.h"
#include "hxsym_leaveutil.h"
#include "hxapihelp.h"


namespace
{



////////////////////////////////////////
// icon indexes:
//
// local media (.rm, .ra, etc.)
// remote media (url)
// list  (.ram)
// broken local file
// 
enum ClipTypeIconIndex
{
    IconLocal,
    IconRemote,
    IconList,
    IconLocalBroken
};

ClipTypeIconIndex GetIconIndex(const TDesC& path, bool bPathIsLocal)
{
    ClipTypeIconIndex idxIcon = ClipTypeIconIndex(-1);

    if( bPathIsLocal )
    {
        CHXAvFile::FileType type = CHXAvFile::GetFileType(path);
        if (CHXAvFile::ftRam == type)
        {
            idxIcon = IconList;
        }
        else
        {
            // for now, use local icon for everything else
            idxIcon = IconLocal;
        }

        if (!ConeUtils::FileExists(path))
	{
	    // file no longer exists
	    idxIcon = IconLocalBroken;
	}
    }
    else
    {
        idxIcon = IconRemote;
    }

    return idxIcon;
}




} // end locals

namespace CHXAvMisc
{


////////////////////////////////////////////////////////////
// allocate descriptive filesize text, e.g., "4.9 kbytes"
HBufC* AllocFileSizeDescL(TUint cbFile)
{
    HBufC* pbuff = 0;
    if( cbFile < CHXAvUtil::k_bytesPerKilobyte )
    {
        CHXAvCleanString text(R_FILEVIEW_BYTES_FORMAT, TInt(cbFile));
	pbuff = text().AllocL();
    }
    else
    {
        HX_ASSERT((cbFile / k_bytesPerKilobyte) <= INT_MAX); // next cast
        CHXAvCleanString text(R_FILEVIEW_KB_FORMAT, TInt(cbFile / k_bytesPerKilobyte));
        pbuff = text().AllocL();
    }
    return pbuff;
}

//
// graphic list item text format is:
//
// [{icon index}]\t{main text}[\t{subtext}][\t{idxIconD}]
//

HBufC* AllocGrListItemL(const TDesC& mainText, const TDesC& subText, TInt idxIcon)
{
    HX_ASSERT(mainText.Length() > 0);
    const TUint cchExtraSpace = 10; // enough for \t and icon indexes
    TUint cchItemText = cchExtraSpace + mainText.Length() + subText.Length();
    
    HBufC* pBuf = HBufC::NewL(cchItemText);
    AUTO_PUSH_POP(pBuf); // out
    TPtr ptr = pBuf->Des();

    // -1 means "no icon"
    if( idxIcon != -1 )
    {
        ptr.AppendNum(idxIcon);
    }

    // main text
    ptr.Append(KListColumnDelimiter);
    HBufC* pFixedMainText = CHXAvMisc::AllocDisplayFriendlyStringL(mainText);
    AUTO_PUSH_POP_DEL(pFixedMainText);
    ptr.Append(*pFixedMainText);

    // sub text
    if( subText.Length() > 0 )
    {
        ptr.Append(KListColumnDelimiter);
        HBufC* pFixedSubText = CHXAvMisc::AllocDisplayFriendlyStringL(subText);
        AUTO_PUSH_POP_DEL(pFixedSubText);
        ptr.Append(*pFixedSubText);
    }
  
    return pBuf;

}



////////////////////////////////////////
//
// graphic list item text must be:
//
// [icon index]\tmain text[\t subtext][\tidxIconD][\tidxIconD]
//
HBufC* AllocGrListItemL(ListType type, 
                                  TInt idxIcon, 
                                  const TDesC& mainText, 
                                  const TDesC& subText)
{

    _LIT(KSingle, "%d\t%S");
    _LIT(KSingleNoIcon, "%d\t%S");
    _LIT(KDouble, "%d\t%S\t%S");
    _LIT(KDoubleNoIcon, "\t%S\t%S");

    HX_ASSERT(mainText.Length() > 0);
    const TUint cchIndexSpace = 20;
    TUint cchItemText = cchIndexSpace + mainText.Length() + subText.Length();
    
    HBufC* pBuf = HBufC::NewL(cchItemText);
    TPtr ptr = pBuf->Des();

    switch (type)
    {
    case Double:
        ptr.Format(KDouble, idxIcon, &mainText, &subText);
        break;
    case DoubleNoIcon:
        ptr.Format(KDoubleNoIcon, &mainText, &subText);
        break;
    case Single:
        ptr.Format(KSingle, idxIcon, &mainText);
        break;
    case SingleNoIcon:
        ptr.Format(KSingleNoIcon, &mainText);
        break;
    default:
        HX_ASSERT(false);
        break;
    }

    return pBuf;

}




////////////////////////////////////////////
// show the access point settings page
//
// wapApId = id of item that should be selected by
// default (i.e., the currently set access point)
//	
TInt RunAccessPointSettingPageL(TInt wapApId)
{
    const bool bStartWithSel            = true; // if false (edit only), other params ignored
    const TSelectionListType listType   = EApSettingsSelListIsPopUp;
    const TSelectionMenuType menuType   = EApSettingsSelMenuSelectNormal;

    const TInt ispFilter                = KEApIspTypeInternetOnly | KEApIspTypeInternetAndWAP;
    const TInt bearerType               = EApBearerTypeAll;
    const TInt sortType                 = KEApSortNameAscending;

    // access point settings ui handler; displays setting page ui
    CApSettingsHandler* pSettings 
        = CApSettingsHandler::NewLC(bStartWithSel, listType, menuType,
                                    ispFilter, bearerType, sortType);
    AUTO_POP_DEL(pSettings);

    TUint32 idSelected = 0;
    pSettings->RunSettingsL(wapApId, idSelected);
    return idSelected;

}





///////////////////////////////////////
// extract text chunk from delimited text (e.g., "\thello\tthere")
TPtrC
TextChunk(const TDesC& text, TInt idxChunk, TChar chDelimit)
{
    TPtrC titleOut(KNullDesC);

    TPtrC temp = text;
    TInt idxDelimit = 0;

    // skip to chunk
    while(idxChunk--)
    {
        // skip past tab delimiter
        idxDelimit = temp.Locate(chDelimit);
        if( KErrNotFound == idxDelimit )
        {
            break;
        }
        temp.Set(temp.Mid(idxDelimit + 1));
    }

    if( idxDelimit != KErrNotFound )
    {
        idxDelimit = 0;
        TInt idxDelimit = temp.Locate(chDelimit);
        if( idxDelimit == KErrNotFound )
        {
            // ok, no ending tab
            idxDelimit = temp.Length();
        }

        // collect to next delimiter or end
	titleOut.Set(temp.Left(idxDelimit));
    }
    
    return titleOut;
}


////////////////////////////////////
//
TInt LaunchAppL(RFs& fsSession, const TDesC& appPath, const TDesC& doc)
{
    // get absolute path to the app
    TFileName fullPathApp;
    TInt err = CHXAvFile::GetFullPath(fsSession, appPath, fullPathApp);
    if( KErrNone == err )
    {
        // set up the command line
        CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
        cmdLine->SetLibraryNameL(fullPathApp);
        cmdLine->SetDocumentNameL(doc);
        cmdLine->SetCommandL(EApaCommandOpen);

        // run the command line
        RApaLsSession ls;
        HXSYM_LEAVE_IF_ERR(ls.Connect());
        CleanupClosePushL(ls);
        HXSYM_LEAVE_IF_ERR(ls.StartApp(*cmdLine));
        CleanupStack::PopAndDestroy(2); // ls and cmdLine
        
    }
    return err;
}

////////////////////////////////////////////////
// iterate all app instances with the given uid
// and either end or kill
//
// note: this will not find embedded instances!
//
void ForEachAppInstanceL(TUid uid, ForEachAppInstanceCommand command)
{
    RWsSession& ws = CEikonEnv::Static()->WsSession();
    
    TInt wgId = 0;
    CApaWindowGroupName::FindByAppUid(uid, ws, wgId);
    while( wgId != KErrNotFound )
    {
        DPRINTF(SYMP_INFO, ("ForEachAppInstanceL(): found task wgid = %ld", wgId));
        TApaTask task(ws);
        task.SetWgId(wgId);

        // end this task if it is not our own (identified by thread id)
        if( task.ThreadId() != RThread().Id() )
        {
            if( command == KillApp )
            {
                DPRINTF(SYMP_INFO, ("ForEachAppInstanceL(): killing task (wgid = %ld)", wgId));
                task.KillTask();
            }
            else
            {
                DPRINTF(SYMP_INFO, ("ForEachAppInstanceL(): ending task (wgid = %ld)", wgId));
                task.EndTask();
            }
        }
        CApaWindowGroupName::FindByAppUid(uid, ws, wgId);
    }
}


////////////////////////////////////////////////
//
void LaunchAppL(TInt uid, const TDesC& arg)
{
    
    TUid uidApp = TUid::Uid(uid);

    TApaTaskList taskList(CEikonEnv::Static()->WsSession());
    TApaTask task = taskList.FindApp(uidApp);
    if (task.Exists())
    {
        // convert to 8-bit message (note that copy, append convert)
        HBufC8* pBuf = HBufC8::NewL(arg.Length());
        AUTO_PUSH_POP_DEL(pBuf);
        TPtr8 ptr = pBuf->Des();
        ptr.Copy(arg);

        // send message to the task; uid is not used
        task.SendMessage(TUid::Uid( 0 ), *pBuf);
        
        //HXSYM_LEAVE_IF_ERR(task.SwitchOpenFile(aFileName));
	//task.BringToForeground();
    }
    else 
    {
        // launch the webbrowser app
        RApaLsSession session;
        HXSYM_LEAVE_IF_ERR(session.Connect());
        TThreadId tidNewAppInstance = 0; // ignored
        session.StartDocument( arg, uidApp, tidNewAppInstance );
        session.Close();
    }

}

////////////////////////////////////////////
// form our app-specific message for BroadcastWsEventL()
HBufC8* MakeHXPlayerPrivateMessageL(const TDesC8& name)
{
    const TUint k_cbForRest = 50;
    HBufC8* pbuff = HBufC8::NewL(name.Length() + k_cbForRest);
    
    // name:tid
    _LIT8(KFormatText, "%S:0x%x");
    TThreadId tid = RThread().Id();
    TPtr8 des = pbuff->Des();
    des.AppendFormat(KFormatText, &name, tid);
    return pbuff;
}

//////////////////////////////////////////////////////
// send TWsEvent to all window groups except ours
void BroadcastWsEventL(TUint type, const HBufC8* pMsg)
{
    TWsEvent event;
    
    event.SetType(type);
    HXSYM_LEAVE_IF_FALSE(pMsg->Length() <= TWsEvent::EWsEventDataSize);
    Mem::Copy(event.EventData(), pMsg->Ptr(), pMsg->Length());

    RWsSession& ws = CEikonEnv::Static()->WsSession();

    TInt wgidThisInstance = GetThisWgId();

    CArrayFixFlat<TInt>* pWindowList = new (ELeave) CArrayFixFlat<TInt>(4);
    AUTO_PUSH_POP_DEL(pWindowList);
    HXSYM_LEAVE_IF_ERR(ws.WindowGroupList(pWindowList));
    for (TInt idx = 0; idx < pWindowList->Count(); ++idx)
    {
        TInt wgid = (*pWindowList)[idx];
        if(wgid != wgidThisInstance)
        {
            ws.SendEventToWindowGroup(wgid, event);
        }
    }
}

TInt GetThisWgId()
{
    RWindowGroup& win = CCoeEnv::Static()->RootWin();
    return win.Identifier();
}

TUid GetAppUidFromWgId(TInt wgid)
{
    RWsSession& ws = CEikonEnv::Static()->WsSession();
    CApaWindowGroupName* pName = CApaWindowGroupName::NewL(ws);
    AUTO_PUSH_POP_DEL(pName);

⌨️ 快捷键说明

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