📄 myiweb.step4
字号:
}
if(pMe->m_pIWeb)
{
IWEB_Release(pMe->m_pIWeb);
pMe->m_pIWeb = NULL;
}
// end of lab 1
}
static void myiweb_ShowMenu(myiweb* pMe)
{
CtlAddItem ai;
if (! pMe->m_pIMenu)
ISHELL_CreateInstance(pMe->pIShell, AEECLSID_MENUCTL, (void **)&pMe->m_pIMenu) ;
IMENUCTL_SetTitle(pMe->m_pIMenu,MYIWEB_RES_FILE,IDS_TITLE,NULL);
ai.pText = NULL;
ai.pImage = NULL;
ai.pszResImage = MYIWEB_RES_FILE;
ai.pszResText = MYIWEB_RES_FILE;
ai.wFont = AEE_FONT_NORMAL;
ai.dwData = NULL;
ai.wText = IDS_WCONNECT;
ai.wImage = IDB_MENU;
ai.wItemID = IDS_WCONNECT;
// Add the item to the menu control
if(IMENUCTL_AddItemEx(pMe->m_pIMenu, &ai )==FALSE)
{
IMENUCTL_Release(pMe->m_pIMenu);
return;
}
ai.wText = IDS_FILE_MGR;
ai.wItemID = IDS_FILE_MGR;
// Add the item to the menu control
IMENUCTL_AddItemEx( pMe->m_pIMenu, &ai );
ai.wText = IDS_FAVOURITE;
ai.wItemID = IDS_FAVOURITE;
// Add the item to the menu control
IMENUCTL_AddItemEx( pMe->m_pIMenu, &ai );
ai.wText = IDS_ABOUT;
ai.wItemID = IDS_ABOUT;
// Add the item to the menu control
IMENUCTL_AddItemEx( pMe->m_pIMenu, &ai );
IMENUCTL_SetActive(pMe->m_pIMenu,TRUE);
pMe->m_iAppStatus = 0;
}
// lab 2
static void myiweb_DisplayInfo(myiweb* pMe,AECHAR* pInfo)
{
AEERect rct;
if(!pMe->m_pIStatic)
ISHELL_CreateInstance(pMe->pIShell, AEECLSID_STATIC, (void **)&pMe->m_pIStatic);
SETAEERECT(&rct, 16, 16, pMe->DeviceInfo.cxScreen-32, pMe->DeviceInfo.cyScreen-32);
ISTATIC_SetRect(pMe->m_pIStatic, &rct);
ISTATIC_SetText(pMe->m_pIStatic, NULL,pInfo, AEE_FONT_BOLD, AEE_FONT_NORMAL);
ISTATIC_Redraw(pMe->m_pIStatic);
}
// end of lab 2
//lab 1
static void myiwebhtml_NotifyCB( void* pvUser, HViewNotify* pNotify )
{
myiweb* pMe = (myiweb*) pvUser;
switch( pNotify->code )
{
case HVN_REDRAW_SCREEN:
IDISPLAY_ClearScreen(pMe->a.m_pIDisplay);
IHTMLVIEWER_Redraw(pMe->m_pHTMLViewer);
break;
case HVN_JUMP:
case HVN_SUBMIT:
// lab 4
STRCPY(pMe->targetUrl,TEST_HOST);
pMe->m_pszFileName = (char*)MALLOC(16);
FIND_STRING(pNotify->u.jump.pszURL, "PIC")
{
STRCAT(pMe->targetUrl,pNotify->u.jump.pszURL+4);
STRCPY(pMe->m_pszFileName,pNotify->u.jump.pszURL+4);
pMe->m_iMediaType = 1;
}
FIND_STRING(pNotify->u.jump.pszURL, "MUSIC")
{
STRCAT(pMe->targetUrl,pNotify->u.jump.pszURL+6);
STRCPY(pMe->m_pszFileName,pNotify->u.jump.pszURL+6);
pMe->m_iMediaType = 2;
}
// end of lab 4
// IHTMLVIEWER_SetNotifyFn(pMe->m_pHTMLViewer, NULL, pMe);
// lab 3
myiweb_Stop(pMe);
// end of lab 3
// lab 4
myiweb_Start(pMe,pMe->targetUrl,1);
// lab 4
break;
case HVN_DONE:
IHTMLVIEWER_SetRect(pMe->m_pHTMLViewer, &pMe->m_rc);
IHTMLVIEWER_Redraw( pMe->m_pHTMLViewer );
break;
}
}
// end of lab 1
//lab 1
static void myiweb_Start(myiweb *pMe, char *pszUrl,int iPage)
{
ISourceUtil *pisu;
pMe->m_iAppStatus = 1;
// look to see if there's POST data, this is totally non-standard, but
// easy to put into tests
pMe->pszPostData = STRCHR(pszUrl, 1);
// if there's post data, construct a stream for IWeb to consume
if ((char *)0 != pMe->pszPostData)
{
*pMe->pszPostData = 0;
if (SUCCESS == ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_SOURCEUTIL, (void **)&pisu))
{
ISOURCEUTIL_PeekFromMemory(pisu, pMe->pszPostData + 1,
STRLEN(pMe->pszPostData + 1), 0, 0,
&pMe->pipPostData);
ISOURCEUTIL_Release(pisu);
}
}
// initialize the callback, where I'll be called when the request
// completes
if(iPage == 0)
{
CALLBACK_Cancel(&pMe->cb);
CALLBACK_Init(&pMe->cb, myiweb_GotResp, pMe);
}// lab 4
else if(iPage ==1)
{
CALLBACK_Cancel(&pMe->cb);
CALLBACK_Init(&pMe->cb, myiweb_DownloadFile, pMe);
}
// end of lab 4
// start transaction, pass callbacks for web status, web headers
// the extra WEBOPT_HEADER is used to help test what's sent
// (snoop.sh above shows all the headers)
if(!pMe->m_pIWeb)
ISHELL_CreateInstance(pMe->a.m_pIShell, AEECLSID_WEB, (void**)(&pMe->m_pIWeb));
if ((IPeek *)0 != pMe->pipPostData) {
IWEB_GetResponse(pMe->m_pIWeb,
(pMe->m_pIWeb, &pMe->piWResp, &pMe->cb, pszUrl,
WEBOPT_HANDLERDATA, pMe,
WEBOPT_HEADER, "X-Method: POST\r\n", /* for kicks */
//lab 2
WEBOPT_HEADERHANDLER, myiweb_Header,
WEBOPT_STATUSHANDLER, myiweb_Status,
// end lab 2
WEBOPT_METHOD, "POST",
WEBOPT_BODY, pMe->pipPostData,
WEBOPT_CONTENTLENGTH, STRLEN(pMe->pszPostData + 1),
WEBOPT_END));
} else {
IWEB_GetResponse(pMe->m_pIWeb,
(pMe->m_pIWeb, &pMe->piWResp, &pMe->cb, pszUrl,
WEBOPT_HANDLERDATA, pMe,
WEBOPT_HEADER, "X-Method: GET \r\n",
//lab 2
WEBOPT_HEADERHANDLER, myiweb_Header,
WEBOPT_STATUSHANDLER, myiweb_Status,
//end of lab 2
WEBOPT_END));
}
}
//end of lab 1
// lab 1
static void myiweb_GotResp(void *p)
{
myiweb *pwa = (myiweb *)p;
WebRespInfo *pwri;
// get information about how the web transaction went
// pwa->piWResp is ***NEVER NULL***, even though the transaction may fail
// for wont of memory
pwri = IWEBRESP_GetInfo(pwa->piWResp);
// ND_Print(pMe, "** got response...\n** info code: %d\n", pwri->nCode);
// body may be NULL
if ((ISource *)0 != pwri->pisMessage)
{
IHTMLVIEWER_LoadSource(pwa->m_pHTMLViewer, pwri->pisMessage);
}
}
// end of lab 1
/*===========================================================================
FUNCTION: myiweb_Header
DESCRIPTION:
Received header callback for a web transaction. cpszName is NULL in the case
of continuation header line parts.
PARAMETERS:
p: a myiweb (the subscriber)
cpszName: the name of the web header (like "Content-Type")
pglVal: the value of the header, like "text/html"
DEPENDENCIES:
None
RETURN VALUE:
None
SIDE EFFECTS:
None
===========================================================================*/
// lab 2
static void myiweb_Header(void *p, const char *cpszName, GetLine *pglVal)
{
myiweb *pwa = (myiweb *)p;
// if (pMe->m_bRS) { // If response is to be displayed
if ((char *)0 != cpszName) {
// ND_Print(pMe, "%s:", cpszName);
}
//ND_Print(pMe, "%s\n", pglVal->psz);
// }
}
// end of lab 2
/*===========================================================================
FUNCTION: myiweb_Status
DESCRIPTION:
Web status callback for a Web transaction
PARAMETERS:
p: a myiweb (the subscriber)
ws: type of status
pVal: unused as of yet
DEPENDENCIES:
None
RETURN VALUE:
None
SIDE EFFECTS:
None
===========================================================================*/
// lab 2
static void myiweb_Status(void *p, WebStatus ws, void *pVal)
{
AECHAR szText[32];
myiweb * pMe = (myiweb *)p;
(void)pVal;
switch (ws) {
case WEBS_CANCELLED:
ISHELL_LoadResString(pMe->a.m_pIShell, MYIWEB_RES_FILE, IDS_WEBS_CANCELLED, szText, sizeof(szText));
break;
case WEBS_GETHOSTBYNAME:
ISHELL_LoadResString(pMe->a.m_pIShell, MYIWEB_RES_FILE, IDS_WEBS_GETHOSTBYNAME,szText, sizeof(szText));
break;
case WEBS_CONNECT:
ISHELL_LoadResString(pMe->a.m_pIShell, MYIWEB_RES_FILE, IDS_WEBS_CONNECT, szText, sizeof(szText));
break;
case WEBS_SENDREQUEST:
ISHELL_LoadResString(pMe->a.m_pIShell, MYIWEB_RES_FILE, IDS_WEBS_SENDREQUEST, szText, sizeof(szText));
break;
case WEBS_READRESPONSE:
ISHELL_LoadResString(pMe->a.m_pIShell, MYIWEB_RES_FILE, IDS_WEBS_READRESPONSE, szText, sizeof(szText));
break;
case WEBS_GOTREDIRECT:
ISHELL_LoadResString(pMe->a.m_pIShell, MYIWEB_RES_FILE, IDS_WEBS_GOTREDIRECT, szText, sizeof(szText));
break;
case WEBS_CACHEHIT:
ISHELL_LoadResString(pMe->a.m_pIShell, MYIWEB_RES_FILE, IDS_WEBS_CACHEHIT, szText, sizeof(szText));
break;
}
// show that status!
if ((AECHAR *)0 != szText)
{
myiweb_DisplayInfo(pMe,szText);
}
}
// end of lab 2
/*===========================================================================
FUNCTION: myiweb_Stop
DESCRIPTION:
Halts a web transaction, wrapped/represented by a myiweb
PARAMETERS:
pwa: the myiweb
DEPENDENCIES:
None
RETURN VALUE:
None
SIDE EFFECTS:
None
===========================================================================*/
// lab 3
static void myiweb_Stop(myiweb *pwa)
{
// this cancels any pending web transaction, or readable on the
// response body. if nothing is pending, this has no effect
CALLBACK_Cancel(&pwa->cb);
// then clean up, if necessary
if ((char *)0 != pwa->pszPostData) {
*pwa->pszPostData = 1; // write delimiter back in, if any
pwa->pszPostData = 0;
}
if (pwa->piWResp)
{
IWEBRESP_Release(pwa->piWResp);
pwa->piWResp= NULL;
}
if (pwa->m_pIWeb)
{
IWEB_Release(pwa->m_pIWeb);
pwa->m_pIWeb = NULL;
}
if (pwa->m_BodyBuffer)
{
FREE(pwa->m_BodyBuffer);
pwa->m_BodyBuffer = NULL;
}
pwa->m_BodySize = 0;
pwa->m_BodyAllocSize = 0;
}
// end of lab 3
// lab 4
static void myiweb_DownloadFile(void * p)
{
myiweb *pMe = (myiweb *)p;
WebRespInfo *pwri;
char targetFile[24];
byte buf[1024];
int ByteCount;
IFile * pFile;
// get information about how the web transaction went
// pwa->piWResp is ***NEVER NULL***, even though the transaction may fail
// for wont of memory
pwri = IWEBRESP_GetInfo(pMe->piWResp);
// ND_Print(pMe, "** got response...\n** info code: %d\n", pwri->nCode);
if(WEB_ERROR_SUCCEEDED(pwri->nCode))
DBGPRINTF("request successful!");
else{
WEB_ERROR_MAP(pwri->nCode);
DBGPRINTF("request successful %i",pwri->nCode);
}
// body may be NULL
if ((ISource *)0 != pwri->pisMessage)
{
ISource * pISource = pwri->pisMessage;
ByteCount = ISOURCE_Read(pISource, (byte*)buf, sizeof(buf));
switch(ByteCount)
{
case ISOURCE_END:
if (pMe->m_BodySize < pMe->m_BodyAllocSize)
pMe->m_BodyBuffer[pMe->m_BodySize] = 0;
else
pMe->m_BodyBuffer[pMe->m_BodyAllocSize - 1] = 0;
if(!pMe->m_pIFileMgr)
ISHELL_CreateInstance(pMe->a.m_pIShell,AEECLSID_FILEMGR, (void **)&pMe->m_pIFileMgr);
if(pMe->m_iMediaType == 1)
{
if (IFILEMGR_MkDir(pMe->m_pIFileMgr, "pic") != SUCCESS)
{
IFILEMGR_Release(pMe->m_pIFileMgr);
pMe->m_pIFileMgr = NULL;
return;
}
STRCPY(targetFile,"pic\\");
}else if(pMe->m_iMediaType == 2)
{
if (IFILEMGR_MkDir(pMe->m_pIFileMgr, "music") != SUCCESS)
{
IFILEMGR_Release(pMe->m_pIFileMgr);
pMe->m_pIFileMgr = NULL;
return;
}
STRCPY(targetFile,"music\\");
}
STRCAT(targetFile,pMe->m_pszFileName);
if(IFILEMGR_Test(pMe->m_pIFileMgr,targetFile)==SUCCESS)
IFILEMGR_Remove(pMe->m_pIFileMgr,targetFile);
pFile= IFILEMGR_OpenFile(pMe->m_pIFileMgr,targetFile,_OFM_CREATE);
IFILE_Write(pFile,(void*)pMe->m_BodyBuffer,pMe->m_BodyAllocSize);
IFILE_Release(pFile);
IFILEMGR_Release(pMe->m_pIFileMgr);
pMe->m_pIFileMgr = NULL;
{
AECHAR szFile[64];
AECHAR szText[64];
STRTOWSTR(pMe->m_pszFileName,szFile,64);
ISHELL_LoadResString(pMe->a.m_pIShell, MYIWEB_RES_FILE, IDS_DOWNLOAD_OK, szText, sizeof(szText));
WSTRCAT(szFile,szText);
myiweb_DisplayInfo(pMe,szFile);
}
FREEIF(pMe->m_pszFileName);
break;
case ISOURCE_ERROR:
break;
case ISOURCE_WAIT:
ISOURCE_Readable(pISource, &pMe->cb);
break;
default:
if (ByteCount)
{
if (pMe->m_BodySize + ByteCount > pMe->m_BodyAllocSize)
{
const int NewSize = pMe->m_BodyAllocSize + 1024;
byte* NewBuf = (byte*)REALLOC(pMe->m_BodyBuffer, NewSize);
if (NewBuf)
{
pMe->m_BodyBuffer = NewBuf;
pMe->m_BodyAllocSize = NewSize;
}
}
if(pMe->m_BodySize + ByteCount <= pMe->m_BodyAllocSize)
{
MEMCPY(pMe->m_BodyBuffer + pMe->m_BodySize, buf, ByteCount);
pMe->m_BodySize += ByteCount;
}
}
ISOURCE_Readable(pISource, &pMe->cb);
break;
}
}
}
// end of lab 4
// lab 5
// end of lab 5
// lab 6
// end of lab 6
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -