File indexing completed on 2024-05-12 16:01:49

0001 /*
0002  *  SPDX-FileCopyrightText: 2019 Agata Cacko <cacko.azh@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef KIS_IMPORT_EXPORT_ERROR_CODES_H
0007 #define KIS_IMPORT_EXPORT_ERROR_CODES_H
0008 
0009 
0010 #include <QFile>
0011 #include <QFileDevice>
0012 #include <QString>
0013 #include <kritaui_export.h>
0014 #include <QDebug>
0015 
0016 
0017 namespace ImportExportCodes
0018 {
0019 
0020     enum ErrorCodeID
0021     {
0022         InternalError, // error that shouldn't happen; only inside ASSERTS
0023 
0024         // Reading
0025         FileNotExist, // there is no file with that name in that location,
0026         NoAccessToRead, // Krita has no reading access to the file,
0027         ErrorWhileReading, // there was an error that occurred during reading,
0028         FileFormatIncorrect, // file format cannot be parsed,
0029         FormatFeaturesUnsupported, // file format can be parsed, but some features are unsupported,
0030         FormatColorSpaceUnsupported, // file format can be parsed, but color space of the image is unsupported
0031 
0032         // Writing
0033         CannotCreateFile, // file cannot be created
0034         NoAccessToWrite, // Krita has no writing access to the file
0035         ErrorWhileWriting, // there was an error that occurred during writing (can be insufficient memory, too, just we don't know)
0036         InsufficientMemory, // there is not enough memory left
0037         FileFormatNotSupported, // this file format is not supported by Krita
0038 
0039         // Both
0040         Cancelled, // cancelled by a user
0041 
0042         // Other
0043         Failure, // unspecified error
0044 
0045         // Could not save because a save operation was already going on
0046         Busy,
0047 
0048         // OK
0049         OK, // everything went ok
0050 
0051     };
0052 
0053 };
0054 
0055 class KisImportExportErrorCode;
0056 
0057 struct KRITAUI_EXPORT KisImportExportComplexError
0058 {
0059     virtual QString errorMessage() const = 0;
0060     KisImportExportComplexError(QFileDevice::FileError error);
0061 
0062     friend inline QDebug &operator<<(QDebug &d,
0063                                      const KisImportExportErrorCode &errorCode);
0064     virtual ~KisImportExportComplexError() = default;
0065 
0066 protected:
0067     QString qtErrorMessage() const;
0068     QFileDevice::FileError m_error;
0069 };
0070 
0071 struct KRITAUI_EXPORT KisImportExportErrorCannotWrite : KisImportExportComplexError
0072 {
0073 
0074     KisImportExportErrorCannotWrite(QFileDevice::FileError error);
0075     QString errorMessage() const override;
0076 
0077     ~KisImportExportErrorCannotWrite() override = default;
0078 
0079     bool operator==(KisImportExportErrorCannotWrite other);
0080 
0081 private:
0082     KisImportExportErrorCannotWrite();
0083 
0084     //friend KisImportExportErrorCode::KisImportExportErrorCode(KisImportExportErrorCannotWrite code);
0085     friend class KisImportExportErrorCode;
0086 
0087 };
0088 
0089 struct KRITAUI_EXPORT KisImportExportErrorCannotRead : KisImportExportComplexError
0090 {
0091 
0092     KisImportExportErrorCannotRead(QFileDevice::FileError error);
0093     QString errorMessage() const override;
0094 
0095     ~KisImportExportErrorCannotRead() override = default;
0096 
0097     bool operator==(KisImportExportErrorCannotRead other);
0098 
0099 private:
0100     KisImportExportErrorCannotRead();
0101 
0102     //friend KisImportExportErrorCode::KisImportExportErrorCode(KisImportExportErrorCannotRead code);
0103     friend class KisImportExportErrorCode;
0104 
0105 };
0106 
0107 
0108 
0109 class KRITAUI_EXPORT KisImportExportErrorCode
0110 {
0111 public:
0112     // required by kis_async_action_feedback
0113     KisImportExportErrorCode();
0114 
0115     KisImportExportErrorCode(ImportExportCodes::ErrorCodeID code);
0116     KisImportExportErrorCode(KisImportExportErrorCannotRead code);
0117     KisImportExportErrorCode(KisImportExportErrorCannotWrite code);
0118 
0119     QString errorMessage() const;
0120     bool isOk() const;
0121     bool isCancelled() const;
0122     bool isInternalError() const;
0123 
0124     friend inline QDebug &operator<<(QDebug &d,
0125                                      const KisImportExportErrorCode &errorCode);
0126 
0127     bool operator==(KisImportExportErrorCode errorCode);
0128 
0129 private:
0130     enum ErrorFieldUsed
0131     {
0132         None,
0133         CodeId,
0134         CannotRead,
0135         CannotWrite
0136     };
0137 
0138     ErrorFieldUsed errorFieldUsed;
0139 
0140     ImportExportCodes::ErrorCodeID codeId {ImportExportCodes::OK};
0141     KisImportExportErrorCannotRead cannotRead;
0142     KisImportExportErrorCannotWrite cannotWrite;
0143 };
0144 
0145 inline QDebug &operator<<(QDebug &d, const KisImportExportErrorCode &errorCode)
0146 {
0147     switch (errorCode.errorFieldUsed) {
0148     case KisImportExportErrorCode::None:
0149         d << "None of the error fields is in use.";
0150         break;
0151     case KisImportExportErrorCode::CannotRead:
0152         d << "Cannot read: " << errorCode.cannotRead.m_error;
0153         break;
0154     case KisImportExportErrorCode::CannotWrite:
0155         d << "Cannot write: " << errorCode.cannotRead.m_error;
0156         break;
0157     case KisImportExportErrorCode::CodeId:
0158         d << "Error code = " << errorCode.codeId;
0159     }
0160     d << " " << errorCode.errorMessage();
0161     return d;
0162 }
0163 
0164 #endif // KIS_IMPORT_EXPORT_ERROR_CODES_H