diff --git a/notebooklist.h b/notebooklist.h new file mode 100644 index 0000000..6ed5b20 --- /dev/null +++ b/notebooklist.h @@ -0,0 +1,37 @@ +/* DrewTechs + * Note Binder + * @Version 1.0 + */ + +#ifndef NOTEBOOKLIST_H +#define NOTEBOOKLIST_H + +#include +#include +#include +#include +#include "notebook.h" +#include "notebookmanager.h" +#include "newNotebookDialog.h" + +class NotebookList : public QObject +{ + Q_OBJECT +public: + explicit NotebookList(QObject *parent = nullptr); + QVector nbList; + +signals: + +public slots: + void addNotebookToList(Notebook* notebook); + QTreeWidgetItem* createSectionIcon(QFileInfo fileInfo); + QTreeWidgetItem* createSectionGroupIcon(SectionGroup* sectionGroup); + QTreeWidgetItem* createNotebookIcon(Notebook* notebook); + + + QIcon SetNotebookIcon(QString notebookColor); + QIcon SetSectionGroupIcon(QString sectionGroupColor); +}; + +#endif // NOTEBOOKLIST_H diff --git a/notebookmanager.cpp b/notebookmanager.cpp new file mode 100644 index 0000000..ec0c8e3 --- /dev/null +++ b/notebookmanager.cpp @@ -0,0 +1,251 @@ +/* DrewTechs + * Note Binder + * @Version 1.0 + */ + +#include "notebookmanager.h" + +Notebook* NewNotebook(QVector nbList) +{ + newNotebookDialog* uiNewNotebookDialog = new newNotebookDialog(); + if(!uiNewNotebookDialog->exec()) return nullptr; + QString notebookName = uiNewNotebookDialog->getNbName(); + QString notebookPath = uiNewNotebookDialog->getNbDirectory(); + QString notebookFileName = uiNewNotebookDialog->getNbFileName(); + QString notebookColor = uiNewNotebookDialog->getNbColor(); + int nb_idx = 0; + for(nb_idx = 0; nb_idx < nbList.size(); nb_idx++) + { + if(notebookPath == nbList.at(nb_idx)->location) + { + return nullptr; // Notebook Already Exists in that spot, don't recreate it! + } + } + Notebook *notebook = new Notebook(); + srand(time(0)); + + notebook->id = random(); + notebook->name = notebookName; + notebook->location = notebookPath; + notebook->color = notebookColor; + + QString notebookFilePath = notebookPath + QDir::separator() + "notebook.noteb"; + + QFile file(notebookFilePath); + if (file.open(QIODevice::ReadWrite)) { + QTextStream in(&file); + in << notebookName << "\n" << notebookColor << "\n"; + } + file.close(); + return notebook; +} + +Notebook openNotebook(QString notebookFilePath) +{ + // Read items from Notebook File + QStringList fields; + QFileInfo notebookFileInfo(notebookFilePath); + /* Notebook File Name must match Directory Name of Notebook. */ + QFile nbFile(notebookFileInfo.absoluteFilePath() + "/" + "notebook.noteb"); + QStringList strings; + int idx = 0; + if(nbFile.exists()) + { + if(nbFile.open(QIODevice::ReadOnly | QIODevice::Text)) + { + QTextStream in(&nbFile); + while (!in.atEnd()) { + strings += in.readLine(); + } + } + + // Open Notebook + Notebook* notebook = new Notebook(); + srand(time(0)); + notebook->id = random(); + notebook->color = strings.at(1); + notebook->name = notebookFileInfo.fileName(); + notebook->location = notebookFilePath; + QDirIterator it(notebookFileInfo.absoluteFilePath(), QDirIterator::NoIteratorFlags); + QStringList fileDirectories; + idx = 0; + while (it.hasNext()) { + QString strFile = it.next(); + QFileInfo fileInfo(strFile); + if(fileInfo.fileName() != "." && fileInfo.fileName() != ".." && fileInfo.fileName().endsWith(".noteb") == false && fileInfo.fileName().endsWith("~") == false) + { + notebook->files.append(fileInfo.filePath()); + idx++; + } + } + return *notebook; + } +} + +SectionGroup openSectionGroup(QString parentFilePath, QString sectionGroupFilePath) +{ + // Read items from SectionGroup File + QFileInfo sectionGroupFileInfo(sectionGroupFilePath); + QFile sgFile(sectionGroupFileInfo.absoluteFilePath() + "/" + "sectiongroup.sectgr"); + QStringList strings; + int idx = 0; + if(sgFile.exists()) + { + if(sgFile.open(QIODevice::ReadOnly | QIODevice::Text)) + { + QTextStream in(&sgFile); + while (!in.atEnd()) { + strings += in.readLine(); + } + } + // Open Section Group + SectionGroup* sectionGroup = new SectionGroup(); + sectionGroup->name = sectionGroupFileInfo.fileName(); + sectionGroup->color = strings.at(1); + sectionGroup->location = sectionGroupFilePath; + QDirIterator it(sectionGroupFileInfo.absoluteFilePath(), QDirIterator::NoIteratorFlags); + QStringList fileDirectories; + idx = 0; + while (it.hasNext()) { + QString strFile = it.next(); + QFileInfo fileInfo(strFile); + if(fileInfo.fileName() != "." && fileInfo.fileName() != ".." && fileInfo.fileName().endsWith(".sectgr") == false && fileInfo.fileName().endsWith("~") == false) + { + sectionGroup->files.append(fileInfo.filePath()); + idx++; + } + } + sectionGroup->parentDir = parentFilePath; // Parent directory information, the Notebook is the root directory so the Notebook doesn't require this variable. + return *sectionGroup; + } else { + // Open Empty Folder as a Section Group with the default color. + SectionGroup* sectionGroup = new SectionGroup(); + sectionGroup->name = sectionGroupFileInfo.fileName(); + sectionGroup->color = DEFAULT_SG_COLOR; + sectionGroup->location = sectionGroupFilePath; + QDirIterator it(sectionGroupFileInfo.absoluteFilePath(), QDirIterator::NoIteratorFlags); + QStringList fileDirectories; + idx = 0; + while (it.hasNext()) { + QString strFile = it.next(); + QFileInfo fileInfo(strFile); + if(fileInfo.fileName() != "." && fileInfo.fileName() != ".." && fileInfo.fileName().endsWith(".sectgr") == false && fileInfo.fileName().endsWith("~") == false) + { + sectionGroup->files.append(fileInfo.filePath()); + idx++; + } + } + sectionGroup->parentDir = parentFilePath; // Parent directory information, the Notebook is the root directory so the Notebook doesn't require this variable. + return *sectionGroup; + } +} + +QString getFileSize(qint64 size) +{ + double byteSize; + if(size >= 1024 && size < 1048576) { + // Kibibyte (KiB) + byteSize = double(size) / double(1024); + return QString("%1").arg(byteSize, 0, 'f', 2) + " KiB"; + } else if (size >= 1048576 && size < 1073741824) { + // Mebibyte (MiB) + byteSize = double(size) / double(1048576); + return QString("%1").arg(byteSize, 0, 'f', 2) + " MiB"; + } else if (size >= 1073741824 && size < 1099511627776) { + // Gibibyte (GiB) + byteSize = double(size) / double(1073741824); + return QString("%1").arg(byteSize, 0, 'f', 2) + " GiB"; + } else if (size >= 1099511627776) { + // Tebibyte (TiB) + byteSize = double(size) / double(1099511627776); + return QString("%1").arg(byteSize, 0, 'f', 2) + " TiB"; + } else { + // Bytes + byteSize = double(size); + return QString::number(byteSize) + " B"; + } +} + +qint64 getDirRecursiveSize(QString directory) +{ + qint64 size = 0; + QDir dir(directory); + QDir::Filters fileFilters = QDir::Files; + for(QString filePath : dir.entryList(fileFilters)) { + QFileInfo fileInfo(dir, filePath); + size+= fileInfo.size(); + } + QDir::Filters dirFilters = QDir::Dirs|QDir::NoDotAndDotDot; + for(QString childDirPath : dir.entryList(dirFilters)) + { + size+= getDirRecursiveSize(directory + QDir::separator() + childDirPath); + } + return size; +} + +QString getDirectorySize(QString directory) +{ + qint64 size = getDirRecursiveSize(directory); + double byteSize; + if(size >= 1024 && size < 1048576) { + // Kibibyte (KiB) + byteSize = double(size) / double(1024); + return QString("%1").arg(byteSize, 0, 'f', 2) + " KiB"; + } else if (size >= 1048576 && size < 1073741824) { + // Mebibyte (MiB) + byteSize = double(size) / double(1048576); + return QString("%1").arg(byteSize, 0, 'f', 2) + " MiB"; + } else if (size >= 1073741824) { + // Gibibyte (GiB) + byteSize = double(size) / double(1073741824); + return QString("%1").arg(byteSize, 0, 'f', 2) + " GiB"; + } else if (size >= 1099511627776) { + // Tebibyte (TiB) + byteSize = double(size) / double(1099511627776); + return QString("%1").arg(byteSize, 0, 'f', 2) + " TiB"; + } else { + // Bytes + byteSize = double(size); + return QString::number(byteSize) + " B"; + } +} + +/* Adds Notebook and Section Groups to the Combo Box */ +QStringList addItemsToDirectorySelection(QVector NotebookList) +{ + int nb_idx = 0; // Notebook Index + int sg_idx = 0; // Section Group Index + QStringList directoryList; + for(nb_idx = 0; nb_idx < NotebookList.size(); nb_idx++) + { + directoryList.append(NotebookList.at(nb_idx)->name); + for(sg_idx = 0; sg_idx < NotebookList.at(nb_idx)->sectionGroupArray.size(); sg_idx++) + { + directoryList.append(" " + NotebookList.at(nb_idx)->sectionGroupArray.at(sg_idx)->name); + directoryList.append(addSubItemsToDirectorySection(NotebookList.at(nb_idx)->sectionGroupArray.at(sg_idx), 2)); + } + } + return directoryList; +} + +QStringList addSubItemsToDirectorySection(SectionGroup* sectionGroup, int tabCount) +{ + // Recursive Function + int sg_idx = 0; + //int subsg_idx = 0; + int tab_idx = 0; + QString tabs = ""; + QStringList directoryList; + for(tab_idx = 0; tab_idx < tabCount; tab_idx++) + { + tabs += " "; + } + for(sg_idx = 0; sg_idx < sectionGroup->sectionGroupArray.size(); sg_idx++) + { + directoryList.append(tabs + sectionGroup->sectionGroupArray.at(sg_idx)->name); + tab_idx++; + directoryList.append(addSubItemsToDirectorySection(sectionGroup->sectionGroupArray.at(sg_idx), tab_idx)); + } + return directoryList; +} + diff --git a/notebookmanager.h b/notebookmanager.h new file mode 100644 index 0000000..891e828 --- /dev/null +++ b/notebookmanager.h @@ -0,0 +1,36 @@ +/* DrewTechs + * Note Binder + * @Version 1.0 + */ + +#ifndef NOTEBOOKMANAGER_H +#define NOTEBOOKMANAGER_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "notebook.h" +#include "notebooklist.h" +#include "sectiongroup.h" + +#define DEFAULT_SG_COLOR "None" + +Notebook* NewNotebook(QVector nbList); // Create New Notebook +Notebook openNotebook(QString notebookFilePath); +SectionGroup openSectionGroup(QString parentFilePath, QString sectionGroupFilePath); +QString getFileSize(qint64 size); +qint64 getDirRecursiveSize(QString directory); +QString getDirectorySize(QString directory); + +QStringList addItemsToDirectorySelection(QVector NotebookList); +QStringList addSubItemsToDirectorySection(SectionGroup* sectionGroup, int tabCount); + + +#endif // NOTEBOOKMANAGER_H diff --git a/sectiongroup.cpp b/sectiongroup.cpp new file mode 100644 index 0000000..c9f2b71 --- /dev/null +++ b/sectiongroup.cpp @@ -0,0 +1,11 @@ +/* DrewTechs + * Note Binder + * @Version 1.0 + */ + +#include "sectiongroup.h" + +SectionGroup::SectionGroup() +{ + +}