cloudstorage: some cleanup of cloud url handling

We know the preference is never empty, so stop testing for this. But
don't maintain two different preferences with basically the same
content. Instead add the '/git' suffix where needed and keep this all in
one place.

Simplify the extraction of the branch name from the cloud URL.

Also a typo fix and a new comment.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2021-04-10 17:40:30 -07:00
parent da6395a4a8
commit c5eb806adb
12 changed files with 14 additions and 41 deletions

View file

@ -19,6 +19,7 @@ CheckCloudConnection::CheckCloudConnection(QObject *parent) :
}
// our own madeup API to make sure we are talking to a Subsurface cloud server
#define TEAPOT "/make-latte?number-of-shots=3"
#define HTTP_I_AM_A_TEAPOT 418
#define MILK "Linus does not like non-fat milk"

View file

@ -284,9 +284,7 @@ int check_git_sha(const char *filename, struct git_repository **git_p, const cha
*git_p = git;
if (branch_p)
*branch_p = branch;
if (prefs.cloud_git_url &&
strstr(filename, prefs.cloud_git_url)
&& git == dummy_git_repository) {
if (strstr(filename, prefs.cloud_base_url) && git == dummy_git_repository) {
/* opening the cloud storage repository failed for some reason,
* so we don't know if there is additional data in the remote */
free(current_sha);
@ -317,13 +315,11 @@ int parse_file(const char *filename, struct dive_table *table, struct trip_table
int ret;
git = is_git_repository(filename, &branch, NULL, false);
if (prefs.cloud_git_url &&
strstr(filename, prefs.cloud_git_url)
&& git == dummy_git_repository) {
if (strstr(filename, prefs.cloud_base_url) && git == dummy_git_repository)
/* opening the cloud storage repository failed for some reason
* give up here and don't send errors about git repositories */
return -1;
}
if (git)
return git_load_dives(git, branch, table, trips, sites, devices, filter_presets);

View file

@ -1023,7 +1023,7 @@ static struct git_repository *is_remote_git_repository(char *remote, const char
/* remember if the current git storage we are working on is our cloud storage
* this is used to create more user friendly error message and warnings */
is_subsurface_cloud = strstr(remote, prefs.cloud_git_url) != NULL;
is_subsurface_cloud = strstr(remote, prefs.cloud_base_url) != NULL;
return get_remote_repo(localdir, remote, branch);
}

View file

@ -103,7 +103,6 @@ void copy_prefs(struct preferences *src, struct preferences *dest)
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->cloud_git_url = copy_string(src->cloud_git_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);

View file

@ -82,7 +82,6 @@ struct preferences {
// ********** CloudStorage **********
bool cloud_auto_sync;
const char *cloud_base_url;
const char *cloud_git_url;
const char *cloud_storage_email;
const char *cloud_storage_email_encoded;
const char *cloud_storage_password;

View file

@ -1442,9 +1442,9 @@ int getCloudURL(QString &filename)
free((void *)prefs.cloud_storage_email_encoded);
prefs.cloud_storage_email_encoded = copy_qstring(email);
}
filename = QString(QString(prefs.cloud_git_url) + "/%1[%1]").arg(email);
filename = QString(QString(prefs.cloud_base_url) + "git/%1[%1]").arg(email);
if (verbose)
qDebug() << "cloud URL set as" << filename;
qDebug() << "returning cloud URL" << filename;
return 0;
}

View file

@ -31,7 +31,6 @@ void qPrefCloudStorage::set_cloud_base_url(const QString &value)
// 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);
qPrefPrivate::copy_txt(&prefs.cloud_git_url, value + "/git");
}
disk_cloud_base_url(true);
@ -44,7 +43,6 @@ void qPrefCloudStorage::disk_cloud_base_url(bool doSync)
qPrefPrivate::propSetValue(keyFromGroupAndName(group, "cloud_base_url"), prefs.cloud_base_url, default_prefs.cloud_base_url);
} else {
prefs.cloud_base_url = copy_qstring(qPrefPrivate::propValue(keyFromGroupAndName(group, "cloud_base_url"), default_prefs.cloud_base_url).toString());
qPrefPrivate::copy_txt(&prefs.cloud_git_url, QString(prefs.cloud_base_url) + "/git");
}
}

View file

@ -9,7 +9,6 @@ class qPrefCloudStorage : public QObject {
Q_OBJECT
Q_PROPERTY(bool cloud_auto_sync READ cloud_auto_sync WRITE set_cloud_auto_sync NOTIFY cloud_auto_syncChanged)
Q_PROPERTY(QString cloud_base_url READ cloud_base_url WRITE set_cloud_base_url NOTIFY cloud_base_urlChanged)
Q_PROPERTY(QString cloud_git_url READ cloud_git_url)
Q_PROPERTY(QString cloud_storage_email READ cloud_storage_email WRITE set_cloud_storage_email NOTIFY cloud_storage_emailChanged)
Q_PROPERTY(QString cloud_storage_email_encoded READ cloud_storage_email_encoded WRITE set_cloud_storage_email_encoded NOTIFY cloud_storage_email_encodedChanged)
Q_PROPERTY(QString cloud_storage_password READ cloud_storage_password WRITE set_cloud_storage_password NOTIFY cloud_storage_passwordChanged)
@ -43,7 +42,6 @@ public:
static bool cloud_auto_sync() { return prefs.cloud_auto_sync; }
static QString cloud_base_url() { return prefs.cloud_base_url; }
static QString cloud_git_url() { return prefs.cloud_git_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; }

View file

@ -628,8 +628,8 @@ void MainWindow::closeCurrentFile()
void MainWindow::updateCloudOnlineStatus()
{
bool is_cloud = existing_filename && prefs.cloud_git_url && prefs.cloud_verification_status == qPrefCloudStorage::CS_VERIFIED &&
strstr(existing_filename, prefs.cloud_git_url);
bool is_cloud = existing_filename && prefs.cloud_verification_status == qPrefCloudStorage::CS_VERIFIED &&
strstr(existing_filename, prefs.cloud_base_url);
ui.actionCloudOnline->setEnabled(is_cloud);
ui.actionCloudOnline->setChecked(is_cloud && !git_local_only);
}
@ -1174,7 +1174,7 @@ void MainWindow::loadRecentFiles()
QString file = s.value(key).toString();
// never add our cloud URL to the recent files
if (!same_string(prefs.cloud_git_url, "") && file.startsWith(prefs.cloud_git_url))
if (file.startsWith(prefs.cloud_base_url))
continue;
// but allow local git repos
QRegularExpression gitrepo("(.*)\\[[^]]+]");
@ -1212,7 +1212,7 @@ void MainWindow::updateRecentFilesMenu()
void MainWindow::addRecentFile(const QString &file, bool update)
{
// never add Subsurface cloud file to the recent files - it has its own menu entry
if (!same_string(prefs.cloud_git_url, "") && file.startsWith(prefs.cloud_git_url))
if (file.startsWith(prefs.cloud_base_url))
return;
QString localFile = QDir::toNativeSeparators(file);
int index = recentFiles.indexOf(localFile);
@ -1262,9 +1262,8 @@ int MainWindow::file_save_as(void)
// 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
if (default_filename && strstr(default_filename, prefs.cloud_git_url)) {
if (default_filename && strstr(default_filename, prefs.cloud_base_url)) {
QString filename(default_filename);
filename.remove(prefs.cloud_git_url);
filename.remove(0, filename.indexOf("[") + 1);
filename.replace("]", ".ssrf");
default_filename = copy_qstring(filename);
@ -1355,7 +1354,7 @@ QString MainWindow::displayedFilename(QString fullFilename)
QFileInfo fileInfo(f);
QString fileName(fileInfo.fileName());
if (fullFilename.contains(prefs.cloud_git_url)) {
if (fullFilename.contains(prefs.cloud_base_url)) {
QString email = fileName.left(fileName.indexOf('['));
return git_local_only ?
tr("[local cache for] %1").arg(email) :

View file

@ -111,14 +111,13 @@ void TestGitStorage::initTestCase()
if (gitUrl.right(1) != "/")
gitUrl += "/";
gitUrl += "git";
prefs.cloud_git_url = copy_qstring(gitUrl);
prefs.cloud_storage_email_encoded = copy_qstring(email);
prefs.cloud_storage_password = copy_qstring(password);
gitUrl += "/" + email;
// all user storage for historical reasons always uses the user's email both as
// repo name and as branch. To allow us to keep testing and not step on parallel
// runs we'll use actually random branch names - yes, this still has a chance of
// conflict, but I'm not going to implement a distributed locak manager for this
// conflict, but I'm not going to implement a distributed lock manager for this
if (email.startsWith("gitstorage")) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
randomBranch = QString::number(QRandomGenerator::global()->bounded(0x1000000), 16) +

View file

@ -23,7 +23,6 @@ void TestQPrefCloudStorage::test_struct_get()
prefs.cloud_auto_sync = true;
prefs.cloud_base_url = copy_qstring("new url");
prefs.cloud_git_url = copy_qstring("new again url");
prefs.cloud_storage_email = copy_qstring("myEmail");
prefs.cloud_storage_email_encoded = copy_qstring("encodedMyEMail");
prefs.cloud_storage_password = copy_qstring("more secret");
@ -34,7 +33,6 @@ void TestQPrefCloudStorage::test_struct_get()
QCOMPARE(tst->cloud_auto_sync(), prefs.cloud_auto_sync);
QCOMPARE(tst->cloud_base_url(), QString(prefs.cloud_base_url));
QCOMPARE(tst->cloud_git_url(), QString(prefs.cloud_git_url));
QCOMPARE(tst->cloud_storage_email(), QString(prefs.cloud_storage_email));
QCOMPARE(tst->cloud_storage_email_encoded(), QString(prefs.cloud_storage_email_encoded));
QCOMPARE(tst->cloud_storage_password(), QString(prefs.cloud_storage_password));
@ -69,9 +67,6 @@ void TestQPrefCloudStorage::test_set_struct()
QCOMPARE((int)prefs.cloud_timeout, 123);
QCOMPARE((int)prefs.cloud_verification_status, (int)qPrefCloudStorage::CS_VERIFIED);
QCOMPARE(prefs.save_password_local, false);
// remark is set with set_base_url
QCOMPARE(QString(prefs.cloud_git_url), QString("t2 base/git"));
}
void TestQPrefCloudStorage::test_set_load_struct()
@ -92,7 +87,6 @@ void TestQPrefCloudStorage::test_set_load_struct()
prefs.cloud_auto_sync = false;
prefs.cloud_base_url = copy_qstring("error1");
prefs.cloud_git_url = copy_qstring("error1");
prefs.cloud_storage_email = copy_qstring("error1");
prefs.cloud_storage_email_encoded = copy_qstring("error1");
prefs.cloud_storage_password = copy_qstring("error1");
@ -111,9 +105,6 @@ void TestQPrefCloudStorage::test_set_load_struct()
QCOMPARE((int)prefs.cloud_timeout, 321);
QCOMPARE((int)prefs.cloud_verification_status, (int)qPrefCloudStorage::CS_NOCLOUD);
QCOMPARE(prefs.save_password_local, true);
// remark is set with set_base_url
QCOMPARE(QString(prefs.cloud_git_url), QString("t3 base/git"));
}
void TestQPrefCloudStorage::test_struct_disk()
@ -136,7 +127,6 @@ void TestQPrefCloudStorage::test_struct_disk()
prefs.cloud_auto_sync = false;
prefs.cloud_base_url = copy_qstring("error1");
prefs.cloud_git_url = copy_qstring("error1");
prefs.cloud_storage_email = copy_qstring("error1");
prefs.cloud_storage_email_encoded = copy_qstring("error1");
prefs.cloud_storage_password = copy_qstring("error1");
@ -156,9 +146,6 @@ void TestQPrefCloudStorage::test_struct_disk()
QCOMPARE((int)prefs.cloud_timeout, 123);
QCOMPARE((int)prefs.cloud_verification_status, (int)qPrefCloudStorage::CS_VERIFIED);
QCOMPARE(prefs.save_password_local, true);
// remark is set with set_base_url
QCOMPARE(QString(prefs.cloud_git_url), QString("t4 base/git"));
}
void TestQPrefCloudStorage::test_multiple()

View file

@ -11,9 +11,6 @@ TestCase {
PrefCloudStorage.cloud_base_url = "my url"
compare(PrefCloudStorage.cloud_base_url, "my url")
var x2 = PrefCloudStorage.cloud_git_url
compare(PrefCloudStorage.cloud_git_url, "my url/git")
var x3 = PrefCloudStorage.cloud_storage_email
PrefCloudStorage.cloud_storage_email = "my email"
compare(PrefCloudStorage.cloud_storage_email, "my email")