mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
core: make getCloudURL() return an std::string
Let's use std::string in the core. Notably, I'd like to make the numerous main() functions mostly independent of Qt. Some things will have to remain, such as argument parsing, of course. This changes the API: instead of returning an error code and taking a pointer to the actual return-value, return an std::optional<std::string>> that is set if the function succeeds. Returning an empty string in the error case might be simpler, but oh well... Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
8e106b0449
commit
c6cd10a43f
7 changed files with 54 additions and 48 deletions
|
@ -1351,27 +1351,34 @@ fraction_t string_to_fraction(const char *str)
|
||||||
return fraction;
|
return fraction;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getCloudURL(QString &filename)
|
// Sadly, the standard C++ library's regexp support is mediocre at best.
|
||||||
|
static void sanitize_email(std::string &email)
|
||||||
{
|
{
|
||||||
QString email = QString(prefs.cloud_storage_email);
|
size_t j = 0;
|
||||||
email.replace(QRegularExpression("[^a-zA-Z0-9@._+-]"), "");
|
for (char c: email) {
|
||||||
if (email.isEmpty() || empty_string(prefs.cloud_storage_password))
|
if (isalnum(c) || c == '@' || c == '.' ||
|
||||||
return report_error("Please configure Cloud storage email and password in the preferences");
|
c == '_' || c == '+' || c == '-')
|
||||||
if (email != prefs.cloud_storage_email_encoded) {
|
email[j++] = c;
|
||||||
free((void *)prefs.cloud_storage_email_encoded);
|
|
||||||
prefs.cloud_storage_email_encoded = copy_qstring(email);
|
|
||||||
}
|
}
|
||||||
filename = QString(QString(prefs.cloud_base_url) + "git/%1[%1]").arg(email);
|
email.resize(j);
|
||||||
if (verbose)
|
|
||||||
qDebug() << "returning cloud URL" << filename;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" char *cloud_url()
|
std::optional<std::string> getCloudURL()
|
||||||
{
|
{
|
||||||
QString filename;
|
std::string email(prefs.cloud_storage_email);
|
||||||
getCloudURL(filename);
|
sanitize_email(email);
|
||||||
return copy_qstring(filename);
|
if (email.empty() || empty_string(prefs.cloud_storage_password)) {
|
||||||
|
report_error("Please configure Cloud storage email and password in the preferences");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
if (email != prefs.cloud_storage_email_encoded) {
|
||||||
|
free((void *)prefs.cloud_storage_email_encoded);
|
||||||
|
prefs.cloud_storage_email_encoded = strdup(email.c_str());
|
||||||
|
}
|
||||||
|
std::string filename = std::string(prefs.cloud_base_url) + "git/" + email + "[" + email + "]";
|
||||||
|
if (verbose)
|
||||||
|
report_info("returning cloud URL %s", filename.c_str());
|
||||||
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void subsurface_mkdir(const char *dir)
|
extern "C" void subsurface_mkdir(const char *dir)
|
||||||
|
|
|
@ -20,6 +20,8 @@ enum watertypes {FRESHWATER, BRACKISHWATER, EN13319WATER, SALTWATER, DC_WATERTYP
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
#include "core/gettextfromc.h"
|
#include "core/gettextfromc.h"
|
||||||
class QImage;
|
class QImage;
|
||||||
|
|
||||||
|
@ -41,7 +43,7 @@ void write_hashes();
|
||||||
QString thumbnailFileName(const QString &filename);
|
QString thumbnailFileName(const QString &filename);
|
||||||
void learnPictureFilename(const QString &originalName, const QString &localName);
|
void learnPictureFilename(const QString &originalName, const QString &localName);
|
||||||
QString localFilePath(const QString &originalFilename);
|
QString localFilePath(const QString &originalFilename);
|
||||||
int getCloudURL(QString &filename);
|
std::optional<std::string> getCloudURL(); // move to prefs.h, probably.
|
||||||
bool parseGpsText(const QString &gps_text, double *latitude, double *longitude);
|
bool parseGpsText(const QString &gps_text, double *latitude, double *longitude);
|
||||||
void init_proxy();
|
void init_proxy();
|
||||||
QStringList getWaterTypesAsString();
|
QStringList getWaterTypesAsString();
|
||||||
|
@ -127,7 +129,6 @@ void updateWindowTitle();
|
||||||
void subsurface_mkdir(const char *dir);
|
void subsurface_mkdir(const char *dir);
|
||||||
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);
|
||||||
const char *local_file_path(struct picture *picture);
|
const char *local_file_path(struct picture *picture);
|
||||||
char *cloud_url();
|
|
||||||
char *hashfile_name_string();
|
char *hashfile_name_string();
|
||||||
enum deco_mode decoMode(bool in_planner);
|
enum deco_mode decoMode(bool in_planner);
|
||||||
void parse_seabear_header(const char *filename, struct xml_params *params);
|
void parse_seabear_header(const char *filename, struct xml_params *params);
|
||||||
|
|
|
@ -46,25 +46,23 @@ extern "C" void print_version()
|
||||||
extern "C" void print_files()
|
extern "C" void print_files()
|
||||||
{
|
{
|
||||||
struct git_info info;
|
struct git_info info;
|
||||||
const char *filename;
|
std::optional<std::string> filename;
|
||||||
|
|
||||||
printf("\nFile locations:\n\n");
|
printf("\nFile locations:\n\n");
|
||||||
printf("Cloud email:%s\n", prefs.cloud_storage_email);
|
printf("Cloud email:%s\n", prefs.cloud_storage_email);
|
||||||
if (!empty_string(prefs.cloud_storage_email) && !empty_string(prefs.cloud_storage_password)) {
|
if (!empty_string(prefs.cloud_storage_email) && !empty_string(prefs.cloud_storage_password)) {
|
||||||
filename = cloud_url();
|
filename = getCloudURL();
|
||||||
|
if (filename)
|
||||||
is_git_repository(filename, &info);
|
is_git_repository(filename->c_str(), &info);
|
||||||
} else {
|
|
||||||
/* strdup so the free below works in either case */
|
|
||||||
filename = strdup("No valid cloud credentials set.\n");
|
|
||||||
}
|
}
|
||||||
|
if (!filename)
|
||||||
|
filename = std::string("No valid cloud credentials set.\n");
|
||||||
if (!info.localdir.empty()) {
|
if (!info.localdir.empty()) {
|
||||||
printf("Local git storage: %s\n", info.localdir.c_str());
|
printf("Local git storage: %s\n", info.localdir.c_str());
|
||||||
} else {
|
} else {
|
||||||
printf("Unable to get local git directory\n");
|
printf("Unable to get local git directory\n");
|
||||||
}
|
}
|
||||||
printf("Cloud URL: %s\n", filename);
|
printf("Cloud URL: %s\n", filename->c_str());
|
||||||
free((void *)filename);
|
|
||||||
char *tmp = hashfile_name_string();
|
char *tmp = hashfile_name_string();
|
||||||
printf("Image filename table: %s\n", tmp);
|
printf("Image filename table: %s\n", tmp);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
|
|
|
@ -404,17 +404,17 @@ void MainWindow::on_actionCloudstorageopen_triggered()
|
||||||
if (!okToClose(tr("Please save or cancel the current dive edit before opening a new file.")))
|
if (!okToClose(tr("Please save or cancel the current dive edit before opening a new file.")))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QString filename;
|
auto filename = getCloudURL();
|
||||||
if (getCloudURL(filename))
|
if (!filename)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (verbose)
|
if (verbose)
|
||||||
qDebug() << "Opening cloud storage from:" << filename;
|
report_info("Opening cloud storage from: %s", filename->c_str());
|
||||||
|
|
||||||
closeCurrentFile();
|
closeCurrentFile();
|
||||||
|
|
||||||
showProgressBar();
|
showProgressBar();
|
||||||
QByteArray fileNamePtr = QFile::encodeName(filename);
|
QByteArray fileNamePtr = QFile::encodeName(QString::fromStdString(*filename));
|
||||||
if (!parse_file(fileNamePtr.data(), &divelog))
|
if (!parse_file(fileNamePtr.data(), &divelog))
|
||||||
setCurrentFile(fileNamePtr.toStdString());
|
setCurrentFile(fileNamePtr.toStdString());
|
||||||
process_loaded_dives();
|
process_loaded_dives();
|
||||||
|
@ -435,23 +435,23 @@ static bool saveToCloudOK()
|
||||||
|
|
||||||
void MainWindow::on_actionCloudstoragesave_triggered()
|
void MainWindow::on_actionCloudstoragesave_triggered()
|
||||||
{
|
{
|
||||||
QString filename;
|
|
||||||
if (!saveToCloudOK())
|
if (!saveToCloudOK())
|
||||||
return;
|
return;
|
||||||
if (getCloudURL(filename))
|
auto filename = getCloudURL();
|
||||||
|
if (!filename)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (verbose)
|
if (verbose)
|
||||||
qDebug() << "Saving cloud storage to:" << filename;
|
report_info("Saving cloud storage to: %s", filename->c_str());
|
||||||
mainTab->stealFocus(); // Make sure that any currently edited field is updated before saving.
|
mainTab->stealFocus(); // Make sure that any currently edited field is updated before saving.
|
||||||
|
|
||||||
showProgressBar();
|
showProgressBar();
|
||||||
int error = save_dives(qPrintable(filename));
|
int error = save_dives(filename->c_str());
|
||||||
hideProgressBar();
|
hideProgressBar();
|
||||||
if (error)
|
if (error)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
setCurrentFile(filename.toStdString());
|
setCurrentFile(*filename);
|
||||||
Command::setClean();
|
Command::setClean();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -567,11 +567,11 @@ void QMLManager::finishSetup()
|
||||||
// Initialize cloud credentials.
|
// Initialize cloud credentials.
|
||||||
git_local_only = !prefs.cloud_auto_sync;
|
git_local_only = !prefs.cloud_auto_sync;
|
||||||
|
|
||||||
QString url;
|
std::optional<std::string> url;
|
||||||
if (!qPrefCloudStorage::cloud_storage_email().isEmpty() &&
|
if (!qPrefCloudStorage::cloud_storage_email().isEmpty() &&
|
||||||
!qPrefCloudStorage::cloud_storage_password().isEmpty() &&
|
!qPrefCloudStorage::cloud_storage_password().isEmpty() &&
|
||||||
getCloudURL(url) == 0) {
|
(url = getCloudURL())) {
|
||||||
openLocalThenRemote(url);
|
openLocalThenRemote(QString::fromStdString(*url));
|
||||||
} else if (!existing_filename.empty() &&
|
} else if (!existing_filename.empty() &&
|
||||||
qPrefCloudStorage::cloud_verification_status() != qPrefCloudStorage::CS_UNKNOWN) {
|
qPrefCloudStorage::cloud_verification_status() != qPrefCloudStorage::CS_UNKNOWN) {
|
||||||
rememberOldStatus();
|
rememberOldStatus();
|
||||||
|
@ -781,13 +781,13 @@ void QMLManager::deleteAccount()
|
||||||
|
|
||||||
void QMLManager::loadDivesWithValidCredentials()
|
void QMLManager::loadDivesWithValidCredentials()
|
||||||
{
|
{
|
||||||
QString url;
|
auto url = getCloudURL();
|
||||||
if (getCloudURL(url)) {
|
if (!url) {
|
||||||
setStartPageText(RED_FONT + tr("Cloud storage error: %1").arg(consumeError()) + END_FONT);
|
setStartPageText(RED_FONT + tr("Cloud storage error: %1").arg(consumeError()) + END_FONT);
|
||||||
revertToNoCloudIfNeeded();
|
revertToNoCloudIfNeeded();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QByteArray fileNamePrt = QFile::encodeName(url);
|
QByteArray fileNamePrt = QFile::encodeName(QString::fromStdString(*url));
|
||||||
struct git_info info;
|
struct git_info info;
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
|
|
|
@ -89,9 +89,9 @@ int main(int argc, char **argv)
|
||||||
if (!defaultFile.isEmpty())
|
if (!defaultFile.isEmpty())
|
||||||
files.push_back(QString(prefs.default_filename));
|
files.push_back(QString(prefs.default_filename));
|
||||||
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
|
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
|
||||||
QString cloudURL;
|
auto cloudURL = getCloudURL();
|
||||||
if (getCloudURL(cloudURL) == 0)
|
if (cloudURL)
|
||||||
files.push_back(cloudURL);
|
files.push_back(QString::fromStdString(*cloudURL));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MainWindow *m = MainWindow::instance();
|
MainWindow *m = MainWindow::instance();
|
||||||
|
|
|
@ -84,9 +84,9 @@ int main(int argc, char **argv)
|
||||||
if (!defaultFile.isEmpty())
|
if (!defaultFile.isEmpty())
|
||||||
files.push_back(QString(prefs.default_filename));
|
files.push_back(QString(prefs.default_filename));
|
||||||
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
|
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
|
||||||
QString cloudURL;
|
auto cloudURL = getCloudURL();
|
||||||
if (getCloudURL(cloudURL) == 0)
|
if (cloudURL)
|
||||||
files.push_back(cloudURL);
|
files.push_back(QString::fromStdString(*cloudURL));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!files.isEmpty()) {
|
if (!files.isEmpty()) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue