/* 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; }