xref: /haiku/src/add-ons/tracker/opentargetfolder/opentargetfolder.cpp (revision a085e81e62d7a860f809b4fb7c7bf5654c396985)
1 /*
2  * Copyright 2003-2008, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 //! Open Target Folder - opens the folder of the link target in Tracker
7 
8 #include <stdio.h>
9 #include <string.h>
10 
11 #include <Alert.h>
12 #include <Directory.h>
13 #include <Entry.h>
14 #include <Messenger.h>
15 #include <Path.h>
16 #include <SymLink.h>
17 
18 
19 extern "C" void
20 process_refs(entry_ref directoryRef, BMessage *msg, void *)
21 {
22 	BDirectory directory(&directoryRef);
23 	if (directory.InitCheck() != B_OK)
24 		return;
25 
26 	int32 errors = 0;
27 	entry_ref ref;
28 	int32 index;
29 	for (index = 0; msg->FindRef("refs", index, &ref) == B_OK; index ++) {
30 		BSymLink link(&ref);
31 		if (link.InitCheck() != B_OK || !link.IsSymLink()) {
32 			errors++;
33 			continue;
34 		}
35 
36 		BEntry targetEntry;
37 		BPath path;
38 		if (link.MakeLinkedPath(&directory, &path) < B_OK
39 			|| targetEntry.SetTo(path.Path()) != B_OK
40 			|| targetEntry.GetParent(&targetEntry) != B_OK) {
41 			BAlert* alert = new BAlert("Open Target Folder",
42 				"Cannot open target folder. Maybe this link is broken?",
43 				"OK", NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
44 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
45 			alert->Go(NULL);
46 			continue;
47 		}
48 
49 		// create Tracker message...
50 		entry_ref target;
51 		targetEntry.GetRef(&target);
52 
53 		BMessage message(B_REFS_RECEIVED);
54 		message.AddRef("refs", &target);
55 
56 		// ...and send it
57 		BMessenger messenger("application/x-vnd.Be-TRAK");
58 		messenger.SendMessage(&message);
59 
60 		// TODO: select entry via scripting?
61 	}
62 
63 	if (errors) {
64 		BAlert* alert = new BAlert("Open Target Folder",
65 			"This add-on can only be used on symbolic links.\n"
66 			"It opens the folder of the link target in Tracker.",
67 			"OK");
68 		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
69 		alert->Go(NULL);
70 	}
71 }
72 
73 
74 int
75 main(int /*argc*/, char **/*argv*/)
76 {
77 	fprintf(stderr, "This can only be used as a Tracker add-on.\n");
78 	return -1;
79 }
80