1 /* 2 * Copyright 2006-2010, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "ApplicationTypesWindow.h" 8 #include "FileTypes.h" 9 #include "FileTypesWindow.h" 10 #include "MimeTypeListView.h" 11 #include "StringView.h" 12 13 #include <AppFileInfo.h> 14 #include <Application.h> 15 #include <Bitmap.h> 16 #include <Box.h> 17 #include <Button.h> 18 #include <Catalog.h> 19 #include <ControlLook.h> 20 #include <LayoutBuilder.h> 21 #include <Locale.h> 22 #include <MenuField.h> 23 #include <MenuItem.h> 24 #include <MessageFormat.h> 25 #include <Mime.h> 26 #include <NodeInfo.h> 27 #include <Path.h> 28 #include <PopUpMenu.h> 29 #include <Query.h> 30 #include <Roster.h> 31 #include <Screen.h> 32 #include <ScrollView.h> 33 #include <StatusBar.h> 34 #include <StringView.h> 35 #include <TextView.h> 36 #include <Volume.h> 37 #include <VolumeRoster.h> 38 39 #include <stdio.h> 40 #include <strings.h> 41 42 43 // TODO: think about adopting Tracker's info window style here (pressable path) 44 45 46 #undef B_TRANSLATION_CONTEXT 47 #define B_TRANSLATION_CONTEXT "Application Types Window" 48 49 50 class ProgressWindow : public BWindow { 51 public: 52 ProgressWindow(const char* message, int32 max, 53 volatile bool* signalQuit); 54 virtual ~ProgressWindow(); 55 56 virtual void MessageReceived(BMessage* message); 57 58 private: 59 BStatusBar* fStatusBar; 60 BButton* fAbortButton; 61 volatile bool* fQuitListener; 62 }; 63 64 const uint32 kMsgTypeSelected = 'typs'; 65 const uint32 kMsgTypeInvoked = 'typi'; 66 const uint32 kMsgRemoveUninstalled = 'runs'; 67 const uint32 kMsgEdit = 'edit'; 68 69 70 const char* 71 variety_to_text(uint32 variety) 72 { 73 switch (variety) { 74 case B_DEVELOPMENT_VERSION: 75 return B_TRANSLATE("Development"); 76 case B_ALPHA_VERSION: 77 return B_TRANSLATE("Alpha"); 78 case B_BETA_VERSION: 79 return B_TRANSLATE("Beta"); 80 case B_GAMMA_VERSION: 81 return B_TRANSLATE("Gamma"); 82 case B_GOLDEN_MASTER_VERSION: 83 return B_TRANSLATE("Golden master"); 84 case B_FINAL_VERSION: 85 return B_TRANSLATE("Final"); 86 } 87 88 return "-"; 89 } 90 91 92 // #pragma mark - 93 94 95 ProgressWindow::ProgressWindow(const char* message, 96 int32 max, volatile bool* signalQuit) 97 : 98 BWindow(BRect(0, 0, 300, 200), B_TRANSLATE("Progress"), B_MODAL_WINDOW_LOOK, 99 B_MODAL_SUBSET_WINDOW_FEEL, B_ASYNCHRONOUS_CONTROLS | 100 B_NOT_V_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS), 101 fQuitListener(signalQuit) 102 { 103 char count[100]; 104 snprintf(count, sizeof(count), "/%" B_PRId32, max); 105 106 fStatusBar = new BStatusBar("status", message, count); 107 fStatusBar->SetMaxValue(max); 108 fAbortButton = new BButton("abort", B_TRANSLATE("Abort"), 109 new BMessage(B_CANCEL)); 110 111 float padding = be_control_look->DefaultItemSpacing(); 112 BLayoutBuilder::Group<>(this, B_VERTICAL, padding) 113 .SetInsets(padding, padding, padding, padding) 114 .Add(fStatusBar) 115 .Add(fAbortButton); 116 117 // center on screen 118 BScreen screen(this); 119 MoveTo(screen.Frame().left + (screen.Frame().Width() 120 - Bounds().Width()) / 2.0f, 121 screen.Frame().top + (screen.Frame().Height() 122 - Bounds().Height()) / 2.0f); 123 } 124 125 126 ProgressWindow::~ProgressWindow() 127 { 128 } 129 130 131 void 132 ProgressWindow::MessageReceived(BMessage* message) 133 { 134 switch (message->what) { 135 case B_UPDATE_STATUS_BAR: 136 char count[100]; 137 snprintf(count, sizeof(count), "%" B_PRId32, 138 (int32)fStatusBar->CurrentValue() + 1); 139 140 fStatusBar->Update(1, NULL, count); 141 break; 142 143 case B_CANCEL: 144 fAbortButton->SetEnabled(false); 145 if (fQuitListener != NULL) 146 *fQuitListener = true; 147 break; 148 149 default: 150 BWindow::MessageReceived(message); 151 break; 152 } 153 } 154 155 156 // #pragma mark - 157 158 159 ApplicationTypesWindow::ApplicationTypesWindow(const BMessage& settings) 160 : BWindow(_Frame(settings), B_TRANSLATE("Application types"), 161 B_TITLED_WINDOW, 162 B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS) 163 { 164 float padding = be_control_look->DefaultItemSpacing(); 165 BAlignment labelAlignment = be_control_look->DefaultLabelAlignment(); 166 BAlignment fullWidthTopAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_TOP); 167 168 // Application list 169 170 fTypeListView = new MimeTypeListView("listview", "application", true, true); 171 fTypeListView->SetSelectionMessage(new BMessage(kMsgTypeSelected)); 172 fTypeListView->SetInvocationMessage(new BMessage(kMsgTypeInvoked)); 173 // TODO: this isn't the perfect solution, but otherwise the window contents 174 // will jump chaotically 175 fTypeListView->SetExplicitMinSize(BSize(200, B_SIZE_UNSET)); 176 fTypeListView->SetExplicitMaxSize(BSize(250, B_SIZE_UNSET)); 177 178 BScrollView* scrollView = new BScrollView("scrollview", fTypeListView, 179 B_FRAME_EVENTS | B_WILL_DRAW, false, true); 180 181 BButton* button = new BButton("remove", B_TRANSLATE("Remove uninstalled"), 182 new BMessage(kMsgRemoveUninstalled)); 183 184 // "Information" group 185 186 BBox* infoBox = new BBox((char*)NULL); 187 infoBox->SetLabel(B_TRANSLATE("Information")); 188 infoBox->SetExplicitAlignment(fullWidthTopAlignment); 189 190 fNameView = new StringView(B_TRANSLATE("Name:"), NULL); 191 fNameView->TextView()->SetExplicitAlignment(labelAlignment); 192 fNameView->LabelView()->SetExplicitAlignment(labelAlignment); 193 fSignatureView = new StringView(B_TRANSLATE("Signature:"), NULL); 194 fSignatureView->TextView()->SetExplicitAlignment(labelAlignment); 195 fSignatureView->LabelView()->SetExplicitAlignment(labelAlignment); 196 fPathView = new StringView(B_TRANSLATE("Path:"), NULL); 197 fPathView->TextView()->SetExplicitAlignment(labelAlignment); 198 fPathView->LabelView()->SetExplicitAlignment(labelAlignment); 199 200 BLayoutBuilder::Grid<>(infoBox, padding, padding) 201 .SetInsets(padding, padding * 2, padding, padding) 202 .Add(fNameView->LabelView(), 0, 0) 203 .Add(fNameView->TextView(), 1, 0, 2) 204 .Add(fSignatureView->LabelView(), 0, 1) 205 .Add(fSignatureView->TextView(), 1, 1, 2) 206 .Add(fPathView->LabelView(), 0, 2) 207 .Add(fPathView->TextView(), 1, 2, 2); 208 209 // "Version" group 210 211 BBox* versionBox = new BBox(""); 212 versionBox->SetLabel(B_TRANSLATE("Version")); 213 versionBox->SetExplicitAlignment(fullWidthTopAlignment); 214 215 fVersionView = new StringView(B_TRANSLATE("Version:"), NULL); 216 fVersionView->TextView()->SetExplicitAlignment(labelAlignment); 217 fVersionView->LabelView()->SetExplicitAlignment(labelAlignment); 218 fDescriptionLabel = new StringView(B_TRANSLATE("Description:"), NULL); 219 fDescriptionLabel->LabelView()->SetExplicitAlignment(labelAlignment); 220 fDescriptionView = new BTextView("description"); 221 fDescriptionView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 222 fDescriptionView->SetLowColor(fDescriptionView->ViewColor()); 223 fDescriptionView->MakeEditable(false); 224 225 BLayoutBuilder::Grid<>(versionBox, padding, padding) 226 .SetInsets(padding, padding * 2, padding, padding) 227 .Add(fVersionView->LabelView(), 0, 0) 228 .Add(fVersionView->TextView(), 1, 0) 229 .Add(fDescriptionLabel->LabelView(), 0, 1) 230 .Add(fDescriptionView, 1, 1, 2, 2); 231 232 // Launch and Tracker buttons 233 234 fEditButton = new BButton(B_TRANSLATE("Edit" B_UTF8_ELLIPSIS), 235 new BMessage(kMsgEdit)); 236 // launch and tracker buttons get messages in _SetType() 237 fLaunchButton = new BButton(B_TRANSLATE("Launch")); 238 fTrackerButton = new BButton( 239 B_TRANSLATE("Show in Tracker" B_UTF8_ELLIPSIS)); 240 241 242 BLayoutBuilder::Group<>(this, B_HORIZONTAL, padding) 243 .AddGroup(B_VERTICAL, padding, 3) 244 .Add(scrollView) 245 .AddGroup(B_HORIZONTAL) 246 .Add(button) 247 .AddGlue() 248 .End() 249 .End() 250 .AddGroup(B_VERTICAL, padding) 251 .Add(infoBox) 252 .Add(versionBox) 253 .AddGroup(B_HORIZONTAL, padding) 254 .Add(fEditButton) 255 .Add(fLaunchButton) 256 .Add(fTrackerButton) 257 .AddGlue() 258 .End() 259 .AddGlue() 260 .End() 261 .SetInsets(padding, padding, padding, padding); 262 263 BMimeType::StartWatching(this); 264 _SetType(NULL); 265 } 266 267 268 ApplicationTypesWindow::~ApplicationTypesWindow() 269 { 270 BMimeType::StopWatching(this); 271 } 272 273 274 BRect 275 ApplicationTypesWindow::_Frame(const BMessage& settings) const 276 { 277 BRect rect; 278 if (settings.FindRect("app_types_frame", &rect) == B_OK) 279 return rect; 280 281 return BRect(100.0f, 100.0f, 540.0f, 480.0f); 282 } 283 284 285 void 286 ApplicationTypesWindow::_RemoveUninstalled() 287 { 288 // Note: this runs in the looper's thread, which isn't that nice 289 290 int32 removed = 0; 291 volatile bool quit = false; 292 293 BWindow* progressWindow = 294 new ProgressWindow( 295 B_TRANSLATE("Removing uninstalled application types"), 296 fTypeListView->FullListCountItems(), &quit); 297 progressWindow->AddToSubset(this); 298 progressWindow->Show(); 299 300 for (int32 i = fTypeListView->FullListCountItems(); i-- > 0 && !quit;) { 301 MimeTypeItem* item = dynamic_cast<MimeTypeItem*> 302 (fTypeListView->FullListItemAt(i)); 303 progressWindow->PostMessage(B_UPDATE_STATUS_BAR); 304 305 if (item == NULL) 306 continue; 307 308 // search for application on all volumes 309 310 bool found = false; 311 312 BVolumeRoster volumeRoster; 313 BVolume volume; 314 while (volumeRoster.GetNextVolume(&volume) == B_OK) { 315 if (!volume.KnowsQuery()) 316 continue; 317 318 BQuery query; 319 query.PushAttr("BEOS:APP_SIG"); 320 query.PushString(item->Type()); 321 query.PushOp(B_EQ); 322 323 query.SetVolume(&volume); 324 query.Fetch(); 325 326 entry_ref ref; 327 if (query.GetNextRef(&ref) == B_OK) { 328 found = true; 329 break; 330 } 331 } 332 333 if (!found) { 334 BMimeType mimeType(item->Type()); 335 mimeType.Delete(); 336 337 removed++; 338 339 // We're blocking the message loop that received the MIME changes, 340 // so we dequeue all waiting messages from time to time 341 if (removed % 10 == 0) 342 UpdateIfNeeded(); 343 } 344 } 345 346 progressWindow->PostMessage(B_QUIT_REQUESTED); 347 348 static BMessageFormat format(B_TRANSLATE("{0, plural, " 349 "one{# Application type could be removed} " 350 "other{# Application types could be removed}}")); 351 BString message; 352 format.Format(message, removed); 353 354 error_alert(message, B_OK, B_INFO_ALERT); 355 } 356 357 358 void 359 ApplicationTypesWindow::_SetType(BMimeType* type, int32 forceUpdate) 360 { 361 bool enabled = type != NULL; 362 bool appFound = true; 363 364 // update controls 365 366 if (type != NULL) { 367 if (fCurrentType == *type) { 368 if (!forceUpdate) 369 return; 370 } else 371 forceUpdate = B_EVERYTHING_CHANGED; 372 373 if (&fCurrentType != type) 374 fCurrentType.SetTo(type->Type()); 375 376 fSignatureView->SetText(type->Type()); 377 378 char description[B_MIME_TYPE_LENGTH]; 379 380 if ((forceUpdate & B_SHORT_DESCRIPTION_CHANGED) != 0) { 381 if (type->GetShortDescription(description) != B_OK) 382 description[0] = '\0'; 383 fNameView->SetText(description); 384 } 385 386 entry_ref ref; 387 if ((forceUpdate & B_APP_HINT_CHANGED) != 0 388 && be_roster->FindApp(fCurrentType.Type(), &ref) == B_OK) { 389 // Set launch message 390 BMessenger tracker("application/x-vnd.Be-TRAK"); 391 BMessage* message = new BMessage(B_REFS_RECEIVED); 392 message->AddRef("refs", &ref); 393 394 fLaunchButton->SetMessage(message); 395 fLaunchButton->SetTarget(tracker); 396 397 // Set path 398 BPath path(&ref); 399 path.GetParent(&path); 400 fPathView->SetText(path.Path()); 401 402 // Set "Show In Tracker" message 403 BEntry entry(path.Path()); 404 entry_ref directoryRef; 405 if (entry.GetRef(&directoryRef) == B_OK) { 406 BMessenger tracker("application/x-vnd.Be-TRAK"); 407 message = new BMessage(B_REFS_RECEIVED); 408 message->AddRef("refs", &directoryRef); 409 410 fTrackerButton->SetMessage(message); 411 fTrackerButton->SetTarget(tracker); 412 } else { 413 fTrackerButton->SetMessage(NULL); 414 appFound = false; 415 } 416 } else if ((forceUpdate & B_APP_HINT_CHANGED) != 0) { 417 fLaunchButton->SetMessage(NULL); 418 fPathView->SetText(NULL); 419 fTrackerButton->SetMessage(NULL); 420 } 421 422 if (forceUpdate == B_EVERYTHING_CHANGED) { 423 // update version information 424 425 BFile file(&ref, B_READ_ONLY); 426 if (file.InitCheck() == B_OK) { 427 BAppFileInfo appInfo(&file); 428 version_info versionInfo; 429 if (appInfo.InitCheck() == B_OK 430 && appInfo.GetVersionInfo(&versionInfo, B_APP_VERSION_KIND) 431 == B_OK) { 432 char version[256]; 433 snprintf(version, sizeof(version), 434 "%" B_PRIu32 ".%" B_PRIu32 ".%" B_PRIu32 ", %s/%" B_PRIu32, 435 versionInfo.major, versionInfo.middle, 436 versionInfo.minor, 437 variety_to_text(versionInfo.variety), 438 versionInfo.internal); 439 fVersionView->SetText(version); 440 fDescriptionView->SetText(versionInfo.long_info); 441 } else { 442 fVersionView->SetText(NULL); 443 fDescriptionView->SetText(NULL); 444 } 445 } 446 } 447 } else { 448 fNameView->SetText(NULL); 449 fSignatureView->SetText(NULL); 450 fPathView->SetText(NULL); 451 452 fVersionView->SetText(NULL); 453 fDescriptionView->SetText(NULL); 454 } 455 456 fNameView->SetEnabled(enabled); 457 fSignatureView->SetEnabled(enabled); 458 fPathView->SetEnabled(enabled); 459 460 fVersionView->SetEnabled(enabled); 461 fDescriptionLabel->SetEnabled(enabled); 462 463 fTrackerButton->SetEnabled(enabled && appFound); 464 fLaunchButton->SetEnabled(enabled && appFound); 465 fEditButton->SetEnabled(enabled && appFound); 466 } 467 468 469 void 470 ApplicationTypesWindow::MessageReceived(BMessage* message) 471 { 472 switch (message->what) { 473 case kMsgTypeSelected: 474 { 475 int32 index; 476 if (message->FindInt32("index", &index) == B_OK) { 477 MimeTypeItem* item = (MimeTypeItem*)fTypeListView->ItemAt(index); 478 if (item != NULL) { 479 BMimeType type(item->Type()); 480 _SetType(&type); 481 } else 482 _SetType(NULL); 483 } 484 break; 485 } 486 487 case kMsgTypeInvoked: 488 { 489 int32 index; 490 if (message->FindInt32("index", &index) == B_OK) { 491 MimeTypeItem* item = (MimeTypeItem*)fTypeListView->ItemAt(index); 492 if (item != NULL) { 493 BMimeType type(item->Type()); 494 entry_ref ref; 495 if (type.GetAppHint(&ref) == B_OK) { 496 BMessage refs(B_REFS_RECEIVED); 497 refs.AddRef("refs", &ref); 498 499 be_app->PostMessage(&refs); 500 } 501 } 502 } 503 break; 504 } 505 506 case kMsgEdit: 507 fTypeListView->Invoke(); 508 break; 509 510 case kMsgRemoveUninstalled: 511 _RemoveUninstalled(); 512 break; 513 514 case B_META_MIME_CHANGED: 515 { 516 const char* type; 517 int32 which; 518 if (message->FindString("be:type", &type) != B_OK 519 || message->FindInt32("be:which", &which) != B_OK) { 520 break; 521 } 522 523 if (fCurrentType.Type() == NULL) 524 break; 525 526 if (!strcasecmp(fCurrentType.Type(), type)) { 527 if (which != B_MIME_TYPE_DELETED) 528 _SetType(&fCurrentType, which); 529 else 530 _SetType(NULL); 531 } 532 break; 533 } 534 535 default: 536 BWindow::MessageReceived(message); 537 } 538 } 539 540 541 bool 542 ApplicationTypesWindow::QuitRequested() 543 { 544 BMessage update(kMsgSettingsChanged); 545 update.AddRect("app_types_frame", Frame()); 546 be_app_messenger.SendMessage(&update); 547 548 be_app->PostMessage(kMsgApplicationTypesWindowClosed); 549 return true; 550 } 551 552 553