Intermediate commit, because I want to rename FolderIterator but it has changes.

* Beginnings of node monitoring support. Currently disabled, but detects
  new, changed and removed files. Folders untested yet. There may also be
  a problem with the toplevel folders when a pose selection message is used.
  That's untested too as of yet.
* Removed some superfluous whitespace.
* Small refactoring in FolderIterator to access some stuff from the outside
  as well.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26795 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2008-08-04 15:16:30 +00:00
parent 34d496f8c3
commit 3b364ddd42
6 changed files with 179 additions and 62 deletions
+4 -1
View File
@@ -36,7 +36,10 @@ public:
// Returns the full path name of the next file. // Returns the full path name of the next file.
virtual bool GetNextName(char* buffer) = 0; virtual bool GetNextName(char* buffer) = 0;
// Tells the Grepper whether the targets wants to know about negative hits.
virtual bool NotifyNegatives() const = 0;
protected: protected:
// Determines whether we can grep a file. // Determines whether we can grep a file.
bool _ExamineFile(BEntry& entry, char* buffer, bool _ExamineFile(BEntry& entry, char* buffer,
+35 -18
View File
@@ -109,21 +109,15 @@ FolderIterator::GetNextName(char* buffer)
} }
// #pragma mark - private
bool bool
FolderIterator::_GetNextEntry(BEntry& entry) FolderIterator::NotifyNegatives() const
{ {
if (fDirectories.CountItems() == 1) return false;
return _GetTopEntry(entry);
else
return _GetSubEntry(entry);
} }
bool bool
FolderIterator::_GetTopEntry(BEntry& entry) FolderIterator::GetTopEntry(BEntry& entry)
{ {
// If the user selected one or more files, we must look // If the user selected one or more files, we must look
// at the "refs" inside the message that was passed into // at the "refs" inside the message that was passed into
@@ -150,6 +144,37 @@ FolderIterator::_GetTopEntry(BEntry& entry)
} }
bool
FolderIterator::FollowSubdir(BEntry& entry) const
{
if (!fRecurseDirs)
return false;
if (fSkipDotDirs) {
char nameBuf[B_FILE_NAME_LENGTH];
if (entry.GetName(nameBuf) == B_OK) {
if (*nameBuf == '.')
return false;
}
}
return true;
}
// #pragma mark - private
bool
FolderIterator::_GetNextEntry(BEntry& entry)
{
if (fDirectories.CountItems() == 1)
return GetTopEntry(entry);
else
return _GetSubEntry(entry);
}
bool bool
FolderIterator::_GetSubEntry(BEntry& entry) FolderIterator::_GetSubEntry(BEntry& entry)
{ {
@@ -173,17 +198,9 @@ FolderIterator::_GetSubEntry(BEntry& entry)
void void
FolderIterator::_ExamineSubdir(BEntry& entry) FolderIterator::_ExamineSubdir(BEntry& entry)
{ {
if (!fRecurseDirs) if (!FollowSubdir(entry))
return; return;
if (fSkipDotDirs) {
char nameBuf[B_FILE_NAME_LENGTH];
if (entry.GetName(nameBuf) == B_OK) {
if (*nameBuf == '.')
return;
}
}
BDirectory* dir = new (nothrow) BDirectory(&entry); BDirectory* dir = new (nothrow) BDirectory(&entry);
if (dir == NULL || dir->InitCheck() != B_OK || !fDirectories.AddItem(dir)) { if (dir == NULL || dir->InitCheck() != B_OK || !fDirectories.AddItem(dir)) {
// clean up // clean up
+7 -5
View File
@@ -47,17 +47,19 @@ public:
virtual ~FolderIterator(); virtual ~FolderIterator();
virtual bool IsValid() const; virtual bool IsValid() const;
// Returns the full path name of the next file.
virtual bool GetNextName(char* buffer); virtual bool GetNextName(char* buffer);
virtual bool NotifyNegatives() const;
// Looks for the next entry in the top-level dir.
bool GetTopEntry(BEntry& entry);
// Should this subfolder be followed?
bool FollowSubdir(BEntry& entry) const;
private: private:
// Looks for the next entry. // Looks for the next entry.
bool _GetNextEntry(BEntry& entry); bool _GetNextEntry(BEntry& entry);
// Looks for the next entry in the top-level dir.
bool _GetTopEntry(BEntry& entry);
// Looks for the next entry in a subdir. // Looks for the next entry in a subdir.
bool _GetSubEntry(BEntry& entry); bool _GetSubEntry(BEntry& entry);
+125 -36
View File
@@ -34,6 +34,7 @@
#include <Alert.h> #include <Alert.h>
#include <Clipboard.h> #include <Clipboard.h>
#include <Path.h> #include <Path.h>
#include <PathMonitor.h>
#include <Roster.h> #include <Roster.h>
#include <String.h> #include <String.h>
#include <UTF8.h> #include <UTF8.h>
@@ -155,133 +156,137 @@ void GrepWindow::MessageReceived(BMessage *message)
case B_ABOUT_REQUESTED: case B_ABOUT_REQUESTED:
_OnAboutRequested(); _OnAboutRequested();
break; break;
case MSG_NEW_WINDOW: case MSG_NEW_WINDOW:
_OnNewWindow(); _OnNewWindow();
break; break;
case B_SIMPLE_DATA: case B_SIMPLE_DATA:
_OnFileDrop(message); _OnFileDrop(message);
break; break;
case MSG_OPEN_PANEL: case MSG_OPEN_PANEL:
_OnOpenPanel(); _OnOpenPanel();
break; break;
case MSG_REFS_RECEIVED: case MSG_REFS_RECEIVED:
_OnRefsReceived(message); _OnRefsReceived(message);
break; break;
case B_CANCEL: case B_CANCEL:
_OnOpenPanelCancel(); _OnOpenPanelCancel();
break; break;
case MSG_RECURSE_LINKS: case MSG_RECURSE_LINKS:
_OnRecurseLinks(); _OnRecurseLinks();
break; break;
case MSG_RECURSE_DIRS: case MSG_RECURSE_DIRS:
_OnRecurseDirs(); _OnRecurseDirs();
break; break;
case MSG_SKIP_DOT_DIRS: case MSG_SKIP_DOT_DIRS:
_OnSkipDotDirs(); _OnSkipDotDirs();
break; break;
case MSG_CASE_SENSITIVE: case MSG_CASE_SENSITIVE:
_OnCaseSensitive(); _OnCaseSensitive();
break; break;
case MSG_ESCAPE_TEXT: case MSG_ESCAPE_TEXT:
_OnEscapeText(); _OnEscapeText();
break; break;
case MSG_TEXT_ONLY: case MSG_TEXT_ONLY:
_OnTextOnly(); _OnTextOnly();
break; break;
case MSG_INVOKE_PE: case MSG_INVOKE_PE:
_OnInvokePe(); _OnInvokePe();
break; break;
case MSG_SEARCH_TEXT: case MSG_SEARCH_TEXT:
_OnSearchText(); _OnSearchText();
break; break;
case MSG_SELECT_HISTORY: case MSG_SELECT_HISTORY:
_OnHistoryItem(message); _OnHistoryItem(message);
break; break;
case MSG_START_CANCEL: case MSG_START_CANCEL:
_OnStartCancel(); _OnStartCancel();
break; break;
case MSG_SEARCH_FINISHED: case MSG_SEARCH_FINISHED:
_OnSearchFinished(); _OnSearchFinished();
break; break;
case B_PATH_MONITOR:
_OnNodeMonitorEvent(message);
break;
case MSG_REPORT_FILE_NAME: case MSG_REPORT_FILE_NAME:
_OnReportFileName(message); _OnReportFileName(message);
break; break;
case MSG_REPORT_RESULT: case MSG_REPORT_RESULT:
_OnReportResult(message); _OnReportResult(message);
break; break;
case MSG_REPORT_ERROR: case MSG_REPORT_ERROR:
_OnReportError(message); _OnReportError(message);
break; break;
case MSG_SELECT_ALL: case MSG_SELECT_ALL:
_OnSelectAll(message); _OnSelectAll(message);
break; break;
case MSG_TRIM_SELECTION: case MSG_TRIM_SELECTION:
_OnTrimSelection(); _OnTrimSelection();
break; break;
case MSG_COPY_TEXT: case MSG_COPY_TEXT:
_OnCopyText(); _OnCopyText();
break; break;
case MSG_SELECT_IN_TRACKER: case MSG_SELECT_IN_TRACKER:
_OnSelectInTracker(); _OnSelectInTracker();
break; break;
case MSG_MENU_SHOW_LINES: case MSG_MENU_SHOW_LINES:
_OnMenuShowLines(); _OnMenuShowLines();
break; break;
case MSG_CHECKBOX_SHOW_LINES: case MSG_CHECKBOX_SHOW_LINES:
_OnCheckboxShowLines(); _OnCheckboxShowLines();
break; break;
case MSG_OPEN_SELECTION: case MSG_OPEN_SELECTION:
// fall through // fall through
case MSG_INVOKE_ITEM: case MSG_INVOKE_ITEM:
_OnInvokeItem(); _OnInvokeItem();
break; break;
case MSG_QUIT_NOW: case MSG_QUIT_NOW:
_OnQuitNow(); _OnQuitNow();
break; break;
case 'utf8': case 'utf8':
fModel->fEncoding = 0; fModel->fEncoding = 0;
break; break;
case B_SJIS_CONVERSION: case B_SJIS_CONVERSION:
fModel->fEncoding = B_SJIS_CONVERSION; fModel->fEncoding = B_SJIS_CONVERSION;
break; break;
case B_EUC_CONVERSION: case B_EUC_CONVERSION:
fModel->fEncoding = B_EUC_CONVERSION; fModel->fEncoding = B_EUC_CONVERSION;
break; break;
case B_JIS_CONVERSION: case B_JIS_CONVERSION:
fModel->fEncoding = B_JIS_CONVERSION; fModel->fEncoding = B_JIS_CONVERSION;
break; break;
default: default:
BWindow::MessageReceived(message); BWindow::MessageReceived(message);
break; break;
@@ -292,6 +297,7 @@ void GrepWindow::MessageReceived(BMessage *message)
void void
GrepWindow::Quit() GrepWindow::Quit()
{ {
_StopNodeMonitoring();
_SavePrefs(); _SavePrefs();
// TODO: stippi: Looks like this could be done // TODO: stippi: Looks like this could be done
@@ -663,12 +669,57 @@ GrepWindow::_SavePrefs()
} }
void
GrepWindow::_StartNodeMonitoring()
{
BMessenger messenger(this);
uint32 fileFlags = B_WATCH_NAME | B_WATCH_STAT;
uint32 dirFlags = B_WATCH_DIRECTORY | B_WATCH_NAME;
// watch the top level folder
BPath path(&fModel->fDirectory);
if (path.InitCheck() == B_OK) {
printf("start monitoring root folder: %s\n", path.Path());
BPrivate::BPathMonitor::StartWatching(path.Path(), dirFlags, messenger);
}
FolderIterator iterator(fModel);
BEntry entry;
while (iterator.GetTopEntry(entry)) {
path.SetTo(&entry);
if (entry.IsDirectory()) {
// subfolder
if (iterator.FollowSubdir(entry)) {
printf("start monitoring folder: %s\n", path.Path());
BPrivate::BPathMonitor::StartWatching(path.Path(),
dirFlags | B_WATCH_RECURSIVELY, messenger);
}
} else {
// regular file
printf("start monitoring file: %s\n", path.Path());
BPrivate::BPathMonitor::StartWatching(path.Path(), fileFlags,
messenger);
}
}
}
void
GrepWindow::_StopNodeMonitoring()
{
BPrivate::BPathMonitor::StopWatching(BMessenger(this));
}
// #pragma mark - events // #pragma mark - events
void void
GrepWindow::_OnStartCancel() GrepWindow::_OnStartCancel()
{ {
_StopNodeMonitoring();
if (fModel->fState == STATE_IDLE) { if (fModel->fState == STATE_IDLE) {
fModel->fState = STATE_SEARCH; fModel->fState = STATE_SEARCH;
@@ -714,8 +765,11 @@ GrepWindow::_OnStartCancel()
// roll back in case of problems // roll back in case of problems
if (fGrepper == NULL) if (fGrepper == NULL)
delete iterator; delete iterator;
delete fGrepper; else {
fGrepper = NULL; // Grepper owns iterator
delete fGrepper;
fGrepper = NULL;
}
fModel->fState = STATE_CANCEL; fModel->fState = STATE_CANCEL;
// TODO: better notification to user // TODO: better notification to user
fprintf(stderr, "Out of memory.\n"); fprintf(stderr, "Out of memory.\n");
@@ -732,6 +786,8 @@ GrepWindow::_OnSearchFinished()
{ {
fModel->fState = STATE_IDLE; fModel->fState = STATE_IDLE;
// _StartNodeMonitoring();
delete fGrepper; delete fGrepper;
fGrepper = NULL; fGrepper = NULL;
@@ -740,11 +796,11 @@ GrepWindow::_OnSearchFinished()
fPreferencesMenu->SetEnabled(true); fPreferencesMenu->SetEnabled(true);
fHistoryMenu->SetEnabled(true); fHistoryMenu->SetEnabled(true);
fEncodingMenu->SetEnabled(true); fEncodingMenu->SetEnabled(true);
fButton->SetLabel(_T("Search")); fButton->SetLabel(_T("Search"));
fButton->SetEnabled(true); fButton->SetEnabled(true);
fSearch->SetEnabled(true); fSearch->SetEnabled(true);
fSearchText->SetEnabled(true); fSearchText->SetEnabled(true);
fSearchText->MakeFocus(true); fSearchText->MakeFocus(true);
fSearchText->SetText(fOldPattern.String()); fSearchText->SetText(fOldPattern.String());
@@ -753,6 +809,39 @@ GrepWindow::_OnSearchFinished()
} }
void
GrepWindow::_OnNodeMonitorEvent(BMessage* message)
{
int32 opCode;
if (message->FindInt32("opcode", &opCode) != B_OK)
return;
switch (opCode) {
case B_ENTRY_CREATED:
printf("B_ENTRY_CREATED\n");
break;
case B_ENTRY_REMOVED:
printf("B_ENTRY_REMOVED\n");
break;
case B_ENTRY_MOVED:
printf("B_ENTRY_MOVED\n");
break;
case B_STAT_CHANGED:
printf("B_STAT_CHANGED\n");
break;
case B_ATTR_CHANGED:
printf("B_ATTR_CHANGED\n");
break;
default:
printf("unkown opcode\n");
break;
}
message->PrintToStream();
}
void void
GrepWindow::_OnReportFileName(BMessage* message) GrepWindow::_OnReportFileName(BMessage* message)
{ {
+6 -2
View File
@@ -53,9 +53,13 @@ private:
void _LoadPrefs(); void _LoadPrefs();
void _SavePrefs(); void _SavePrefs();
void _StartNodeMonitoring();
void _StopNodeMonitoring();
void _OnStartCancel(); void _OnStartCancel();
void _OnSearchFinished(); void _OnSearchFinished();
void _OnNodeMonitorEvent(BMessage* message);
void _OnReportFileName(BMessage* message); void _OnReportFileName(BMessage* message);
void _OnReportResult(BMessage* message); void _OnReportResult(BMessage* message);
void _OnReportError(BMessage* message); void _OnReportError(BMessage* message);
@@ -129,8 +133,8 @@ private:
Grepper* fGrepper; Grepper* fGrepper;
BString fOldPattern; BString fOldPattern;
Model* fModel; Model* fModel;
bigtime_t fLastNodeMonitorEvent;
BFilePanel* fFilePanel; BFilePanel* fFilePanel;
}; };
+2
View File
@@ -2,6 +2,8 @@ SubDir HAIKU_TOP src apps text_search ;
SetSubDirSupportedPlatformsBeOSCompatible ; SetSubDirSupportedPlatformsBeOSCompatible ;
UsePrivateHeaders storage ;
Application TextSearch : Application TextSearch :
FileIterator.cpp FileIterator.cpp
FolderIterator.cpp FolderIterator.cpp