Krita Source Code Documentation
Loading...
Searching...
No Matches
KisImportExportErrorCode.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2019 Agata Cacko <cacko.azh@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8#include <KLocalizedString>
9#include <kis_assert.h>
10
11
12
13KisImportExportComplexError::KisImportExportComplexError(QFileDevice::FileError error) : m_error(error) { }
14
15
17{
18 // Error descriptions in most cases taken from https://doc.qt.io/qt-5/qfiledevice.html
19 QString unspecifiedError = i18n("An unspecified error occurred.");
20 switch (m_error) {
21 case QFileDevice::FileError::NoError :
22 // Returning this file error may mean that something is wrong in our code.
23 // Successful operation should return ImportExportCodes::OK instead.
24 return i18n("The action has been completed successfully.");
25 case QFileDevice::FileError::ReadError :
26 return i18n("An error occurred when reading from the file.");
27 case QFileDevice::FileError::WriteError :
28 return i18n("An error occurred when writing to the file.");
29 case QFileDevice::FileError::FatalError :
30 return i18n("A fatal error occurred.");
31 case QFileDevice::FileError::ResourceError :
32 return i18n("Out of resources (e.g. out of memory).");
33 case QFileDevice::FileError::OpenError :
34 return i18n("The file could not be opened.");
35 case QFileDevice::FileError::AbortError :
36 return i18n("The operation was aborted.");
37 case QFileDevice::FileError::TimeOutError :
38 return i18n("A timeout occurred.");
39 case QFileDevice::FileError::UnspecifiedError :
40 return unspecifiedError;
41 case QFileDevice::FileError::RemoveError :
42 return i18n("The file could not be removed.");
43 case QFileDevice::FileError::RenameError :
44 return i18n("The file could not be renamed.");
45 case QFileDevice::FileError::PositionError :
46 return i18n("The position in the file could not be changed.");
47 case QFileDevice::FileError::ResizeError :
48 return i18n("The file could not be resized.");
49 case QFileDevice::FileError::PermissionsError :
50 return i18n("Permission denied. Krita is not allowed to read or write to the file.");
51 case QFileDevice::FileError::CopyError :
52 return i18n("The file could not be copied.");
53 }
54 return unspecifiedError;
55}
56
58
60 KIS_SAFE_ASSERT_RECOVER(error != QFileDevice::NoError) {m_error = QFileDevice::ReadError; }
61}
62
64{
65 return i18n("Cannot open file for reading. Reason: %1", qtErrorMessage());
66}
67
72
74
76 KIS_SAFE_ASSERT_RECOVER(error != QFileDevice::NoError) {m_error = QFileDevice::WriteError; }
77}
78
80{
81 return i18n("Cannot open file for writing. Reason: %1", qtErrorMessage());
82}
83
88
89
90
91
92
94 : errorFieldUsed(None)
95 , cannotRead()
96 , cannotWrite()
97{ }
98
100 : errorFieldUsed(CodeId)
101 , codeId(id)
102 , cannotRead()
103 , cannotWrite()
104{ }
105
107 : errorFieldUsed(CannotRead)
108 , codeId(ImportExportCodes::Failure)
109 , cannotRead(error)
110 , cannotWrite()
111{ }
112
114 : errorFieldUsed(CannotWrite)
115 , codeId(ImportExportCodes::Failure)
116 , cannotRead()
117 , cannotWrite(error)
118{ }
119
120
122{
123 // if cannotRead or cannotWrite is "NoError", it means that something is wrong in our code
125}
126
131
136
138{
139 QString internal = i18n("Unexpected error.");
140 if (errorFieldUsed == CannotRead) {
141 return cannotRead.errorMessage();
142 } else if (errorFieldUsed == CannotWrite) {
143 return cannotWrite.errorMessage();
144 } else if (errorFieldUsed == CodeId) {
145 switch (codeId) {
146 // Reading
148 return i18n("The file doesn't exist.");
150 return i18n("Permission denied: Krita is not allowed to read the file.");
152 return i18n("The file format cannot be parsed.");
154 return i18n("The file format contains unsupported features.");
156 return i18n("The file format contains unsupported color space.");
158 return i18n("Error occurred while reading from the file.");
159
160 // Writing
162 return i18n("The file cannot be created.");
164 return i18n("Permission denied: Krita is not allowed to write to the file.");
166 return i18n("There is not enough disk space left to save the file.");
168 return i18n("Error occurred while writing to the file.");
170 return i18n("Krita does not support this file format.");
171
172 // Both
174 return i18n("The action was cancelled by the user.");
175
176 // Other
178 return i18n("Unknown error.");
180 return internal;
182 return i18n("Image is busy.");
183 // OK
185 return i18n("The action has been completed successfully.");
186 default:
187 return internal;
188
189 }
190 }
191 return internal; // errorFieldUsed = None
192}
193
194
195
197{
198 if (errorFieldUsed != errorCode.errorFieldUsed) {
199 return false;
200 }
201 if (errorFieldUsed == CodeId) {
202 return codeId == errorCode.codeId;
203 }
204 if (errorFieldUsed == CannotRead) {
205 return cannotRead == errorCode.cannotRead;
206 }
207 return cannotWrite == errorCode.cannotWrite;
208}
KisImportExportErrorCannotRead cannotRead
ImportExportCodes::ErrorCodeID codeId
KisImportExportErrorCannotWrite cannotWrite
bool operator==(KisImportExportErrorCode errorCode)
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126
KisImportExportComplexError(QFileDevice::FileError error)
bool operator==(KisImportExportErrorCannotRead other)
bool operator==(KisImportExportErrorCannotWrite other)