Warning, file /office/marknote/src/notebooksmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-FileCopyrightText: 2023 Mathis BrĂ¼chert <mbb@kaidan.im>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "notebooksmodel.h"
0005 #include <QDateTime>
0006 #include <QDebug>
0007 #include <QFile>
0008 #include <QStandardPaths>
0009 #include <QStringBuilder>
0010 #include <QUrl>
0011 
0012 #include <KConfigGroup>
0013 #include <KDesktopFile>
0014 
0015 NoteBooksModel::NoteBooksModel(QObject *parent)
0016     : QAbstractListModel(parent)
0017     , directory(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + QDir::separator() + "Notes")
0018 {
0019     directory.mkpath(".");
0020     qDebug() << directory.path();
0021 }
0022 
0023 int NoteBooksModel::rowCount(const QModelIndex &index) const
0024 {
0025     return index.isValid() ? 0 : directory.entryList(QDir::AllDirs | QDir::NoDotAndDotDot).count();
0026 }
0027 
0028 QVariant NoteBooksModel::data(const QModelIndex &index, int role) const
0029 {
0030     switch (role) {
0031     case Role::Path:
0032         return directory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot).at(index.row()).filePath();
0033 
0034     case Role::Icon: {
0035         const QString dotDirectory =
0036             directory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot).at(index.row()).filePath() % QDir::separator() % ".directory";
0037         if (QFile::exists(dotDirectory)) {
0038             return KDesktopFile(dotDirectory).readIcon();
0039         } else {
0040             return QStringLiteral("addressbook-details");
0041         }
0042     }
0043     case Role::Color: {
0044         const QString dotDirectory =
0045             directory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot).at(index.row()).filePath() % QDir::separator() % ".directory";
0046         if (QFile::exists(dotDirectory)) {
0047             return KDesktopFile(dotDirectory).desktopGroup().readEntry("X-MarkNote-Color");
0048         } else {
0049             return QStringLiteral("addressbook-details");
0050         }
0051     }
0052     case Role::Name:
0053         return directory.entryList(QDir::AllDirs | QDir::NoDotAndDotDot).at(index.row());
0054     }
0055 
0056     Q_UNREACHABLE();
0057 
0058     return {};
0059 }
0060 
0061 QHash<int, QByteArray> NoteBooksModel::roleNames() const
0062 {
0063     return {{Role::Icon, "iconName"}, {Role::Path, "path"}, {Role::Name, "name"}, {Role::Color, "color"}};
0064 }
0065 
0066 void NoteBooksModel::addNoteBook(const QString &name, const QString &icon, const QString &color)
0067 {
0068     qDebug() << Q_FUNC_INFO;
0069 
0070     beginResetModel();
0071     directory.mkdir(name);
0072     const QString dotDirectory = directory.path() % QDir::separator() % name % QDir::separator() % ".directory";
0073     KConfig desktopFile(dotDirectory, KConfig::SimpleConfig);
0074     auto desktopEntry = desktopFile.group("Desktop Entry");
0075     desktopEntry.writeEntry("Icon", icon);
0076     desktopEntry.writeEntry("X-MarkNote-Color", color);
0077     desktopFile.sync();
0078     endResetModel();
0079 }
0080 
0081 void NoteBooksModel::deleteNoteBook(const QString &name)
0082 {
0083     beginResetModel();
0084     QDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + QDir::separator() + "Notes" + QDir::separator() + name).removeRecursively();
0085     endResetModel();
0086 }
0087 
0088 void NoteBooksModel::renameNoteBook(const QUrl &path, const QString &name)
0089 {
0090     QString newPath = directory.path() + QDir::separator() + name + ".md";
0091     beginResetModel();
0092     QFile::rename(path.toLocalFile(), newPath);
0093     endResetModel();
0094 }