/***********************************************************************************************************
 * 程序作者:赵进军
 * 函数功能:删除XML文件中指定的节点的最后一个节点
 * 参数说明:
 * delNodeName:需要删除的节点的名称
 *   nodeIndex:需要删除的节点的位置
 * 注意事项:null
 * 修改日期:2015/12/12 16:49:00
 ***********************************************************************************************************/
bool OperationProfile_XML::DeleteNodeByNameIndex(char* delNodeName, int nodeIndex)
{
	if (nodeIndex >= groupNodeCount)
		printf("当前删除的节点位置不存在!");

	if (!OperationProfile_XML::XMLExits())
		return false;

	TiXmlDocument* myDocument = new TiXmlDocument();

	if (NULL == myDocument)
		return false;
	myDocument->LoadFile(IOperationProfile::ProfileAddress);

	TiXmlElement* pRootEle = myDocument->RootElement();
	if (NULL == pRootEle)
		return false;

	TiXmlElement *pNode = NULL;

	if (arrayIndex != 0)
		arrayIndex = 0;
	if (!GetNodePointerByName(pRootEle, delNodeName, pNode, nodeIndex))
		return false;

	if (pRootEle == pNode)
	{
		if (myDocument->RemoveChild(pRootEle))
		{
			myDocument->SaveFile(IOperationProfile::ProfileAddress);
			return true;
		}
		else
		{
			return false;
		}
	}

	if (NULL != pNode)
	{
		TiXmlNode* pParNode = pNode->Parent();
		if (NULL == pParNode)
			return false;

		TiXmlElement* pParentEle = pParNode->ToElement();
		if (NULL != pParentEle)
		{
			if (pParentEle->RemoveChild(pNode))
				myDocument->SaveFile(IOperationProfile::ProfileAddress);
			else
				return false;
		}
	}
	else
	{
		return false;
	}
	return false;
}
/***********************************************************************************************************
 * 程序作者:赵进军
 * 函数功能:删除XML文件中所有符合条件的节点
 * 参数说明:
 * strNodeName:需要删除的节点指针的名称
 * 注意事项:null
 * 修改日期:2015/12/13 14:52:00
 ***********************************************************************************************************/
bool OperationProfile_XML::DeleteNodeByNameAll(char* delNodeName)
{
	if (!OperationProfile_XML::XMLExits())
		return false;

	TiXmlDocument* myDocument = new TiXmlDocument();

	if (NULL == myDocument)
		return false;
	myDocument->LoadFile(IOperationProfile::ProfileAddress);

	TiXmlElement* pRootEle = myDocument->RootElement();
	if (NULL == pRootEle)
		return false;

	if (arrayIndex != 0)
		arrayIndex = 0;
	TiXmlElement* pNodes[] = { NULL, NULL };																		// 该处设计存在缺陷,无法定义动态数组
	GetNodePointerByNameAll(pRootEle, delNodeName, pNodes);


	if (pRootEle == pNodes[0])
	{
		if (myDocument->RemoveChild(pRootEle))
		{
			myDocument->SaveFile(IOperationProfile::ProfileAddress);
			return true;
		}
		else
		{
			return false;
		}
	}

	if (NULL != pNodes)
	{
		for (int i = 0; i < groupNodeCount; i++)
		{
			TiXmlNode* pParNode = pNodes[i]->Parent();
			if (NULL == pParNode)
				return false;

			TiXmlElement* pParentEle = pParNode->ToElement();
			if (NULL != pParentEle)
				if (pParentEle->RemoveChild(pNodes[i]))
					myDocument->SaveFile(IOperationProfile::ProfileAddress);
		}
	}
	else
	{
		return false;
	}
	return false;
}
Esempio n. 3
0
void CAppManager::UpdateAppStat(const std::string& AppId)
{
  CStdString fileName = _P("special://profile/apps/apps.xml");
  CStdString strValue, currAppStr;
  CStdString tmp;
  CStdString appsPath = _P("special://home/apps/");
  CAppDescriptor::AppDescriptorsMap installedAppsDesc;

  GetInstalledAppsInternal(installedAppsDesc, appsPath, "", false);

  TiXmlDocument xmlDoc;
  TiXmlElement *pRootElement = NULL;
  TiXmlNode *pTempNode = NULL;
  bool fixDoc = true;

  CLog::Log(LOGINFO, "updating %s's information in apps.xml", AppId.c_str());

  if ( xmlDoc.LoadFile( fileName) )
  {
    pRootElement = xmlDoc.RootElement();
    if (pRootElement)
    {
      strValue = pRootElement->Value();
      if ( strValue == "apps")
      {
        fixDoc = false;
      }
    }
  }

  if (fixDoc)
  {
    if (pRootElement)
    {
      xmlDoc.RemoveChild(pRootElement);
    }
    else
    {
      pTempNode = xmlDoc.FirstChild();
      if (pTempNode > 0)
        xmlDoc.RemoveChild(pTempNode);
    }

    pRootElement = new TiXmlElement( "apps" );
    pRootElement->SetAttribute("version", "1.0");
    xmlDoc.LinkEndChild(pRootElement);
  }

  TiXmlNode *pAppNode = pRootElement->FirstChild("app");
  TiXmlNode *pOpenedCntNode = NULL, *pIdNode = NULL, *pLastOpenedDateNode = NULL;

  while (pAppNode > 0)
  {
    pIdNode = pAppNode->FirstChild("id");
    if (pIdNode && pIdNode->FirstChild())
    {
      currAppStr = pIdNode->FirstChild()->Value();
      if (currAppStr == AppId)
      {
        pLastOpenedDateNode = pAppNode->FirstChild("lastopeneddate");
        pOpenedCntNode = pAppNode->FirstChild("timesopened");
        if (pOpenedCntNode && pOpenedCntNode->FirstChild())
        {

          int openedCnt = atoi (pOpenedCntNode->FirstChild()->Value());
          openedCnt++;
          tmp = BOXEE::BXUtils::IntToString(openedCnt);

          pOpenedCntNode->FirstChild()->SetValue(tmp.c_str());
          //CLog::Log(LOGDEBUG,"    Found name: %s", strName.c_str());
        }
        else
        {
          if (pOpenedCntNode)
          {
            pAppNode->RemoveChild(pOpenedCntNode);
          }
          TiXmlElement * timesOpenedElement = new TiXmlElement( "timesopened" );
          TiXmlText * timesOpenedText = new TiXmlText( "1" );
          timesOpenedElement->LinkEndChild( timesOpenedText );
          pAppNode->LinkEndChild(timesOpenedElement);
        }
        if (pLastOpenedDateNode)
        {
          pAppNode->RemoveChild(pLastOpenedDateNode);
        }

        tmp = BOXEE::BXUtils::IntToString(std::time(NULL));
        TiXmlElement * lastOpenedElement = new TiXmlElement( "lastopeneddate" );
        TiXmlText * lastOpenedText = new TiXmlText(tmp.c_str());
        lastOpenedElement->LinkEndChild( lastOpenedText );
        pAppNode->LinkEndChild(lastOpenedElement);
      }

      CLog::Log(LOGDEBUG, "deleting %s from apps map\n", currAppStr.c_str());
      installedAppsDesc.erase(currAppStr);
    }
    pAppNode = pAppNode->NextSiblingElement("app");

  }

  tmp = BOXEE::BXUtils::IntToString(std::time(NULL));

  CAppDescriptor::AppDescriptorsMap::iterator it = installedAppsDesc.begin();
  for (; it != installedAppsDesc.end(); it++)
  {
    TiXmlElement * appElement = new TiXmlElement( "app" );
    pRootElement->LinkEndChild(appElement);

    TiXmlElement * appIdElement = new TiXmlElement( "id" );
    TiXmlText * appIdText = new TiXmlText(it->first);
    appIdElement->LinkEndChild( appIdText );
    appElement->LinkEndChild(appIdElement);

    TiXmlElement * timesOpenedElement = new TiXmlElement( "timesopened" );
    TiXmlText * timesOpenedText = new TiXmlText( "1" );
    timesOpenedElement->LinkEndChild( timesOpenedText );
    appElement->LinkEndChild(timesOpenedElement);

    TiXmlElement * lastOpenedElement = new TiXmlElement( "lastopeneddate" );
    TiXmlText * lastOpenedText = new TiXmlText(tmp.c_str());
    lastOpenedElement->LinkEndChild( lastOpenedText );
    appElement->LinkEndChild(lastOpenedElement);

    CLog::Log(LOGINFO, "adding %s to app.xml file\n", it->first.c_str());

  }

  xmlDoc.SaveFile();

  m_mapAppNameToStat.clear();
  pRootElement = xmlDoc.RootElement();
  pAppNode = pRootElement->FirstChild("app");

  CLog::Log(LOGDEBUG, "reading apps.xml file");
  // read the content of app.xml file
  while (pAppNode > 0)
  {
    pIdNode = pAppNode->FirstChild("id");
    currAppStr = pIdNode->FirstChild()->Value();
    pLastOpenedDateNode = pAppNode->FirstChild("lastopeneddate");
    pOpenedCntNode = pAppNode->FirstChild("timesopened");
    int lastOpenedTime = 1;
    int timesOpened = 1;
    if (pOpenedCntNode && pLastOpenedDateNode->FirstChild())
    {
      lastOpenedTime = BOXEE::BXUtils::StringToInt(pLastOpenedDateNode->FirstChild()->Value());
    }
    if (pOpenedCntNode && pOpenedCntNode->FirstChild())
    {
      timesOpened = BOXEE::BXUtils::StringToInt(pOpenedCntNode->FirstChild()->Value());
    }
    std::map<CStdString, int> statsMap;
    statsMap["lastopeneddate"] = lastOpenedTime;
    statsMap["timesopened"] = timesOpened;
    m_mapAppNameToStat[currAppStr] = statsMap;
    pAppNode = pAppNode->NextSiblingElement("app");
  }
}