Changeset View
Changeset View
Standalone View
Standalone View
src/ioslaves/file/file_p.h
Show All 15 Lines | 1 | /*** | |||
---|---|---|---|---|---|
16 | 16 | | |||
17 | You should have received a copy of the GNU Lesser General Public | 17 | You should have received a copy of the GNU Lesser General Public | ||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | 18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | ***/ | 19 | ***/ | ||
20 | 20 | | |||
21 | #ifndef FILE_P_H | 21 | #ifndef FILE_P_H | ||
22 | #define FILE_P_H | 22 | #define FILE_P_H | ||
23 | 23 | | |||
24 | enum ActionType { | 24 | enum ActionType { | ||
dfaure: #define is for 1990, these days we have a proper C++ language where preprocessor hacks are less… | |||||
25 | CHMOD = 1, | 25 | CHMOD = 1, | ||
26 | CHOWN, | 26 | CHOWN, | ||
27 | DEL, | 27 | DEL, | ||
28 | MKDIR, | 28 | MKDIR, | ||
29 | OPEN, | 29 | OPEN, | ||
30 | OPENDIR, | 30 | OPENDIR, | ||
31 | RENAME, | 31 | RENAME, | ||
32 | RMDIR, | 32 | RMDIR, | ||
33 | SYMLINK, | 33 | SYMLINK, | ||
34 | UTIME, | 34 | UTIME, | ||
35 | }; | 35 | }; | ||
36 | 36 | | |||
37 | /** | ||||
38 | * PrivilegeOperationReturnValue encapsulates the return value from execWithElevatedPrivilege() in a convenient way. | ||||
39 | * Warning, this class will cast to a bool that is false on success and true on failure. This unusual solution allows | ||||
40 | * to write kioslave code like this: | ||||
41 | | ||||
42 | if (!dir.rmdir(itemPath)) { | ||||
43 | if (auto ret = execWithElevatedPrivilege(RMDIR, itemPath)) { | ||||
44 | if (!ret.wasCanceled()) { | ||||
45 | error(KIO::ERR_CANNOT_DELETE, itemPath); | ||||
46 | } | ||||
47 | return false; | ||||
48 | } | ||||
49 | } | ||||
50 | // directory successfully removed, continue with the next operation | ||||
51 | */ | ||||
52 | class PrivilegeOperationReturnValue | ||||
53 | { | ||||
54 | public: | ||||
55 | static PrivilegeOperationReturnValue success() { return PrivilegeOperationReturnValue{false, false}; } | ||||
56 | static PrivilegeOperationReturnValue failure() { return PrivilegeOperationReturnValue{true, false}; } | ||||
57 | static PrivilegeOperationReturnValue canceled() { return PrivilegeOperationReturnValue{true, true}; } | ||||
58 | operator bool() const { return m_failed; } | ||||
59 | bool wasCanceled() const { return m_canceled; } | ||||
60 | private: | ||||
61 | PrivilegeOperationReturnValue(bool failed, bool canceled) : m_failed(failed), m_canceled(canceled) {} | ||||
62 | const bool m_failed; | ||||
63 | const bool m_canceled; | ||||
64 | }; | ||||
65 | | ||||
66 | class QString; | ||||
67 | const QString socketPath(); | ||||
68 | | ||||
37 | #endif | 69 | #endif |
#define is for 1990, these days we have a proper C++ language where preprocessor hacks are less and less needed.
An enum value is probably the cleanest way here (to avoid the whole issue of "in which .cpp file to implement it", if it was an actual int variable).
The naming EFOO is very libc-like, I wouldn't use this here.
In fact, why not use KIO::ERR_USER_CANCELED? (note that it has a value of 1, don't use 1 for something else ;)