* Refactoring, added const specifiers to helper methods that don't modify the

object.
* Improved check that enforces search pattern history limit to also handle
  the case when the limit is changed in the source.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26787 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2008-08-04 12:53:55 +00:00
parent 684507777b
commit 3896078543
2 changed files with 39 additions and 51 deletions
+34 -46
View File
@@ -60,8 +60,7 @@ Model::Model()
fEncoding(0) fEncoding(0)
{ {
BPath path; BPath path;
status_t status = find_directory(B_USER_DIRECTORY, &path); if (find_directory(B_USER_DIRECTORY, &path) == B_OK)
if (status == B_OK)
fFilePanelPath = path.Path(); fFilePanelPath = path.Path();
else else
fFilePanelPath = "/boot/home"; fFilePanelPath = "/boot/home";
@@ -72,9 +71,7 @@ status_t
Model::LoadPrefs() Model::LoadPrefs()
{ {
BFile file; BFile file;
status_t status = _OpenFile(&file, PREFS_FILE, B_READ_ONLY, status_t status = _OpenFile(&file, PREFS_FILE);
B_USER_SETTINGS_DIRECTORY, NULL);
if (status != B_OK) if (status != B_OK)
return status; return status;
@@ -139,9 +136,8 @@ status_t
Model::SavePrefs() Model::SavePrefs()
{ {
BFile file; BFile file;
status_t status = _OpenFile(&file, PREFS_FILE, status_t status = _OpenFile(&file, PREFS_FILE,
B_CREATE_FILE | B_WRITE_ONLY, B_USER_SETTINGS_DIRECTORY, NULL); B_CREATE_FILE | B_WRITE_ONLY);
if (status != B_OK) if (status != B_OK)
return status; return status;
@@ -193,30 +189,31 @@ Model::SavePrefs()
void void
Model::AddToHistory(const char* text) Model::AddToHistory(const char* text)
{ {
BList* items = _LoadHistory(); BList items;
if (items == NULL) if (!_LoadHistory(items))
return; return;
BString* string = new (nothrow) BString(text); BString* string = new (nothrow) BString(text);
if (string == NULL || !items->AddItem(string)) { if (string == NULL || !items.AddItem(string)) {
delete string; delete string;
_FreeHistory(items);
return; return;
} }
int32 count = items->CountItems() - 1; int32 count = items.CountItems() - 1;
// don't check last item, since that's the one we just added // don't check last item, since that's the one we just added
for (int32 t = 0; t < count; ++t) { for (int32 t = 0; t < count; ++t) {
// If the same text is already in the list, // If the same text is already in the list,
// then remove it first. Case-sensitive. // then remove it first. Case-sensitive.
BString* string = static_cast<BString*>(items->ItemAt(t)); BString* string = static_cast<BString*>(items.ItemAt(t));
if (*string == text) { if (*string == text) {
delete static_cast<BString*>(items->RemoveItem(t)); delete static_cast<BString*>(items.RemoveItem(t));
break; break;
} }
} }
if (items->CountItems() == HISTORY_LIMIT) while (items.CountItems() >= HISTORY_LIMIT)
delete static_cast<BString*>(items->RemoveItem(0L)); delete static_cast<BString*>(items.RemoveItem(0L));
_SaveHistory(items); _SaveHistory(items);
_FreeHistory(items); _FreeHistory(items);
@@ -224,14 +221,14 @@ Model::AddToHistory(const char* text)
void void
Model::FillHistoryMenu(BMenu* menu) Model::FillHistoryMenu(BMenu* menu) const
{ {
BList* items = _LoadHistory(); BList items;
if (items == NULL) if (!_LoadHistory(items))
return; return;
for (int32 t = items->CountItems() - 1; t >= 0; --t) { for (int32 t = items.CountItems() - 1; t >= 0; --t) {
BString* item = static_cast<BString*>(items->ItemAt(t)); BString* item = static_cast<BString*>(items.ItemAtFast(t));
BMessage* message = new BMessage(MSG_SELECT_HISTORY); BMessage* message = new BMessage(MSG_SELECT_HISTORY);
message->AddString("text", item->String()); message->AddString("text", item->String());
menu->AddItem(new BMenuItem(item->String(), message)); menu->AddItem(new BMenuItem(item->String(), message));
@@ -244,46 +241,40 @@ Model::FillHistoryMenu(BMenu* menu)
// #pragma mark - private // #pragma mark - private
BList* bool
Model::_LoadHistory() Model::_LoadHistory(BList& items) const
{ {
BList* items = new (nothrow) BList();
if (items == NULL)
return NULL;
BFile file; BFile file;
status_t status = _OpenFile(&file, PREFS_FILE, B_READ_ONLY, status_t status = _OpenFile(&file, PREFS_FILE);
B_USER_SETTINGS_DIRECTORY, NULL);
if (status != B_OK) if (status != B_OK)
return items; return false;
status = file.Lock(); status = file.Lock();
if (status != B_OK) if (status != B_OK)
return items; return false;
BMessage message; BMessage message;
status = message.Unflatten(&file); status = message.Unflatten(&file);
if (status != B_OK) if (status != B_OK)
return items; return false;
file.Unlock(); file.Unlock();
BString string; BString string;
for (int32 x = 0; message.FindString("string", x, &string) == B_OK; x++) { for (int32 x = 0; message.FindString("string", x, &string) == B_OK; x++) {
BString* copy = new (nothrow) BString(string); BString* copy = new (nothrow) BString(string);
if (copy == NULL || !items->AddItem(copy)) { if (copy == NULL || !items.AddItem(copy)) {
delete copy; delete copy;
break; break;
} }
} }
return items; return true;
} }
status_t status_t
Model::_SaveHistory(BList* items) Model::_SaveHistory(const BList& items) const
{ {
BFile file; BFile file;
status_t status = _OpenFile(&file, PREFS_FILE, status_t status = _OpenFile(&file, PREFS_FILE,
@@ -298,10 +289,9 @@ Model::_SaveHistory(BList* items)
return status; return status;
BMessage message; BMessage message;
for (int32 x = 0; ; x++) { int32 count = items.CountItems();
BString* string = static_cast<BString*>(items->ItemAt(x)); for (int32 i = 0; i < count; i++) {
if (string == NULL) BString* string = static_cast<BString*>(items.ItemAtFast(i));
break;
if (message.AddString("string", string->String()) != B_OK) if (message.AddString("string", string->String()) != B_OK)
break; break;
@@ -317,18 +307,16 @@ Model::_SaveHistory(BList* items)
void void
Model::_FreeHistory(BList* items) Model::_FreeHistory(const BList& items) const
{ {
for (int32 t = items->CountItems() - 1; t >= 0; --t) for (int32 t = items.CountItems() - 1; t >= 0; --t)
delete static_cast<BString*>((items->RemoveItem(t))); delete static_cast<BString*>((items.ItemAtFast(t)));
delete items;
} }
status_t status_t
Model::_OpenFile(BFile* file, const char* name, uint32 openMode, Model::_OpenFile(BFile* file, const char* name, uint32 openMode,
directory_which which, BVolume* volume) directory_which which, BVolume* volume) const
{ {
if (file == NULL) if (file == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
+5 -5
View File
@@ -81,7 +81,7 @@ public:
status_t SavePrefs(); status_t SavePrefs();
void AddToHistory(const char* text); void AddToHistory(const char* text);
void FillHistoryMenu(BMenu* menu); void FillHistoryMenu(BMenu* menu) const;
public: public:
// The directory we were invoked from. // The directory we were invoked from.
@@ -127,14 +127,14 @@ public:
uint32 fEncoding; uint32 fEncoding;
private: private:
BList* _LoadHistory(); bool _LoadHistory(BList& items) const;
status_t _SaveHistory(BList* items); status_t _SaveHistory(const BList& items) const;
void _FreeHistory(BList* items); void _FreeHistory(const BList& items) const;
status_t _OpenFile(BFile* file, const char* name, status_t _OpenFile(BFile* file, const char* name,
uint32 openMode = B_READ_ONLY, uint32 openMode = B_READ_ONLY,
directory_which which directory_which which
= B_USER_SETTINGS_DIRECTORY, = B_USER_SETTINGS_DIRECTORY,
BVolume* volume = NULL); BVolume* volume = NULL) const;
}; };
#endif // MODEL_H #endif // MODEL_H