From c5eb806adb345e24b13ac4bf106cceb1dc9d3431 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sat, 10 Apr 2021 17:40:30 -0700 Subject: [PATCH] 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 --- core/checkcloudconnection.cpp | 1 + core/file.c | 10 +++------- core/git-access.c | 2 +- core/pref.c | 1 - core/pref.h | 1 - core/qthelper.cpp | 4 ++-- core/settings/qPrefCloudStorage.cpp | 2 -- core/settings/qPrefCloudStorage.h | 2 -- desktop-widgets/mainwindow.cpp | 13 ++++++------- tests/testgitstorage.cpp | 3 +-- tests/testqPrefCloudStorage.cpp | 13 ------------- tests/tst_qPrefCloudStorage.qml | 3 --- 12 files changed, 14 insertions(+), 41 deletions(-) diff --git a/core/checkcloudconnection.cpp b/core/checkcloudconnection.cpp index 98b87aef3d..b9a7d3b0e5 100644 --- a/core/checkcloudconnection.cpp +++ b/core/checkcloudconnection.cpp @@ -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" diff --git a/core/file.c b/core/file.c index d1cbc3c623..e53d39f4d0 100644 --- a/core/file.c +++ b/core/file.c @@ -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); diff --git a/core/git-access.c b/core/git-access.c index 891ecf1e20..0936fa3991 100644 --- a/core/git-access.c +++ b/core/git-access.c @@ -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); } diff --git a/core/pref.c b/core/pref.c index bc1f7f3ea3..61619c0d12 100644 --- a/core/pref.c +++ b/core/pref.c @@ -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); diff --git a/core/pref.h b/core/pref.h index 410280a106..97590c4dc3 100644 --- a/core/pref.h +++ b/core/pref.h @@ -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; diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 702ee965de..6678cc87ff 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -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; } diff --git a/core/settings/qPrefCloudStorage.cpp b/core/settings/qPrefCloudStorage.cpp index 24b73394b0..6caf619ddd 100644 --- a/core/settings/qPrefCloudStorage.cpp +++ b/core/settings/qPrefCloudStorage.cpp @@ -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"); } } diff --git a/core/settings/qPrefCloudStorage.h b/core/settings/qPrefCloudStorage.h index b0d24227a3..d30f26ae02 100644 --- a/core/settings/qPrefCloudStorage.h +++ b/core/settings/qPrefCloudStorage.h @@ -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 @@ class qPrefCloudStorage : public QObject { 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; } diff --git a/desktop-widgets/mainwindow.cpp b/desktop-widgets/mainwindow.cpp index 197c54bdd8..9a2b9665d7 100644 --- a/desktop-widgets/mainwindow.cpp +++ b/desktop-widgets/mainwindow.cpp @@ -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) : diff --git a/tests/testgitstorage.cpp b/tests/testgitstorage.cpp index f916b4e40d..95ad3c3086 100644 --- a/tests/testgitstorage.cpp +++ b/tests/testgitstorage.cpp @@ -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) + diff --git a/tests/testqPrefCloudStorage.cpp b/tests/testqPrefCloudStorage.cpp index 9d55ca743b..a4e58ed823 100644 --- a/tests/testqPrefCloudStorage.cpp +++ b/tests/testqPrefCloudStorage.cpp @@ -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() diff --git a/tests/tst_qPrefCloudStorage.qml b/tests/tst_qPrefCloudStorage.qml index 7bc137d203..682fe16699 100644 --- a/tests/tst_qPrefCloudStorage.qml +++ b/tests/tst_qPrefCloudStorage.qml @@ -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")