home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 December / PCWDEC07.iso / Software / Freeware / FlashGot 0.6.4 / chrome / flashgot.jar / content / flashgot / FlashGot.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-08-29  |  8.4 KB  |  444 lines

  1. #include "stdafx.h"
  2. #include <atlstr.h>
  3. #include <shellapi.h>
  4. #include <sys/stat.h>
  5. #include <comdef.h>
  6. #include <atlbase.h>
  7. #include <atlcom.h>
  8. #include <objbase.h>
  9. #include <wininet.h>
  10. #include <time.h>
  11. #include <string>
  12.  
  13. #define VERSION "0.5.99"
  14.  
  15. #define DMS_POLL_DELAY 100
  16. #define DMS_POLL_TIMEOUT 60000
  17. enum OpType { 
  18.         OP_ONE=0, OP_SEL=1, OP_ALL=2, 
  19.         OP_QET=3,    
  20.         OP_MIN=OP_ONE, OP_MAX=OP_QET };
  21.  
  22. typedef struct _LinkInfo 
  23. {
  24.     bstr_t url;
  25.     bstr_t comment;
  26.     bstr_t cookie;
  27.     bstr_t postdata;
  28. } LinkInfo;
  29.  
  30. #define EXTRAS_COUNT 5
  31.  
  32. typedef struct _DownloadInfo 
  33. {
  34.     char *dmName;
  35.     OpType opType;
  36.     bstr_t folder;
  37.     bstr_t *rawParms;
  38.     bstr_t referer;
  39.     LinkInfo *links;
  40.     int linksCount;
  41.     bstr_t *extras;
  42. } DownloadInfo;
  43.  
  44. void fail(char *msg, int code);
  45.  
  46. class CookieManager
  47. {
  48.     private: 
  49.         
  50.         const DownloadInfo *downloadInfo;
  51.     public:
  52.         CookieManager() : downloadInfo(NULL) {}
  53.         
  54.         CookieManager(const DownloadInfo *downloadInfo) : downloadInfo(downloadInfo)
  55.         {
  56.             setAll(downloadInfo, 3600); // 1 hour timespan;
  57.         }
  58.  
  59.         static void makeExpiration(char *expiration, size_t buflen, int offset)
  60.         {
  61.             time_t now;
  62.             time(&now);
  63.             now += offset;
  64.         
  65.             strftime(expiration, buflen,
  66.                 "%a, %d-%b-%Y %H:%M:%S GMT", gmtime(&now) );
  67.         }
  68.  
  69.         static void setCookie(const bstr_t *url, const bstr_t *cookie, char *expiration) {
  70.             int cookieLen;
  71.             if(cookie  && (cookieLen=cookie->length()))
  72.             {
  73.                 
  74.                 char *lpszUrl=*url;
  75.                 char *lpszCookie=new char[cookieLen+16+strlen(expiration)];
  76.                 
  77.                 for(char *cookiePiece=*cookie; cookiePiece && cookiePiece[0]; )
  78.                 {
  79.                     if(cookiePiece[0]==' ')
  80.                     {
  81.                         cookiePiece++;
  82.                         continue;
  83.                     }
  84.  
  85.                     char *nextPiece=strchr(cookiePiece,';');
  86.                     if(nextPiece)
  87.                     {
  88.                         nextPiece[0]='\0';
  89.                         nextPiece++;
  90.                     }
  91.                     ;
  92.                 
  93.                     InternetSetCookie(lpszUrl, NULL, 
  94.                         strcat(strcat(strcpy(lpszCookie,cookiePiece),"; expires = "),expiration));
  95.                     cookiePiece=nextPiece;
  96.                 }
  97.  
  98.                 
  99.                 delete [] lpszCookie;
  100.             } 
  101.             
  102.         }
  103.  
  104.         static void setAll(const DownloadInfo *downloadInfo, long offsetExpiration)
  105.         {
  106.             char expiration[64];
  107.             makeExpiration(expiration,64,offsetExpiration);
  108.             LinkInfo *links=downloadInfo->links;
  109.             for(int j=downloadInfo->linksCount; j-->0;)
  110.             {
  111.                 LinkInfo l=links[j];
  112.                 setCookie(&l.url,&l.cookie,expiration);
  113.             }
  114.         }
  115.     
  116.     
  117. };
  118.  
  119. class FGArray
  120. {
  121.     private:
  122.         VARIANT varStr,array;
  123.         SAFEARRAY *psa;
  124.         int elemCount;
  125.         long ix[1];
  126.         
  127.     
  128.         void init(int elemCount)
  129.         {
  130.             this->psa=createSafeArray(elemCount);
  131.             this->elemCount=elemCount;
  132.             ix[0]=0;
  133.             varStr.vt=VT_BSTR;
  134.         }
  135.  
  136.     public:
  137.  
  138.     static SAFEARRAY *createSafeArray(int elemCount) 
  139.         {
  140.             SAFEARRAYBOUND bounds [1]; 
  141.             bounds[0].cElements=elemCount;
  142.             bounds[0].lLbound=0;
  143.             return SafeArrayCreate(VT_VARIANT, 1, bounds);
  144.         }
  145.  
  146.  
  147.         FGArray(int elemCount)
  148.         {
  149.             init(elemCount);
  150.         }
  151.         
  152.         FGArray(const DownloadInfo *downloadInfo)
  153.         {        
  154.             init(downloadInfo->linksCount * 2 + 1); // linksCount * 2 (url,info)
  155.             addString(downloadInfo->referer);
  156.             addLinks(downloadInfo);
  157.             
  158.         }
  159.         
  160.         void addLinks(const DownloadInfo *downloadInfo) {
  161.             LinkInfo *links=downloadInfo->links;
  162.             for (int j=0, linksCount=downloadInfo->linksCount; j < linksCount ; j++) 
  163.             {
  164.                 LinkInfo l=links[j];
  165.                 addString(l.url);
  166.                 addString(l.comment);
  167.             }
  168.         }
  169.  
  170.         BOOL addString(bstr_t s) 
  171.         {
  172.             if(ix[0]<elemCount) 
  173.             {
  174.                 varStr.bstrVal=s; 
  175.                 SafeArrayPutElement(psa, ix, &varStr); 
  176.                 ix[0]++;
  177.                 return TRUE;
  178.             }
  179.             return FALSE;
  180.         }
  181.  
  182.         BOOL putString(int idx, bstr_t s)
  183.         {
  184.             setIdx(idx);
  185.             return addString(s);
  186.         }
  187.         
  188.         void setIdx(int idx)
  189.         {
  190.             ix[0]=idx;
  191.         }
  192.         
  193.         VARIANT *asVariant(VARIANT *array)
  194.         {
  195.             array->vt = VT_ARRAY | VT_VARIANT | VT_BYREF; // VBScript Array
  196.             array->pparray=&psa;
  197.             return array;
  198.         }
  199.  
  200.         VARIANT *asVariant()
  201.         {
  202.             return asVariant(&this->array);
  203.         }
  204.  
  205.         ~FGArray()
  206.         {
  207.             SafeArrayDestroy(psa);
  208.         }
  209.     
  210. };
  211.  
  212.  
  213. class FGCOMGuard
  214. {
  215. private:
  216.     static FGCOMGuard *instance;
  217.     static int refCount;
  218.  
  219.     FGCOMGuard()
  220.     {
  221.         CoInitialize(NULL);
  222.     }
  223.     ~FGCOMGuard()
  224.     {
  225.         CoUninitialize();
  226.     }
  227. public:
  228.     
  229.     static void addClient()
  230.     {
  231.         if(!instance) instance=new FGCOMGuard();
  232.         refCount++;
  233.     }
  234.     
  235.     static void removeClient()
  236.     {
  237.         if(--refCount<0 || !instance) throw "FGGuard Illegal state";
  238.         if(refCount == 0)
  239.         {
  240.             delete instance;
  241.         }
  242.         
  243.     }
  244.  
  245.  
  246. };
  247.  
  248.  
  249. // [START DOWNLOAD MANAGER SUPPORT CLASSES]
  250.  
  251. class DMSupport
  252. {
  253.  
  254. protected:
  255.     
  256.     static BOOL createProcess(char *commandLine, PROCESS_INFORMATION *pi) {
  257.         STARTUPINFO si;
  258.         ZeroMemory( &si, sizeof(si) );
  259.         si.cb = sizeof(si);
  260.         
  261.         PROCESS_INFORMATION *mypi=NULL;
  262.         if(!pi) {
  263.             pi=mypi=new PROCESS_INFORMATION;
  264.         }
  265.         ZeroMemory(pi, sizeof(pi) );
  266.  
  267.         
  268.         BOOL ret=CreateProcess( NULL, 
  269.                 commandLine, // Command line. 
  270.                 NULL,             // Process handle not inheritable. 
  271.                 NULL,             // Thread handle not inheritable. 
  272.                 FALSE,            // Set handle inheritance to FALSE. 
  273.                 0,                // No creation flags. 
  274.                 NULL,             // Use parent's environment block. 
  275.                 NULL,             // Use parent's starting directory. 
  276.                 &si,              // Pointer to STARTUPINFO structure.
  277.                 pi )             // Pointer to PROCESS_INFORMATION structure.
  278.             ;
  279.         
  280.         if(mypi) {
  281.             closeProcess(mypi);
  282.             delete mypi;
  283.         }
  284.         
  285.         return ret;
  286.     }
  287.  
  288.     static void closeProcess(PROCESS_INFORMATION *pi) {
  289.         CloseHandle( pi->hProcess );
  290.         CloseHandle( pi->hThread );
  291.     }
  292.  
  293.  
  294. public:
  295.     
  296.     
  297.     virtual void check(void) = 0;
  298.     virtual void dispatch(const DownloadInfo *downloadInfo) = 0;
  299.     virtual const char *getName(void) = 0;
  300.  
  301. };
  302.  
  303. #define COMCALL(Call) if(hr=FAILED(Call)) throw _com_error(hr)
  304. #define HELPER(name) FGCOMHelper name(this->getProgId())
  305. class FGCOMHelper
  306. {
  307. private:
  308.     HRESULT hr;
  309.     CComDispatchDriver  comObj;
  310.     CComPtr<IDispatch>  lpTDispatch;
  311.     
  312.     VARIANT tmpVarStr;
  313.  
  314.     const char *className;
  315.  
  316.     
  317.     void prepareCOMObj() {
  318.         
  319.         COMCALL(lpTDispatch.CoCreateInstance(_bstr_t(className),NULL));
  320.         comObj=lpTDispatch;
  321.     }
  322.  
  323. public:
  324.     
  325.     void getMemberID(char *memberName, DISPID *dispid) {
  326.         USES_CONVERSION;
  327.         OLECHAR FAR* oleMember=A2OLE(memberName);
  328.         COMCALL(comObj->GetIDsOfNames(IID_NULL, &oleMember, 1, LOCALE_SYSTEM_DEFAULT, dispid));
  329.     }
  330.  
  331.     void invoke(DISPID *dispid, VARIANT *parms, const unsigned int parmsCount) {    
  332.         COMCALL(comObj.InvokeN(*dispid,parms,parmsCount,NULL));
  333.     }
  334.     
  335.     void invoke(DISPID *dispid) {    
  336.         COMCALL(comObj.Invoke0(*dispid,NULL));
  337.     }
  338.  
  339.     void invoke(char *memberName, VARIANT *parms, const unsigned int parmsCount) {
  340.         DISPID dispid;
  341.         getMemberID(memberName,&dispid);
  342.         invoke(&dispid,parms,parmsCount);
  343.     }
  344.  
  345.     void invoke(char *memberName, _bstr_t parm) {
  346.         tmpVarStr.bstrVal=parm;
  347.         invoke(memberName,&tmpVarStr,1);
  348.     }
  349.     
  350.     void invoke(char *memberName) {
  351.         invoke(memberName,&tmpVarStr,0);
  352.     }
  353.  
  354.     IDispatch *getIDispatch() {
  355.         return lpTDispatch;
  356.     }
  357.     void set(char *propertyName, VARIANT *val) {
  358.         USES_CONVERSION;
  359.         LPCOLESTR oleMember=A2COLE(propertyName);
  360.         COMCALL(comObj.PutPropertyByName(oleMember, val));
  361.     }
  362.     
  363.     void set(char *propertyName, bstr_t val) {
  364.         tmpVarStr.bstrVal=val;
  365.         set(propertyName,&tmpVarStr);
  366.     }
  367.  
  368.     void get(char *propertyName, VARIANT *val, UINT indexCount) {
  369.         DISPPARAMS dispparams = {indexCount==0?NULL:&(val[1]), NULL, indexCount, 0};
  370.         DISPID dispid;
  371.         getMemberID(propertyName,&dispid);
  372.         COMCALL(lpTDispatch->Invoke(dispid, IID_NULL,
  373.                 LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
  374.                 &dispparams, val, NULL, NULL));
  375.     }
  376.     
  377.     
  378.  
  379.  
  380.     FGCOMHelper(const char *className) {
  381.     
  382.         this->className=className;
  383.         prepareCOMObj();
  384.         tmpVarStr.vt=VT_BSTR;
  385.     }
  386.  
  387.     
  388.     ~FGCOMHelper()
  389.     {
  390.         
  391.     }
  392.  
  393. };
  394.  
  395.  
  396. class DMSupportCOM :
  397.     public DMSupport
  398. {
  399.  
  400. protected:
  401.     
  402.     
  403.     virtual const char * getProgId() = 0;
  404.     
  405. public:
  406.     
  407.     DMSupportCOM() {
  408.         FGCOMGuard::addClient();
  409.     }
  410.     void check() 
  411.     {
  412.         HRESULT hr;
  413.         CLSID clsid;
  414.         COMCALL(CLSIDFromProgID(_bstr_t(getProgId()),&clsid));
  415.     }
  416.     
  417.     
  418.  
  419.     virtual ~DMSupportCOM() {
  420.         FGCOMGuard::removeClient();
  421.     }
  422.     
  423. };
  424.  
  425.  
  426. class DMSDownloadAcceleratorPlus:
  427.     public DMSupportCOM
  428. {
  429. protected:
  430.     
  431.     const char * getProgId() 
  432.     { 
  433.         return "dapie.catcher"; 
  434.     }
  435.  
  436. public:
  437.     const char * getName() 
  438.     { 
  439.         return "Download Accelerator Plus";  
  440.     }
  441.     void dispatch(const DownloadInfo *downloadInfo);
  442.     
  443. };
  444.