1 | /* $Id: DragAndDrop.h 74526 2018-09-28 15:08:24Z 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/cdefs.h>
|
---|
32 | #include <iprt/dir.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/file.h>
|
---|
35 | #include <iprt/types.h>
|
---|
36 |
|
---|
37 | #include <iprt/cpp/list.h>
|
---|
38 | #include <iprt/cpp/ministring.h>
|
---|
39 |
|
---|
40 | /** DnDURIDroppedFiles flags. */
|
---|
41 | typedef uint32_t DNDURIDROPPEDFILEFLAGS;
|
---|
42 |
|
---|
43 | /** No flags specified. */
|
---|
44 | #define DNDURIDROPPEDFILE_FLAGS_NONE 0
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Class for maintaining a "dropped files" directory
|
---|
48 | * on the host or guest. This will contain all received files & directories
|
---|
49 | * for a single drag and drop operation.
|
---|
50 | *
|
---|
51 | * In case of a failed drag and drop operation this class can also
|
---|
52 | * perform a gentle rollback if required.
|
---|
53 | */
|
---|
54 | class DnDDroppedFiles
|
---|
55 | {
|
---|
56 |
|
---|
57 | public:
|
---|
58 |
|
---|
59 | DnDDroppedFiles(void);
|
---|
60 | DnDDroppedFiles(const char *pszPath, DNDURIDROPPEDFILEFLAGS fFlags = DNDURIDROPPEDFILE_FLAGS_NONE);
|
---|
61 | virtual ~DnDDroppedFiles(void);
|
---|
62 |
|
---|
63 | public:
|
---|
64 |
|
---|
65 | int AddFile(const char *pszFile);
|
---|
66 | int AddDir(const char *pszDir);
|
---|
67 | int Close(void);
|
---|
68 | bool IsOpen(void) const;
|
---|
69 | int OpenEx(const char *pszPath, DNDURIDROPPEDFILEFLAGS fFlags = DNDURIDROPPEDFILE_FLAGS_NONE);
|
---|
70 | int OpenTemp(DNDURIDROPPEDFILEFLAGS fFlags = DNDURIDROPPEDFILE_FLAGS_NONE);
|
---|
71 | const char *GetDirAbs(void) const;
|
---|
72 | int Reopen(void);
|
---|
73 | int Reset(bool fDeleteContent);
|
---|
74 | int Rollback(void);
|
---|
75 |
|
---|
76 | protected:
|
---|
77 |
|
---|
78 | int closeInternal(void);
|
---|
79 |
|
---|
80 | protected:
|
---|
81 |
|
---|
82 | /** Open flags. */
|
---|
83 | uint32_t m_fOpen;
|
---|
84 | /** Directory handle for drop directory. */
|
---|
85 | RTDIR m_hDir;
|
---|
86 | /** Absolute path to drop directory. */
|
---|
87 | RTCString m_strPathAbs;
|
---|
88 | /** List for holding created directories in the case of a rollback. */
|
---|
89 | RTCList<RTCString> m_lstDirs;
|
---|
90 | /** List for holding created files in the case of a rollback. */
|
---|
91 | RTCList<RTCString> m_lstFiles;
|
---|
92 | };
|
---|
93 |
|
---|
94 | bool DnDMIMEHasFileURLs(const char *pcszFormat, size_t cchFormatMax);
|
---|
95 | bool DnDMIMENeedsDropDir(const char *pcszFormat, size_t cchFormatMax);
|
---|
96 |
|
---|
97 | int DnDPathSanitizeFilename(char *pszPath, size_t cbPath);
|
---|
98 | int DnDPathSanitize(char *pszPath, size_t cbPath);
|
---|
99 |
|
---|
100 | /** DnDURIObject flags. */
|
---|
101 | typedef uint32_t DNDURIOBJECTFLAGS;
|
---|
102 |
|
---|
103 | /** No flags specified. */
|
---|
104 | #define DNDURIOBJECT_FLAGS_NONE 0
|
---|
105 |
|
---|
106 | class DnDURIObject
|
---|
107 | {
|
---|
108 | public:
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Enumeration for specifying an URI object type.
|
---|
112 | */
|
---|
113 | enum Type
|
---|
114 | {
|
---|
115 | /** Unknown type, do not use. */
|
---|
116 | Type_Unknown = 0,
|
---|
117 | /** Object is a file. */
|
---|
118 | Type_File,
|
---|
119 | /** Object is a directory. */
|
---|
120 | Type_Directory,
|
---|
121 | /** The usual 32-bit hack. */
|
---|
122 | Type_32Bit_Hack = 0x7fffffff
|
---|
123 | };
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Enumeration for specifying an URI object view
|
---|
127 | * represent its data accordingly.
|
---|
128 | */
|
---|
129 | enum View
|
---|
130 | {
|
---|
131 | /** Unknown view, do not use. */
|
---|
132 | View_Unknown = 0,
|
---|
133 | /** Handle data from the source point of view. */
|
---|
134 | View_Source,
|
---|
135 | /** Handle data from the destination point of view. */
|
---|
136 | View_Target,
|
---|
137 | /** The usual 32-bit hack. */
|
---|
138 | View_Dest_32Bit_Hack = 0x7fffffff
|
---|
139 | };
|
---|
140 |
|
---|
141 | DnDURIObject(void);
|
---|
142 | DnDURIObject(Type type,
|
---|
143 | const RTCString &strSrcPath = "",
|
---|
144 | const RTCString &strDstPath = "",
|
---|
145 | uint32_t fMode = 0, uint64_t cbSize = 0);
|
---|
146 | virtual ~DnDURIObject(void);
|
---|
147 |
|
---|
148 | public:
|
---|
149 |
|
---|
150 | const RTCString &GetSourcePath(void) const { return m_strSrcPath; }
|
---|
151 | const RTCString &GetDestPath(void) const { return m_strTgtPath; }
|
---|
152 | uint32_t GetMode(void) const { return m_fMode; }
|
---|
153 | uint64_t GetProcessed(void) const { return m_cbProcessed; }
|
---|
154 | uint64_t GetSize(void) const { return m_cbSize; }
|
---|
155 | Type GetType(void) const { return m_Type; }
|
---|
156 |
|
---|
157 | public:
|
---|
158 |
|
---|
159 | int SetSize(uint64_t uSize) { m_cbSize = uSize; return VINF_SUCCESS; }
|
---|
160 |
|
---|
161 | public:
|
---|
162 |
|
---|
163 | void Close(void);
|
---|
164 | bool IsComplete(void) const;
|
---|
165 | bool IsOpen(void) const;
|
---|
166 | int Open(View enmView, uint64_t fOpen, uint32_t fMode = 0);
|
---|
167 | int OpenEx(const RTCString &strPath, Type enmType, View enmView, uint64_t fOpen = 0, uint32_t fMode = 0, DNDURIOBJECTFLAGS = DNDURIOBJECT_FLAGS_NONE);
|
---|
168 | int Read(void *pvBuf, size_t cbBuf, uint32_t *pcbRead);
|
---|
169 | void Reset(void);
|
---|
170 | int Write(const void *pvBuf, size_t cbBuf, uint32_t *pcbWritten);
|
---|
171 |
|
---|
172 | public:
|
---|
173 |
|
---|
174 | static int RebaseURIPath(RTCString &strPath, const RTCString &strBaseOld = "", const RTCString &strBaseNew = "");
|
---|
175 |
|
---|
176 | protected:
|
---|
177 |
|
---|
178 | void closeInternal(void);
|
---|
179 |
|
---|
180 | protected:
|
---|
181 |
|
---|
182 | Type m_Type;
|
---|
183 | RTCString m_strSrcPath;
|
---|
184 | RTCString m_strTgtPath;
|
---|
185 | /** Whether the object is in "opened" state. */
|
---|
186 | bool m_fOpen;
|
---|
187 | /** Object (file/directory) mode. */
|
---|
188 | uint32_t m_fMode;
|
---|
189 | /** Size (in bytes) to read/write. */
|
---|
190 | uint64_t m_cbSize;
|
---|
191 | /** Bytes processed reading/writing. */
|
---|
192 | uint64_t m_cbProcessed;
|
---|
193 |
|
---|
194 | union
|
---|
195 | {
|
---|
196 | RTFILE m_hFile;
|
---|
197 | } u;
|
---|
198 | };
|
---|
199 |
|
---|
200 | /** DnDURIList flags. */
|
---|
201 | typedef uint32_t DNDURILISTFLAGS;
|
---|
202 |
|
---|
203 | /** No flags specified. */
|
---|
204 | #define DNDURILIST_FLAGS_NONE 0
|
---|
205 | /** Keep the original paths, don't convert paths to relative ones. */
|
---|
206 | #define DNDURILIST_FLAGS_ABSOLUTE_PATHS RT_BIT(0)
|
---|
207 | /** Resolve all symlinks. */
|
---|
208 | #define DNDURILIST_FLAGS_RESOLVE_SYMLINKS RT_BIT(1)
|
---|
209 | /** Keep the files + directory entries open while
|
---|
210 | * being in this list. */
|
---|
211 | #define DNDURILIST_FLAGS_KEEP_OPEN RT_BIT(2)
|
---|
212 | /** Lazy loading: Only enumerate sub directories when needed.
|
---|
213 | ** @todo Implement lazy loading. */
|
---|
214 | #define DNDURILIST_FLAGS_LAZY RT_BIT(3)
|
---|
215 |
|
---|
216 | class DnDURIList
|
---|
217 | {
|
---|
218 | public:
|
---|
219 |
|
---|
220 | DnDURIList(void);
|
---|
221 | virtual ~DnDURIList(void);
|
---|
222 |
|
---|
223 | public:
|
---|
224 |
|
---|
225 | int AppendNativePath(const char *pszPath, DNDURILISTFLAGS fFlags);
|
---|
226 | int AppendNativePathsFromList(const char *pszNativePaths, size_t cbNativePaths, DNDURILISTFLAGS fFlags);
|
---|
227 | int AppendNativePathsFromList(const RTCList<RTCString> &lstNativePaths, DNDURILISTFLAGS fFlags);
|
---|
228 | int AppendURIPath(const char *pszURI, DNDURILISTFLAGS fFlags);
|
---|
229 | int AppendURIPathsFromList(const char *pszURIPaths, size_t cbURIPaths, DNDURILISTFLAGS fFlags);
|
---|
230 | int AppendURIPathsFromList(const RTCList<RTCString> &lstURI, DNDURILISTFLAGS fFlags);
|
---|
231 |
|
---|
232 | void Clear(void);
|
---|
233 | DnDURIObject *First(void) { return m_lstTree.first(); }
|
---|
234 | bool IsEmpty(void) const { return m_lstTree.isEmpty(); }
|
---|
235 | void RemoveFirst(void);
|
---|
236 | int SetFromURIData(const void *pvData, size_t cbData, DNDURILISTFLAGS fFlags);
|
---|
237 |
|
---|
238 | RTCString GetRootEntries(const RTCString &strPathBase = "", const RTCString &strSeparator = "\r\n") const;
|
---|
239 | uint64_t GetRootCount(void) const { return m_lstRoot.size(); }
|
---|
240 | uint64_t GetTotalCount(void) const { return m_cTotal; }
|
---|
241 | uint64_t GetTotalBytes(void) const { return m_cbTotal; }
|
---|
242 |
|
---|
243 | protected:
|
---|
244 |
|
---|
245 | int addEntry(const char *pcszSource, const char *pcszTarget, DNDURILISTFLAGS fFlags);
|
---|
246 | int appendPathRecursive(const char *pcszSrcPath, const char *pcszDstPath, const char *pcszDstBase, size_t cchDstBase, DNDURILISTFLAGS fFlags);
|
---|
247 |
|
---|
248 | protected:
|
---|
249 |
|
---|
250 | /** List of all top-level file/directory entries.
|
---|
251 | * Note: All paths are kept internally as UNIX paths for
|
---|
252 | * easier conversion/handling! */
|
---|
253 | RTCList<RTCString> m_lstRoot;
|
---|
254 | /** List of all URI objects added. The list's content
|
---|
255 | * might vary depending on how the objects are being
|
---|
256 | * added (lazy or not). */
|
---|
257 | RTCList<DnDURIObject *> m_lstTree;
|
---|
258 | /** Total number of all URI objects. */
|
---|
259 | uint64_t m_cTotal;
|
---|
260 | /** Total size of all URI objects, that is, the file
|
---|
261 | * size of all objects (in bytes).
|
---|
262 | * Note: Do *not* size_t here, as we also want to support large files
|
---|
263 | * on 32-bit guests. */
|
---|
264 | uint64_t m_cbTotal;
|
---|
265 | };
|
---|
266 |
|
---|
267 | #endif /* !___VBox_GuestHost_DragAndDrop_h */
|
---|
268 |
|
---|