RssDownloader.cpp

/****************************************************************************/

/**                                                                        **/

/** Copyright (c) 2005 Diodia Software          (http://www.diodia.com/)   **/

/**                                                                        **/

/** Use in any form with or without modification for commercial and non-   **/

/** commercial purposes is permitted.                                      **/

/**                                                                        **/

/****************************************************************************/

#include "stdafx.h"

 

#include "urlmon.h"

 

#include "Helper.h"

#include "ContextC.h"

#include "RssHelpers.h"

#include "RssDownloaderC.h"

 

RssDownloaderC RssDownloader;

 

/****************************************************************************/

/*

/* DESCRIPTION : RssDownloaderC constructor.

/*

/* RETURN VALUE: -

/*

/****************************************************************************/

RssDownloaderC::RssDownloaderC()

{

   mDownloadMutex = CreateMutex(NULL, FALSE, "DiodiaRssDownload");

   mDownloadOwner = false;

 

   mRegistryMutex = CreateMutex(NULL, FALSE, "DiodiaRssRegistry");

 

   mStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

   mStopped = true;

 

   mThreadId = 0;

   mThread = NULL;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : RssDownloaderC destructor.

/*

/* RETURN VALUE: -

/*

/****************************************************************************/

RssDownloaderC::~RssDownloaderC()

{

   /* tell download thread to stop */

   SetEvent(mStopEvent);

 

   /* wait while download thread exits  */

   for (int Counter = 0; Counter < 500 && !mStopped; Counter++)

      Sleep(10);

 

   if (!mStopped && mThread)

      TerminateThread(mThread, 0);

 

   CloseHandle(mStopEvent);

   mStopEvent = NULL;

 

   CloseHandle(mRegistryMutex);

 

   CloseHandle(mDownloadMutex);

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Gain access to feed list in Registry.

/*

/* RETURN VALUE: true - if registry locked

/*               false - otherwise

/*

/****************************************************************************/

bool RssDownloaderC::LockRegistry()

{

   if (WaitForSingleObject(mRegistryMutex, 5000) == WAIT_TIMEOUT)

      return false;

   return true;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Release access to feed list in Registry.

/*

/* RETURN VALUE: None

/*

/****************************************************************************/

void RssDownloaderC::UnlockRegistry()

{

   ReleaseMutex(mRegistryMutex);

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Start downloader.

/*

/* RETURN VALUE: None

/*

/****************************************************************************/

void RssDownloaderC::Start(HWND BrowserWnd, const char *DllPath)

{

   size_t        Pos;

 

   if (!mFeedDirectory.empty())

      return;

 

   mStopped = false;

 

   mFeedDirectory = DllPath;

   Pos = mFeedDirectory.rfind('\\');

   if (Pos == std::string::npos)

   {

      MessageBox(BrowserWnd, ((std::string) "Unable to create feed directory from DLL path " + DllPath).c_str(), "RSS Feeds Toolbar", MB_OK);

      return;

   }

   mFeedDirectory.erase(Pos + 1);

   mFeedDirectory += "Feeds";

   if (!CreatePath(mFeedDirectory.c_str()))

   {

      MessageBox(BrowserWnd, ((std::string) "Unable to create feed directory " + mFeedDirectory).c_str(), "RSS Feeds Toolbar", MB_OK);

      return;

   }

 

   mThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) _DownloadThread, (LPVOID) this, 0, &mThreadId);

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Static wrapper for DownloadThread (thread procedure).

/*

/* RETURN VALUE: S_OK

/*

/****************************************************************************/

DWORD WINAPI RssDownloaderC::_DownloadThread(RssDownloaderC *Downloader)

{

   Downloader->DownloadThread();

   return S_OK;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Update all RSS feeds every XX minutes.

/*

/* RETURN VALUE: None

/*

/****************************************************************************/

void RssDownloaderC::DownloadThread()

{

   int          Timeout;

 

   while (true)

   {

      Download();

      Timeout = 60 * 1000 * MAX(1, RegistryReadInt(HKEY_CURRENT_USER, REG_KEY, "UpdateFrequency"));

      if (WaitForSingleObject(mStopEvent, Timeout) != WAIT_TIMEOUT)

         break;

   }

 

   if (mDownloadOwner)

      ReleaseMutex(mDownloadMutex);

   mStopped = true;

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Download all RSS feeds.

/*

/* RETURN VALUE: None

/*

/****************************************************************************/

void RssDownloaderC::Download()

{

   std::string  Name;

   std::string  Address;

   char         NameVal[32];

   char         AddressVal[32];

   int          Index = 0;

 

   if (WaitForSingleObject(mDownloadMutex, 0) == WAIT_TIMEOUT)

      return;

 

   mDownloadOwner = true;

 

   if (!LockRegistry())

      return;

 

   while (true)

   {

      sprintf(NameVal, "Name%d", Index);

      sprintf(AddressVal, "Address%d", Index);

 

      Name = RegistryReadString(HKEY_CURRENT_USER, REG_KEY "\\Feeds", NameVal);

      Address = RegistryReadString(HKEY_CURRENT_USER, REG_KEY "\\Feeds", AddressVal);

 

      if (Name.empty() || Address.empty())

         break;

 

      DownloadOneFeed(Name.c_str(), Address.c_str());

 

      Index++;

   }

 

   RegistrySaveInt(HKEY_CURRENT_USER, REG_KEY, "LastUpdate", GetTickCount());

 

   UnlockRegistry();

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Download one RSS feeds.

/*

/* RETURN VALUE: None

/*

/****************************************************************************/

void RssDownloaderC::DownloadOneFeed(const char *Name, const char *Address)

{

   std::string  FullName;

 

   FullName = FeedNameToFileName(mFeedDirectory.c_str(), Name);

 

   URLDownloadToFile(NULL, Address, FullName.c_str(), 0, NULL);

}

 

/****************************************************************************/

/*

/* DESCRIPTION : Get feeds directory.

/*

/* RETURN VALUE: Path

/*

/****************************************************************************/

const char  *RssDownloaderC::GetFeedDirectory()

{

   return mFeedDirectory.c_str();

}

 





Source Files
   Commands.cpp
   Context.cpp
   CreatePath.cpp
   DateParser.cpp
   DlgEditFeedC.cpp
   DlgManageFeedsC.cpp
   Hook.cpp
   Iso8601.cpp
   MsXmlMisc.cpp
   MsXmlSelect.cpp
   RegistryNotify.cpp
   RegistryUtil.cpp
   RSS.cpp
   RSS.def
   RSS.idl
   RssAction.cpp
   RssDownloader.cpp
   RssFeed.cpp
   RssHelpers.cpp
   RssInit.cpp
   RssItem.cpp
   RssMultiFeed.cpp
   RssTimer.cpp
   RssXml.cpp
   StdAfx.cpp

Header Files
   Commands.h
   ContextC.h
   DateParser.h
   DlgEditFeedC.h
   DlgManageFeedsC.h
   Helper.h
   HookC.h
   RegistryNotify.h
   RegistryNotifyC.h
   Resource.h
   RssDownloaderC.h
   RssFeedC.h
   RssHelpers.h
   RssItemC.h
   RssMultiFeedC.h
   StdAfx.h
   ThreadObject.h
   XmlHelpers.h

Resource Files
   about.htm
   DiodiaLogoSmall.gif
   logo.bmp
   manifest.xml
   read.bmp
   RSS.rc
   RSS.rgs
   ToolBandLayout.xml
 

© 2002-2008 Diodia Software