xref: /haiku/src/kits/tracker/TrackerScripting.cpp (revision 4f2fd49bdc6078128b1391191e4edac647044c3d)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
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:
14 
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.
17 
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.
24 
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.
28 
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.
33 */
34 
35 #include <Message.h>
36 #include <PropertyInfo.h>
37 
38 #include "Tracker.h"
39 #include "FSUtils.h"
40 
41 #define kPropertyTrash "Trash"
42 #define kPropertyFolder "Folder"
43 #define kPropertyPreferences "Preferences"
44 
45 #if 0
46 
47 doo Tracker delete Trash
48 doo Tracker create Folder to '/boot/home/Desktop/hello'
49 
50 ToDo:
51 Create file: on a "Tracker" "File" "B_CREATE_PROPERTY" "name"
52 Create query: on a "Tracker" "Query" "B_CREATE_PROPERTY" "name"
53 Open a folder: Tracker Execute "Folder" bla
54 Find a window for a path
55 
56 #endif
57 
58 
59 #if _SUPPORTS_FEATURE_SCRIPTING
60 
61 const property_info kTrackerPropertyList[] = {
62 	{	kPropertyTrash,
63 		{ B_DELETE_PROPERTY },
64 		{ B_DIRECT_SPECIFIER },
65 		"delete Trash # Empties the Trash",
66 		0,
67 		{},
68 		{},
69 		{}
70 	},
71 	{	kPropertyFolder,
72 		{ B_CREATE_PROPERTY },
73 		{ B_DIRECT_SPECIFIER },
74 		"create Folder to path # creates a new folder",
75 		0,
76 		{ B_REF_TYPE },
77 		{},
78 		{}
79 	},
80 	{	kPropertyPreferences,
81 		{ B_EXECUTE_PROPERTY },
82 		{ B_DIRECT_SPECIFIER },
83 		"shows Tracker preferences",
84 		0,
85 		{},
86 		{},
87 		{}
88 	},
89 	{NULL,
90 		{},
91 		{},
92 		NULL, 0,
93 		{},
94 		{},
95 		{}
96 	}
97 };
98 
99 
100 status_t
101 TTracker::GetSupportedSuites(BMessage *data)
102 {
103 	data->AddString("suites", kTrackerSuites);
104 	BPropertyInfo propertyInfo(const_cast<property_info *>(kTrackerPropertyList));
105 	data->AddFlat("messages", &propertyInfo);
106 
107 	return _inherited::GetSupportedSuites(data);
108 }
109 
110 
111 BHandler *
112 TTracker::ResolveSpecifier(BMessage *message, int32 index,
113 	BMessage *specifier, int32 form, const char *property)
114 {
115 	BPropertyInfo propertyInfo(const_cast<property_info *>(kTrackerPropertyList));
116 
117 	int32 result = propertyInfo.FindMatch(message, index, specifier, form, property);
118 	if (result < 0) {
119 		//PRINT(("FindMatch result %d %s\n", result, strerror(result)));
120 		return _inherited::ResolveSpecifier(message, index, specifier,
121 			form, property);
122 	}
123 
124 	return this;
125 }
126 
127 
128 bool
129 TTracker::HandleScriptingMessage(BMessage *message)
130 {
131 	if (message->what != B_GET_PROPERTY
132 		&& message->what != B_SET_PROPERTY
133 		&& message->what != B_CREATE_PROPERTY
134 		&& message->what != B_COUNT_PROPERTIES
135 		&& message->what != B_DELETE_PROPERTY
136 		&& message->what != B_EXECUTE_PROPERTY)
137 		return false;
138 
139 	// dispatch scripting messages
140 	BMessage reply(B_REPLY);
141 	const char *property = 0;
142 	bool handled = false;
143 
144 	int32 index = 0;
145 	int32 form = 0;
146 	BMessage specifier;
147 
148 	status_t result = message->GetCurrentSpecifier(&index, &specifier,
149 		&form, &property);
150 
151 	if (result != B_OK || index == -1)
152 		return false;
153 
154 	ASSERT(property);
155 
156 	switch (message->what) {
157 		case B_CREATE_PROPERTY:
158 			handled = CreateProperty(message, &specifier, form, property, &reply);
159 			break;
160 
161 		case B_GET_PROPERTY:
162 			handled = GetProperty(&specifier, form, property, &reply);
163 			break;
164 
165 		case B_SET_PROPERTY:
166 			handled = SetProperty(message, &specifier, form, property, &reply);
167 			break;
168 
169 		case B_COUNT_PROPERTIES:
170 			handled = CountProperty(&specifier, form, property, &reply);
171 			break;
172 
173 		case B_DELETE_PROPERTY:
174 			handled = DeleteProperty(&specifier, form, property, &reply);
175 			break;
176 
177 		case B_EXECUTE_PROPERTY:
178 			handled = ExecuteProperty(&specifier, form, property, &reply);
179 			break;
180 	}
181 
182 	if (handled)
183 		// done handling message, send a reply
184 		message->SendReply(&reply);
185 
186 	return handled;
187 }
188 
189 
190 bool
191 TTracker::CreateProperty(BMessage *message, BMessage *, int32 form,
192 	const char *property, BMessage *reply)
193 {
194 	bool handled = false;
195 	status_t error = B_OK;
196 	if (strcmp(property, kPropertyFolder) == 0) {
197 		if (form != B_DIRECT_SPECIFIER)
198 			return false;
199 
200 		// create new empty folders
201 		entry_ref ref;
202 		for (int32 index = 0;
203 			message->FindRef("data", index, &ref) == B_OK; index++) {
204 
205 			BEntry entry(&ref);
206 			if (!entry.Exists())
207 				error = FSCreateNewFolder(&ref);
208 
209 			if (error != B_OK)
210 				break;
211 		}
212 
213 		handled = true;
214 	}
215 
216 	if (error != B_OK)
217 		reply->AddInt32("error", error);
218 
219 	return handled;
220 }
221 
222 
223 bool
224 TTracker::DeleteProperty(BMessage */*specifier*/, int32 form,
225 	const char *property, BMessage */*reply*/)
226 {
227 	if (strcmp(property, kPropertyTrash) == 0) {
228 		// deleting on a selection is handled as removing a part of the selection
229 		// not to be confused with deleting a selected item
230 
231 		if (form != B_DIRECT_SPECIFIER)
232 			// only support direct specifier
233 			return false;
234 
235 		// empty the trash
236 		FSEmptyTrash();
237 		return true;
238 
239 	}
240 	return false;
241 }
242 
243 #else	/* _SUPPORTS_FEATURE_SCRIPTING */
244 
245 status_t
246 TTracker::GetSupportedSuites(BMessage */*data*/)
247 {
248 	return B_UNSUPPORTED;
249 }
250 
251 
252 BHandler *
253 TTracker::ResolveSpecifier(BMessage */*message*/,
254 	int32 /*index*/, BMessage */*specifier*/,
255 	int32 /*form*/, const char */*property*/)
256 {
257 	return NULL;
258 }
259 
260 
261 bool
262 TTracker::HandleScriptingMessage(BMessage */*message*/)
263 {
264 	return false;
265 }
266 
267 
268 bool
269 TTracker::CreateProperty(BMessage */*message*/, BMessage *, int32 /*form*/,
270 	const char */*property*/, BMessage */*reply*/)
271 {
272 	return false;
273 }
274 
275 
276 bool
277 TTracker::DeleteProperty(BMessage */*specifier*/, int32 /*form*/,
278 	const char */*property*/, BMessage *)
279 {
280 	return false;
281 }
282 
283 #endif	/* _SUPPORTS_FEATURE_SCRIPTING */
284 
285 
286 bool
287 TTracker::ExecuteProperty(BMessage *, int32 form, const char *property, BMessage *)
288 {
289 	if (strcmp(property, kPropertyPreferences) == 0) {
290 
291 		if (form != B_DIRECT_SPECIFIER)
292 			// only support direct specifier
293 			return false;
294 
295 		ShowSettingsWindow();
296 		return true;
297 
298 	}
299 	return false;
300 }
301 
302 
303 bool
304 TTracker::CountProperty(BMessage *, int32, const char *, BMessage *)
305 {
306 	return false;
307 }
308 
309 
310 bool
311 TTracker::GetProperty(BMessage *, int32, const char *,	BMessage *)
312 {
313 	return false;
314 }
315 
316 
317 bool
318 TTracker::SetProperty(BMessage *, BMessage *, int32, const char *, BMessage *)
319 {
320 	return false;
321 }
322 
323