|
/*
Project : RSS Reader plugin for Internet Explorer
File name : RRPConfigurationManager.cpp
Date :
Purpose : It is the configuration manager.It reads from the config.xml file
and provides single point access to configuration settings
Note : It is a Singlton class(Implments Singleton Pattern GoF)
Send comments
or Bugs to : prafulla_t@users.sourceforge.net
*/
#include "StdAfx.h"
#include "RRPConfigurationManager.h"
#include <iostream>
RRPConfigurationManager::RRPConfigurationManager(void) {
doc = NULL;
modified = false;
CRegKey currentVersionRegEntry;
TCHAR programFilesAddress[2048];
CString rssDirectory;
currentVersionRegEntry.Create(HKEY_LOCAL_MACHINE,
_T("Software\\Microsoft\\Windows\\CurrentVersion"));
DWORD length= 2048;
if(ERROR_SUCCESS ==
currentVersionRegEntry.QueryValue(programFilesAddress, _T("ProgramFilesDir"),&length)) {
__configFilePath = CString(programFilesAddress) + _T("\\RssReader for IE\\config.xml");
rssDirectory = CString(programFilesAddress) + _T("\\RssReader for IE");
}
else {
//C:\\Program Files is taken as default
__configFilePath = _T("C:\\Program Files\\RssReader for IE\\config.xml");
rssDirectory = _T("C:\\Program Files\\RssReader for IE");
}
configFile = new char[__configFilePath.GetLength() + 1];
int i = 0;
for(i = 0;i < __configFilePath.GetLength();i++)
configFile[i] = __configFilePath[i];
configFile[i] = '\0';
rssDir = new char[rssDirectory.GetLength() + 1];
for(i = 0;i < rssDirectory.GetLength();i++)
rssDir[i] = rssDirectory[i];
rssDir[i] = '\0';
OFSTRUCT fileInformation;
//Checking if the file exists
fileHandle = ::OpenFile(configFile,&fileInformation,OF_READ);
GetFileTime(reinterpret_cast<HANDLE>(fileHandle),&creationOld,&lastAccessedOld,&lastWriteOld);
if(fileHandle == HFILE_ERROR) {
//Creating directory if it does not exist
::CreateDirectory(rssDirectory,NULL);
rssDirectory += _T("\\rss");
::CreateDirectory(rssDirectory,NULL);
::CloseHandle(reinterpret_cast<HANDLE>(fileHandle));
fileHandle = ::OpenFile(configFile,&fileInformation,OF_CREATE);
GetFileTime(reinterpret_cast<HANDLE>(fileHandle),&creationOld,&lastAccessedOld,&lastWriteOld);
::CloseHandle(reinterpret_cast<HANDLE>(fileHandle));
writeDefaultConfigurationFile();
}
::CloseHandle(reinterpret_cast<HANDLE>(fileHandle));
//Parse it up
parse();
toolbar = NULL;
//We'll not delete all the files on closing of IE,by default
//But if any of the button settings is modified we will delete them to ensure that
//next IE instance do not show wrong news
deleteOnCloseFlag = false;
}
RRPConfigurationManager::~RRPConfigurationManager(void) {
OFSTRUCT fileInformation;
fileHandle = ::OpenFile(configFile,&fileInformation,OF_READ);
FILETIME lastWrite;
FILETIME creation;
FILETIME lastAccessed;
GetFileTime(reinterpret_cast<HANDLE>(fileHandle),&creation,&lastAccessed,&lastWrite);
::CloseHandle(reinterpret_cast<HANDLE>(fileHandle));
//Ensuring that changes made by some recent process are not
//lost by this process
if((lastWrite.dwLowDateTime == lastWriteOld.dwLowDateTime) &&
lastWrite.dwHighDateTime == lastWriteOld.dwHighDateTime)
writeConfigurationFile();
int n = newsButtonVector.size();
for(int i = 0;i<n;i++)
delete newsButtonVector[i];
if(doc)
delete doc;
if(configFile)
delete configFile;
if(rssDir)
delete rssDir;
}
//Returns the Installation directory
char *RRPConfigurationManager::getInstallationDirectory() {
return rssDir;
}
void RRPConfigurationManager::writeDefaultConfigurationFile() {
TiXmlDocument defaultDoc = TiXmlDocument();
TiXmlComment *title = new TiXmlComment("Configuration file for RSS Reader plugin for IE");
TiXmlComment *author = new TiXmlComment("Author : Prafulla Tekawade(prafulla_t@users.sourceforge.net)");
TiXmlElement *rssReader = new TiXmlElement("rss-reader");
TiXmlElement *newsItem = new TiXmlElement("news-item");
TiXmlElement *titleElement = new TiXmlElement("title");
titleElement->LinkEndChild(new TiXmlText("Top Stories"));
TiXmlElement *linkElement = new TiXmlElement("link");
linkElement->LinkEndChild(new TiXmlText("http://www.ibnlive.com/xml/top.xml"));
newsItem->LinkEndChild(titleElement);
newsItem->LinkEndChild(linkElement);
rssReader->LinkEndChild(newsItem);
newsItem = new TiXmlElement("news-item");
titleElement = new TiXmlElement("title");
titleElement->LinkEndChild(new TiXmlText("World News"));
linkElement = new TiXmlElement("link");
linkElement->LinkEndChild(new TiXmlText("http://www.ibnlive.com/xml/world.xml"));
newsItem->LinkEndChild(titleElement);
newsItem->LinkEndChild(linkElement);
rssReader->LinkEndChild(newsItem);
newsItem = new TiXmlElement("news-item");
titleElement = new TiXmlElement("title");
titleElement->LinkEndChild(new TiXmlText("Business"));
linkElement = new TiXmlElement("link");
linkElement->LinkEndChild(new TiXmlText("http://www.ibnlive.com/xml/business.xml"));
newsItem->LinkEndChild(titleElement);
newsItem->LinkEndChild(linkElement);
rssReader->LinkEndChild(newsItem);
newsItem = new TiXmlElement("news-item");
titleElement = new TiXmlElement("title");
titleElement->LinkEndChild(new TiXmlText("India"));
linkElement = new TiXmlElement("link");
linkElement->LinkEndChild(new TiXmlText("http://www.ibnlive.com/xml/india.xml"));
newsItem->LinkEndChild(titleElement);
newsItem->LinkEndChild(linkElement);
rssReader->LinkEndChild(newsItem);
newsItem = new TiXmlElement("news-item");
titleElement = new TiXmlElement("title");
titleElement->LinkEndChild(new TiXmlText("Latest Articles"));
linkElement = new TiXmlElement("link");
linkElement->LinkEndChild(new TiXmlText("http://www.codeproject.com/webservices/articlerss.aspx?cat=1"));
newsItem->LinkEndChild(titleElement);
newsItem->LinkEndChild(linkElement);
rssReader->LinkEndChild(newsItem);
defaultDoc.LinkEndChild(title);
defaultDoc.LinkEndChild(author);
defaultDoc.LinkEndChild(rssReader);
defaultDoc.SaveFile(configFile);
// ::WriteFile(reinterpret_cast<HANDLE>(fileHandle),text,strlen(text),&numberOfBytesWritten,NULL);
}
//Writes configuration file
void RRPConfigurationManager::writeConfigurationFile() {
TiXmlDocument defaultDoc = TiXmlDocument();
TiXmlComment *title = new TiXmlComment("Configuration file for RSS Reader plugin for IE");
TiXmlComment *author = new TiXmlComment("Author : Prafulla Tekawade(prafulla_t@users.sourceforge.net)");
TiXmlElement *rssReader = new TiXmlElement("rss-reader");
TiXmlElement *newsItem;
int n = newsButtonVector.size();
for(int i = 0;i<n;i++) {
if(newsButtonVector[i]) {
newsItem = new TiXmlElement("news-item");
TiXmlElement *titleElement = new TiXmlElement("title");
titleElement->LinkEndChild(new TiXmlText(newsButtonVector[i]->title));
TiXmlElement *linkElement = new TiXmlElement("link");
linkElement->LinkEndChild(new TiXmlText(newsButtonVector[i]->link));
newsItem->LinkEndChild(titleElement);
newsItem->LinkEndChild(linkElement);
rssReader->LinkEndChild(newsItem);
}
}
defaultDoc.LinkEndChild(title);
defaultDoc.LinkEndChild(author);
defaultDoc.LinkEndChild(rssReader);
defaultDoc.SaveFile(configFile);
}
void RRPConfigurationManager::parse() {
if(doc) {
delete doc;
doc = NULL;
}
doc = new TiXmlDocument(configFile);
doc->LoadFile();
if(doc->Error())
return;
TiXmlElement *rootElement = doc->FirstChildElement();
TiXmlElement *newsItemElement;
TiXmlElement *titleElement,*linkElement;
newsItemElement = rootElement->FirstChildElement("news-item");
//Traversing all news item tags
while(newsItemElement) {
NewsButtonInfo *obj = new NewsButtonInfo();
titleElement = newsItemElement->FirstChildElement("title");
linkElement = newsItemElement->FirstChildElement("link");
obj->title = const_cast<char *>(titleElement->GetText());
obj->link = const_cast<char *>(linkElement->GetText());
if(!obj->title) {
obj->title = new char[10];
strcpy(obj->title,"No-Title");
}
if(!obj->link) {
obj->link = new char[20];
strcpy(obj->link,"http://No-Link");
}
newsButtonVector.push_back(obj);
//std::cout << titleElement->GetText() << std::endl;
//std::cout << linkElement->GetText() << std::endl;
newsItemElement = newsItemElement->NextSiblingElement();
}
}
//Add the news to the News Vector
void RRPConfigurationManager::addNews(char *title,char *link) {
NewsButtonInfo *obj = new NewsButtonInfo();
obj->title = new char[strlen(title) + 1];
strcpy(obj->title,title);
obj->link = new char[strlen(link) + 1];
strcpy(obj->link,link);
newsButtonVector.push_back(obj);
toolbar->addButton(getSize() - 1);
}
//Modifies the news at the specified index
void RRPConfigurationManager::modifyNews(int index,char *title,char *link) {
modified = true;
deleteOnCloseFlag = true;
NewsButtonInfo *obj = newsButtonVector[index];
if(title) {
obj->title = new char[strlen(title) + 1];
strcpy(obj->title,title);
}
if(link) {
obj->link = new char[strlen(link) + 1];
strcpy(obj->link,link);
}
newsButtonVector[index] = obj;
toolbar->changeButtonCaption(index,title);
}
//Removes the news from the specified index
void RRPConfigurationManager::removeNews(unsigned int index) {
unsigned int n = newsButtonVector.size();
if(index < n) {
delete newsButtonVector[index];
newsButtonVector[index] = NULL;
std::vector<NewsButtonInfo *>::iterator it = newsButtonVector.begin();
int i = 0;
while(i != index) {
if(i > newsButtonVector.size())
break;
it++;
i++;
}
newsButtonVector.erase(it);
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Quote : "Life is all about solving problems and enjoying their solutions !! "