xref: /haiku/src/add-ons/tracker/openterminal/OpenTerminal.cpp (revision 3126032d475fe2d3e9f0234b723ec2eae75e6335)
1 /*
2  * Copyright 2007 - 2009 Chris Roberts. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Chris Roberts, cpr420@gmail.com
7  */
8 
9 #include <StorageKit.h>
10 #include <SupportKit.h>
11 #include <AppKit.h>
12 
13 #include <vector>
14 
15 #include <add-ons/tracker/TrackerAddOn.h>
16 
17 const char* kTerminalSignature = "application/x-vnd.Haiku-Terminal";
18 
19 static void
launch_terminal(BEntry & targetEntry)20 launch_terminal(BEntry& targetEntry) {
21 	BPath targetPath;
22 
23 	if (targetEntry.GetPath(&targetPath) != B_OK)
24 		return;
25 
26 	// Launch the Terminal.
27 	const char* argv[] = {"-w", targetPath.Path(), NULL};
28 	be_roster->Launch(kTerminalSignature, 2, argv);
29 }
30 
31 
32 void
process_refs(entry_ref base_ref,BMessage * message,void * reserved)33 process_refs(entry_ref base_ref, BMessage* message, void* reserved)
34 {
35 	BEntry entry;
36 	int32 i;
37 	entry_ref tracker_ref;
38 	std::vector<BEntry> entries;
39 
40 	// Iterate through the refs that Tracker has sent us.
41 	for (i = 0; message->FindRef("refs", i, &tracker_ref) == B_OK; i++) {
42 
43 		// Pass 'true' as the traverse argument below, so that if the ref
44 		// is a symbolic link we get the target of the link to ensure that
45 		// it actually exists.
46 		if (entry.SetTo(&tracker_ref, true) != B_OK)
47 			continue;
48 
49 		// If the entry is a file then look for the parent directory.
50 		if (!entry.IsDirectory()) {
51 			if (entry.GetParent(&entry) != B_OK)
52 				continue;
53 		}
54 
55 		bool duplicate = false;
56 
57 		// Check for duplicates.
58 		for (uint x = 0; x < entries.size(); x++) {
59 			if (entries[x] == entry) {
60 				duplicate = true;
61 				break;
62 			}
63 		}
64 
65 		// This is a duplicate.  Continue to next ref.
66 		if (duplicate)
67 			continue;
68 
69 		// Push entry onto the vector so we can check for duplicates later.
70 		entries.push_back(BEntry(entry));
71 
72 		launch_terminal(entry);
73 	}
74 
75 	// If nothing was selected we'll use the base folder.
76 	if (i == 0) {
77 		if (entry.SetTo(&base_ref) == B_OK)
78 			launch_terminal(entry);
79 	}
80 }
81