File indexing completed on 2024-04-21 03:48:20

0001 /*
0002  * Vocabulary Document for KDE Edu
0003  * SPDX-FileCopyrightText: 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
0004  * SPDX-FileCopyrightText: 2005, 2007 Peter Hedlund <peter.hedlund@kdemail.net>
0005  * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 #ifndef KEDUVOCDOCUMENT_H
0009 #define KEDUVOCDOCUMENT_H
0010 
0011 #include "keduvocdocument_export.h"
0012 
0013 #include "keduvocarticle.h"
0014 #include "keduvocconjugation.h"
0015 #include "keduvocidentifier.h"
0016 
0017 #include <QList>
0018 #include <QMap>
0019 #include <QObject>
0020 #include <QUrl>
0021 
0022 class KEduVocLesson;
0023 class KEduVocWordType;
0024 class KEduVocLeitnerBox;
0025 
0026 /**
0027  * @brief The primary entry point to the hierarchy of objects describing vocabularies.
0028  * @details This class contains the expressions of your vocabulary
0029  * as well as other information about the vocabulary.
0030  */
0031 class KEDUVOCDOCUMENT_EXPORT KEduVocDocument : public QObject
0032 {
0033     Q_OBJECT
0034 public:
0035     ///@todo in new API change enums to QFlags
0036 
0037     /// known vocabulary file types
0038     enum FileType {
0039         KvdNone, ///< handles nothing
0040         Automatic, ///< try to automatically determine file type
0041         Kvtml, ///< Kvtml 2.0
0042         Wql, ///< Word Quiz format
0043         Pauker, ///< Pauker format
0044         Vokabeln, ///< Vokabeln format
0045         Xdxf, ///< XDXF format @see https://github.com/soshial/xdxf_makedict/blob/master/format_standard/xdxf_strict.dtd
0046         Csv, ///< Command separated values
0047         Kvtml1 ///< KVTML 1.0
0048     };
0049 
0050     /// the return code when opening/saving
0051     enum ErrorCode {
0052         NoError = 0, ///< no error
0053         Unknown, ///< unspecified error
0054         InvalidXml, ///< malformed xml or bad file formatting
0055         FileTypeUnknown, ///< unknown file type
0056         FileCannotWrite, ///< unwritable file
0057         FileWriterFailed, ///< file writer failed
0058         FileCannotRead, ///< unreadable file
0059         FileReaderFailed, ///< file reader failed
0060         FileDoesNotExist, ///< unknown file type
0061         FileLocked, ///< An autosave file exists for this document
0062         FileCannotLock, ///< Can't create an autosave file for this document
0063         FileIsReadOnly, ///< Can't save this file because it was opened read-only
0064     };
0065 
0066     /// indicates file open/save status locking or readonly
0067     enum FileHandlingFlags {
0068         FileDefaultHandling = 0x0, ///< Default status
0069         FileIgnoreLock = 0x1, ///< Ignore the file lock
0070         FileOpenReadOnly = 0x2, ///< Open without any intention to change and save back later. This also implies FileIgnoreLock.
0071     };
0072 
0073     /// used as parameter for pattern
0074     enum FileDialogMode {
0075         Reading,
0076         Writing,
0077     };
0078 
0079     /// delete only empty lessons or also if they have entries
0080     enum LessonDeletion {
0081         DeleteEmptyLesson,
0082         DeleteEntriesAndLesson,
0083     };
0084 
0085     /**
0086      * Constructor for a KDEEdu vocabulary document
0087      *
0088      * @param parent calling object
0089      */
0090     explicit KEduVocDocument(QObject *parent = nullptr);
0091 
0092     /**
0093      * Destructor
0094      */
0095     ~KEduVocDocument() override;
0096 
0097     /** @name Whole Document Methods
0098      * Methods related to saving/loading and locating the document
0099      * @nosubgrouping
0100      */
0101     ///@{
0102 
0103     /**
0104      * Opens and locks a document file
0105      *
0106      * Note: This is intended to be preserve binary compatible with int open(const QUrl&)
0107      *       When the API increments the major version number, then merge them
0108      *
0109      * @param url      url to file to open
0110      * @param flags How to handle expected unusual conditions (i.e. locking)
0111      * @returns        ErrorCode
0112      */
0113     ErrorCode open(const QUrl &url, FileHandlingFlags flags = FileDefaultHandling);
0114 
0115     /**
0116      * Close a document file and release the lock on the file
0117      *
0118      */
0119     void close();
0120 
0121     /**
0122      * Saves the data under the given name
0123      *
0124      * @pre generator is set.
0125      *
0126      * Note: This is intended to be preserve binary compatible with
0127      *       int saveAs(const QUrl&, FileType ft, const QString & generator );
0128      *       When the API increments the major version number, then merge them
0129      *
0130      * @param url        if url is empty (or NULL) actual name is preserved
0131      * @param ft         the filetype to be used when saving the document
0132      * @param flags How to handle expected unusual conditions (i.e. locking)
0133      * @returns          ErrorCode
0134      */
0135     ErrorCode saveAs(const QUrl &url, FileType ft, FileHandlingFlags flags = FileDefaultHandling);
0136 
0137     /** @details  returns a QByteArray KVTML2 representation of this doc
0138      * @param generator name of the application creating the QByteArray
0139      * @return KVTML2 XML
0140      * @todo in new API if this should be part of save*/
0141     QByteArray toByteArray(const QString &generator);
0142 
0143     /**
0144      * Merges data from another document (UNIMPLEMENTED)
0145      *
0146      * @param docToMerge       document containing the data to be merged
0147      * @param matchIdentifiers if true only entries having identifiers present in the
0148      *                         current document will be mergedurl is empty (or NULL) actual name is preserved
0149      */
0150     void merge(KEduVocDocument *docToMerge, bool matchIdentifiers);
0151 
0152     /**
0153      * Indicates if the document is modified
0154      *
0155      * @param dirty   new state
0156      */
0157     void setModified(bool dirty = true);
0158 
0159     /** @returns the modification state of the doc */
0160     bool isModified() const;
0161 
0162     /**
0163      * Sets the URL of the XML file
0164      * @param url URL
0165      */
0166     void setUrl(const QUrl &url);
0167 
0168     /** @returns the URL of the XML file */
0169     QUrl url() const;
0170 
0171     ///@}
0172 
0173     /** @name General Document Properties
0174      *
0175      */
0176     ///@{
0177 
0178     /** Set the title of the file
0179      * @param title title to set */
0180     void setTitle(const QString &title);
0181 
0182     /** @returns the title of the file */
0183     QString title() const;
0184 
0185     /** Set the author of the file
0186      * @param author author to set */
0187     void setAuthor(const QString &author);
0188 
0189     /** @returns the author of the file */
0190     QString author() const;
0191 
0192     /** Set the author contact info
0193      * @param authorContact contact email/contact info to set */
0194     void setAuthorContact(const QString &authorContact);
0195 
0196     /** @returns the author contact information */
0197     QString authorContact() const;
0198 
0199     /** Set the license of the file
0200      * @param license license to set */
0201     void setLicense(const QString &license);
0202 
0203     /** @returns the license of the file */
0204     QString license() const;
0205 
0206     /** Set the comment of the file
0207      * @param comment comment to set */
0208     void setDocumentComment(const QString &comment);
0209 
0210     /** @return the comment of the file */
0211     QString documentComment() const;
0212 
0213     /** Set the category of the file
0214      * @param category category to set */
0215     void setCategory(const QString &category);
0216 
0217     /** @return the category of the file */
0218     QString category() const;
0219 
0220     /**
0221      * Sets the generator of the file
0222      * @param generator name of the application generating the file
0223      */
0224     void setGenerator(const QString &generator);
0225 
0226     /** @returns the generator of the file */
0227     QString generator() const;
0228 
0229     /** Sets version of the loaded file
0230      * @param ver the new version */
0231     void setVersion(const QString &ver);
0232 
0233     /** @returns the version of the loaded file */
0234     QString version() const;
0235 
0236     ///@}
0237 
0238     /** @name Identifier Methods
0239      *
0240      */
0241     ///@{
0242 
0243     /**
0244      * @returns the number of different identifiers (usually languages)
0245      */
0246     int identifierCount() const;
0247 
0248     /**
0249      * Appends a new identifier (usually a language)
0250      *
0251      * @param identifier the identifier to append. If empty default names are used.
0252      * @returns the identifier number
0253      */
0254     int appendIdentifier(const KEduVocIdentifier &identifier = KEduVocIdentifier());
0255 
0256     /**
0257      * Sets the identifier of translation
0258      *
0259      * @param index            number of translation 0..x
0260      * @param lang             the language identifier: en=english, de=german, ...
0261      */
0262     void setIdentifier(int index, const KEduVocIdentifier &lang);
0263 
0264     /**
0265      * Returns the identifier of translation @p index
0266      *
0267      * @param index            number of translation 0..x
0268      * @returns                the language identifier: en=english, de=german, ...
0269      */
0270     KEduVocIdentifier &identifier(int index);
0271 
0272     /**
0273      * Const overload of identifier(int);
0274      * @param index of the identifier
0275      * @return reference to the identifier
0276      */
0277     const KEduVocIdentifier &identifier(int index) const;
0278 
0279     /**
0280      * Removes identifier and the according translation in all entries
0281      *
0282      * @param index            number of translation 0..x
0283      */
0284     void removeIdentifier(int index);
0285 
0286     /**
0287      * Determines the index of a given identifier
0288      *
0289      * @param name             identifier of language
0290      * @returns                index of identifier, 0 = original, 1..n = translation, -1 = not found
0291      */
0292     int indexOfIdentifier(const QString &name) const;
0293 
0294     ///@}
0295 
0296     /** @name Grade Methods
0297      *
0298      */
0299     ///@{
0300 
0301     /**
0302      * Retrieves the identifiers for the current query
0303      * not written in the new version!
0304      *
0305      * @param org        identifier for original
0306      * @param trans      identifier for translation
0307      */
0308     KEDUVOCDOCUMENT_DEPRECATED void queryIdentifier(QString &org, QString &trans) const;
0309 
0310     /**
0311      * Sets the identifiers for the current query
0312      * not written in the new version!
0313      *
0314      * @param org        identifier for original
0315      * @param trans      identifier for translation
0316      */
0317     KEDUVOCDOCUMENT_DEPRECATED void setQueryIdentifier(const QString &org, const QString &trans);
0318 
0319     ///@}
0320 
0321     /** @name Lesson Methods
0322      *
0323      */
0324     ///@{
0325 
0326     /** @brief Get the lesson root object
0327      * @returns a pointer to the lesson object
0328      */
0329     KEduVocLesson *lesson();
0330 
0331     /** @brief Returns the root word type object
0332         @return poitner to the internal word type object */
0333     KEduVocWordType *wordTypeContainer();
0334 
0335     /** @brief Returns the root Leitner container
0336         @return poitner to the internal Leitner container object
0337         @todo in new API determine if this is used */
0338     KEduVocLeitnerBox *leitnerContainer();
0339 
0340     ///@}
0341 
0342     /** @name File Format Specific Methods
0343      *
0344      */
0345     ///@{
0346 
0347     /**
0348      * Returns the delimiter (separator) used for csv import and export.
0349      * The default is a single tab character
0350      *
0351      * @returns                the delimiter used
0352      */
0353     QString csvDelimiter() const;
0354 
0355     /**
0356      * Sets the delimiter (separator) used for csv import and export
0357      *
0358      * @param delimiter        the delimiter to use
0359      */
0360     void setCsvDelimiter(const QString &delimiter);
0361 
0362     ///@}
0363 
0364     /**
0365      * @details Detects the file type
0366      * @param fileName filename
0367      * @return enum of filetype
0368      */
0369     static FileType detectFileType(const QString &fileName);
0370 
0371     /**
0372      * Create a string with the supported document types, that can be used
0373      * as filter in KFileDialog. It includes also an entry to match all the
0374      * supported types.
0375      *
0376      * @param mode             the mode for the supported document types
0377      * @returns                the filter string
0378      */
0379     static QString pattern(FileDialogMode mode);
0380 
0381     /**
0382      * @brief Returns a QString description of the errorCode
0383      * @param errorCode the code
0384      * @returns a user useful error string.
0385      */
0386     static QString errorDescription(int errorCode);
0387 
0388 Q_SIGNALS:
0389     /**
0390      * @brief never used
0391      * @param curr_percent
0392      */
0393     void progressChanged(KEduVocDocument *, int curr_percent);
0394 
0395     /**
0396      * @brief Emitted when the document becomes modified or saved.
0397      * @param mod the current modified/dirty state
0398      * @returns state (true=modified)
0399      */
0400     void docModified(bool mod);
0401 
0402 private:
0403     // The private data of this - see KEduVocDocument::Private, implemented in keduvocdocument.cpp
0404     class KEduVocDocumentPrivate;
0405     KEduVocDocumentPrivate *const d; ///< d pointer to private class
0406 
0407     Q_DISABLE_COPY(KEduVocDocument)
0408 };
0409 
0410 #endif // KEDUVOCDOCUMENT_H