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