aboutsummaryrefslogtreecommitdiff
path: root/lib/service/listboxservice.cpp
blob: 19ff69333907d8ae6dbe6aaabb7b2df2c9798110 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <lib/service/listboxservice.h>
#include <lib/service/service.h>

void eListboxServiceContent::setRoot(const eServiceReference &root)
{
	m_list.clear();
	m_root = root;
	
	assert(m_service_center);
	
	ePtr<iListableService> lst;
	if (m_service_center->list(m_root, lst))
		eDebug("no list available!");
	else
		if (lst->getContent(m_list))
			eDebug("getContent failed");

	m_size = m_list.size();
	cursorHome();
	
	if (m_listbox)
		m_listbox->entryReset();
}

void eListboxServiceContent::getCurrent(eServiceReference &ref)
{
	if (cursorValid())
		ref = *m_cursor;
	else
		ref = eServiceReference();
}

void eListboxServiceContent::initMarked()
{
	m_marked.clear();
}

void eListboxServiceContent::addMarked(const eServiceReference &ref)
{
	m_marked.insert(ref);
	if (m_listbox)
		m_listbox->entryChanged(lookupService(ref));
}

void eListboxServiceContent::removeMarked(const eServiceReference &ref)
{
	m_marked.erase(ref);
	if (m_listbox)
		m_listbox->entryChanged(lookupService(ref));
}

int eListboxServiceContent::isMarked(const eServiceReference &ref)
{
	return m_marked.find(ref) != m_marked.end();
}

void eListboxServiceContent::markedQueryStart()
{
	m_marked_iterator = m_marked.begin();
}

int eListboxServiceContent::markedQueryNext(eServiceReference &ref)
{
	if (m_marked_iterator == m_marked.end())
		return -1;
	ref = *m_marked_iterator++;
	return 0;
}

int eListboxServiceContent::lookupService(const eServiceReference &ref)
{
		/* shortcut for cursor */
	if (ref == *m_cursor)
		return m_cursor_number;
		/* otherwise, search in the list.. */
	int index = 0;
	for (list::const_iterator i(m_list.begin()); i != m_list.end(); ++i, ++index);
	
		/* this is ok even when the index was not found. */
	return index;
}

void eListboxServiceContent::setVisualMode(int mode)
{
	m_visual_mode = mode;
	
	if (m_visual_mode == visModeSimple)
	{
		m_element_position[celServiceName] = eRect(ePoint(0, 0), m_itemsize);
		m_element_font[celServiceName] = new gFont("Arial", 23);
		m_element_position[celServiceNumber] = eRect();
		m_element_font[celServiceNumber] = 0;
		m_element_position[celIcon] = eRect();
		m_element_position[celServiceInfo] = eRect();
		m_element_font[celServiceInfo] = 0;
	}
}

void eListboxServiceContent::setElementPosition(int element, eRect where)
{
	if ((element >= 0) && (element < celElements))
		m_element_position[element] = where;
}

void eListboxServiceContent::setElementFont(int element, gFont *font)
{
	if ((element >= 0) && (element < celElements))
		m_element_font[element] = font;
}

void eListboxServiceContent::sort()
{
	ePtr<iListableService> lst;
  if (!m_service_center->list(m_root, lst))
  {
		m_list.sort(iListableServiceCompare(lst));
			/* FIXME: is this really required or can we somehow keep the current entry? */
		cursorHome();
		if (m_listbox)
			m_listbox->entryReset();
	}
}

DEFINE_REF(eListboxServiceContent);

eListboxServiceContent::eListboxServiceContent()
	:m_visual_mode(visModeSimple), m_size(0), m_current_marked(false), m_swap(m_list.end())
{
	cursorHome();
	eServiceCenter::getInstance(m_service_center);
}

void eListboxServiceContent::cursorHome()
{
	list::iterator old = m_cursor;

	m_cursor = m_list.begin();
	m_cursor_number = 0;

	if ( m_current_marked && m_saved_cursor == m_list.end() )
		std::iter_swap( old, m_cursor );
}

void eListboxServiceContent::cursorEnd()
{
	if ( m_current_marked && m_saved_cursor == m_list.end() && m_cursor != m_list.end() )
		m_swap = m_cursor;
	m_cursor = m_list.end();
	m_cursor_number = m_size;
}

int eListboxServiceContent::setCurrentMarked(bool state)
{
	bool prev = m_current_marked;
	m_current_marked = state;

	if (state != prev && m_listbox)
	{
		m_listbox->entryChanged(m_cursor_number);
		if (!state)
		{
			ePtr<iListableService> lst;
			if (m_service_center->list(m_root, lst))
				eDebug("no list available!");
			else
			{
				ePtr<iMutableServiceList> list;
				if (lst->startEdit(list))
					eDebug("no editable list");
				else
				{
					eServiceReference ref;
					getCurrent(ref);
					if(!ref)
						eDebug("no valid service selected");
					else
					{
						int pos = cursorGet();
						eDebugNoNewLine("move %s to %d ", ref.toString().c_str(), pos);
						if (list->moveService(ref, cursorGet()))
							eDebug("failed");
						else
							eDebug("ok");
					}
				}
			}
		}
	}

	return 0;
}

int eListboxServiceContent::cursorMove(int count)
{
	list::iterator old = m_cursor;

	if (count > 0)
	{
		while(count && (m_cursor != m_list.end()))
		{
			++m_cursor;
			++m_cursor_number;
			--count;
		}
	} else if (count < 0)
	{
		while (count && (m_cursor != m_list.begin()))
		{
			--m_cursor;
			--m_cursor_number;
			++count;
		}
	}

	if ( m_current_marked && m_saved_cursor == m_list.end() )
	{
		if ( m_cursor == m_list.end() )
			m_swap = old;
		else if ( old == m_list.end() )
		{
			std::iter_swap( m_swap, m_cursor );
			m_swap = m_list.end();
		}
		else
			std::iter_swap( old, m_cursor );
	}

	return 0;
}

int eListboxServiceContent::cursorValid()
{
	return m_cursor != m_list.end();
}

int eListboxServiceContent::cursorSet(int n)
{
	cursorHome();
	cursorMove(n);
	
	return 0;
}

int eListboxServiceContent::cursorGet()
{
	return m_cursor_number;
}

void eListboxServiceContent::cursorSave()
{
	m_saved_cursor = m_cursor;
	m_saved_cursor_number = m_cursor_number;
}

void eListboxServiceContent::cursorRestore()
{
	m_cursor = m_saved_cursor;
	m_cursor_number = m_saved_cursor_number;
	m_saved_cursor = m_list.end();
}

int eListboxServiceContent::size()
{
	return m_size;
}
	
void eListboxServiceContent::setSize(const eSize &size)
{
	m_itemsize = size;
	setVisualMode(m_visual_mode);
}

void eListboxServiceContent::paint(gPainter &painter, eWindowStyle &style, const ePoint &offset, int selected)
{
	painter.clip(eRect(offset, m_itemsize));

	if (m_current_marked && selected)
		style.setStyle(painter, eWindowStyle::styleListboxMarked);
	else if (cursorValid() && isMarked(*m_cursor))
		style.setStyle(painter, eWindowStyle::styleListboxMarked);
	else
		style.setStyle(painter, selected ? eWindowStyle::styleListboxSelected : eWindowStyle::styleListboxNormal);
	painter.clear();
	
	if (cursorValid())
	{
			/* get service information */
		ePtr<iStaticServiceInformation> service_info;
		m_service_center->info(*m_cursor, service_info);
		
		for (int e = 0; e < celElements; ++e)
		{
			if (!m_element_font[e])
				continue;
			painter.setFont(m_element_font[e]);
			
			std::string text = "<n/a>";
			
			switch (e)
			{
			case celServiceName:
			{
				if (service_info)
					service_info->getName(*m_cursor, text);
				break;
			}
			case celServiceNumber:
			{
				char bla[10];
				sprintf(bla, "%d", m_cursor_number + 1);
				text = bla;
				break;
			}
			case celServiceInfo:
			{
				text = "now&next";
				break;
			}
			case celIcon:
				continue;
			}
			
			eRect area = m_element_position[e];
			area.moveBy(offset.x(), offset.y());
			
			painter.renderText(area, text);
		}
		
		if (selected)
			style.drawFrame(painter, eRect(offset, m_itemsize), eWindowStyle::frameListboxEntry);
	}
	
	painter.clippop();
}