1/*
2 * Copyright 2009 Jonas Sundstr��m, jonas@kirilla.com
3 * Copyright 1998-2007 Matthijs Hollemans
4 * All rights reserved. Distributed under the terms of the MIT License.
5 */
6#ifndef _TRACKER_ADDON_APP_LAUNCH_H
7#define _TRACKER_ADDON_APP_LAUNCH_H
8
9
10#include <Entry.h>
11#include <image.h>
12#include <Roster.h>
13#include <TrackerAddOn.h>
14
15
16void
17process_refs(entry_ref directory, BMessage* refs, void* reserved)
18{
19	// When this header file is included by an application it becomes
20	// ready to be used as a Tracker add-on. This function will launch
21	// the application with the entry_refs given by Tracker to the addon.
22
23	// The "directory" entry_ref holds the entry_ref of the folder from where
24	// the addon was launched. If launched from a query it's probably invalid.
25	// The "refs" BMessage is a standard B_REFS_RECEIVED message whose
26	// "refs" array contains the entry_refs of the selected files.
27	// The last argument, "reserved", is currently unused.
28
29	refs->AddRef("dir_ref", &directory);
30		// The folder's entry_ref might be useful to some applications.
31		// We include it by a different name than the other entry_refs.
32		// It could be useful to know if an application got its refs as a
33		// Tracker addon. A B_REFS_RECEIVED message with a "dir_ref"
34		// provides such a hint.
35
36	// get the path of the Tracker add-on
37	image_info image;
38	int32 cookie = 0;
39	status_t status = get_next_image_info(0, &cookie, &image);
40
41	while (status == B_OK) {
42		if (((char*)process_refs >= (char*)image.text
43			&& (char*)process_refs <= (char*)image.text + image.text_size)
44			|| ((char*)process_refs >= (char*)image.data
45			&& (char*)process_refs <= (char*)image.data + image.data_size))
46			break;
47
48		status = get_next_image_info(0, &cookie, &image);
49	}
50
51	entry_ref addonRef;
52	if (get_ref_for_path(image.name, &addonRef) != B_OK)
53		return;
54
55	status = be_roster->Launch(&addonRef, refs);
56	if (status == B_OK || status == B_ALREADY_RUNNING)
57		return;
58
59	// If launching by entry_ref fails it's probably futile to
60	// launch by signature this way, but we can try anyway.
61
62	app_info appInfo;
63	if (be_roster->GetAppInfo(&addonRef, &appInfo) != B_OK)
64		return;
65
66	be_roster->Launch(appInfo.signature, refs);
67}
68
69
70#endif	// _TRACKER_ADDON_APP_LAUNCH_H
71
72