1 | /* $Id: DragAndDrop.h 76379 2018-12-22 22:48:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DnD - Shared functions between host and guest.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2018 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef ___VBox_GuestHost_DragAndDrop_h
|
---|
28 | #define ___VBox_GuestHost_DragAndDrop_h
|
---|
29 |
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/fs.h>
|
---|
32 |
|
---|
33 | #include <iprt/cpp/list.h>
|
---|
34 | #include <iprt/cpp/ministring.h>
|
---|
35 |
|
---|
36 | /** DnDURIDroppedFiles flags. */
|
---|
37 | typedef uint32_t DNDURIDROPPEDFILEFLAGS;
|
---|
38 |
|
---|
39 | /** No flags specified. */
|
---|
40 | #define DNDURIDROPPEDFILE_FLAGS_NONE 0
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Class for maintaining a "dropped files" directory
|
---|
44 | * on the host or guest. This will contain all received files & directories
|
---|
45 | * for a single drag and drop operation.
|
---|
46 | *
|
---|
47 | * In case of a failed drag and drop operation this class can also
|
---|
48 | * perform a gentle rollback if required.
|
---|
49 | */
|
---|
50 | class DnDDroppedFiles
|
---|
51 | {
|
---|
52 |
|
---|
53 | public:
|
---|
54 |
|
---|
55 | DnDDroppedFiles(void);
|
---|
56 | DnDDroppedFiles(const char *pszPath, DNDURIDROPPEDFILEFLAGS fFlags = DNDURIDROPPEDFILE_FLAGS_NONE);
|
---|
57 | virtual ~DnDDroppedFiles(void);
|
---|
58 |
|
---|
59 | public:
|
---|
60 |
|
---|
61 | int AddFile(const char *pszFile);
|
---|
62 | int AddDir(const char *pszDir);
|
---|
63 | int Close(void);
|
---|
64 | bool IsOpen(void) const;
|
---|
65 | int OpenEx(const char *pszPath, DNDURIDROPPEDFILEFLAGS fFlags = DNDURIDROPPEDFILE_FLAGS_NONE);
|
---|
66 | int OpenTemp(DNDURIDROPPEDFILEFLAGS fFlags = DNDURIDROPPEDFILE_FLAGS_NONE);
|
---|
67 | const char *GetDirAbs(void) const;
|
---|
68 | int Reopen(void);
|
---|
69 | int Reset(bool fDeleteContent);
|
---|
70 | int Rollback(void);
|
---|
71 |
|
---|
72 | protected:
|
---|
73 |
|
---|
74 | int closeInternal(void);
|
---|
75 |
|
---|
76 | protected:
|
---|
77 |
|
---|
78 | /** Open flags. */
|
---|
79 | uint32_t m_fOpen;
|
---|
80 | /** Directory handle for drop directory. */
|
---|
81 | RTDIR m_hDir;
|
---|
82 | /** Absolute path to drop directory. */
|
---|
83 | RTCString m_strPathAbs;
|
---|
84 | /** List for holding created directories in the case of a rollback. */
|
---|
85 | RTCList<RTCString> m_lstDirs;
|
---|
86 | /** List for holding created files in the case of a rollback. */
|
---|
87 | RTCList<RTCString> m_lstFiles;
|
---|
88 | };
|
---|
89 |
|
---|
90 | bool DnDMIMEHasFileURLs(const char *pcszFormat, size_t cchFormatMax);
|
---|
91 | bool DnDMIMENeedsDropDir(const char *pcszFormat, size_t cchFormatMax);
|
---|
92 |
|
---|
93 | int DnDPathSanitizeFilename(char *pszPath, size_t cbPath);
|
---|
94 | int DnDPathSanitize(char *pszPath, size_t cbPath);
|
---|
95 |
|
---|
96 | /** DnDURIObject flags. */
|
---|
97 | typedef uint32_t DNDURIOBJECTFLAGS;
|
---|
98 |
|
---|
99 | /** No flags specified. */
|
---|
100 | #define DNDURIOBJECT_FLAGS_NONE 0
|
---|
101 |
|
---|
102 | /** Mask of all valid DnD URI object flags. */
|
---|
103 | #define DNDURIOBJECT_FLAGS_VALID_MASK UINT32_C(0x0)
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Class for handling DnD URI objects.
|
---|
107 | * This class abstracts the access and handling objects when performing DnD actions.
|
---|
108 | */
|
---|
109 | class DnDURIObject
|
---|
110 | {
|
---|
111 | public:
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Enumeration for specifying an URI object type.
|
---|
115 | */
|
---|
116 | enum Type
|
---|
117 | {
|
---|
118 | /** Unknown type, do not use. */
|
---|
119 | Type_Unknown = 0,
|
---|
120 | /** Object is a file. */
|
---|
121 | Type_File,
|
---|
122 | /** Object is a directory. */
|
---|
123 | Type_Directory,
|
---|
124 | /** The usual 32-bit hack. */
|
---|
125 | Type_32Bit_Hack = 0x7fffffff
|
---|
126 | };
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Enumeration for specifying an URI object view
|
---|
130 | * for representing its data accordingly.
|
---|
131 | */
|
---|
132 | enum View
|
---|
133 | {
|
---|
134 | /** Unknown view, do not use. */
|
---|
135 | View_Unknown = 0,
|
---|
136 | /** Handle data from the source point of view. */
|
---|
137 | View_Source,
|
---|
138 | /** Handle data from the destination point of view. */
|
---|
139 | View_Target,
|
---|
140 | /** The usual 32-bit hack. */
|
---|
141 | View_Dest_32Bit_Hack = 0x7fffffff
|
---|
142 | };
|
---|
143 |
|
---|
144 | DnDURIObject(void);
|
---|
145 | DnDURIObject(Type type,
|
---|
146 | const RTCString &strSrcPathAbs = "",
|
---|
147 | const RTCString &strDstPathAbs = "");
|
---|
148 | virtual ~DnDURIObject(void);
|
---|
149 |
|
---|
150 | public:
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Returns the given absolute source path of the object.
|
---|
154 | *
|
---|
155 | * @return Absolute source path of the object.
|
---|
156 | */
|
---|
157 | const RTCString &GetSourcePathAbs(void) const { return m_strSrcPathAbs; }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Returns the given, absolute destination path of the object.
|
---|
161 | *
|
---|
162 | * @return Absolute destination path of the object.
|
---|
163 | */
|
---|
164 | const RTCString &GetDestPathAbs(void) const { return m_strTgtPathAbs; }
|
---|
165 |
|
---|
166 | RTFMODE GetMode(void) const;
|
---|
167 |
|
---|
168 | uint64_t GetProcessed(void) const;
|
---|
169 |
|
---|
170 | uint64_t GetSize(void) const;
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Returns the object's type.
|
---|
174 | *
|
---|
175 | * @return The object's type.
|
---|
176 | */
|
---|
177 | Type GetType(void) const { return m_enmType; }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Returns the object's view.
|
---|
181 | *
|
---|
182 | * @return The object's view.
|
---|
183 | */
|
---|
184 | View GetView(void) const { return m_enmView; }
|
---|
185 |
|
---|
186 | public:
|
---|
187 |
|
---|
188 | int SetSize(uint64_t cbSize);
|
---|
189 |
|
---|
190 | public:
|
---|
191 |
|
---|
192 | void Close(void);
|
---|
193 | bool IsComplete(void) const;
|
---|
194 | bool IsOpen(void) const;
|
---|
195 | int Open(View enmView, uint64_t fOpen, RTFMODE fMode = 0);
|
---|
196 | int OpenEx(const RTCString &strPath, View enmView, uint64_t fOpen = 0, RTFMODE fMode = 0, DNDURIOBJECTFLAGS = DNDURIOBJECT_FLAGS_NONE);
|
---|
197 | int QueryInfo(View enmView);
|
---|
198 | int Read(void *pvBuf, size_t cbBuf, uint32_t *pcbRead);
|
---|
199 | void Reset(void);
|
---|
200 | int Write(const void *pvBuf, size_t cbBuf, uint32_t *pcbWritten);
|
---|
201 |
|
---|
202 | public:
|
---|
203 |
|
---|
204 | static int RebaseURIPath(RTCString &strPath, const RTCString &strBaseOld = "", const RTCString &strBaseNew = "");
|
---|
205 |
|
---|
206 | protected:
|
---|
207 |
|
---|
208 | void closeInternal(void);
|
---|
209 | int queryInfoInternal(View enmView);
|
---|
210 |
|
---|
211 | protected:
|
---|
212 |
|
---|
213 | /** The object's type. */
|
---|
214 | Type m_enmType;
|
---|
215 | /** The object's view. */
|
---|
216 | View m_enmView;
|
---|
217 | /** Absolute path (base) for the source. */
|
---|
218 | RTCString m_strSrcPathAbs;
|
---|
219 | /** Absolute path (base) for the target. */
|
---|
220 | RTCString m_strTgtPathAbs;
|
---|
221 | /** Whether the object is in "opened" state. */
|
---|
222 | bool m_fIsOpen;
|
---|
223 |
|
---|
224 | /** Union containing data depending on the object's type. */
|
---|
225 | union
|
---|
226 | {
|
---|
227 | /** Structure containing members for objects that
|
---|
228 | * are files. */
|
---|
229 | struct
|
---|
230 | {
|
---|
231 | /** File handle. */
|
---|
232 | RTFILE hFile;
|
---|
233 | /** File system object information of this file. */
|
---|
234 | RTFSOBJINFO objInfo;
|
---|
235 | /** Bytes to proces for reading/writing. */
|
---|
236 | uint64_t cbToProcess;
|
---|
237 | /** Bytes processed reading/writing. */
|
---|
238 | uint64_t cbProcessed;
|
---|
239 | } File;
|
---|
240 | struct
|
---|
241 | {
|
---|
242 | /** Directory handle. */
|
---|
243 | RTDIR hDir;
|
---|
244 | /** File system object information of this directory. */
|
---|
245 | RTFSOBJINFO objInfo;
|
---|
246 | } Dir;
|
---|
247 | } u;
|
---|
248 | };
|
---|
249 |
|
---|
250 | /** DnDURIList flags. */
|
---|
251 | typedef uint32_t DNDURILISTFLAGS;
|
---|
252 |
|
---|
253 | /** No flags specified. */
|
---|
254 | #define DNDURILIST_FLAGS_NONE 0
|
---|
255 | /** Keep the original paths, don't convert paths to relative ones. */
|
---|
256 | #define DNDURILIST_FLAGS_ABSOLUTE_PATHS RT_BIT(0)
|
---|
257 | /** Resolve all symlinks. */
|
---|
258 | #define DNDURILIST_FLAGS_RESOLVE_SYMLINKS RT_BIT(1)
|
---|
259 | /** Keep the files + directory entries open while
|
---|
260 | * being in this list. */
|
---|
261 | #define DNDURILIST_FLAGS_KEEP_OPEN RT_BIT(2)
|
---|
262 | /** Lazy loading: Only enumerate sub directories when needed.
|
---|
263 | ** @todo Implement lazy loading. */
|
---|
264 | #define DNDURILIST_FLAGS_LAZY RT_BIT(3)
|
---|
265 |
|
---|
266 | /** Mask of all valid DnD URI list flags. */
|
---|
267 | #define DNDURILIST_FLAGS_VALID_MASK UINT32_C(0xF)
|
---|
268 |
|
---|
269 | class DnDURIList
|
---|
270 | {
|
---|
271 | public:
|
---|
272 |
|
---|
273 | DnDURIList(void);
|
---|
274 | virtual ~DnDURIList(void);
|
---|
275 |
|
---|
276 | public:
|
---|
277 |
|
---|
278 | int AppendNativePath(const char *pszPath, DNDURILISTFLAGS fFlags);
|
---|
279 | int AppendNativePathsFromList(const char *pszNativePaths, size_t cbNativePaths, DNDURILISTFLAGS fFlags);
|
---|
280 | int AppendNativePathsFromList(const RTCList<RTCString> &lstNativePaths, DNDURILISTFLAGS fFlags);
|
---|
281 | int AppendURIPath(const char *pszURI, DNDURILISTFLAGS fFlags);
|
---|
282 | int AppendURIPathsFromList(const char *pszURIPaths, size_t cbURIPaths, DNDURILISTFLAGS fFlags);
|
---|
283 | int AppendURIPathsFromList(const RTCList<RTCString> &lstURI, DNDURILISTFLAGS fFlags);
|
---|
284 |
|
---|
285 | void Clear(void);
|
---|
286 | DnDURIObject *First(void) { return m_lstTree.first(); }
|
---|
287 | bool IsEmpty(void) const { return m_lstTree.isEmpty(); }
|
---|
288 | void RemoveFirst(void);
|
---|
289 | int SetFromURIData(const void *pvData, size_t cbData, DNDURILISTFLAGS fFlags);
|
---|
290 |
|
---|
291 | RTCString GetRootEntries(const RTCString &strPathBase = "", const RTCString &strSeparator = "\r\n") const;
|
---|
292 | uint64_t GetRootCount(void) const { return m_lstRoot.size(); }
|
---|
293 | uint64_t GetTotalCount(void) const { return m_cTotal; }
|
---|
294 | uint64_t GetTotalBytes(void) const { return m_cbTotal; }
|
---|
295 |
|
---|
296 | protected:
|
---|
297 |
|
---|
298 | int addEntry(const char *pcszSource, const char *pcszTarget, DNDURILISTFLAGS fFlags);
|
---|
299 | int appendPathRecursive(const char *pcszSrcPath, const char *pcszDstPath, const char *pcszDstBase, size_t cchDstBase, DNDURILISTFLAGS fFlags);
|
---|
300 |
|
---|
301 | protected:
|
---|
302 |
|
---|
303 | /** List of all top-level file/directory entries.
|
---|
304 | * Note: All paths are kept internally as UNIX paths for
|
---|
305 | * easier conversion/handling! */
|
---|
306 | RTCList<RTCString> m_lstRoot;
|
---|
307 | /** List of all URI objects added. The list's content
|
---|
308 | * might vary depending on how the objects are being
|
---|
309 | * added (lazy or not). */
|
---|
310 | RTCList<DnDURIObject *> m_lstTree;
|
---|
311 | /** Total number of all URI objects. */
|
---|
312 | uint64_t m_cTotal;
|
---|
313 | /** Total size of all URI objects, that is, the file
|
---|
314 | * size of all objects (in bytes).
|
---|
315 | * Note: Do *not* size_t here, as we also want to support large files
|
---|
316 | * on 32-bit guests. */
|
---|
317 | uint64_t m_cbTotal;
|
---|
318 | };
|
---|
319 |
|
---|
320 | #endif /* !___VBox_GuestHost_DragAndDrop_h */
|
---|
321 |
|
---|