/** * Checks if the filename has to be changed * Generates a new name and filename and removes the old file * (the new file is not stored to a note text file!) */ void Note::handleNoteTextFileName() { // split the text into a string list QStringList noteTextLines = this->noteText.split( QRegExp("(\\r\\n)|(\\n\\r)|\\r|\\n")); // do nothing if there is no text if (noteTextLines.count() == 0) return; QString name = noteTextLines[0]; // do nothing if the first line is empty if (name == "") return; // remove a leading "# " for markdown headlines name.remove(QRegularExpression("^#\\s")); // cleanup additional characters name = cleanupFileName(name); // check if name has changed if (name != this->name) { qDebug() << __func__ << " - 'name' was changed: " << name; QString fileName = name + "." + defaultNoteFileExtension(); // check if note with this filename already exists Note note = Note::fetchByFileName(fileName); if (note.id > 0) { // find new filename for the note (not very safe yet) QDateTime currentDateTime = QDateTime::currentDateTime(); name += " " + currentDateTime .toString(Qt::ISODate).replace(":", "_"); } // remove old note file // TODO(pbek): note doesn't seem to be removed very often this->removeNoteFile(); // update the first line of the note text // TODO(pbek): UI has to be updated too then! noteTextLines[0] = name; this->noteText = noteTextLines.join("\n"); // store new name and filename this->name = name; this->fileName = name + "." + defaultNoteFileExtension(); this->store(); } qDebug() << __func__ << " - 'name': " << name; qDebug() << __func__ << " - 'this->id': " << this->id; }
/** * @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(getFullNoteFilePathForFile(this->fileName)); 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; }
// // inserts or updates a note object in the database // bool Note::store() { QSqlDatabase db = QSqlDatabase::database("memory"); QSqlQuery query(db); if (this->fileName == "") { this->fileName = this->name + "." + defaultNoteFileExtension(); } if (this->id > 0) { query.prepare("UPDATE note SET " "name = :name," "file_name = :file_name," "note_text = :note_text," "decrypted_note_text = :decrypted_note_text," "has_dirty_data = :has_dirty_data, " "file_last_modified = :file_last_modified," "file_created = :file_created," "crypto_key = :crypto_key," "crypto_password = :crypto_password," "modified = :modified " "WHERE id = :id"); query.bindValue(":id", this->id); } else { query.prepare("INSERT INTO note" "(name, file_name, note_text, has_dirty_data," "file_last_modified, file_created, crypto_key," "modified, crypto_password, decrypted_note_text) " "VALUES (:name, :file_name, :note_text," ":has_dirty_data, :file_last_modified," ":file_created, :crypto_key, :modified," ":crypto_password, :decrypted_note_text)"); } QDateTime modified = QDateTime::currentDateTime(); query.bindValue(":name", this->name); query.bindValue(":file_name", this->fileName); query.bindValue(":note_text", this->noteText); query.bindValue(":decrypted_note_text", this->decryptedNoteText); query.bindValue(":has_dirty_data", this->hasDirtyData ? 1 : 0); query.bindValue(":file_created", this->fileCreated); query.bindValue(":file_last_modified", this->fileLastModified); query.bindValue(":crypto_key", this->cryptoKey); query.bindValue(":crypto_password", this->cryptoPassword); query.bindValue(":modified", modified); // on error if (!query.exec()) { qWarning() << __func__ << ": " << query.lastError(); return false; } else if (this->id == 0) { // on insert this->id = query.lastInsertId().toInt(); } this->modified = modified; return true; }