コード例 #1
0
ファイル: vnotefile.cpp プロジェクト: vscanf/vnote
bool VNoteFile::deleteFile(VNoteFile *p_file, QString *p_errMsg)
{
    Q_ASSERT(!p_file->isOpened());

    bool ret = true;
    QString name = p_file->getName();
    QString path = p_file->fetchPath();

    if (!p_file->deleteFile(p_errMsg)) {
        qWarning() << "fail to delete file" << name << path;
        ret = false;
    }

    VDirectory *dir = p_file->getDirectory();
    Q_ASSERT(dir);
    if (!dir->removeFile(p_file)) {
        qWarning() << "fail to remove file from directory" << name << path;
        VUtils::addErrMsg(p_errMsg, tr("Fail to remove the note from the folder configuration."));
        ret = false;
    }

    delete p_file;

    return ret;
}
コード例 #2
0
ファイル: vnotefile.cpp プロジェクト: vscanf/vnote
bool VNoteFile::copyFile(VDirectory *p_destDir,
                         const QString &p_destName,
                         VNoteFile *p_file,
                         bool p_isCut,
                         VNoteFile **p_targetFile,
                         QString *p_errMsg)
{
    bool ret = true;
    *p_targetFile = NULL;
    int nrImageCopied = 0;
    bool attachmentFolderCopied = false;

    QString srcPath = QDir::cleanPath(p_file->fetchPath());
    QString destPath = QDir::cleanPath(QDir(p_destDir->fetchPath()).filePath(p_destName));
    if (VUtils::equalPath(srcPath, destPath)) {
        *p_targetFile = p_file;
        return false;
    }

    if (!p_destDir->isOpened()) {
        VUtils::addErrMsg(p_errMsg, tr("Fail to open target folder."));
        return false;
    }

    QString opStr = p_isCut ? tr("cut") : tr("copy");
    VDirectory *srcDir = p_file->getDirectory();
    DocType docType = p_file->getDocType();

    Q_ASSERT(srcDir->isOpened());
    Q_ASSERT(docType == VUtils::docTypeFromName(p_destName));

    // Images to be copied.
    QVector<ImageLink> images;
    if (docType == DocType::Markdown) {
        images = VUtils::fetchImagesFromMarkdownFile(p_file,
                                                     ImageLink::LocalRelativeInternal);
    }

    // Attachments to be copied.
    QString attaFolder = p_file->getAttachmentFolder();
    QString attaFolderPath;
    if (!attaFolder.isEmpty()) {
        attaFolderPath = p_file->fetchAttachmentFolderPath();
    }

    // Copy the note file.
    if (!VUtils::copyFile(srcPath, destPath, p_isCut)) {
        VUtils::addErrMsg(p_errMsg, tr("Fail to %1 the note file.").arg(opStr));
        qWarning() << "fail to" << opStr << "the note file" << srcPath << "to" << destPath;
        return false;
    }

    // Add file to VDirectory.
    VNoteFile *destFile = NULL;
    if (p_isCut) {
        srcDir->removeFile(p_file);
        p_file->setName(p_destName);
        if (p_destDir->addFile(p_file, -1)) {
            destFile = p_file;
        } else {
            destFile = NULL;
        }
    } else {
        destFile = p_destDir->addFile(p_destName, -1);
    }

    if (!destFile) {
        VUtils::addErrMsg(p_errMsg, tr("Fail to add the note to target folder's configuration."));
        return false;
    }

    // Copy images.
    if (!copyInternalImages(images,
                            destFile->fetchBasePath(),
                            p_isCut,
                            &nrImageCopied,
                            p_errMsg)) {
        ret = false;
    }

    // Copy attachment folder.
    if (!attaFolderPath.isEmpty()) {
        QDir dir(destFile->fetchBasePath());
        QString folderPath = dir.filePath(destFile->getNotebook()->getAttachmentFolder());
        attaFolder = VUtils::getDirNameWithSequence(folderPath, attaFolder);
        folderPath = QDir(folderPath).filePath(attaFolder);

        // Copy attaFolderPath to folderPath.
        if (!VUtils::copyDirectory(attaFolderPath, folderPath, p_isCut)) {
            VUtils::addErrMsg(p_errMsg, tr("Fail to %1 attachments folder %2 to %3. "
                                           "Please manually maintain it.")
                                          .arg(opStr).arg(attaFolderPath).arg(folderPath));
            QVector<VAttachment> emptyAttas;
            destFile->setAttachments(emptyAttas);
            ret = false;
        } else {
            attachmentFolderCopied = true;

            destFile->setAttachmentFolder(attaFolder);
            if (!p_isCut) {
                destFile->setAttachments(p_file->getAttachments());
            }
        }

        if (!p_destDir->updateFileConfig(destFile)) {
            VUtils::addErrMsg(p_errMsg, tr("Fail to update configuration of note %1.")
                                          .arg(destFile->fetchPath()));
            ret = false;
        }
    }

    qDebug() << "copyFile:" << p_file << "to" << destFile
             << "copied_images:" << nrImageCopied
             << "copied_attachments:" << attachmentFolderCopied;

    *p_targetFile = destFile;
    return ret;
}