1/*
2 * Copyright 2004-2008, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		J��r��me Duval
7 */
8
9#include "TeamListItem.h"
10
11#include <string.h>
12
13#include <FindDirectory.h>
14#include <LocaleRoster.h>
15#include <NodeInfo.h>
16#include <Path.h>
17#include <View.h>
18
19
20static const int32 kItemMargin = 2;
21
22
23bool gLocalizedNamePreferred;
24
25
26TeamListItem::TeamListItem(team_info &teamInfo)
27	:
28	fTeamInfo(teamInfo),
29	fAppInfo(),
30	fMiniIcon(BRect(0, 0, 15, 15), B_RGBA32),
31	fLargeIcon(BRect(0, 0, 31, 31), B_RGBA32),
32	fFound(false),
33	fRefusingToQuit(false)
34{
35	int32 cookie = 0;
36	image_info info;
37	if (get_next_image_info(teamInfo.team, &cookie, &info) == B_OK) {
38		fPath = BPath(info.name);
39		BNode node(info.name);
40		BNodeInfo nodeInfo(&node);
41		nodeInfo.GetTrackerIcon(&fMiniIcon, B_MINI_ICON);
42		nodeInfo.GetTrackerIcon(&fLargeIcon, B_LARGE_ICON);
43	}
44
45	if (be_roster->GetRunningAppInfo(fTeamInfo.team, &fAppInfo) != B_OK)
46		fAppInfo.signature[0] = '\0';
47
48	CacheLocalizedName();
49}
50
51
52TeamListItem::~TeamListItem()
53{
54}
55
56
57void
58TeamListItem::CacheLocalizedName()
59{
60	if (BLocaleRoster::Default()->GetLocalizedFileName(fLocalizedName,
61			fAppInfo.ref, true) != B_OK)
62		fLocalizedName = fPath.Leaf();
63}
64
65
66void
67TeamListItem::DrawItem(BView* owner, BRect frame, bool complete)
68{
69	rgb_color kHighlight = { 140, 140, 140, 0 };
70	rgb_color kBlack = { 0, 0, 0, 0 };
71	rgb_color kBlue = { 0, 0, 255, 0 };
72	rgb_color kRed = { 255, 0, 0, 0 };
73
74	BRect r(frame);
75
76	if (IsSelected() || complete) {
77		rgb_color color;
78		if (IsSelected())
79			color = kHighlight;
80		else
81			color = owner->ViewColor();
82
83		owner->SetHighColor(color);
84		owner->SetLowColor(color);
85		owner->FillRect(r);
86		owner->SetHighColor(kBlack);
87	} else {
88		owner->SetLowColor(owner->ViewColor());
89	}
90
91	frame.left += 4;
92	BRect iconFrame(frame);
93	iconFrame.Set(iconFrame.left, iconFrame.top + 1, iconFrame.left + 15,
94		iconFrame.top + 16);
95	owner->SetDrawingMode(B_OP_ALPHA);
96	owner->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
97	owner->DrawBitmap(&fMiniIcon, iconFrame);
98	owner->SetDrawingMode(B_OP_COPY);
99
100	frame.left += 16;
101	if (fRefusingToQuit)
102		owner->SetHighColor(kRed);
103	else
104		owner->SetHighColor(IsSystemServer() ? kBlue : kBlack);
105
106	BFont font = be_plain_font;
107	font_height	finfo;
108	font.GetHeight(&finfo);
109	owner->SetFont(&font);
110	owner->MovePenTo(frame.left + 8, frame.top + ((frame.Height()
111			- (finfo.ascent + finfo.descent + finfo.leading)) / 2)
112		+ finfo.ascent);
113
114	if (gLocalizedNamePreferred)
115		owner->DrawString(fLocalizedName.String());
116	else
117		owner->DrawString(fPath.Leaf());
118}
119
120
121/*static*/ int32
122TeamListItem::MinimalHeight()
123{
124	return 16 + kItemMargin;
125}
126
127
128void
129TeamListItem::Update(BView* owner, const BFont* font)
130{
131	// we need to override the update method so we can make sure
132	// the list item size doesn't change
133	BListItem::Update(owner, font);
134	if (Height() < MinimalHeight())
135		SetHeight(MinimalHeight());
136}
137
138
139const team_info*
140TeamListItem::GetInfo()
141{
142	return &fTeamInfo;
143}
144
145
146bool
147TeamListItem::IsSystemServer()
148{
149	static bool firstCall = true;
150	static BPath systemServersPath;
151	static BPath trackerPath;
152	static BPath deskbarPath;
153
154	if (firstCall) {
155		find_directory(B_SYSTEM_SERVERS_DIRECTORY, &systemServersPath);
156
157		find_directory(B_SYSTEM_DIRECTORY, &trackerPath);
158		trackerPath.Append("Tracker");
159
160		find_directory(B_SYSTEM_DIRECTORY, &deskbarPath);
161		deskbarPath.Append("Deskbar");
162
163		firstCall = false;
164	}
165
166	if (strncmp(systemServersPath.Path(), fTeamInfo.args,
167			strlen(systemServersPath.Path())) == 0)
168		return true;
169
170	if (strncmp(trackerPath.Path(), fTeamInfo.args,
171			strlen(trackerPath.Path())) == 0)
172		return true;
173
174	if (strncmp(deskbarPath.Path(), fTeamInfo.args,
175			strlen(deskbarPath.Path())) == 0)
176		return true;
177
178	return false;
179}
180
181
182bool
183TeamListItem::IsApplication() const
184{
185	return fAppInfo.signature[0] != '\0';
186}
187
188
189void
190TeamListItem::SetRefusingToQuit(bool refusing)
191{
192	fRefusingToQuit = refusing;
193}
194
195
196bool
197TeamListItem::IsRefusingToQuit()
198{
199	return fRefusingToQuit;
200}
201