void VDirectoryTree::deleteSelectedDirectory() { Q_ASSERT(selectedItems().size() <= 1); QTreeWidgetItem *curItem = currentItem(); if (!curItem) { return; } VDirectory *curDir = getVDirectory(curItem); int ret = VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Are you sure to delete folder <span style=\"%1\">%2</span>?") .arg(g_config->c_dataTextStyle) .arg(curDir->getName()), tr("<span style=\"%1\">WARNING</span>: " "VNote will delete the whole directory " "<span style=\"%2\">%3</span>." "Deleted files could be found in the recycle bin " "of this folder.<br>" "The operation is IRREVERSIBLE!") .arg(g_config->c_warningTextStyle) .arg(g_config->c_dataTextStyle) .arg(curDir->fetchPath()), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok, this, MessageBoxType::Danger); if (ret == QMessageBox::Ok) { int nrDeleted = 1; m_editArea->closeFile(curDir, true); // Remove the item from the tree. delete curItem; QString msg; QString dirName = curDir->getName(); QString dirPath = curDir->fetchPath(); if (!VDirectory::deleteDirectory(curDir, false, &msg)) { VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to delete folder <span style=\"%1\">%2</span>.<br>" "Please check <span style=\"%1\">%3</span> and manually delete it.") .arg(g_config->c_dataTextStyle) .arg(dirName) .arg(dirPath), msg, QMessageBox::Ok, QMessageBox::Ok, this); } else { g_mainWin->showStatusMessage(tr("%1 %2 deleted") .arg(nrDeleted) .arg(nrDeleted > 1 ? tr("folders") : tr("folder"))); } } }
void VDirectoryTree::copySelectedDirectories(bool p_isCut) { QList<QTreeWidgetItem *> items = selectedItems(); if (items.isEmpty()) { return; } QJsonArray dirs; for (int i = 0; i < items.size(); ++i) { VDirectory *dir = getVDirectory(items[i]); dirs.append(dir->fetchPath()); } QJsonObject clip; clip[ClipboardConfig::c_magic] = getNewMagic(); clip[ClipboardConfig::c_type] = (int)ClipboardOpType::CopyDir; clip[ClipboardConfig::c_isCut] = p_isCut; clip[ClipboardConfig::c_dirs] = dirs; QClipboard *clipboard = QApplication::clipboard(); clipboard->setText(QJsonDocument(clip).toJson(QJsonDocument::Compact)); qDebug() << "copied directories info" << clipboard->text(); int cnt = dirs.size(); g_mainWin->showStatusMessage(tr("%1 %2 %3") .arg(cnt) .arg(cnt > 1 ? tr("folders") : tr("folder")) .arg(p_isCut ? tr("cut") : tr("copied"))); }
void VListFolderUE::processCommand(int p_id, const QString &p_cmd) { Q_UNUSED(p_id); init(); QString folderPath = m_folderPath; if (folderPath.isEmpty()) { VDirectory *dir = g_mainWin->getDirectoryTree()->currentDirectory(); folderPath = dir->fetchPath(); } listFolder(folderPath, p_cmd); m_listWidget->updateGeometry(); emit widgetUpdated(); emit stateUpdated(State::Success); }
void VDirectoryTree::buildSubTree(QTreeWidgetItem *p_parent, int p_depth) { if (p_depth == 0) { return; } Q_ASSERT(p_parent); VDirectory *dir = getVDirectory(p_parent); if (!dir->open()) { VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to open folder <span style=\"%1\">%2</span>.") .arg(g_config->c_dataTextStyle) .arg(dir->getName()), tr("Please check if directory <span style=\"%1\">%2</span> exists.") .arg(g_config->c_dataTextStyle) .arg(dir->fetchPath()), QMessageBox::Ok, QMessageBox::Ok, this); return; } if (p_parent->childCount() > 0) { // This directory has been built before. Try its children directly. int cnt = p_parent->childCount(); for (int i = 0; i < cnt; ++i) { buildSubTree(p_parent->child(i), p_depth -1); } } else { const QVector<VDirectory *> &subDirs = dir->getSubDirs(); for (int i = 0; i < subDirs.size(); ++i) { VDirectory *subDir = subDirs[i]; QTreeWidgetItem *item = new QTreeWidgetItem(p_parent); fillTreeItem(item, subDir); buildSubTree(item, p_depth - 1); } } if (dir->isExpanded()) { expandItem(p_parent); } }
void VDirectoryTree::newSubDirectory() { if (!m_notebook) { return; } QTreeWidgetItem *curItem = currentItem(); if (!curItem) { return; } VDirectory *curDir = getVDirectory(curItem); QString info = tr("Create a subfolder in <span style=\"%1\">%2</span>.") .arg(g_config->c_dataTextStyle) .arg(curDir->getName()); QString defaultName("new_folder"); defaultName = VUtils::getDirNameWithSequence(curDir->fetchPath(), defaultName); VNewDirDialog dialog(tr("Create Folder"), info, defaultName, curDir, this); if (dialog.exec() == QDialog::Accepted) { QString name = dialog.getNameInput(); QString msg; VDirectory *subDir = curDir->createSubDirectory(name, &msg); if (!subDir) { VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to create subfolder <span style=\"%1\">%2</span>.") .arg(g_config->c_dataTextStyle) .arg(name), msg, QMessageBox::Ok, QMessageBox::Ok, this); return; } updateItemDirectChildren(curItem); locateDirectory(subDir); } }
bool VNoteFile::rename(const QString &p_name) { if (m_name == p_name) { return true; } QString oldName = m_name; VDirectory *dir = getDirectory(); Q_ASSERT(dir); // Rename it in disk. QDir diskDir(dir->fetchPath()); if (!diskDir.rename(m_name, p_name)) { qWarning() << "fail to rename file" << m_name << "to" << p_name << "in disk"; return false; } m_name = p_name; // Update parent directory's config file. if (!dir->updateFileConfig(this)) { m_name = oldName; diskDir.rename(p_name, m_name); return false; } // Can't not change doc type. Q_ASSERT(m_docType == DocType::Unknown || m_docType == VUtils::docTypeFromName(m_name)); m_docType = VUtils::docTypeFromName(m_name); qDebug() << "file renamed from" << oldName << "to" << m_name; return true; }
void VDirectoryTree::newRootDirectory() { if (!m_notebook) { return; } VDirectory *rootDir = m_notebook->getRootDir(); QString info = tr("Create a root folder in notebook <span style=\"%1\">%2</span>.") .arg(g_config->c_dataTextStyle) .arg(m_notebook->getName()); QString defaultName("new_folder"); defaultName = VUtils::getDirNameWithSequence(rootDir->fetchPath(), defaultName); VNewDirDialog dialog(tr("Create Root Folder"), info, defaultName, rootDir, this); if (dialog.exec() == QDialog::Accepted) { QString name = dialog.getNameInput(); QString msg; VDirectory *dir = rootDir->createSubDirectory(name, &msg); if (!dir) { VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to create root folder <span style=\"%1\">%2</span>.") .arg(g_config->c_dataTextStyle) .arg(name), msg, QMessageBox::Ok, QMessageBox::Ok, this); return; } updateItemDirectChildren(NULL); locateDirectory(dir); } }
bool VListFolderUE::listFolder(const QString &p_path, const QString &p_cmd) { VDirectory *dir = g_vnote->getInternalDirectory(p_path); if (!dir) { // See if it is a notebook. VNotebook *nb = g_vnote->getNotebook(p_path); if (!nb) { return false; } dir = nb->getRootDir(); } m_panel->clearTitle(); m_listWidget->clearAll(); m_data.clear(); m_currentFolderPath = dir->fetchPath(); m_panel->setTitle(m_currentFolderPath); if (!dir->open()) { return true; } if (p_cmd.isEmpty()) { // List the content. for (auto const & it : dir->getSubDirs()) { QSharedPointer<VSearchResultItem> item(new VSearchResultItem(VSearchResultItem::Folder, VSearchResultItem::LineNumber, it->getName(), it->fetchPath())); addResultItem(item); } for (auto const & file : dir->getFiles()) { QSharedPointer<VSearchResultItem> item(new VSearchResultItem(VSearchResultItem::Note, VSearchResultItem::LineNumber, file->getName(), file->fetchPath())); addResultItem(item); } } else { // Search the content. VSearchConfig config(VSearchConfig::CurrentFolder, VSearchConfig::Name, VSearchConfig::Note | VSearchConfig::Folder, VSearchConfig::Internal, VSearchConfig::NoneOption, p_cmd, QString()); for (auto const & it : dir->getSubDirs()) { QString name = it->getName(); if (!config.m_token.matched(name)) { continue; } QSharedPointer<VSearchResultItem> item(new VSearchResultItem(VSearchResultItem::Folder, VSearchResultItem::LineNumber, name, it->fetchPath())); addResultItem(item); } for (auto const & file : dir->getFiles()) { QString name = file->getName(); if (!config.m_token.matched(name)) { continue; } QSharedPointer<VSearchResultItem> item(new VSearchResultItem(VSearchResultItem::Note, VSearchResultItem::LineNumber, name, file->fetchPath())); addResultItem(item); } } return true; }
void VDirectoryTree::pasteDirectories(VDirectory *p_destDir, const QVector<QString> &p_dirs, bool p_isCut) { if (!p_destDir || p_dirs.isEmpty()) { return; } int nrPasted = 0; for (int i = 0; i < p_dirs.size(); ++i) { VDirectory *dir = g_vnote->getInternalDirectory(p_dirs[i]); if (!dir) { qWarning() << "Copied dir is not an internal folder" << p_dirs[i]; VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to paste folder <span style=\"%1\">%2</span>.") .arg(g_config->c_dataTextStyle) .arg(p_dirs[i]), tr("VNote could not find this folder in any notebook."), QMessageBox::Ok, QMessageBox::Ok, this); continue; } if (dir == p_destDir) { continue; } QString dirName = dir->getName(); VDirectory *paDir = dir->getParentDirectory(); if (paDir == p_destDir) { if (p_isCut) { continue; } // Copy and paste in the same folder. // Rename it to xxx_copy. dirName = VUtils::generateCopiedDirName(paDir->fetchPath(), dirName); } else { // Rename it to xxx_copy if needed. dirName = VUtils::generateCopiedDirName(p_destDir->fetchPath(), dirName); } QString msg; VDirectory *destDir = NULL; bool ret = VDirectory::copyDirectory(p_destDir, dirName, dir, p_isCut, &destDir, &msg); if (!ret) { VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to copy folder <span style=\"%1\">%2</span>.") .arg(g_config->c_dataTextStyle) .arg(p_dirs[i]), msg, QMessageBox::Ok, QMessageBox::Ok, this); } if (destDir) { ++nrPasted; // Update QTreeWidget. bool isWidget; QTreeWidgetItem *destItem = findVDirectory(p_destDir, &isWidget); if (destItem || isWidget) { updateItemDirectChildren(destItem); } if (p_isCut) { QTreeWidgetItem *srcItem = findVDirectory(paDir, &isWidget); if (srcItem || isWidget) { updateItemDirectChildren(srcItem); } } // Broadcast this update emit directoryUpdated(destDir, p_isCut ? UpdateAction::Moved : UpdateAction::InfoChanged); } } qDebug() << "pasted" << nrPasted << "directories"; if (nrPasted > 0) { g_mainWin->showStatusMessage(tr("%1 %2 pasted") .arg(nrPasted) .arg(nrPasted > 1 ? tr("folders") : tr("folder"))); } getNewMagic(); }