File indexing completed on 2024-05-12 04:45:56

0001 #ifndef OWL_H
0002 #define OWL_H
0003 
0004 #include <QStandardPaths>
0005 #include <QString>
0006 #include <QUrl>
0007 #include <QUuid>
0008 #include <QFile>
0009 #include <QDebug>
0010 
0011 #include <MauiKit3/Core/fmh.h>
0012 
0013 namespace OWL
0014 {
0015 Q_NAMESPACE
0016 enum class TABLE : uint8_t { NOTES, NOTES_SYNC, BOOKS, BOOKLETS, BOOKLETS_SYNC, NONE };
0017 
0018 static const QMap<TABLE, QString> TABLEMAP = {{TABLE::NOTES, "notes"}, {TABLE::NOTES_SYNC, "notes_sync"}, {TABLE::BOOKS, "books"}, {TABLE::BOOKLETS, "booklets"}, {TABLE::BOOKLETS_SYNC, "booklets_sync"}};
0019 
0020 const static inline QUrl CollectionDBPath = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/buho/");
0021 const static inline QUrl NotesPath = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/buho/notes/");
0022 
0023 const static inline QString DBName = "collection.db";
0024 
0025 static inline bool saveNoteFile(const QUrl &url, const QByteArray &data)
0026 {
0027     if (data.isEmpty()) {
0028         qWarning() << "the note is empty, therefore it could not be saved into a file" << url;
0029         return false;
0030     }
0031 
0032     if (url.isEmpty() || !url.isValid()) {
0033         qWarning() << "the url is not valid or is empty, therefore it could not be saved into a file" << url;
0034         return false;
0035     }
0036 
0037     QFile file(url.toLocalFile());
0038     if (file.open(QFile::WriteOnly)) {
0039         file.write(data);
0040         file.close();
0041         return true;
0042     } else
0043         qWarning() << "Couldn-t open file to writte the text " << url;
0044 
0045     return false;
0046 }
0047 
0048 static inline const QString createId()
0049 {
0050     return QUuid::createUuid().toString();
0051 }
0052 
0053 static inline const QString fileContentPreview(const QUrl &path)
0054 {
0055     if (!path.isLocalFile()) {
0056         qWarning() << "Can not open note file, the url is not a local path";
0057         return QString();
0058     }
0059 
0060     if (!FMH::fileExists(path))
0061         return QString();
0062 
0063     QFile file(path.toLocalFile());
0064     if (file.open(QFile::ReadOnly)) {
0065         const auto content = file.read(512);
0066         file.close();
0067         return QString(content);
0068     }
0069 
0070     return QString();
0071 }
0072 }
0073 
0074 #endif // OWL_H