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, BMessage* m, bool purge = false); 33 team_id Team() { return fTeam; } 34 35 private: 36 team_id fTeam; 37 }; 38 39 40 QuitMenuItem::QuitMenuItem(team_id team, BBitmap* icon, const char* title, BMessage* m, bool purge) : 41 IconMenuItem(icon, title, m, true, purge), fTeam(team) 42 { 43 } 44 45 46 // #pragma mark - 47 48 49 QuitMenu::QuitMenu(const char* title, info_pack* infos, int infosCount) 50 : BMenu(title), 51 fInfos(infos), 52 fInfosCount(infosCount), 53 fMe(NULL) 54 { 55 SetTargetForItems(gPCView); 56 } 57 58 59 void 60 QuitMenu::AttachedToWindow() 61 { 62 if (!fMe) 63 fMe = new BMessenger(this); 64 65 be_roster->StartWatching(*fMe, B_REQUEST_LAUNCHED | B_REQUEST_QUIT); 66 BList apps; 67 team_id tmid; 68 be_roster->GetAppList(&apps); 69 for (int t = CountItems() - 1; t >= 0; t--) { 70 QuitMenuItem* item = (QuitMenuItem*)ItemAt(t); 71 bool found = false; 72 for (int a = 0; !found && (tmid = (team_id)apps.ItemAt(a)) != 0; a++) 73 if (item->Team() == tmid) 74 found = true; 75 if (!found) 76 RemoveItem(t); 77 } 78 for (int a = 0; (tmid = (team_id) apps.ItemAt(a)) != 0; a++) { 79 AddTeam(tmid); 80 } 81 82 BMenu::AttachedToWindow(); 83 } 84 85 86 void 87 QuitMenu::DetachedFromWindow() 88 { 89 BMenu::DetachedFromWindow(); 90 be_roster->StopWatching(*fMe); 91 delete fMe; 92 fMe = NULL; 93 } 94 95 96 void 97 QuitMenu::AddTeam(team_id tmid) 98 { 99 int t = 0; 100 QuitMenuItem* item; 101 while ((item = (QuitMenuItem*) ItemAt(t++)) != NULL) { 102 if (item->Team() == tmid) 103 return; 104 } 105 106 t = 0; 107 while (t < fInfosCount && tmid != fInfos[t].team_info.team) { 108 t++; 109 } 110 111 BMessage* message = new BMessage ('QtTm'); 112 message->AddInt32 ("team", tmid); 113 item = NULL; 114 if (t < fInfosCount) 115 item = new QuitMenuItem(tmid, fInfos[t].team_icon, fInfos[t].team_name, message); 116 else { 117 info_pack infos; 118 if (get_team_info(tmid, &infos.team_info) == B_OK 119 && get_team_name_and_icon(infos, true)) 120 item = new QuitMenuItem(tmid, infos.team_icon, infos.team_name, message, true); 121 } 122 if (item) { 123 item->SetTarget(gPCView); 124 AddItem(item); 125 } else 126 delete message; 127 } 128 129 130 void 131 QuitMenu::MessageReceived(BMessage *msg) 132 { 133 switch (msg->what) { 134 case B_SOME_APP_LAUNCHED: 135 { 136 int32 tmid; 137 if (msg->FindInt32("be:team", &tmid) == B_OK) 138 AddTeam(tmid); 139 break; 140 } 141 case B_SOME_APP_QUIT: 142 { 143 int32 tmid; 144 if (msg->FindInt32("be:team", &tmid) == B_OK) { 145 QuitMenuItem* item; 146 int t = 0; 147 while ((item = (QuitMenuItem*) ItemAt(t++)) != NULL) { 148 if (item->Team() == tmid) { 149 delete RemoveItem(--t); 150 return; 151 } 152 } 153 } 154 break; 155 } 156 157 default: 158 BMenu::MessageReceived(msg); 159 } 160 } 161