src/writely/WriterUI.cpp
/**
 * Save Document
 * @param   filePath           The origininal path of document
 * @param   documentTitle      Document's title. The saved file might be renamed if the title is changed.
 * @param   documentContent    Content of the document
 * @param   saveOptions        :keepTitle    Do not rename the file if title is changed
 * @return                     0 if there is no error,
 *                             1 if there is no error, but the document name has been changed
 *                             or an non-zero error code
 */
Save Document
@param   filePath           The origininal path of document
@param   documentTitle      Document's title. The saved file might be renamed if the title is changed.
@param   documentContent    Content of the document
@param   saveOptions        :keepTitle    Do not rename the file if title is changed
@return                     0 if there is no error,
1 if there is no error, but the document name has been changed
or an non-zero error code
Save Document @param filePath The origininal path of document @param documentTitle Document ' s title . The saved file might be renamed if the title is changed . @param documentContent Content of the document @param saveOptions : keepTitle Do not rename the file if title is changed @return 0 if there is no error 1 if there is no error but the document name has been changed or an non - zero error code
int WriterUI::saveDocument(QString filePath, QString documentTitle, QString documentContent, QVariantMap saveOptions) {
    bool keepTitle    = ( saveOptions.contains("keepTitle") && saveOptions["keepTitle"].toBool() );
    bool titleChanged = false;
    qDebug() << "WriterUI::saveDocument:filePath" << filePath << ":title:" << documentTitle<< ":saveOptions:" << saveOptions;
    QFile file(filePath);
    if ( !file.exists() ) return 0x1000;
    if (!keepTitle) {
        QFileInfo fileInfo( file );
        qDebug() << "File basename:" << fileInfo.baseName() << "completeBaseName:" << fileInfo.completeBaseName();
        QString newFileName = correctFileName( documentTitle );
        if (newFileName.isEmpty()) {
            newFileName = availableUntitledFilePath( fileInfo.path(), filePath );
        }
        if (newFileName != fileInfo.baseName()) {
            QString newName = fileInfo.path() + "/" + newFileName + ".txt";
            if ( QFile::exists(newName) ) {
                int counter = 0;
                while (true) {
                    QString fName = fileInfo.path() + "/" + newFileName + ( counter==0?"":QString(" %1").arg(counter) ) + ".txt";
                    if (!QFile::exists(fName)) break;
                    counter++;
                }
                newName = fileInfo.path() + "/" + newFileName + ( counter==0?"":QString(" %1").arg(counter) ) + ".txt";
            }
            qDebug() << "Update document's name -> " << newName;
            if (!file.rename(newName))
                return 0x1010;
            titleChanged = true;
        }
    }
    if ( !file.open(QIODevice::WriteOnly) )
        return 0x1001;
    if ( file.write( documentContent.toUtf8() ) == -1 ) {
        file.close();
        return 0x1002;
    }
    file.close();
    return titleChanged?1:0;
}
int WriterUI :: saveDocument ( QString filePath , QString documentTitle , QString documentContent , QVariantMap saveOptions ) { bool keepTitle = ( saveOptions . contains ( " " ) && saveOptions [ " " ] . toBool ( ) ) ; bool titleChanged = false ; qDebug ( ) << " " << filePath << " " << documentTitle << " " << saveOptions ; QFile file ( filePath ) ; if ( ! file . exists ( ) ) return 0x1000 ; if ( ! keepTitle ) { QFileInfo fileInfo ( file ) ; qDebug ( ) << " " << fileInfo . baseName ( ) << " " << fileInfo . completeBaseName ( ) ; QString newFileName = correctFileName ( documentTitle ) ; if ( newFileName . isEmpty ( ) ) { newFileName = availableUntitledFilePath ( fileInfo . path ( ) , filePath ) ; } if ( newFileName != fileInfo . baseName ( ) ) { QString newName = fileInfo . path ( ) + " " + newFileName + " " ; if ( QFile :: exists ( newName ) ) { int counter = 0 ; while ( true ) { QString fName = fileInfo . path ( ) + " " + newFileName + ( counter == 0 ? " " : QString ( " " ) . arg ( counter ) ) + " " ; if ( ! QFile :: exists ( fName ) ) break ; counter ++ ; } newName = fileInfo . path ( ) + " " + newFileName + ( counter == 0 ? " " : QString ( " " ) . arg ( counter ) ) + " " ; } qDebug ( ) << " " << newName ; if ( ! file . rename ( newName ) ) return 0x1010 ; titleChanged = true ; } } if ( ! file . open ( QIODevice :: WriteOnly ) ) return 0x1001 ; if ( file . write ( documentContent . toUtf8 ( ) ) == -1 ) { file . close ( ) ; return 0x1002 ; } file . close ( ) ; return titleChanged ? 1 : 0 ; }
Save Document
 @param   filePath           The origininal path of document
 @param   documentTitle      Document's title.
Save Document @param filePath The origininal path of document @param documentTitle Document ' s title .
// rename if needed // if newFileName is empty, pick an unused name // rename the file // if there is already a file with given name, add a counter value to filename // TODO