Ensure that CALL3.TXT is not deleted while updating the file

This is necessary so that symlinks that may be used to share CALL3.TXT
between WSJT-X  instances and with  and MAP65  do not get  broken when
adding new entries in either application.
This commit is contained in:
Bill Somerville 2021-09-20 17:38:09 +01:00
parent f884f77321
commit 917acbb97f
No known key found for this signature in database
GPG Key ID: D864B06D1E81618F
2 changed files with 87 additions and 81 deletions

View File

@ -1970,58 +1970,61 @@ void MainWindow::on_addButton_clicked() //Add button
<< endl << endl
#endif #endif
; ;
f1.close(); f1.seek (0);
f1.open(QIODevice::ReadOnly | QIODevice::Text);
} }
QString tmpFile = m_appDir + "/CALL3.TMP"; QString tmpFile = m_appDir + "/CALL3.TMP";
QFile f2(tmpFile); QFile f2(tmpFile);
if(!f2.open(QIODevice::WriteOnly | QIODevice::Text)) { if(!f2.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
msgBox("Cannot open " + tmpFile); msgBox("Cannot open " + tmpFile);
return; return;
} }
QTextStream in(&f1); {
QTextStream out(&f2); QTextStream in(&f1);
QString hc=hiscall; QTextStream out(&f2);
QString hc1=""; QString hc=hiscall;
QString hc2="000000"; QString hc1="";
QString s; QString hc2="000000";
do { QString s;
s=in.readLine(); do {
hc1=hc2; s=in.readLine();
if(s.mid(0,2)=="//") { hc1=hc2;
out << s + "\n"; if(s.mid(0,2)=="//") {
} else {
int i1=s.indexOf(",");
hc2=s.mid(0,i1);
if(hc>hc1 && hc<hc2) {
out << newEntry + "\n";
out << s + "\n"; out << s + "\n";
m_call3Modified=true;
} else if(hc==hc2) {
QString t=s + "\n\n is already in CALL3.TXT\n" +
"Do you wish to replace it?";
int ret = QMessageBox::warning(this, "Add",t,
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(ret==QMessageBox::Yes) {
out << newEntry + "\n";
m_call3Modified=true;
}
} else { } else {
if(s!="") out << s + "\n"; int i1=s.indexOf(",");
hc2=s.mid(0,i1);
if(hc>hc1 && hc<hc2) {
out << newEntry + "\n";
out << s + "\n";
m_call3Modified=true;
} else if(hc==hc2) {
QString t=s + "\n\n is already in CALL3.TXT\n" +
"Do you wish to replace it?";
int ret = QMessageBox::warning(this, "Add",t,
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(ret==QMessageBox::Yes) {
out << newEntry + "\n";
m_call3Modified=true;
}
} else {
if(s!="") out << s + "\n";
}
} }
} } while(!s.isNull());
} while(!s.isNull()); if(hc>hc1 && !m_call3Modified) out << newEntry + "\n";
}
f1.close();
if(hc>hc1 && !m_call3Modified) out << newEntry + "\n";
if(m_call3Modified) { if(m_call3Modified) {
QFile f0(m_appDir + "/CALL3.OLD"); auto const& old_path = m_appDir + "/CALL3.OLD";
if(f0.exists()) f0.remove(); QFile f0 {old_path};
QFile f1(m_appDir + "/CALL3.TXT"); if (f0.exists ()) f0.remove ();
f1.rename(m_appDir + "/CALL3.OLD"); f1.copy (old_path); // copying as we want to preserve
f2.rename(m_appDir + "/CALL3.TXT"); // symlinks
f2.close(); f1.open (QFile::WriteOnly | QFile::Text); // truncates
f2.seek (0);
f1.write (f2.readAll ()); // copy contents
f2.remove ();
} }
} }

View File

@ -5670,57 +5670,60 @@ void MainWindow::on_addButton_clicked() //Add button
<< endl << endl
#endif #endif
; ;
f1.close(); f1.seek (0);
f1.open(QIODevice::ReadOnly | QIODevice::Text);
} }
QFile f2 {m_config.writeable_data_dir ().absoluteFilePath ("CALL3.TMP")}; QFile f2 {m_config.writeable_data_dir ().absoluteFilePath ("CALL3.TMP")};
if(!f2.open(QIODevice::WriteOnly | QIODevice::Text)) { if(!f2.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
MessageBox::warning_message (this, tr ("Add to CALL3.TXT") MessageBox::warning_message (this, tr ("Add to CALL3.TXT")
, tr ("Cannot open \"%1\" for writing: %2") , tr ("Cannot open \"%1\" for writing: %2")
.arg (f2.fileName ()).arg (f2.errorString ())); .arg (f2.fileName ()).arg (f2.errorString ()));
return; return;
} }
QTextStream in(&f1); //Read from CALL3.TXT {
QTextStream out(&f2); //Copy into CALL3.TMP QTextStream in(&f1); //Read from CALL3.TXT
QString hc=hisCall; QTextStream out(&f2); //Copy into CALL3.TMP
QString hc1=""; QString hc=hisCall;
QString hc2="000000"; QString hc1="";
QString s; QString hc2="000000";
do { QString s;
s=in.readLine(); do {
hc1=hc2; s=in.readLine();
if(s.mid(0,2)=="//") { hc1=hc2;
out << s + QChar::LineFeed; //Copy all comment lines if(s.mid(0,2)=="//") {
} else { out << s + QChar::LineFeed; //Copy all comment lines
int i1=s.indexOf(",");
hc2=s.mid(0,i1);
if(hc>hc1 && hc<hc2) {
out << newEntry + QChar::LineFeed;
out << s + QChar::LineFeed;
m_call3Modified=true;
} else if(hc==hc2) {
QString t {tr ("%1\nis already in CALL3.TXT"
", do you wish to replace it?").arg (s)};
int ret = MessageBox::query_message (this, tr ("Add to CALL3.TXT"), t);
if(ret==MessageBox::Yes) {
out << newEntry + QChar::LineFeed;
m_call3Modified=true;
}
} else { } else {
if(s!="") out << s + QChar::LineFeed; int i1=s.indexOf(",");
hc2=s.mid(0,i1);
if(hc>hc1 && hc<hc2) {
out << newEntry + QChar::LineFeed;
out << s + QChar::LineFeed;
m_call3Modified=true;
} else if(hc==hc2) {
QString t {tr ("%1\nis already in CALL3.TXT"
", do you wish to replace it?").arg (s)};
int ret = MessageBox::query_message (this, tr ("Add to CALL3.TXT"), t);
if(ret==MessageBox::Yes) {
out << newEntry + QChar::LineFeed;
m_call3Modified=true;
}
} else {
if(s!="") out << s + QChar::LineFeed;
}
} }
} } while(!s.isNull());
} while(!s.isNull()); if(hc>hc1 && !m_call3Modified) out << newEntry + QChar::LineFeed;
}
f1.close();
if(hc>hc1 && !m_call3Modified) out << newEntry + QChar::LineFeed;
if(m_call3Modified) { if(m_call3Modified) {
QFile f0 {m_config.writeable_data_dir ().absoluteFilePath ("CALL3.OLD")}; auto const& old_path = m_config.writeable_data_dir ().absoluteFilePath ("CALL3.OLD");
if(f0.exists()) f0.remove(); QFile f0 {old_path};
QFile f1 {m_config.writeable_data_dir ().absoluteFilePath ("CALL3.TXT")}; if (f0.exists ()) f0.remove ();
f1.rename(m_config.writeable_data_dir ().absoluteFilePath ("CALL3.OLD")); f1.copy (old_path); // copying as we want to
f2.rename(m_config.writeable_data_dir ().absoluteFilePath ("CALL3.TXT")); // preserve symlinks
f2.close(); f1.open (QFile::WriteOnly | QFile::Text); // truncates
f2.seek (0);
f1.write (f2.readAll ()); // copy contents
f2.remove ();
} }
} }