Пример #1
0
void VAttachmentList::deleteSelectedItems()
{
    QVector<ConfirmItemInfo> items;
    const QList<QListWidgetItem *> selectedItems = m_attachmentList->selectedItems();

    if (selectedItems.isEmpty()) {
        return;
    }

    for (auto const & item : selectedItems) {
        items.push_back(ConfirmItemInfo(item->text(),
                                        item->text(),
                                        "",
                                        NULL));
    }

    QString text = tr("Are you sure to delete these attachments of note "
                      "<span style=\"%1\">%2</span>?")
                     .arg(g_config->c_dataTextStyle).arg(m_file->getName());

    QString info = tr("Deleted files could be found in the recycle "
                      "bin of this note.<br>"
                      "Click \"Cancel\" to leave them untouched.");

    VConfirmDeletionDialog dialog(tr("Confirm Deleting Attachments"),
                                  text,
                                  info,
                                  items,
                                  false,
                                  false,
                                  false,
                                  g_mainWin);
    if (dialog.exec()) {
        items = dialog.getConfirmedItems();

        QVector<QString> names;
        for (auto const & item : items) {
            names.push_back(item.m_name);
        }

        if (!m_file->deleteAttachments(names)) {
            VUtils::showMessage(QMessageBox::Warning,
                                tr("Warning"),
                                tr("Fail to delete attachments of note <span style=\"%1\">%2</span>.")
                                  .arg(g_config->c_dataTextStyle)
                                  .arg(m_file->getName()),
                                tr("Please check the attachments folder and "
                                   "maintain the configuration file manually."),
                                QMessageBox::Ok,
                                QMessageBox::Ok,
                                g_mainWin);
        }

        updateButtonState();

        updateContent();
    }
}
Пример #2
0
void VAttachmentList::checkAttachments()
{
    if (!m_file) {
        return;
    }

    QVector<QString> missingAttas = m_file->checkAttachments();
    if (missingAttas.isEmpty()) {
        return;
    }

    QVector<ConfirmItemInfo> items;
    for (auto const & atta : missingAttas) {
        items.push_back(ConfirmItemInfo(atta,
                                        atta,
                                        "",
                                        NULL));
    }

    QString text = tr("VNote detects that these attachments of note "
                      "<span style=\"%1\">%2</span> are missing in disk. "
                      "Would you like to remove them from the note?")
                     .arg(g_config->c_dataTextStyle)
                     .arg(m_file->getName());

    QString info = tr("Click \"Cancel\" to leave them untouched.");

    VConfirmDeletionDialog dialog(tr("Confirm Deleting Attachments"),
                                  text,
                                  info,
                                  items,
                                  false,
                                  false,
                                  false,
                                  g_mainWin);
    if (dialog.exec()) {
        items = dialog.getConfirmedItems();

        QVector<QString> names;
        for (auto const & item : items) {
            names.push_back(item.m_name);
        }

        if (!m_file->deleteAttachments(names, true)) {
            VUtils::showMessage(QMessageBox::Warning,
                                tr("Warning"),
                                tr("Fail to delete attachments of note <span style=\"%1\">%2</span>.")
                                  .arg(g_config->c_dataTextStyle)
                                  .arg(m_file->getName()),
                                tr("Please check the attachments folder and "
                                   "maintain the configuration file manually."),
                                QMessageBox::Ok,
                                QMessageBox::Ok,
                                g_mainWin);
        }

        updateButtonState();

        updateContent();
    }
}
Пример #3
0
void VMdEditor::clearUnusedImages()
{
    QVector<ImageLink> images = VUtils::fetchImagesFromMarkdownFile(m_file,
                                                                    ImageLink::LocalRelativeInternal);

    QSet<QString> unusedImages;

    if (!m_insertedImages.isEmpty()) {
        for (int i = 0; i < m_insertedImages.size(); ++i) {
            const ImageLink &link = m_insertedImages[i];

            if (link.m_type != ImageLink::LocalRelativeInternal) {
                continue;
            }

            int j;
            for (j = 0; j < images.size(); ++j) {
                if (VUtils::equalPath(link.m_path, images[j].m_path)) {
                    break;
                }
            }

            // This inserted image is no longer in the file.
            if (j == images.size()) {
                unusedImages.insert(link.m_path);
            }
        }

        m_insertedImages.clear();
    }

    for (int i = 0; i < m_initImages.size(); ++i) {
        const ImageLink &link = m_initImages[i];

        V_ASSERT(link.m_type == ImageLink::LocalRelativeInternal);

        int j;
        for (j = 0; j < images.size(); ++j) {
            if (VUtils::equalPath(link.m_path, images[j].m_path)) {
                break;
            }
        }

        // Original local relative image is no longer in the file.
        if (j == images.size()) {
            unusedImages.insert(link.m_path);
        }
    }

    if (!unusedImages.isEmpty()) {
        if (g_config->getConfirmImagesCleanUp()) {
            QVector<ConfirmItemInfo> items;
            for (auto const & img : unusedImages) {
                items.push_back(ConfirmItemInfo(img,
                                                img,
                                                img,
                                                NULL));

            }

            QString text = tr("Following images seems not to be used in this note anymore. "
                              "Please confirm the deletion of these images.");

            QString info = tr("Deleted files could be found in the recycle "
                              "bin of this note.<br>"
                              "Click \"Cancel\" to leave them untouched.");

            VConfirmDeletionDialog dialog(tr("Confirm Cleaning Up Unused Images"),
                                          text,
                                          info,
                                          items,
                                          true,
                                          true,
                                          true,
                                          this);

            unusedImages.clear();
            if (dialog.exec()) {
                items = dialog.getConfirmedItems();
                g_config->setConfirmImagesCleanUp(dialog.getAskAgainEnabled());

                for (auto const & item : items) {
                    unusedImages.insert(item.m_name);
                }
            }
        }

        for (auto const & item : unusedImages) {
            bool ret = false;
            if (m_file->getType() == FileType::Note) {
                const VNoteFile *tmpFile = dynamic_cast<const VNoteFile *>((VFile *)m_file);
                ret = VUtils::deleteFile(tmpFile->getNotebook(), item, false);
            } else if (m_file->getType() == FileType::Orphan) {
                const VOrphanFile *tmpFile = dynamic_cast<const VOrphanFile *>((VFile *)m_file);
                ret = VUtils::deleteFile(tmpFile, item, false);
            } else {
                Q_ASSERT(false);
            }

            if (!ret) {
                qWarning() << "fail to delete unused original image" << item;
            } else {
                qDebug() << "delete unused image" << item;
            }
        }
    }

    m_initImages.clear();
}