1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
/*
* Copyright (C) 2014 David Edmundson <david@davidedmundson.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "filesystemnoteloader.h"
#include "note.h"
#include <QUuid>
#include <QDebug>
#include <QStandardPaths>
#include <KDirWatch>
class FileNote : public Note
{
Q_OBJECT
public:
FileNote(const QString &path, const QString &id);
void load();
void save(const QString &text) override;
private:
void fileSystemChanged(const QString &path);
QString m_path;
KDirWatch *m_watcher;
};
FileSystemNoteLoader::FileSystemNoteLoader()
{
const QString genericDataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
const QString suffix = QStringLiteral("plasma_notes");
QDir(genericDataLocation).mkdir(suffix);
m_notesDir.setPath(genericDataLocation + QDir::separator() + suffix);
}
QStringList FileSystemNoteLoader::allNoteIds()
{
return m_notesDir.entryList(QStringList {QStringLiteral("*.txt")});
}
void FileSystemNoteLoader::deleteNoteResources(const QString &id)
{
m_notesDir.remove(id);
}
Note* FileSystemNoteLoader::loadNote(const QString &id)
{
QString idToUse = id;
if (id.isEmpty()) {
idToUse = QUuid::createUuid().toString().mid(1, 34);//UUID adds random braces I don't want them on my file system
}
FileNote* note = new FileNote(m_notesDir.absoluteFilePath(idToUse), idToUse);
return note;
}
FileNote::FileNote(const QString& path, const QString& id):
Note(id),
m_path(path),
m_watcher(new KDirWatch(this))
{
m_watcher->addFile(path);
connect(m_watcher, &KDirWatch::created, this, &FileNote::fileSystemChanged);
connect(m_watcher, &KDirWatch::dirty, this, &FileNote::fileSystemChanged);
load();
}
void FileNote::load()
{
QFile file(m_path);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
setNoteText(QString::fromUtf8(file.readAll()));
}
}
void FileNote::save(const QString &text)
{
if (text == noteText()) {
return;
}
m_watcher->removeFile(m_path);
QFile file(m_path);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
file.write(text.toUtf8());
} else {
qWarning() << "Could not write notes to file" << m_path;
}
setNoteText(text);
m_watcher->addFile(m_path);
}
void FileNote::fileSystemChanged(const QString &path)
{
if (path == m_path) {
load();
}
}
#include "filesystemnoteloader.moc"
|