core: turn existing_filename into std::string

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-03-16 16:06:39 +01:00 committed by bstoeger
parent 37f2207f94
commit 41cb916060
10 changed files with 59 additions and 55 deletions

View file

@ -207,8 +207,6 @@ extern void invalidate_dive_cache(struct dive *dc);
extern int total_weight(const struct dive *); extern int total_weight(const struct dive *);
extern const char *existing_filename;
extern bool has_planned(const struct dive *dive, bool planned); extern bool has_planned(const struct dive *dive, bool planned);
/* Get gasmixes at increasing timestamps. /* Get gasmixes at increasing timestamps.
@ -229,8 +227,11 @@ extern void update_setpoint_events(const struct dive *dive, struct divecomputer
* QVariants and through QML. * QVariants and through QML.
*/ */
#include <QObject> #include <QObject>
#include <string>
Q_DECLARE_METATYPE(struct dive *); Q_DECLARE_METATYPE(struct dive *);
extern std::string existing_filename;
#endif #endif
#endif // DIVE_H #endif // DIVE_H

View file

@ -44,7 +44,7 @@
#include <libxslt/documents.h> #include <libxslt/documents.h>
const char *existing_filename; std::string existing_filename;
static QLocale loc; static QLocale loc;
static inline QString degreeSigns() static inline QString degreeSigns()
@ -521,12 +521,6 @@ QLocale getLocale()
return loc; return loc;
} }
void set_filename(const char *filename)
{
free((void *)existing_filename);
existing_filename = copy_string(filename);
}
QString get_depth_string(int mm, bool showunit, bool showdecimal) QString get_depth_string(int mm, bool showunit, bool showdecimal)
{ {
if (prefs.units.length == units::METERS) { if (prefs.units.length == units::METERS) {

View file

@ -125,7 +125,6 @@ bool canReachCloudServer(struct git_info *);
void updateWindowTitle(); void updateWindowTitle();
void subsurface_mkdir(const char *dir); void subsurface_mkdir(const char *dir);
char *get_file_name(const char *fileName); char *get_file_name(const char *fileName);
void set_filename(const char *filename);
void copy_image_and_overwrite(const char *cfileName, const char *path, const char *cnewName); void copy_image_and_overwrite(const char *cfileName, const char *path, const char *cnewName);
char *move_away(const char *path); char *move_away(const char *path);
const char *local_file_path(struct picture *picture); const char *local_file_path(struct picture *picture);

View file

@ -1192,12 +1192,12 @@ static int create_new_commit(struct git_info *info, git_oid *tree_id, bool creat
return report_error("Unable to look up parent in branch '%s'", info->branch); return report_error("Unable to look up parent in branch '%s'", info->branch);
if (!saved_git_id.empty()) { if (!saved_git_id.empty()) {
if (existing_filename && verbose) if (!existing_filename.empty() && verbose)
report_info("existing filename %s\n", existing_filename); report_info("existing filename %s\n", existing_filename.c_str());
const git_oid *id = git_commit_id((const git_commit *) parent); const git_oid *id = git_commit_id((const git_commit *) parent);
/* if we are saving to the same git tree we got this from, let's make /* if we are saving to the same git tree we got this from, let's make
* sure there is no confusion */ * sure there is no confusion */
if (same_string(existing_filename, info->url) && git_oid_strcmp(id, saved_git_id.c_str())) if (existing_filename == info->url && git_oid_strcmp(id, saved_git_id.c_str()))
return report_error("The git branch does not match the git parent of the source"); return report_error("The git branch does not match the git parent of the source");
} }

View file

@ -76,5 +76,11 @@ std::string format_string_std(const char *fmt, Args&&... args)
return res; return res;
} }
// Sadly, starts_with only with C++20!
inline bool starts_with(const std::string &s, const char *s2)
{
return s.rfind(s2, 0) == 0;
}
#endif #endif
#endif // SUBSURFACE_STRING_H #endif // SUBSURFACE_STRING_H

View file

@ -512,22 +512,22 @@ void MainWindow::closeCurrentFile()
clear_dive_file_data(); // this clears all the core data structures and resets the models clear_dive_file_data(); // this clears all the core data structures and resets the models
setCurrentFile(nullptr); setCurrentFile(nullptr);
diveList->setSortOrder(DiveTripModelBase::NR, Qt::DescendingOrder); diveList->setSortOrder(DiveTripModelBase::NR, Qt::DescendingOrder);
if (!existing_filename) if (existing_filename.empty())
setTitle(); setTitle();
disableShortcuts(); disableShortcuts();
} }
void MainWindow::updateCloudOnlineStatus() void MainWindow::updateCloudOnlineStatus()
{ {
bool is_cloud = existing_filename && prefs.cloud_verification_status == qPrefCloudStorage::CS_VERIFIED && bool is_cloud = !existing_filename.empty() && prefs.cloud_verification_status == qPrefCloudStorage::CS_VERIFIED &&
strstr(existing_filename, prefs.cloud_base_url); existing_filename.find(prefs.cloud_base_url) != std::string::npos;
ui.actionCloudOnline->setEnabled(is_cloud); ui.actionCloudOnline->setEnabled(is_cloud);
ui.actionCloudOnline->setChecked(is_cloud && !git_local_only); ui.actionCloudOnline->setChecked(is_cloud && !git_local_only);
} }
void MainWindow::setCurrentFile(const char *f) void MainWindow::setCurrentFile(const char *f)
{ {
set_filename(f); existing_filename = f;
setTitle(); setTitle();
updateCloudOnlineStatus(); updateCloudOnlineStatus();
} }
@ -545,13 +545,18 @@ void MainWindow::updateLastUsedDir(const QString &dir)
qPrefDisplay::set_lastDir(dir); qPrefDisplay::set_lastDir(dir);
} }
static QString get_current_filename()
{
return existing_filename.empty() ? QString(prefs.default_filename)
: QString::fromStdString(existing_filename);
}
void MainWindow::on_actionPrint_triggered() void MainWindow::on_actionPrint_triggered()
{ {
#ifndef NO_PRINTING #ifndef NO_PRINTING
// When in planner, only print the planned dive. // When in planner, only print the planned dive.
dive *singleDive = appState == ApplicationState::PlanDive ? plannerWidgets->getDive() dive *singleDive = appState == ApplicationState::PlanDive ? plannerWidgets->getDive()
: nullptr; : nullptr;
QString filename = existing_filename ?: prefs.default_filename; QString filename = get_current_filename();
PrintDialog dlg(singleDive, filename, this); PrintDialog dlg(singleDive, filename, this);
dlg.exec(); dlg.exec();
@ -608,7 +613,7 @@ void MainWindow::on_actionQuit_triggered()
void MainWindow::on_actionDownloadDC_triggered() void MainWindow::on_actionDownloadDC_triggered()
{ {
QString filename = existing_filename ?: prefs.default_filename; QString filename = get_current_filename();
DownloadFromDCWidget dlg(filename, this); DownloadFromDCWidget dlg(filename, this);
dlg.exec(); dlg.exec();
} }
@ -980,7 +985,7 @@ bool MainWindow::askSaveChanges()
{ {
QMessageBox response(this); QMessageBox response(this);
QString message = existing_filename ? QString message = !existing_filename.empty() ?
tr("Do you want to save the changes that you made in the file %1?").arg(displayedFilename(existing_filename)) : tr("Do you want to save the changes that you made in the file %1?").arg(displayedFilename(existing_filename)) :
tr("Do you want to save the changes that you made in the data file?"); tr("Do you want to save the changes that you made in the data file?");
@ -1157,23 +1162,23 @@ void MainWindow::recentFileTriggered(bool)
int MainWindow::file_save_as(void) int MainWindow::file_save_as(void)
{ {
QString filename; QString filename;
const char *default_filename = existing_filename; std::string default_filename = existing_filename;
// if the default is to save to cloud storage, pick something that will work as local file: // if the default is to save to cloud storage, pick something that will work as local file:
// simply extract the branch name which should be the users email address // simply extract the branch name which should be the users email address
if (default_filename && QString(default_filename).contains(QRegularExpression(CLOUD_HOST_PATTERN))) { if (!default_filename.empty() && QString::fromStdString(default_filename).contains(QRegularExpression(CLOUD_HOST_PATTERN))) {
QString filename(default_filename); QString filename = QString::fromStdString(default_filename);
filename.remove(0, filename.indexOf("[") + 1); filename.remove(0, filename.indexOf("[") + 1);
filename.replace("]", ".ssrf"); filename.replace("]", ".ssrf");
default_filename = copy_qstring(filename); default_filename = filename.toStdString();
} }
// create a file dialog that allows us to save to a new file // create a file dialog that allows us to save to a new file
QFileDialog selection_dialog(this, tr("Save file as"), default_filename, QFileDialog selection_dialog(this, tr("Save file as"), default_filename.c_str(),
tr("Subsurface files") + " (*.ssrf *.xml)"); tr("Subsurface files") + " (*.ssrf *.xml)");
selection_dialog.setAcceptMode(QFileDialog::AcceptSave); selection_dialog.setAcceptMode(QFileDialog::AcceptSave);
selection_dialog.setFileMode(QFileDialog::AnyFile); selection_dialog.setFileMode(QFileDialog::AnyFile);
selection_dialog.setDefaultSuffix(""); selection_dialog.setDefaultSuffix("");
if (empty_string(default_filename)) { if (default_filename.empty()) {
QFileInfo defaultFile(system_default_filename()); QFileInfo defaultFile(system_default_filename());
selection_dialog.setDirectory(qPrintable(defaultFile.absolutePath())); selection_dialog.setDirectory(qPrintable(defaultFile.absolutePath()));
} }
@ -1207,15 +1212,15 @@ int MainWindow::file_save(void)
const char *current_default; const char *current_default;
bool is_cloud = false; bool is_cloud = false;
if (!existing_filename) if (existing_filename.empty())
return file_save_as(); return file_save_as();
is_cloud = (strncmp(existing_filename, "http", 4) == 0); is_cloud = (starts_with(existing_filename, "http") == 0);
if (is_cloud && !saveToCloudOK()) if (is_cloud && !saveToCloudOK())
return -1; return -1;
current_default = prefs.default_filename; current_default = prefs.default_filename;
if (strcmp(existing_filename, current_default) == 0) { if (existing_filename == current_default) {
/* if we are using the default filename the directory /* if we are using the default filename the directory
* that we are creating the file in may not exist */ * that we are creating the file in may not exist */
QDir current_def_dir = QFileInfo(current_default).absoluteDir(); QDir current_def_dir = QFileInfo(current_default).absoluteDir();
@ -1224,7 +1229,7 @@ int MainWindow::file_save(void)
} }
if (is_cloud) if (is_cloud)
showProgressBar(); showProgressBar();
if (save_dives(existing_filename)) { if (save_dives(existing_filename.c_str())) {
if (is_cloud) if (is_cloud)
hideProgressBar(); hideProgressBar();
return -1; return -1;
@ -1232,7 +1237,7 @@ int MainWindow::file_save(void)
if (is_cloud) if (is_cloud)
hideProgressBar(); hideProgressBar();
Command::setClean(); Command::setClean();
addRecentFile(QString(existing_filename), true); addRecentFile(QString::fromStdString(existing_filename), true);
return 0; return 0;
} }
@ -1241,13 +1246,13 @@ NotificationWidget *MainWindow::getNotificationWidget()
return ui.mainErrorMessage; return ui.mainErrorMessage;
} }
QString MainWindow::displayedFilename(QString fullFilename) QString MainWindow::displayedFilename(const std::string &fullFilename)
{ {
QFile f(fullFilename); QFile f(fullFilename.c_str());
QFileInfo fileInfo(f); QFileInfo fileInfo(f);
QString fileName(fileInfo.fileName()); QString fileName(fileInfo.fileName());
if (fullFilename.contains(prefs.cloud_base_url)) { if (fullFilename.find(prefs.cloud_base_url) != std::string::npos) {
QString email = fileName.left(fileName.indexOf('[')); QString email = fileName.left(fileName.indexOf('['));
return git_local_only ? return git_local_only ?
tr("[local cache for] %1").arg(email) : tr("[local cache for] %1").arg(email) :
@ -1265,7 +1270,7 @@ void MainWindow::setAutomaticTitle()
void MainWindow::setTitle() void MainWindow::setTitle()
{ {
if (empty_string(existing_filename)) { if (existing_filename.empty()) {
setWindowTitle("Subsurface"); setWindowTitle("Subsurface");
return; return;
} }
@ -1397,7 +1402,7 @@ void MainWindow::on_actionExport_triggered()
void MainWindow::on_actionConfigure_Dive_Computer_triggered() void MainWindow::on_actionConfigure_Dive_Computer_triggered()
{ {
QString filename = existing_filename ?: prefs.default_filename; QString filename = get_current_filename();
ConfigureDiveComputerDialog *dcConfig = new ConfigureDiveComputerDialog(filename, this); ConfigureDiveComputerDialog *dcConfig = new ConfigureDiveComputerDialog(filename, this);
dcConfig->show(); dcConfig->show();
} }

View file

@ -177,7 +177,7 @@ private:
QString filter_import(); QString filter_import();
QString filter_import_dive_sites(); QString filter_import_dive_sites();
static MainWindow *m_Instance; static MainWindow *m_Instance;
QString displayedFilename(QString fullFilename); QString displayedFilename(const std::string &fullFilename);
bool askSaveChanges(); bool askSaveChanges();
bool okToClose(QString message); bool okToClose(QString message);
void closeCurrentFile(); void closeCurrentFile();

View file

@ -430,7 +430,7 @@ void QMLManager::openLocalThenRemote(QString url)
git_local_only = false; git_local_only = false;
appendTextToLog(QStringLiteral("taking things online to be able to switch to cloud account")); appendTextToLog(QStringLiteral("taking things online to be able to switch to cloud account"));
} }
set_filename(encodedFilename.constData()); existing_filename = encodedFilename.toStdString();
if (git_local_only && qPrefCloudStorage::cloud_verification_status() != qPrefCloudStorage::CS_NOCLOUD) if (git_local_only && qPrefCloudStorage::cloud_verification_status() != qPrefCloudStorage::CS_NOCLOUD)
appendTextToLog(QStringLiteral("have cloud credentials, but user asked not to connect to network")); appendTextToLog(QStringLiteral("have cloud credentials, but user asked not to connect to network"));
@ -572,24 +572,24 @@ void QMLManager::finishSetup()
!qPrefCloudStorage::cloud_storage_password().isEmpty() && !qPrefCloudStorage::cloud_storage_password().isEmpty() &&
getCloudURL(url) == 0) { getCloudURL(url) == 0) {
openLocalThenRemote(url); openLocalThenRemote(url);
} else if (!empty_string(existing_filename) && } else if (!existing_filename.empty() &&
qPrefCloudStorage::cloud_verification_status() != qPrefCloudStorage::CS_UNKNOWN) { qPrefCloudStorage::cloud_verification_status() != qPrefCloudStorage::CS_UNKNOWN) {
rememberOldStatus(); rememberOldStatus();
set_filename(qPrintable(nocloud_localstorage())); existing_filename = nocloud_localstorage().toStdString();
qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD); qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD);
emit passwordStateChanged(); emit passwordStateChanged();
saveCloudCredentials(qPrefCloudStorage::cloud_storage_email(), qPrefCloudStorage::cloud_storage_password(), qPrefCloudStorage::cloud_storage_pin()); saveCloudCredentials(qPrefCloudStorage::cloud_storage_email(), qPrefCloudStorage::cloud_storage_password(), qPrefCloudStorage::cloud_storage_pin());
appendTextToLog(tr("working in no-cloud mode")); appendTextToLog(tr("working in no-cloud mode"));
int error = parse_file(existing_filename, &divelog); int error = parse_file(existing_filename.c_str(), &divelog);
if (error) { if (error) {
// we got an error loading the local file // we got an error loading the local file
setNotificationText(tr("Error parsing local storage, giving up")); setNotificationText(tr("Error parsing local storage, giving up"));
set_filename(NULL); existing_filename.clear();
} else { } else {
// successfully opened the local file, now add thigs to the dive list // successfully opened the local file, now add thigs to the dive list
consumeFinishedLoad(); consumeFinishedLoad();
updateHaveLocalChanges(true); updateHaveLocalChanges(true);
appendTextToLog(QString("working in no-cloud mode, finished loading %1 dives from %2").arg(divelog.dives->nr).arg(existing_filename)); appendTextToLog(QString("working in no-cloud mode, finished loading %1 dives from %2").arg(divelog.dives->nr).arg(existing_filename.c_str()));
} }
} else { } else {
qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_UNKNOWN); qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_UNKNOWN);
@ -769,7 +769,7 @@ void QMLManager::deleteAccount()
qPrefCloudStorage::set_cloud_storage_password(""); qPrefCloudStorage::set_cloud_storage_password("");
qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD); qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD);
emit passwordStateChanged(); emit passwordStateChanged();
set_filename(qPrintable(nocloud_localstorage())); existing_filename = nocloud_localstorage().toStdString();
setStartPageText(tr("Cloud storage account deleted.")); setStartPageText(tr("Cloud storage account deleted."));
return; return;
} }
@ -808,12 +808,12 @@ void QMLManager::loadDivesWithValidCredentials()
setDiveListProcessing(false); setDiveListProcessing(false);
if (!error) { if (!error) {
report_error("filename is now %s", fileNamePrt.data()); report_error("filename is now %s", fileNamePrt.data());
set_filename(fileNamePrt.data()); existing_filename = fileNamePrt.toStdString();
} else { } else {
report_error("failed to open file %s", fileNamePrt.data()); report_error("failed to open file %s", fileNamePrt.data());
setNotificationText(consumeError()); setNotificationText(consumeError());
revertToNoCloudIfNeeded(); revertToNoCloudIfNeeded();
set_filename(NULL); existing_filename.clear();
return; return;
} }
consumeFinishedLoad(); consumeFinishedLoad();
@ -873,7 +873,7 @@ void QMLManager::revertToNoCloudIfNeeded()
rememberOldStatus(); rememberOldStatus();
qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD); qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NOCLOUD);
emit passwordStateChanged(); emit passwordStateChanged();
set_filename(qPrintable(nocloud_localstorage())); existing_filename = nocloud_localstorage().toStdString();
setStartPageText(RED_FONT + tr("Failed to connect to cloud server, reverting to no cloud status") + END_FONT); setStartPageText(RED_FONT + tr("Failed to connect to cloud server, reverting to no cloud status") + END_FONT);
} }
} }
@ -1477,7 +1477,7 @@ void QMLManager::openNoCloudRepo()
// repo doesn't exist, create it and write the empty dive list to it // repo doesn't exist, create it and write the empty dive list to it
git_create_local_repo(qPrintable(filename)); git_create_local_repo(qPrintable(filename));
save_dives(qPrintable(filename)); save_dives(qPrintable(filename));
set_filename(qPrintable(filename)); existing_filename = filename.toStdString();
auto s = qPrefLog::instance(); auto s = qPrefLog::instance();
s->set_default_filename(qPrintable(filename)); s->set_default_filename(qPrintable(filename));
s->set_default_file_behavior(LOCAL_DEFAULT_FILE); s->set_default_file_behavior(LOCAL_DEFAULT_FILE);
@ -1489,10 +1489,10 @@ void QMLManager::saveChangesLocal(bool fromUndo)
{ {
if (unsavedChanges()) { if (unsavedChanges()) {
if (qPrefCloudStorage::cloud_verification_status() == qPrefCloudStorage::CS_NOCLOUD) { if (qPrefCloudStorage::cloud_verification_status() == qPrefCloudStorage::CS_NOCLOUD) {
if (empty_string(existing_filename)) { if (existing_filename.empty()) {
QString filename = nocloud_localstorage(); QString filename = nocloud_localstorage();
git_create_local_repo(qPrintable(filename)); git_create_local_repo(qPrintable(filename));
set_filename(qPrintable(filename)); existing_filename = filename.toStdString();
auto s = qPrefLog::instance(); auto s = qPrefLog::instance();
s->set_default_filename(qPrintable(filename)); s->set_default_filename(qPrintable(filename));
s->set_default_file_behavior(LOCAL_DEFAULT_FILE); s->set_default_file_behavior(LOCAL_DEFAULT_FILE);
@ -1505,11 +1505,11 @@ void QMLManager::saveChangesLocal(bool fromUndo)
} }
bool glo = git_local_only; bool glo = git_local_only;
git_local_only = true; git_local_only = true;
int error = save_dives(existing_filename); int error = save_dives(existing_filename.c_str());
git_local_only = glo; git_local_only = glo;
if (error) { if (error) {
setNotificationText(consumeError()); setNotificationText(consumeError());
set_filename(NULL); existing_filename.clear();
return; return;
} }
mark_divelist_changed(false); mark_divelist_changed(false);

View file

@ -67,7 +67,6 @@ void init_ui()
void exit_ui() void exit_ui()
{ {
free_globals(); free_globals();
free((void *)existing_filename);
} }
#ifdef SUBSURFACE_MOBILE #ifdef SUBSURFACE_MOBILE

View file

@ -79,9 +79,9 @@ int main(int argc, char **argv)
} }
init_ui(); init_ui();
if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE) if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE)
set_filename(prefs.default_filename); existing_filename = prefs.default_filename;
else else
set_filename(NULL); existing_filename.clear();
// some hard coded settings // some hard coded settings
qPrefCloudStorage::set_save_password_local(true); qPrefCloudStorage::set_save_password_local(true);