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