xref: /haiku/src/kits/tracker/FindPanel.h (revision fc7456e9b1ec38c941134ed6d01c438cf289381e)
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 #ifndef _FIND_PANEL_H
35 #define _FIND_PANEL_H
36 
37 
38 #include <ByteOrder.h>
39 #include <FilePanel.h>
40 #include <ObjectList.h>
41 #include <View.h>
42 #include <Window.h>
43 
44 #include "DialogPane.h"
45 #include "MimeTypeList.h"
46 #include "Utilities.h"
47 #include "NodeWalker.h"
48 
49 
50 class BFilePanel;
51 class BQuery;
52 class BBox;
53 class BTextControl;
54 class BCheckBox;
55 class BMenuBar;
56 class BMenuField;
57 class BFile;
58 class BPopUpMenu;
59 class BGroupView;
60 class BGridLayout;
61 
62 namespace BPrivate {
63 
64 class FindPanel;
65 class Model;
66 class DraggableIcon;
67 class TAttrView;
68 
69 const uint32 kVolumeItem = 'Fvol';
70 const uint32 kAttributeItemMain = 'Fatr';
71 const uint32 kByNameItem = 'Fbyn';
72 const uint32 kByAttributeItem = 'Fbya';
73 const uint32 kByFormulaItem = 'Fbyq';
74 const uint32 kAddItem = 'Fadd';
75 const uint32 kRemoveItem = 'Frem';
76 
77 const uint32 kClearHistory = 'FClH';
78 const uint32 kClearTemplates = 'FClT';
79 const uint32 kSaveQueryOrTemplate = 'FSaQ';
80 const uint32 kOpenSaveAsPanel = 'Fosv';
81 const uint32 kOpenLoadQueryPanel = 'Folo';
82 const uint32 kTemporaryOptionClicked = 'FTCl';
83 const uint32 kSearchInTrashOptionClicked = 'FSCl';
84 
85 const uint32 kSelectDirectoryFilter = 'FSeD';
86 const uint32 kAddDirectoryFilters = 'FAdF';
87 const uint32 kRemoveDirectoryFilter = 'FRmD';
88 
89 #ifdef _IMPEXP_TRACKER
90 _IMPEXP_TRACKER
91 #endif
92 BMenu* TrackerBuildRecentFindItemsMenu(const char* title);
93 
94 struct MoreOptionsStruct {
95 			// Some options used to be in a collapsable part of the window, but
96 			// this was removed. Now the options are always visible.
97 			bool 				showMoreOptions;
98 			bool 				searchTrash;
99 			// reserve a bunch of fields so that we can add stuff later but not
100 			// make old queries incompatible. Reserved fields are set to 0 when
101 			// saved
102 			int32 				reserved1;
103 			bool 				temporary;
104 			bool 				reserved9;
105 			bool 				reserved10;
106 			bool 				reserved11;
107 			int32 				reserved3;
108 			int32 				reserved4;
109 			int32 				reserved5;
110 			int32 				reserved6;
111 			int32 				reserved7;
112 			int32 				reserved8;
113 
114 								MoreOptionsStruct()
115 									:
116 									showMoreOptions(true),
117 									searchTrash(false),
118 									reserved1(0),
119 									temporary(true),
120 									reserved9(false),
121 									reserved10(false),
122 									reserved11(false),
123 									reserved3(0),
124 									reserved4(0),
125 									reserved5(0),
126 									reserved6(0),
127 									reserved7(0),
128 									reserved8(0)
129 								{}
130 
131 	static	void				EndianSwap(void* castToThis);
132 
133 	static	void 				SetQueryTemporary(BNode*, bool on);
134 	static	bool 				QueryTemporary(const BNode*);
135 };
136 
137 
138 class FindWindow : public BWindow {
139 public:
140 								FindWindow(const entry_ref* ref = NULL,
141 									bool editIfTemplateOnly = false);
142 	virtual 					~FindWindow();
143 
144 			FindPanel* 			BackgroundView() const { return fBackground; }
145 
146 			BNode* 				QueryNode() const { return fFile; }
147 
148 	// reads in the query name from either a saved name in a template
149 	// or form a saved query name
150 	const 	char* 				QueryName() const;
151 
152 	static 	bool 				IsQueryTemplate(BNode* file);
153 			void				SetOptions(bool searchInTrash);
154 			void				AddIconToMenuBar(BView*);
155 
156 protected:
157 	virtual	void 				MessageReceived(BMessage* message);
158 
159 private:
160 	static 	BFile* 				TryOpening(const entry_ref* ref);
161 	// when opening an empty panel, use the default query to set the panel up
162 	static 	void 				GetDefaultQuery(BEntry& entry);
163 			void 				SaveQueryAttributes(BNode* file, bool templateQuery);
164 
165 			// retrieve the results
166 			void 				Find();
167 			// save the contents of the find window into the query file
168 			void 				Save();
169 
170 			void 				SwitchToTemplate(const entry_ref*);
171 			bool 				FindSaveCommon(bool find);
172 
173 			status_t 			SaveQueryAsAttributes(BNode*, BEntry*, bool queryTemplate,
174 									const BMessage* oldAttributes = 0,
175 									const BPoint* oldLocation = 0, bool temporary = true);
176 			status_t			GetQueryLastChangeTimeFromFile(BMessage* message);
177 
178 			void 				GetDefaultName(BString&);
179 			// dynamic date is a date such as 'today'
180 			void 				GetPredicateString(BString&, bool& dynamicDate);
181 
182 			void				BuildMenuBar();
183 			void				PopulateTemplatesMenu();
184 			void				UpdateFileReferences(const entry_ref*);
185 			void				ClearHistoryOrTemplates(bool clearTemplates, bool temporaryOnly);
186 			status_t			DeleteQueryOrTemplate(BEntry*);
187 
188 private:
189 			BFile* 				fFile;
190 			entry_ref 			fRef;
191 			bool 				fFromTemplate;
192 			bool 				fEditTemplateOnly;
193 			FindPanel* 			fBackground;
194 	mutable BString 			fQueryNameFromTemplate;
195 			BFilePanel* 		fSaveAsPanel;
196 			BFilePanel*			fOpenQueryPanel;
197 
198 			// Menu Bar For New Panel
199 			BGroupView*			fMenuBarContainer;
200 			BMenuBar*			fMenuBar;
201 			BMenu*				fQueryMenu;
202 			BMenuItem*			fSaveQueryOrTemplateItem;
203 			BMenu*				fOptionsMenu;
204 			BMenu*				fTemplatesMenu;
205 			BMenu*				fHistoryMenu;
206 			BMenu*				fSaveAsMenu;
207 			BMenuItem*			fSearchInTrash;
208 
209 	typedef BWindow _inherited;
210 };
211 
212 
213 class FindPanel : public BView {
214 public:
215 								FindPanel(BFile*, FindWindow* parent, bool fromTemplate,
216 									bool editTemplateOnly);
217 	virtual 					~FindPanel();
218 
219 	virtual	void 				AttachedToWindow();
220 	virtual	void 				Draw(BRect updateRect);
221 	virtual	void 				MessageReceived(BMessage*);
222 
223 			void 				BuildAttrQuery(BQuery*, bool& dynamicDate) const;
224 			BPopUpMenu* 		MimeTypeMenu() const { return fMimeTypeMenu; }
225 			BMenuItem* 			CurrentMimeType(const char** type = NULL) const;
226 			status_t 			SetCurrentMimeType(BMenuItem* item);
227 			status_t 			SetCurrentMimeType(const char* label);
228 
229 			BPopUpMenu* 		VolMenu() const { return fVolMenu; }
230 			uint32 				Mode() const { return fMode; }
231 
232 	static 	uint32 				InitialMode(const BNode* entry);
233 			void 				SaveWindowState(BNode*, bool editTemplate);
234 
235 			void 				SwitchToTemplate(const BNode*);
236 
237 			// build up a query from by-attribute items
238 			void 				GetByAttrPredicate(BQuery*, bool& dynamicDate) const;
239 			// build up a simple query from the name we are searching for
240 			void 				GetByNamePredicate(BQuery*) const;
241 
242 			void 				GetDefaultName(BString&) const;
243 			void 				GetDefaultAttrName(BString&, int32) const;
244 	// name filled out in the query name text field
245 	const 	char* 				UserSpecifiedName() const;
246 
247 	// populate the recent query menu with query templates and recent queries
248 	static 	void 				AddRecentQueries(BMenu*, bool addSaveAsItem,
249 									const BMessenger* target, uint32 what,
250 									bool includeTemplates = true,
251 									bool includeTemporaryQueries = true,
252 									bool includePersistedQueries = true);
253 
254 			status_t			SaveDirectoryFiltersToFile(BNode*);
255 			void				LoadDirectoryFiltersFromFile(const BNode*);
256 
257 private:
258 	// populates the type menu
259 	void 						AddMimeTypesToMenu();
260 	static 	bool 				AddOneMimeTypeToMenu(const ShortMimeInfo*, void* castToMenu);
261 
262 			// populates the volume menu
263 			void 				AddVolumes(BMenu*);
264 
265 			void 				ShowVolumeMenuLabel();
266 
267 			// add one more attribute item to the attr view
268 			void 				AddAttrRow();
269 			// remove the last attribute item
270 			void 				RemoveAttrRow();
271 			void 				AddFirstAttr();
272 
273 			// panel building/restoring calls
274 			void 				RestoreWindowState(const BNode*);
275 			void 				RestoreMimeTypeMenuSelection(const BNode*);
276 			void 				AddByAttributeItems(const BNode*);
277 			void 				RemoveByAttributeItems();
278 			void 				RemoveAttrViewItems(bool removeGrid = true);
279 			// MimeTypeWindow is only shown in kByNameItem and kByAttributeItem modes
280 			void 				ShowOrHideMimeTypeMenu();
281 
282 			void 				AddAttributeControls(int32);
283 
284 			// fMode gets set by this and the call relies on it being up-to-date
285 			void 				ShowOrHideMoreOptions(bool show);
286 	static 	int32 				InitialAttrCount(const BNode*);
287 			void 				FillCurrentQueryName(BTextControl*, FindWindow*);
288 			void 				AddByNameOrFormulaItems();
289 			void 				SetupAddRemoveButtons();
290 
291 			// go from search by name to search by attribute, etc.
292 			void 				SwitchMode(uint32);
293 
294 			void 				PushMimeType(BQuery* query) const;
295 
296 			void 				SaveAsQueryOrTemplate(const entry_ref*,
297 									const char*, bool queryTemplate);
298 
299 			BView* 				FindAttrView(const char*, int row) const;
300 
301 			void 				AddAttributes(BMenu* menu, const BMimeType& type);
302 			void 				AddMimeTypeAttrs(BMenu* menu);
303 			void 				RestoreAttrState(const BMessage&, int32);
304 			void 				SaveAttrState(BMessage*, int32);
305 			void 				AddLogicMenu(int32, bool selectAnd = true);
306 			void 				RemoveLogicMenu(int32);
307 
308 			void 				ResizeMenuField(BMenuField*);
309 
310 			status_t			AddDirectoryFiltersToMenu(BMenu*, BHandler* target);
311 			status_t			AddDirectoryFilter(const entry_ref* ref, bool addToMenu = true);
312 			void				RemoveDirectoryFilter(const entry_ref* ref);
313 	static	status_t			AddDirectoryFilterItemToMenu(BMenu*, const entry_ref* ref,
314 									BHandler* target, int32 index = -1);
315 			void				UnmarkDisks();
316 			void				MarkDiskAccordingToDirectoryFilter(entry_ref*);
317 
318 public:
319 			BObjectList<entry_ref>	fDirectoryFilters;
320 
321 private:
322 			uint32 				fMode;
323 			BGridLayout*		fAttrGrid;
324 			BPopUpMenu* 		fMimeTypeMenu;
325 			BMenuField* 		fMimeTypeField;
326 			BPopUpMenu*			fSearchModeMenu;
327 			BMenuField* 		fSearchModeField;
328 			BPopUpMenu* 		fVolMenu;
329 			BMenuField* 		fVolumeField;
330 			BPopUpMenu* 		fRecentQueries;
331 			BBox* 				fMoreOptions;
332 			BTextControl* 		fQueryName;
333 			BString 			fInitialQueryName;
334 
335 			DraggableIcon* 		fDraggableIcon;
336 
337 			BFilePanel*			fDirectorySelectPanel;
338 
339 			bool				fAddSeparatorItemState;
340 
341 	typedef BView _inherited;
342 
343 	friend class RecentQueriesPopUp;
344 };
345 
346 
347 // transient queries get deleted if they didn't get used in a while;
348 // this is the task that takes care of it
349 class DeleteTransientQueriesTask {
350 public:
351 	virtual 					~DeleteTransientQueriesTask();
352 
353 	static 	void 				StartUpTransientQueryCleaner();
354 
355 			// returns true when done
356 			bool 				DoSomeWork();
357 
358 protected:
359 								DeleteTransientQueriesTask();
360 
361 			void 				Initialize();
362 			bool 				GetSome();
363 
364 			bool 				ProcessOneRef(Model*);
365 
366 protected:
367 			enum State {
368 				kInitial,
369 				kAllocatedWalker,
370 				kTraversing,
371 				kError
372 			};
373 
374 protected:
375 			State 				state;
376 
377 private:
378 			BTrackerPrivate::TNodeWalker* fWalker;
379 };
380 
381 
382 class RecentFindItemsMenu : public BMenu {
383 public:
384 								RecentFindItemsMenu(const char* title,
385 									const BMessenger* target, uint32 what);
386 
387 protected:
388 	virtual void 				AttachedToWindow();
389 
390 private:
391 			BMessenger 			fTarget;
392 			uint32 				fWhat;
393 };
394 
395 
396 // query/query template drag&drop helper
397 class DraggableQueryIcon : public DraggableIcon {
398 public:
399 								DraggableQueryIcon(BRect frame, const char* name,
400 									const BMessage* message, BMessenger target,
401 									uint32 resizeFlags = B_FOLLOW_LEFT | B_FOLLOW_TOP,
402 									uint32 flags = B_WILL_DRAW);
403 
404 protected:
405 	virtual bool 				DragStarted(BMessage*);
406 	virtual	void				Draw(BRect);
407 };
408 
409 
410 class FolderFilter : public BRefFilter {
411 public:
412 	virtual	bool				Filter(const entry_ref*, BNode*, struct stat_beos*, const char*);
413 };
414 
415 } // namespace BPrivate
416 
417 using namespace BPrivate;
418 
419 #endif	// _FIND_PANEL_H
420