Subversion Repositories spk

Rev

Rev 298 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#include <spk.h>
187 cycrow 2
#include "Utils/WString.h"
1 cycrow 3
 
4
using namespace System;
5
using namespace Runtime::InteropServices;
6
using namespace System::Drawing;
7
 
8
#define CONNECTERROR_UNKNOWN	-1
9
enum {CONNECTERROR_NONE, CONNECTERROR_TIMEOUT, CONNECTERROR_NOFILE, CONNECTERROR_FAILED};
10
 
187 cycrow 11
inline Utils::WString _WS(System::String^ str)
12
{
13
	if (!str || !str->Length)
14
		return L"";
15
	const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(str)).ToPointer();
16
	std::wstring s = chars;
17
	Marshal::FreeHGlobal(IntPtr((void*)chars));
18
	return s;
19
}
1 cycrow 20
inline int CheckWebFileExists(String ^url)
21
{
22
	int ret = CONNECTERROR_UNKNOWN;
23
	try 
24
	{
25
		// check for the file
26
		Net::WebRequest ^check = (Net::HttpWebRequest ^)Net::WebRequest::Create(url);
27
		check->Credentials = Net::CredentialCache::DefaultCredentials;
28
		Net::WebResponse ^response = (Net::HttpWebResponse ^)check->GetResponse();
29
		// check the file size
30
		__int64 fileSize = response->ContentLength;
31
		if ( fileSize )
32
			ret = CONNECTERROR_NONE;
33
 
34
		response->Close();
35
 
36
	}
37
	catch (System::Net::WebException ^ex)
38
	{
39
		if ( ex->Status == System::Net::WebExceptionStatus::ConnectFailure )
40
			ret = CONNECTERROR_FAILED;
121 cycrow 41
		else if(ex->Status == System::Net::WebExceptionStatus::ConnectionClosed && !ex->Response)
42
			ret = CONNECTERROR_FAILED;
1 cycrow 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
 
187 cycrow 87
inline System::String^ _US(const Utils::WString& str)
39 cycrow 88
{
187 cycrow 89
	System::String^ Str = gcnew System::String(str.c_str());
39 cycrow 90
	return Str;
91
}
1 cycrow 92
 
93
inline System::Drawing::Bitmap ^LoadIcon(CBaseFile *p)
94
{
197 cycrow 95
	Utils::WString file = p->icon()->filePointer();
129 cycrow 96
	if (!file.empty())
1 cycrow 97
	{
197 cycrow 98
		file = file.findReplace(L"/", L"\\").findReplace(L"\\\\", L"\\");
1 cycrow 99
		bool doIcon = false;
129 cycrow 100
		System::String ^sFile = _US(file);
1 cycrow 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
{
197 cycrow 113
	System::String ^str = "V" + _US(Utils::WString::FromFloat(version, 2));
1 cycrow 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