1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
---|---|
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "fileoperation.h" |
6 | #include "processutil.h" |
7 | #include "common.h" |
8 | |
9 | #include <QDialog> |
10 | #include <QDir> |
11 | #include <QFileInfo> |
12 | |
13 | const QString DELETE_MESSAGE_TEXT {QDialog::tr("The delete operation will be removed from" |
14 | "the disk and will not be recoverable " |
15 | "after this operation.\nDelete anyway?")}; |
16 | |
17 | const QString DELETE_WINDOW_TEXT {QDialog::tr("Delete Warning")}; |
18 | |
19 | bool FileOperation::doMoveMoveToTrash(const QString &filePath) |
20 | { |
21 | return ProcessUtil::moveToTrash(filePath); |
22 | } |
23 | |
24 | bool FileOperation::doRecoverFromTrash(const QString &filePath) |
25 | { |
26 | return ProcessUtil::recoverFromTrash(filePath); |
27 | } |
28 | |
29 | bool FileOperation::doRemove(const QString &filePath) |
30 | { |
31 | return QFile(filePath).remove(); |
32 | } |
33 | |
34 | bool FileOperation::doNewDocument(const QString &parentPath, const QString &docName) |
35 | { |
36 | QFileInfo info(parentPath); |
37 | if (!info.exists() || !info.isDir()) |
38 | return false; |
39 | else { |
40 | QFile file(parentPath + QDir::separator() + docName); |
41 | if (file.open(QFile::OpenModeFlag::NewOnly)){ |
42 | file.close(); |
43 | } |
44 | return false; |
45 | } |
46 | } |
47 | |
48 | bool FileOperation::doNewFolder(const QString &parentPath, const QString &folderName) |
49 | { |
50 | QFileInfo info(parentPath); |
51 | if (!info.exists() || !info.isDir()) |
52 | return false; |
53 | else |
54 | return QDir(parentPath).mkdir(folderName); |
55 | } |
56 | |
57 | bool FileOperation::deleteDir(const QString &path) |
58 | { |
59 | if (path.isEmpty()){ |
60 | return true; |
61 | } |
62 | |
63 | QDir dir(path); |
64 | if(!dir.exists()){ |
65 | return true; |
66 | } |
67 | |
68 | dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); |
69 | QFileInfoList fileList = dir.entryInfoList(); |
70 | foreach (auto file, fileList) { |
71 | if (file.isFile()) |
72 | file.dir().remove(file.fileName()); |
73 | else |
74 | deleteDir(file.absoluteFilePath()); |
75 | } |
76 | |
77 | return dir.rmdir(dir.absolutePath()); |
78 | } |
79 | |
80 | QString FileOperation::checkCreateDir(const QString &src, const QString &dirName) |
81 | { |
82 | QDir dir(src); |
83 | if (dir.exists()) { |
84 | if (!dir.exists(dirName)) { |
85 | dir.mkdir(dirName); |
86 | } |
87 | dir.cd(dirName); |
88 | } |
89 | return dir.path(); |
90 | } |
91 | |
92 | QString FileOperation::readAll(const QString &filePath) |
93 | { |
94 | QFile file(filePath); |
95 | if (!file.open(QFile::ReadOnly)) { |
96 | qCritical() << file.errorString(); |
97 | return ""; |
98 | } |
99 | |
100 | QString ret = file.readAll(); |
101 | file.close(); |
102 | return ret; |
103 | } |
104 | |
105 | bool FileOperation::exists(const QString &filePath) |
106 | { |
107 | return QFileInfo(filePath).exists(); |
108 | } |
109 |