xref: /haiku/src/tests/add-ons/index_server/fulltext_search/SearchWindow.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /*
2  * Copyright 2009 - 2010 Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ankur Sethi (get.me.ankur@gmail.com)
7  *		Clemens Zeidler (haiku@clemens-zeidler.de)
8  */
9 
10 #include "SearchWindow.h"
11 
12 #include <Alert.h>
13 #include <Application.h>
14 #include <GroupLayout.h>
15 #include <GroupLayoutBuilder.h>
16 #include <Roster.h>
17 
18 #include "BeaconSearcher.h"
19 
20 
21 const uint32 kSearchMessage = '&sea';
22 const uint32 kLaunchMessage = '&lnc';
23 
24 
25 SearchWindow::SearchWindow(BRect frame)
26 	:
27 	BWindow(frame, "Fulltext Search", B_TITLED_WINDOW,
28 		B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS)
29 {
30 	fSearchButton = new BButton("Search", new BMessage(kSearchMessage));
31 	fSearchField = new BTextControl("", "", new BMessage(kSearchMessage));
32 
33 	fSearchResults = new BListView();
34 	fSearchResults->SetInvocationMessage(new BMessage(kLaunchMessage));
35 
36 	fScrollView = new BScrollView("SearchResults", fSearchResults, 0,
37 		true, true);
38 
39 	SetLayout(new BGroupLayout(B_VERTICAL));
40 
41 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 10)
42 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, 10)
43 			.Add(fSearchField)
44 			.Add(fSearchButton)
45 			.SetInsets(5, 5, 5, 5)
46 		)
47 	.Add(fScrollView)
48 	.SetInsets(5, 5, 5, 5)
49 	);
50 }
51 
52 
53 void
54 SearchWindow::MessageReceived(BMessage *message)
55 {
56 	switch(message->what) {
57 		case kSearchMessage:
58 			_Search();
59 			break;
60 
61 		case kLaunchMessage:
62 			_LaunchFile(message);
63 			break;
64 
65 		default:
66 			BWindow::MessageReceived(message);
67 	}
68 }
69 
70 
71 void
72 SearchWindow::_Search()
73 {
74 	fSearchResults->MakeEmpty();
75 	BeaconSearcher searcher;
76 	searcher.Search(fSearchField->Text());
77 	wchar_t *wPath;
78 	char *path;
79 	while((wPath = searcher.GetNextHit()) != NULL) {
80 		path = new char[wcslen(wPath)*sizeof(wchar_t)];
81 		wcstombs(path, wPath, wcslen(wPath)*sizeof(wchar_t));
82 		fSearchResults->AddItem(new BStringItem(path));
83 	}
84 }
85 
86 
87 void
88 SearchWindow::_LaunchFile(BMessage *message)
89 {
90 	BListView *searchResults;
91 	int32 index;
92 
93 	message->FindPointer("source", (void**)&searchResults);
94 	message->FindInt32("index", &index);
95 	BStringItem *result = (BStringItem*)searchResults->ItemAt(index);
96 
97 	entry_ref ref ;
98 	BEntry entry(result->Text());
99 	entry.GetRef(&ref);
100 	be_roster->Launch(&ref);
101 }
102