6 Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 BeMail(TM), Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
35 #include "QueryMenu.h"
39 #include <NodeMonitor.h>
40 #include <VolumeRoster.h>
45 #include <PopUpMenu.h>
48 BLooper
*QueryMenu::fQueryLooper
= NULL
;
49 int32
QueryMenu::fMenuCount
= 0;
57 class QHandler
: public BHandler
60 QHandler(QueryMenu
*queryMenu
);
61 virtual void MessageReceived(BMessage
*msg
);
63 QueryMenu
*fQueryMenu
;
67 QHandler::QHandler(QueryMenu
*queryMenu
)
68 : BHandler((const char *)NULL
),
74 void QHandler::MessageReceived(BMessage
*msg
)
79 fQueryMenu
->DoQueryMessage(msg
);
83 BHandler::MessageReceived(msg
);
94 QueryMenu::QueryMenu(const char *title
, bool popUp
, bool radioMode
, bool autoRename
)
95 : BPopUpMenu(title
, radioMode
, autoRename
),
99 if (atomic_add(&fMenuCount
, 1) == 0)
101 fQueryLooper
= new BLooper("Query Watcher");
104 fQueryHandler
= new QHandler(this);
105 fQueryLooper
->Lock();
106 fQueryLooper
->AddHandler(fQueryHandler
);
107 fQueryLooper
->Unlock();
109 // BMessenger mercury(fQueryHandler, fQueryLooper);
110 // fQuery = new BQuery();
111 // fQuery->SetTarget(mercury);
115 QueryMenu::~QueryMenu(void)
120 int32 queries
= fQueries
.size();
121 for (int i
= 0; i
< queries
; i
++) {
122 fQueries
[i
]->Clear();
127 fQueryLooper
->Lock();
128 fQueryLooper
->RemoveHandler(fQueryHandler
);
129 delete fQueryHandler
;
130 if (atomic_add(&fMenuCount
, -1) == 1)
131 fQueryLooper
->Quit();
133 fQueryLooper
->Unlock();
137 void QueryMenu::DoQueryMessage(BMessage
*msg
)
143 if (msg
->FindInt32("opcode", &opcode
) == B_OK
144 && msg
->FindInt64("directory", &directory
) == B_OK
145 && msg
->FindInt32("device", &device
) == B_OK
146 && msg
->FindInt64("node", &node
) == B_OK
)
149 if (opcode
== B_ENTRY_CREATED
&& msg
->FindString("name", &name
) == B_OK
)
151 entry_ref
ref(device
, directory
, name
);
152 EntryCreated(ref
, node
);
155 else if (opcode
== B_ENTRY_REMOVED
)
157 BAutolock
lock(fQueryLock
);
158 if (!lock
.IsLocked())
166 status_t
QueryMenu::SetPredicate(const char *expr
, BVolume
*volume
)
170 if (volume
== NULL
) {
171 BVolumeRoster roster
;
173 BMessenger
mercury(fQueryHandler
, fQueryLooper
);
176 while (roster
.GetNextVolume(&volume
) == B_NO_ERROR
) {
177 if ((volume
.KnowsQuery() == true) && (volume
.KnowsAttr() == true) &&
178 (volume
.KnowsMime() == true)) {
180 BQuery
*query
= new BQuery();
181 if (query
->SetVolume(&volume
) != B_OK
) {
185 if (query
->SetPredicate(expr
) != B_OK
) {
189 if (query
->SetTarget(mercury
) != B_OK
) {
194 fQueries
.push_back(query
);
201 // if (volume == NULL)
203 // BVolume bootVolume;
204 // BVolumeRoster().GetBootVolume(&bootVolume);
206 // if ( (status = fQuery->SetVolume(&bootVolume)) != B_OK)
209 // else if ((status = fQuery->SetVolume(volume)) != B_OK)
212 // if ((status = fQuery->SetPredicate(expr)) < B_OK)
215 // Force query thread to exit if still running
219 // Remove all existing menu items (if any... )
223 // Resolve Query/Build Menu in seperate thread
225 thread
= spawn_thread(query_thread
, "query menu thread", B_NORMAL_PRIORITY
, this);
227 return resume_thread(thread
);
231 void QueryMenu::RemoveEntries()
234 for (int32 i
= CountItems() - 1;i
>= 0;i
--)
236 if (ItemAt(i
)->Message()->FindInt64("node", &node
) == B_OK
)
242 int32
QueryMenu::query_thread(void *data
)
244 return ((QueryMenu
*)(data
))->QueryThread();
248 int32
QueryMenu::QueryThread()
250 BAutolock
lock(fQueryLock
);
252 if (!lock
.IsLocked())
255 // Begin resolving query
256 fCancelQuery
= false;
257 int32 queries
= fQueries
.size();
258 for (int i
= 0; i
< queries
; i
++) {
259 BQuery
*query
= fQueries
[i
];
266 while (query
->GetNextRef(&ref
) == B_OK
&& !fCancelQuery
)
269 entry
.GetNodeRef(&node
);
270 EntryCreated(ref
, node
.node
);
274 // Remove the group separator if there are no groups or no items without groups
276 if (dynamic_cast<BSeparatorItem
*>(item
= ItemAt(0)) != NULL
)
278 else if (dynamic_cast<BSeparatorItem
*>(item
= ItemAt(CountItems() - 1)) != NULL
)
285 status_t
QueryMenu::SetTargetForItems(BHandler
*handler
)
287 fTargetHandler
= handler
;
288 return BMenu::SetTargetForItems(handler
);
291 // Include the following version of SetTargetForItems() to eliminate
292 // hidden polymorphism warning. Should be correct, but is unused and untested.
294 status_t
QueryMenu::SetTargetForItems(BMessenger messenger
)
296 if (messenger
.IsTargetLocal())
298 BLooper
*ignore
; // don't care what value this gets
299 fTargetHandler
= messenger
.Target(&ignore
);
300 return BMenu::SetTargetForItems(messenger
);
306 void QueryMenu::EntryCreated(const entry_ref
&ref
, ino_t node
)
311 msg
= new BMessage(B_REFS_RECEIVED
);
312 msg
->AddRef("refs", &ref
);
313 msg
->AddInt64("node", node
);
314 item
= new BMenuItem(ref
.name
, msg
);
315 if (fTargetHandler
) {
316 item
->SetTarget(fTargetHandler
);
322 void QueryMenu::EntryRemoved(ino_t node
)
324 // Search for item in menu
326 for (int32 i
= 0;(item
= ItemAt(i
)) != NULL
;i
++)
330 if ((item
->Message())->FindInt64("node", &inode
) == B_OK
340 BPoint
QueryMenu::ScreenLocation()
343 return BPopUpMenu::ScreenLocation();
345 return BMenu::ScreenLocation();