xref: /haiku/src/apps/processcontroller/QuitMenu.cpp (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 /*
2 	ProcessController © 2000, Georges-Edouard Berenger, All Rights Reserved.
3 	Copyright (C) 2004 beunited.org
4 
5 	This library is free software; you can redistribute it and/or
6 	modify it under the terms of the GNU Lesser General Public
7 	License as published by the Free Software Foundation; either
8 	version 2.1 of the License, or (at your option) any later version.
9 
10 	This library is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 	Lesser General Public License for more details.
14 
15 	You should have received a copy of the GNU Lesser General Public
16 	License along with this library; if not, write to the Free Software
17 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 
20 
21 #include "QuitMenu.h"
22 #include "IconMenuItem.h"
23 #include "ProcessController.h"
24 
25 #include <Roster.h>
26 #include <Window.h>
27 #include <stdio.h>
28 
29 
30 class QuitMenuItem : public IconMenuItem {
31 	public:
32 				QuitMenuItem(team_id team, BBitmap* icon, const char* title,
33 					BMessage* m, bool purge = false);
34 		team_id	Team() { return fTeam; }
35 
36 	private:
37 		team_id	fTeam;
38 };
39 
40 
41 QuitMenuItem::QuitMenuItem(team_id team, BBitmap* icon, const char* title,
42 	BMessage* m, bool purge)
43 	:
44 	IconMenuItem(icon, title, m, true, purge), fTeam(team)
45 {
46 }
47 
48 
49 //	#pragma mark -
50 
51 
52 QuitMenu::QuitMenu(const char* title, info_pack* infos, int infosCount)
53 	: BMenu(title),
54 	fInfos(infos),
55 	fInfosCount(infosCount),
56 	fMe(NULL)
57 {
58 	SetTargetForItems(gPCView);
59 }
60 
61 
62 void
63 QuitMenu::AttachedToWindow()
64 {
65 	if (!fMe)
66 		fMe = new BMessenger(this);
67 
68 	be_roster->StartWatching(*fMe, B_REQUEST_LAUNCHED | B_REQUEST_QUIT);
69 	BList apps;
70 	team_id tmid;
71 	be_roster->GetAppList(&apps);
72 	for (int t = CountItems() - 1; t >= 0; t--) {
73 		QuitMenuItem* item = (QuitMenuItem*)ItemAt(t);
74 		bool found = false;
75 		for (int a = 0; !found && (tmid = (team_id)apps.ItemAt(a)) != 0; a++)
76 			if (item->Team() == tmid)
77 				found = true;
78 		if (!found)
79 			RemoveItem(t);
80 	}
81 	for (int a = 0; (tmid = (team_id) apps.ItemAt(a)) != 0; a++) {
82 		AddTeam(tmid);
83 	}
84 
85 	BMenu::AttachedToWindow();
86 }
87 
88 
89 void
90 QuitMenu::DetachedFromWindow()
91 {
92 	BMenu::DetachedFromWindow();
93 	be_roster->StopWatching(*fMe);
94 	delete fMe;
95 	fMe = NULL;
96 }
97 
98 
99 void
100 QuitMenu::AddTeam(team_id tmid)
101 {
102 	int	t = 0;
103 	QuitMenuItem* item;
104 	while ((item = (QuitMenuItem*) ItemAt(t++)) != NULL) {
105 		if (item->Team() == tmid)
106 			return;
107 	}
108 
109 	t = 0;
110 	while (t < fInfosCount && tmid != fInfos[t].team_info.team) {
111 		t++;
112 	}
113 
114 	BMessage* message = new BMessage ('QtTm');
115 	message->AddInt32 ("team", tmid);
116 	item = NULL;
117 	if (t < fInfosCount)
118 		item = new QuitMenuItem(tmid, fInfos[t].team_icon, fInfos[t].team_name,
119 			message);
120 	else {
121 		info_pack infos;
122 		if (get_team_info(tmid, &infos.team_info) == B_OK
123 			&& get_team_name_and_icon(infos, true))
124 			item = new QuitMenuItem(tmid, infos.team_icon, infos.team_name,
125 				message, true);
126 	}
127 	if (item) {
128 		item->SetTarget(gPCView);
129 		AddItem(item);
130 	} else
131 		delete message;
132 }
133 
134 
135 void
136 QuitMenu::MessageReceived(BMessage *msg)
137 {
138 	switch (msg->what) {
139 		case B_SOME_APP_LAUNCHED:
140 		{
141 			int32 tmid;
142 			if (msg->FindInt32("be:team", &tmid) == B_OK)
143 				AddTeam(tmid);
144 			break;
145 		}
146 		case B_SOME_APP_QUIT:
147 		{
148 			int32 tmid;
149 			if (msg->FindInt32("be:team", &tmid) == B_OK) {
150 				QuitMenuItem* item;
151 				int t = 0;
152 				while ((item = (QuitMenuItem*) ItemAt(t++)) != NULL) {
153 					if (item->Team() == tmid) {
154 						delete RemoveItem(--t);
155 						return;
156 					}
157 				}
158 			}
159 			break;
160 		}
161 
162 		default:
163 			BMenu::MessageReceived(msg);
164 	}
165 }
166