File indexing completed on 2024-05-12 16:59:35

0001 /*
0002  * SPDX-FileCopyrightText: 2014 David Edmundson <david@davidedmundson.co.uk>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  *
0006  */
0007 
0008 #include "filesystemnoteloader.h"
0009 
0010 #include "note.h"
0011 
0012 #include <QDebug>
0013 #include <QStandardPaths>
0014 #include <QUuid>
0015 
0016 #include <KDirWatch>
0017 
0018 class FileNote : public Note
0019 {
0020     Q_OBJECT
0021 public:
0022     FileNote(const QString &path, const QString &id);
0023     void load();
0024     void save(const QString &text) override;
0025 
0026 private:
0027     void fileSystemChanged(const QString &path);
0028     QString m_path;
0029     KDirWatch *m_watcher;
0030 };
0031 
0032 FileSystemNoteLoader::FileSystemNoteLoader()
0033 {
0034     const QString genericDataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
0035     const QString suffix = QStringLiteral("plasma_notes");
0036     QDir(genericDataLocation).mkdir(suffix);
0037     m_notesDir.setPath(genericDataLocation + QDir::separator() + suffix);
0038 }
0039 
0040 QStringList FileSystemNoteLoader::allNoteIds()
0041 {
0042     return m_notesDir.entryList(QStringList{QStringLiteral("*.txt")});
0043 }
0044 
0045 void FileSystemNoteLoader::deleteNoteResources(const QString &id)
0046 {
0047     m_notesDir.remove(id);
0048 }
0049 
0050 Note *FileSystemNoteLoader::loadNote(const QString &id)
0051 {
0052     QString idToUse = id;
0053     if (id.isEmpty()) {
0054         idToUse = QUuid::createUuid().toString().mid(1, 34); // UUID adds random braces I don't want them on my file system
0055     }
0056 
0057     FileNote *note = new FileNote(m_notesDir.absoluteFilePath(idToUse), idToUse);
0058     return note;
0059 }
0060 
0061 FileNote::FileNote(const QString &path, const QString &id)
0062     : Note(id)
0063     , m_path(path)
0064     , m_watcher(new KDirWatch(this))
0065 {
0066     m_watcher->addFile(path);
0067 
0068     connect(m_watcher, &KDirWatch::created, this, &FileNote::fileSystemChanged);
0069     connect(m_watcher, &KDirWatch::dirty, this, &FileNote::fileSystemChanged);
0070 
0071     load();
0072 }
0073 
0074 void FileNote::load()
0075 {
0076     QFile file(m_path);
0077     if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
0078         setNoteText(QString::fromUtf8(file.readAll()));
0079     }
0080 }
0081 
0082 void FileNote::save(const QString &text)
0083 {
0084     if (text == noteText()) {
0085         return;
0086     }
0087 
0088     m_watcher->removeFile(m_path);
0089 
0090     QFile file(m_path);
0091     if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0092         file.write(text.toUtf8());
0093     } else {
0094         qWarning() << "Could not write notes to file" << m_path;
0095     }
0096     setNoteText(text);
0097 
0098     m_watcher->addFile(m_path);
0099 }
0100 
0101 void FileNote::fileSystemChanged(const QString &path)
0102 {
0103     if (path == m_path) {
0104         load();
0105     }
0106 }
0107 
0108 #include "filesystemnoteloader.moc"