void QMLManager::loadDivesWithValidCredentials() { QString url; if (getCloudURL(url)) { QString errorString(get_error_string()); appendTextToLog(errorString); setStartPageText(RED_FONT + tr("Cloud storage error: %1").arg(errorString) + END_FONT); setAccessingCloud(-1); alreadySaving = false; return; } QByteArray fileNamePrt = QFile::encodeName(url); git_repository *git; const char *branch; int error; if (check_git_sha(fileNamePrt.data(), &git, &branch) == 0) { qDebug() << "local cache was current, no need to modify dive list"; appendTextToLog("Cloud sync shows local cache was current"); setLoadFromCloud(true); setAccessingCloud(-1); alreadySaving = false; return; } appendTextToLog("Cloud sync brought newer data, reloading the dive list"); clear_dive_file_data(); if (git != dummy_git_repository) { appendTextToLog(QString("have repository and branch %1").arg(branch)); error = git_load_dives(git, branch); } else { appendTextToLog(QString("didn't receive valid git repo, try again")); error = parse_file(fileNamePrt.data()); } setAccessingCloud(-1); if (!error) { report_error("filename is now %s", fileNamePrt.data()); const char *error_string = get_error_string(); appendTextToLog(error_string); set_filename(fileNamePrt.data(), true); } else { report_error("failed to open file %s", fileNamePrt.data()); QString errorString(get_error_string()); appendTextToLog(errorString); setStartPageText(RED_FONT + tr("Cloud storage error: %1").arg(errorString) + END_FONT); alreadySaving = false; return; } prefs.unit_system = informational_prefs.unit_system; if (informational_prefs.unit_system == IMPERIAL) informational_prefs.units = IMPERIAL_units; prefs.units = informational_prefs.units; DiveListModel::instance()->clear(); process_dives(false, false); DiveListModel::instance()->addAllDives(); appendTextToLog(QStringLiteral("%1 dives loaded").arg(dive_table.nr)); if (dive_table.nr == 0) setStartPageText(tr("Cloud storage open successfully. No dives in dive list.")); setLoadFromCloud(true); alreadySaving = false; }
void QMLManager::loadDivesWithValidCredentials() { QString url; timestamp_t currentDiveTimestamp = selectedDiveTimestamp(); if (getCloudURL(url)) { QString errorString(get_error_string()); appendTextToLog(errorString); setStartPageText(RED_FONT + tr("Cloud storage error: %1").arg(errorString) + END_FONT); revertToNoCloudIfNeeded(); return; } QByteArray fileNamePrt = QFile::encodeName(url); git_repository *git; const char *branch; int error; if (check_git_sha(fileNamePrt.data(), &git, &branch) == 0) { qDebug() << "local cache was current, no need to modify dive list"; appendTextToLog("Cloud sync shows local cache was current"); goto successful_exit; } appendTextToLog("Cloud sync brought newer data, reloading the dive list"); clear_dive_file_data(); if (git != dummy_git_repository) { appendTextToLog(QString("have repository and branch %1").arg(branch)); error = git_load_dives(git, branch); } else { appendTextToLog(QString("didn't receive valid git repo, try again")); error = parse_file(fileNamePrt.data()); } setAccessingCloud(-1); if (!error) { report_error("filename is now %s", fileNamePrt.data()); const char *error_string = get_error_string(); appendTextToLog(error_string); set_filename(fileNamePrt.data(), true); } else { report_error("failed to open file %s", fileNamePrt.data()); QString errorString(get_error_string()); appendTextToLog(errorString); revertToNoCloudIfNeeded(); return; } consumeFinishedLoad(currentDiveTimestamp); successful_exit: alreadySaving = false; setLoadFromCloud(true); // if we came from local storage mode, let's merge the local data into the local cache // for the remote data - which then later gets merged with the remote data if necessary if (oldStatus() == NOCLOUD) { git_storage_update_progress(false, "import dives from nocloud local storage"); dive_table.preexisting = dive_table.nr; mergeLocalRepo(); DiveListModel::instance()->clear(); DiveListModel::instance()->addAllDives(); appendTextToLog(QStringLiteral("%1 dives loaded after importing nocloud local storage").arg(dive_table.nr)); saveChangesLocal(); if (syncToCloud() == false) { appendTextToLog(QStringLiteral("taking things back offline now that storage is synced")); prefs.git_local_only = syncToCloud(); } } setAccessingCloud(-1); // if we got here just for an initial connection to the cloud, reset to offline if (currentGitLocalOnly) { currentGitLocalOnly = false; prefs.git_local_only = true; } return; }
int parse_file(const char *filename) { struct git_repository *git; const char *branch; struct memblock mem; char *fmt; int ret; git = is_git_repository(filename, &branch, NULL); if (prefs.cloud_git_url && strstr(filename, prefs.cloud_git_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 0; if (git && !git_load_dives(git, branch)) return 0; if (readfile(filename, &mem) < 0) { /* we don't want to display an error if this was the default file or the cloud storage */ if ((prefs.default_filename && !strcmp(filename, prefs.default_filename)) || isCloudUrl(filename)) return 0; return report_error(translate("gettextFromC", "Failed to read '%s'"), filename); } fmt = strrchr(filename, '.'); if (fmt && (!strcasecmp(fmt + 1, "DB") || !strcasecmp(fmt + 1, "BAK") || !strcasecmp(fmt + 1, "SQL"))) { if (!try_to_open_db(filename, &mem)) { free(mem.buffer); return 0; } } /* Divesoft Freedom */ if (fmt && (!strcasecmp(fmt + 1, "DLF"))) { if (!parse_dlf_buffer(mem.buffer, mem.size)) { free(mem.buffer); return 0; } return -1; } /* DataTrak/Wlog */ if (fmt && !strcasecmp(fmt + 1, "LOG")) { datatrak_import(filename, &dive_table); return 0; } /* OSTCtools */ if (fmt && (!strcasecmp(fmt + 1, "DIVE"))) { ostctools_import(filename, &dive_table); return 0; } ret = parse_file_buffer(filename, &mem); free(mem.buffer); return ret; }
int parse_file(const char *filename) { struct git_repository *git; const char *branch = NULL; char *current_sha = copy_string(saved_git_id); struct memblock mem; char *fmt; 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) { /* opening the cloud storage repository failed for some reason * give up here and don't send errors about git repositories */ free(current_sha); return -1; } /* if this is a git repository, do we already have this exact state loaded ? * get the SHA and compare with what we currently have */ if (git && git != dummy_git_repository) { const char *sha = get_sha(git, branch); if (!same_string(sha, "") && same_string(sha, current_sha) && !unsaved_changes()) { fprintf(stderr, "already have loaded SHA %s - don't load again\n", sha); free(current_sha); return 0; } } free(current_sha); if (git) return git_load_dives(git, branch); if ((ret = readfile(filename, &mem)) < 0) { /* we don't want to display an error if this was the default file or the cloud storage */ if ((prefs.default_filename && !strcmp(filename, prefs.default_filename)) || isCloudUrl(filename)) return 0; return report_error(translate("gettextFromC", "Failed to read '%s'"), filename); } else if (ret == 0) { return report_error(translate("gettextFromC", "Empty file '%s'"), filename); } fmt = strrchr(filename, '.'); if (fmt && (!strcasecmp(fmt + 1, "DB") || !strcasecmp(fmt + 1, "BAK") || !strcasecmp(fmt + 1, "SQL"))) { if (!try_to_open_db(filename, &mem)) { free(mem.buffer); return 0; } } /* Divesoft Freedom */ if (fmt && (!strcasecmp(fmt + 1, "DLF"))) { if (!parse_dlf_buffer(mem.buffer, mem.size)) { free(mem.buffer); return 0; } return -1; } /* DataTrak/Wlog */ if (fmt && !strcasecmp(fmt + 1, "LOG")) { datatrak_import(filename, &dive_table); return 0; } /* OSTCtools */ if (fmt && (!strcasecmp(fmt + 1, "DIVE"))) { ostctools_import(filename, &dive_table); return 0; } ret = parse_file_buffer(filename, &mem); free(mem.buffer); return ret; }