File indexing completed on 2024-12-22 04:30:16

0001 #include "notescontroller.h"
0002 #include "db/db.h"
0003 #include "owl.h"
0004 #include <QStringRef>
0005 
0006 #include <MauiKit3/FileBrowsing/fmstatic.h>
0007 #include <MauiKit3/FileBrowsing/tagging.h>
0008 
0009 Q_DECLARE_METATYPE(FMH::MODEL_LIST)
0010 Q_DECLARE_METATYPE(FMH::MODEL)
0011 
0012 NotesController::NotesController(QObject *parent)
0013     : QObject(parent)
0014     , m_db(DB::getInstance())
0015 {
0016     qRegisterMetaType<FMH::MODEL_LIST>("MODEL_LIST");
0017     qRegisterMetaType<FMH::MODEL>("MODEL");
0018 
0019     auto m_loader = new NotesLoader;
0020     m_loader->moveToThread(&m_worker);
0021 
0022     connect(&m_worker, &QThread::finished, m_loader, &QObject::deleteLater);
0023     connect(this, &NotesController::fetchNotes, m_loader, &NotesLoader::fetchNotes);
0024 
0025     connect(m_loader, &NotesLoader::noteReady, this, &NotesController::noteReady);
0026     connect(m_loader, &NotesLoader::notesReady, this, &NotesController::notesReady);
0027 
0028     m_worker.start();
0029 }
0030 
0031 NotesController::~NotesController()
0032 {
0033     m_worker.quit();
0034     m_worker.wait();
0035 }
0036 
0037 bool NotesController::insertNote(FMH::MODEL &note)
0038 {
0039     if (note.isEmpty()) {
0040         qWarning() << "Could not insert note locally. The note is empty. NotesController::insertNote.";
0041         return false;
0042     }
0043 
0044     if ((OWL::NotesPath.isLocalFile() && !FMH::fileExists(OWL::NotesPath)) || OWL::NotesPath.isEmpty() || !OWL::NotesPath.isValid()) {
0045         qWarning() << "The url destination is not valid or does not exists, therefore it could not be saved into a file" << OWL::NotesPath;
0046         qWarning() << "File could not be saved. NotesController::insertNote.";
0047         return false;
0048     }
0049 
0050     note[FMH::MODEL_KEY::ID] = OWL::createId();
0051     const auto url_ = QUrl(OWL::NotesPath.toString() + note[FMH::MODEL_KEY::ID] + note[FMH::MODEL_KEY::FORMAT]);
0052     if (!OWL::saveNoteFile(url_, note[FMH::MODEL_KEY::CONTENT].toUtf8()))
0053         return false;
0054 
0055     note[FMH::MODEL_KEY::URL] = url_.toString();
0056 
0057     //  for(const auto &tg : note[FMH::MODEL_KEY::TAG].split(",", QString::SplitBehavior::SkipEmptyParts))
0058     //        Tagging::getInstance()->tagAbstract(tg, "notes", note[FMH::MODEL_KEY::URL], note[FMH::MODEL_KEY::COLOR]);
0059 
0060     return (this->m_db->insert(OWL::TABLEMAP[OWL::TABLE::NOTES], FMH::toMap(FMH::filterModel(note, {FMH::MODEL_KEY::URL, FMH::MODEL_KEY::ID, FMH::MODEL_KEY::COLOR, FMH::MODEL_KEY::FAVORITE}))));
0061 }
0062 
0063 bool NotesController::updateNote(FMH::MODEL &note, QString id)
0064 {
0065     if (note.isEmpty())
0066         return false;
0067 
0068     if (note[FMH::MODEL_KEY::URL].isEmpty())
0069         note[FMH::MODEL_KEY::URL] = [&]() -> const QString {
0070             const auto data = DB::getInstance()->getDBData(QString("select url from notes where id = '%1'").arg(id));
0071             return data.isEmpty() ? QString() : data.first()[FMH::MODEL_KEY::URL];
0072         }();
0073 
0074     if (note[FMH::MODEL_KEY::URL].isEmpty())
0075         return false;
0076 
0077     const auto f_note = FMH::toMap(FMH::filterModel(note, {FMH::MODEL_KEY::COLOR, FMH::MODEL_KEY::FAVORITE}));
0078     if (f_note.isEmpty())
0079         return true;
0080 
0081     return this->m_db->update(OWL::TABLEMAP[OWL::TABLE::NOTES], f_note, QVariantMap{{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}});
0082 }
0083 
0084 bool NotesController::removeNote(const QString &id)
0085 {
0086     const auto url = QUrl([&]() -> const QString {
0087         const auto data = DB::getInstance()->getDBData(QString("select url from notes where id = '%1'").arg(id));
0088         return data.isEmpty() ? QString() : data.first()[FMH::MODEL_KEY::URL];
0089     }());
0090 
0091     this->m_db->remove(OWL::TABLEMAP[OWL::TABLE::NOTES_SYNC], {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}});
0092 
0093     FMStatic::removeFiles({url});
0094 
0095     return this->m_db->remove(OWL::TABLEMAP[OWL::TABLE::NOTES], {{FMH::MODEL_NAME[FMH::MODEL_KEY::ID], id}});
0096 }
0097 
0098 void NotesController::getNotes()
0099 {
0100     Q_EMIT this->fetchNotes(this->m_db->getDBData("select * from notes"));
0101 }
0102 
0103 void NotesLoader::fetchNotes(FMH::MODEL_LIST notes)
0104 {
0105     for (auto &note : notes) {
0106         const auto url = QUrl(note[FMH::MODEL_KEY::URL]);
0107         const auto contentPreview = OWL::fileContentPreview(url);
0108         note[FMH::MODEL_KEY::CONTENT] = contentPreview;
0109         Q_EMIT this->noteReady(note);
0110     }
0111 
0112     qDebug() << "FINISHED FETCHING URLS";
0113     Q_EMIT this->notesReady(notes);
0114 }