ktexteditor_wakatime v1.5.1
Kate plugin to interface with WakaTime.
Loading...
Searching...
No Matches
wakatime.cpp
1// SPDX-License-Identifier: MIT
2#include <QtCore/QDir>
3#include <QtCore/QProcess>
4
5#include "wakatime.h"
6
7Q_LOGGING_CATEGORY(gLogWakaTime, "wakatime")
8
9const auto kWakaTimeCli = QStringLiteral("wakatime-cli");
10
11WakaTime::WakaTime(QObject *parent) : lastTimeSent(QDateTime::fromMSecsSinceEpoch(0)) {
12 Q_UNUSED(parent);
13}
14
15QString WakaTime::getBinPath(const QStringList &binNames) {
16 for (auto &name : binNames) {
17 if (binPathCache.contains(name)) {
18 return binPathCache.value(name);
19 }
20 }
21 auto dotWakaTime = QStringLiteral("%1/.wakatime").arg(QDir::homePath());
22#ifndef Q_OS_WIN
23 static const auto pathSeparator = QStringLiteral(":");
24 static const auto kDefaultPath =
25 QStringLiteral("/usr/bin:/usr/local/bin:/opt/bin:/opt/local/bin");
26#else
27 static const auto pathSeparator = QStringLiteral(";");
28 static const auto kDefaultPath = QStringLiteral("C:\\Windows\\System32;C:\\Windows");
29#endif
30 const auto path = qEnvironmentVariable("PATH", kDefaultPath);
31 auto paths = path.split(pathSeparator, Qt::SkipEmptyParts);
32 paths.insert(0, dotWakaTime);
33 for (auto path : paths) {
34 for (auto &name : binNames) {
35 auto lookFor = path + QDir::separator() + name;
36 auto fi = QFileInfo(lookFor);
37 if (fi.exists(lookFor) && fi.isExecutable()) {
38 binPathCache[name] = lookFor;
39 return lookFor;
40 }
41 }
42 }
43 return QString();
44}
45
46QString WakaTime::getProjectDirectory(const QFileInfo &fileInfo) {
47 QDir currentDirectory(fileInfo.canonicalPath());
48 static const auto gitStr = QStringLiteral(".git");
49 static const auto svnStr = QStringLiteral(".svn");
50 do {
51 auto entries =
52 currentDirectory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Hidden);
53 for (const auto &entry : entries) {
54 auto name = entry.fileName();
55 if ((name == gitStr || name == svnStr) && entry.isDir()) {
56 return currentDirectory.dirName();
57 }
58 }
59 // isRoot() covers drive roots such as C:/ where a comparison with / never matches.
60 } while (!currentDirectory.isRoot() && currentDirectory.cdUp());
61 return QString();
62}
63
64WakaTime::State WakaTime::send(const QString &filePath,
65 const QString &mode,
66 int lineNumber,
67 int cursorPosition,
68 int linesInFile,
69 bool isWrite) {
70#ifdef Q_OS_WIN
71#ifdef Q_PROCESSOR_X86_64
72 auto wakatimeCliPath = getBinPath({QStringLiteral("wakatime-cli-windows-amd64.exe"),
73 QStringLiteral("wakatime-cli.exe"),
74 QStringLiteral("wakatime.exe")});
75#elif defined(Q_PROCESSOR_ARM)
76 auto wakatimeCliPath = getBinPath({QStringLiteral("wakatime-cli-windows-arm64.exe"),
77 QStringLiteral("wakatime-cli.exe"),
78 QStringLiteral("wakatime.exe")});
79#else
80 auto wakatimeCliPath = getBinPath({QStringLiteral("wakatime-cli-windows-386.exe"),
81 QStringLiteral("wakatime-cli.exe"),
82 QStringLiteral("wakatime.exe")});
83#endif // Q_PROCESSOR_X86_64
84#elif defined(Q_OS_APPLE)
85 auto wakatimeCliPath = getBinPath({QStringLiteral("wakatime-cli-darwin-arm64"),
86 QStringLiteral("wakatime-cli-darwin-amd64"),
87 kWakaTimeCli,
88 QStringLiteral("wakatime")});
89#else
90 auto wakatimeCliPath = getBinPath({kWakaTimeCli, QStringLiteral("wakatime")});
91#endif // Q_OS_WIN
92 if (wakatimeCliPath.isEmpty()) {
93 qCWarning(gLogWakaTime) << "wakatime-cli not found in PATH.";
95 }
96 // Could be untitled, or a URI (including HTTP). Only local files are handled for now.
97 if (filePath.isEmpty()) {
98 qCDebug(gLogWakaTime) << "Nothing to send about";
99 return NothingToSend;
100 }
101 QStringList arguments;
102 const QFileInfo fileInfo(filePath);
103 // They have it sending the real file path, maybe not respecting symlinks, etc.
104 auto canonicalFilePath = fileInfo.canonicalFilePath();
105 qCDebug(gLogWakaTime) << "File path:" << canonicalFilePath;
106 // Compare date and make sure it has been at least 15 minutes.
107 const auto currentMs = QDateTime::currentMSecsSinceEpoch();
108 const auto deltaMs = currentMs - lastTimeSent.toMSecsSinceEpoch();
109 static const auto intervalMs = 120000; // ms
110 // If the current file has not changed and it has not been 2 minutes since the last heartbeat
111 // was sent, do NOT send this heartbeat. This does not apply to write events as they are always
112 // sent.
113 if (!isWrite) {
114 if (hasSent && deltaMs <= intervalMs && lastFileSent == canonicalFilePath) {
115 qCDebug(gLogWakaTime) << "Not enough time has passed since last send";
116 qCDebug(gLogWakaTime) << "Delta:" << deltaMs / 1000 / 60 << "/ 2 minutes";
117 return TooSoon;
118 }
119 }
120 arguments << QStringLiteral("--entity") << canonicalFilePath;
121 arguments << QStringLiteral("--plugin")
122 << QStringLiteral("ktexteditor-wakatime/%1").arg(VERSION);
123 auto projectName = getProjectDirectory(fileInfo);
124 if (!projectName.isEmpty()) {
125 arguments << QStringLiteral("--alternate-project") << projectName;
126 } else {
127 // LCOV_EXCL_START
128 qCDebug(gLogWakaTime) << "Warning: No project name found";
129 // LCOV_EXCL_STOP
130 }
131 if (isWrite) {
132 arguments << QStringLiteral("--write");
133 }
134 if (!mode.isEmpty()) {
135 arguments << QStringLiteral("--language") << mode;
136 }
137 arguments << QStringLiteral("--lineno") << QString::number(lineNumber);
138 arguments << QStringLiteral("--cursorpos") << QString::number(cursorPosition);
139 arguments << QStringLiteral("--lines-in-file") << QString::number(linesInFile);
140 qCDebug(gLogWakaTime) << "Running:" << wakatimeCliPath << arguments.join(QStringLiteral(" "));
141 // wakatime-cli performs network requests. Waiting for it on the GUI thread freezes Kate.
142 if (!QProcess::startDetached(wakatimeCliPath, arguments)) {
143 qCWarning(gLogWakaTime) << "Failed to start" << wakatimeCliPath;
144 binPathCache.clear();
145 return ErrorSending;
146 }
147 lastTimeSent = QDateTime::currentDateTime();
148 lastFileSent = canonicalFilePath;
149 hasSent = true;
150 return SentSuccessfully;
151}
WakaTime(QObject *parent=nullptr)
Definition wakatime.cpp:11
QString getProjectDirectory(const QFileInfo &fileInfo)
Definition wakatime.cpp:46
@ SentSuccessfully
Definition wakatime.h:25
@ ErrorSending
Definition wakatime.h:23
@ TooSoon
Definition wakatime.h:26
@ NothingToSend
Definition wakatime.h:24
@ WakaTimeCliNotInPath
Definition wakatime.h:27
QString getBinPath(const QStringList &binNames)
Definition wakatime.cpp:15
WakaTime::State send(const QString &filePath, const QString &mode, int lineNumber, int cursorPosition, int linesInFile, bool isWrite)
Definition wakatime.cpp:64