Back to index

lightning-sunbird  0.9+nobinonly
Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Attributes
CDownloadProgressView Class Reference

#include <UDownloadDisplay.h>

Collaboration diagram for CDownloadProgressView:
Collaboration graph
[legend]

List of all members.

Public Types

enum  { class_ID = FOUR_CHAR_CODE('DPrV') }

Public Member Functions

 CDownloadProgressView ()
 CDownloadProgressView (LStream *inStream)
virtual ~CDownloadProgressView ()
virtual Boolean ObeyCommand (CommandT inCommand, void *ioParam)
virtual void FinishCreateSelf ()
virtual void ListenToMessage (MessageT inMessage, void *ioParam)
virtual void SetDownload (CDownload *aDownload)
virtual void CancelDownload ()
virtual Boolean IsActive ()
virtual void UpdateStatus (CDownload::MsgOnDLProgressChangeInfo *info)
LStr255 & FormatBytes (float inBytes, LStr255 &ioString)
LStr255 & FormatFuzzyTime (PRInt32 inSecs, LStr255 &ioString)

Static Public Member Functions

static OSErr CreateStyleRecFromThemeFont (ThemeFontID inThemeID, ControlFontStyleRec &outStyle)

Protected Types

enum  { kStatusUpdateIntervalTicks = 60 }

Protected Attributes

LProgressBar * mProgressBar
LControl * mCancelButton
LControl * mOpenButton
LControl * mRevealButton
LControl * mCloseButton
LStaticText * mStatusText
LStaticText * mTimeRemainingText
LStaticText * mSrcURIText
LStaticText * mDestFileText
nsCOMPtr< nsIDownloadmDownload
Boolean mDownloadActive
SInt32 mLastStatusUpdateTicks

Detailed Description

Definition at line 128 of file UDownloadDisplay.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
class_ID 

Definition at line 133 of file UDownloadDisplay.h.

{ class_ID = FOUR_CHAR_CODE('DPrV') };
anonymous enum [protected]
Enumerator:
kStatusUpdateIntervalTicks 

Definition at line 165 of file UDownloadDisplay.h.


Constructor & Destructor Documentation

Definition at line 251 of file UDownloadDisplay.cpp.

                                             :
    mDownloadActive(false)
{
}

Definition at line 256 of file UDownloadDisplay.cpp.

                                                              :
    LView(inStream),
    mDownloadActive(false)
{
}

Definition at line 262 of file UDownloadDisplay.cpp.

Here is the call graph for this function:


Member Function Documentation

Definition at line 485 of file UDownloadDisplay.cpp.

{
    CDownload *download = dynamic_cast<CDownload*>(mDownload.get());
    download->Cancel();
}

Here is the call graph for this function:

Here is the caller graph for this function:

OSErr CDownloadProgressView::CreateStyleRecFromThemeFont ( ThemeFontID  inThemeID,
ControlFontStyleRec &  outStyle 
) [static]

Definition at line 588 of file UDownloadDisplay.cpp.

{
    Str255  themeFontName;
    SInt16  themeFontSize;
    Style   themeStyle;
    SInt16  themeFontNum;

    OSErr err = ::GetThemeFont(kThemeSmallSystemFont,
                               smSystemScript,
                               themeFontName,
                               &themeFontSize,
                               &themeStyle);
    if (err != noErr)
        return err;
            
    outStyle.flags   = kControlUseFontMask +
                      kControlUseFaceMask +
                      kControlUseSizeMask;
    
    ::GetFNum(themeFontName, &themeFontNum);
    outStyle.font    = themeFontNum;
    outStyle.size    = themeFontSize;
    outStyle.style   = themeStyle;

    return noErr;
}

Here is the caller graph for this function:

Definition at line 270 of file UDownloadDisplay.cpp.

{
    mProgressBar = dynamic_cast<LProgressBar*>(FindPaneByID(paneID_ProgressBar));
    mCancelButton = dynamic_cast<LControl*>(FindPaneByID(paneID_CancelButton));
    mOpenButton = dynamic_cast<LControl*>(FindPaneByID(paneID_OpenButton));
    mRevealButton = dynamic_cast<LControl*>(FindPaneByID(paneID_RevealButton));
    mCloseButton = dynamic_cast<LControl*>(FindPaneByID(paneID_CloseButton));
    mStatusText = dynamic_cast<LStaticText*>(FindPaneByID(paneID_StatusText));
    mTimeRemainingText = dynamic_cast<LStaticText*>(FindPaneByID(paneID_TimeRemText));
    mSrcURIText = dynamic_cast<LStaticText*>(FindPaneByID(paneID_SrcURIText));
    mDestFileText = dynamic_cast<LStaticText*>(FindPaneByID(paneID_DestFileText));

    // The control value is set in terms of percent
    if (mProgressBar)
        mProgressBar->SetMaxValue(100);
        
    ControlFontStyleRec styleRec;
    
    if (CreateStyleRecFromThemeFont(kThemeSmallSystemFont, styleRec) == noErr) {
        if (mStatusText)
            mStatusText->SetFontStyle(styleRec);
        if (mTimeRemainingText)
            mTimeRemainingText->SetFontStyle(styleRec);
        if (mSrcURIText)
            mSrcURIText->SetFontStyle(styleRec);
        if (mDestFileText)
            mDestFileText->SetFontStyle(styleRec);    
    }
    if (CreateStyleRecFromThemeFont(kThemeSmallEmphasizedSystemFont, styleRec) == noErr) {
        
        ResIDT labelIDs [] = { paneID_StatusLabel,
                               paneID_TimeRemLabel,
                               paneID_SrcURILabel,
                               paneID_DestFileLabel };
                                 
        for (size_t i = 0; i < sizeof(labelIDs) / sizeof(labelIDs[0]); i++) {
            LStaticText *staticText = dynamic_cast<LStaticText*>(FindPaneByID(labelIDs[i]));
            if (staticText)
                staticText->SetFontStyle(styleRec);
        }
    }
   
    UReanimator::LinkListenerToControls(this, this, view_DownloadProgressItem);
    StartListening();
}

Here is the call graph for this function:

LStr255 & CDownloadProgressView::FormatBytes ( float  inBytes,
LStr255 &  ioString 
)

Definition at line 528 of file UDownloadDisplay.cpp.

{
    const float kOneThousand24 = 1024.0;
    char buf[256];
    
    if (inBytes < 0) {
        return (ioString = "???");
    }
    if (inBytes < kOneThousand24) {
        sprintf(buf, "%.1f Bytes", inBytes);
        return (ioString = buf);
    }
    inBytes /= kOneThousand24;
    if (inBytes < 1024) {
        sprintf(buf, "%.1f KB", inBytes);
        return (ioString = buf);
    }
    inBytes /= kOneThousand24;
    if (inBytes < 1024) {
        sprintf(buf, "%.1f MB", inBytes);
        return (ioString = buf);
    }
    inBytes /= kOneThousand24;
    sprintf(buf, "%.2f GB", inBytes);
    return (ioString = buf);
}

Here is the caller graph for this function:

LStr255 & CDownloadProgressView::FormatFuzzyTime ( PRInt32  inSecs,
LStr255 &  ioString 
)

Definition at line 555 of file UDownloadDisplay.cpp.

{
    char valueBuf[32];

    if (inSecs < 90) {
        if (inSecs < 7)
            ioString.Assign(STRx_DownloadStatus, str_About5Seconds);
        else if (inSecs < 13)
            ioString.Assign(STRx_DownloadStatus, str_About10Seconds);
        else if (inSecs < 60)
            ioString.Assign(STRx_DownloadStatus, str_LessThan1Minute);
        else
            ioString.Assign(STRx_DownloadStatus, str_About1Minute);
        return ioString;
    }
    inSecs = (inSecs + 30) / 60; // Round up so we don't say "About 1 minutes"
    if (inSecs < 60) {
        sprintf(valueBuf, "%d", inSecs);
        ioString.Assign(STRx_DownloadStatus, str_AboutNMinutes);
        ioString.Replace(ioString.Find("\p@1"), 2, LStr255(valueBuf));
        return ioString;
    }
    inSecs /= 60;
    if (inSecs == 1)
        ioString.Assign(STRx_DownloadStatus, str_About1Hour);
    else {
        sprintf(valueBuf, "%d", inSecs);
        ioString.Assign(STRx_DownloadStatus, str_AboutNHours);
        ioString.Replace(ioString.Find("\p@1"), 2, LStr255(valueBuf));
    }
    return ioString;
}

Here is the caller graph for this function:

Definition at line 491 of file UDownloadDisplay.cpp.

{
    return mDownloadActive;
}

Here is the caller graph for this function:

void CDownloadProgressView::ListenToMessage ( MessageT  inMessage,
void ioParam 
) [virtual]

Definition at line 366 of file UDownloadDisplay.cpp.

{
    switch (inMessage) {
    
        case CDownload::msg_OnDLStart:
        {
            mDownloadActive = true;
            if (mCancelButton)
                mCancelButton->Enable();
        }
        break;

        case CDownload::msg_OnDLComplete:
        {
            mDownloadActive = false;
            if (mCancelButton)
                mCancelButton->Disable();
            if (mCloseButton)
                mCloseButton->Enable();

            CDownload *download = reinterpret_cast<CDownload*>(ioParam);                    
            nsresult downloadStatus;
            download->GetStatus(downloadStatus);
            
            // When saving documents as plain text, we might not get any
            // progress change notifications in which to set the progress bar.
            // Now that the download is done, put the bar in a reasonable state.
            if (mProgressBar) {
                mProgressBar->SetIndeterminateFlag(false, false);
                if (NS_SUCCEEDED(downloadStatus))
                    mProgressBar->SetValue(mProgressBar->GetMaxValue());
                else
                    mProgressBar->SetValue(0);
            }
            
            if (NS_SUCCEEDED(downloadStatus)) {
                if (mOpenButton)
                    mOpenButton->Enable();
            }
            if (mRevealButton)
                mRevealButton->Enable();
            if (mTimeRemainingText)
                mTimeRemainingText->SetText(LStr255("\p"));
        }
        break;

        case CDownload::msg_OnDLProgressChange:
        {
            CDownload::MsgOnDLProgressChangeInfo *info =
                static_cast<CDownload::MsgOnDLProgressChangeInfo*>(ioParam);
                            
            if (mProgressBar) {
                PRInt32 percentComplete;
                info->mBroadcaster->GetPercentComplete(&percentComplete);
                if (percentComplete != -1 && mProgressBar->IsIndeterminate())
                    mProgressBar->SetIndeterminateFlag(false, false);
                else if (percentComplete == -1 && !mProgressBar->IsIndeterminate())
                    mProgressBar->SetIndeterminateFlag(true, true);
                
                if (!mProgressBar->IsIndeterminate()) {
                    PRInt32 controlVal = min(100, max(0, percentComplete));
                    mProgressBar->SetValue(controlVal);
                }
            }
            
            // Set the progress bar as often as we're called. Smooth movement is nice.
            // Limit the frequency at which the textual status is updated though.
            if (info->mCurProgress == info->mMaxProgress ||
                ::TickCount() - mLastStatusUpdateTicks >= kStatusUpdateIntervalTicks) {
                UpdateStatus(info);
                mLastStatusUpdateTicks = ::TickCount();
            }
        }
        break;
                  
        default:
          ProcessCommand(inMessage, ioParam);
          break;
    }
}

Here is the call graph for this function:

Boolean CDownloadProgressView::ObeyCommand ( CommandT  inCommand,
void ioParam 
) [virtual]

Definition at line 317 of file UDownloadDisplay.cpp.

{
#pragma unused(ioParam)

    Boolean cmdHandled = false;
    nsCOMPtr<nsILocalFile> targetFile;
    
    switch (inCommand)
    {            
        case paneID_CancelButton:
        {
            CancelDownload();
            cmdHandled = true;
        }
        break;
        
        case paneID_OpenButton:
        {
            mDownload->GetTarget(getter_AddRefs(targetFile));
            if (targetFile)
                targetFile->Launch();
        }
        break;
        
        case paneID_RevealButton:
        {
            mDownload->GetTarget(getter_AddRefs(targetFile));
            if (targetFile)
                targetFile->Reveal();
        }
        break;
        
        case paneID_CloseButton:
        {
            LView *view = this, *superView;
            while ((superView = view->GetSuperView()) != nil)
                view = superView;
            CMultiDownloadProgressWindow *multiWindow = dynamic_cast<CMultiDownloadProgressWindow*>(view);
            if (multiWindow)
                multiWindow->RemoveDownloadView(this);
        }
        break;

    }

    return cmdHandled;
}

Here is the call graph for this function:

Definition at line 448 of file UDownloadDisplay.cpp.

{
    mDownload = aDownload;
    if (!mDownload)
        return;
    
    mDownloadActive = true;
    mLastStatusUpdateTicks = ::TickCount();    
    aDownload->AddListener(this);
    
    nsresult rv;
    nsCAutoString tempStr;
    
    if (mSrcURIText) {
        nsCOMPtr<nsIURI> srcURI;
        aDownload->GetSource(getter_AddRefs(srcURI));
        if (srcURI) {
            rv = srcURI->GetSpec(tempStr);
            if (NS_SUCCEEDED(rv))
                mSrcURIText->SetText(const_cast<Ptr>(PromiseFlatCString(tempStr).get()), tempStr.Length());    
        }
    }
    
    if (mDestFileText) {
        nsCOMPtr<nsILocalFile> destFile;
        aDownload->GetTarget(getter_AddRefs(destFile));
        if (destFile) {
            rv = destFile->GetNativePath(tempStr);
            if (NS_SUCCEEDED(rv))
                mDestFileText->SetText(const_cast<Ptr>(PromiseFlatCString(tempStr).get()), tempStr.Length());    
        }
    }
    
    // At this point, make sure our window is showing.
    LWindow::FetchWindowObject(GetMacWindow())->Show();
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 496 of file UDownloadDisplay.cpp.

{    
    PRInt64 startTime;
    mDownload->GetStartTime(&startTime);
    PRInt32 elapsedSecs = (PR_Now() - startTime) / PR_USEC_PER_SEC;
    float bytesPerSec = info->mCurProgress / elapsedSecs;

    UInt8 startPos;
    LStr255 valueStr;
    
    if (mStatusText) {
        // "@1 of @2 (at @3/sec)"
        LStr255 formatStr(STRx_DownloadStatus, str_ProgressFormat);
        
        // Insert each item into the string individually in order to
        // allow certain elements to be omitted from the format.
        if ((startPos = formatStr.Find("\p@1")) != 0)
            formatStr.Replace(startPos, 2, FormatBytes(info->mCurProgress, valueStr));
        if ((startPos = formatStr.Find("\p@2")) != 0)
            formatStr.Replace(startPos, 2, FormatBytes(info->mMaxProgress, valueStr));
        if ((startPos = formatStr.Find("\p@3")) != 0)
            formatStr.Replace(startPos, 2, FormatBytes(bytesPerSec, valueStr));
            
        mStatusText->SetText(formatStr);
    }
    
    if (mTimeRemainingText) {
        PRInt32 secsRemaining = (PRInt32)(float(info->mMaxProgress - info->mCurProgress) / bytesPerSec + 0.5);
        mTimeRemainingText->SetText(FormatFuzzyTime(secsRemaining, valueStr));
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

LControl* CDownloadProgressView::mCancelButton [protected]

Definition at line 168 of file UDownloadDisplay.h.

LControl* CDownloadProgressView::mCloseButton [protected]

Definition at line 169 of file UDownloadDisplay.h.

LStaticText * CDownloadProgressView::mDestFileText [protected]

Definition at line 171 of file UDownloadDisplay.h.

Definition at line 173 of file UDownloadDisplay.h.

Definition at line 174 of file UDownloadDisplay.h.

Definition at line 175 of file UDownloadDisplay.h.

LControl * CDownloadProgressView::mOpenButton [protected]

Definition at line 168 of file UDownloadDisplay.h.

LProgressBar* CDownloadProgressView::mProgressBar [protected]

Definition at line 167 of file UDownloadDisplay.h.

LControl * CDownloadProgressView::mRevealButton [protected]

Definition at line 168 of file UDownloadDisplay.h.

LStaticText* CDownloadProgressView::mSrcURIText [protected]

Definition at line 171 of file UDownloadDisplay.h.

LStaticText* CDownloadProgressView::mStatusText [protected]

Definition at line 170 of file UDownloadDisplay.h.

LStaticText * CDownloadProgressView::mTimeRemainingText [protected]

Definition at line 170 of file UDownloadDisplay.h.


The documentation for this class was generated from the following files: