示例#1
0
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")));
        }
    }
}
示例#2
0
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);
    }
}
示例#3
0
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);
    }
}
示例#4
0
void VDirectoryTree::editDirectoryInfo()
{
    QTreeWidgetItem *curItem = currentItem();
    if (!curItem) {
        return;
    }

    VDirectory *curDir = getVDirectory(curItem);
    QString curName = curDir->getName();

    VDirInfoDialog dialog(tr("Folder Information"),
                          "",
                          curDir,
                          curDir->getParentDirectory(),
                          this);
    if (dialog.exec() == QDialog::Accepted) {
        QString name = dialog.getNameInput();
        if (name == curName) {
            return;
        }

        if (!curDir->rename(name)) {
            VUtils::showMessage(QMessageBox::Warning,
                                tr("Warning"),
                                tr("Fail to rename folder <span style=\"%1\">%2</span>.")
                                  .arg(g_config->c_dataTextStyle)
                                  .arg(curName),
                                "",
                                QMessageBox::Ok,
                                QMessageBox::Ok,
                                this);
            return;
        }

        fillTreeItem(curItem, curDir);

        emit directoryUpdated(curDir, UpdateAction::InfoChanged);
    }
}
示例#5
0
void VSearcher::startSearch()
{
    if (m_inSearch) {
        return;
    }

    m_searchBtn->setEnabled(false);
    setProgressVisible(true);
    m_results->clearResults();
    m_askedToStop = false;
    m_inSearch = true;
    m_consoleEdit->clear();
    appendLogLine(tr("Search started."));

    updateItemToComboBox(m_keywordCB);
    updateItemToComboBox(m_filePatternCB);

    m_search.clear();

    QSharedPointer<VSearchConfig> config(new VSearchConfig(m_searchScopeCB->currentData().toInt(),
                                                           m_searchObjectCB->currentData().toInt(),
                                                           m_searchTargetCB->currentData().toInt(),
                                                           m_searchEngineCB->currentData().toInt(),
                                                           getSearchOption(),
                                                           m_keywordCB->currentText(),
                                                           m_filePatternCB->currentText()));
    m_search.setConfig(config);

    g_config->setSearchOptions(config->toConfig());

    QSharedPointer<VSearchResult> result;
    switch (config->m_scope) {
    case VSearchConfig::CurrentNote:
    {
        QVector<VFile *> files;
        files.append(g_mainWin->getCurrentFile());
        if (files[0]) {
            QString msg(tr("Search current note %1.").arg(files[0]->getName()));
            appendLogLine(msg);
            showMessage(msg);
        }

        result = m_search.search(files);
        break;
    }

    case VSearchConfig::OpenedNotes:
    {
        QVector<VEditTabInfo> tabs = g_mainWin->getEditArea()->getAllTabsInfo();
        QVector<VFile *> files;
        files.reserve(tabs.size());
        for (auto const & ta : tabs) {
            files.append(ta.m_editTab->getFile());
        }

        result = m_search.search(files);
        break;
    }

    case VSearchConfig::CurrentFolder:
    {
        VDirectory *dir = g_mainWin->getDirectoryTree()->currentDirectory();
        if (dir) {
            QString msg(tr("Search current folder %1.").arg(dir->getName()));
            appendLogLine(msg);
            showMessage(msg);
        }

        result = m_search.search(dir);
        break;
    }

    case VSearchConfig::CurrentNotebook:
    {
        QVector<VNotebook *> notebooks;
        notebooks.append(g_mainWin->getNotebookSelector()->currentNotebook());
        if (notebooks[0]) {
            QString msg(tr("Search current notebook %1.").arg(notebooks[0]->getName()));
            appendLogLine(msg);
            showMessage(msg);
        }

        result = m_search.search(notebooks);
        break;
    }

    case VSearchConfig::AllNotebooks:
    {
        const QVector<VNotebook *> &notebooks = g_vnote->getNotebooks();
        result = m_search.search(notebooks);
        break;
    }

    case VSearchConfig::ExplorerDirectory:
    {
        QString rootDirectory = g_mainWin->getExplorer()->getRootDirectory();
        if (!rootDirectory.isEmpty()) {
            QString msg(tr("Search Explorer directory %1.").arg(rootDirectory));
            appendLogLine(msg);
            showMessage(msg);
        }

        result = m_search.search(rootDirectory);
        break;
    }

    default:
        break;
    }

    handleSearchFinished(result);
}
示例#6
0
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();
}
示例#7
0
void VDirectoryTree::reloadFromDisk()
{
    if (!m_notebook) {
        return;
    }

    QString msg;
    QString info;
    VDirectory *curDir = NULL;
    QTreeWidgetItem *curItem = currentItem();
    if (curItem) {
        // Reload current directory.
        curDir = getVDirectory(curItem);
        info = tr("Are you sure to reload folder <span style=\"%1\">%2</span>?")
                 .arg(g_config->c_dataTextStyle).arg(curDir->getName());
        msg = tr("Folder %1 reloaded from disk").arg(curDir->getName());
    } else {
        // Reload notebook.
        info = tr("Are you sure to reload notebook <span style=\"%1\">%2</span>?")
                 .arg(g_config->c_dataTextStyle).arg(m_notebook->getName());
        msg = tr("Notebook %1 reloaded from disk").arg(m_notebook->getName());
    }

    if (g_config->getConfirmReloadFolder()) {
        int ret = VUtils::showMessage(QMessageBox::Information, tr("Information"),
                                      info,
                                      tr("VNote will close all the related notes before reload."),
                                      QMessageBox::Ok | QMessageBox::YesToAll | QMessageBox::Cancel,
                                      QMessageBox::Ok,
                                      this);
        switch (ret) {
        case QMessageBox::YesToAll:
            g_config->setConfirmReloadFolder(false);
            // Fall through.

        case QMessageBox::Ok:
            break;

        case QMessageBox::Cancel:
            return;

        default:
            return;
        }
    }

    m_notebookCurrentDirMap.remove(m_notebook);

    if (curItem) {
        if (!m_editArea->closeFile(curDir, false)) {
            return;
        }

        setCurrentItem(NULL);

        curItem->setExpanded(false);
        curDir->setExpanded(false);

        curDir->close();

        // Remove all its children.
        QList<QTreeWidgetItem *> children = curItem->takeChildren();
        for (int i = 0; i < children.size(); ++i) {
            delete children[i];
        }

        buildSubTree(curItem, 1);

        setCurrentItem(curItem);
    } else {
        if (!m_editArea->closeFile(m_notebook, false)) {
            return;
        }

        m_notebook->close();

        if (!m_notebook->open()) {
            VUtils::showMessage(QMessageBox::Warning, tr("Warning"),
                                tr("Fail to open notebook <span style=\"%1\">%2</span>.")
                                  .arg(g_config->c_dataTextStyle).arg(m_notebook->getName()),
                                tr("Please check if path <span style=\"%1\">%2</span> exists.")
                                  .arg(g_config->c_dataTextStyle).arg(m_notebook->getPath()),
                                QMessageBox::Ok, QMessageBox::Ok, this);
            clear();
            return;
        }

        updateDirectoryTree();
    }

    if (!msg.isEmpty()) {
        g_mainWin->showStatusMessage(msg);
    }
}