-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsurface-downloader-main.cpp
141 lines (127 loc) · 4.18 KB
/
subsurface-downloader-main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-License-Identifier: GPL-2.0
/* main.c */
#include "core/downloadfromdcthread.h" // for fill_computer_list
#include "core/errorhelper.h"
#include "core/parse.h"
#include "core/qthelper.h"
#include "core/settings/qPref.h"
#include "core/tag.h"
#include "core/dive.h"
#include "core/divelog.h"
#include "core/subsurfacestartup.h"
#include "core/subsurface-string.h"
#include "core/file.h"
#include "core/trip.h"
#include "core/libdivecomputer.h"
#include "commands/command.h"
#include <QApplication>
#include <QLoggingCategory>
#include <QStringList>
#include <git2.h>
static void messageHandler(QtMsgType type, const QMessageLogContext &ctx, const QString &msg);
extern void cliDownloader(const std::string &vendor, const std::string &product, const std::string &device);
int main(int argc, char **argv)
{
Command::init();
qInstallMessageHandler(messageHandler);
// we always run this in verbose mode as there is no UI
verbose = 1;
// now let's say Hi
print_version();
// supporting BT makes sense when used with an iPhone and an rfcomm BT device?
QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
bool no_filenames = true;
std::unique_ptr<QCoreApplication> app(new QCoreApplication(argc, argv));
std::vector<std::string> files;
std::vector<std::string> importedFiles;
QStringList arguments = QCoreApplication::arguments();
// set a default logfile name for libdivecomputer so we always get a logfile
logfile_name = "subsurface-downloader.log";
subsurface_mkdir(system_default_directory().c_str());
if (subsurface_user_is_root() && !force_root) {
printf("You are running Subsurface as root. This is not recommended.\n");
printf("If you insist to do so, run with option --allow_run_as_root.\n");
exit(0);
}
git_libgit2_init();
setup_system_prefs();
prefs = default_prefs;
// now handle the arguments
fill_computer_list();
for (int i = 1; i < arguments.length(); i++) {
std::string a = arguments[i].toStdString();
if (a.empty())
continue;
if (a[0] == '-') {
parse_argument(a.c_str());
continue;
}
if (imported) {
importedFiles.push_back(a);
} else {
no_filenames = false;
files.push_back(a);
}
}
parse_xml_init();
taglist_init_global();
if (no_filenames) {
if (prefs.default_file_behavior == LOCAL_DEFAULT_FILE) {
if (!prefs.default_filename.empty())
files.emplace_back(prefs.default_filename.c_str());
} else if (prefs.default_file_behavior == CLOUD_DEFAULT_FILE) {
auto cloudURL = getCloudURL();
if (cloudURL)
files.emplace_back(*cloudURL);
}
}
if (!files.empty()) {
report_info("loading dive data from %s", join(files, ", ").c_str());
if (parse_file(files.front().c_str(), &divelog) < 0) {
printf("Failed to load dives from file '%s', aborting.\n", files.front().c_str());
exit(1);
}
}
print_files();
if (!quit) {
if (!prefs.dive_computer.vendor.empty() && !prefs.dive_computer.product.empty() && !prefs.dive_computer.device.empty()) {
// download from that dive computer
printf("Downloading dives from %s %s (via %s)\n", prefs.dive_computer.vendor.c_str(),
prefs.dive_computer.product.c_str(), prefs.dive_computer.device.c_str());
cliDownloader(prefs.dive_computer.vendor, prefs.dive_computer.product, prefs.dive_computer.device);
}
}
if (!files.empty()) {
report_info("saving dive data to %s", join(files, ", ").c_str());
save_dives(files.front().c_str());
} else {
printf("No log files given, not saving dive data.\n");
printf("Give a log file name as argument, or configure a cloud URL.\n");
}
parse_xml_exit();
// Sync struct preferences to disk
qPref::sync();
return 0;
}
// install this message handler primarily so that the Windows build can log to files
void messageHandler(QtMsgType type, const QMessageLogContext &, const QString &msg)
{
QByteArray localMsg = msg.toUtf8();
switch (type) {
case QtDebugMsg:
fprintf(stdout, "%s\n", localMsg.constData());
break;
case QtInfoMsg:
fprintf(stdout, "%s\n", localMsg.constData());
break;
case QtWarningMsg:
fprintf(stderr, "%s\n", localMsg.constData());
break;
case QtCriticalMsg:
fprintf(stderr, "%s\n", localMsg.constData());
break;
case QtFatalMsg:
fprintf(stderr, "%s\n", localMsg.constData());
abort();
}
}