00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "FdmApp.h"
00008 #include "Downloads_Opinions_WBEvents.h"
00009 #include "Downloads_Opinions.h"
00010 #include <mshtml.h>
00011 #include "MainFrm.h"
00012
00013 _COM_SMARTPTR_TYPEDEF(IHTMLInputElement, __uuidof(IHTMLInputElement));
00014
00015 #ifdef _DEBUG
00016 #define new DEBUG_NEW
00017 #undef THIS_FILE
00018 static char THIS_FILE[] = __FILE__;
00019 #endif
00020
00021 CDownloads_Opinions_WBEvents::CDownloads_Opinions_WBEvents()
00022 {
00023 EnableAutomation ();
00024 m_pwndOpinions = NULL;
00025 }
00026
00027 CDownloads_Opinions_WBEvents::~CDownloads_Opinions_WBEvents()
00028 {
00029 }
00030
00031 BEGIN_MESSAGE_MAP(CDownloads_Opinions_WBEvents, CCmdTarget)
00032
00033
00034
00035 END_MESSAGE_MAP()
00036
00037 BEGIN_DISPATCH_MAP(CDownloads_Opinions_WBEvents, CCmdTarget)
00038 DISP_FUNCTION_ID(CDownloads_Opinions_WBEvents, "DocumentComplete", DISPID_DOCUMENTCOMPLETE, OnDocumentComplete, VT_EMPTY, VTS_DISPATCH VTS_PVARIANT)
00039 DISP_FUNCTION_ID(CDownloads_Opinions_WBEvents, "NavigateError", DISPID_NAVIGATEERROR, OnNavigateError, VT_EMPTY, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
00040 END_DISPATCH_MAP()
00041
00042 void CDownloads_Opinions_WBEvents::OnDocumentComplete(LPDISPATCH pdWB, VARIANT *URL)
00043 {
00044 IWebBrowser2Ptr spWB (pdWB);
00045 if (spWB == NULL)
00046 return;
00047
00048 IDispatchPtr spdDoc;
00049 spWB->get_Document (&spdDoc);
00050 IHTMLDocumentPtr spDoc (spdDoc);
00051 if (spDoc == NULL)
00052 return;
00053
00054 ICustomDocPtr spCD (spDoc);
00055 if (spCD != NULL)
00056 spCD->SetUIHandler (&m_wbUIHandler);
00057
00058 bool bSetTab = false;
00059 if (m_pwndOpinions->m_cSetAsCurrentTabAfterLoadComplete)
00060 {
00061 InterlockedDecrement (&m_pwndOpinions->m_cSetAsCurrentTabAfterLoadComplete);
00062 bSetTab = true;
00063 }
00064
00065 if (m_pwndOpinions->m_dld == NULL)
00066 return;
00067
00068 if (bSetTab)
00069 _pwndDownloads->m_wndDownloads.m_info.set_CurTab (DIT_OPINIONS);
00070
00071 IDispatch *pdScript;
00072 spDoc->get_Script (&pdScript);
00073 if (pdScript == NULL)
00074 return;
00075
00076 DISPID didFun;
00077 OLECHAR* pwszFunName = L"processform";
00078
00079 HRESULT hr;
00080 hr = pdScript->GetIDsOfNames (IID_NULL, &pwszFunName, 1, LOCALE_SYSTEM_DEFAULT, &didFun);
00081 if (FAILED (hr))
00082 return;
00083
00084 COleVariant vtResult;
00085 BYTE params [] = {VTS_BSTR VTS_BSTR VTS_BSTR VTS_BSTR VTS_BSTR};
00086
00087 CString strVersion;
00088 CMainFrame* pFrm = (CMainFrame*)AfxGetApp ()->m_pMainWnd;
00089 strVersion.Format ("%d_%d", PRG_BUILD_NUMBER, pFrm->m_Customizations.get_AffiliateID ());
00090
00091 CString strState;
00092 if (m_pwndOpinions->m_dld->pMgr->IsDone ())
00093 strState = "Downloaded";
00094 else if (m_pwndOpinions->m_dld->pMgr->IsRunning ())
00095 strState = "Downloading";
00096 else
00097 strState = "Paused";
00098 CString strSize;
00099 UINT64 u = m_pwndOpinions->m_dld->pMgr->GetSSFileSize ();
00100 if (u != _UI64_MAX) {
00101 char sz [100];
00102 _i64toa ((__int64)u, sz, 10);
00103 strSize = sz;
00104 }
00105 else
00106 strSize = "Unknown";
00107 CString strComment = m_pwndOpinions->m_dld->strComment;
00108 strComment.Replace ("\r\n", " ");
00109 strComment.Replace ("\r", " ");
00110 strComment.Replace ("\n", " ");
00111
00112 CString strUrl;
00113 if (m_pwndOpinions->m_dld->pMgr->IsBittorrent ())
00114 strUrl = m_pwndOpinions->m_dld->pMgr->GetBtDownloadMgr ()->get_InfoHash ();
00115 else
00116 strUrl = m_pwndOpinions->m_dld->pMgr->get_URL ();
00117
00118 COleDispatchDriver ddr (pdScript);
00119 ddr.InvokeHelper (didFun, DISPATCH_METHOD, VT_VARIANT, (void*)&vtResult,
00120 params, strVersion, strUrl, strComment,
00121 strState, strSize);
00122
00123 RetrieveLinkToUsText (spdDoc);
00124 }
00125
00126 HRESULT CDownloads_Opinions_WBEvents::Attach()
00127 {
00128 IConnectionPointContainerPtr spCPC;
00129
00130 LPUNKNOWN pUnk = m_pwndOpinions->m_wb.GetControlUnknown ();
00131 if (pUnk == NULL)
00132 return E_FAIL;
00133
00134 spCPC = pUnk;
00135 if (spCPC == NULL)
00136 return E_FAIL;
00137
00138 spCPC->FindConnectionPoint (DIID_DWebBrowserEvents2, &m_spCP);
00139 if (m_spCP == NULL)
00140 return E_FAIL;
00141
00142 return m_spCP->Advise (GetIDispatch (FALSE), &m_dwCookie);
00143 }
00144
00145 void CDownloads_Opinions_WBEvents::Detach()
00146 {
00147 if (m_spCP)
00148 {
00149 m_spCP->Unadvise (m_dwCookie);
00150 m_spCP = NULL;
00151 }
00152 }
00153
00154 void CDownloads_Opinions_WBEvents::RetrieveLinkToUsText(IDispatch *pdDoc)
00155 {
00156 IHTMLDocument2Ptr spDoc (pdDoc);
00157 ASSERT (spDoc != NULL);
00158 IHTMLElementCollectionPtr spelForms;
00159 spDoc->get_forms (&spelForms);
00160 if (spelForms == NULL)
00161 return;
00162
00163 long cForms = 0;
00164 spelForms->get_length (&cForms);
00165 for (long i = 0; i < cForms; i++)
00166 {
00167 IDispatchPtr spdel;
00168 spelForms->item (COleVariant (i), COleVariant (i), &spdel);
00169 ASSERT (spdel != NULL);
00170 IHTMLFormElementPtr spForm (spdel);
00171 ASSERT (spForm != NULL);
00172
00173 bool bFound;
00174
00175 CString str = GetFormInputElementText (spForm, "LINKTOUSTEXT", bFound);
00176 if (bFound)
00177 _App.View_SpreadHelpDialog_LinkToUsText (str);
00178
00179 str = GetFormInputElementText (spForm, "RADIOBUTTON1TEXT", bFound);
00180 if (bFound)
00181 _App.View_SpreadHelpDialog_RadioButton1Text (str);
00182 str = GetFormInputElementText (spForm, "RADIOBUTTON2TEXT", bFound);
00183 if (bFound)
00184 _App.View_SpreadHelpDialog_RadioButton2Text (str);
00185 }
00186 }
00187
00188 CString CDownloads_Opinions_WBEvents::GetFormInputElementText(IHTMLFormElement *pForm, LPCSTR pszElementName, bool& bFound)
00189 {
00190 bFound = false;
00191 IDispatchPtr spdelInp;
00192 pForm->item (COleVariant (pszElementName), COleVariant ((long)0), &spdelInp);
00193 if (spdelInp == NULL)
00194 return "";
00195 IHTMLInputElementPtr spelInp (spdelInp);
00196 if (spelInp == NULL)
00197 return "";
00198 BSTR bstr;
00199 spelInp->get_defaultValue (&bstr);
00200 CString strEl = bstr;
00201 SysFreeString (bstr);
00202 bFound = true;
00203 return strEl;
00204 }
00205
00206 void CDownloads_Opinions_WBEvents::OnNavigateError(LPDISPATCH pDisp, VARIANT *URL, VARIANT *TargetFrameName, VARIANT *StatusCode, BOOL *pbCancel)
00207 {
00208 if (m_pwndOpinions->m_cSetAsCurrentTabAfterLoadComplete)
00209 {
00210
00211 InterlockedDecrement (&m_pwndOpinions->m_cSetAsCurrentTabAfterLoadComplete);
00212 }
00213 else
00214 {
00215
00216 AfxGetApp ()->m_pMainWnd->MessageBox (LS (L_COMMUNITYSERVUNAVAIL), LS (L_ERR), MB_ICONERROR);
00217 _pwndDownloads->m_wndDownloads.m_info.set_CurTab (DIT_LOG);
00218 }
00219
00220 m_pwndOpinions->m_enCS = DOCS_UNKNOWN;
00221
00222 *pbCancel = TRUE;
00223 }