| 1 | cycrow | 1 | #include <spk.h>
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | using namespace System;
 | 
        
           |  |  | 4 | using namespace Runtime::InteropServices;
 | 
        
           |  |  | 5 | using namespace System::Drawing;
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | #define CONNECTERROR_UNKNOWN	-1
 | 
        
           |  |  | 8 | enum {CONNECTERROR_NONE, CONNECTERROR_TIMEOUT, CONNECTERROR_NOFILE, CONNECTERROR_FAILED};
 | 
        
           |  |  | 9 |   | 
        
           | 39 | cycrow | 10 | inline Utils::String _S(System::String ^str)
 | 
        
           |  |  | 11 | {
 | 
        
           |  |  | 12 | 	if ( !str || !str->Length )
 | 
        
           |  |  | 13 | 		return "";
 | 
        
           |  |  | 14 | 	const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(str)).ToPointer();
 | 
        
           |  |  | 15 | 	std::string s = chars;
 | 
        
           |  |  | 16 | 	Marshal::FreeHGlobal(IntPtr((void*)chars));
 | 
        
           |  |  | 17 | 	return s;
 | 
        
           |  |  | 18 | }
 | 
        
           | 1 | cycrow | 19 | inline CyString CyStringFromSystemString( System::String ^str)
 | 
        
           |  |  | 20 | {
 | 
        
           |  |  | 21 | 	if ( !str || !str->Length )
 | 
        
           |  |  | 22 | 		return NullString;
 | 
        
           |  |  | 23 | 	const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(str)).ToPointer();
 | 
        
           |  |  | 24 | 	std::string s = chars;
 | 
        
           |  |  | 25 | //	CyString *newStr = new CyString(chars);
 | 
        
           |  |  | 26 | 	Marshal::FreeHGlobal(IntPtr((void*)chars));
 | 
        
           |  |  | 27 | //	return *newStr;
 | 
        
           |  |  | 28 | 	return CyString(s);
 | 
        
           |  |  | 29 | }
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | inline int CheckWebFileExists(String ^url)
 | 
        
           |  |  | 32 | {
 | 
        
           |  |  | 33 | 	int ret = CONNECTERROR_UNKNOWN;
 | 
        
           |  |  | 34 | 	try 
 | 
        
           |  |  | 35 | 	{
 | 
        
           |  |  | 36 | 		// check for the file
 | 
        
           |  |  | 37 | 		Net::WebRequest ^check = (Net::HttpWebRequest ^)Net::WebRequest::Create(url);
 | 
        
           |  |  | 38 | 		check->Credentials = Net::CredentialCache::DefaultCredentials;
 | 
        
           |  |  | 39 | 		Net::WebResponse ^response = (Net::HttpWebResponse ^)check->GetResponse();
 | 
        
           |  |  | 40 | 		// check the file size
 | 
        
           |  |  | 41 | 		__int64 fileSize = response->ContentLength;
 | 
        
           |  |  | 42 | 		if ( fileSize )
 | 
        
           |  |  | 43 | 			ret = CONNECTERROR_NONE;
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | 		response->Close();
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 | 	}
 | 
        
           |  |  | 48 | 	catch (System::Net::WebException ^ex)
 | 
        
           |  |  | 49 | 	{
 | 
        
           |  |  | 50 | 		if ( ex->Status == System::Net::WebExceptionStatus::ConnectFailure )
 | 
        
           |  |  | 51 | 			ret = CONNECTERROR_FAILED;
 | 
        
           | 121 | cycrow | 52 | 		else if(ex->Status == System::Net::WebExceptionStatus::ConnectionClosed && !ex->Response)
 | 
        
           |  |  | 53 | 			ret = CONNECTERROR_FAILED;
 | 
        
           | 1 | cycrow | 54 | 		else
 | 
        
           |  |  | 55 | 		{
 | 
        
           |  |  | 56 | 			switch ( cli::safe_cast<Net::HttpWebResponse ^>(ex->Response)->StatusCode )
 | 
        
           |  |  | 57 | 			{
 | 
        
           |  |  | 58 | 				case Net::HttpStatusCode::NotFound:
 | 
        
           |  |  | 59 | 					ret = CONNECTERROR_NOFILE;
 | 
        
           |  |  | 60 | 					break;
 | 
        
           |  |  | 61 | 				case Net::HttpStatusCode::RequestTimeout:
 | 
        
           |  |  | 62 | 					ret = CONNECTERROR_TIMEOUT;
 | 
        
           |  |  | 63 | 					break;
 | 
        
           |  |  | 64 | 				default:
 | 
        
           |  |  | 65 | 					ret = CONNECTERROR_UNKNOWN;
 | 
        
           |  |  | 66 | 			}
 | 
        
           |  |  | 67 | 		}
 | 
        
           |  |  | 68 | 	}
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 | 	return ret;
 | 
        
           |  |  | 71 | }
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 | inline String ^ReadDataFromWeb(String ^url)
 | 
        
           |  |  | 74 | {
 | 
        
           |  |  | 75 | 	bool ret = false;
 | 
        
           |  |  | 76 | 	String ^data;
 | 
        
           |  |  | 77 | 	try 
 | 
        
           |  |  | 78 | 	{
 | 
        
           |  |  | 79 | 		System::Net::WebClient ^Client = gcnew System::Net::WebClient();
 | 
        
           |  |  | 80 | 		Client->Headers->Add( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" );
 | 
        
           |  |  | 81 | 		System::IO::Stream ^strm = Client->OpenRead(url);
 | 
        
           |  |  | 82 | 		System::IO::StreamReader ^sr = gcnew System::IO::StreamReader(strm);
 | 
        
           |  |  | 83 | 		data = sr->ReadToEnd();
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 | 		strm->Close();
 | 
        
           |  |  | 86 | 		sr->Close();
 | 
        
           |  |  | 87 | 	}
 | 
        
           |  |  | 88 | 	catch (System::Net::WebException ^)
 | 
        
           |  |  | 89 | 	{
 | 
        
           |  |  | 90 | 		return nullptr;
 | 
        
           |  |  | 91 | 	}
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 | 	if ( data && data->Length )
 | 
        
           |  |  | 94 | 		return data;
 | 
        
           |  |  | 95 | 	return nullptr;
 | 
        
           |  |  | 96 | }
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 | inline System::String ^SystemStringFromCyString ( CyString str )
 | 
        
           |  |  | 99 | {
 | 
        
           |  |  | 100 | 	System::String ^Str = gcnew System::String(str.c_str());
 | 
        
           |  |  | 101 | 	return Str;
 | 
        
           |  |  | 102 | }
 | 
        
           | 39 | cycrow | 103 | inline System::String ^_US(const Utils::String &str)
 | 
        
           |  |  | 104 | {
 | 
        
           |  |  | 105 | 	System::String ^Str = gcnew System::String(str.c_str());
 | 
        
           |  |  | 106 | 	return Str;
 | 
        
           |  |  | 107 | }
 | 
        
           | 1 | cycrow | 108 |   | 
        
           |  |  | 109 | inline System::Drawing::Bitmap ^LoadIcon(CBaseFile *p)
 | 
        
           |  |  | 110 | {
 | 
        
           |  |  | 111 | 	CyString file = p->GetIcon()->GetFilePointer();
 | 
        
           |  |  | 112 | 	if ( !file.Empty() )
 | 
        
           |  |  | 113 | 	{
 | 
        
           |  |  | 114 | 		file = file.FindReplace("/", "\\").FindReplace("\\\\", "\\");
 | 
        
           |  |  | 115 | 		bool doIcon = false;
 | 
        
           |  |  | 116 | 		System::String ^sFile = SystemStringFromCyString(file);
 | 
        
           |  |  | 117 | 		if ( System::IO::File::Exists(sFile) )
 | 
        
           |  |  | 118 | 		{
 | 
        
           |  |  | 119 | 			Bitmap ^myBitmap = gcnew Bitmap(sFile);
 | 
        
           |  |  | 120 | 			return myBitmap;
 | 
        
           |  |  | 121 | 		}
 | 
        
           |  |  | 122 | 	}
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 | 	return nullptr;
 | 
        
           |  |  | 125 | }
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 | inline System::String ^GetProgramVersionString(float version, int beta)
 | 
        
           |  |  | 128 | {
 | 
        
           |  |  | 129 | 	System::String ^str = "V" + SystemStringFromCyString(CyString::CreateFromFloat(version, 2));
 | 
        
           |  |  | 130 | 	// beta version
 | 
        
           |  |  | 131 | 	if ( beta > 0 )
 | 
        
           |  |  | 132 | 		str += " (Beta " + beta + ")";
 | 
        
           |  |  | 133 | 	// RC release
 | 
        
           |  |  | 134 | 	else if ( beta < 0 )
 | 
        
           |  |  | 135 | 		str += " (RC " + (0 - beta) + ")";
 | 
        
           |  |  | 136 | 	return str;
 | 
        
           |  |  | 137 | }
 | 
        
           |  |  | 138 |   |