xref: /haiku/src/apps/musiccollection/QueryMonitor.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2011, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Clemens Zeidler <haiku@clemens-zeidler.de>
7  */
8 
9 
10 #include "QueryMonitor.h"
11 
12 
13 QueryHandler::QueryHandler(EntryViewInterface* listener)
14 	:
15 	FileMonitor(listener)
16 {
17 
18 }
19 
20 
21 void
22 QueryHandler::MessageReceived(BMessage* message)
23 {
24 	int32 opcode;
25 	if (message->what == B_QUERY_UPDATE
26 		&& message->FindInt32("opcode", &opcode) == B_OK) {
27 		switch (opcode) {
28 			case B_ENTRY_CREATED:
29 			case B_ENTRY_REMOVED:
30 				message->what = B_NODE_MONITOR;
31 				break;
32 		}
33 	}
34 
35 	FileMonitor::MessageReceived(message);
36 }
37 
38 
39 QueryReader::QueryReader(QueryHandler* handler)
40 	:
41 	ReadThread(handler)
42 {
43 
44 }
45 
46 
47 QueryReader::~QueryReader()
48 {
49 	Reset();
50 }
51 
52 
53 bool
54 QueryReader::AddQuery(BQuery* query)
55 {
56 	query->SetTarget(fTarget);
57 	query->Fetch();
58 	return fQueries.AddItem(query);
59 }
60 
61 
62 void
63 QueryReader::Reset()
64 {
65 	Stop();
66 	Wait();
67 
68 	for (int32 i = 0; i < fLiveQueries.CountItems(); i++)
69 		delete fLiveQueries.ItemAt(i);
70 	fLiveQueries.MakeEmpty();
71 
72 	for (int32 i = 0; i < fQueries.CountItems(); i++)
73 		delete fQueries.ItemAt(i);
74 	fQueries.MakeEmpty();
75 }
76 
77 
78 bool
79 QueryReader::ReadNextEntry(entry_ref& entry)
80 {
81 	BQuery* query = fQueries.ItemAt(0);
82 	if (query == NULL)
83 		return false;
84 	if (query->GetNextRef(&entry) != B_OK) {
85 		fQueries.RemoveItemAt(0);
86 		fLiveQueries.AddItem(query);
87 		return ReadNextEntry(entry);
88 	}
89 	return true;
90 }
91