Merge pull request #26166 from ksooo/improve-plugin-ctx-menus
[xbmc.git] / xbmc / utils / FileOperationJob.h
blob5dad5403bd8d68c91e337b2c9e5fa27769fefa2c
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "FileItem.h"
12 #include "FileItemList.h"
13 #include "filesystem/IFileTypes.h"
14 #include "utils/ProgressJob.h"
16 #include <string>
17 #include <vector>
19 class CFileOperationJob : public CProgressJob
21 public:
22 enum FileAction
24 ActionCopy = 1,
25 ActionMove,
26 ActionDelete,
27 ActionReplace, ///< Copy, emptying any existing destination directories first
28 ActionCreateFolder,
29 ActionDeleteFolder,
32 CFileOperationJob();
33 CFileOperationJob(FileAction action, CFileItemList & items,
34 const std::string& strDestFile,
35 bool displayProgress = false,
36 int errorHeading = 0, int errorLine = 0);
38 static std::string GetActionString(FileAction action);
40 // implementations of CJob
41 bool DoWork() override;
42 const char* GetType() const override { return m_displayProgress ? "filemanager" : ""; }
43 bool operator==(const CJob *job) const override;
45 void SetFileOperation(FileAction action,
46 const CFileItemList& items,
47 const std::string& strDestFile);
49 const std::string &GetAverageSpeed() const { return m_avgSpeed; }
50 const std::string &GetCurrentOperation() const { return m_currentOperation; }
51 const std::string &GetCurrentFile() const { return m_currentFile; }
52 const CFileItemList &GetItems() const { return m_items; }
53 FileAction GetAction() const { return m_action; }
54 int GetHeading() const { return m_heading; }
55 int GetLine() const { return m_line; }
57 private:
58 class CFileOperation : public XFILE::IFileCallback
60 public:
61 CFileOperation(FileAction action, const std::string &strFileA, const std::string &strFileB, int64_t time);
63 bool OnFileCallback(void* pContext, int ipercent, float avgSpeed) override;
65 bool ExecuteOperation(CFileOperationJob *base, double &current, double opWeight);
67 private:
68 FileAction m_action;
69 std::string m_strFileA, m_strFileB;
70 int64_t m_time;
72 friend class CFileOperation;
74 typedef std::vector<CFileOperation> FileOperationList;
75 bool DoProcess(FileAction action,
76 const CFileItemList& items,
77 const std::string& strDestFile,
78 FileOperationList& fileOperations,
79 double& totalTime);
80 bool DoProcessFolder(FileAction action, const std::string& strPath, const std::string& strDestFile, FileOperationList &fileOperations, double &totalTime);
81 bool DoProcessFile(FileAction action, const std::string& strFileA, const std::string& strFileB, FileOperationList &fileOperations, double &totalTime);
83 static inline bool CanBeRenamed(const std::string &strFileA, const std::string &strFileB);
85 FileAction m_action = ActionCopy;
86 CFileItemList m_items;
87 std::string m_strDestFile;
88 std::string m_avgSpeed, m_currentOperation, m_currentFile;
89 bool m_displayProgress = false;
90 int m_heading = 0;
91 int m_line = 0;