Esempio n. 1
0
/**
 * Renames a note
 */
bool Note::renameNoteFile(QString newName) {
    // cleanup not allowed characters characters
    newName = cleanupFileName(newName);

    // add the old file suffix to the name
    QString newFileName = newName + "." + fileNameSuffix();

    // check if name has really changed
    if (name == newName) {
        return false;
    }

    // check if name already exists
    Note existingNote = Note::fetchByName(newName);
    if (existingNote.isFetched() && (existingNote.getId() != id)) {
        return false;
    }

    // get the note file to rename it
    QFile file(fullNoteFilePath());

    // store the new note file name
    fileName = newFileName;
    name = newName;
    store();

    // rename the note file name
    return file.rename(fullNoteFilePath());
}
Esempio n. 2
0
/**
 * @brief Copies a note to an other path
 *
 * @param destinationPath
 * @return bool
 */
bool Note::copy(QString destinationPath) {
    QDir d;
    if (this->fileExists() && (d.exists(destinationPath))) {
        QFile file(fullNoteFilePath());
        QString destinationFileName =
                destinationPath + QDir::separator() + this->fileName;

        if (d.exists(destinationFileName)) {
            qDebug() << destinationFileName << "already exists!";

            // find a new filename for the note
            QDateTime currentDateTime = QDateTime::currentDateTime();
            destinationFileName =
                    destinationPath + QDir::separator() + this->name + " " +
                    currentDateTime.toString(Qt::ISODate).replace(":", "_") +
                    "." + defaultNoteFileExtension();

            qDebug() << "New file name:" << destinationFileName;
        }

        return file.copy(destinationFileName);
    }

    return false;
}
Esempio n. 3
0
/**
 * Returns the full url of the note file
 */
QUrl Note::fullNoteFileUrl() {
    QString windowsSlash = "";

#ifdef Q_OS_WIN32
    // we need an other slash for Windows
    windowsSlash = "/";
#endif

    return QUrl("file://" + windowsSlash + fullNoteFilePath());
}
Esempio n. 4
0
//
// remove the file of the note
//
bool Note::removeNoteFile() {
    if (this->fileExists()) {
        QFile file(fullNoteFilePath());
        qDebug() << __func__ << " - 'this->fileName': " << this->fileName;
        qDebug() << __func__ << " - 'file': " << file.fileName();
        return file.remove();
    }

    return false;
}
Esempio n. 5
0
bool Note::storeNoteTextFileToDisk() {

    // checks if filename has to be changed (and change it if needed)
    this->handleNoteTextFileName();

    QFile file( fullNoteFilePath( this->fileName ) );
    bool fileExists = this->fileExists();

    qDebug() << "storing note file: "  << this->fileName;

    if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
    {
//        QMessageBox::critical( 0, "Can not store note!", "Can not store note <strong>" + this->name + "</strong><br /><br />" + file.errorString() );
        qDebug() << file.errorString();
        return false;
    }

    // transform all types of newline to \n (maybe the ownCloud-sync works better then)
    QString text = this->noteText;
    text.replace( QRegExp( "(\\r\\n)|(\\n\\r)|\\r|\\n" ), "\n" );

//    diff_match_patch *diff = new diff_match_patch();
//    QList<Diff> diffList = diff->diff_main( this->noteText, text );

//    QString html = diff->diff_prettyHtml( diffList );
//    diff->diff_cleanupSemantic( diffList );
//    qDebug() << __func__ << " - 'diffList': " << diffList[0].toString();
//    qDebug() << __func__ << " - 'html': " << html;

    QTextStream out( &file );
    out.setCodec("UTF-8");
    out << text;
    file.flush();
    file.close();

    this->hasDirtyData = false;
    this->fileLastModified = QDateTime::currentDateTime();

    if ( !fileExists ) {
        this->fileCreated = this->fileLastModified;
    }

    return this->store();
}
Esempio n. 6
0
bool Note::updateNoteTextFromDisk() {
    QFile file( fullNoteFilePath( this->fileName ) );

    if ( !file.open( QIODevice::ReadOnly ) )
    {
        qDebug() << file.errorString();
        return false;
    }

    QTextStream in( &file );
    in.setCodec("UTF-8");
    this->noteText = in.readAll();
    file.close();

    // strangly it sometimes gets null
    if ( this->noteText.isNull() ) this->noteText = "";

    return true;
}
Esempio n. 7
0
//
// checks if file of note exists in the filesystem
//
bool Note::fileExists() {
    QFile file( fullNoteFilePath( this->fileName ) );
    return file.exists();
}
Esempio n. 8
0
/**
 * Returns the full url of the note file
 */
QUrl Note::fullNoteFileUrl() {
    return QUrl("file://" + fullNoteFilePath());
}
Esempio n. 9
0
/**
 * Stores a note text file to disk
 * The file name will be changed if needed
 */
bool Note::storeNoteTextFileToDisk() {
    QString oldName = name;

    if (allowDifferentFileName()) {
        // check if a QML function wants to set an other note file name and
        // modify it accordingly
        modifyNoteTextFileNameFromQMLHook();
    } else {
        // checks if filename has to be changed (and change it if needed)
        handleNoteTextFileName();
    }

    QString newName = name;

    // assign the tags to the new name if the name has changed
    if (oldName != newName) {
        // TODO(pbek): we need to heed note subfolders here
        Tag::renameNoteFileNamesOfLinks(oldName, newName);
    }

    QFile file(fullNoteFilePath());
    bool fileExists = this->fileExists();

    qDebug() << "storing note file: " << this->fileName;

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qCritical() << file.errorString();
        return false;
    }

    // if we find a decrypted text to encrypt, then we attempt encrypt it
    if (!decryptedNoteText.isEmpty()) {
        noteText = decryptedNoteText;
        encryptNoteText();
        decryptedNoteText = "";
    }

    // transform all types of newline to \n
    // (maybe the ownCloud-sync works better then)
    QString text = this->noteText;
    text.replace(QRegExp("(\\r\\n)|(\\n\\r)|\\r|\\n"), "\n");

//    diff_match_patch *diff = new diff_match_patch();
//    QList<Diff> diffList = diff->diff_main( this->noteText, text );

//    QString html = diff->diff_prettyHtml( diffList );
//    diff->diff_cleanupSemantic( diffList );
//    qDebug() << __func__ << " - 'diffList': " << diffList[0].toString();
//    qDebug() << __func__ << " - 'html': " << html;

    QTextStream out(&file);
    out.setCodec("UTF-8");
    out << text;
    file.flush();
    file.close();

    this->hasDirtyData = false;
    this->fileLastModified = QDateTime::currentDateTime();

    if (!fileExists) {
        this->fileCreated = this->fileLastModified;
    }

    return this->store();
}
Esempio n. 10
0
//
// checks if file of note exists in the filesystem
//
bool Note::fileExists() {
    QFile file(fullNoteFilePath());
    return file.exists();
}