util.cpp 6.79 KB
#include <windows.h>
#include  "util.h"



Util::Util(){
	//downloading = true;
	//downloading = down;	
}

string Util::clrStringTostring(String^ strIn)
{

	char cStr[256];

	
  		sprintf(cStr, "%s", strIn);
		string stlString(cStr);

	return stlString;
}

 void DownloadProgressCallback(Object^ sender, 
          DownloadProgressChangedEventArgs^ e)
      {
          // Displays the operation identifier, and the transfer progress.
          Console::WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...", 
              (String ^)e->UserState, 
              e->BytesReceived, 
              e->TotalBytesToReceive,
              e->ProgressPercentage);
      }

 void DownloadFileCallBack2(Object^ sender, AsyncCompletedEventArgs^ args)
    {
        cout<<"Terminou de baixar"<< endl;
      
    }
        

bool Util::checkNet()
{

	try
	{
	   
		WebRequest^ request = WebRequest::Create( "http://vlibras.lavid.ufpb.br" );

   // If required by the server, set the credentials.
   		//request->Credentials = CredentialCache::DefaultCredentials;

   // Get the response.
  		 HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());

		if (response->StatusDescription == "OK"){
			
			return true;
		}
		return false;
		   response->Close();
		}
	catch ( WebException^ e ) 
	{
		return false;
	}  
   
}


vector<string>  Util::checkVersion(bool atualize)
{
	XmlDocument^ xmlDoc;
	XmlNodeList^ files;
	XmlDocument^ xmlDocLocal;
	XmlNodeList^ filesLocal;
	vector<string> updates;
	vector<string> localversion = localVersion();
	try
	{
		xmlDoc = (gcnew XmlDocument());
		xmlDocLocal = (gcnew XmlDocument());

		
		string url = "http://vlibras.lavid.ufpb.br/api/dicionario/"+localversion[3]+"?type=windows";
	   WebRequest^ request = WebRequest::Create( gcnew String(url.c_str()));
	   // If required by the server, set the credentials.
	   request->Credentials = CredentialCache::DefaultCredentials;
	   // Get the response.
	   HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
		if (!(response->StatusDescription == "OK"))
			goto erro;	
		// Get the stream containing content returned by the server.
		Stream^ dataStream = response->GetResponseStream();
	   // Open the stream using a StreamReader for easy access.
	   StreamReader^ reader = gcnew StreamReader( dataStream );
	   // Read the content.
	   String^ responseFromServer = reader->ReadToEnd();
	   xmlDoc->LoadXml(responseFromServer);
		files = xmlDoc->GetElementsByTagName("version");
		
		long numitems = files->Count;

		
		
		//for (int i=0;i<numitems;i++)
		if(numitems>0)
		{	
			String^ playerVersion = files[0]["playerVersion"]->InnerText;
			String^ dictionaryVersion = files[0]["dictionaryVersion"]->InnerText;

			if(String::Compare( playerVersion, gcnew String(localversion[1].c_str())))
			{	
				updates.push_back("playerVersion");
				updates.push_back(clrStringTostring(playerVersion));
				updates.push_back(clrStringTostring(files[0]["playerUrl"]->InnerText));
			}
			if(String::Compare(dictionaryVersion,gcnew String(localversion[3].c_str())))
			{
				updates.push_back("dictionaryVersion");
				updates.push_back(clrStringTostring(dictionaryVersion));
				updates.push_back(clrStringTostring(files[0]["dictionaryUrl"]->InnerText));
			}

			//if(atualize)
				//saveNewXmlVersion(playerVersion,dictionaryVersion);	
				
		}	

	   erro:
	   // Cleanup the streams and the response.
	   reader->Close();
	   dataStream->Close();
	}catch(Exception^ e)
	{
		return updates;
	}   

	return updates;
}


vector<string>  Util::localVersion()
{
	XmlDocument^ xmlDocLocal;
	XmlNodeList^ filesLocal;
	vector<string> updates;
	try
	{
		xmlDocLocal = (gcnew XmlDocument());
		//xml version local
		StreamReader^ readerLocal = gcnew StreamReader("c:\\vlibras-libs\\update\\updateVersion.xml");
	   
	   String^ responseLocal = readerLocal->ReadToEnd();

	   xmlDocLocal->LoadXml(responseLocal);
		
		filesLocal = xmlDocLocal->GetElementsByTagName("version");

		updates.push_back("playerVersion");
		String^ playerVersion = filesLocal[0]["playerVersion"]->InnerText;
		updates.push_back(clrStringTostring(playerVersion));
		updates.push_back("dictionaryVersion");
		String^ dictionaryVersion = filesLocal[0]["dictionaryVersion"]->InnerText;
		updates.push_back(clrStringTostring(dictionaryVersion));
			
	   // Cleanup the streams and the response.
	   readerLocal->Close();
	}catch(Exception^ e)
	{
		return updates;
	}   

	return updates;
}


string Util::Download(DownloadProgressChangedEventHandler^ dpch ,string url,  string fileName)
{
	try
	{
		myWebClient = gcnew WebClient();
		Uri^ uri = gcnew Uri(gcnew String(url.c_str()));
		myWebClient->DownloadProgressChanged += dpch;
		myWebClient->DownloadFileCompleted += gcnew AsyncCompletedEventHandler( DownloadFileCallBack2 );
		String^ outPath = gcnew String(("c:\\vlibras-libs\\update\\"+fileName).c_str());
		myWebClient->DownloadFileAsync(uri, outPath);
	}catch (Exception^ e)
	{
		return "FALHA";
	}

	return "OK";	 
 }



 bool Util::saveNewXmlVersion(string playerVersion,string dictionaryVersion)
 {
	
 	XmlDocument^ xmlDocLocal;
	XmlNodeList^ filesLocal;
	vector<string> updates;
	try
	{
		xmlDocLocal = (gcnew XmlDocument());
		//xml version local
		StreamReader^ readerLocal = gcnew StreamReader("c:\\vlibras-libs\\update\\updateVersion.xml");
	   
	   String^ responseLocal = readerLocal->ReadToEnd();

	   xmlDocLocal->LoadXml(responseLocal);
		
		filesLocal = xmlDocLocal->GetElementsByTagName("version");

		if(!playerVersion.empty())
			filesLocal[0]["playerVersion"]->InnerText = gcnew String(playerVersion.c_str());
		
		if(!dictionaryVersion.empty())
			filesLocal[0]["dictionaryVersion"]->InnerText = gcnew String(dictionaryVersion.c_str());
		
			
	   // Cleanup the streams and the response.
	   readerLocal->Close();

	   xmlDocLocal->Save("c:\\vlibras-libs\\update\\updateVersion.xml");
	}catch(Exception^ e)
	{
		
	}   

	return true;
 }


 bool Util::extractZip(string zipFile, string outPath)
 {
 	 		try
            {
            	String^ zipPath = gcnew String(zipFile.c_str());
    			String^ extractPath = gcnew String(outPath.c_str());
            	ZipFile::ExtractToDirectory(zipPath, extractPath);

        	}catch(Exception^ e)
        	{
        		return false;
        	}

 	return true;
 }

bool Util::backup()
{
	CHAR nome_atual[] = "c:\\vlibras-libs";
  // novo nome do diretório
  CHAR nome_novo[] = "c:\\vlibras-libs-BK";
  
  // renomear o diretório
  if(MoveFile(nome_atual, nome_novo)){
                        
  }
  else{
   return false;    
  }
	return true;
}
bool Util::restore()
{
	CHAR nome_atual[] = "c:\\vlibras-libs-BK";
  // novo nome do diretório
  CHAR nome_novo[] = "c:\\vlibras-libs";
  
  //  renomear o diretório
  if(MoveFile(nome_atual, nome_novo)){
    return true;                      
  }
  else{
    return false;
      //GetLastError() << endl;     
  }
	return true;
}