preferences: use std::string in struct preferences

This is a messy commit, because the "qPref" system relies
heavily on QString, which means lots of conversions between
the two worlds. Ultimately, I plan to base the preferences
system on std::string and only convert to QString when
pushing through Qt's property system or when writing into
Qt's settings.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-13 22:59:32 +02:00 committed by bstoeger
parent 82fc9de40b
commit ccdd92aeb7
78 changed files with 645 additions and 694 deletions

View file

@ -41,31 +41,31 @@ static std::string make_default_filename()
return system_default_path() + "/subsurface.xml";
}
const char android_system_divelist_default_font[] = "Roboto";
const char *system_divelist_default_font = android_system_divelist_default_font;
double system_divelist_default_font_size = -1;
using namespace std::string_literals;
std::string system_divelist_default_font = "Roboto"s;
double system_divelist_default_font_size = -1.0;
int get_usb_fd(uint16_t idVendor, uint16_t idProduct);
void subsurface_OS_pref_setup()
{
}
bool subsurface_ignore_font(const char *font)
bool subsurface_ignore_font(const std::string &font)
{
// there are no old default fonts that we would want to ignore
return false;
}
const char *system_default_directory()
std::string system_default_directory()
{
static const std::string path = system_default_path();
return path.c_str();
return path;
}
const char *system_default_filename()
std::string system_default_filename()
{
static const std::string fn = make_default_filename();
return fn.c_str();
return fn;
}

View file

@ -21,7 +21,6 @@ CheckCloudConnection::CheckCloudConnection(QObject *parent) :
QObject(parent),
reply(0)
{
}
// two free APIs to figure out where we are
@ -43,7 +42,7 @@ bool CheckCloudConnection::checkServer()
QNetworkRequest request;
request.setRawHeader("Accept", "text/plain");
request.setRawHeader("User-Agent", getUserAgent().toUtf8());
request.setUrl(QString(prefs.cloud_base_url) + TEAPOT);
request.setUrl(QString::fromStdString(prefs.cloud_base_url) + TEAPOT);
reply = mgr->get(request);
QTimer timer;
timer.setSingleShot(true);
@ -73,7 +72,7 @@ bool CheckCloudConnection::checkServer()
}
}
if (verbose)
report_info("connection test to cloud server %s failed %d %s %d %s", prefs.cloud_base_url,
report_info("connection test to cloud server %s failed %d %s %d %s", prefs.cloud_base_url.c_str(),
static_cast<int>(reply->error()), qPrintable(reply->errorString()),
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(),
qPrintable(reply->readAll()));
@ -109,19 +108,16 @@ bool CheckCloudConnection::nextServer()
};
const char *server = nullptr;
for (serverTried &item: cloudServers) {
if (strstr(prefs.cloud_base_url, item.server))
if (contains(prefs.cloud_base_url, item.server))
item.tried = true;
else if (item.tried == false)
server = item.server;
}
if (server) {
int s = strlen(server);
char *baseurl = (char *)malloc(10 + s);
strcpy(baseurl, "https://");
strncat(baseurl, server, s);
strcat(baseurl, "/");
report_info("failed to connect to %s next server to try: %s", prefs.cloud_base_url, baseurl);
prefs.cloud_base_url = baseurl;
using namespace std::string_literals;
std::string base_url = "https://"s + server + "/"s;
report_info("failed to connect to %s next server to try: %s", prefs.cloud_base_url.c_str(), base_url.c_str());
prefs.cloud_base_url = std::move(base_url);
git_storage_update_progress(qPrintable(tr("Trying different cloud server...")));
return true;
}
@ -192,7 +188,7 @@ void CheckCloudConnection::gotContinent(QNetworkReply *reply)
base_url = "https://" CLOUD_HOST_US "/";
else
base_url = "https://" CLOUD_HOST_EU "/";
if (!same_string(base_url, prefs.cloud_base_url)) {
if (base_url != prefs.cloud_base_url) {
if (verbose)
report_info("remember cloud server %s based on IP location in %s", base_url, qPrintable(continentString));
qPrefCloudStorage::instance()->store_cloud_base_url(base_url);
@ -211,7 +207,7 @@ bool canReachCloudServer(struct git_info *info)
// the cloud_base_url ends with a '/', so we need the text starting at "git/..."
size_t pos = info->url.find("org/git/");
if (pos != std::string::npos) {
info->url = format_string_std("%s%s", prefs.cloud_base_url, info->url.c_str() + pos + 4);
info->url = format_string_std("%s%s", prefs.cloud_base_url.c_str(), info->url.c_str() + pos + 4);
if (verbose)
report_info("updating remote to: %s", info->url.c_str());
}

View file

@ -13,24 +13,43 @@ CloudStorageAuthenticate::CloudStorageAuthenticate(QObject *parent) :
userAgent = getUserAgent();
}
#define CLOUDURL QString(prefs.cloud_base_url)
#define CLOUDBACKENDSTORAGE CLOUDURL + "/storage"
#define CLOUDBACKENDVERIFY CLOUDURL + "/verify"
#define CLOUDBACKENDUPDATE CLOUDURL + "/update"
#define CLOUDBACKENDDELETE CLOUDURL + "/delete-account"
static QString cloudUrl()
{
return QString::fromStdString(prefs.cloud_base_url);
}
static QUrl cloudBackendStorage()
{
return QUrl(cloudUrl() + "/storage");
}
static QUrl cloudBackendVerify()
{
return QUrl(cloudUrl() + "/verify");
}
static QUrl cloudBackendUpdate()
{
return QUrl(cloudUrl() + "/update");
}
static QUrl cloudBackendDelete()
{
return QUrl(cloudUrl() + "/delete-account");
}
QNetworkReply* CloudStorageAuthenticate::backend(const QString& email,const QString& password,const QString& pin,const QString& newpasswd)
{
QString payload(email + QChar(' ') + password);
QUrl requestUrl;
if (pin.isEmpty() && newpasswd.isEmpty()) {
requestUrl = QUrl(CLOUDBACKENDSTORAGE);
requestUrl = cloudBackendStorage();
} else if (!newpasswd.isEmpty()) {
requestUrl = QUrl(CLOUDBACKENDUPDATE);
requestUrl = cloudBackendUpdate();
payload += QChar(' ') + newpasswd;
cloudNewPassword = newpasswd;
} else {
requestUrl = QUrl(CLOUDBACKENDVERIFY);
requestUrl = cloudBackendVerify();
payload += QChar(' ') + pin;
}
QNetworkRequest *request = new QNetworkRequest(requestUrl);
@ -54,7 +73,7 @@ QNetworkReply* CloudStorageAuthenticate::backend(const QString& email,const QStr
QNetworkReply* CloudStorageAuthenticate::deleteAccount(const QString& email, const QString& password)
{
QString payload(email + QChar(' ') + password);
QNetworkRequest *request = new QNetworkRequest(QUrl(CLOUDBACKENDDELETE));
QNetworkRequest *request = new QNetworkRequest(cloudBackendDelete());
request->setRawHeader("Accept", "text/xml, text/plain");
request->setRawHeader("User-Agent", userAgent.toUtf8());
request->setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");

View file

@ -2639,18 +2639,6 @@ void set_informational_units(const char *units)
}
void set_git_prefs(const char *prefs)
{
if (strstr(prefs, "TANKBAR"))
git_prefs.tankbar = 1;
if (strstr(prefs, "SHOW_SETPOINT"))
git_prefs.show_ccr_setpoint = 1;
if (strstr(prefs, "SHOW_SENSORS"))
git_prefs.show_ccr_sensors = 1;
if (strstr(prefs, "PO2_GRAPH"))
git_prefs.pp_graphs.po2 = 1;
}
/* clones a dive and moves given dive computer to front */
std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number)
{

View file

@ -146,8 +146,6 @@ extern unsigned int number_of_computers(const struct dive *dive);
extern struct divecomputer *get_dive_dc(struct dive *dive, int nr);
extern const struct divecomputer *get_dive_dc(const struct dive *dive, int nr);
extern void set_git_prefs(const char *prefs);
extern std::unique_ptr<dive> clone_make_first_dc(const struct dive &d, int dc_number);
extern std::unique_ptr<dive> clone_delete_divecomputer(const struct dive &d, int dc_number);
extern std::array<std::unique_ptr<dive>, 2> split_divecomputer(const struct dive &src, int num);

View file

@ -402,10 +402,10 @@ cylinder_t *get_or_create_cylinder(struct dive *d, int idx)
/* if a default cylinder is set, use that */
void fill_default_cylinder(const struct dive *dive, cylinder_t *cyl)
{
const char *cyl_name = prefs.default_cylinder;
const std::string &cyl_name = prefs.default_cylinder;
pressure_t pO2 = {.mbar = static_cast<int>(lrint(prefs.modpO2 * 1000.0))};
if (!cyl_name)
if (cyl_name.empty())
return;
for (auto &ti: tank_info_table) {
if (ti.name == cyl_name) {
@ -454,7 +454,7 @@ void add_default_cylinder(struct dive *d)
return;
cylinder_t cyl;
if (!empty_string(prefs.default_cylinder)) {
if (!prefs.default_cylinder.empty()) {
cyl = create_new_cylinder(d);
} else {
// roughly an AL80

View file

@ -289,7 +289,7 @@ int parse_file(const char *filename, struct divelog *log)
auto [mem, err] = readfile(filename);
if (err < 0) {
/* we don't want to display an error if this was the default file */
if (same_string(filename, prefs.default_filename))
if (filename == prefs.default_filename)
return 0;
return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);

View file

@ -150,7 +150,7 @@ std::string get_local_dir(const std::string &url, const std::string &branch)
sha.update(branch);
auto hash = sha.hash();
return format_string_std("%s/cloudstorage/%02x%02x%02x%02x%02x%02x%02x%02x",
system_default_directory(),
system_default_directory().c_str(),
hash[0], hash[1], hash[2], hash[3],
hash[4], hash[5], hash[6], hash[7]);
}
@ -246,27 +246,27 @@ int credential_ssh_cb(git_cred **out,
unsigned int allowed_types,
void *)
{
const char *username = prefs.cloud_storage_email_encoded;
const char *passphrase = prefs.cloud_storage_password ? prefs.cloud_storage_password : "";
const std::string &username = prefs.cloud_storage_email_encoded;
const std::string &passphrase = prefs.cloud_storage_password;
// TODO: We need a way to differentiate between password and private key authentication
if (allowed_types & GIT_CREDTYPE_SSH_KEY) {
std::string priv_key = std::string(system_default_directory()) + "/ssrf_remote.key";
std::string priv_key = system_default_directory() + "/ssrf_remote.key";
if (!access(priv_key.c_str(), F_OK)) {
if (exceeded_auth_attempts())
return GIT_EUSER;
return git_cred_ssh_key_new(out, username, NULL, priv_key.c_str(), passphrase);
return git_cred_ssh_key_new(out, username.c_str(), NULL, priv_key.c_str(), passphrase.c_str());
}
}
if (allowed_types & GIT_CREDTYPE_USERPASS_PLAINTEXT) {
if (exceeded_auth_attempts())
return GIT_EUSER;
return git_cred_userpass_plaintext_new(out, username, passphrase);
return git_cred_userpass_plaintext_new(out, username.c_str(), passphrase.c_str());
}
if (allowed_types & GIT_CREDTYPE_USERNAME)
return git_cred_username_new(out, username);
return git_cred_username_new(out, username.c_str());
report_error("No supported ssh authentication.");
return GIT_EUSER;
@ -281,10 +281,10 @@ int credential_https_cb(git_cred **out,
if (exceeded_auth_attempts())
return GIT_EUSER;
const char *username = prefs.cloud_storage_email_encoded;
const char *password = prefs.cloud_storage_password ? prefs.cloud_storage_password : "";
const std::string &username = prefs.cloud_storage_email_encoded;
const std::string &password = prefs.cloud_storage_password;
return git_cred_userpass_plaintext_new(out, username, password);
return git_cred_userpass_plaintext_new(out, username.c_str(), password.c_str());
}
int certificate_check_cb(git_cert *cert, int valid, const char *host, void *)
@ -609,10 +609,10 @@ static std::string getProxyString()
{
if (prefs.proxy_type == QNetworkProxy::HttpProxy) {
if (prefs.proxy_auth)
return format_string_std("http://%s:%s@%s:%d", prefs.proxy_user, prefs.proxy_pass,
prefs.proxy_host, prefs.proxy_port);
return format_string_std("http://%s:%s@%s:%d", prefs.proxy_user.c_str(), prefs.proxy_pass.c_str(),
prefs.proxy_host.c_str(), prefs.proxy_port);
else
return format_string_std("http://%s:%d", prefs.proxy_host, prefs.proxy_port);
return format_string_std("http://%s:%d", prefs.proxy_host.c_str(), prefs.proxy_port);
}
return std::string();
}
@ -990,7 +990,7 @@ std::string extract_username(struct git_info *info, const std::string &url)
* Ugly, ugly. Parsing the remote repo user name also sets
* it in the preferences. We should do this somewhere else!
*/
prefs.cloud_storage_email_encoded = strdup(info->username.c_str());
prefs.cloud_storage_email_encoded = info->username;
return url.substr(at + 1 - url.c_str());
}
@ -1107,7 +1107,7 @@ bool is_git_repository(const char *filename, struct git_info *info)
*
* This is used to create more user friendly error message and warnings.
*/
info->is_subsurface_cloud = (strstr(info->url.c_str(), prefs.cloud_base_url) != NULL);
info->is_subsurface_cloud = contains(info->url, prefs.cloud_base_url);
return true;
}

View file

@ -32,8 +32,8 @@ static std::string make_default_filename()
return system_default_path() + "/subsurface.xml";
}
const char mac_system_divelist_default_font[] = "Arial";
const char *system_divelist_default_font = mac_system_divelist_default_font;
using namespace std::string_literals;
std::string system_divelist_default_font = "Arial"s;
double system_divelist_default_font_size = -1.0;
void subsurface_OS_pref_setup()
@ -41,22 +41,22 @@ void subsurface_OS_pref_setup()
// nothing
}
bool subsurface_ignore_font(const char*)
bool subsurface_ignore_font(const std::string &)
{
// there are no old default fonts that we would want to ignore
return false;
}
const char *system_default_directory()
std::string system_default_directory()
{
static const std::string path = system_default_path();
return path.c_str();
return path;
}
const char *system_default_filename()
std::string system_default_filename()
{
static const std::string fn = make_default_filename();
return fn.c_str();
return fn;
}
int enumerate_devices(device_callback_t, void *, unsigned int)

View file

@ -911,7 +911,7 @@ static std::string fingerprint_file(device_data_t *devdata)
serial = devdata->devinfo.serial;
return format_string_std("%s/fingerprints/%04x.%u",
system_default_directory(),
system_default_directory().c_str(),
model, serial);
}
@ -952,7 +952,7 @@ static void save_fingerprint(device_data_t *devdata)
return;
// Make sure the fingerprints directory exists
std::string dir = format_string_std("%s/fingerprints", system_default_directory());
std::string dir = system_default_directory() + "/fingerprints";
subsurface_mkdir(dir.c_str());
std::string final = fingerprint_file(devdata);

View file

@ -47,8 +47,8 @@ static std::string make_default_filename()
return system_default_path() + "/" + user + ".xml";
}
const char mac_system_divelist_default_font[] = "Arial";
const char *system_divelist_default_font = mac_system_divelist_default_font;
using namespace std::string_literals;
std::string system_divelist_default_font = "Arial"s;
double system_divelist_default_font_size = -1.0;
void subsurface_OS_pref_setup()
@ -56,22 +56,22 @@ void subsurface_OS_pref_setup()
// nothing
}
bool subsurface_ignore_font(const char *)
bool subsurface_ignore_font(const std::string &)
{
// there are no old default fonts to ignore
return false;
}
const char *system_default_directory()
std::string system_default_directory()
{
static const std::string path = system_default_path();
return path.c_str();
}
const char *system_default_filename()
std::string system_default_filename()
{
static const std::string fn = make_default_filename();
return fn.c_str();
return fn;
}
int enumerate_devices(device_callback_t callback, void *userdata, unsigned int transport)

View file

@ -3,153 +3,112 @@
#include "subsurface-string.h"
#include "git-access.h" // for CLOUD_HOST
struct preferences prefs, git_prefs;
struct preferences default_prefs = {
.animation_speed = 500,
.cloud_base_url = "https://" CLOUD_HOST_EU "/", // if we don't know any better, use the European host
struct preferences prefs, git_prefs, default_prefs;
preferences::preferences() :
animation_speed(500),
cloud_base_url("https://" CLOUD_HOST_EU "/"), // if we don't know any better, use the European host
#if defined(SUBSURFACE_MOBILE)
.cloud_timeout = 10,
cloud_timeout(10),
#else
.cloud_timeout = 5,
cloud_timeout(5),
#endif
.sync_dc_time = false,
.display_invalid_dives = false,
.divelist_font = NULL,
.font_size = -1,
.mobile_scale = 1.0,
.show_developer = true,
.three_m_based_grid = false,
.map_short_names = false,
.default_cylinder = NULL,
.include_unused_tanks = false,
.display_default_tank_infos = true,
.auto_recalculate_thumbnails = true,
.extract_video_thumbnails = true,
.extract_video_thumbnails_position = 20, // The first fifth seems like a reasonable place
.ffmpeg_executable = NULL,
.defaultsetpoint = 1100,
.default_filename = NULL,
.default_file_behavior = LOCAL_DEFAULT_FILE,
.o2consumption = 720,
.pscr_ratio = 100,
.use_default_file = true,
.extraEnvironmentalDefault = false,
.salinityEditDefault = false,
.geocoding = {
.category = { TC_NONE, TC_NONE, TC_NONE }
},
.date_format = NULL,
.date_format_override = false,
.date_format_short = NULL,
.locale = {
.use_system_language = true,
},
.time_format = NULL,
.time_format_override = false,
.proxy_auth = false,
.proxy_host = NULL,
.proxy_port = 0,
.proxy_type = 0,
.proxy_user = NULL,
.proxy_pass = NULL,
.ascratelast6m = 9000 / 60,
.ascratestops = 9000 / 60,
.ascrate50 = 9000 / 60,
.ascrate75 = 9000 / 60,
.bestmixend = { 30000 },
.bottompo2 = 1400,
.bottomsac = 20000,
.decopo2 = 1600,
.decosac = 17000,
.descrate = 18000 / 60,
.display_duration = true,
.display_runtime = true,
.display_transitions = true,
.display_variations = false,
.doo2breaks = false,
.dobailout = false,
.o2narcotic = true,
.drop_stone_mode = false,
.last_stop = false,
.min_switch_duration = 60,
.surface_segment = 0,
.planner_deco_mode = BUEHLMANN,
.problemsolvingtime = 4,
.reserve_gas=40000,
.sacfactor = 400,
.safetystop = true,
.switch_at_req_stop = false,
.verbatim_plan = false,
.calcalltissues = false,
.calcceiling = false,
.calcceiling3m = false,
.calcndltts = false,
.decoinfo = true,
.dcceiling = true,
.display_deco_mode = BUEHLMANN,
.ead = false,
.gfhigh = 75,
.gflow = 30,
.gf_low_at_maxdepth = false,
.hrgraph = false,
.mod = false,
.modpO2 = 1.6,
.percentagegraph = false,
.pp_graphs = {
.po2 = false,
.pn2 = false,
.phe = false,
.po2_threshold_min = 0.16,
.po2_threshold_max = 1.6,
.pn2_threshold = 4.0,
.phe_threshold = 13.0,
},
.redceiling = false,
.rulergraph = false,
.show_average_depth = true,
.show_ccr_sensors = false,
.show_ccr_setpoint = false,
.show_icd = false,
.show_pictures_in_profile = true,
.show_sac = false,
.show_scr_ocpo2 = false,
.tankbar = false,
.vpmb_conservatism = 3,
.zoomed_plot = false,
.infobox = true,
.coordinates_traditional = true,
.unit_system = METRIC,
.units = SI_UNITS,
.update_manager = { false, NULL, 0 }
};
/* copy a preferences block, including making copies of all included strings */
void copy_prefs(struct preferences *src, struct preferences *dest)
sync_dc_time(false),
display_invalid_dives(false),
font_size(-1),
mobile_scale(1.0),
show_developer(true),
three_m_based_grid(false),
map_short_names(false),
include_unused_tanks(false),
display_default_tank_infos(true),
auto_recalculate_thumbnails(true),
extract_video_thumbnails(true),
extract_video_thumbnails_position(20), // The first fifth seems like a reasonable place
defaultsetpoint(1100),
default_file_behavior(LOCAL_DEFAULT_FILE),
o2consumption(720),
pscr_ratio(100),
use_default_file(true),
extraEnvironmentalDefault(false),
salinityEditDefault(false),
date_format_override(false),
time_format_override(false),
proxy_auth(false),
proxy_port(0),
proxy_type(0),
ascratelast6m(9000 / 60),
ascratestops(9000 / 60),
ascrate50(9000 / 60),
ascrate75(9000 / 60),
bestmixend({ 30000 }),
bottompo2(1400),
bottomsac(20000),
decopo2(1600),
decosac(17000),
descrate(18000 / 60),
display_duration(true),
display_runtime(true),
display_transitions(true),
display_variations(false),
doo2breaks(false),
dobailout(false),
o2narcotic(true),
drop_stone_mode(false),
last_stop(false),
min_switch_duration(60),
surface_segment(0),
planner_deco_mode(BUEHLMANN),
problemsolvingtime(4),
reserve_gas(40000),
sacfactor(400),
safetystop(true),
switch_at_req_stop(false),
verbatim_plan(false),
calcalltissues(false),
calcceiling(false),
calcceiling3m(false),
calcndltts(false),
decoinfo(true),
dcceiling(true),
display_deco_mode(BUEHLMANN),
ead(false),
gfhigh(75),
gflow(30),
gf_low_at_maxdepth(false),
hrgraph(false),
mod(false),
modpO2(1.6),
percentagegraph(false),
redceiling(false),
rulergraph(false),
show_average_depth(true),
show_ccr_sensors(false),
show_ccr_setpoint(false),
show_icd(false),
show_pictures_in_profile(true),
show_sac(false),
show_scr_ocpo2(false),
tankbar(false),
vpmb_conservatism(3),
zoomed_plot(false),
infobox(true),
coordinates_traditional(true),
unit_system(METRIC),
units(SI_UNITS)
{
*dest = *src;
dest->divelist_font = copy_string(src->divelist_font);
dest->default_filename = copy_string(src->default_filename);
dest->default_cylinder = copy_string(src->default_cylinder);
dest->cloud_base_url = copy_string(src->cloud_base_url);
dest->proxy_host = copy_string(src->proxy_host);
dest->proxy_user = copy_string(src->proxy_user);
dest->proxy_pass = copy_string(src->proxy_pass);
dest->time_format = copy_string(src->time_format);
dest->date_format = copy_string(src->date_format);
dest->date_format_short = copy_string(src->date_format_short);
dest->cloud_storage_password = copy_string(src->cloud_storage_password);
dest->cloud_storage_email = copy_string(src->cloud_storage_email);
dest->cloud_storage_email_encoded = copy_string(src->cloud_storage_email_encoded);
dest->ffmpeg_executable = copy_string(src->ffmpeg_executable);
}
/*
* Free strduped prefs before exit.
*
* These are not real leaks but they plug the holes found by eg.
* valgrind so you can find the real leaks.
*/
void free_prefs()
preferences::~preferences() = default;
void set_git_prefs(std::string_view prefs)
{
// nop
if (contains(prefs, "TANKBAR"))
git_prefs.tankbar = 1;
if (contains(prefs, "SHOW_SETPOINT"))
git_prefs.show_ccr_setpoint = 1;
if (contains(prefs, "SHOW_SENSORS"))
git_prefs.show_ccr_sensors = 1;
if (contains(prefs, "PO2_GRAPH"))
git_prefs.pp_graphs.po2 = 1;
}

View file

@ -5,24 +5,27 @@
#include "units.h"
#include "taxonomy.h"
#include <string>
#include <string_view>
struct partial_pressure_graphs_t {
bool po2;
bool pn2;
bool phe;
double po2_threshold_min;
double po2_threshold_max;
double pn2_threshold;
double phe_threshold;
bool po2 = false;
bool pn2 = false;
bool phe = false;
double po2_threshold_min = 0.16;
double po2_threshold_max = 1.6;
double pn2_threshold = 4.0;
double phe_threshold = 13.0;
};
struct geocoding_prefs_t {
enum taxonomy_category category[3];
enum taxonomy_category category[3] = { TC_NONE, TC_NONE, TC_NONE };
};
struct locale_prefs_t {
const char *language;
const char *lang_locale;
bool use_system_language;
std::string language;
std::string lang_locale;
bool use_system_language = true;
};
enum deco_mode {
@ -39,16 +42,16 @@ enum def_file_behavior {
};
struct update_manager_prefs_t {
bool dont_check_for_updates;
const char *last_version_used;
int next_check;
bool dont_check_for_updates = false;
std::string last_version_used;
int next_check = 0;
};
struct dive_computer_prefs_t {
const char *vendor;
const char *product;
const char *device;
const char *device_name;
std::string vendor;
std::string product;
std::string device;
std::string device_name;
};
// NOTE: these enums are duplicated in mobile-widgets/qmlinterface.h
@ -74,11 +77,11 @@ struct preferences {
// ********** CloudStorage **********
bool cloud_auto_sync;
const char *cloud_base_url;
const char *cloud_storage_email;
const char *cloud_storage_email_encoded;
const char *cloud_storage_password;
const char *cloud_storage_pin;
std::string cloud_base_url;
std::string cloud_storage_email;
std::string cloud_storage_email_encoded;
std::string cloud_storage_password;
std::string cloud_storage_pin;
int cloud_timeout;
int cloud_verification_status;
bool save_password_local;
@ -93,7 +96,7 @@ struct preferences {
// ********** Display *************
bool display_invalid_dives;
const char *divelist_font;
std::string divelist_font;
double font_size;
double mobile_scale;
bool show_developer;
@ -101,7 +104,7 @@ struct preferences {
bool map_short_names;
// ********** Equipment tab *******
const char *default_cylinder;
std::string default_cylinder;
bool include_unused_tanks;
bool display_default_tank_infos;
@ -109,9 +112,9 @@ struct preferences {
bool auto_recalculate_thumbnails;
bool extract_video_thumbnails;
int extract_video_thumbnails_position; // position in stream: 0=first 100=last second
const char *ffmpeg_executable; // path of ffmpeg binary
std::string ffmpeg_executable; // path of ffmpeg binary
int defaultsetpoint; // default setpoint in mbar
const char *default_filename;
std::string default_filename;
enum def_file_behavior default_file_behavior;
int o2consumption; // ml per min
int pscr_ratio; // dump ratio times 1000
@ -123,20 +126,20 @@ struct preferences {
geocoding_prefs_t geocoding;
// ********** Language **********
const char *date_format;
std::string date_format;
bool date_format_override;
const char *date_format_short;
std::string date_format_short;
locale_prefs_t locale; //: TODO: move the rest of locale based info here.
const char *time_format;
std::string time_format;
bool time_format_override;
// ********** Network **********
bool proxy_auth;
const char *proxy_host;
std::string proxy_host;
int proxy_port;
int proxy_type;
const char *proxy_user;
const char *proxy_pass;
std::string proxy_user;
std::string proxy_pass;
// ********** Planner **********
int ascratelast6m;
@ -206,19 +209,22 @@ struct preferences {
// ********** UpdateManager **********
update_manager_prefs_t update_manager;
preferences(); // Initialize to default
~preferences();
};
extern struct preferences prefs, default_prefs, git_prefs;
extern const char *system_divelist_default_font;
extern std::string system_divelist_default_font;
extern double system_divelist_default_font_size;
extern const char *system_default_directory();
extern const char *system_default_filename();
extern bool subsurface_ignore_font(const char *font);
extern std::string system_default_directory();
extern std::string system_default_filename();
extern bool subsurface_ignore_font(const std::string &font);
extern void subsurface_OS_pref_setup();
extern void copy_prefs(struct preferences *src, struct preferences *dest);
extern void set_informational_units(const char *units);
extern void set_git_prefs(std::string_view prefs);
#endif // PREF_H

View file

@ -9,7 +9,7 @@
#include "errorhelper.h"
#include "core/settings/qPref.h"
char *settings_suffix = NULL;
std::string settings_suffix;
static QTranslator qtTranslator, ssrfTranslator, parentLanguageTranslator;
void init_qt_late()
@ -29,17 +29,17 @@ void init_qt_late()
QGuiApplication::setDesktopFileName("subsurface");
#endif
// enable user specific settings (based on command line argument)
if (settings_suffix) {
if (!settings_suffix.empty()) {
if (verbose)
#if defined(SUBSURFACE_MOBILE) && ((defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) || (defined(Q_OS_DARWIN) && !defined(Q_OS_IOS)))
report_info("using custom config for Subsurface-Mobile-%s", settings_suffix);
report_info("using custom config for Subsurface-Mobile-%s", settings_suffix.c_str());
#else
report_info("using custom config for Subsurface-%s", settings_suffix);
report_info("using custom config for Subsurface-%s", settings_suffix.c_str());
#endif
#if defined(SUBSURFACE_MOBILE) && ((defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) || (defined(Q_OS_DARWIN) && !defined(Q_OS_IOS)))
QCoreApplication::setApplicationName(QString("Subsurface-Mobile-%1").arg(settings_suffix));
QCoreApplication::setApplicationName(QString::fromStdString("Subsurface-Mobile-" + settings_suffix));
#else
QCoreApplication::setApplicationName(QString("Subsurface-%1").arg(settings_suffix));
QCoreApplication::setApplicationName(QString::fromStdString("Subsurface-" + settings_suffix));
#endif
} else {
#if defined(SUBSURFACE_MOBILE) && ((defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)) || (defined(Q_OS_DARWIN) && !defined(Q_OS_IOS)))

View file

@ -402,7 +402,7 @@ std::string subsurface_user_agent()
QString getUiLanguage()
{
return prefs.locale.lang_locale;
return QString::fromStdString(prefs.locale.lang_locale);
}
static std::vector<std::string> get_languages(const QLocale &loc)
@ -457,10 +457,9 @@ void initUiLanguage()
#endif
}
free((void*)prefs.locale.lang_locale);
prefs.locale.lang_locale = strdup(uiLang.c_str());
prefs.locale.lang_locale = uiLang;
if (!prefs.date_format_override || empty_string(prefs.date_format)) {
if (!prefs.date_format_override || prefs.date_format.empty()) {
// derive our standard date format from what the locale gives us
// the long format uses long weekday and month names, so replace those with the short ones
// for time we don't want the time zone designator and don't want leading zeroes on the hours
@ -469,23 +468,20 @@ void initUiLanguage()
// special hack for Swedish as our switching from long weekday names to short weekday names
// messes things up there
dateFormat.replace("'en' 'den' d:'e'", " d");
free((void *)prefs.date_format);
prefs.date_format = copy_qstring(dateFormat);
prefs.date_format = dateFormat.toStdString();
}
if (!prefs.date_format_override || empty_string(prefs.date_format_short)) {
if (!prefs.date_format_override || prefs.date_format_short.empty()) {
// derive our standard date format from what the locale gives us
shortDateFormat = loc.dateFormat(QLocale::ShortFormat);
free((void *)prefs.date_format_short);
prefs.date_format_short = copy_qstring(shortDateFormat);
prefs.date_format_short = shortDateFormat.toStdString();
}
if (!prefs.time_format_override || empty_string(prefs.time_format)) {
if (!prefs.time_format_override || prefs.time_format.empty()) {
timeFormat = loc.timeFormat();
timeFormat.replace("(t)", "").replace(" t", "").replace("t", "").replace("hh", "h").replace("HH", "H").replace("'kl'.", "");
timeFormat.replace(".ss", "").replace(":ss", "").replace("ss", "");
free((void *)prefs.time_format);
prefs.time_format = copy_qstring(timeFormat);
prefs.time_format = timeFormat.toStdString();
}
}
@ -683,7 +679,7 @@ static const char *printing_templates = "printing_templates";
QString getPrintingTemplatePathUser()
{
// Function-local statics are initialized on first invocation
static QString path(QString(system_default_directory()) +
static QString path(QString::fromStdString(system_default_directory()) +
QDir::separator() +
QString(printing_templates));
return path;
@ -958,7 +954,7 @@ QString get_dive_date_string(timestamp_t when)
{
QDateTime ts;
ts.setMSecsSinceEpoch(when * 1000L);
return loc.toString(ts.toUTC(), QString(prefs.date_format) + " " + prefs.time_format);
return loc.toString(ts.toUTC(), QString::fromStdString(prefs.date_format + " " + prefs.time_format));
}
// Get local seconds since Epoch from ISO formatted UTC date time + offset string
@ -971,7 +967,7 @@ QString get_short_dive_date_string(timestamp_t when)
{
QDateTime ts;
ts.setMSecsSinceEpoch(when * 1000L);
return loc.toString(ts.toUTC(), QString(prefs.date_format_short) + " " + prefs.time_format);
return loc.toString(ts.toUTC(), QString::fromStdString(prefs.date_format_short + " " + prefs.time_format));
}
std::string get_dive_date_c_string(timestamp_t when)
@ -983,7 +979,7 @@ static QString get_dive_only_date_string(timestamp_t when)
{
QDateTime ts;
ts.setMSecsSinceEpoch(when * 1000L);
return loc.toString(ts.toUTC(), QString(prefs.date_format));
return loc.toString(ts.toUTC(), QString::fromStdString(prefs.date_format));
}
QString get_first_dive_date_string()
@ -1003,7 +999,7 @@ std::string get_current_date()
QDateTime ts(QDateTime::currentDateTime());;
QString current_date;
current_date = loc.toString(ts, QString(prefs.date_format_short));
current_date = loc.toString(ts, QString::fromStdString(prefs.date_format_short));
return current_date.toStdString();
}
@ -1018,7 +1014,7 @@ std::string hashfile_name()
static QString thumbnailDir()
{
return QString(system_default_directory()) + "/thumbnails/";
return QString::fromStdString(system_default_directory() + "/thumbnails/");
}
// Calculate thumbnail filename by hashing name of file.
@ -1329,14 +1325,11 @@ std::optional<std::string> getCloudURL()
{
std::string email(prefs.cloud_storage_email);
sanitize_email(email);
if (email.empty() || empty_string(prefs.cloud_storage_password)) {
if (email.empty() || prefs.cloud_storage_password.empty()) {
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());
}
prefs.cloud_storage_email_encoded = email;
std::string filename = std::string(prefs.cloud_base_url) + "git/" + email + "[" + email + "]";
if (verbose)
report_info("returning cloud URL %s", filename.c_str());
@ -1359,11 +1352,11 @@ void init_proxy()
{
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::ProxyType(prefs.proxy_type));
proxy.setHostName(prefs.proxy_host);
proxy.setHostName(QString::fromStdString(prefs.proxy_host));
proxy.setPort(prefs.proxy_port);
if (prefs.proxy_auth) {
proxy.setUser(prefs.proxy_user);
proxy.setPassword(prefs.proxy_pass);
proxy.setUser(QString::fromStdString(prefs.proxy_user));
proxy.setPassword(QString::fromStdString(prefs.proxy_pass));
}
QNetworkProxy::setApplicationProxy(proxy);
}

View file

@ -27,11 +27,10 @@ HANDLE_PREFERENCE_BOOL(CloudStorage, "cloud_auto_sync", cloud_auto_sync);
void qPrefCloudStorage::set_cloud_base_url(const QString &value)
{
if (value != prefs.cloud_base_url) {
if (value.toStdString() != prefs.cloud_base_url) {
// only free and set if not default
if (prefs.cloud_base_url != default_prefs.cloud_base_url) {
qPrefPrivate::copy_txt(&prefs.cloud_base_url, value);
}
if (prefs.cloud_base_url != default_prefs.cloud_base_url)
prefs.cloud_base_url = value.toStdString();
disk_cloud_base_url(true);
emit instance()->cloud_base_urlChanged(value);
@ -42,14 +41,15 @@ void qPrefCloudStorage::store_cloud_base_url(const QString &value)
{
// this is used if we want to update the on-disk settings without changing
// the runtime preference
qPrefPrivate::propSetValue(keyFromGroupAndName(group, "cloud_base_url"), value, default_prefs.cloud_base_url);
qPrefPrivate::propSetValue(keyFromGroupAndName(group, "cloud_base_url"), value, QString::fromStdString(default_prefs.cloud_base_url));
}
void qPrefCloudStorage::disk_cloud_base_url(bool doSync)
{
// we don't allow to automatically write back the prefs value for the cloud_base_url.
// in order to do that you need to use the explicit function above store_cloud_base_url()
if (!doSync)
prefs.cloud_base_url = copy_qstring(qPrefPrivate::propValue(keyFromGroupAndName(group, "cloud_base_url"), default_prefs.cloud_base_url).toString());
prefs.cloud_base_url = qPrefPrivate::propValue(keyFromGroupAndName(group, "cloud_base_url"),
QString::fromStdString(default_prefs.cloud_base_url)).toString().toStdString();
}
HANDLE_PREFERENCE_TXT(CloudStorage, "email", cloud_storage_email);
@ -58,8 +58,8 @@ HANDLE_PREFERENCE_TXT(CloudStorage, "email_encoded", cloud_storage_email_encoded
void qPrefCloudStorage::set_cloud_storage_password(const QString &value)
{
if (value != prefs.cloud_storage_password) {
qPrefPrivate::copy_txt(&prefs.cloud_storage_password, value);
if (value.toStdString() != prefs.cloud_storage_password) {
prefs.cloud_storage_password = value.toStdString();
disk_cloud_storage_password(true);
emit instance()->cloud_storage_passwordChanged(value);
}
@ -68,9 +68,11 @@ void qPrefCloudStorage::disk_cloud_storage_password(bool doSync)
{
if (doSync) {
if (prefs.save_password_local)
qPrefPrivate::propSetValue(keyFromGroupAndName(group, "password"), prefs.cloud_storage_password, default_prefs.cloud_storage_password);
qPrefPrivate::propSetValue(keyFromGroupAndName(group, "password"), prefs.cloud_storage_password,
default_prefs.cloud_storage_password);
} else {
prefs.cloud_storage_password = copy_qstring(qPrefPrivate::propValue(keyFromGroupAndName(group, "password"), default_prefs.cloud_storage_password).toString());
prefs.cloud_storage_password = qPrefPrivate::propValue(keyFromGroupAndName(group, "password"),
default_prefs.cloud_storage_password).toString().toStdString();
}
}
@ -84,17 +86,17 @@ HANDLE_PREFERENCE_BOOL(CloudStorage, "save_password_local", save_password_local)
QString qPrefCloudStorage::diveshare_uid()
{
return qPrefPrivate::propValue(keyFromGroupAndName("", "diveshareExport/uid"), "").toString();
return qPrefPrivate::propValue(keyFromGroupAndName("", "diveshareExport/uid"), QString()).toString();
}
void qPrefCloudStorage::set_diveshare_uid(const QString &value)
{
qPrefPrivate::propSetValue(keyFromGroupAndName("", "diveshareExport/uid"), value, "");
qPrefPrivate::propSetValue(keyFromGroupAndName("", "diveshareExport/uid"), value, QString());
emit instance()->diveshare_uidChanged(value);
}
bool qPrefCloudStorage::diveshare_private()
{
return qPrefPrivate::propValue(keyFromGroupAndName("", "diveshareExport/private"), "").toBool();
return qPrefPrivate::propValue(keyFromGroupAndName("", "diveshareExport/private"), QString()).toBool();
}
void qPrefCloudStorage::set_diveshare_private(bool value)
{
@ -104,21 +106,20 @@ void qPrefCloudStorage::set_diveshare_private(bool value)
QString qPrefCloudStorage::divelogde_user()
{
return qPrefPrivate::propValue(keyFromGroupAndName("", "divelogde_user"), "").toString();
return qPrefPrivate::propValue(keyFromGroupAndName("", "divelogde_user"), QString()).toString();
}
void qPrefCloudStorage::set_divelogde_user(const QString &value)
{
qPrefPrivate::propSetValue(keyFromGroupAndName("", "divelogde_user"), value, "");
qPrefPrivate::propSetValue(keyFromGroupAndName("", "divelogde_user"), value, QString());
emit instance()->divelogde_userChanged(value);
}
QString qPrefCloudStorage::divelogde_pass()
{
return qPrefPrivate::propValue(keyFromGroupAndName("", "divelogde_pass"), "").toString();
return qPrefPrivate::propValue(keyFromGroupAndName("", "divelogde_pass"), QString()).toString();
}
void qPrefCloudStorage::set_divelogde_pass(const QString &value)
{
qPrefPrivate::propSetValue(keyFromGroupAndName("", "divelogde_pass"), value, "");
qPrefPrivate::propSetValue(keyFromGroupAndName("", "divelogde_pass"), value, QString());
emit instance()->divelogde_passChanged(value);
}

View file

@ -41,11 +41,11 @@ public:
Q_ENUM(cloud_status);
static bool cloud_auto_sync() { return prefs.cloud_auto_sync; }
static QString cloud_base_url() { return prefs.cloud_base_url; }
static QString cloud_storage_email() { return prefs.cloud_storage_email; }
static QString cloud_storage_email_encoded() { return prefs.cloud_storage_email_encoded; }
static QString cloud_storage_password() { return prefs.cloud_storage_password; }
static QString cloud_storage_pin() { return prefs.cloud_storage_pin; }
static QString cloud_base_url() { return QString::fromStdString(prefs.cloud_base_url); }
static QString cloud_storage_email() { return QString::fromStdString(prefs.cloud_storage_email); }
static QString cloud_storage_email_encoded() { return QString::fromStdString(prefs.cloud_storage_email_encoded); }
static QString cloud_storage_password() { return QString::fromStdString(prefs.cloud_storage_password); }
static QString cloud_storage_pin() { return QString::fromStdString(prefs.cloud_storage_pin); }
static int cloud_timeout() { return prefs.cloud_timeout; }
static int cloud_verification_status() { return prefs.cloud_verification_status; }
static bool save_password_local() { return prefs.save_password_local; }

View file

@ -77,9 +77,9 @@ void qPrefDisplay::set_divelist_font(const QString &value)
if (value.contains(","))
newValue = value.left(value.indexOf(","));
if (newValue != prefs.divelist_font &&
!subsurface_ignore_font(qPrintable(newValue))) {
qPrefPrivate::copy_txt(&prefs.divelist_font, value);
if (newValue.toStdString() != prefs.divelist_font &&
!subsurface_ignore_font(newValue.toStdString())) {
prefs.divelist_font = value.toStdString();
disk_divelist_font(true);
qApp->setFont(QFont(newValue));
@ -170,12 +170,10 @@ void qPrefDisplay::setCorrectFont()
QString fontName = defaultFont.toString();
if (fontName.contains(","))
fontName = fontName.left(fontName.indexOf(","));
if (subsurface_ignore_font(qPrintable(fontName))) {
defaultFont = QFont(prefs.divelist_font);
} else {
free((void *)prefs.divelist_font);
prefs.divelist_font = copy_qstring(fontName);
}
if (subsurface_ignore_font(fontName.toStdString()))
defaultFont = QFont(QString::fromStdString(prefs.divelist_font));
else
prefs.divelist_font = fontName.toStdString();
defaultFont.setPointSizeF(prefs.font_size * prefs.mobile_scale);
qApp->setFont(defaultFont);

View file

@ -38,7 +38,7 @@ public:
public:
static int animation_speed() { return prefs.animation_speed; }
static QString divelist_font() { return prefs.divelist_font; }
static QString divelist_font() { return QString::fromStdString(prefs.divelist_font); }
static double font_size() { return prefs.font_size; }
static double mobile_scale() { return prefs.mobile_scale; }
static bool display_invalid_dives() { return prefs.display_invalid_dives; }

View file

@ -6,11 +6,11 @@
#include <QObject>
#define IMPLEMENT5GETTERS(name) \
static QString name() { return prefs.dive_computer.name; } \
static QString name##1() { return prefs.dive_computer##1 .name; } \
static QString name##2() { return prefs.dive_computer##2 .name; } \
static QString name##3() { return prefs.dive_computer##3 .name; } \
static QString name##4() { return prefs.dive_computer##4 .name; }
static QString name() { return QString::fromStdString(prefs.dive_computer.name); } \
static QString name##1() { return QString::fromStdString(prefs.dive_computer##1 .name); } \
static QString name##2() { return QString::fromStdString(prefs.dive_computer##2 .name); } \
static QString name##3() { return QString::fromStdString(prefs.dive_computer##3 .name); } \
static QString name##4() { return QString::fromStdString(prefs.dive_computer##4 .name); }
class qPrefDiveComputer : public QObject {
Q_OBJECT

View file

@ -20,7 +20,7 @@ public:
static void sync() { loadSync(true); }
public:
static QString default_cylinder() { return prefs.default_cylinder; }
static QString default_cylinder() { return QString::fromStdString(prefs.default_cylinder); }
static bool include_unused_tanks() { return prefs.include_unused_tanks; }
static bool display_default_tank_infos() { return prefs.display_default_tank_infos; }

View file

@ -25,12 +25,12 @@ public:
static void sync() { loadSync(true); }
public:
static const QString date_format() { return prefs.date_format; }
static const QString date_format() { return QString::fromStdString(prefs.date_format); }
static bool date_format_override() { return prefs.date_format_override; }
static const QString date_format_short() { return prefs.date_format_short; }
static const QString language() { return prefs.locale.language; }
static const QString lang_locale() { return prefs.locale.lang_locale; }
static const QString time_format() { return prefs.time_format; }
static const QString date_format_short() { return QString::fromStdString(prefs.date_format_short); }
static const QString language() { return QString::fromStdString(prefs.locale.language); }
static const QString lang_locale() { return QString::fromStdString(prefs.locale.lang_locale); }
static const QString time_format() { return QString::fromStdString(prefs.time_format); }
static bool time_format_override() { return prefs.time_format_override; }
static bool use_system_language() { return prefs.locale.use_system_language; }

View file

@ -28,7 +28,7 @@ void qPrefLog::set_default_file_behavior(enum def_file_behavior value)
if (value == UNDEFINED_DEFAULT_FILE) {
// undefined, so check if there's a filename set and
// use that, otherwise go with no default file
prefs.default_file_behavior = QString(prefs.default_filename).isEmpty() ? NO_DEFAULT_FILE : LOCAL_DEFAULT_FILE;
prefs.default_file_behavior = prefs.default_filename.empty() ? NO_DEFAULT_FILE : LOCAL_DEFAULT_FILE;
} else {
prefs.default_file_behavior = value;
}
@ -45,7 +45,7 @@ void qPrefLog::disk_default_file_behavior(bool doSync)
if (prefs.default_file_behavior == UNDEFINED_DEFAULT_FILE)
// undefined, so check if there's a filename set and
// use that, otherwise go with no default file
prefs.default_file_behavior = QString(prefs.default_filename).isEmpty() ? NO_DEFAULT_FILE : LOCAL_DEFAULT_FILE;
prefs.default_file_behavior = prefs.default_filename.empty() ? NO_DEFAULT_FILE : LOCAL_DEFAULT_FILE;
}
}
@ -58,4 +58,3 @@ HANDLE_PREFERENCE_BOOL(Log, "use_default_file", use_default_file);
HANDLE_PREFERENCE_BOOL(Log, "salinityEditDefault", salinityEditDefault);
HANDLE_PREFERENCE_BOOL(Log, "show_average_depth", show_average_depth);

View file

@ -23,7 +23,7 @@ public:
static void sync() { return loadSync(true); }
public:
static QString default_filename() { return prefs.default_filename; }
static QString default_filename() { return QString::fromStdString(prefs.default_filename); }
static enum def_file_behavior default_file_behavior() { return prefs.default_file_behavior; }
static bool use_default_file() { return prefs.use_default_file; }
static bool extraEnvironmentalDefault() { return prefs.extraEnvironmentalDefault; }

View file

@ -23,4 +23,3 @@ HANDLE_PREFERENCE_BOOL(Media, "auto_recalculate_thumbnails", auto_recalculate_th
HANDLE_PREFERENCE_BOOL(Media, "extract_video_thumbnails", extract_video_thumbnails);
HANDLE_PREFERENCE_INT(Media, "extract_video_thumbnails_position", extract_video_thumbnails_position);
HANDLE_PREFERENCE_TXT(Media, "ffmpeg_executable", ffmpeg_executable);

View file

@ -24,7 +24,7 @@ public:
static bool auto_recalculate_thumbnails() { return prefs.auto_recalculate_thumbnails; }
static bool extract_video_thumbnails() { return prefs.extract_video_thumbnails; }
static int extract_video_thumbnails_position() { return prefs.extract_video_thumbnails_position; }
static QString ffmpeg_executable() { return prefs.ffmpeg_executable; }
static QString ffmpeg_executable() { return QString::fromStdString(prefs.ffmpeg_executable); }
public slots:
static void set_auto_recalculate_thumbnails(bool value);

View file

@ -4,12 +4,6 @@
#include <QSettings>
void qPrefPrivate::copy_txt(const char **name, const QString &string)
{
free((void *)*name);
*name = copy_qstring(string);
}
QString keyFromGroupAndName(QString group, QString name)
{
QString slash = (group.endsWith('/') || name.startsWith('/')) ? "" : "/";
@ -35,8 +29,19 @@ void qPrefPrivate::propSetValue(const QString &key, const QVariant &value, const
s.remove(key);
}
void qPrefPrivate::propSetValue(const QString &key, const std::string &value, const std::string &defaultValue)
{
propSetValue(key, QString::fromStdString(value), QString::fromStdString(defaultValue));
}
QVariant qPrefPrivate::propValue(const QString &key, const QVariant &defaultValue)
{
QSettings s;
return s.value(key, defaultValue);
}
QVariant qPrefPrivate::propValue(const QString &key, const std::string &defaultValue)
{
QSettings s;
return s.value(key, QString::fromStdString(defaultValue));
}

View file

@ -16,10 +16,10 @@ class qPrefPrivate {
public:
// Helper functions
static void copy_txt(const char **name, const QString &string);
static void propSetValue(const QString &key, const QVariant &value, const QVariant &defaultValue);
static void propSetValue(const QString &key, const std::string &value, const std::string &defaultValue);
static QVariant propValue(const QString &key, const QVariant &defaultValue);
static QVariant propValue(const QString &key, const std::string &defaultValue);
private:
qPrefPrivate() {}
@ -134,29 +134,29 @@ extern QString keyFromGroupAndName(QString group, QString name);
#define DISK_LOADSYNC_TXT_EXT(usegroup, name, field, usestruct) \
void qPref##usegroup::disk_##field(bool doSync) \
{ \
static QString current_state; \
static std::string current_state; \
if (doSync) { \
if (current_state != QString(prefs.usestruct field)) { \
current_state = QString(prefs.usestruct field); \
qPrefPrivate::propSetValue(keyFromGroupAndName(group, name), prefs.usestruct field, default_prefs.usestruct field); \
if (current_state != prefs.usestruct field) { \
current_state = prefs.usestruct field; \
qPrefPrivate::propSetValue(keyFromGroupAndName(group, name), QString::fromStdString(prefs.usestruct field), QString::fromStdString(default_prefs.usestruct field)); \
} \
} else { \
prefs.usestruct field = copy_qstring(qPrefPrivate::propValue(keyFromGroupAndName(group, name), default_prefs.usestruct field).toString()); \
current_state = QString(prefs.usestruct field); \
prefs.usestruct field = qPrefPrivate::propValue(keyFromGroupAndName(group, name), QString::fromStdString(default_prefs.usestruct field)).toString().toStdString(); \
current_state = prefs.usestruct field; \
} \
}
#define DISK_LOADSYNC_TXT_EXT_ALT(usegroup, name, field, usestruct, alt) \
void qPref##usegroup::disk_##field##alt(bool doSync) \
{ \
static QString current_state; \
static std::string current_state; \
if (doSync) { \
if (current_state != QString(prefs.usestruct ## alt .field)) { \
current_state = QString(prefs.usestruct ## alt .field); \
qPrefPrivate::propSetValue(keyFromGroupAndName(group, name), prefs.usestruct ##alt .field, default_prefs.usestruct ##alt .field); \
if (current_state != prefs.usestruct ## alt .field) { \
current_state = prefs.usestruct ## alt .field; \
qPrefPrivate::propSetValue(keyFromGroupAndName(group, name), QString::fromStdString(prefs.usestruct ##alt .field), QString::fromStdString(default_prefs.usestruct ##alt .field)); \
} \
} else { \
prefs.usestruct ##alt .field = copy_qstring(qPrefPrivate::propValue(keyFromGroupAndName(group, name), default_prefs.usestruct ##alt .field).toString()); \
current_state = QString(prefs.usestruct ##alt .field); \
prefs.usestruct ##alt .field = qPrefPrivate::propValue(keyFromGroupAndName(group, name), QString::fromStdString(default_prefs.usestruct ##alt .field)).toString().toStdString(); \
current_state = prefs.usestruct ##alt .field; \
} \
}
#define DISK_LOADSYNC_TXT(usegroup, name, field) \
@ -226,8 +226,8 @@ extern QString keyFromGroupAndName(QString group, QString name);
#define SET_PREFERENCE_TXT_EXT(usegroup, field, usestruct) \
void qPref##usegroup::set_##field(const QString &value) \
{ \
if (value != prefs.usestruct field) { \
qPrefPrivate::copy_txt(&prefs.usestruct field, value); \
if (value.toStdString() != prefs.usestruct field) { \
prefs.usestruct field = value.toStdString(); \
disk_##field(true); \
emit instance()->field##Changed(value); \
} \
@ -236,8 +236,8 @@ extern QString keyFromGroupAndName(QString group, QString name);
#define SET_PREFERENCE_TXT_EXT_ALT(usegroup, field, usestruct, alt) \
void qPref##usegroup::set_##field##alt(const QString &value) \
{ \
if (value != prefs.usestruct ##alt .field) { \
qPrefPrivate::copy_txt(&prefs.usestruct ##alt .field, value); \
if (value.toStdString() != prefs.usestruct ##alt .field) { \
prefs.usestruct ##alt .field = value.toStdString(); \
disk_##field##alt(true); \
emit instance()->field##alt##Changed(value); \
} \

View file

@ -25,11 +25,11 @@ public:
public:
static bool proxy_auth() { return prefs.proxy_auth; }
static QString proxy_host() { return prefs.proxy_host; }
static QString proxy_pass() { return prefs.proxy_pass; }
static QString proxy_host() { return QString::fromStdString(prefs.proxy_host); }
static QString proxy_pass() { return QString::fromStdString(prefs.proxy_pass); }
static int proxy_port() { return prefs.proxy_port; }
static int proxy_type() { return prefs.proxy_type; }
static QString proxy_user() { return prefs.proxy_user; }
static QString proxy_user() { return QString::fromStdString(prefs.proxy_user); }
public slots:
static void set_proxy_auth(bool value);

View file

@ -41,7 +41,7 @@ void qPrefUpdateManager::disk_next_check(bool doSync)
if (doSync)
qPrefPrivate::propSetValue(keyFromGroupAndName(group, "NextCheck"), prefs.update_manager.next_check, default_prefs.update_manager.next_check);
else
prefs.update_manager.next_check = qPrefPrivate::propValue(keyFromGroupAndName(group, "NextCheck"), 0).toInt();
prefs.update_manager.next_check = qPrefPrivate::propValue(keyFromGroupAndName(group, "NextCheck"), QVariant(0)).toInt();
}
HANDLE_PROP_QSTRING(UpdateManager, "UpdateManager/UUID", uuidString);

View file

@ -23,7 +23,7 @@ public:
public:
static bool dont_check_for_updates() { return prefs.update_manager.dont_check_for_updates; }
static const QString last_version_used() { return prefs.update_manager.last_version_used; }
static const QString last_version_used() { return QString::fromStdString(prefs.update_manager.last_version_used); }
static const QDate next_check() { return QDate::fromJulianDay(prefs.update_manager.next_check); }
static const QString uuidString() { return st_uuidString; }

View file

@ -241,20 +241,20 @@ QString formatDiveGPS(const dive *d)
QString formatDiveDate(const dive *d)
{
QDateTime localTime = timestampToDateTime(d->when);
return localTime.date().toString(prefs.date_format_short);
return localTime.date().toString(QString::fromStdString(prefs.date_format_short));
}
QString formatDiveTime(const dive *d)
{
QDateTime localTime = timestampToDateTime(d->when);
return localTime.time().toString(prefs.time_format);
return localTime.time().toString(QString::fromStdString(prefs.time_format));
}
QString formatDiveDateTime(const dive *d)
{
QDateTime localTime = timestampToDateTime(d->when);
return QStringLiteral("%1 %2").arg(localTime.date().toString(prefs.date_format_short),
localTime.time().toString(prefs.time_format));
return QStringLiteral("%1 %2").arg(localTime.date().toString(QString::fromStdString(prefs.date_format_short)),
localTime.time().toString(QString::fromStdString(prefs.time_format)));
}
QString formatDiveGasString(const dive *d)
@ -308,7 +308,7 @@ QString formatTripTitle(const dive_trip &trip)
QString prefix = !trip.location.empty() ? QString::fromStdString(trip.location) + ", " : QString();
if (getday)
return prefix + loc.toString(localTime, prefs.date_format);
return prefix + loc.toString(localTime, QString::fromStdString(prefs.date_format));
else
return prefix + loc.toString(localTime, "MMM yyyy");
}

View file

@ -27,11 +27,6 @@ static inline bool empty_string(const char *s)
return !s || !*s;
}
static inline char *copy_string(const char *s)
{
return (s && *s) ? strdup(s) : NULL;
}
extern double permissive_strtod(const char *str, const char **ptr);
extern double ascii_strtod(const char *str, const char **ptr);
@ -47,6 +42,16 @@ inline bool contains(std::string_view s, char c)
return s.find(c) != std::string::npos;
}
inline bool contains(std::string_view haystack, const char *needle)
{
return haystack.find(needle) != std::string::npos;
}
inline bool contains(std::string_view haystack, const std::string &needle)
{
return haystack.find(needle) != std::string::npos;
}
std::string join(const std::vector<std::string> &l, const std::string &separator, bool skip_empty = false);
#endif // SUBSURFACE_STRING_H

View file

@ -49,8 +49,8 @@ void print_files()
std::optional<std::string> filename;
printf("\nFile locations:\n\n");
printf("Cloud email:%s\n", prefs.cloud_storage_email);
if (!empty_string(prefs.cloud_storage_email) && !empty_string(prefs.cloud_storage_password)) {
printf("Cloud email:%s\n", prefs.cloud_storage_email.c_str());
if (!prefs.cloud_storage_email.empty() && !prefs.cloud_storage_password.empty()) {
filename = getCloudURL();
if (filename)
is_git_repository(filename->c_str(), &info);
@ -107,7 +107,7 @@ void parse_argument(const char *arg)
/* long options with -- */
/* first test for --user=bla which allows the use of user specific settings */
if (strncmp(arg, "--user=", sizeof("--user=") - 1) == 0) {
settings_suffix = strdup(arg + sizeof("--user=") - 1);
settings_suffix = arg + sizeof("--user=") - 1;
return;
}
if (strncmp(arg, "--cloud-timeout=", sizeof("--cloud-timeout=") - 1) == 0) {
@ -144,15 +144,15 @@ void parse_argument(const char *arg)
}
#if SUBSURFACE_DOWNLOADER
if (strncmp(arg, "--dc-vendor=", sizeof("--dc-vendor=") - 1) == 0) {
prefs.dive_computer.vendor = strdup(arg + sizeof("--dc-vendor=") - 1);
prefs.dive_computer.vendor = arg + sizeof("--dc-vendor=") - 1;
return;
}
if (strncmp(arg, "--dc-product=", sizeof("--dc-product=") - 1) == 0) {
prefs.dive_computer.product = strdup(arg + sizeof("--dc-product=") - 1);
prefs.dive_computer.product = arg + sizeof("--dc-product=") - 1;
return;
}
if (strncmp(arg, "--device=", sizeof("--device=") - 1) == 0) {
prefs.dive_computer.device = strdup(arg + sizeof("--device=") - 1);
prefs.dive_computer.device = arg + sizeof("--device=") - 1;
return;
}
if (strncmp(arg, "--list-dc", sizeof("--list-dc") - 1) == 0) {
@ -194,12 +194,12 @@ void setup_system_prefs()
const char *env;
subsurface_OS_pref_setup();
default_prefs.divelist_font = strdup(system_divelist_default_font);
default_prefs.divelist_font = system_divelist_default_font;
default_prefs.font_size = system_divelist_default_font_size;
default_prefs.ffmpeg_executable = strdup("ffmpeg");
default_prefs.ffmpeg_executable = "ffmpeg";
#if !defined(SUBSURFACE_MOBILE)
default_prefs.default_filename = copy_string(system_default_filename());
default_prefs.default_filename = system_default_filename();
#endif
env = getenv("LC_MEASUREMENT");
if (!env)

View file

@ -2,19 +2,19 @@
#ifndef SUBSURFACESTARTUP_H
#define SUBSURFACESTARTUP_H
#include <string>
extern bool imported;
extern int quit, force_root, ignore_bt;
void setup_system_prefs();
void parse_argument(const char *arg);
void free_prefs();
void print_files();
void print_version();
extern char *settings_suffix;
extern std::string settings_suffix;
#ifdef SUBSURFACE_MOBILE_DESKTOP
#include <string>
extern std::string testqml;
#endif

View file

@ -27,9 +27,9 @@ extern const char *taxonomy_category_names[TC_NR_CATEGORIES];
extern const char *taxonomy_api_names[TC_NR_CATEGORIES];
struct taxonomy {
taxonomy_category category; /* the category for this tag: ocean, country, admin_l1, admin_l2, localname, etc */
std::string value; /* the value returned, parsed, or manually entered for that category */
taxonomy_origin origin;
taxonomy_category category = TC_NONE; /* the category for this tag: ocean, country, admin_l1, admin_l2, localname, etc */
std::string value; /* the value returned, parsed, or manually entered for that category */
taxonomy_origin origin = GEOCODED;
};
/* the data block contains taxonomy structures - unused ones have a tag value of NONE */

View file

@ -35,8 +35,8 @@ static std::string make_default_filename()
}
// the DE should provide us with a default font and font size...
const char unix_system_divelist_default_font[] = "Sans";
const char *system_divelist_default_font = unix_system_divelist_default_font;
using namespace std::string_literals;
std::string system_divelist_default_font = "Sans"s;
double system_divelist_default_font_size = -1.0;
void subsurface_OS_pref_setup()
@ -44,22 +44,22 @@ void subsurface_OS_pref_setup()
// nothing
}
bool subsurface_ignore_font(const char *)
bool subsurface_ignore_font(const std::string &)
{
// there are no old default fonts to ignore
return false;
}
const char *system_default_directory()
std::string system_default_directory()
{
static const std::string path = system_default_path();
return path.c_str();
return path;
}
const char *system_default_filename()
std::string system_default_filename()
{
static const std::string fn = make_default_filename();
return fn.c_str();
return fn;
}
int enumerate_devices(device_callback_t callback, void *userdata, unsigned int transport)

View file

@ -83,7 +83,7 @@ void VideoFrameExtractor::processItem(QString originalFilename, QString filename
.arg(position.seconds % 60, 2, 10, QChar('0'));
QProcess ffmpeg;
ffmpeg.start(prefs.ffmpeg_executable, QStringList {
ffmpeg.start(prefs.ffmpeg_executable.c_str(), QStringList {
"-ss", posString, "-i", filename, "-vframes", "1", "-q:v", "2", "-f", "image2", "-"
});
if (!ffmpeg.waitForStarted()) {

View file

@ -99,40 +99,39 @@ static std::wstring make_default_filename()
return path + L"\\" + filename;
}
const char non_standard_system_divelist_default_font[] = "Calibri";
const char current_system_divelist_default_font[] = "Segoe UI";
const char *system_divelist_default_font = non_standard_system_divelist_default_font;
using namespace std::string_literals;
static std::string non_standard_system_divelist_default_font = "Calibri"s;
static std::string current_system_divelist_default_font = "Segoe UI"s;
std::string system_divelist_default_font;
double system_divelist_default_font_size = -1;
void subsurface_OS_pref_setup()
{
if (isWin7Or8())
system_divelist_default_font = current_system_divelist_default_font;
system_divelist_default_font = isWin7Or8() ? current_system_divelist_default_font
: non_standard_system_divelist_default_font;
}
bool subsurface_ignore_font(const char *font)
bool subsurface_ignore_font(const std::string &font)
{
// if this is running on a recent enough version of Windows and the font
// passed in is the pre 4.3 default font, ignore it
if (isWin7Or8() && strcmp(font, non_standard_system_divelist_default_font) == 0)
return true;
return false;
return isWin7Or8() && font == non_standard_system_divelist_default_font;
}
#define utf8_to_utf16(s) utf8_to_utf16_fl(s, __FILE__, __LINE__)
/* '\' not included at the end.
*/
const char *system_default_directory()
std::string system_default_directory()
{
static std::string path = utf16_to_utf8(system_default_path());
return path.c_str();
return path;
}
const char *system_default_filename()
std::string system_default_filename()
{
static std::string path = utf16_to_utf8(make_default_filename());
return path.c_str();
return path;
}
int enumerate_devices(device_callback_t callback, void *userdata, unsigned int transport)