bool MainWindow2::saveObject( QString strSavedFilename ) { QString filePath = strSavedFilename; bool savingTheOLDWAY = filePath.endsWith( PFF_OLD_EXTENSION ); QFileInfo fileInfo( filePath ); if ( fileInfo.isDir() ) return false; QString tmpFilePath; if ( !savingTheOLDWAY ) {// create temporary directory for compressing files tmpFilePath = QDir::tempPath() + "/" + fileInfo.completeBaseName() + PFF_TMP_COMPRESS_EXT; QFileInfo tmpDataInfo( tmpFilePath ); if ( !tmpDataInfo.exists() ) { QDir dir( QDir::tempPath() ); // --the directory where filePath is or will be saved dir.mkpath( tmpFilePath ); // --creates a directory with the same name +".data" } } else { tmpFilePath = fileInfo.absolutePath(); } QString dataLayersDir; if ( savingTheOLDWAY ) { dataLayersDir = filePath + "." + PFF_LAYERS_DIR; } else { dataLayersDir = tmpFilePath + "/" + PFF_LAYERS_DIR; } QFileInfo dataInfo( dataLayersDir ); if ( !dataInfo.exists() ) { QDir dir( tmpFilePath ); // the directory where filePath is or will be saved dir.mkpath( dataLayersDir ); // creates a directory with the same name +".data" } //savedName = filePath; this->setWindowTitle( filePath ); QProgressDialog progress( tr("Saving document..."), tr("Abort"), 0, 100, this ); progress.setWindowModality( Qt::WindowModal ); progress.show(); int progressValue = 0; // save data int nLayers = m_object->getLayerCount(); qDebug( "Layer Count=%d", nLayers ); for ( int i = 0; i < nLayers; i++ ) { Layer* layer = m_object->getLayer( i ); qDebug() << "Saving Layer " << i << "(" << layer->name << ")"; progressValue = (i * 100) / nLayers; progress.setValue( progressValue ); if ( layer->type() == Layer::BITMAP ) ((LayerBitmap*)layer)->saveImages( dataLayersDir, i ); if ( layer->type() == Layer::VECTOR ) ((LayerVector*)layer)->saveImages( dataLayersDir, i ); if ( layer->type() == Layer::SOUND ) ((LayerSound*)layer)->saveImages( dataLayersDir, i ); } // save palette m_object->savePalette( dataLayersDir ); // -------- save main XML file ----------- QString mainXMLfile; if ( !savingTheOLDWAY ) { mainXMLfile = tmpFilePath + "/" + PFF_XML_FILE_NAME; } else { mainXMLfile = filePath; } QFile* file = new QFile( mainXMLfile ); if ( !file->open( QFile::WriteOnly | QFile::Text ) ) { //QMessageBox::warning(this, "Warning", "Cannot write file"); return false; } QTextStream out( file ); QDomDocument doc( "PencilDocument" ); QDomElement root = doc.createElement( "document" ); doc.appendChild( root ); // save editor information QDomElement editorElement = createDomElement( doc ); root.appendChild( editorElement ); qDebug( "Save Editor Node." ); // save object QDomElement objectElement = m_object->createDomElement( doc ); root.appendChild( objectElement ); qDebug( "Save Object Node." ); int IndentSize = 2; doc.save( out, IndentSize ); // ----------------------------------- if ( !savingTheOLDWAY ) { qDebug() << "Now compressing data to PFF - PCLX ..."; JlCompress::compressDir( filePath, tmpFilePath ); removePFFTmpDirectory( tmpFilePath ); // --removing temporary files qDebug() << "Compressed. File saved."; } progress.setValue( 100 ); m_object->modified = false; m_pTimeLine->updateContent(); m_object->setFilePath( strSavedFilename ); m_recentFileMenu->addRecentFile( strSavedFilename ); m_recentFileMenu->saveToDisk(); return true; }
Status FileManager::save( Object* object, QString strFileName ) { QStringList debugDetails = QStringList() << "FileManager::save" << QString( "strFileName = " ).append( strFileName ); if ( object == nullptr ) { return Status( Status::INVALID_ARGUMENT, debugDetails << "object parameter is null" ); } QFileInfo fileInfo( strFileName ); if ( fileInfo.isDir() ) { return Status( Status::INVALID_ARGUMENT, debugDetails << "strFileName points to a directory", tr( "Invalid Save Path" ), tr( "The file path you have specified (\"%1\") points to a directory, so the file cannot be saved." ).arg( fileInfo.absoluteFilePath() ) ); } if ( fileInfo.exists() && !fileInfo.isWritable() ) { return Status( Status::INVALID_ARGUMENT, debugDetails << "strFileName points to a file that is not writable", tr( "Invalid Save Path" ), tr( "The file path you have specified (\"%1\") cannot be written to, so the file cannot be saved. Please make sure that you have sufficient permissions to save to that location and try again." ).arg( fileInfo.absoluteFilePath() ) ); } QString strTempWorkingFolder; QString strMainXMLFile; QString strDataFolder; bool isOldFile = strFileName.endsWith( PFF_OLD_EXTENSION ); if ( isOldFile ) { qCDebug( mLog ) << "Save in Old Pencil File Format (*.pcl) !"; strMainXMLFile = strFileName; strDataFolder = strMainXMLFile + "." + PFF_OLD_DATA_DIR; } else { qCDebug( mLog ) << "Save in New zipped Pencil File Format (*.pclx) !"; strTempWorkingFolder = object->workingDir(); Q_ASSERT( QDir( strTempWorkingFolder ).exists() ); debugDetails << QString( "strTempWorkingFolder = " ).append( strTempWorkingFolder ); qCDebug( mLog ) << "Temp Folder=" << strTempWorkingFolder; strMainXMLFile = QDir( strTempWorkingFolder ).filePath( PFF_XML_FILE_NAME ); strDataFolder = QDir( strTempWorkingFolder ).filePath( PFF_OLD_DATA_DIR ); } QFileInfo dataInfo( strDataFolder ); if ( !dataInfo.exists() ) { QDir dir( strDataFolder ); // the directory where filePath is or will be saved // creates a directory with the same name +".data" if( !dir.mkpath( strDataFolder ) ) { debugDetails << QString( "dir.absolutePath() = %1" ).arg( dir.absolutePath() ); if( isOldFile ) { return Status( Status::ERROR_FILE_CANNOT_OPEN, debugDetails, tr( "Cannot Create Data Directory" ), tr( "Cannot create the data directory at \"%1\". Please make sure that you have sufficient permissions to save to that location and try again. Alternatively try saving as pclx format." ).arg( strDataFolder ) ); } else { return Status( Status::FAIL, debugDetails, tr("Internal Error"), tr( "Cannot create the data directory at temporary location \"%1\". Please make sure that you have sufficient permissions to save to that location and try again. Alternatively try saving as pcl format." ).arg( strDataFolder ) ); } } } if( !dataInfo.isDir() ) { debugDetails << QString( "dataInfo.absoluteFilePath() = ").append(dataInfo.absoluteFilePath()); if( isOldFile ) { return Status( Status::ERROR_FILE_CANNOT_OPEN, debugDetails, tr( "Cannot Create Data Directory" ), tr( "Cannot use the path \"%1\" as a data directory since that currently points to a file. Please move or delete that file and try again. Alternatively try saving with the pclx format." ).arg( dataInfo.absoluteFilePath() ) ); } else { return Status( Status::FAIL, debugDetails, tr( "Internal Error" ), tr( "Cannot use the data directory at temporary location \"%1\" since it is a file. Please move or delete that file and try again. Alternatively try saving with the pcl format." ).arg( dataInfo.absoluteFilePath() ) ); } } // save data int layerCount = object->getLayerCount(); debugDetails << QString("layerCount = %1").arg(layerCount); qCDebug( mLog ) << QString( "Total layers = %1" ).arg( layerCount ); bool isOkay = true; for ( int i = 0; i < layerCount; ++i ) { Layer* layer = object->getLayer( i ); qCDebug( mLog ) << QString( "Saving Layer %1" ).arg( i ).arg( layer->mName ); //progressValue = (i * 100) / nLayers; //progress.setValue( progressValue ); debugDetails << QString("layer[%1] = Layer[id=%2, name=%3, type=%4]").arg( i ).arg( layer->id() ).arg( layer->name() ).arg( layer->type() ); switch ( layer->type() ) { case Layer::BITMAP: case Layer::VECTOR: case Layer::SOUND: { Status st = layer->save( strDataFolder ); if( !st.ok() ) { isOkay = false; QStringList layerDetails = st.detailsList(); for ( QString detail : layerDetails ) { detail.prepend( " " ); } debugDetails << QString( "- Layer[%1] failed to save" ).arg( i ) << layerDetails; } break; } case Layer::CAMERA: break; case Layer::UNDEFINED: case Layer::MOVIE: Q_ASSERT( false ); break; } if( !isOkay ) { return Status( Status::FAIL, debugDetails, tr( "Internal Error" ), tr( "An internal error occurred while trying to save the file. Some or all of your file may not have saved." ) ); } } // save palette object->savePalette( strDataFolder ); // -------- save main XML file ----------- QScopedPointer<QFile> file( new QFile( strMainXMLFile ) ); if ( !file->open( QFile::WriteOnly | QFile::Text ) ) { return Status::ERROR_FILE_CANNOT_OPEN; } QDomDocument xmlDoc( "PencilDocument" ); QDomElement root = xmlDoc.createElement( "document" ); xmlDoc.appendChild( root ); // save editor information //QDomElement editorElement = createDomElement( xmlDoc ); //root.appendChild( editorElement ); qCDebug( mLog ) << "Save Editor Node."; // save object QDomElement objectElement = object->saveXML( xmlDoc ); root.appendChild( objectElement ); qCDebug( mLog ) << "Save Object Node."; const int IndentSize = 2; QTextStream out( file.data() ); xmlDoc.save( out, IndentSize ); if ( !isOldFile ) { qCDebug( mLog ) << "Now compressing data to PFF - PCLX ..."; bool ok = JlCompress::compressDir( strFileName, strTempWorkingFolder ); if ( !ok ) { return Status::FAIL; } qCDebug( mLog ) << "Compressed. File saved."; } object->setFilePath( strFileName ); object->setModified( false ); return Status::OK; }
bool MainWindow2::saveObject(QString strSavedFilename) { QString filePath = strSavedFilename; QFileInfo fileInfo(filePath); if (fileInfo.isDir()) return false; QFileInfo dataInfo(filePath+".data"); if (!dataInfo.exists()) { QDir dir(fileInfo.absolutePath()); // the directory where filePath is or will be saved dir.mkpath(filePath+".data"); // creates a directory with the same name +".data" } //savedName = filePath; this->setWindowTitle( filePath ); QProgressDialog progress("Saving document...", "Abort", 0, 100, this); progress.setWindowModality(Qt::WindowModal); progress.show(); int progressValue = 0; // save data int nLayers = m_object->getLayerCount(); qDebug("Layer Count=%d", nLayers); for (int i = 0; i < nLayers; i++) { Layer* layer = m_object->getLayer(i); qDebug() << "Saving Layer " << i << "(" <<layer->name << ")"; progressValue = (i * 100) / nLayers; progress.setValue(progressValue); if (layer->type == Layer::BITMAP) ((LayerBitmap*)layer)->saveImages(filePath+".data", i); if (layer->type == Layer::VECTOR) ((LayerVector*)layer)->saveImages(filePath+".data", i); if (layer->type == Layer::SOUND) ((LayerSound*)layer)->saveImages(filePath+".data", i); } // save palette m_object->savePalette(filePath+".data"); // -------- save main XML file ----------- QFile* file = new QFile(filePath); if (!file->open(QFile::WriteOnly | QFile::Text)) { //QMessageBox::warning(this, "Warning", "Cannot write file"); return false; } QTextStream out(file); QDomDocument doc("PencilDocument"); QDomElement root = doc.createElement("document"); doc.appendChild(root); // save editor information QDomElement editorElement = createDomElement(doc); root.appendChild(editorElement); qDebug("Save Editor Node."); // save object QDomElement objectElement = m_object->createDomElement(doc); root.appendChild(objectElement); qDebug("Save Object Node."); int IndentSize = 2; doc.save(out, IndentSize); // ----------------------------------- progress.setValue(100); m_object->modified = false; m_pTimeLine->updateContent(); m_object->strCurrentFilePath = strSavedFilename; return true; }
int AutoDetectStreamBuf::readFromDevice(char* buffer, std::streamsize length) { poco_assert_dbg(length >= 8); if (_pIstr == 0 || length == 0) return -1; if (_reposition) { _pIstr->seekg(_start, std::ios_base::beg); _reposition = false; if (!_pIstr->good()) return -1; } if (!_prefix.empty()) { std::streamsize n = (_prefix.size() > length) ? length : static_cast<std::streamsize>(_prefix.size()); std::memcpy(buffer, _prefix.data(), n); _prefix.erase(0, n); return n; } if (_eofDetected) { if (!_postfix.empty()) { std::streamsize n = (_postfix.size() > length) ? length : static_cast<std::streamsize>(_postfix.size()); std::memcpy(buffer, _postfix.data(), n); _postfix.erase(0, n); return n; } else return -1; } if (!_pIstr->good()) return -1; std::streamsize offset = 0; static std::istream::int_type eof = std::istream::traits_type::eof(); while (_pIstr->good() && !_pIstr->eof() && (offset + 4) < length) { std::istream::int_type c = _pIstr->get(); if (c != eof) { if (_matchCnt < 3) { if (c == ZipDataInfo::HEADER[_matchCnt]) { ++_matchCnt; } else { for (int i = 0; i < _matchCnt; i++) { buffer[offset++] = ZipDataInfo::HEADER[i]; } if (c == ZipDataInfo::HEADER[0]) { _matchCnt = 1; } else { _matchCnt = 0; buffer[offset++] = static_cast<char>(c); } } } else if (_matchCnt == 3) { if (ZipDataInfo::HEADER[3] == c) { ZipDataInfo dataInfo(*_pIstr, true); if (!_pIstr->good()) throw Poco::IOException("Failed to read data descriptor"); if (dataInfo.getCompressedSize() == _length + offset) { _pIstr->seekg(-static_cast<int>(dataInfo.getFullHeaderSize()), std::ios::cur); if (!_pIstr->good()) throw Poco::IOException("Failed to seek on input stream"); _eofDetected = true; _length += offset; if (offset == 0 && !_postfix.empty()) { offset = (_postfix.size() > length) ? length : static_cast<std::streamsize>(_postfix.size()); std::memcpy(buffer, _postfix.data(), offset); _postfix.erase(0, offset); } return offset; } else { _pIstr->seekg(-static_cast<int>(dataInfo.getFullHeaderSize() - 4), std::ios::cur); if (!_pIstr->good()) throw Poco::IOException("Failed to seek on input stream"); buffer[offset++] = ZipDataInfo::HEADER[0]; buffer[offset++] = ZipDataInfo::HEADER[1]; buffer[offset++] = ZipDataInfo::HEADER[2]; buffer[offset++] = ZipDataInfo::HEADER[3]; _matchCnt = 0; } } else { buffer[offset++] = ZipDataInfo::HEADER[0]; buffer[offset++] = ZipDataInfo::HEADER[1]; buffer[offset++] = ZipDataInfo::HEADER[2]; buffer[offset++] = c; _matchCnt = 0; } } } } _length += offset; return offset; }