#include "copyItemDialog.h" #include "ui_copyItemDialog.h" CopyItemDialog::CopyItemDialog(bool moveItem, QStringList notebookColorList, QStringList notebookDirList, QStringList list, QString itemName, QString itemLocation, QWidget *parent) : QDialog(parent), ui(new Ui::CopyItemDialog) { setMoveItemState(moveItem); setNotebookColorList(notebookColorList); setNotebookDirList(notebookDirList); setDirectories(list); setFileName(itemName); setFileLocation(itemLocation); ui->setupUi(this); init(); } CopyItemDialog::~CopyItemDialog() { delete ui; } void CopyItemDialog::init() { ui->fileNameBox->setText(getFileName()); if(moveItemState == false) { // Copying Item: ui->moveItemState_Label->setText("Copy Item:"); } else { // Moving Item: ui->moveItemState_Label->setText("Move Item:"); } addItemsToDirectory(); } void CopyItemDialog::addItemsToDirectory() { int idx = 0; for(idx = 0; idx < getDirectories().size(); idx++) { if(!getDirectories().at(idx).startsWith(" ")) { ui->directorySelection->addItem(SetNotebookIcon(notebookcolorlist.at(idx)), getDirectories().at(idx)); } else { ui->directorySelection->addItem(SetSectionGroupIcon(notebookcolorlist.at(idx)), getDirectories().at(idx)); } } } QIcon CopyItemDialog::SetNotebookIcon(QString notebookColor) { QIcon icon; QString iconPath = "../NoteBinder/Icons"; if(!QDir(iconPath).exists()) { return icon; } if(notebookColor == "Red") { icon = QIcon(iconPath + "/NotebookIcon_Red.png"); } else if(notebookColor == "Blue") { icon = QIcon(iconPath + "/NotebookIcon_Blue.png"); } else if(notebookColor == "Purple") { icon = QIcon(iconPath + "/NotebookIcon_Purple.png"); } else if(notebookColor == "Orange") { icon = QIcon(iconPath + "/NotebookIcon_Orange.png"); } else if(notebookColor == "Yellow") { icon = QIcon(iconPath + "/NotebookIcon_Yellow.png"); } else if(notebookColor == "Green") { icon = QIcon(iconPath + "/NotebookIcon_Green.png"); } else if(notebookColor == "Cyan") { icon = QIcon(iconPath + "/NotebookIcon_Cyan.png"); } else if(notebookColor == "Tan") { icon = QIcon(iconPath + "/NotebookIcon_Tan.png"); } else if(notebookColor == "Teal") { icon = QIcon(iconPath + "/NotebookIcon_Teal.png"); } else if(notebookColor == "Red Chalk") { icon = QIcon(iconPath + "/NotebookIcon_RedChalk.png"); } else if(notebookColor == "Blue Mist") { icon = QIcon(iconPath + "/NotebookIcon_BlueMist.png"); } else if(notebookColor == "Purple Mist") { icon = QIcon(iconPath + "/NotebookIcon_PurpleMist.png"); } else if(notebookColor == "Magenta") { icon = QIcon(iconPath + "/NotebookIcon_Magenta.png"); } else if(notebookColor == "Lemon Lime") { icon = QIcon(iconPath + "/NotebookIcon_LemonLime.png"); } else if(notebookColor == "Apple") { icon = QIcon(iconPath + "/NotebookIcon_Apple.png"); } else if(notebookColor == "Silver") { icon = QIcon(iconPath + "/NotebookIcon_Silver.png"); } else if(notebookColor == "Black") { icon = QIcon(iconPath + "/NotebookIcon_Black.png"); } else { // If it's either White/None or an invalid value, set to the white icon icon = QIcon(iconPath + "/NotebookIcon_White.png"); } return icon; } QIcon CopyItemDialog::SetSectionGroupIcon(QString sectionGroupColor) { QIcon icon; QString iconPath = "../NoteBinder/Icons"; if(!QDir(iconPath).exists()) { return icon; } if(sectionGroupColor == "Red") { icon = QIcon(iconPath + "/SectionGroupIcon_Red.png"); } else if(sectionGroupColor == "Blue") { icon = QIcon(iconPath + "/SectionGroupIcon_Blue.png"); } else if(sectionGroupColor == "Purple") { icon = QIcon(iconPath + "/SectionGroupIcon_Purple.png"); } else if(sectionGroupColor == "Orange") { icon = QIcon(iconPath + "/SectionGroupIcon_Orange.png"); } else if(sectionGroupColor == "Yellow") { icon = QIcon(iconPath + "/SectionGroupIcon_Yellow.png"); } else if(sectionGroupColor == "Green") { icon = QIcon(iconPath + "/SectionGroupIcon_Green.png"); } else if(sectionGroupColor == "Cyan") { icon = QIcon(iconPath + "/SectionGroupIcon_Cyan.png"); } else if(sectionGroupColor == "Tan") { icon = QIcon(iconPath + "/SectionGroupIcon_Tan.png"); } else if(sectionGroupColor == "Teal") { icon = QIcon(iconPath + "/SectionGroupIcon_Teal.png"); } else if(sectionGroupColor == "Red Chalk") { icon = QIcon(iconPath + "/SectionGroupIcon_RedChalk.png"); } else if(sectionGroupColor == "Blue Mist") { icon = QIcon(iconPath + "/SectionGroupIcon_BlueMist.png"); } else if(sectionGroupColor == "Purple Mist") { icon = QIcon(iconPath + "/SectionGroupIcon_PurpleMist.png"); } else if(sectionGroupColor == "Magenta") { icon = QIcon(iconPath + "/SectionGroupIcon_Magenta.png"); } else if(sectionGroupColor == "Lemon Lime") { icon = QIcon(iconPath + "/SectionGroupIcon_LemonLime.png"); } else if(sectionGroupColor == "Apple") { icon = QIcon(iconPath + "/SectionGroupIcon_Apple.png"); } else if(sectionGroupColor == "Silver") { icon = QIcon(iconPath + "/SectionGroupIcon_Silver.png"); } else if(sectionGroupColor == "Black") { icon = QIcon(iconPath + "/SectionGroupIcon_Black.png"); } else { // If it's either White/None or an invalid value, set to the white icon icon = QIcon(iconPath + "/SectionGroupIcon_White.png"); } return icon; } /* Getters */ bool CopyItemDialog::getMoveItemState() const { return moveItemState; } QStringList CopyItemDialog::getDirectories() const { return var_directoryList; } QStringList CopyItemDialog::getNotebookDirList() const { return notebookdirList; } QStringList CopyItemDialog::getNotebookColorList() const { return notebookcolorlist; } QString CopyItemDialog::getFileName() const { return var_itemName; } QString CopyItemDialog::getFileLocation() const { return var_itemLocation; } /* Setters */ void CopyItemDialog::setMoveItemState(bool updateMoveState) { moveItemState = updateMoveState; } void CopyItemDialog::setDirectories(QStringList dirList) { var_directoryList = dirList; } void CopyItemDialog::setNotebookDirList(QStringList nbDirList) { notebookdirList = nbDirList; } void CopyItemDialog::setNotebookColorList(QStringList nbColorList) { notebookcolorlist = nbColorList; } void CopyItemDialog::setFileName(QString newItemName) { var_itemName = newItemName; } void CopyItemDialog::setFileLocation(QString newItemLocation) { var_itemLocation = newItemLocation; } void CopyItemDialog::on_buttonBox_accepted() { if(ui->fileNameBox->text() != "") { QString fullFileName = getFileLocation(); setFileName(ui->fileNameBox->text()); QString newFullFileName = getNotebookDirList().at(ui->directorySelection->currentIndex()) + QDir::separator() + getFileName(); QFile file(newFullFileName); QDir dir(fullFileName); QFileInfo fileInfo(newFullFileName); if(fileInfo.absoluteFilePath() == fullFileName) { reject(); // Cannot write onto itself. } else { if(!file.exists()) { if(fileInfo.isFile()) { // File file.copy(fullFileName, newFullFileName); } else { copyRecursively(fullFileName, newFullFileName); QFile fileSectGr(newFullFileName + QDir::separator() + "sectiongroup.sectgr"); QStringList fileItems; // Read This file if(fileSectGr.exists()) { if(fileSectGr.open(QIODevice::ReadOnly)) { QTextStream in(&fileSectGr); while(!in.atEnd()) { fileItems += in.readLine(); } } fileSectGr.remove(); if (fileSectGr.open(QIODevice::ReadWrite)) { QTextStream in(&fileSectGr); in << fileInfo.fileName() << "\n" << fileItems.at(1) << "\n"; } fileSectGr.close(); } } setFileLocation(newFullFileName); accept(); } else { QMessageBox messageBox; QFont fontSize; fontSize.setPointSize(12); messageBox.setWindowTitle("Replace Existing Item?"); messageBox.setFont(fontSize); messageBox.setText("Item already exists! Do you want to overwrite it?"); messageBox.setStandardButtons(QMessageBox::Yes); messageBox.addButton(QMessageBox::No); messageBox.setDefaultButton(QMessageBox::No); if(messageBox.exec() == QMessageBox::Yes) { if(fileInfo.isFile()) { // File file.copy(fullFileName, newFullFileName); } else { copyRecursively(fullFileName, newFullFileName); QFile fileSectGr(newFullFileName + QDir::separator() + "sectiongroup.sectgr"); QStringList fileItems; // Read This file if(fileSectGr.exists()) { if(fileSectGr.open(QIODevice::ReadOnly)) { QTextStream in(&fileSectGr); while(!in.atEnd()) { fileItems += in.readLine(); } } fileSectGr.remove(); if (fileSectGr.open(QIODevice::ReadWrite)) { QTextStream in(&fileSectGr); in << fileInfo.fileName() << "\n" << fileItems.at(1) << "\n"; } fileSectGr.close(); } } setFileLocation(newFullFileName); accept(); } else { reject(); } } } } } void CopyItemDialog::on_buttonBox_rejected() { reject(); } bool CopyItemDialog::copyRecursively(const QString &srcFilePath, const QString &tgtFilePath) { /* Function written by ssendeavour: https://gist.github.com/ssendeavour/7324701#file-copy-recursively-cpp */ QFileInfo srcFileInfo(srcFilePath); if (srcFileInfo.isDir()) { QDir targetDir(tgtFilePath); targetDir.cdUp(); if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName())) return false; QDir sourceDir(srcFilePath); QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System); foreach (const QString &fileName, fileNames) { const QString newSrcFilePath = srcFilePath + QDir::separator() + fileName; const QString newTgtFilePath = tgtFilePath + QDir::separator() + fileName; if (!copyRecursively(newSrcFilePath, newTgtFilePath)) return false; } } else { if (!QFile::copy(srcFilePath, tgtFilePath)) return false; } return true; }