xref: /haiku/src/kits/tracker/FSUtils.h (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
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 FS_UTILS_H
35 #define FS_UTILS_H
36 
37 
38 #include <FindDirectory.h>
39 #include <List.h>
40 #include <Point.h>
41 #include <StorageDefs.h>
42 
43 #include <vector>
44 
45 #include "Model.h"
46 #include "ObjectList.h"
47 
48 
49 // APIs/code in FSUtils.h and FSUtils.cpp is slated for a major cleanup
50 // -- in other words, you will find a lot of ugly cruft in here
51 
52 class BDirectory;
53 class BEntry;
54 class BList;
55 class BFile;
56 
57 namespace BPrivate {
58 
59 class BInfoWindow;
60 
61 //! Controls the copy engine; may be overriden to specify how conflicts are
62 // handled, etc.
63 class CopyLoopControl {
64 public:
65 	virtual						~CopyLoopControl();
66 
67 	virtual	void				Init(uint32 jobKind);
68 	virtual	void				Init(int32 totalItems, off_t totalSize,
69 									const entry_ref* destDir = NULL,
70 									bool showCount = true);
71 
72 	//! Inform that a file error occurred while copying <name>.
73 	// \return \c True if user decided to continue
74 	virtual	bool				FileError(const char* message,
75 									const char* name, status_t error,
76 									bool allowContinue);
77 
78 	virtual	void				UpdateStatus(const char* name,
79 									const entry_ref& ref, int32 count,
80 									bool optional = false);
81 
82 	//! \return \c true if canceled
83 	virtual	bool				CheckUserCanceled();
84 
85 			enum OverwriteMode {
86 				kSkip,			// do not replace, go to next entry
87 				kReplace,		// remove entry before copying new one
88 				kMerge			// for folders: leave existing folder, update
89 								// contents leaving nonconflicting items
90 								// for files: save original attributes on file
91 			};
92 
93 	//! Override to always overwrite, never overwrite, let user decide,
94 	// compare dates, etc.
95 	virtual	OverwriteMode		OverwriteOnConflict(const BEntry* srcEntry,
96 									const char* destName,
97 									const BDirectory* destDir,
98 									bool srcIsDir, bool dstIsDir);
99 
100 	//! Override to prevent copying of a given file or directory
101 	virtual	bool				SkipEntry(const BEntry*, bool file);
102 
103 	//! During a file copy, this is called every time a chunk of data
104 	// is copied.  Users may override to keep a running checksum.
105 	virtual	void				ChecksumChunk(const char* block, size_t size);
106 
107 	//! This is called when a file is finished copying.  Users of this
108 	// class may override to verify that the checksum they've been
109 	// computing in ChecksumChunk matches.  If this returns true,
110 	// the copy will continue.  If false, if will abort.
111 	virtual	bool				ChecksumFile(const entry_ref*);
112 
113 	virtual	bool				SkipAttribute(const char* attributeName);
114 	virtual	bool				PreserveAttribute(const char* attributeName);
115 };
116 
117 
118 //! This is the Tracker copy-specific version of CopyLoopControl.
119 class TrackerCopyLoopControl : public CopyLoopControl {
120 public:
121 								TrackerCopyLoopControl();
122 								TrackerCopyLoopControl(uint32 jobKind);
123 								TrackerCopyLoopControl(int32 totalItems,
124 									off_t totalSize);
125 	virtual						~TrackerCopyLoopControl();
126 
127 	virtual	void				Init(uint32 state);
128 	virtual	void				Init(int32 totalItems, off_t totalSize,
129 									const entry_ref* destDir = NULL,
130 									bool showCount = true);
131 
132 	virtual	bool				FileError(const char* message,
133 									const char* name, status_t error,
134 									bool allowContinue);
135 
136 	virtual	void				UpdateStatus(const char* name,
137 									const entry_ref& ref, int32 count,
138 									bool optional = false);
139 
140 	virtual	bool				CheckUserCanceled();
141 
142 	virtual	bool				SkipAttribute(const char* attributeName);
143 
144 
145 	// One can specify an entry_ref list with the source entries. This will
146 	// then trigger the feature to pull additional source entries from the
147 	// status window, such that the user can drop additional items onto the
148 	// progress display of the ongoing copy process to copy these items to
149 	// the same target directory.
150 			typedef BObjectList<entry_ref> EntryList;
151 
152 			void				SetSourceList(EntryList* list);
153 
154 private:
155 			thread_id			fThread;
156 
157 			EntryList*			fSourceList;
158 };
159 
160 
161 #define B_DESKTOP_DIR_NAME "Desktop"
162 
163 #ifndef _IMPEXP_TRACKER
164 #define _IMPEXP_TRACKER
165 #endif
166 _IMPEXP_TRACKER status_t FSCopyAttributesAndStats(BNode*, BNode*);
167 
168 _IMPEXP_TRACKER void FSDuplicate(BObjectList<entry_ref>* srcList,
169 	BList* pointList);
170 _IMPEXP_TRACKER void FSMoveToFolder(BObjectList<entry_ref>* srcList, BEntry*,
171 	uint32 moveMode, BList* pointList = NULL);
172 _IMPEXP_TRACKER void FSMakeOriginalName(char* name, BDirectory* destDir,
173 	const char* suffix);
174 _IMPEXP_TRACKER bool FSIsTrashDir(const BEntry*);
175 _IMPEXP_TRACKER bool FSIsPrintersDir(const BEntry*);
176 _IMPEXP_TRACKER bool FSIsDeskDir(const BEntry*);
177 _IMPEXP_TRACKER bool FSIsHomeDir(const BEntry*);
178 _IMPEXP_TRACKER bool FSIsRootDir(const BEntry*);
179 _IMPEXP_TRACKER void FSMoveToTrash(BObjectList<entry_ref>* srcList,
180 	BList* pointList = NULL, bool async = true);
181 	// Deprecated
182 
183 void FSDeleteRefList(BObjectList<entry_ref>*, bool, bool confirm = true);
184 void FSDelete(entry_ref*, bool, bool confirm = true);
185 void FSRestoreRefList(BObjectList<entry_ref>* list, bool async);
186 
187 _IMPEXP_TRACKER status_t FSLaunchItem(const entry_ref* application,
188 	const BMessage* refsReceived, bool async, bool openWithOK);
189 	// Preferred way of launching; only pass an actual application in
190 	// <application>, not a document; to open documents with the preferred
191 	// app, pase 0 in <application> and stuff all the document refs into
192 	// <refsReceived> Consider having silent mode that does not show alerts,
193 	// just returns error code
194 
195 _IMPEXP_TRACKER status_t FSOpenWith(BMessage* listOfRefs);
196 	// runs the Open With window; pas a list of refs
197 
198 _IMPEXP_TRACKER void FSEmptyTrash();
199 _IMPEXP_TRACKER status_t FSCreateNewFolderIn(const node_ref* destDir,
200 	entry_ref* newRef, node_ref* new_node);
201 _IMPEXP_TRACKER void FSCreateTrashDirs();
202 _IMPEXP_TRACKER status_t FSGetTrashDir(BDirectory* trashDir, dev_t volume);
203 _IMPEXP_TRACKER status_t FSGetDeskDir(BDirectory* deskDir);
204 _IMPEXP_TRACKER status_t FSRecursiveCalcSize(BInfoWindow*,
205 	CopyLoopControl* loopControl, BDirectory*, off_t* runningSize,
206 	int32* fileCount, int32* dirCount);
207 
208 bool FSInTrashDir(const entry_ref*);
209 
210 // doesn't need to be exported
211 bool FSGetPoseLocation(const BNode* node, BPoint* point);
212 status_t FSSetPoseLocation(BEntry* entry, BPoint point);
213 status_t FSSetPoseLocation(ino_t destDirInode, BNode* destNode, BPoint point);
214 status_t FSGetBootDeskDir(BDirectory* deskDir);
215 
216 status_t FSGetOriginalPath(BEntry* entry, BPath* path);
217 
218 enum ReadAttrResult {
219 	kReadAttrFailed,
220 	kReadAttrNativeOK,
221 	kReadAttrForeignOK
222 };
223 
224 ReadAttrResult ReadAttr(const BNode*, const char* hostAttrName,
225 	const char* foreignAttrName, type_code, off_t, void*, size_t,
226 	void (*swapFunc)(void*) = 0, bool isForeign = false);
227 	// Endian swapping ReadAttr call; endianness is determined by trying
228 	// first the native attribute name, then the foreign one; an endian
229 	// swapping function can be passed, if null data won't be swapped;
230 	// if <isForeign> set the foreign endianness will be read directly
231 	// without first trying the native one
232 
233 ReadAttrResult GetAttrInfo(const BNode*, const char* hostAttrName,
234 	const char* foreignAttrName, type_code* = NULL, size_t* = NULL);
235 
236 status_t FSCreateNewFolder(const entry_ref*);
237 status_t FSRecursiveCreateFolder(const char* path);
238 void FSMakeOriginalName(BString &name, const BDirectory* destDir,
239 	const char* suffix = 0);
240 
241 status_t FSGetParentVirtualDirectoryAware(const BEntry& entry, entry_ref& _ref);
242 status_t FSGetParentVirtualDirectoryAware(const BEntry& entry, BEntry& _entry);
243 status_t FSGetParentVirtualDirectoryAware(const BEntry& entry, BNode& _node);
244 
245 status_t TrackerLaunch(const entry_ref* app, bool async);
246 status_t TrackerLaunch(const BMessage* refs, bool async,
247 	bool okToRunOpenWith = true);
248 status_t TrackerLaunch(const entry_ref* app, const BMessage* refs, bool async,
249 	bool okToRunOpenWith = true);
250 status_t LaunchBrokenLink(const char*, const BMessage*);
251 
252 status_t FSFindTrackerSettingsDir(BPath*, bool autoCreate = true);
253 
254 bool FSIsDeskDir(const BEntry*);
255 
256 // two separate ifYouDoAction and toDoAction versions are needed for
257 // localization purposes. The first one is used in "If you do action..."
258 // sentence, the second one in the "To do action" sentence.
259 bool ConfirmChangeIfWellKnownDirectory(const BEntry* entry,
260 	const char* ifYouDoAction, const char* toDoAction,
261 	const char* toConfirmAction, bool dontAsk = false,
262 	int32* confirmedAlready = NULL);
263 
264 bool CheckDevicesEqual(const entry_ref* entry, const Model* targetModel);
265 
266 // Deprecated calls use newer calls above instead
267 _IMPEXP_TRACKER void FSLaunchItem(const entry_ref*, BMessage* = NULL,
268 	int32 workspace = -1);
269 _IMPEXP_TRACKER status_t FSLaunchItem(const entry_ref*, BMessage*,
270 	int32 workspace, bool asynch);
271 _IMPEXP_TRACKER void FSOpenWithDocuments(const entry_ref* executableToLaunch,
272 	BMessage* documentEntryRefs);
273 _IMPEXP_TRACKER status_t FSLaunchUsing(const entry_ref* ref,
274 	BMessage* listOfRefs);
275 
276 
277 // some extra directory_which values
278 // move these to FindDirectory.h
279 const uint32 B_USER_MAIL_DIRECTORY = 3500;
280 const uint32 B_USER_QUERIES_DIRECTORY = 3501;
281 const uint32 B_USER_PEOPLE_DIRECTORY = 3502;
282 const uint32 B_USER_DOWNLOADS_DIRECTORY = 3503;
283 const uint32 B_USER_DESKBAR_APPS_DIRECTORY = 3504;
284 const uint32 B_USER_DESKBAR_PREFERENCES_DIRECTORY = 3505;
285 const uint32 B_USER_DESKBAR_DEVELOP_DIRECTORY = 3506;
286 const uint32 B_BOOT_DISK = 3507;
287 	// map /boot into the directory_which enum for convenience
288 
289 class WellKnowEntryList {
290 	// matches up names, id's and node_refs of well known entries in the
291 	// system hierarchy
292 	public:
293 		struct WellKnownEntry {
294 			WellKnownEntry(const node_ref* node, directory_which which,
295 				const char* name)
296 				:
297 				node(*node),
298 				which(which),
299 				name(name)
300 			{
301 			}
302 
303 			// mwcc needs these explicitly to use vector
304 			WellKnownEntry(const WellKnownEntry &clone)
305 				:
306 				node(clone.node),
307 				which(clone.which),
308 				name(clone.name)
309 			{
310 			}
311 
312 			WellKnownEntry()
313 			{
314 			}
315 
316 			node_ref node;
317 			directory_which which;
318 			BString name;
319 		};
320 
321 		static directory_which Match(const node_ref*);
322 		static const WellKnownEntry* MatchEntry(const node_ref*);
323 		static void Quit();
324 
325 	private:
326 		const WellKnownEntry* MatchEntryCommon(const node_ref*);
327 		WellKnowEntryList();
328 		void AddOne(directory_which, const char* name);
329 		void AddOne(directory_which, const char* path, const char* name);
330 		void AddOne(directory_which, directory_which base,
331 			const char* extension, const char* name);
332 
333 		std::vector<WellKnownEntry> entries;
334 		static WellKnowEntryList* self;
335 };
336 
337 #if B_BEOS_VERSION_DANO
338 #undef _IMPEXP_TRACKER
339 #endif
340 
341 } // namespace BPrivate
342 
343 using namespace BPrivate;
344 
345 #endif	// FS_UTILS_H
346