Added safe saveing file of pattern.

--HG--
branch : develop
This commit is contained in:
dismine 2013-10-04 16:22:37 +03:00
parent 233e1f7cbc
commit 016e539c6b
2 changed files with 76 additions and 29 deletions

View File

@ -461,8 +461,8 @@ void MainWindow::showEvent( QShowEvent *event ){
void MainWindow::closeEvent(QCloseEvent *event){ void MainWindow::closeEvent(QCloseEvent *event){
if(changeInFile == true){ if(changeInFile == true){
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setText("The pattern has been modified."); msgBox.setText(tr("The pattern has been modified."));
msgBox.setInformativeText("Do you want to save your changes?"); msgBox.setInformativeText(tr("Do you want to save your changes?"));
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save); msgBox.setDefaultButton(QMessageBox::Save);
msgBox.setIcon(QMessageBox::Question); msgBox.setIcon(QMessageBox::Question);
@ -475,7 +475,13 @@ void MainWindow::closeEvent(QCloseEvent *event){
} else { } else {
ActionSave(); ActionSave();
} }
if(changeInFile){
// We did't save file
event->ignore();
} else {
// We have successfully saved the file
event->accept(); event->accept();
}
break; break;
case QMessageBox::Discard: case QMessageBox::Discard:
// Don't Save was clicked // Don't Save was clicked
@ -726,9 +732,9 @@ void MainWindow::ActionDetails(bool checked){
} }
void MainWindow::ActionSaveAs(){ void MainWindow::ActionSaveAs(){
QString filters("Lekalo files (*.xml);;All files (*.*)"); QString filters(tr("Lekalo files (*.xml);;All files (*.*)"));
QString defaultFilter("Lekalo files (*.xml)"); QString defaultFilter(tr("Lekalo files (*.xml)"));
QString fName = QFileDialog::getSaveFileName(this, "Зберегти файл як", QDir::homePath(), QString fName = QFileDialog::getSaveFileName(this, tr("Save as"), QDir::homePath(),
filters, &defaultFilter); filters, &defaultFilter);
if(fName.isEmpty()) if(fName.isEmpty())
return; return;
@ -736,35 +742,29 @@ void MainWindow::ActionSaveAs(){
fName.append(".xml"); fName.append(".xml");
} }
fileName = fName; fileName = fName;
QFileInfo info(fileName);
QString title(info.fileName());
title.append("-Valentina");
setWindowTitle(title);
QFile file(fileName); ActionSave();
if(file.open(QIODevice::WriteOnly| QIODevice::Truncate)){
doc->GarbageCollector();
const int Indent = 4;
QTextStream out(&file);
doc->save(out, Indent);
file.close();
}
ui->actionSave->setEnabled(false);
changeInFile = false;
} }
void MainWindow::ActionSave(){ void MainWindow::ActionSave(){
if(!fileName.isEmpty()){ if(!fileName.isEmpty()){
QFile file(fileName); bool result = SafeSaveing(fileName);
if(file.open(QIODevice::WriteOnly| QIODevice::Truncate)){ if(result){
doc->GarbageCollector();
const int Indent = 4;
QTextStream out(&file);
doc->save(out, Indent);
file.close();
}
ui->actionSave->setEnabled(false); ui->actionSave->setEnabled(false);
changeInFile = false; changeInFile = false;
QFileInfo info(fileName);
QString title(info.fileName());
title.append("-Valentina");
setWindowTitle(title);
} else {
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Error!"));
msgBox.setText(tr("Error saving file. Can't save file."));
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setIcon(QMessageBox::Warning);
msgBox.exec();
}
} }
} }
@ -810,6 +810,10 @@ void MainWindow::haveChange(){
if(!fileName.isEmpty()){ if(!fileName.isEmpty()){
ui->actionSave->setEnabled(true); ui->actionSave->setEnabled(true);
changeInFile = true; changeInFile = true;
QFileInfo info(fileName);
QString title(info.fileName());
title.append("*-Valentina");
setWindowTitle(title);
} }
} }
@ -902,6 +906,48 @@ void MainWindow::MinimumScrollBar(){
verScrollBar->setValue(verScrollBar->minimum()); verScrollBar->setValue(verScrollBar->minimum());
} }
bool MainWindow::SafeSaveing(const QString &fileName) const{
if(fileName.isEmpty()){
qWarning()<<tr("Got empty file name.");
return false;
}
//Writing in temporary file
QFileInfo tempInfo(fileName);
QString temp = tempInfo.absolutePath() + "/" + tempInfo.baseName() + ".tmp";
qDebug()<<"file "<<fileName<<"temp file "<<temp;
QFile tempFile(temp);
if(tempFile.open(QIODevice::WriteOnly| QIODevice::Truncate)){
const int Indent = 4;
QTextStream out(&tempFile);
doc->save(out, Indent);
tempFile.close();
}
//Replace temp file our
bool result = false;
QFile patternFile(fileName);
// We need here temporary file because we need restore pattern after error of copying temp file.
QTemporaryFile tempOfPattern;
if (tempOfPattern.open()) {
patternFile.copy(tempOfPattern.fileName());
}
if ( !patternFile.exists() || patternFile.remove() ) {
if ( !tempFile.copy(patternFile.fileName()) ){
qCritical()<<tr("Could not copy temp file to pattern file")<<Q_FUNC_INFO;
tempOfPattern.copy(fileName);
result = false;
} else {
result = true;
}
} else {
qCritical()<<tr("Could not remove pattern file")<<Q_FUNC_INFO;
result = false;
}
if(result){
tempFile.remove();
}
return result;
}
MainWindow::~MainWindow(){ MainWindow::~MainWindow(){
CanselTool(); CanselTool();
delete ui; delete ui;

View File

@ -153,6 +153,7 @@ private:
template <typename T> template <typename T>
void AddToolToDetail(T *tool, const qint64 &id, Tool::Tools typeTool, void AddToolToDetail(T *tool, const qint64 &id, Tool::Tools typeTool,
const qint64 &idDetail); const qint64 &idDetail);
bool SafeSaveing(const QString &fileName)const;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H