Skip to content

Commit

Permalink
bring static functions into googltakeout class. (#1240)
Browse files Browse the repository at this point in the history
and other tidy modernizations.
  • Loading branch information
tsteven4 authored Nov 23, 2023
1 parent e251cac commit e8c5cc2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 67 deletions.
30 changes: 13 additions & 17 deletions googletakeout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,17 @@
#include "src/core/file.h" // for File
#include "src/core/logging.h" // for Debug, FatalMsg, Warning

#define MYNAME "Google Takeout"
#define TIMELINE_OBJECTS "timelineObjects"

static const QList<QString> takeout_month_names{
"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY",
"AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"
};

static void takeout_fatal(const QString& message) {
void GoogleTakeoutFormat::takeout_fatal(const QString& message) {
fatal(FatalMsg() << MYNAME << ": " << message);
}

static void takeout_warning(const QString& message) {
void GoogleTakeoutFormat::takeout_warning(const QString& message) {
Warning() << MYNAME << ": " << message;
}

/* create a waypoint from late7/lone7 and optional metadata */
static Waypoint* takeout_waypoint(
Waypoint* GoogleTakeoutFormat::takeout_waypoint(
int lat_e7,
int lon_e7,
const QString* shortname,
Expand All @@ -81,7 +74,7 @@ static Waypoint* takeout_waypoint(
return waypoint;
}

static bool track_maybe_add_wpt(route_head* route, Waypoint* waypoint) {
bool GoogleTakeoutFormat::track_maybe_add_wpt(route_head* route, Waypoint* waypoint) {
if (waypoint->latitude == 0 && waypoint->longitude == 0) {
if (global_opts.debug_level >= 2) {
Debug(2) << "Track " << route->rte_name << "@" <<
Expand All @@ -95,7 +88,7 @@ static bool track_maybe_add_wpt(route_head* route, Waypoint* waypoint) {
return true;
}

static QList<QJsonObject> readJson(
QList<QJsonObject> GoogleTakeoutFormat::GoogleTakeoutInputStream::readJson(
const QString& source)
{
if (global_opts.debug_level >= 2) {
Expand Down Expand Up @@ -142,7 +135,7 @@ static QList<QJsonObject> readJson(
return timeline;
}

static QList<QString> readDir(
QList<QString> GoogleTakeoutFormat::GoogleTakeoutInputStream::readDir(
const QString& source)
{
if (global_opts.debug_level >= 2) {
Expand All @@ -158,6 +151,10 @@ static QList<QString> readDir(
* folders
*/
if (baseName.length() == 4 && baseName.toInt() > 0) {
static const QList<QString> takeout_month_names{
"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY",
"AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"
};
for (auto&& month : takeout_month_names) {
const QString path = source + "/" + baseName + "_" + month + ".json";
const QFileInfo info{path};
Expand Down Expand Up @@ -337,7 +334,6 @@ GoogleTakeoutFormat::add_activity_segment(const QJsonObject& activitySegment)
* TODO: do something with simplifiedRawPath
*/
int n_points = 0;
Waypoint* waypoint = nullptr;
auto* route = new route_head;
const QJsonObject startLoc = activitySegment[START_LOCATION].toObject();
const QJsonObject endLoc = activitySegment[END_LOCATION].toObject();
Expand All @@ -347,7 +343,7 @@ GoogleTakeoutFormat::add_activity_segment(const QJsonObject& activitySegment)
track_add_head(route);
QString timestamp;
timestamp = activitySegment[DURATION][START_TIMESTAMP].toString();
waypoint = takeout_waypoint(
Waypoint* waypoint = takeout_waypoint(
startLoc[LOCATION_LATE7].toInt(),
startLoc[LOCATION_LONE7].toInt(),
nullptr, nullptr,
Expand Down Expand Up @@ -404,7 +400,7 @@ GoogleTakeoutFormat::add_activity_segment(const QJsonObject& activitySegment)
return n_points;
}

void GoogleTakeoutInputStream::loadSource(const QString& source) {
void GoogleTakeoutFormat::GoogleTakeoutInputStream::loadSource(const QString& source) {
const QFileInfo info{source};
if (info.isDir()) {
sources += readDir(source);
Expand All @@ -415,7 +411,7 @@ void GoogleTakeoutInputStream::loadSource(const QString& source) {
}
}

QJsonValue GoogleTakeoutInputStream::next() {
QJsonValue GoogleTakeoutFormat::GoogleTakeoutInputStream::next() {
if (!timelineObjects.isEmpty()) {
QJsonValue nextObject = timelineObjects.first();
timelineObjects.removeFirst();
Expand Down
109 changes: 59 additions & 50 deletions googletakeout.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <QJsonValue> // for QJsonValue
#include <QList> // for QList
#include <QString> // for QString
#include <QStringLiteral> // for qMakeStringPrivate, QStringLiteral
#include <QVector> // for QVector

#include "defs.h"
Expand All @@ -38,29 +37,6 @@
*
* TODO: Allow date ranges
*/
class GoogleTakeoutInputStream
{
public:
/* Special Member Functions */
GoogleTakeoutInputStream() = default;
GoogleTakeoutInputStream(const QString& source) : sources({source}) {}

/* Member Functions */

// Returns the next timelineObject, or a null QJsonValue if we're at the end
QJsonValue next();

private:
/* Member Functions */

void loadSource(const QString& source);

/* Data Members */

QList<QString> sources;
QList<QJsonObject> timelineObjects;
};

/* Read-only Google Timeline Location History gpsbabel Format */
class GoogleTakeoutFormat : public Format
{
Expand All @@ -75,7 +51,7 @@ class GoogleTakeoutFormat : public Format

ff_type get_type() const override
{
return ff_type_file;
return ff_type_file;
}

QVector<ff_cap> get_cap() const override
Expand All @@ -90,35 +66,68 @@ class GoogleTakeoutFormat : public Format
private:
/* Constants */

const QString PLACE_VISIT = QStringLiteral("placeVisit");
const QString ACTIVITY_SEGMENT = QStringLiteral("activitySegment");
const QString ACTIVITY_TYPE = QStringLiteral("activityType");
const QString LOCATION = QStringLiteral("location");
const QString LOCATION_LATE7 = QStringLiteral("latitudeE7");
const QString LOCATION_LONE7 = QStringLiteral("longitudeE7");
const QString NAME = QStringLiteral("name");
const QString ADDRESS = QStringLiteral("address");
const QString DURATION = QStringLiteral("duration");
const QString START_TIMESTAMP = QStringLiteral("startTimestamp");
const QString START_LOCATION = QStringLiteral("startLocation");
const QString END_TIMESTAMP = QStringLiteral("endTimestamp");
const QString END_LOCATION = QStringLiteral("endLocation");
const QString TIMESTAMP = QStringLiteral("timestamp");
const QString SIMPLE_PATH = QStringLiteral("simplifiedRawPath");
const QString POINTS = QStringLiteral("points");
const QString WAYPOINT_PATH = QStringLiteral("waypointPath");
const QString WAYPOINTS = QStringLiteral("waypoints");
// for some reason that probably only a former Google engineer knows,);
// we use = QStringLiteral("latE7"/"lngE7" here instead of "latitudeE7"/"longitudeE7".);
// +10 points for brevity, but -100 points for inconsistency.);
const QString LATE7 = QStringLiteral("latE7");
const QString LONE7 = QStringLiteral("lngE7");
static constexpr char MYNAME[] = "Google Takeout";
static constexpr char TIMELINE_OBJECTS[] = "timelineObjects";
static constexpr char16_t PLACE_VISIT[] = u"placeVisit";
static constexpr char16_t ACTIVITY_SEGMENT[] = u"activitySegment";
static constexpr char16_t ACTIVITY_TYPE[] = u"activityType";
static constexpr char16_t LOCATION[] = u"location";
static constexpr char16_t LOCATION_LATE7[] = u"latitudeE7";
static constexpr char16_t LOCATION_LONE7[] = u"longitudeE7";
static constexpr char16_t NAME[] = u"name";
static constexpr char16_t ADDRESS[] = u"address";
static constexpr char16_t DURATION[] = u"duration";
static constexpr char16_t START_TIMESTAMP[] = u"startTimestamp";
static constexpr char16_t START_LOCATION[] = u"startLocation";
static constexpr char16_t END_TIMESTAMP[] = u"endTimestamp";
static constexpr char16_t END_LOCATION[] = u"endLocation";
static constexpr char16_t TIMESTAMP[] = u"timestamp";
static constexpr char16_t SIMPLE_PATH[] = u"simplifiedRawPath";
static constexpr char16_t POINTS[] = u"points";
static constexpr char16_t WAYPOINT_PATH[] = u"waypointPath";
static constexpr char16_t WAYPOINTS [] = u"waypoints";
// for some reason that probably only a former Google engineer knows,;
// we use[] = u"latE7"/"lngE7" here instead of "latitudeE7"/"longitudeE7".;
// +10 points for brevity, but -100 points for inconsistency.;
static constexpr char16_t LATE7[] = u"latE7";
static constexpr char16_t LONE7[] = u"lngE7";

/* Types */

class GoogleTakeoutInputStream
{
public:
/* Special Member Functions */
GoogleTakeoutInputStream() = default;
GoogleTakeoutInputStream(const QString& source) : sources({source}) {}

/* Member Functions */

/* Member Functions */
// Returns the next timelineObject, or a null QJsonValue if we're at the end
QJsonValue next();

private:
/* Member Functions */

static QList<QJsonObject> readJson(const QString& source);
static QList<QString> readDir(const QString& source);
void loadSource(const QString& source);

/* Data Members */

QList<QString> sources;
QList<QJsonObject> timelineObjects;
};

/* Member Functions */

static void takeout_fatal(const QString& message);
static void takeout_warning(const QString& message);
Waypoint* takeout_waypoint(int lat_e7, int lon_e7, const QString* shortname, const QString* description, const QString* start_str);
static bool track_maybe_add_wpt(route_head* route, Waypoint* waypoint);
static void title_case(QString& title);
void add_place_visit(const QJsonObject& placeVisit);
int add_activity_segment(const QJsonObject& activitySegment);
static void title_case(QString& title);

/* Data Members */

Expand Down

0 comments on commit e8c5cc2

Please sign in to comment.