1 /* 2 * Copyright (c) 1998-2007 Matthijs Hollemans 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #include "GrepWindow.h" 6 7 #include <ctype.h> 8 #include <errno.h> 9 #include <new> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 #include <unistd.h> 14 15 #include <Application.h> 16 #include <AppFileInfo.h> 17 #include <Alert.h> 18 #include <Clipboard.h> 19 #include <LayoutBuilder.h> 20 #include <MessageRunner.h> 21 #include <MimeType.h> 22 #include <Path.h> 23 #include <PathMonitor.h> 24 #include <Roster.h> 25 #include <SpaceLayoutItem.h> 26 #include <String.h> 27 #include <UTF8.h> 28 29 #include "ChangesIterator.h" 30 #include "GlobalDefs.h" 31 #include "Grepper.h" 32 #include "InitialIterator.h" 33 34 #undef B_TRANSLATION_CONTEXT 35 #define B_TRANSLATION_CONTEXT "GrepWindow" 36 37 38 const char* kAppName = B_TRANSLATE_MARK_SYSTEM_NAME("TextSearch"); 39 40 41 using std::nothrow; 42 43 static const bigtime_t kChangesPulseInterval = 150000; 44 45 #define TRACE_NODE_MONITORING 46 #ifdef TRACE_NODE_MONITORING 47 # define TRACE_NM(x...) printf(x) 48 #else 49 # define TRACE_NM(x...) 50 #endif 51 52 //#define TRACE_FUNCTIONS 53 #ifdef TRACE_FUNCTIONS 54 class FunctionTracer { 55 public: 56 FunctionTracer(const char* functionName) 57 : fName(functionName) 58 { 59 printf("%s - enter\n", fName.String()); 60 } 61 ~FunctionTracer() 62 { 63 printf("%s - exit\n", fName.String()); 64 } 65 private: 66 BString fName; 67 }; 68 # define CALLED() FunctionTracer functionTracer(__PRETTY_FUNCTION__) 69 #else 70 # define CALLED() 71 #endif // TRACE_FUNCTIONS 72 73 74 GrepWindow::GrepWindow(BMessage* message) 75 : BWindow(BRect(0, 0, 525, 430), NULL, B_DOCUMENT_WINDOW, 76 B_AUTO_UPDATE_SIZE_LIMITS), 77 fSearchText(NULL), 78 fSearchResults(NULL), 79 fMenuBar(NULL), 80 fFileMenu(NULL), 81 fNew(NULL), 82 fOpen(NULL), 83 fClose(NULL), 84 fQuit(NULL), 85 fActionMenu(NULL), 86 fSelectAll(NULL), 87 fSearch(NULL), 88 fTrimSelection(NULL), 89 fCopyText(NULL), 90 fSelectInTracker(NULL), 91 fOpenSelection(NULL), 92 fPreferencesMenu(NULL), 93 fRecurseLinks(NULL), 94 fRecurseDirs(NULL), 95 fSkipDotDirs(NULL), 96 fCaseSensitive(NULL), 97 fRegularExpression(NULL), 98 fTextOnly(NULL), 99 fInvokeEditor(NULL), 100 fHistoryMenu(NULL), 101 fEncodingMenu(NULL), 102 fUTF8(NULL), 103 fShiftJIS(NULL), 104 fEUC(NULL), 105 fJIS(NULL), 106 107 fShowLinesCheckbox(NULL), 108 fButton(NULL), 109 110 fGrepper(NULL), 111 fOldPattern(""), 112 fModel(new (nothrow) Model()), 113 fLastNodeMonitorEvent(system_time()), 114 fChangesIterator(NULL), 115 fChangesPulse(NULL), 116 117 fFilePanel(NULL) 118 { 119 if (fModel == NULL) 120 return; 121 122 entry_ref directory; 123 _InitRefsReceived(&directory, message); 124 125 fModel->fDirectory = directory; 126 fModel->fSelectedFiles = *message; 127 128 _SetWindowTitle(); 129 _CreateMenus(); 130 _UpdateMenus(); 131 _CreateViews(); 132 _LayoutViews(); 133 _LoadPrefs(); 134 _TileIfMultipleWindows(); 135 136 Show(); 137 } 138 139 140 GrepWindow::~GrepWindow() 141 { 142 delete fGrepper; 143 delete fModel; 144 } 145 146 147 void GrepWindow::FrameResized(float width, float height) 148 { 149 BWindow::FrameResized(width, height); 150 fModel->fFrame = Frame(); 151 _SavePrefs(); 152 } 153 154 155 void GrepWindow::FrameMoved(BPoint origin) 156 { 157 BWindow::FrameMoved(origin); 158 fModel->fFrame = Frame(); 159 _SavePrefs(); 160 } 161 162 163 void GrepWindow::MenusBeginning() 164 { 165 fModel->FillHistoryMenu(fHistoryMenu); 166 BWindow::MenusBeginning(); 167 } 168 169 170 void GrepWindow::MenusEnded() 171 { 172 for (int32 t = fHistoryMenu->CountItems(); t > 0; --t) 173 delete fHistoryMenu->RemoveItem(t - 1); 174 175 BWindow::MenusEnded(); 176 } 177 178 179 void GrepWindow::MessageReceived(BMessage* message) 180 { 181 switch (message->what) { 182 case MSG_NEW_WINDOW: 183 _OnNewWindow(); 184 break; 185 186 case B_SIMPLE_DATA: 187 _OnFileDrop(message); 188 break; 189 190 case MSG_OPEN_PANEL: 191 _OnOpenPanel(); 192 break; 193 194 case MSG_REFS_RECEIVED: 195 _OnRefsReceived(message); 196 break; 197 198 case MSG_SET_TARGET_TO_PARENT: 199 _OnSetTargetToParent(); 200 break; 201 202 case B_CANCEL: 203 _OnOpenPanelCancel(); 204 break; 205 206 case MSG_RECURSE_LINKS: 207 _OnRecurseLinks(); 208 break; 209 210 case MSG_RECURSE_DIRS: 211 _OnRecurseDirs(); 212 break; 213 214 case MSG_SKIP_DOT_DIRS: 215 _OnSkipDotDirs(); 216 break; 217 218 case MSG_CASE_SENSITIVE: 219 _OnCaseSensitive(); 220 break; 221 222 case MSG_REGULAR_EXPRESSION: 223 _OnRegularExpression(); 224 break; 225 226 case MSG_TEXT_ONLY: 227 _OnTextOnly(); 228 break; 229 230 case MSG_INVOKE_EDITOR: 231 _OnInvokeEditor(); 232 break; 233 234 case MSG_SEARCH_TEXT: 235 _OnSearchText(); 236 break; 237 238 case MSG_SELECT_HISTORY: 239 _OnHistoryItem(message); 240 break; 241 242 case MSG_START_CANCEL: 243 _OnStartCancel(); 244 break; 245 246 case MSG_SEARCH_FINISHED: 247 _OnSearchFinished(); 248 break; 249 250 case MSG_START_NODE_MONITORING: 251 _StartNodeMonitoring(); 252 break; 253 254 case B_PATH_MONITOR: 255 _OnNodeMonitorEvent(message); 256 break; 257 258 case MSG_NODE_MONITOR_PULSE: 259 _OnNodeMonitorPulse(); 260 break; 261 262 case MSG_REPORT_FILE_NAME: 263 _OnReportFileName(message); 264 break; 265 266 case MSG_REPORT_RESULT: 267 _OnReportResult(message); 268 break; 269 270 case MSG_REPORT_ERROR: 271 _OnReportError(message); 272 break; 273 274 case MSG_SELECT_ALL: 275 _OnSelectAll(message); 276 break; 277 278 case MSG_TRIM_SELECTION: 279 _OnTrimSelection(); 280 break; 281 282 case MSG_COPY_TEXT: 283 _OnCopyText(); 284 break; 285 286 case MSG_SELECT_IN_TRACKER: 287 _OnSelectInTracker(); 288 break; 289 290 case MSG_CHECKBOX_SHOW_LINES: 291 _OnCheckboxShowLines(); 292 break; 293 294 case MSG_OPEN_SELECTION: 295 // fall through 296 case MSG_INVOKE_ITEM: 297 _OnInvokeItem(); 298 break; 299 300 case MSG_QUIT_NOW: 301 _OnQuitNow(); 302 break; 303 304 case 'utf8': 305 fModel->fEncoding = 0; 306 break; 307 308 case B_SJIS_CONVERSION: 309 fModel->fEncoding = B_SJIS_CONVERSION; 310 break; 311 312 case B_EUC_CONVERSION: 313 fModel->fEncoding = B_EUC_CONVERSION; 314 break; 315 316 case B_JIS_CONVERSION: 317 fModel->fEncoding = B_JIS_CONVERSION; 318 break; 319 320 default: 321 BWindow::MessageReceived(message); 322 break; 323 } 324 } 325 326 327 void 328 GrepWindow::Quit() 329 { 330 CALLED(); 331 332 _StopNodeMonitoring(); 333 _SavePrefs(); 334 335 // TODO: stippi: Looks like this could be done 336 // by maintaining a counter in GrepApp with the number of open 337 // grep windows... and just quit when it goes zero 338 if (be_app->Lock()) { 339 be_app->PostMessage(MSG_TRY_QUIT); 340 be_app->Unlock(); 341 BWindow::Quit(); 342 } 343 } 344 345 346 // #pragma mark - 347 348 349 void 350 GrepWindow::_InitRefsReceived(entry_ref* directory, BMessage* message) 351 { 352 // HACK-HACK-HACK: 353 // If the user selected a single folder and invoked TextSearch on it, 354 // but recurse directories is switched off, TextSearch would do nothing. 355 // In that special case, we'd like it to recurse into that folder (but 356 // not go any deeper after that). 357 358 type_code code; 359 int32 count; 360 message->GetInfo("refs", &code, &count); 361 362 if (count == 0) { 363 if (message->FindRef("dir_ref", 0, directory) == B_OK) 364 message->MakeEmpty(); 365 } 366 367 if (count == 1) { 368 entry_ref ref; 369 if (message->FindRef("refs", 0, &ref) == B_OK) { 370 BEntry entry(&ref, true); 371 if (entry.IsDirectory()) { 372 // ok, special case, we use this folder as base directory 373 // and pretend nothing had been selected: 374 *directory = ref; 375 message->MakeEmpty(); 376 } 377 } 378 } 379 } 380 381 382 void 383 GrepWindow::_SetWindowTitle() 384 { 385 BEntry entry(&fModel->fDirectory, true); 386 BString title; 387 if (entry.InitCheck() == B_OK) { 388 BPath path; 389 if (entry.GetPath(&path) == B_OK) { 390 if (fOldPattern.Length()) { 391 title = B_TRANSLATE("%appname% : %path% : %searchtext%"); 392 title.ReplaceAll("%searchtext%", fOldPattern.String()); 393 } else 394 title = B_TRANSLATE("%appname% : %path%"); 395 396 title.ReplaceAll("%appname%", B_TRANSLATE_NOCOLLECT(kAppName)); 397 title.ReplaceAll("%path%", path.Path()); 398 } 399 } 400 401 if (!title.Length()) 402 title = B_TRANSLATE_NOCOLLECT(kAppName); 403 404 SetTitle(title.String()); 405 } 406 407 408 void 409 GrepWindow::_CreateMenus() 410 { 411 fMenuBar = new BMenuBar("menubar"); 412 413 fFileMenu = new BMenu(B_TRANSLATE("File")); 414 fActionMenu = new BMenu(B_TRANSLATE("Actions")); 415 fPreferencesMenu = new BMenu(B_TRANSLATE("Settings")); 416 fHistoryMenu = new BMenu(B_TRANSLATE("History")); 417 fEncodingMenu = new BMenu(B_TRANSLATE("Encoding")); 418 419 fNew = new BMenuItem( 420 B_TRANSLATE("New window"), new BMessage(MSG_NEW_WINDOW), 'N'); 421 422 fOpen = new BMenuItem( 423 B_TRANSLATE("Set target" B_UTF8_ELLIPSIS), new BMessage(MSG_OPEN_PANEL), 'F'); 424 425 fSetTargetToParent = new BMenuItem( 426 B_TRANSLATE("Set target to parent folder"), 427 new BMessage(MSG_SET_TARGET_TO_PARENT), B_UP_ARROW); 428 429 fClose = new BMenuItem( 430 B_TRANSLATE("Close"), new BMessage(B_QUIT_REQUESTED), 'W'); 431 432 fQuit = new BMenuItem( 433 B_TRANSLATE("Quit"), new BMessage(MSG_QUIT_NOW), 'Q'); 434 435 fSearch = new BMenuItem( 436 B_TRANSLATE("Search"), new BMessage(MSG_START_CANCEL), 'S'); 437 438 fSelectAll = new BMenuItem( 439 B_TRANSLATE("Select all"), new BMessage(MSG_SELECT_ALL), 440 'A', B_SHIFT_KEY); 441 442 fTrimSelection = new BMenuItem( 443 B_TRANSLATE("Trim to selection"), new BMessage(MSG_TRIM_SELECTION), 'T'); 444 445 fOpenSelection = new BMenuItem( 446 B_TRANSLATE("Open selection"), new BMessage(MSG_OPEN_SELECTION), 'O'); 447 448 fSelectInTracker = new BMenuItem( 449 B_TRANSLATE("Show files in Tracker"), 450 new BMessage(MSG_SELECT_IN_TRACKER), 'K'); 451 452 fCopyText = new BMenuItem( 453 B_TRANSLATE("Copy text to clipboard"), new BMessage(MSG_COPY_TEXT), 'B'); 454 455 fRecurseLinks = new BMenuItem( 456 B_TRANSLATE("Follow symbolic links"), new BMessage(MSG_RECURSE_LINKS)); 457 458 fRecurseDirs = new BMenuItem( 459 B_TRANSLATE("Look in sub-folders"), new BMessage(MSG_RECURSE_DIRS)); 460 461 fSkipDotDirs = new BMenuItem( 462 B_TRANSLATE("Skip folders starting with a dot"), 463 new BMessage(MSG_SKIP_DOT_DIRS)); 464 465 fCaseSensitive = new BMenuItem( 466 B_TRANSLATE("Case-sensitive"), new BMessage(MSG_CASE_SENSITIVE)); 467 468 fRegularExpression = new BMenuItem( 469 B_TRANSLATE("Regular expression"), new BMessage(MSG_REGULAR_EXPRESSION)); 470 471 fTextOnly = new BMenuItem( 472 B_TRANSLATE("Text files only"), new BMessage(MSG_TEXT_ONLY)); 473 474 fInvokeEditor = new BMenuItem( 475 B_TRANSLATE("Open files in code editor"), new BMessage(MSG_INVOKE_EDITOR)); 476 477 fUTF8 = new BMenuItem("UTF8", new BMessage('utf8')); 478 fShiftJIS = new BMenuItem("ShiftJIS", new BMessage(B_SJIS_CONVERSION)); 479 fEUC = new BMenuItem("EUC", new BMessage(B_EUC_CONVERSION)); 480 fJIS = new BMenuItem("JIS", new BMessage(B_JIS_CONVERSION)); 481 482 fFileMenu->AddItem(fNew); 483 fFileMenu->AddSeparatorItem(); 484 fFileMenu->AddItem(fOpen); 485 fFileMenu->AddItem(fSetTargetToParent); 486 fFileMenu->AddItem(fClose); 487 fFileMenu->AddSeparatorItem(); 488 fFileMenu->AddItem(fQuit); 489 490 fActionMenu->AddItem(fSearch); 491 fActionMenu->AddSeparatorItem(); 492 fActionMenu->AddItem(fSelectAll); 493 fActionMenu->AddItem(fTrimSelection); 494 fActionMenu->AddSeparatorItem(); 495 fActionMenu->AddItem(fOpenSelection); 496 fActionMenu->AddItem(fSelectInTracker); 497 fActionMenu->AddItem(fCopyText); 498 499 fPreferencesMenu->AddItem(fRecurseLinks); 500 fPreferencesMenu->AddItem(fRecurseDirs); 501 fPreferencesMenu->AddItem(fSkipDotDirs); 502 fPreferencesMenu->AddItem(fCaseSensitive); 503 fPreferencesMenu->AddItem(fRegularExpression); 504 fPreferencesMenu->AddItem(fTextOnly); 505 fPreferencesMenu->AddItem(fInvokeEditor); 506 507 fEncodingMenu->AddItem(fUTF8); 508 fEncodingMenu->AddItem(fShiftJIS); 509 fEncodingMenu->AddItem(fEUC); 510 fEncodingMenu->AddItem(fJIS); 511 512 // fEncodingMenu->SetLabelFromMarked(true); 513 // Do we really want this ? 514 fEncodingMenu->SetRadioMode(true); 515 fEncodingMenu->ItemAt(0)->SetMarked(true); 516 517 fMenuBar->AddItem(fFileMenu); 518 fMenuBar->AddItem(fActionMenu); 519 fMenuBar->AddItem(fPreferencesMenu); 520 fMenuBar->AddItem(fHistoryMenu); 521 fMenuBar->AddItem(fEncodingMenu); 522 523 fSearch->SetEnabled(false); 524 } 525 526 527 void 528 GrepWindow::_UpdateMenus() 529 { 530 bool targetIsSingleDirectory = 531 BEntry(&(fModel->fDirectory)).InitCheck() == B_OK; 532 fSetTargetToParent->SetEnabled(targetIsSingleDirectory); 533 } 534 535 536 void 537 GrepWindow::_CreateViews() 538 { 539 // The search pattern entry field does not send a message when 540 // <Enter> is pressed, because the "Search/Cancel" button already 541 // does this and we don't want to send the same message twice. 542 543 fSearchText = new BTextControl( 544 "SearchText", NULL, NULL, NULL, 545 B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_NAVIGABLE); 546 547 fSearchText->TextView()->SetMaxBytes(1000); 548 fSearchText->SetModificationMessage(new BMessage(MSG_SEARCH_TEXT)); 549 550 fButton = new BButton( 551 "Button", B_TRANSLATE("Search"), 552 new BMessage(MSG_START_CANCEL)); 553 fButton->MakeDefault(true); 554 fButton->SetEnabled(false); 555 556 fShowLinesCheckbox = new BCheckBox( 557 "ShowLines", B_TRANSLATE("Show lines"), 558 new BMessage(MSG_CHECKBOX_SHOW_LINES)); 559 fShowLinesCheckbox->SetValue(B_CONTROL_ON); 560 561 fSearchResults = new GrepListView(); 562 fSearchResults->SetInvocationMessage(new BMessage(MSG_INVOKE_ITEM)); 563 } 564 565 566 void 567 GrepWindow::_LayoutViews() 568 { 569 BScrollView* scroller = new BScrollView( 570 "ScrollSearchResults", fSearchResults, 571 B_FULL_UPDATE_ON_RESIZE, true, true); 572 573 BLayoutBuilder::Group<>(this, B_VERTICAL, 0) 574 .SetInsets(0, 0, -1, -1) 575 .Add(fMenuBar) 576 .AddGrid(B_USE_HALF_ITEM_INSETS) 577 .SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING, 578 B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING) 579 .Add(fSearchText, 0, 0, 3) 580 .Add(fShowLinesCheckbox, 0, 1) 581 .Add(BSpaceLayoutItem::CreateGlue(), 1, 1) 582 .Add(fButton, 2, 1) 583 .End() 584 .AddGroup(B_VERTICAL, 0) 585 .SetInsets(-2, 0, -1, -1) 586 .Add(scroller) 587 .End() 588 .End(); 589 590 fSearchText->MakeFocus(true); 591 592 SetKeyMenuBar(fMenuBar); 593 } 594 595 596 void 597 GrepWindow::_TileIfMultipleWindows() 598 { 599 if (be_app->Lock()) { 600 int32 windowCount = be_app->CountWindows(); 601 be_app->Unlock(); 602 603 if (windowCount > 1) 604 MoveBy(20, 20); 605 } 606 607 BScreen screen(this); 608 BRect screenFrame = screen.Frame(); 609 BRect windowFrame = Frame(); 610 611 if (windowFrame.left > screenFrame.right 612 || windowFrame.top > screenFrame.bottom 613 || windowFrame.right < screenFrame.left 614 || windowFrame.bottom < screenFrame.top) 615 MoveTo(50, 50); 616 } 617 618 619 // #pragma mark - 620 621 622 void 623 GrepWindow::_LoadPrefs() 624 { 625 Lock(); 626 627 fModel->LoadPrefs(); 628 629 fRecurseDirs->SetMarked(fModel->fRecurseDirs); 630 fRecurseLinks->SetMarked(fModel->fRecurseLinks); 631 fSkipDotDirs->SetMarked(fModel->fSkipDotDirs); 632 fCaseSensitive->SetMarked(fModel->fCaseSensitive); 633 fRegularExpression->SetMarked(fModel->fRegularExpression); 634 fTextOnly->SetMarked(fModel->fTextOnly); 635 fInvokeEditor->SetMarked(fModel->fInvokeEditor); 636 637 fShowLinesCheckbox->SetValue(fModel->fShowLines); 638 639 switch (fModel->fEncoding) { 640 case 0: 641 fUTF8->SetMarked(true); 642 break; 643 case B_SJIS_CONVERSION: 644 fShiftJIS->SetMarked(true); 645 break; 646 case B_EUC_CONVERSION: 647 fEUC->SetMarked(true); 648 break; 649 case B_JIS_CONVERSION: 650 fJIS->SetMarked(true); 651 break; 652 default: 653 printf("Woops. Bad fModel->fEncoding value.\n"); 654 break; 655 } 656 657 MoveTo(fModel->fFrame.left, fModel->fFrame.top); 658 ResizeTo(fModel->fFrame.Width(), fModel->fFrame.Height()); 659 660 Unlock(); 661 } 662 663 664 void 665 GrepWindow::_SavePrefs() 666 { 667 fModel->SavePrefs(); 668 } 669 670 671 void 672 GrepWindow::_StartNodeMonitoring() 673 { 674 CALLED(); 675 676 _StopNodeMonitoring(); 677 678 BMessenger messenger(this); 679 uint32 fileFlags = B_WATCH_NAME | B_WATCH_STAT | B_WATCH_ATTR; 680 681 682 // watch the top level folder only, rest should be done through filtering 683 // the node monitor notifications 684 BPath path(&fModel->fDirectory); 685 if (path.InitCheck() == B_OK) { 686 TRACE_NM("start monitoring root folder: %s\n", path.Path()); 687 BPrivate::BPathMonitor::StartWatching(path.Path(), 688 fileFlags | B_WATCH_RECURSIVELY | B_WATCH_FILES_ONLY, messenger); 689 } 690 691 if (fChangesPulse == NULL) { 692 BMessage message(MSG_NODE_MONITOR_PULSE); 693 fChangesPulse = new BMessageRunner(BMessenger(this), &message, 694 kChangesPulseInterval); 695 } 696 } 697 698 699 void 700 GrepWindow::_StopNodeMonitoring() 701 { 702 if (fChangesPulse == NULL) 703 return; 704 705 CALLED(); 706 707 BPrivate::BPathMonitor::StopWatching(BMessenger(this)); 708 delete fChangesIterator; 709 fChangesIterator = NULL; 710 delete fChangesPulse; 711 fChangesPulse = NULL; 712 } 713 714 715 // #pragma mark - events 716 717 718 void 719 GrepWindow::_OnStartCancel() 720 { 721 CALLED(); 722 723 _StopNodeMonitoring(); 724 725 if (fModel->fState == STATE_IDLE) { 726 fSearchResults->MakeEmpty(); 727 728 if (fSearchText->TextView()->TextLength() == 0) 729 return; 730 731 fModel->fState = STATE_SEARCH; 732 733 fModel->AddToHistory(fSearchText->Text()); 734 735 // From now on, we don't want to be notified when the 736 // search pattern changes, because the control will be 737 // displaying the names of the files we are grepping. 738 739 fSearchText->SetModificationMessage(NULL); 740 741 fFileMenu->SetEnabled(false); 742 fActionMenu->SetEnabled(false); 743 fPreferencesMenu->SetEnabled(false); 744 fHistoryMenu->SetEnabled(false); 745 fEncodingMenu->SetEnabled(false); 746 747 fSearchText->SetEnabled(false); 748 749 fButton->MakeFocus(true); 750 fButton->SetLabel(B_TRANSLATE("Cancel")); 751 752 fSearch->SetEnabled(false); 753 754 // We need to remember the search pattern, because during 755 // the grepping, the text control's text will be replaced 756 // by the name of the file that's currently being grepped. 757 // When the grepping finishes, we need to restore the old 758 // search pattern. 759 760 fOldPattern = fSearchText->Text(); 761 762 _SetWindowTitle(); 763 764 FileIterator* iterator = new (nothrow) InitialIterator(fModel); 765 fGrepper = new (nothrow) Grepper(fOldPattern.String(), fModel, 766 this, iterator); 767 if (fGrepper != NULL && fGrepper->IsValid()) 768 fGrepper->Start(); 769 else { 770 // roll back in case of problems 771 if (fGrepper == NULL) 772 delete iterator; 773 else { 774 // Grepper owns iterator 775 delete fGrepper; 776 fGrepper = NULL; 777 } 778 fModel->fState = STATE_IDLE; 779 // TODO: better notification to user 780 fprintf(stderr, "Out of memory.\n"); 781 } 782 } else if (fModel->fState == STATE_SEARCH) { 783 fModel->fState = STATE_CANCEL; 784 fGrepper->Cancel(); 785 } 786 } 787 788 789 void 790 GrepWindow::_OnSearchFinished() 791 { 792 fModel->fState = STATE_IDLE; 793 794 delete fGrepper; 795 fGrepper = NULL; 796 797 fFileMenu->SetEnabled(true); 798 fActionMenu->SetEnabled(true); 799 fPreferencesMenu->SetEnabled(true); 800 fHistoryMenu->SetEnabled(true); 801 fEncodingMenu->SetEnabled(true); 802 803 fButton->SetLabel(B_TRANSLATE("Search")); 804 805 fButton->SetEnabled(true); 806 fSearch->SetEnabled(true); 807 808 fSearchText->SetEnabled(true); 809 fSearchText->MakeFocus(true); 810 fSearchText->SetText(fOldPattern.String()); 811 fSearchText->TextView()->SelectAll(); 812 fSearchText->SetModificationMessage(new BMessage(MSG_SEARCH_TEXT)); 813 814 PostMessage(MSG_START_NODE_MONITORING); 815 } 816 817 818 void 819 GrepWindow::_OnNodeMonitorEvent(BMessage* message) 820 { 821 int32 opCode; 822 if (message->FindInt32("opcode", &opCode) != B_OK) 823 return; 824 825 if (fChangesIterator == NULL) { 826 fChangesIterator = new (nothrow) ChangesIterator(fModel); 827 if (fChangesIterator == NULL || !fChangesIterator->IsValid()) { 828 delete fChangesIterator; 829 fChangesIterator = NULL; 830 } 831 } 832 833 switch (opCode) { 834 case B_ENTRY_CREATED: 835 case B_ENTRY_REMOVED: 836 { 837 TRACE_NM("%s\n", opCode == B_ENTRY_CREATED ? "B_ENTRY_CREATED" 838 : "B_ENTRY_REMOVED"); 839 BString path; 840 if (message->FindString("path", &path) == B_OK) { 841 if (opCode == B_ENTRY_CREATED) { 842 if (fChangesIterator != NULL) 843 fChangesIterator->EntryAdded(path.String()); 844 } else { 845 // in order to remove temporary files 846 if (fChangesIterator != NULL) 847 fChangesIterator->EntryRemoved(path.String()); 848 // remove from the list view already 849 BEntry entry(path.String()); 850 entry_ref ref; 851 if (entry.GetRef(&ref) == B_OK) 852 fSearchResults->RemoveResults(ref, true); 853 } 854 } else { 855 #ifdef TRACE_NODE_MONITORING 856 printf("incompatible message:\n"); 857 message->PrintToStream(); 858 #endif 859 } 860 TRACE_NM("path: %s\n", path.String()); 861 break; 862 } 863 case B_ENTRY_MOVED: 864 { 865 TRACE_NM("B_ENTRY_MOVED\n"); 866 867 BString path; 868 if (message->FindString("path", &path) != B_OK) { 869 #ifdef TRACE_NODE_MONITORING 870 printf("incompatible message:\n"); 871 message->PrintToStream(); 872 #endif 873 break; 874 } 875 876 bool added; 877 if (message->FindBool("added", &added) != B_OK) 878 added = false; 879 bool removed; 880 if (message->FindBool("removed", &removed) != B_OK) 881 removed = false; 882 883 if (added) { 884 // new files 885 } else if (removed) { 886 // remove files 887 } else { 888 // files changed location, but are still within the search 889 // path! 890 BEntry entry(path.String()); 891 entry_ref ref; 892 if (entry.GetRef(&ref) == B_OK) { 893 int32 index; 894 ResultItem* item = fSearchResults->FindItem(ref, &index); 895 if (item != NULL) { 896 item->SetText(path.String()); 897 // take care of invalidation, the index is currently 898 // the full list index, but needs to be the visible 899 // items index for this 900 index = fSearchResults->IndexOf(item); 901 fSearchResults->InvalidateItem(index); 902 } 903 } 904 } 905 break; 906 } 907 case B_STAT_CHANGED: 908 case B_ATTR_CHANGED: 909 { 910 TRACE_NM("%s\n", opCode == B_STAT_CHANGED ? "B_STAT_CHANGED" 911 : "B_ATTR_CHANGED"); 912 // For directly watched files, the path will include the 913 // name. When the event occurs for a file in a watched directory, 914 // the message will have an extra name field for the respective 915 // file. 916 BString path; 917 if (message->FindString("path", &path) == B_OK) { 918 if (fChangesIterator != NULL) 919 fChangesIterator->EntryChanged(path.String()); 920 } else { 921 #ifdef TRACE_NODE_MONITORING 922 printf("incompatible message:\n"); 923 message->PrintToStream(); 924 #endif 925 } 926 TRACE_NM("path: %s\n", path.String()); 927 // message->PrintToStream(); 928 break; 929 } 930 931 default: 932 TRACE_NM("unkown op code\n"); 933 break; 934 } 935 936 fLastNodeMonitorEvent = system_time(); 937 } 938 939 940 void 941 GrepWindow::_OnNodeMonitorPulse() 942 { 943 if (fChangesIterator == NULL || fChangesIterator->IsEmpty()) 944 return; 945 946 if (system_time() - fLastNodeMonitorEvent < kChangesPulseInterval) { 947 // wait for things to settle down before running the search for changes 948 return; 949 } 950 951 if (fModel->fState != STATE_IDLE) { 952 // An update or search is still in progress. New node monitor messages 953 // may arrive while an update is still running. They should not arrive 954 // during a regular search, but we want to be prepared for that anyways 955 // and check != STATE_IDLE. 956 return; 957 } 958 959 fOldPattern = fSearchText->Text(); 960 961 #ifdef TRACE_NODE_MONITORING 962 fChangesIterator->PrintToStream(); 963 #endif 964 965 fGrepper = new (nothrow) Grepper(fOldPattern.String(), fModel, 966 this, fChangesIterator); 967 if (fGrepper != NULL && fGrepper->IsValid()) { 968 fGrepper->Start(); 969 fChangesIterator = NULL; 970 fModel->fState = STATE_UPDATE; 971 } else { 972 // roll back in case of problems 973 if (fGrepper == NULL) 974 delete fChangesIterator; 975 else { 976 // Grepper owns iterator 977 delete fGrepper; 978 fGrepper = NULL; 979 } 980 fprintf(stderr, "Out of memory.\n"); 981 } 982 } 983 984 985 void 986 GrepWindow::_OnReportFileName(BMessage* message) 987 { 988 if (fModel->fState != STATE_UPDATE) { 989 BString name = message->FindString("filename"); 990 fSearchText->TruncateString(&name, B_TRUNCATE_MIDDLE, 991 fSearchText->Bounds().Width() - 10); 992 993 fSearchText->SetText(name); 994 } 995 } 996 997 998 void 999 GrepWindow::_OnReportResult(BMessage* message) 1000 { 1001 CALLED(); 1002 entry_ref ref; 1003 if (message->FindRef("ref", &ref) != B_OK) 1004 return; 1005 1006 type_code type; 1007 int32 count; 1008 message->GetInfo("text", &type, &count); 1009 1010 BStringItem* item = NULL; 1011 if (fModel->fState == STATE_UPDATE) { 1012 // During updates because of node monitor events, negatives are 1013 // also reported (count == 0). 1014 item = fSearchResults->RemoveResults(ref, count == 0); 1015 } 1016 1017 if (count == 0) 1018 return; 1019 1020 if (item == NULL) { 1021 item = new ResultItem(ref); 1022 fSearchResults->AddItem(item); 1023 item->SetExpanded(fShowLinesCheckbox->Value() == 1); 1024 } 1025 1026 const char* buf; 1027 while (message->FindString("text", --count, &buf) == B_OK) { 1028 uchar* temp = (uchar*)strdup(buf); 1029 uchar* ptr = temp; 1030 1031 while (true) { 1032 // replace all non-printable characters by spaces 1033 uchar c = *ptr; 1034 1035 if (c == '\0') 1036 break; 1037 1038 if (!(c & 0x80) && iscntrl(c)) 1039 *ptr = ' '; 1040 1041 ++ptr; 1042 } 1043 1044 fSearchResults->AddUnder(new BStringItem((const char*)temp), item); 1045 1046 free(temp); 1047 } 1048 } 1049 1050 1051 void 1052 GrepWindow::_OnReportError(BMessage* message) 1053 { 1054 const char* buf; 1055 if (message->FindString("error", &buf) == B_OK) 1056 fSearchResults->AddItem(new BStringItem(buf)); 1057 } 1058 1059 1060 void 1061 GrepWindow::_OnRecurseLinks() 1062 { 1063 fModel->fRecurseLinks = !fModel->fRecurseLinks; 1064 fRecurseLinks->SetMarked(fModel->fRecurseLinks); 1065 _ModelChanged(); 1066 } 1067 1068 1069 void 1070 GrepWindow::_OnRecurseDirs() 1071 { 1072 fModel->fRecurseDirs = !fModel->fRecurseDirs; 1073 fRecurseDirs->SetMarked(fModel->fRecurseDirs); 1074 _ModelChanged(); 1075 } 1076 1077 1078 void 1079 GrepWindow::_OnSkipDotDirs() 1080 { 1081 fModel->fSkipDotDirs = !fModel->fSkipDotDirs; 1082 fSkipDotDirs->SetMarked(fModel->fSkipDotDirs); 1083 _ModelChanged(); 1084 } 1085 1086 1087 void 1088 GrepWindow::_OnRegularExpression() 1089 { 1090 fModel->fRegularExpression = !fModel->fRegularExpression; 1091 fRegularExpression->SetMarked(fModel->fRegularExpression); 1092 _ModelChanged(); 1093 } 1094 1095 1096 void 1097 GrepWindow::_OnCaseSensitive() 1098 { 1099 fModel->fCaseSensitive = !fModel->fCaseSensitive; 1100 fCaseSensitive->SetMarked(fModel->fCaseSensitive); 1101 _ModelChanged(); 1102 } 1103 1104 1105 void 1106 GrepWindow::_OnTextOnly() 1107 { 1108 fModel->fTextOnly = !fModel->fTextOnly; 1109 fTextOnly->SetMarked(fModel->fTextOnly); 1110 _ModelChanged(); 1111 } 1112 1113 1114 void 1115 GrepWindow::_OnInvokeEditor() 1116 { 1117 fModel->fInvokeEditor = !fModel->fInvokeEditor; 1118 fInvokeEditor->SetMarked(fModel->fInvokeEditor); 1119 _SavePrefs(); 1120 } 1121 1122 1123 void 1124 GrepWindow::_OnCheckboxShowLines() 1125 { 1126 // Selection in BOutlineListView in multiple selection mode 1127 // gets weird when collapsing. I've tried all sorts of things. 1128 // It seems impossible to make it behave just right. 1129 1130 // Going from collapsed to expande mode, the superitems 1131 // keep their selection, the subitems don't (yet) have 1132 // a selection. This works as expected, AFAIK. 1133 1134 // Going from expanded to collapsed mode, I would like 1135 // for a selected subitem (line) to select its superitem, 1136 // (its file) and the subitem be unselected. 1137 1138 // I've successfully tried code patches that apply the 1139 // selection pattern that I want, but with weird effects 1140 // on subsequent manual selection. 1141 // Lines stay selected while the user tries to select 1142 // some other line. It just gets weird. 1143 1144 // It's as though listItem->Select() and Deselect() 1145 // put the items in some semi-selected state. 1146 // Or maybe I've got it all wrong. 1147 1148 // So, here's the plain basic collapse/expand. 1149 // I think it's the least bad of what's possible on BeOS R5, 1150 // but perhaps someone comes along with a patch of magic. 1151 1152 fModel->fShowLines = (fShowLinesCheckbox->Value() == 1); 1153 1154 int32 numItems = fSearchResults->FullListCountItems(); 1155 for (int32 x = 0; x < numItems; ++x) { 1156 BListItem* listItem = fSearchResults->FullListItemAt(x); 1157 if (listItem->OutlineLevel() == 0) { 1158 if (fModel->fShowLines) { 1159 if (!fSearchResults->IsExpanded(x)) 1160 fSearchResults->Expand(listItem); 1161 } else { 1162 if (fSearchResults->IsExpanded(x)) 1163 fSearchResults->Collapse(listItem); 1164 } 1165 } 1166 } 1167 1168 fSearchResults->Invalidate(); 1169 1170 _SavePrefs(); 1171 } 1172 1173 1174 void 1175 GrepWindow::_OnInvokeItem() 1176 { 1177 for (int32 selectionIndex = 0; ; selectionIndex++) { 1178 int32 itemIndex = fSearchResults->CurrentSelection(selectionIndex); 1179 BListItem* item = fSearchResults->ItemAt(itemIndex); 1180 if (item == NULL) 1181 break; 1182 1183 int32 level = item->OutlineLevel(); 1184 int32 lineNum = -1; 1185 1186 // Get the line number. 1187 // only this level has line numbers 1188 if (level == 1) { 1189 BStringItem* str = dynamic_cast<BStringItem*>(item); 1190 if (str != NULL) { 1191 lineNum = atol(str->Text()); 1192 // fortunately, atol knows when to stop the conversion 1193 } 1194 } 1195 1196 // Get the top-most item and launch its entry_ref. 1197 while (level != 0) { 1198 item = fSearchResults->Superitem(item); 1199 if (item == NULL) 1200 break; 1201 level = item->OutlineLevel(); 1202 } 1203 1204 ResultItem* entry = dynamic_cast<ResultItem*>(item); 1205 if (entry != NULL) { 1206 if (fModel->fInvokeEditor && _OpenInEditor(entry->ref, lineNum)) 1207 return; 1208 1209 // ask tracker to open it for us 1210 BMessenger target(TRACKER_SIGNATURE); 1211 BMessage message(B_REFS_RECEIVED); 1212 message.AddRef("refs", &entry->ref); 1213 if (lineNum > -1) { 1214 message.AddInt32("be:line", lineNum); 1215 } 1216 target.SendMessage(&message); 1217 } 1218 } 1219 } 1220 1221 1222 void 1223 GrepWindow::_OnSearchText() 1224 { 1225 CALLED(); 1226 1227 bool enabled = fSearchText->TextView()->TextLength() != 0; 1228 fButton->SetEnabled(enabled); 1229 fSearch->SetEnabled(enabled); 1230 _StopNodeMonitoring(); 1231 } 1232 1233 1234 void 1235 GrepWindow::_OnHistoryItem(BMessage* message) 1236 { 1237 const char* buf; 1238 if (message->FindString("text", &buf) == B_OK) 1239 fSearchText->SetText(buf); 1240 } 1241 1242 1243 void 1244 GrepWindow::_OnTrimSelection() 1245 { 1246 if (fSearchResults->CurrentSelection() < 0) { 1247 BString text; 1248 text << B_TRANSLATE("Please select the files you wish to keep searching."); 1249 text << "\n"; 1250 text << B_TRANSLATE("The unselected files will be removed from the list."); 1251 text << "\n"; 1252 BAlert* alert = new BAlert(NULL, text.String(), B_TRANSLATE("OK"), NULL, NULL, 1253 B_WIDTH_AS_USUAL, B_WARNING_ALERT); 1254 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 1255 alert->Go(NULL); 1256 return; 1257 } 1258 1259 BMessage message; 1260 BString path; 1261 1262 for (int32 index = 0; ; index++) { 1263 BStringItem* item = dynamic_cast<BStringItem*>( 1264 fSearchResults->ItemAt(index)); 1265 if (item == NULL) 1266 break; 1267 1268 if (!item->IsSelected() || item->OutlineLevel() != 0) 1269 continue; 1270 1271 if (path == item->Text()) 1272 continue; 1273 1274 path = item->Text(); 1275 entry_ref ref; 1276 if (get_ref_for_path(path.String(), &ref) == B_OK) 1277 message.AddRef("refs", &ref); 1278 } 1279 1280 fModel->fDirectory = entry_ref(); 1281 // invalidated on purpose 1282 1283 fModel->fSelectedFiles.MakeEmpty(); 1284 fModel->fSelectedFiles = message; 1285 1286 PostMessage(MSG_START_CANCEL); 1287 1288 _SetWindowTitle(); 1289 } 1290 1291 1292 void 1293 GrepWindow::_OnCopyText() 1294 { 1295 bool onlyCopySelection = true; 1296 1297 if (fSearchResults->CurrentSelection() < 0) 1298 onlyCopySelection = false; 1299 1300 BString buffer; 1301 1302 for (int32 index = 0; ; index++) { 1303 BStringItem* item = dynamic_cast<BStringItem*>( 1304 fSearchResults->ItemAt(index)); 1305 if (item == NULL) 1306 break; 1307 1308 if (onlyCopySelection) { 1309 if (item->IsSelected()) 1310 buffer << item->Text() << "\n"; 1311 } else 1312 buffer << item->Text() << "\n"; 1313 } 1314 1315 status_t status = B_OK; 1316 1317 BMessage* clip = NULL; 1318 1319 if (be_clipboard->Lock()) { 1320 be_clipboard->Clear(); 1321 1322 clip = be_clipboard->Data(); 1323 1324 clip->AddData("text/plain", B_MIME_TYPE, buffer.String(), 1325 buffer.Length()); 1326 1327 status = be_clipboard->Commit(); 1328 1329 if (status != B_OK) { 1330 be_clipboard->Unlock(); 1331 return; 1332 } 1333 1334 be_clipboard->Unlock(); 1335 } 1336 } 1337 1338 1339 void 1340 GrepWindow::_OnSelectInTracker() 1341 { 1342 if (fSearchResults->CurrentSelection() < 0) { 1343 BAlert* alert = new BAlert("Info", 1344 B_TRANSLATE("Please select the files you wish to have selected for you in " 1345 "Tracker."), 1346 B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); 1347 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 1348 alert->Go(NULL); 1349 return; 1350 } 1351 1352 BMessage message; 1353 BString filePath; 1354 BPath folderPath; 1355 BList folderList; 1356 BString lastFolderAddedToList; 1357 1358 for (int32 index = 0; ; index++) { 1359 BStringItem* item = dynamic_cast<BStringItem*>( 1360 fSearchResults->ItemAt(index)); 1361 if (item == NULL) 1362 break; 1363 1364 // only open selected and top level (file) items 1365 if (!item->IsSelected() || item->OutlineLevel() > 0) 1366 continue; 1367 1368 // check if this was previously opened 1369 if (filePath == item->Text()) 1370 continue; 1371 1372 filePath = item->Text(); 1373 entry_ref file_ref; 1374 if (get_ref_for_path(filePath.String(), &file_ref) != B_OK) 1375 continue; 1376 1377 message.AddRef("refs", &file_ref); 1378 1379 // add parent folder to list of folders to open 1380 folderPath.SetTo(filePath.String()); 1381 if (folderPath.GetParent(&folderPath) == B_OK) { 1382 BPath* path = new BPath(folderPath); 1383 if (path->Path() != lastFolderAddedToList) { 1384 // catches some duplicates 1385 folderList.AddItem(path); 1386 lastFolderAddedToList = path->Path(); 1387 } else 1388 delete path; 1389 } 1390 } 1391 1392 _RemoveFolderListDuplicates(&folderList); 1393 _OpenFoldersInTracker(&folderList); 1394 1395 int32 aShortWhile = 100000; 1396 snooze(aShortWhile); 1397 1398 if (!_AreAllFoldersOpenInTracker(&folderList)) { 1399 for (int32 x = 0; x < 5; x++) { 1400 aShortWhile += 100000; 1401 snooze(aShortWhile); 1402 _OpenFoldersInTracker(&folderList); 1403 } 1404 } 1405 1406 if (!_AreAllFoldersOpenInTracker(&folderList)) { 1407 BString str1; 1408 str1 << B_TRANSLATE("%APP_NAME couldn't open one or more folders."); 1409 str1.ReplaceFirst("%APP_NAME", B_TRANSLATE_NOCOLLECT(kAppName)); 1410 BAlert* alert = new BAlert(NULL, str1.String(), B_TRANSLATE("OK"), 1411 NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT); 1412 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 1413 alert->Go(NULL); 1414 goto out; 1415 } 1416 1417 _SelectFilesInTracker(&folderList, &message); 1418 1419 out: 1420 // delete folderList contents 1421 int32 folderCount = folderList.CountItems(); 1422 for (int32 x = 0; x < folderCount; x++) 1423 delete static_cast<BPath*>(folderList.ItemAt(x)); 1424 } 1425 1426 1427 void 1428 GrepWindow::_OnQuitNow() 1429 { 1430 if (be_app->Lock()) { 1431 be_app->PostMessage(B_QUIT_REQUESTED); 1432 be_app->Unlock(); 1433 } 1434 } 1435 1436 1437 void 1438 GrepWindow::_OnFileDrop(BMessage* message) 1439 { 1440 if (fModel->fState != STATE_IDLE) 1441 return; 1442 1443 entry_ref directory; 1444 _InitRefsReceived(&directory, message); 1445 1446 fModel->fDirectory = directory; 1447 fModel->fSelectedFiles.MakeEmpty(); 1448 fModel->fSelectedFiles = *message; 1449 1450 fSearchResults->MakeEmpty(); 1451 fOldPattern = ""; 1452 1453 _UpdateMenus(); 1454 _SetWindowTitle(); 1455 } 1456 1457 1458 void 1459 GrepWindow::_OnRefsReceived(BMessage* message) 1460 { 1461 _OnFileDrop(message); 1462 fOldPattern = ""; 1463 // It seems a B_CANCEL always follows a B_REFS_RECEIVED 1464 // from a BFilePanel in Open mode. 1465 // 1466 // _OnOpenPanelCancel() is called on B_CANCEL. 1467 // That's where saving the current dir of the file panel occurs, for now, 1468 // and also the neccesary deletion of the file panel object. 1469 // A hidden file panel would otherwise jam the shutdown process. 1470 } 1471 1472 1473 void 1474 GrepWindow::_OnOpenPanel() 1475 { 1476 if (fFilePanel != NULL) 1477 return; 1478 1479 entry_ref path; 1480 if (get_ref_for_path(fModel->fFilePanelPath.String(), &path) != B_OK) 1481 return; 1482 1483 BMessenger messenger(this); 1484 BMessage message(MSG_REFS_RECEIVED); 1485 fFilePanel = new BFilePanel(B_OPEN_PANEL, &messenger, &path, 1486 B_FILE_NODE | B_DIRECTORY_NODE | B_SYMLINK_NODE, true, 1487 &message, NULL, true, true); 1488 1489 fFilePanel->Show(); 1490 } 1491 1492 1493 void 1494 GrepWindow::_OnOpenPanelCancel() 1495 { 1496 entry_ref panelDirRef; 1497 fFilePanel->GetPanelDirectory(&panelDirRef); 1498 BPath path(&panelDirRef); 1499 fModel->fFilePanelPath = path.Path(); 1500 delete fFilePanel; 1501 fFilePanel = NULL; 1502 } 1503 1504 1505 void 1506 GrepWindow::_OnSelectAll(BMessage* message) 1507 { 1508 BMessenger messenger(fSearchResults); 1509 messenger.SendMessage(B_SELECT_ALL); 1510 } 1511 1512 1513 void 1514 GrepWindow::_OnNewWindow() 1515 { 1516 BMessage cloneRefs; 1517 // we don't want GrepWindow::InitRefsReceived() 1518 // to mess with the refs of the current window 1519 1520 cloneRefs = fModel->fSelectedFiles; 1521 cloneRefs.AddRef("dir_ref", &(fModel->fDirectory)); 1522 1523 new GrepWindow(&cloneRefs); 1524 } 1525 1526 1527 void 1528 GrepWindow::_OnSetTargetToParent() 1529 { 1530 BEntry entry(&(fModel->fDirectory)); 1531 BEntry parent; 1532 1533 if (entry.GetParent(&parent) == B_OK) { 1534 entry_ref parent_ref; 1535 parent.GetRef(&parent_ref); 1536 1537 BMessage parentRefs; 1538 parentRefs.AddRef("dir_ref", &parent_ref); 1539 _OnFileDrop(&parentRefs); 1540 } 1541 } 1542 1543 1544 // #pragma mark - 1545 1546 1547 void 1548 GrepWindow::_ModelChanged() 1549 { 1550 CALLED(); 1551 1552 _StopNodeMonitoring(); 1553 _SavePrefs(); 1554 } 1555 1556 bool 1557 GrepWindow::_OpenInEditor(const entry_ref &ref, int32 lineNum) 1558 { 1559 BMessage message(B_REFS_RECEIVED); 1560 message.AddRef("refs", &ref); 1561 1562 if (lineNum != -1) { 1563 message.AddInt32("line", lineNum); // for Pe 1564 message.AddInt32("be:line", lineNum); 1565 } 1566 1567 // Find the preferred code editor 1568 char editorSig[B_MIME_TYPE_LENGTH]; 1569 BMimeType mimeType("text/x-source-code"); 1570 mimeType.GetPreferredApp(editorSig); 1571 1572 entry_ref editor; 1573 if (be_roster->FindApp(editorSig, &editor) != B_OK) 1574 return false; 1575 1576 if (be_roster->IsRunning(&editor)) { 1577 BMessenger msngr(NULL, be_roster->TeamFor(&editor)); 1578 if (msngr.SendMessage(&message) != B_OK) 1579 return false; 1580 } else { 1581 if (be_roster->Launch(&editor, &message) != B_OK) 1582 return false; 1583 } 1584 1585 return true; 1586 } 1587 1588 1589 void 1590 GrepWindow::_RemoveFolderListDuplicates(BList* folderList) 1591 { 1592 if (folderList == NULL) 1593 return; 1594 1595 int32 folderCount = folderList->CountItems(); 1596 BString folderX; 1597 BString folderY; 1598 1599 for (int32 x = 0; x < folderCount; x++) { 1600 BPath* path = static_cast<BPath*>(folderList->ItemAt(x)); 1601 folderX = path->Path(); 1602 1603 for (int32 y = x + 1; y < folderCount; y++) { 1604 path = static_cast<BPath*>(folderList->ItemAt(y)); 1605 folderY = path->Path(); 1606 if (folderX == folderY) { 1607 delete static_cast<BPath*>(folderList->RemoveItem(y)); 1608 folderCount--; 1609 y--; 1610 } 1611 } 1612 } 1613 } 1614 1615 1616 status_t 1617 GrepWindow::_OpenFoldersInTracker(BList* folderList) 1618 { 1619 status_t status = B_OK; 1620 BMessage refsMsg(B_REFS_RECEIVED); 1621 1622 int32 folderCount = folderList->CountItems(); 1623 for (int32 index = 0; index < folderCount; index++) { 1624 BPath* path = static_cast<BPath*>(folderList->ItemAt(index)); 1625 1626 entry_ref folderRef; 1627 status = get_ref_for_path(path->Path(), &folderRef); 1628 if (status != B_OK) 1629 return status; 1630 1631 status = refsMsg.AddRef("refs", &folderRef); 1632 if (status != B_OK) 1633 return status; 1634 } 1635 1636 status = be_roster->Launch(TRACKER_SIGNATURE, &refsMsg); 1637 if (status != B_OK && status != B_ALREADY_RUNNING) 1638 return status; 1639 1640 return B_OK; 1641 } 1642 1643 1644 bool 1645 GrepWindow::_AreAllFoldersOpenInTracker(BList* folderList) 1646 { 1647 // Compare the folders we want open in Tracker to 1648 // the actual Tracker windows currently open. 1649 1650 // We build a list of open Tracker windows, and compare 1651 // it to the list of folders we want open in Tracker. 1652 1653 // If all folders exists in the list of Tracker windows 1654 // return true 1655 1656 status_t status = B_OK; 1657 BMessenger trackerMessenger(TRACKER_SIGNATURE); 1658 BMessage sendMessage; 1659 BMessage replyMessage; 1660 BList windowList; 1661 1662 if (!trackerMessenger.IsValid()) 1663 return false; 1664 1665 for (int32 count = 1; ; count++) { 1666 sendMessage.MakeEmpty(); 1667 replyMessage.MakeEmpty(); 1668 1669 sendMessage.what = B_GET_PROPERTY; 1670 sendMessage.AddSpecifier("Path"); 1671 sendMessage.AddSpecifier("Poses"); 1672 sendMessage.AddSpecifier("Window", count); 1673 1674 status = trackerMessenger.SendMessage(&sendMessage, &replyMessage); 1675 if (status != B_OK) 1676 return false; 1677 1678 entry_ref* trackerRef = new (nothrow) entry_ref; 1679 status = replyMessage.FindRef("result", trackerRef); 1680 if (status != B_OK || !windowList.AddItem(trackerRef)) { 1681 delete trackerRef; 1682 break; 1683 } 1684 } 1685 1686 int32 folderCount = folderList->CountItems(); 1687 int32 windowCount = windowList.CountItems(); 1688 1689 int32 found = 0; 1690 BPath* folderPath; 1691 entry_ref* windowRef; 1692 BString folderString; 1693 BString windowString; 1694 bool result = false; 1695 1696 if (folderCount > windowCount) { 1697 // at least one folder is not open in Tracker 1698 goto out; 1699 } 1700 1701 // Loop over the two lists and see if all folders exist as window 1702 for (int32 x = 0; x < folderCount; x++) { 1703 for (int32 y = 0; y < windowCount; y++) { 1704 1705 folderPath = static_cast<BPath*>(folderList->ItemAt(x)); 1706 windowRef = static_cast<entry_ref*>(windowList.ItemAt(y)); 1707 1708 if (folderPath == NULL) 1709 break; 1710 1711 if (windowRef == NULL) 1712 break; 1713 1714 folderString = folderPath->Path(); 1715 1716 BEntry entry; 1717 BPath path; 1718 1719 if (entry.SetTo(windowRef) == B_OK && path.SetTo(&entry) == B_OK) { 1720 1721 windowString = path.Path(); 1722 1723 if (folderString == windowString) { 1724 found++; 1725 break; 1726 } 1727 } 1728 } 1729 } 1730 1731 result = found == folderCount; 1732 1733 out: 1734 // delete list of window entry_refs 1735 for (int32 x = 0; x < windowCount; x++) 1736 delete static_cast<entry_ref*>(windowList.ItemAt(x)); 1737 1738 return result; 1739 } 1740 1741 1742 status_t 1743 GrepWindow::_SelectFilesInTracker(BList* folderList, BMessage* refsMessage) 1744 { 1745 // loops over Tracker windows, find each windowRef, 1746 // extract the refs that are children of windowRef, 1747 // add refs to selection-message 1748 1749 status_t status = B_OK; 1750 BMessenger trackerMessenger(TRACKER_SIGNATURE); 1751 BMessage windowSendMessage; 1752 BMessage windowReplyMessage; 1753 BMessage selectionSendMessage; 1754 BMessage selectionReplyMessage; 1755 1756 if (!trackerMessenger.IsValid()) 1757 return status; 1758 1759 // loop over Tracker windows 1760 for (int32 windowCount = 1; ; windowCount++) { 1761 1762 windowSendMessage.MakeEmpty(); 1763 windowReplyMessage.MakeEmpty(); 1764 1765 windowSendMessage.what = B_GET_PROPERTY; 1766 windowSendMessage.AddSpecifier("Path"); 1767 windowSendMessage.AddSpecifier("Poses"); 1768 windowSendMessage.AddSpecifier("Window", windowCount); 1769 1770 status = trackerMessenger.SendMessage(&windowSendMessage, 1771 &windowReplyMessage); 1772 1773 if (status != B_OK) 1774 return status; 1775 1776 entry_ref windowRef; 1777 status = windowReplyMessage.FindRef("result", &windowRef); 1778 if (status != B_OK) 1779 break; 1780 1781 int32 folderCount = folderList->CountItems(); 1782 1783 // loop over folders in folderList 1784 for (int32 x = 0; x < folderCount; x++) { 1785 BPath* folderPath = static_cast<BPath*>(folderList->ItemAt(x)); 1786 if (folderPath == NULL) 1787 break; 1788 1789 BString folderString = folderPath->Path(); 1790 1791 BEntry windowEntry; 1792 BPath windowPath; 1793 BString windowString; 1794 1795 status = windowEntry.SetTo(&windowRef); 1796 if (status != B_OK) 1797 break; 1798 1799 status = windowPath.SetTo(&windowEntry); 1800 if (status != B_OK) 1801 break; 1802 1803 windowString = windowPath.Path(); 1804 1805 // if match, loop over items in refsMessage 1806 // and add those that live in window/folder 1807 // to a selection message 1808 1809 if (windowString == folderString) { 1810 selectionSendMessage.MakeEmpty(); 1811 selectionSendMessage.what = B_SET_PROPERTY; 1812 selectionReplyMessage.MakeEmpty(); 1813 1814 // loop over refs and add to message 1815 entry_ref ref; 1816 for (int32 index = 0; ; index++) { 1817 status = refsMessage->FindRef("refs", index, &ref); 1818 if (status != B_OK) 1819 break; 1820 1821 BDirectory directory(&windowRef); 1822 BEntry entry(&ref); 1823 if (directory.Contains(&entry)) 1824 selectionSendMessage.AddRef("data", &ref); 1825 } 1826 1827 // finish selection message 1828 selectionSendMessage.AddSpecifier("Selection"); 1829 selectionSendMessage.AddSpecifier("Poses"); 1830 selectionSendMessage.AddSpecifier("Window", windowCount); 1831 1832 trackerMessenger.SendMessage(&selectionSendMessage, 1833 &selectionReplyMessage); 1834 } 1835 } 1836 } 1837 1838 return B_OK; 1839 } 1840