xref: /haiku/src/kits/tracker/FindPanel.h (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
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 BMenuField;
55 class BFile;
56 class BPopUpMenu;
57 class BGridLayout;
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 kByFormulaItem = '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* title);
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 		:
99 		showMoreOptions(false),
100 		searchTrash(false),
101 		reserved1(0),
102 		temporary(true),
103 		reserved9(false),
104 		reserved10(false),
105 		reserved11(false),
106 		reserved3(0),
107 		reserved4(0),
108 		reserved5(0),
109 		reserved6(0),
110 		reserved7(0),
111 		reserved8(0)
112 	{
113 	}
114 
115 	static void EndianSwap(void* castToThis);
116 
117 	static void SetQueryTemporary(BNode*, bool on);
118 	static bool QueryTemporary(const BNode*);
119 };
120 
121 
122 class FindWindow : public BWindow {
123 public:
124 	FindWindow(const entry_ref* ref = NULL,
125 		bool editIfTemplateOnly = false);
126 	virtual ~FindWindow();
127 
128 	FindPanel* BackgroundView() const
129 		{ return fBackground; }
130 
131 	BNode* QueryNode() const
132 		{ return fFile; }
133 
134 	const char* QueryName() const;
135 		// reads in the query name from either a saved name in a template
136 		// or form a saved query name
137 
138 	static bool IsQueryTemplate(BNode* file);
139 
140 protected:
141 	virtual	void MessageReceived(BMessage* message);
142 
143 private:
144 	static BFile* TryOpening(const entry_ref* ref);
145 	static void GetDefaultQuery(BEntry &entry);
146 		// when opening an empty panel, use the default query to set the
147 		// panel up
148 	void SaveQueryAttributes(BNode* file, bool templateQuery);
149 
150 	void Find();
151 		// retrieve the results
152 	void Save();
153 		// save the contents of the find window into the query file
154 
155 	void SwitchToTemplate(const entry_ref*);
156 	bool FindSaveCommon(bool find);
157 
158 	status_t SaveQueryAsAttributes(BNode*, BEntry*, bool queryTemplate,
159 		const BMessage* oldAttributes = 0, const BPoint* oldLocation = 0);
160 
161 	void GetDefaultName(BString &);
162 	void GetPredicateString(BString &, bool &dynamicDate);
163 		// dynamic date is a date such as 'today'
164 
165 	BFile* fFile;
166 	entry_ref fRef;
167 	bool fFromTemplate;
168 	bool fEditTemplateOnly;
169 	FindPanel* fBackground;
170 	mutable BString fQueryNameFromTemplate;
171 	BFilePanel* fSaveAsTemplatePanel;
172 
173 	typedef BWindow _inherited;
174 };
175 
176 
177 class FindPanel : public BView {
178 public:
179 	FindPanel(BFile*, FindWindow* parent, bool fromTemplate,
180 		bool editTemplateOnly);
181 	virtual ~FindPanel();
182 
183 	virtual	void AttachedToWindow();
184 	virtual	void Draw(BRect updateRect);
185 	virtual	void MessageReceived(BMessage*);
186 
187 	void BuildAttrQuery(BQuery*, bool& dynamicDate) const;
188 	BPopUpMenu* MimeTypeMenu() const
189 		{ return fMimeTypeMenu; }
190 	BMenuItem* CurrentMimeType(const char** type = NULL) const;
191 	status_t SetCurrentMimeType(BMenuItem* item);
192 	status_t SetCurrentMimeType(const char* label);
193 
194 	BPopUpMenu* VolMenu() const
195 		{ return fVolMenu; }
196 	uint32 Mode() const
197 		{ return fMode; }
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 	void GetDefaultAttrName(BString&, int32) const;
211 	const char* UserSpecifiedName() const;
212 		// name filled out in the query name text field
213 
214 	static void AddRecentQueries(BMenu*, bool addSaveAsItem,
215 		const BMessenger* target, uint32 what);
216 		// populate the recent query menu with query templates and recent
217 		// queries
218 
219 private:
220 	void AddMimeTypesToMenu();
221 		// populates the type menu
222 	static bool AddOneMimeTypeToMenu(const ShortMimeInfo*, void* castToMenu);
223 
224 	void AddVolumes(BMenu*);
225 		// populates the volume menu
226 	void ShowVolumeMenuLabel();
227 
228 	void AddAttrRow();
229 		// add one more attribute item to the attr view
230 	void RemoveAttrRow();
231 		// remove the last attribute item
232 	void AddFirstAttr();
233 
234 	// panel building/restoring calls
235 	void RestoreWindowState(const BNode*);
236 	void RestoreMimeTypeMenuSelection(const BNode*);
237 	void AddByAttributeItems(const BNode*);
238 	void RemoveByAttributeItems();
239 	void RemoveAttrViewItems(bool removeGrid = true);
240 	void ShowOrHideMimeTypeMenu();
241 		// MimeTypeWindow is only shown in kByNameItem and
242 		// kByAttributeItem modes
243 
244 	void AddAttributeControls(int32);
245 
246 	void ShowOrHideMoreOptions(bool show);
247 		// fMode gets set by this and the call relies on it being
248 		// up-to-date
249 	static int32 InitialAttrCount(const BNode*);
250 	void FillCurrentQueryName(BTextControl*, FindWindow*);
251 	void AddByNameOrFormulaItems();
252 	void SetUpAddRemoveButtons();
253 
254 	void SwitchMode(uint32);
255 		// go from search by name to search by attribute, etc.
256 
257 	void PushMimeType(BQuery* query) const;
258 
259 	void SaveAsQueryOrTemplate(const entry_ref*, const char*, bool queryTemplate);
260 
261 	BView* FindAttrView(const char*, int row) const;
262 
263 	void AddAttributes(BMenu* menu, const BMimeType &type);
264 	void AddMimeTypeAttrs(BMenu* menu);
265 	void RestoreAttrState(const BMessage &, int32);
266 	void SaveAttrState(BMessage*, int32);
267 	void AddLogicMenu(int32, bool selectAnd = true);
268 	void RemoveLogicMenu(int32);
269 
270 	void ResizeMenuField(BMenuField*);
271 
272 	uint32 fMode;
273 	BGridLayout* fAttrGrid;
274 	BPopUpMenu* fMimeTypeMenu;
275 	BMenuField* fMimeTypeField;
276 	BPopUpMenu* fVolMenu;
277 	BPopUpMenu* fSearchModeMenu;
278 	BPopUpMenu* fRecentQueries;
279 	BBox* fMoreOptions;
280 	BTextControl* fQueryName;
281 	BString fInitialQueryName;
282 
283 	BCheckBox* fTemporaryCheck;
284 	BCheckBox* fSearchTrashCheck;
285 
286 	PaneSwitch* fLatch;
287 	DraggableIcon* fDraggableIcon;
288 
289 	typedef BView _inherited;
290 
291 	friend class RecentQueriesPopUp;
292 };
293 
294 
295 class DeleteTransientQueriesTask {
296 	// transient queries get deleted if they didn't get used in a while;
297 	// this is the task that takes care of it
298 public:
299 	static void StartUpTransientQueryCleaner();
300 
301 	bool DoSomeWork();
302 	virtual ~DeleteTransientQueriesTask();
303 
304 protected:
305 	DeleteTransientQueriesTask();
306 		// returns true when done
307 
308 	enum State {
309 		kInitial,
310 		kAllocatedWalker,
311 		kTraversing,
312 		kError
313 	};
314 
315 	State state;
316 
317 	void Initialize();
318 	bool GetSome();
319 
320 	bool ProcessOneRef(Model*);
321 
322 private:
323 	BTrackerPrivate::TNodeWalker* fWalker;
324 };
325 
326 
327 class RecentFindItemsMenu : public BMenu {
328 public:
329 	RecentFindItemsMenu(const char* title, const BMessenger* target,
330 		uint32 what);
331 
332 protected:
333 	virtual void AttachedToWindow();
334 
335 private:
336 	BMessenger fTarget;
337 	uint32 fWhat;
338 };
339 
340 
341 class DraggableQueryIcon : public DraggableIcon {
342 	// query/query template drag&drop helper
343 public:
344 	DraggableQueryIcon(BRect frame, const char* name,
345 		const BMessage* message, BMessenger target,
346 		uint32 resizeFlags = B_FOLLOW_LEFT | B_FOLLOW_TOP,
347 		uint32 flags = B_WILL_DRAW);
348 
349 protected:
350 	virtual bool DragStarted(BMessage*);
351 };
352 
353 } // namespace BPrivate
354 
355 using namespace BPrivate;
356 
357 
358 #endif	// _FIND_PANEL_H
359