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