xref: /haiku/src/kits/tracker/OpenWithWindow.h (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
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	_OPEN_WITH_WINDOW_H
36 #define _OPEN_WITH_WINDOW_H
37 
38 #include <String.h>
39 
40 #include "ContainerWindow.h"
41 #include "EntryIterator.h"
42 #include "NodeWalker.h"
43 #include "PoseView.h"
44 #include "Query.h"
45 #include "SlowMenu.h"
46 #include "Utilities.h"
47 
48 namespace BPrivate {
49 
50 class OpenWithPoseView;
51 
52 // OpenWithContainerWindow supports the Open With feature
53 
54 enum {
55 	kUnknownRelation = -1,
56 	kNoRelation = 0,
57 	kSuperhandler,
58 	kSupportsSupertype,
59 	kSupportsType,
60 	kPreferredForType,
61 	kPreferredForFile
62 };
63 
64 // pass in a predicate; a query will search for matches
65 // matches will be returned in iteration
66 class SearchForSignatureEntryList : public EntryListBase {
67 	public:
68 		SearchForSignatureEntryList(bool canAddAllApps);
69 		virtual ~SearchForSignatureEntryList();
70 
71 		void PushUniqueSignature(const char *);
72 			// add one signature to search for
73 
74 		// entry list iterators
75 		virtual status_t GetNextEntry(BEntry *entry, bool traverse = false);
76 		virtual status_t GetNextRef(entry_ref *ref);
77 		virtual int32 GetNextDirents(struct dirent *buffer, size_t length,
78 			int32 count = INT_MAX);
79 
80 		virtual status_t Rewind();
81 		virtual int32 CountEntries();
82 
83 		bool GetPreferredApp(entry_ref *ref) const;
84 			// gets the preferred app for all the files it was asked to
85 			// find supporting apps for, returns false if no preferred app
86 			// found or if more than one found
87 		void TrySettingPreferredApp(const entry_ref *);
88 		void TrySettingPreferredAppForFile(const entry_ref *);
89 
90 		int32 Relation(const BMessage *entriesToOpen, const Model *) const;
91 			// returns the reason why an application is shown in Open With window
92 		void RelationDescription(const BMessage *entriesToOpen, const Model *,
93 			BString *) const;
94 			// returns a string describing why application handles files to open
95 
96 		static int32 Relation(const BMessage *entriesToOpen,
97 			const Model *, const entry_ref *preferredApp,
98 			const entry_ref *preferredAppForFile);
99 			// returns the reason why an application is shown in Open With window
100 			// static version, needs the preferred app for preformance
101 		static void RelationDescription(const BMessage *entriesToOpen,
102 			const Model *, BString *, const entry_ref *preferredApp,
103 			const entry_ref *preferredAppForFile);
104 			// returns a string describing why application handles files to open
105 
106 		bool CanOpenWithFilter(const Model *appModel, const BMessage *entriesToOpen,
107 			const entry_ref *preferredApp);
108 
109 		void NonGenericFileFound();
110 		bool GenericFilesOnly() const;
111 
112 		bool ShowAllApplications() const;
113 
114 	private:
115 		static int32 Relation(const Model *node, const Model *app);
116 			// returns the reason why an application is shown in Open With window
117 
118 		CachedEntryIteratorList *fIteratorList;
119 		BObjectList<BString> fSignatures;
120 
121 		entry_ref fPreferredRef;
122 		int32 fPreferredAppCount;
123 		entry_ref fPreferredRefForFile;
124 		int32 fPreferredAppForFileCount;
125 		bool fGenericFilesOnly;
126 		bool fCanAddAllApps;
127 		bool fFoundOneNonSuperHandler;
128 };
129 
130 class OpenWithContainerWindow : public BContainerWindow {
131 	public:
132 		OpenWithContainerWindow(BMessage *entriesToOpen,
133 			LockingList<BWindow> *windowList,
134 			window_look look = B_DOCUMENT_WINDOW_LOOK,
135 			window_feel feel = B_NORMAL_WINDOW_FEEL,
136 			uint32 flags = 0, uint32 workspace = B_CURRENT_WORKSPACE);
137 			// <entriesToOpen> eventually get opened by the selected app
138 		virtual ~OpenWithContainerWindow();
139 
140 		virtual void Init(const BMessage *message);
141 
142 		const BMessage *EntryList() const;
143 			// return the list of the entries we are supposed to open
144 
145 		void SetCanSetAppAsDefault(bool);
146 		void SetCanOpen(bool);
147 
148 		OpenWithPoseView *PoseView() const;
149 
150 	protected:
151 		virtual BPoseView *NewPoseView(Model *model, BRect rect, uint32 viewMode);
152 
153 		virtual	bool ShouldAddMenus() const;
154 		virtual	void ShowContextMenu(BPoint, const entry_ref *, BView *);
155 		virtual void AddShortcuts();
156 		virtual void NewAttributeMenu(BMenu *);
157 
158 		virtual void RestoreState();
159 		virtual	void RestoreState(const BMessage &);
160 		virtual void RestoreWindowState(AttributeStreamNode *);
161 		virtual void RestoreWindowState(const BMessage &);
162 		virtual bool NeedsDefaultStateSetup();
163 		virtual	void SaveState(bool hide = true);
164 		virtual	void SaveState(BMessage &) const;
165 		virtual void SetUpDefaultState();
166 
167 		virtual bool IsShowing(const node_ref *) const;
168 		virtual bool IsShowing(const entry_ref *) const;
169 
170 		virtual void MessageReceived(BMessage *);
171 
172 		void OpenWithSelection();
173 			// open entries with the selected app
174 		void MakeDefaultAndOpen();
175 			// open entries with the selected app and make it the default handler
176 
177 	private:
178 		static filter_result KeyDownFilter(BMessage *, BHandler **, BMessageFilter *);
179 
180 		BMessage *fEntriesToOpen;
181 		BButton *fLaunchButton;
182 		BButton *fLaunchAndMakeDefaultButton;
183 		float fMinimalWidth;
184 
185 		typedef BContainerWindow _inherited;
186 };
187 
188 class OpenWithPoseView : public BPoseView {
189 	public:
190 		OpenWithPoseView(BRect, uint32 resizeMask = B_FOLLOW_ALL);
191 
192 		virtual void OpenSelection(BPose *, int32 *);
193 			// open entries with the selected app
194 
195 		int32 OpenWithRelation(const Model *) const;
196 			// returns the reason why an application is shown in Open With window
197 		void OpenWithRelationDescription(const Model *, BString *) const;
198 			// returns a string describing why application handles files to open
199 
200 		OpenWithContainerWindow *ContainerWindow() const;
201 
202 		virtual bool AddPosesThreadValid(const entry_ref *) const;
203 
204 	protected:
205 		// don't do any volume watching and memtamime watching in open with panels for now
206 		virtual void InitialStartWatching() {}
207 		virtual void FinalStopWatching() {}
208 
209 		virtual void AttachedToWindow();
210 		EntryListBase *InitDirentIterator(const entry_ref *ref);
211 
212 		virtual void SetUpDefaultColumnsIfNeeded();
213 			// show launch window specific columns
214 
215 		// empty overrides for functions that depend on having an fModel
216 		virtual	void SaveState(AttributeStreamNode *);
217 		virtual void RestoreState(AttributeStreamNode *);
218 		virtual	void SaveState(BMessage &) const;
219 		virtual void RestoreState(const BMessage &);
220 		virtual void SavePoseLocations(BRect * = NULL);
221 		virtual void MoveSelectionToTrash(bool selectNext = true);
222 		virtual void MoveSelectionTo(BPoint, BPoint, BContainerWindow*);
223 		virtual void MoveSelectionInto(Model* destFolder, BContainerWindow *srcWindow,
224 			bool forceCopy, bool create_link = false);
225 		virtual bool HandleMessageDropped(BMessage *);
226 		virtual bool CanHandleDragSelection(const Model *, const BMessage *, bool);
227 
228 		virtual bool Represents(const node_ref *) const;
229 		virtual bool Represents(const entry_ref *) const;
230 
231 		virtual void CreatePoses(Model **models, PoseInfo *poseInfoArray, int32 count,
232 			BPose **resultingPoses, bool insertionSort = true, int32 *lastPoseIndexPtr = NULL,
233 			BRect *boundsPtr = NULL, bool forceDraw = false);
234 			// override to add selecting the default handling app for selection
235 
236 		virtual bool ShouldShowPose(const Model *, const PoseInfo *);
237 
238 		virtual void Pulse();
239 
240 		virtual void KeyDown(const char *bytes, int32 count);
241 
242 	private:
243 		entry_ref fPreferredRef;
244 		bool fHaveCommonPreferredApp;
245 
246 		SearchForSignatureEntryList *fIterator;
247 			// private copy of the iterator pointer
248 
249 		typedef BPoseView _inherited;
250 };
251 
252 class RelationCachingModelProxy {
253 	public:
254 		RelationCachingModelProxy(Model *model);
255 		~RelationCachingModelProxy();
256 
257 		int32 Relation(SearchForSignatureEntryList *iterator, BMessage *entries) const;
258 
259 		Model *fModel;
260 		mutable int32 fRelation;
261 };
262 
263 class OpenWithMenu : public BSlowMenu {
264 	public:
265 		OpenWithMenu(const char *label, const BMessage *entriesToOpen,
266 			BWindow *parentWindow, BHandler *target);
267 		OpenWithMenu(const char *label, const BMessage *entriesToOpen,
268 			BWindow *parentWindow, const BMessenger &target);
269 
270 	private:
271 		friend int SortByRelationAndName(const RelationCachingModelProxy *,
272 			const RelationCachingModelProxy *, void *);
273 
274 		virtual bool StartBuildingItemList();
275 		virtual bool AddNextItem();
276 		virtual void DoneBuildingItemList();
277 		virtual void ClearMenuBuildingState();
278 
279 		BMessage fEntriesToOpen;
280 		BHandler *target;
281 		BMessenger fMessenger;
282 
283 		// menu building state
284 		SearchForSignatureEntryList *fIterator;
285 		entry_ref fPreferredRef;
286 		BObjectList<RelationCachingModelProxy> *fSupportingAppList;
287 		bool fHaveCommonPreferredApp;
288 		BWindow *fParentWindow;
289 
290 		typedef BSlowMenu _inherited;
291 };
292 
293 // used for optionally showing the list of all apps. Do nothing
294 // until asked to iterate and only if supposed to do so
295 class ConditionalAllAppsIterator : public EntryListBase {
296 	public:
297 		ConditionalAllAppsIterator(SearchForSignatureEntryList *parent);
298 		~ConditionalAllAppsIterator();
299 
300 		virtual status_t GetNextEntry(BEntry *entry, bool traverse = false);
301 		virtual status_t GetNextRef(entry_ref *ref);
302 		virtual int32 GetNextDirents(struct dirent *buffer, size_t length,
303 			int32 count = INT_MAX);
304 
305 		virtual status_t Rewind();
306 		virtual int32 CountEntries();
307 
308 	protected:
309 		bool Iterate() const;
310 		void Instantiate();
311 
312 	private:
313 		SearchForSignatureEntryList *fParent;
314 		BTrackerPrivate::TWalker *fWalker;
315 };
316 
317 } // namespace BPrivate
318 
319 using namespace BPrivate;
320 
321 #endif	// _OPEN_WITH_WINDOW_H
322