xref: /haiku/src/kits/tracker/TrackerScripting.cpp (revision 1026b0a1a76dc88927bb8175c470f638dc5464ee)
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*>
105 		(kTrackerPropertyList));
106 	data->AddFlat("messages", &propertyInfo);
107 
108 	return _inherited::GetSupportedSuites(data);
109 }
110 
111 
112 BHandler*
113 TTracker::ResolveSpecifier(BMessage* message, int32 index,
114 	BMessage* specifier, int32 form, const char* property)
115 {
116 	BPropertyInfo propertyInfo(const_cast<property_info*>
117 		(kTrackerPropertyList));
118 
119 	int32 result = propertyInfo.FindMatch(message, index, specifier, form,
120 		property);
121 	if (result < 0) {
122 		//PRINT(("FindMatch result %d %s\n", result, strerror(result)));
123 		return _inherited::ResolveSpecifier(message, index, specifier,
124 			form, property);
125 	}
126 
127 	return this;
128 }
129 
130 
131 bool
132 TTracker::HandleScriptingMessage(BMessage* message)
133 {
134 	if (message->what != B_GET_PROPERTY
135 		&& message->what != B_SET_PROPERTY
136 		&& message->what != B_CREATE_PROPERTY
137 		&& message->what != B_COUNT_PROPERTIES
138 		&& message->what != B_DELETE_PROPERTY
139 		&& message->what != B_EXECUTE_PROPERTY)
140 		return false;
141 
142 	// dispatch scripting messages
143 	BMessage reply(B_REPLY);
144 	const char* property = 0;
145 	bool handled = false;
146 
147 	int32 index = 0;
148 	int32 form = 0;
149 	BMessage specifier;
150 
151 	status_t result = message->GetCurrentSpecifier(&index, &specifier,
152 		&form, &property);
153 
154 	if (result != B_OK || index == -1)
155 		return false;
156 
157 	ASSERT(property);
158 
159 	switch (message->what) {
160 		case B_CREATE_PROPERTY:
161 			handled = CreateProperty(message, &specifier, form, property,
162 				&reply);
163 			break;
164 
165 		case B_GET_PROPERTY:
166 			handled = GetProperty(&specifier, form, property, &reply);
167 			break;
168 
169 		case B_SET_PROPERTY:
170 			handled = SetProperty(message, &specifier, form, property,
171 				&reply);
172 			break;
173 
174 		case B_COUNT_PROPERTIES:
175 			handled = CountProperty(&specifier, form, property, &reply);
176 			break;
177 
178 		case B_DELETE_PROPERTY:
179 			handled = DeleteProperty(&specifier, form, property, &reply);
180 			break;
181 
182 		case B_EXECUTE_PROPERTY:
183 			handled = ExecuteProperty(&specifier, form, property, &reply);
184 			break;
185 	}
186 
187 	if (handled) {
188 		// done handling message, send a reply
189 		message->SendReply(&reply);
190 	}
191 
192 	return handled;
193 }
194 
195 
196 bool
197 TTracker::CreateProperty(BMessage* message, BMessage*, int32 form,
198 	const char* property, BMessage* reply)
199 {
200 	bool handled = false;
201 	status_t error = B_OK;
202 	if (strcmp(property, kPropertyFolder) == 0) {
203 		if (form != B_DIRECT_SPECIFIER)
204 			return false;
205 
206 		// create new empty folders
207 		entry_ref ref;
208 		for (int32 index = 0;
209 			message->FindRef("data", index, &ref) == B_OK; index++) {
210 
211 			BEntry entry(&ref);
212 			if (!entry.Exists())
213 				error = FSCreateNewFolder(&ref);
214 
215 			if (error != B_OK)
216 				break;
217 		}
218 
219 		handled = true;
220 	}
221 
222 	if (error != B_OK)
223 		reply->AddInt32("error", error);
224 
225 	return handled;
226 }
227 
228 
229 bool
230 TTracker::DeleteProperty(BMessage* /*specifier*/, int32 form,
231 	const char* property, BMessage* /*reply*/)
232 {
233 	if (strcmp(property, kPropertyTrash) == 0) {
234 		// deleting on a selection is handled as removing a part of the
235 		// selection not to be confused with deleting a selected item
236 
237 		if (form != B_DIRECT_SPECIFIER)
238 			// only support direct specifier
239 			return false;
240 
241 		// empty the trash
242 		FSEmptyTrash();
243 		return true;
244 
245 	}
246 	return false;
247 }
248 
249 #else	// _SUPPORTS_FEATURE_SCRIPTING
250 
251 status_t
252 TTracker::GetSupportedSuites(BMessage* /*data*/)
253 {
254 	return B_UNSUPPORTED;
255 }
256 
257 
258 BHandler*
259 TTracker::ResolveSpecifier(BMessage* /*message*/,
260 	int32 /*index*/, BMessage* /*specifier*/,
261 	int32 /*form*/, const char* /*property*/)
262 {
263 	return NULL;
264 }
265 
266 
267 bool
268 TTracker::HandleScriptingMessage(BMessage* /*message*/)
269 {
270 	return false;
271 }
272 
273 
274 bool
275 TTracker::CreateProperty(BMessage* /*message*/, BMessage*, int32 /*form*/,
276 	const char* /*property*/, BMessage* /*reply*/)
277 {
278 	return false;
279 }
280 
281 
282 bool
283 TTracker::DeleteProperty(BMessage* /*specifier*/, int32 /*form*/,
284 	const char* /*property*/, BMessage*)
285 {
286 	return false;
287 }
288 
289 #endif	// _SUPPORTS_FEATURE_SCRIPTING
290 
291 
292 bool
293 TTracker::ExecuteProperty(BMessage*, int32 form, const char* property, BMessage*)
294 {
295 	if (strcmp(property, kPropertyPreferences) == 0) {
296 
297 		if (form != B_DIRECT_SPECIFIER)
298 			// only support direct specifier
299 			return false;
300 
301 		ShowSettingsWindow();
302 		return true;
303 
304 	}
305 	return false;
306 }
307 
308 
309 bool
310 TTracker::CountProperty(BMessage*, int32, const char*, BMessage*)
311 {
312 	return false;
313 }
314 
315 
316 bool
317 TTracker::GetProperty(BMessage*, int32, const char*, BMessage*)
318 {
319 	return false;
320 }
321 
322 
323 bool
324 TTracker::SetProperty(BMessage*, BMessage*, int32, const char*, BMessage*)
325 {
326 	return false;
327 }
328