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