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