summaryrefslogtreecommitdiff
path: root/src/loadstatus.h
blob: 06d515ae3a2dd1aa70ce1e5876f56dd872b7d5ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Minetest-c55
Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef LOADSTATUS_HEADER
#define LOADSTATUS_HEADER

class LoadStatus
{
	bool ready;
	JMutex ready_mutex;
	
	u32 done;
	JMutex done_mutex;

	u32 todo;
	JMutex todo_mutex;

	wchar_t *text;
	JMutex text_mutex;

public:

	LoadStatus(bool a_ready=false, u32 a_done=0, u32 a_todo=0)
	{
		ready = a_ready;
		done = a_done;
		todo = a_todo;
		text = NULL;
		ready_mutex.Init();
		done_mutex.Init();
		todo_mutex.Init();
		text_mutex.Init();
	}

	void setReady(bool a_ready)
	{
		ready_mutex.Lock();
		ready = a_ready;
		ready_mutex.Unlock();
	}

	bool getReady(void)
	{
		ready_mutex.Lock();
		bool a_ready = ready;
		ready_mutex.Unlock();
		return a_ready;
	}

	void setDone(u32 a_done)
	{
		done_mutex.Lock();
		done = a_done;
		done_mutex.Unlock();
	}

	u32 getDone(void)
	{
		done_mutex.Lock();
		u32 a_done = done;
		done_mutex.Unlock();
		return a_done;
	}

	void setTodo(u32 a_todo)
	{
		todo_mutex.Lock();
		todo = a_todo;
		todo_mutex.Unlock();
	}

	u32 getTodo(void)
	{
		todo_mutex.Lock();
		u32 a_todo = todo;
		todo_mutex.Unlock();
		return a_todo;
	}

	/*
		Copies the text if not NULL,
		If NULL; sets text to NULL.
	*/
	void setText(const wchar_t *a_text)
	{
		text_mutex.Lock();
		if(text != NULL)
			free(text);
		if(a_text == NULL){
			text = NULL;
			text_mutex.Unlock();
			return;
		}
		u32 len = wcslen(a_text);
		text = (wchar_t*)malloc(sizeof(wchar_t) * (len+1));
		if(text == NULL) throw;
		swprintf(text, len+1, L"%ls", a_text);
		text_mutex.Unlock();
	}
	
	/*
		Return value must be free'd
		Return value can be NULL
	*/
	wchar_t * getText()
	{
		text_mutex.Lock();
		if(text == NULL){
			text_mutex.Unlock();
			return NULL;
		}
		u32 len = wcslen(text);
		wchar_t *b_text = (wchar_t*)malloc(sizeof(wchar_t) * (len+1));
		if(b_text == NULL) throw;
		swprintf(b_text, len+1, L"%ls", text);
		text_mutex.Unlock();
		return b_text;
	}
	
	/*
		Return value must be free'd
	*/
	wchar_t * getNiceText()
	{
		const wchar_t *defaulttext = L"Loading";
		wchar_t *t = getText();
		u32 maxlen = 20; // " (%i/%i)"
		if(t != NULL)
			maxlen += wcslen(t);
		else
			maxlen += wcslen(defaulttext);
		wchar_t *b_text = (wchar_t*)malloc(sizeof(wchar_t) * (maxlen+1));
		if(b_text == NULL) throw;
		if(t != NULL)
			swprintf(b_text, maxlen+1, L"%ls (%i/%i)",
					t, getDone(), getTodo());
		else
			swprintf(b_text, maxlen+1, L"%ls (%i/%i)",
					defaulttext, getDone(), getTodo());
		if(t != NULL)
			free(t);
		return b_text;
	}
};

#endif