Tracker: Remove "Clear history" and instead add a way to open the Queries directory.

According to humdinger, if automatic deletion of old queries
is working (which it should be), then "Clear history" isn't needed.

Instead, turn the "Recent queries" menu into an item that can open
the queries folder, where users can then delete or otherwise modify
the recent queries directly.
This commit is contained in:
Augustin Cavalier
2024-10-18 14:45:39 -04:00
parent 1ffd5a3665
commit 4f83537025
2 changed files with 28 additions and 103 deletions
+27 -99
View File
@@ -44,7 +44,6 @@ All rights reserved.
#include <string.h> #include <string.h>
#include <strings.h> #include <strings.h>
#include <Alert.h>
#include <Application.h> #include <Application.h>
#include <Box.h> #include <Box.h>
#include <Button.h> #include <Button.h>
@@ -260,9 +259,8 @@ FindWindow::FindWindow(const entry_ref* newRef, bool editIfTemplateOnly)
} }
} else { } else {
// no initial query, fall back on the default query template // no initial query, fall back on the default query template
BEntry entry; BDirectory directory(GetQueriesDirectory().Path());
GetDefaultQuery(entry); BEntry entry(&directory, "default");
entry.GetRef(&fRef);
if (entry.Exists()) if (entry.Exists())
fFile = TryOpening(&fRef); fFile = TryOpening(&fRef);
@@ -317,11 +315,9 @@ FindWindow::BuildMenuBar()
fHistoryMenu = new BMenu(B_TRANSLATE("Recent queries")); fHistoryMenu = new BMenu(B_TRANSLATE("Recent queries"));
BMessenger messenger(fBackground); BMessenger messenger(fBackground);
FindPanel::AddRecentQueries(fHistoryMenu, false, &messenger, kSwitchToQueryTemplate, false); FindPanel::AddRecentQueries(fHistoryMenu, false, &messenger, kSwitchToQueryTemplate, false);
if (fHistoryMenu->CountItems() > 0) {
fHistoryMenu->AddSeparatorItem(); IconMenuItem* historyMenuItem = new IconMenuItem(fHistoryMenu,
fHistoryMenu->AddItem(new BMenuItem(B_TRANSLATE("Clear history"), new BMessage(kOpenDir), B_DIR_MIMETYPE);
new BMessage(kClearHistory)));
}
BMenuItem* saveAsQueryItem = new BMenuItem(B_TRANSLATE("Save as query" B_UTF8_ELLIPSIS), NULL); BMenuItem* saveAsQueryItem = new BMenuItem(B_TRANSLATE("Save as query" B_UTF8_ELLIPSIS), NULL);
BMessage* saveAsQueryMessage = new BMessage(kOpenSaveAsPanel); BMessage* saveAsQueryMessage = new BMessage(kOpenSaveAsPanel);
@@ -340,7 +336,7 @@ FindWindow::BuildMenuBar()
fQueryMenu->AddItem(saveAsQueryItem); fQueryMenu->AddItem(saveAsQueryItem);
fQueryMenu->AddItem(saveAsQueryTemplateItem); fQueryMenu->AddItem(saveAsQueryTemplateItem);
fQueryMenu->AddSeparatorItem(); fQueryMenu->AddSeparatorItem();
fQueryMenu->AddItem(fHistoryMenu); fQueryMenu->AddItem(historyMenuItem);
fSearchInTrash = new BMenuItem( fSearchInTrash = new BMenuItem(
B_TRANSLATE("Include Trash"), new BMessage(kSearchInTrashOptionClicked)); B_TRANSLATE("Include Trash"), new BMessage(kSearchInTrashOptionClicked));
@@ -412,68 +408,7 @@ FindWindow::DeleteQueryOrTemplate(BEntry* entry)
} }
void static bool
FindWindow::ClearHistoryOrTemplates(bool clearTemplates, bool temporaryOnly)
{
BVolumeRoster roster;
BVolume volume;
while (roster.GetNextVolume(&volume) == B_OK) {
if (volume.IsPersistent() && volume.KnowsQuery() && volume.KnowsAttr()) {
BQuery query;
query.SetVolume(&volume);
query.SetPredicate("_trk/recentQuery == 1");
if (query.Fetch() != B_OK)
continue;
BEntry entry;
entry_ref ref;
while (query.GetNextEntry(&entry) == B_OK) {
entry.GetRef(&ref);
if (FSInTrashDir(&ref) && !BEntry(&ref).Exists())
continue;
char type[B_MIME_TYPE_LENGTH];
BNodeInfo(new BNode(&entry)).GetType(type);
if (strcmp(type, B_QUERY_TEMPLATE_MIMETYPE) == 0) {
if (clearTemplates)
DeleteQueryOrTemplate(&entry);
else
continue;
}
if (!clearTemplates) {
BFile file(&entry, B_READ_ONLY);
bool isTemporary;
if (file.ReadAttr("_trk/temporary", B_BOOL_TYPE, 0, &isTemporary,
sizeof(isTemporary))
== sizeof(isTemporary)) {
if (!temporaryOnly) {
DeleteQueryOrTemplate(&entry);
} else {
if (isTemporary)
DeleteQueryOrTemplate(&entry);
}
}
}
}
}
}
if (clearTemplates) {
ClearMenu(fTemplatesMenu);
} else {
ClearMenu(fHistoryMenu);
BMessenger messenger(fBackground);
FindPanel::AddRecentQueries(fHistoryMenu, false, &messenger, kSwitchToQueryTemplate, false,
false, true);
if (fHistoryMenu->CountItems() > 0) {
fHistoryMenu->AddSeparatorItem();
fHistoryMenu->AddItem(new BMenuItem(B_TRANSLATE("Clear history"),
new BMessage(kClearHistory)));
}
}
}
bool
CheckForDuplicates(BObjectList<entry_ref>* list, entry_ref* ref) CheckForDuplicates(BObjectList<entry_ref>* list, entry_ref* ref)
{ {
// Simple Helper Function To Check For Duplicates Within an Entry List of Templates // Simple Helper Function To Check For Duplicates Within an Entry List of Templates
@@ -552,16 +487,16 @@ FindWindow::TryOpening(const entry_ref* ref)
} }
void BPath
FindWindow::GetDefaultQuery(BEntry& entry) FindWindow::GetQueriesDirectory()
{ {
BPath path; BPath path;
if (find_directory(B_USER_DIRECTORY, &path, true) == B_OK if (find_directory(B_USER_DIRECTORY, &path, true) == B_OK
&& path.Append("queries") == B_OK && path.Append("queries") == B_OK
&& (mkdir(path.Path(), 0777) == 0 || errno == EEXIST)) { && (mkdir(path.Path(), 0777) == 0 || errno == EEXIST)) {
BDirectory directory(path.Path()); return path;
entry.SetTo(&directory, "default");
} }
return BPath();
} }
@@ -903,11 +838,9 @@ FindWindow::FindSaveCommon(bool find)
if (newFile) { if (newFile) {
// create query file in the user's directory // create query file in the user's directory
BPath path; BPath path = GetQueriesDirectory();
// there might be no queries folder yet, create one // there might be no queries folder yet, create one
if (find_directory(B_USER_DIRECTORY, &path, true) == B_OK if (path.Path()[0] != '\0') {
&& path.Append("queries") == B_OK
&& (mkdir(path.Path(), 0777) == 0 || errno == EEXIST)) {
// either use the user specified name, or go with the name // either use the user specified name, or go with the name
// generated from the predicate, etc. // generated from the predicate, etc.
BString name; BString name;
@@ -942,6 +875,18 @@ void
FindWindow::MessageReceived(BMessage* message) FindWindow::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case kOpenDir:
{
BMessage message(B_REFS_RECEIVED);
BEntry entry(GetQueriesDirectory().Path());
entry_ref ref;
if (entry.GetRef(&ref) == B_OK) {
message.AddRef("refs", &ref);
be_app->PostMessage(&message);
}
break;
}
case kFindButton: case kFindButton:
Find(); Find();
break; break;
@@ -957,23 +902,6 @@ FindWindow::MessageReceived(BMessage* message)
break; break;
} }
case kClearHistory:
{
// BAlert will manage its memory independently
BAlert* alert = new BAlert(B_TRANSLATE("Clear history?"),
B_TRANSLATE("Do you want to clear temporary queries or all queries?"
" This action is irreversible!"),
B_TRANSLATE("Cancel"),
B_TRANSLATE("Clear all"),
B_TRANSLATE("Clear temporary queries only"), B_WIDTH_AS_USUAL, B_OFFSET_SPACING,
B_WARNING_ALERT);
alert->SetShortcut(0, B_ESCAPE);
int32 choice = alert->Go();
if (choice)
ClearHistoryOrTemplates(false, choice == 2);
break;
}
case kOpenSaveAsPanel: case kOpenSaveAsPanel:
{ {
if (fSaveAsPanel == NULL) if (fSaveAsPanel == NULL)
+1 -4
View File
@@ -74,8 +74,6 @@ const uint32 kByFormulaItem = 'Fbyq';
const uint32 kAddItem = 'Fadd'; const uint32 kAddItem = 'Fadd';
const uint32 kRemoveItem = 'Frem'; const uint32 kRemoveItem = 'Frem';
const uint32 kClearHistory = 'FClH';
const uint32 kClearTemplates = 'FClT';
const uint32 kSaveQueryOrTemplate = 'FSaQ'; const uint32 kSaveQueryOrTemplate = 'FSaQ';
const uint32 kOpenSaveAsPanel = 'Fosv'; const uint32 kOpenSaveAsPanel = 'Fosv';
const uint32 kOpenLoadQueryPanel = 'Folo'; const uint32 kOpenLoadQueryPanel = 'Folo';
@@ -158,8 +156,7 @@ protected:
private: private:
static BFile* TryOpening(const entry_ref* ref); static BFile* TryOpening(const entry_ref* ref);
// when opening an empty panel, use the default query to set the panel up static BPath GetQueriesDirectory();
static void GetDefaultQuery(BEntry& entry);
void SaveQueryAttributes(BNode* file, bool templateQuery); void SaveQueryAttributes(BNode* file, bool templateQuery);
// retrieve the results // retrieve the results