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
+3
View File
@@ -37,6 +37,9 @@ 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);
+91 -2
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>
@@ -220,6 +221,10 @@ void GrepWindow::MessageReceived(BMessage *message)
_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;
@@ -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;
@@ -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)
{ {
+5 -1
View File
@@ -54,8 +54,12 @@ 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