From 1f17f750dbac143855f0e7883092447c1755c187 Mon Sep 17 00:00:00 2001 From: John Scipione Date: Sat, 14 Jun 2014 20:48:15 -0400 Subject: [PATCH] Tracker: Use BPathFinder to find add-ons --- src/kits/tracker/DeskWindow.cpp | 139 ++++++++++++++------------------ 1 file changed, 61 insertions(+), 78 deletions(-) diff --git a/src/kits/tracker/DeskWindow.cpp b/src/kits/tracker/DeskWindow.cpp index ca1acdc5f7..ca46dc657a 100644 --- a/src/kits/tracker/DeskWindow.cpp +++ b/src/kits/tracker/DeskWindow.cpp @@ -38,11 +38,14 @@ All rights reserved. #include #include #include +#include #include #include #include #include #include +#include +#include #include #include @@ -126,62 +129,52 @@ FindElement(struct AddonShortcut* item, void* castToOther) static void -LoadAddOnDir(directory_which dirName, BDeskWindow* window, +LoadAddOnDir(BDirectory directory, BDeskWindow* window, LockingList* list) { - BPath path; - if (find_directory(dirName, &path) == B_OK) { - path.Append("Tracker"); - - BDirectory dir; - BEntry entry; - - if (dir.SetTo(path.Path()) != B_OK) - return; - - while (dir.GetNextEntry(&entry) == B_OK) { - Model* model = new Model(&entry); - if (model->InitCheck() == B_OK && model->IsSymLink()) { - // resolve symlinks - Model* resolved = new Model(model->EntryRef(), true, true); - if (resolved->InitCheck() == B_OK) - model->SetLinkTo(resolved); - else - delete resolved; - } - if (model->InitCheck() != B_OK - || !model->ResolveIfLink()->IsExecutable()) { - delete model; - continue; - } - - char* name = strdup(model->Name()); - if (!list->EachElement(MatchOne, name)) { - struct AddonShortcut* item = new struct AddonShortcut; - item->model = model; - - BResources resources(model->ResolveIfLink()->EntryRef()); - size_t size; - char* shortcut = (char*)resources.LoadResource(B_STRING_TYPE, - kDefaultShortcut, &size); - if (shortcut == NULL || strlen(shortcut) > 1) - item->key = '\0'; - else - item->key = shortcut[0]; - AddOneShortcut(model, item->key, kDefaultModifiers, window); - item->defaultKey = item->key; - item->modifiers = kDefaultModifiers; - list->AddItem(item); - } - free(name); + BEntry entry; + while (directory.GetNextEntry(&entry) == B_OK) { + Model* model = new Model(&entry); + if (model->InitCheck() == B_OK && model->IsSymLink()) { + // resolve symlinks + Model* resolved = new Model(model->EntryRef(), true, true); + if (resolved->InitCheck() == B_OK) + model->SetLinkTo(resolved); + else + delete resolved; + } + if (model->InitCheck() != B_OK + || !model->ResolveIfLink()->IsExecutable()) { + delete model; + continue; } - BNode node(path.Path()); - node_ref nodeRef; - node.GetNodeRef(&nodeRef); - - TTracker::WatchNode(&nodeRef, B_WATCH_DIRECTORY, window); + char* name = strdup(model->Name()); + if (!list->EachElement(MatchOne, name)) { + struct AddonShortcut* item = new struct AddonShortcut; + item->model = model; + + BResources resources(model->ResolveIfLink()->EntryRef()); + size_t size; + char* shortcut = (char*)resources.LoadResource(B_STRING_TYPE, + kDefaultShortcut, &size); + if (shortcut == NULL || strlen(shortcut) > 1) + item->key = '\0'; + else + item->key = shortcut[0]; + AddOneShortcut(model, item->key, kDefaultModifiers, window); + item->defaultKey = item->key; + item->modifiers = kDefaultModifiers; + list->AddItem(item); + } + free(name); } + + BNode node(&directory, NULL); + node_ref nodeRef; + node.GetNodeRef(&nodeRef); + + TTracker::WatchNode(&nodeRef, B_WATCH_DIRECTORY, window); } @@ -279,10 +272,13 @@ BDeskWindow::InitAddonsList(bool update) fAddonsList->MakeEmpty(true); } - LoadAddOnDir(B_USER_NONPACKAGED_ADDONS_DIRECTORY, this, fAddonsList); - LoadAddOnDir(B_USER_ADDONS_DIRECTORY, this, fAddonsList); - LoadAddOnDir(B_SYSTEM_NONPACKAGED_ADDONS_DIRECTORY, this, fAddonsList); - LoadAddOnDir(B_SYSTEM_ADDONS_DIRECTORY, this, fAddonsList); + BStringList addOnPaths; + BPathFinder::FindPaths(B_FIND_PATH_ADD_ONS_DIRECTORY, "Tracker/", + addOnPaths); + for (int32 i = 0; i < addOnPaths.CountStrings(); i++) { + LoadAddOnDir(BDirectory(addOnPaths.StringAt(i)), this, + fAddonsList); + } } } @@ -324,31 +320,18 @@ BDeskWindow::ApplyShortcutPreferences(bool update) if (message.FindString("command", &command) != B_OK) continue; - BPath path; bool isInAddons = false; - if (find_directory(B_SYSTEM_ADDONS_DIRECTORY, &path) - == B_OK) { - path.Append("Tracker/"); - isInAddons = command.FindFirst(path.Path()) == 0; - } - if (!isInAddons - && (find_directory(B_SYSTEM_NONPACKAGED_ADDONS_DIRECTORY, - &path) == B_OK)) { - path.Append("Tracker/"); - isInAddons = command.FindFirst(path.Path()) == 0; - } - if (!isInAddons - && (find_directory(B_USER_ADDONS_DIRECTORY, &path) - == B_OK)) { - path.Append("Tracker/"); - isInAddons = command.FindFirst(path.Path()) == 0; - } - if (!isInAddons - && (find_directory(B_USER_NONPACKAGED_ADDONS_DIRECTORY, - &path) == B_OK)) { - path.Append("Tracker/"); - isInAddons = command.FindFirst(path.Path()) == 0; + + BStringList addOnPaths; + BPathFinder::FindPaths(B_FIND_PATH_ADD_ONS_DIRECTORY, + "Tracker/", addOnPaths); + for (int32 i = 0; i < addOnPaths.CountStrings(); i++) { + if (command.FindFirst(addOnPaths.StringAt(i)) == 0) { + isInAddons = true; + break; + } } + if (!isInAddons) continue;