xref: /haiku/src/apps/text_search/TextSearch.cpp (revision 58481f0f6ef1a61ba07283f012cafbc2ed874ead)
1 /*
2  * Copyright (c) 1998-2007 Matthijs Hollemans
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 // NOTE: This code is the same in DiskUsge. Maybe it could be made
24 // into some kind of common base class?
25 
26 #include "GrepApp.h"
27 
28 #include <AppFileInfo.h>
29 #include <Entry.h>
30 #include <Roster.h>
31 #include <TrackerAddOn.h>
32 
33 #include "GlobalDefs.h"
34 
35 
36 int
37 main()
38 {
39 	GrepApp app;
40 	app.Run();
41 	return 0;
42 }
43 
44 
45 void
46 process_refs(entry_ref dirRef, BMessage *message, void* /*reserved*/)
47 {
48 	// Tracker calls this function when the user invokes the add-on.
49 	// "dir_ref" contains the entry_ref of the current directory.
50 	// "message" is a standard B_REFS_RECEIVED BMessage whose "refs"
51 	// array contains the entry_refs of the selected files. The last
52 	// argument, "reserved", is currently unused.
53 
54 	// This version of TextSearch is a Tracker add-on, but primarily
55 	// it is a stand-alone application. The add-on launches the app
56 	// on the set of files you had selected in Tracker. That way you
57 	// get the benefits of the Tracker add-on with the benefits of the
58 	// stand-alone application.
59 
60 	message->AddRef("dir_ref", &dirRef);
61 
62 	// get the path of the Tracker add-on
63 	image_info image;
64 	int32 cookie = 0;
65 	status_t status = get_next_image_info(0, &cookie, &image);
66 
67 	while (status == B_OK) {
68 		if (((char*)process_refs >= (char*)image.text
69 				&& (char*)process_refs <= (char*)image.text + image.text_size)
70 			|| ((char*)process_refs >= (char*)image.data
71 				&& (char*)process_refs <= (char*)image.data + image.data_size))
72 			break;
73 
74 		status = get_next_image_info(0, &cookie, &image);
75 	}
76 
77 	entry_ref addonRef;
78 
79 	if (get_ref_for_path(image.name, &addonRef) == B_OK) {
80 		// It's better to launch the application by its entry
81 		// than by its application signature. There may be
82 		// multiple instances and we would not be certain
83 		// that the desired one is launched - the one which was
84 		// loaded into Tracker and whose process_refs() was called.
85 		be_roster->Launch(&addonRef, message);
86 	} else
87 		be_roster->Launch(APP_SIGNATURE, message);
88 }
89