📄 booklibrary.cpp
字号:
if(classRoot == NULL)
return 1;
TiXmlNode* child;
TiXmlNode* subClass;
child = this->FindBookClass(classRoot->ToElement(), bookClass.GetID());
if(NULL == child) // 没有相应的类别
{
throw KXmlFileException("没有指定的图书分类!");
}
for(subClass = child->FirstChild("BookClass");
subClass != NULL;
subClass = subClass->NextSibling("BookClass"))
{
const char* strID = ((TiXmlElement*)subClass)->Attribute("id");
Library::BookClass obj("");
if(strID != NULL)
{
obj.SetID(strID);
}
const char* strName = ((TiXmlElement*)subClass)->Attribute("name");
if(strName != NULL)
{
obj.SetName(strName);
}
const char* strDesc = ((TiXmlElement*)subClass)->Attribute("desc");
if(strDesc != NULL)
{
obj.SetDescription(strDesc);
}
subs.push_back(obj);
}
return 0;
}
/// <summary>得到图书根分类</summary>
/// <param name="bookClasses">[OUT]根分类的名称</param>
/// <returns></returns>
/// <exception>这个函数不抛出异常</exception>
/// <remarks>这些分类名为:"我的电子书", "收藏夹", "最近浏览"
/// </remarks>
int BookLibrary::GetRootBookClass(std::vector<BookClass>& bookClasses)
{
if(classRoot == NULL)
return 1;
// TiXmlNode* child;
TiXmlNode* subClass;
for(subClass = classRoot->FirstChild("BookClass");
subClass != NULL;
subClass = subClass->NextSibling("BookClass"))
{
const char* strID = ((TiXmlElement*)subClass)->Attribute("id");
Library::BookClass obj("");
if(strID != NULL)
{
obj.SetID(strID);
}
const char* strName = ((TiXmlElement*)subClass)->Attribute("name");
if(strName != NULL)
{
obj.SetName(strName);
}
const char* strDesc = ((TiXmlElement*)subClass)->Attribute("desc");
if(strDesc != NULL)
{
obj.SetDescription(strDesc);
}
bookClasses.push_back(obj);
}
return 0;
}
/// <summary>在某个分类下查找一本书(BookItem)</summary>
/// <param name="classElem">[IN]在这个分类下查找</param>
/// <param name="bookID">[IN]图书的ID</param>
/// <returns>找到, 返回BookItem节点的指针; 没有返回NULL</returns>
/// <exception>这个函数不抛出异常</exception>
/// <remarks>通过递归,查找bookID所在的图书分类.删除图书使用了这个递归</remarks>
/// <seealso cref="BookLibrary::DeleteBook">BookLibrary::DeleteBook</seealso>
TiXmlElement* BookLibrary::FindClassOfBook(TiXmlElement* classElem, const std::string& bookID)
{
TiXmlNode* childClass = NULL;
TiXmlNode* bookNode = NULL;
for(childClass = classElem->FirstChild("BookClass");
childClass != NULL;
childClass = childClass->NextSibling("BookClass"))
{
// childClass是否包含指定的图书
bookNode = childClass->FirstChild("BookItem");
for(; bookNode != NULL; bookNode = bookNode->NextSibling("BookItem"))
{
if(bookID == std::string(bookNode->ToElement()->Attribute("id")))
{
//break; // 找到 指定的相应的电子书
return bookNode->ToElement();
}
}
if(bookNode == NULL)
{
TiXmlNode* hr = FindClassOfBook(childClass->ToElement(), bookID);
if(hr != NULL)
return hr->ToElement();
}
}
return NULL;
}
/// <summary>将图书book(ID)添加到收藏夹</summary>
/// <param name="book"><b>[IN]</b>图书(ID)</param>
/// <returns>成功返回0; 失败返回 其他值</returns>
/// <exception>NULL</exception>
/// <remarks></remarks>
int BookLibrary::AddToFavorite(const BookObject& book)
{
// Todo:检查Book是否已在收藏夹中
TiXmlElement bookItem("BookItem");
bookItem.SetAttribute("id", book.GetID());
TiXmlNode* pClassElem = this->classRoot->FirstChild("BookClass");
for(; pClassElem != NULL; pClassElem = pClassElem->NextSibling("BookClass"))
{
if(IDS_FAVORITE == std::string(pClassElem->ToElement()->Attribute("id")))
{
break; // 找到
}
}
if(pClassElem != NULL)
{
pClassElem->InsertEndChild(bookItem);
return 0;
}
else
{
return 1;
}
}
/// <summary>将图书book从收藏夹删除</summary>
/// <param name="book"><b>[IN]</b>图书(ID)</param>
/// <returns>成功返回0; 失败返回 其他值</returns>
/// <exception>NULL</exception>
/// <remarks></remarks>
int BookLibrary::DeleteFromFavorite(const BookObject& book)
{
TiXmlNode* bookItem = NULL;
TiXmlNode* pClassElem = this->classRoot->FirstChild("BookClass");
for(; pClassElem != NULL; pClassElem = pClassElem->NextSibling("BookClass"))
{
if(IDS_FAVORITE == std::string(pClassElem->ToElement()->Attribute("id")))
{
break; // 找到
}
}
if(pClassElem != NULL) // 找到收藏家
{
bookItem = pClassElem->FirstChild("BookItem");
for(; bookItem != NULL; bookItem = bookItem->NextSibling("BookItem"))
{
if(book.GetID() == std::string(bookItem->ToElement()->Attribute("id")))
{
break; // 找到
}
}
if(bookItem != NULL)
{
pClassElem->RemoveChild(bookItem);
return 0;
}
}
else
{
return 1;
}
return 0;
}
/// <summary>当打开图书时, 执行需要处理的任务</summary>
/// <param name="bookID"><b>[IN]</b>打开的图书的ID</param>
/// <returns>成功返回0; 失败返回 其他值</returns>
/// <remarks>将执行以下任务:
/// <list type="bullet">
/// <item><description>
/// 记录打开操作, 记录时间和图书的打开次数, 保存信息到"最近浏览"
/// </description></item>
/// </list>
/// </remarks>
int BookLibrary::OnOpenBook(const Library::BookObject &bookID)
{
// 查找"最近浏览"根节点
TiXmlNode* pRecentRoot = this->classRoot->FirstChild("BookClass");
for(; pRecentRoot != NULL; pRecentRoot = pRecentRoot->NextSibling("BookClass"))
{
if(IDS_RECENT == std::string(pRecentRoot->ToElement()->Attribute("id")))
{
break; // 找到
}
}
// TODO: 没有"最近浏览"根节点, 需要新建
if(pRecentRoot == NULL)
{
return 1;
}
// 查找 bookID, 区分两种情况
TiXmlNode* pBookItem = pRecentRoot->FirstChild("BookItem");
for(; pBookItem != NULL; pBookItem = pBookItem->NextSibling("BookItem"))
{
if(bookID.GetID() == std::string(pBookItem->ToElement()->Attribute("id")))
{
break; // 找到
}
}
if(pBookItem == NULL) // 没有这个ID, 这本书是第一次打开
{
// 当前时间
time_t now = time(NULL);
std::string strNow;
strNow = ToString<char>(now);
// 一项
Library::RecentItem recentItem;
recentItem.SetID(bookID.GetID());
recentItem.SetLastTime(strNow);
recentItem.SetCount( 1 );
// 更新
TiXmlElement newBookItem("BookItem");
recentItem.SaveToXml(&newBookItem);
pRecentRoot->InsertEndChild(newBookItem);
}
else // 找到ID, 更新这本书的lasttime 和 count
{
time_t now = time(NULL);
std::string strNow;
strNow = ToString<char>(now);
// 一项
Library::RecentItem recentItem;
recentItem.LoadFromXml((TiXmlElement*) pBookItem);
recentItem.SetLastTime(strNow);
recentItem.SetCount( recentItem.GetCount() + 1 );
// 更新
recentItem.SaveToXml((TiXmlElement*) pBookItem);
}
//
if(!this->doc.SaveFile())
{
throw KXmlFileException("无法删除指定书:更新数据文件失败");
}
return 0;
}
/// <summary>得到最近浏览的信息</summary>
/// <param name="recentItems"><b>[OUT]</b>保存得到的信息</param>
/// <returns>成功返回0; 失败返回 其他值</returns>
/// <remarks>得到的信息包括:图书的ID, 最后打开时间, 打开次数
/// </remarks>
int BookLibrary::GetRecentInfo(std::vector<RecentItem>& recentItems)
{
TiXmlNode* pRecentRoot = this->classRoot->FirstChild("BookClass");
for(; pRecentRoot != NULL; pRecentRoot = pRecentRoot->NextSibling("BookClass"))
{
if(IDS_RECENT == std::string(pRecentRoot->ToElement()->Attribute("id")))
{
break; // 找到
}
}
// 没有"最近浏览"根节点
if(pRecentRoot == NULL)
{
return 1;
}
// 查找 bookID, 区分两种情况
TiXmlNode* pBookItem = pRecentRoot->FirstChild("BookItem");
for(; pBookItem != NULL; pBookItem = pBookItem->NextSibling("BookItem"))
{
Library::RecentItem item;
item.LoadFromXml((TiXmlElement*)pBookItem);
recentItems.push_back(item);
}
return 0;
}
} // Library
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -