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 <Mime.h> 25 #include <NodeInfo.h> 26 #include <Path.h> 27 #include <PopUpMenu.h> 28 #include <Query.h> 29 #include <Roster.h> 30 #include <Screen.h> 31 #include <ScrollView.h> 32 #include <StatusBar.h> 33 #include <StringView.h> 34 #include <TextView.h> 35 #include <Volume.h> 36 #include <VolumeRoster.h> 37 38 #include <stdio.h> 39 40 41 // TODO: think about adopting Tracker's info window style here (pressable path) 42 43 44 #undef B_TRANSLATION_CONTEXT 45 #define B_TRANSLATION_CONTEXT "Application Types Window" 46 47 48 class ProgressWindow : public BWindow { 49 public: 50 ProgressWindow(const char* message, int32 max, 51 volatile bool* signalQuit); 52 virtual ~ProgressWindow(); 53 54 virtual void MessageReceived(BMessage* message); 55 56 private: 57 BStatusBar* fStatusBar; 58 BButton* fAbortButton; 59 volatile bool* fQuitListener; 60 }; 61 62 const uint32 kMsgTypeSelected = 'typs'; 63 const uint32 kMsgTypeInvoked = 'typi'; 64 const uint32 kMsgRemoveUninstalled = 'runs'; 65 const uint32 kMsgEdit = 'edit'; 66 67 68 const char* 69 variety_to_text(uint32 variety) 70 { 71 #if defined(HAIKU_TARGET_PLATFORM_BEOS) || defined(HAIKU_TARGET_PLATFORM_BONE) 72 # define B_DEVELOPMENT_VERSION 0 73 # define B_ALPHA_VERSION 1 74 # define B_BETA_VERSION 2 75 # define B_GAMMA_VERSION 3 76 # define B_GOLDEN_MASTER_VERSION 4 77 # define B_FINAL_VERSION 5 78 #endif 79 80 switch (variety) { 81 case B_DEVELOPMENT_VERSION: 82 return B_TRANSLATE("Development"); 83 case B_ALPHA_VERSION: 84 return B_TRANSLATE("Alpha"); 85 case B_BETA_VERSION: 86 return B_TRANSLATE("Beta"); 87 case B_GAMMA_VERSION: 88 return B_TRANSLATE("Gamma"); 89 case B_GOLDEN_MASTER_VERSION: 90 return B_TRANSLATE("Golden master"); 91 case B_FINAL_VERSION: 92 return B_TRANSLATE("Final"); 93 } 94 95 return "-"; 96 } 97 98 99 // #pragma mark - 100 101 102 ProgressWindow::ProgressWindow(const char* message, 103 int32 max, volatile bool* signalQuit) 104 : 105 BWindow(BRect(0, 0, 300, 200), B_TRANSLATE("Progress"), B_MODAL_WINDOW_LOOK, 106 B_MODAL_SUBSET_WINDOW_FEEL, B_ASYNCHRONOUS_CONTROLS | 107 B_NOT_V_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS), 108 fQuitListener(signalQuit) 109 { 110 char count[100]; 111 snprintf(count, sizeof(count), "/%ld", max); 112 113 fStatusBar = new BStatusBar("status", message, count); 114 fStatusBar->SetMaxValue(max); 115 fAbortButton = new BButton("abort", B_TRANSLATE("Abort"), 116 new BMessage(B_CANCEL)); 117 118 float padding = be_control_look->DefaultItemSpacing(); 119 BLayoutBuilder::Group<>(this, B_VERTICAL, padding) 120 .SetInsets(padding, padding, padding, padding) 121 .Add(fStatusBar) 122 .Add(fAbortButton); 123 124 // center on screen 125 BScreen screen(this); 126 MoveTo(screen.Frame().left + (screen.Frame().Width() 127 - Bounds().Width()) / 2.0f, 128 screen.Frame().top + (screen.Frame().Height() 129 - Bounds().Height()) / 2.0f); 130 } 131 132 133 ProgressWindow::~ProgressWindow() 134 { 135 } 136 137 138 void 139 ProgressWindow::MessageReceived(BMessage* message) 140 { 141 switch (message->what) { 142 case B_UPDATE_STATUS_BAR: 143 char count[100]; 144 snprintf(count, sizeof(count), "%ld", (int32)fStatusBar->CurrentValue() + 1); 145 146 fStatusBar->Update(1, NULL, count); 147 break; 148 149 case B_CANCEL: 150 fAbortButton->SetEnabled(false); 151 if (fQuitListener != NULL) 152 *fQuitListener = true; 153 break; 154 155 default: 156 BWindow::MessageReceived(message); 157 break; 158 } 159 } 160 161 162 // #pragma mark - 163 164 165 ApplicationTypesWindow::ApplicationTypesWindow(const BMessage& settings) 166 : BWindow(_Frame(settings), B_TRANSLATE("Application types"), 167 B_TITLED_WINDOW, 168 B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS) 169 { 170 float padding = be_control_look->DefaultItemSpacing(); 171 BAlignment labelAlignment = be_control_look->DefaultLabelAlignment(); 172 BAlignment fullWidthTopAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_TOP); 173 174 // Application list 175 176 fTypeListView = new MimeTypeListView("listview", "application", true, true); 177 fTypeListView->SetSelectionMessage(new BMessage(kMsgTypeSelected)); 178 fTypeListView->SetInvocationMessage(new BMessage(kMsgTypeInvoked)); 179 // TODO: this isn't the perfect solution, but otherwise the window contents 180 // will jump chaotically 181 fTypeListView->SetExplicitMinSize(BSize(200, B_SIZE_UNSET)); 182 fTypeListView->SetExplicitMaxSize(BSize(250, B_SIZE_UNSET)); 183 184 BScrollView* scrollView = new BScrollView("scrollview", fTypeListView, 185 B_FRAME_EVENTS | B_WILL_DRAW, false, true); 186 187 BButton* button = new BButton("remove", B_TRANSLATE("Remove uninstalled"), 188 new BMessage(kMsgRemoveUninstalled)); 189 190 // "Information" group 191 192 BBox* infoBox = new BBox((char*)NULL); 193 infoBox->SetLabel(B_TRANSLATE("Information")); 194 infoBox->SetExplicitAlignment(fullWidthTopAlignment); 195 196 fNameView = new StringView(B_TRANSLATE("Name:"), NULL); 197 fNameView->TextView()->SetExplicitAlignment(labelAlignment); 198 fNameView->LabelView()->SetExplicitAlignment(labelAlignment); 199 fSignatureView = new StringView(B_TRANSLATE("Signature:"), NULL); 200 fSignatureView->TextView()->SetExplicitAlignment(labelAlignment); 201 fSignatureView->LabelView()->SetExplicitAlignment(labelAlignment); 202 fPathView = new StringView(B_TRANSLATE("Path:"), NULL); 203 fPathView->TextView()->SetExplicitAlignment(labelAlignment); 204 fPathView->LabelView()->SetExplicitAlignment(labelAlignment); 205 206 BLayoutBuilder::Grid<>(infoBox, padding, padding) 207 .SetInsets(padding, padding * 2, padding, padding) 208 .Add(fNameView->LabelView(), 0, 0) 209 .Add(fNameView->TextView(), 1, 0, 2) 210 .Add(fSignatureView->LabelView(), 0, 1) 211 .Add(fSignatureView->TextView(), 1, 1, 2) 212 .Add(fPathView->LabelView(), 0, 2) 213 .Add(fPathView->TextView(), 1, 2, 2); 214 215 // "Version" group 216 217 BBox* versionBox = new BBox(""); 218 versionBox->SetLabel(B_TRANSLATE("Version")); 219 versionBox->SetExplicitAlignment(fullWidthTopAlignment); 220 221 fVersionView = new StringView(B_TRANSLATE("Version:"), NULL); 222 fVersionView->TextView()->SetExplicitAlignment(labelAlignment); 223 fVersionView->LabelView()->SetExplicitAlignment(labelAlignment); 224 fDescriptionLabel = new StringView(B_TRANSLATE("Description:"), NULL); 225 fDescriptionLabel->LabelView()->SetExplicitAlignment(labelAlignment); 226 fDescriptionView = new BTextView("description"); 227 fDescriptionView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 228 fDescriptionView->SetLowColor(fDescriptionView->ViewColor()); 229 fDescriptionView->MakeEditable(false); 230 231 BLayoutBuilder::Grid<>(versionBox, padding, padding) 232 .SetInsets(padding, padding * 2, padding, padding) 233 .Add(fVersionView->LabelView(), 0, 0) 234 .Add(fVersionView->TextView(), 1, 0) 235 .Add(fDescriptionLabel->LabelView(), 0, 1) 236 .Add(fDescriptionView, 1, 1, 2, 2); 237 238 // Launch and Tracker buttons 239 240 fEditButton = new BButton(B_TRANSLATE("Edit" B_UTF8_ELLIPSIS), 241 new BMessage(kMsgEdit)); 242 // launch and tracker buttons get messages in _SetType() 243 fLaunchButton = new BButton(B_TRANSLATE("Launch")); 244 fTrackerButton = new BButton( 245 B_TRANSLATE("Show in Tracker" B_UTF8_ELLIPSIS)); 246 247 248 BLayoutBuilder::Group<>(this, B_HORIZONTAL, padding) 249 .AddGroup(B_VERTICAL, padding, 3) 250 .Add(scrollView) 251 .AddGroup(B_HORIZONTAL) 252 .Add(button) 253 .AddGlue() 254 .End() 255 .End() 256 .AddGroup(B_VERTICAL, padding) 257 .Add(infoBox) 258 .Add(versionBox) 259 .AddGroup(B_HORIZONTAL, padding) 260 .Add(fEditButton) 261 .Add(fLaunchButton) 262 .Add(fTrackerButton) 263 .AddGlue() 264 .End() 265 .AddGlue() 266 .End() 267 .SetInsets(padding, padding, padding, padding); 268 269 BMimeType::StartWatching(this); 270 _SetType(NULL); 271 } 272 273 274 ApplicationTypesWindow::~ApplicationTypesWindow() 275 { 276 BMimeType::StopWatching(this); 277 } 278 279 280 BRect 281 ApplicationTypesWindow::_Frame(const BMessage& settings) const 282 { 283 BRect rect; 284 if (settings.FindRect("app_types_frame", &rect) == B_OK) 285 return rect; 286 287 return BRect(100.0f, 100.0f, 540.0f, 480.0f); 288 } 289 290 291 void 292 ApplicationTypesWindow::_RemoveUninstalled() 293 { 294 // Note: this runs in the looper's thread, which isn't that nice 295 296 int32 removed = 0; 297 volatile bool quit = false; 298 299 BWindow* progressWindow = 300 new ProgressWindow( 301 B_TRANSLATE("Removing uninstalled application types"), 302 fTypeListView->FullListCountItems(), &quit); 303 progressWindow->AddToSubset(this); 304 progressWindow->Show(); 305 306 for (int32 i = fTypeListView->FullListCountItems(); i-- > 0 && !quit;) { 307 MimeTypeItem* item = dynamic_cast<MimeTypeItem*> 308 (fTypeListView->FullListItemAt(i)); 309 progressWindow->PostMessage(B_UPDATE_STATUS_BAR); 310 311 if (item == NULL) 312 continue; 313 314 // search for application on all volumes 315 316 bool found = false; 317 318 BVolumeRoster volumeRoster; 319 BVolume volume; 320 while (volumeRoster.GetNextVolume(&volume) == B_OK) { 321 if (!volume.KnowsQuery()) 322 continue; 323 324 BQuery query; 325 query.PushAttr("BEOS:APP_SIG"); 326 query.PushString(item->Type()); 327 query.PushOp(B_EQ); 328 329 query.SetVolume(&volume); 330 query.Fetch(); 331 332 entry_ref ref; 333 if (query.GetNextRef(&ref) == B_OK) { 334 found = true; 335 break; 336 } 337 } 338 339 if (!found) { 340 BMimeType mimeType(item->Type()); 341 mimeType.Delete(); 342 343 removed++; 344 345 // We're blocking the message loop that received the MIME changes, 346 // so we dequeue all waiting messages from time to time 347 if (removed % 10 == 0) 348 UpdateIfNeeded(); 349 } 350 } 351 352 progressWindow->PostMessage(B_QUIT_REQUESTED); 353 354 char message[512]; 355 // TODO: Use ICU to properly format this. 356 snprintf(message, sizeof(message), 357 B_TRANSLATE("%ld Application type%s could be removed."), 358 removed, removed == 1 ? "" : "s"); 359 error_alert(message, B_OK, B_INFO_ALERT); 360 } 361 362 363 void 364 ApplicationTypesWindow::_SetType(BMimeType* type, int32 forceUpdate) 365 { 366 bool enabled = type != NULL; 367 bool appFound = true; 368 369 // update controls 370 371 if (type != NULL) { 372 if (fCurrentType == *type) { 373 if (!forceUpdate) 374 return; 375 } else 376 forceUpdate = B_EVERYTHING_CHANGED; 377 378 if (&fCurrentType != type) 379 fCurrentType.SetTo(type->Type()); 380 381 fSignatureView->SetText(type->Type()); 382 383 char description[B_MIME_TYPE_LENGTH]; 384 385 if ((forceUpdate & B_SHORT_DESCRIPTION_CHANGED) != 0) { 386 if (type->GetShortDescription(description) != B_OK) 387 description[0] = '\0'; 388 fNameView->SetText(description); 389 } 390 391 entry_ref ref; 392 if ((forceUpdate & B_APP_HINT_CHANGED) != 0 393 && be_roster->FindApp(fCurrentType.Type(), &ref) == B_OK) { 394 // Set launch message 395 BMessenger tracker("application/x-vnd.Be-TRAK"); 396 BMessage* message = new BMessage(B_REFS_RECEIVED); 397 message->AddRef("refs", &ref); 398 399 fLaunchButton->SetMessage(message); 400 fLaunchButton->SetTarget(tracker); 401 402 // Set path 403 BPath path(&ref); 404 path.GetParent(&path); 405 fPathView->SetText(path.Path()); 406 407 // Set "Show In Tracker" message 408 BEntry entry(path.Path()); 409 entry_ref directoryRef; 410 if (entry.GetRef(&directoryRef) == B_OK) { 411 BMessenger tracker("application/x-vnd.Be-TRAK"); 412 message = new BMessage(B_REFS_RECEIVED); 413 message->AddRef("refs", &directoryRef); 414 415 fTrackerButton->SetMessage(message); 416 fTrackerButton->SetTarget(tracker); 417 } else { 418 fTrackerButton->SetMessage(NULL); 419 appFound = false; 420 } 421 } 422 423 if (forceUpdate == B_EVERYTHING_CHANGED) { 424 // update version information 425 426 BFile file(&ref, B_READ_ONLY); 427 if (file.InitCheck() == B_OK) { 428 BAppFileInfo appInfo(&file); 429 version_info versionInfo; 430 if (appInfo.InitCheck() == B_OK 431 && appInfo.GetVersionInfo(&versionInfo, B_APP_VERSION_KIND) 432 == B_OK) { 433 char version[256]; 434 snprintf(version, sizeof(version), "%lu.%lu.%lu, %s/%lu", 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