* Copyright 2007 - 2009 Chris Roberts. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Chris Roberts, cpr420@gmail.com
*/
#include <StorageKit.h>
#include <SupportKit.h>
#include <AppKit.h>
#include <vector>
#include <add-ons/tracker/TrackerAddOn.h>
const char* kTerminalSignature = "application/x-vnd.Haiku-Terminal";
const directory_which kAppsDirectory = B_SYSTEM_APPS_DIRECTORY;
void
launch_terminal(BEntry* targetEntry, BPath* terminalPath) {
BPath targetPath;
if (targetEntry->GetPath(&targetPath) != B_OK)
return;
BString target(targetPath.Path());
target.ReplaceAll("'", "'\\''");
BString terminal(terminalPath->Path());
terminal.ReplaceAll("'", "'\\''");
BString command("cd '");
command << target << "'; '" << terminal << "' &";
system(command.String());
}
void
process_refs(entry_ref base_ref, BMessage* message, void* reserved)
{
BPath terminalPath;
bool terminalFound = false;
entry_ref terminalRef;
if (be_roster->FindApp(kTerminalSignature, &terminalRef) == B_OK) {
if (terminalPath.SetTo(&terminalRef) == B_OK)
terminalFound = true;
}
if (!terminalFound) {
if (find_directory(kAppsDirectory, &terminalPath) != B_OK)
return;
if (terminalPath.Append("Terminal") != B_OK)
return;
}
BEntry entry;
int32 i;
entry_ref tracker_ref;
std::vector<BEntry> entries;
for (i = 0; message->FindRef("refs", i, &tracker_ref) == B_OK; i++) {
if (entry.SetTo(&tracker_ref, true) != B_OK)
continue;
if (!entry.IsDirectory()) {
if (entry.GetParent(&entry) != B_OK)
continue;
}
bool duplicate = false;
for (uint x = 0; x < entries.size(); x++) {
if (entries[x] == entry) {
duplicate = true;
break;
}
}
if (duplicate)
continue;
entries.push_back(BEntry(entry));
launch_terminal(&entry, &terminalPath);
}
if (i == 0) {
if (entry.SetTo(&base_ref) == B_OK)
launch_terminal(&entry, &terminalPath);
}
}