btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / tracker / TrackerScripting.cpp
blob1cd6cee5e8d47406b97d6be02c2c72dc876f1b55
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, 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 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.
32 All rights reserved.
36 #include <Message.h>
37 #include <PropertyInfo.h>
39 #include "ContainerWindow.h"
40 #include "FSUtils.h"
41 #include "Tracker.h"
44 #define kPropertyTrash "Trash"
45 #define kPropertyFolder "Folder"
46 #define kPropertyPreferences "Preferences"
50 Available scripting commands:
52 doo Tracker delete Trash
53 doo Tracker create Folder to '/boot/home/Desktop/hello' # mkdir
54 doo Tracker get Folder to '/boot/home/Desktop/hello' # get window for path
55 doo Tracker execute Folder to '/boot/home/Desktop/hello' # open window
57 ToDo:
58 Create file: on a "Tracker" "File" "B_CREATE_PROPERTY" "name"
59 Create query: on a "Tracker" "Query" "B_CREATE_PROPERTY" "name"
63 const property_info kTrackerPropertyList[] = {
65 kPropertyTrash,
66 { B_DELETE_PROPERTY },
67 { B_DIRECT_SPECIFIER },
68 "delete Trash # Empties the Trash",
70 {},
71 {},
75 kPropertyFolder,
76 { B_CREATE_PROPERTY, B_GET_PROPERTY, B_EXECUTE_PROPERTY },
77 { B_DIRECT_SPECIFIER },
78 "create Folder to path # creates a new folder\n"
79 "get Folder to path # get Tracker window pointing to folder\n"
80 "execute Folder to path # opens Tracker window pointing to folder\n",
82 { B_REF_TYPE },
83 {},
87 kPropertyPreferences,
88 { B_EXECUTE_PROPERTY },
89 { B_DIRECT_SPECIFIER },
90 "shows Tracker preferences",
92 {},
93 {},
97 { 0 }
101 status_t
102 TTracker::GetSupportedSuites(BMessage* data)
104 data->AddString("suites", kTrackerSuites);
105 BPropertyInfo propertyInfo(const_cast<property_info*>(
106 kTrackerPropertyList));
107 data->AddFlat("messages", &propertyInfo);
109 return _inherited::GetSupportedSuites(data);
113 BHandler*
114 TTracker::ResolveSpecifier(BMessage* message, int32 index, BMessage* specifier,
115 int32 form, const char* property)
117 BPropertyInfo propertyInfo(const_cast<property_info*>(
118 kTrackerPropertyList));
120 int32 result = propertyInfo.FindMatch(message, index, specifier, form,
121 property);
122 if (result < 0) {
123 //PRINT(("FindMatch result %d %s\n", result, strerror(result)));
124 return _inherited::ResolveSpecifier(message, index, specifier,
125 form, property);
128 return this;
132 bool
133 TTracker::HandleScriptingMessage(BMessage* message)
135 if (message->what != B_GET_PROPERTY
136 && message->what != B_SET_PROPERTY
137 && message->what != B_CREATE_PROPERTY
138 && message->what != B_COUNT_PROPERTIES
139 && message->what != B_DELETE_PROPERTY
140 && message->what != B_EXECUTE_PROPERTY) {
141 return false;
144 // dispatch scripting messages
145 BMessage reply(B_REPLY);
146 const char* property = NULL;
147 bool handled = false;
149 int32 index = 0;
150 int32 form = 0;
151 BMessage specifier;
153 status_t result = message->GetCurrentSpecifier(&index, &specifier,
154 &form, &property);
155 if (result != B_OK || index == -1)
156 return false;
158 ASSERT(property != NULL);
160 switch (message->what) {
161 case B_CREATE_PROPERTY:
162 handled = CreateProperty(message, &specifier, form, property,
163 &reply);
164 break;
166 case B_GET_PROPERTY:
167 handled = GetProperty(message, form, property, &reply);
168 break;
170 case B_SET_PROPERTY:
171 handled = SetProperty(message, &specifier, form, property,
172 &reply);
173 break;
175 case B_COUNT_PROPERTIES:
176 handled = CountProperty(&specifier, form, property, &reply);
177 break;
179 case B_DELETE_PROPERTY:
180 handled = DeleteProperty(&specifier, form, property, &reply);
181 break;
183 case B_EXECUTE_PROPERTY:
184 handled = ExecuteProperty(message, form, property, &reply);
185 break;
188 if (handled) {
189 // done handling message, send a reply
190 message->SendReply(&reply);
193 return handled;
197 bool
198 TTracker::CreateProperty(BMessage* message, BMessage*, int32 form,
199 const char* property, BMessage* reply)
201 bool handled = false;
202 status_t error = B_OK;
204 if (strcmp(property, kPropertyFolder) == 0) {
205 if (form != B_DIRECT_SPECIFIER)
206 return false;
208 // create new empty folders
209 entry_ref ref;
210 for (int32 index = 0;
211 message->FindRef("data", index, &ref) == B_OK; index++) {
213 BEntry entry(&ref);
214 if (!entry.Exists())
215 error = FSCreateNewFolder(&ref);
217 if (error != B_OK)
218 break;
221 handled = true;
224 if (error != B_OK)
225 reply->AddInt32("error", error);
227 return handled;
231 bool
232 TTracker::DeleteProperty(BMessage*, int32 form, const char* property, BMessage*)
234 if (strcmp(property, kPropertyTrash) == 0) {
235 // deleting on a selection is handled as removing a part of the
236 // selection not to be confused with deleting a selected item
238 if (form != B_DIRECT_SPECIFIER) {
239 // only support direct specifier
240 return false;
243 // empty the trash
244 FSEmptyTrash();
246 return true;
250 return false;
254 bool
255 TTracker::ExecuteProperty(BMessage* message, int32 form, const char* property,
256 BMessage* reply)
258 if (strcmp(property, kPropertyPreferences) == 0) {
259 if (form != B_DIRECT_SPECIFIER) {
260 // only support direct specifier
261 return false;
263 ShowSettingsWindow();
265 return true;
268 if (strcmp(property, kPropertyFolder) == 0) {
269 message->PrintToStream();
270 if (form != B_DIRECT_SPECIFIER)
271 return false;
273 // create new empty folders
274 entry_ref ref;
275 for (int32 index = 0;
276 message->FindRef("data", index, &ref) == B_OK; index++) {
277 status_t error = OpenRef(&ref, NULL, NULL, kOpen, NULL);
279 if (error == B_OK) {
280 reply->AddMessenger("window",
281 BMessenger(FindContainerWindow(&ref)));
282 } else {
283 reply->AddInt32("error", error);
284 break;
288 return true;
291 return false;
295 bool
296 TTracker::CountProperty(BMessage*, int32, const char*, BMessage*)
298 return false;
302 bool
303 TTracker::GetProperty(BMessage* message, int32 form, const char* property,
304 BMessage* reply)
306 if (strcmp(property, kPropertyFolder) == 0) {
307 message->PrintToStream();
308 if (form != B_DIRECT_SPECIFIER)
309 return false;
311 // create new empty folders
312 entry_ref ref;
313 for (int32 index = 0;
314 message->FindRef("data", index, &ref) == B_OK; index++) {
315 BHandler* window = FindContainerWindow(&ref);
317 if (window != NULL)
318 reply->AddMessenger("window", BMessenger(window));
319 else {
320 reply->AddInt32("error", B_NAME_NOT_FOUND);
321 break;
325 return true;
328 return false;
332 bool
333 TTracker::SetProperty(BMessage*, BMessage*, int32, const char*, BMessage*)
335 return false;