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