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
121 : errorFieldUsed(CodeId)
122 , codeId(ImportExportCodes::Failure)
123 , cannotRead()
124 , cannotWrite()
125 , failureMessage(message)
126{ }
127
129{
130 // if cannotRead or cannotWrite is "NoError", it means that something is wrong in our code
132}
133
138
143
145{
146 QString internal = i18n("Unexpected error.");
147 if (errorFieldUsed == CannotRead) {
148 return cannotRead.errorMessage();
149 } else if (errorFieldUsed == CannotWrite) {
150 return cannotWrite.errorMessage();
151 } else if (errorFieldUsed == CodeId) {
152 switch (codeId) {
153 // Reading
155 return i18n("The file doesn't exist.");
157 return i18n("Permission denied: Krita is not allowed to read the file.");
159 return i18n("The file format cannot be parsed.");
161 return i18n("The file format contains unsupported features.");
163 return i18n("The file format contains unsupported color space.");
165 return i18n("Error occurred while reading from the file.");
166
167 // Writing
169 return i18n("The file cannot be created.");
171 return i18n("Permission denied: Krita is not allowed to write to the file.");
173 return i18n("There is not enough disk space left to save the file.");
175 return i18n("Error occurred while writing to the file.");
177 return i18n("Krita does not support this file format.");
178
179 // Both
181 return i18n("The action was cancelled by the user.");
182
183 // Other
185 return failureMessage.isEmpty() ? i18n("Unknown error.") : failureMessage;
187 return internal;
189 return i18n("Image is busy.");
190 // OK
192 return i18n("The action has been completed successfully.");
193 default:
194 return internal;
195
196 }
197 }
198 return internal; // errorFieldUsed = None
199}
200
201
202
204{
205 if (errorFieldUsed != errorCode.errorFieldUsed) {
206 return false;
207 }
208 if (errorFieldUsed == CodeId) {
209 return codeId == errorCode.codeId;
210 }
211 if (errorFieldUsed == CannotRead) {
212 return cannotRead == errorCode.cannotRead;
213 }
214 return cannotWrite == errorCode.cannotWrite;
215}
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)