Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list open windows for tracking #15

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ interface Window {
value: string,
internal?: boolean,
) => string;
shell: {
openExternal: (url: string) => void;
};
isMonitored: (path: string) => boolean;
setMonitored: (path: string, monitor: boolean) => void;
getAppVersion: () => string;
Expand All @@ -47,6 +50,7 @@ interface Window {
setAutoUpdateEnabled: (autoUpdateEnabled: boolean) => void;
codeTimeInStatusBar: () => boolean;
setCodeTimeInStatusBar: (codeTimeInStatusBar: boolean) => void;
getOpenWindows: () => Promise<import("./utils/validators").AppData[]>;
};
}

Expand Down
4 changes: 3 additions & 1 deletion electron/helpers/apps-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export class AppsManager {
const data = fs.readFileSync(this.cacheFilePath, { encoding: "utf-8" });
const apps = z.array(appDataSchema).parse(JSON.parse(data));
return apps;
} catch (error) {}
} catch (_error) {
/* empty */
}
return [];
}

Expand Down
6 changes: 3 additions & 3 deletions electron/helpers/config-file-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export abstract class ConfigFileReader {

try {
contents = fs.readFileSync(file, { encoding: "utf-8" });
} catch (error) {
} catch (_error) {
contents = "[" + section + "]\n" + key + " = " + value;
try {
fs.writeFileSync(file, contents, { encoding: "utf-8" });
Expand All @@ -43,7 +43,7 @@ export abstract class ConfigFileReader {
}
}
const lines = contents ? contents.split("\n") : [];
let output: string[] = [];
const output: string[] = [];
let currentSection = "";
let found = false;
for (const line of lines) {
Expand All @@ -55,7 +55,7 @@ export abstract class ConfigFileReader {
output.push(line);
currentSection = line.slice(1, line.length - 1);
} else if (currentSection === section) {
let parts = line.split("=", 2);
const parts = line.split("=", 2);
if (parts.length === 2 && parts[0].trim() === key) {
if (!found) {
output.push(key + " = " + value);
Expand Down
7 changes: 4 additions & 3 deletions electron/helpers/filter-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FilterType } from "../utils/constants";
import { PropertiesManager } from "./properties-manager";

export abstract class FilterManager {
Expand All @@ -14,7 +13,7 @@ export abstract class FilterManager {
const httpsUrl = "https://" + url;

switch (PropertiesManager.filterType) {
case FilterType.denylist:
case "denylist": {
for (const pattern of patterns) {
if (
url.match(pattern) ||
Expand All @@ -26,7 +25,8 @@ export abstract class FilterManager {
}
}
break;
case FilterType.allowlist:
}
case "allowlist": {
const matchedPatternIndex = patterns.findIndex(
(pattern) =>
url.match(pattern) ||
Expand All @@ -37,6 +37,7 @@ export abstract class FilterManager {
return false;
}
break;
}
}

// The given address passed all filters and will be included
Expand Down
19 changes: 12 additions & 7 deletions electron/helpers/installed-apps/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from "node:path";

import { AppData } from "../../utils/validators";
import { allApps } from "../../watchers/apps";
import { getAppIconMac, getInstalledApps as getInstalledAppsMac } from "./mac";
Expand Down Expand Up @@ -39,7 +41,7 @@ export async function getApps(): Promise<AppData[]> {
let filePath: string | null = null;
try {
filePath = getFilePathWindows(record, app.windows.exePath);
} catch (error) {
} catch (_error) {
/* empty */
}
if (!filePath) {
Expand All @@ -49,7 +51,7 @@ export async function getApps(): Promise<AppData[]> {
let icon: string | null = null;
try {
icon = await getIconFromWindows(filePath);
} catch (error) {
} catch (_error) {
/* empty */
}

Expand All @@ -69,6 +71,7 @@ export async function getApps(): Promise<AppData[]> {
isBrowser: app.isBrowser ?? false,
isDefaultEnabled: app.isDefaultEnabled ?? false,
isElectronApp: app.isElectronApp ?? false,
execName: path.parse(filePath).base,
} satisfies AppData;
}

Expand All @@ -83,16 +86,17 @@ export async function getApps(): Promise<AppData[]> {
if (!record) {
return null;
}
const name = record["kMDItemDisplayName"]?.replace(".app", "");
const path = record["_FILE_PATH"];
if (!path || !name) {
const execName = record["kMDItemDisplayName"];
const name = execName?.replace(".app", "");
const filePath = record["_FILE_PATH"];
if (!filePath || !name) {
return;
}
const icon = await getAppIconMac(path);
const icon = await getAppIconMac(filePath);
const version = record["kMDItemVersion"] || null;

return {
path,
path: filePath,
icon,
name,
bundleId: app.mac.bundleId,
Expand All @@ -101,6 +105,7 @@ export async function getApps(): Promise<AppData[]> {
isDefaultEnabled: app.isDefaultEnabled ?? false,
isElectronApp: app.isElectronApp ?? false,
version,
execName: path.parse(filePath).base,
} satisfies AppData;
}

Expand Down
4 changes: 2 additions & 2 deletions electron/helpers/installed-apps/mac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ export async function getAppIconMac(appPath: string) {
}
icnsPath = path.join(appPath, "Contents/Resources", iconFileName);
}
} catch (error) {
} catch (_error) {
/* empty */
}

if (!icnsPath) {
try {
icnsPath = await getFirstIcnsFileFromResourcesDir();
} catch (error) {
} catch (_error) {
/* empty */
}
}
Expand Down
6 changes: 3 additions & 3 deletions electron/helpers/installed-apps/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "node:fs";
import path from "node:path";
import Winreg from "winreg";

import { store } from "../../store";
import { Store } from "../../store";

export function getFilePathWindows(
appData: Record<string, string>,
Expand Down Expand Up @@ -49,15 +49,15 @@ export async function getIconFromWindows(filePath: string) {
return null;
}

const cachedIcon = store.get(`${filePath}-icon`);
const cachedIcon = Store.instance().get(`${filePath}-icon`);
if (typeof cachedIcon === "string") {
return cachedIcon;
}
const { extractIcon } = await import("exe-icon-extractor");

const buffer = extractIcon(filePath, "large");
const icon = "data:image/png;base64," + buffer.toString("base64");
store.set(`${filePath}-icon`, icon);
Store.instance().set(`${filePath}-icon`, icon);
return icon;
}

Expand Down
48 changes: 29 additions & 19 deletions electron/helpers/monitored-app.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { WindowInfo } from "@miniben90/x-win";

import { DomainPreferenceType } from "../utils/constants";
import { Category } from "../utils/types";
import { AppData } from "../utils/validators";
import { FilterManager } from "./filter-manager";
import { PropertiesManager } from "./properties-manager";

export interface HeartbeatData {
entity: string;
category: Category | null;
language: string | null;
project: string | null;
}

export class MonitoredApp {
static heartbeatData(windowInfo: WindowInfo, app: AppData) {
static heartbeatData(
windowInfo: WindowInfo,
app?: AppData,
): HeartbeatData | null {
const entity = this.entity(windowInfo, app);
const category = this.category(app);
const language = this.language(app);
const category = app ? this.category(app) : null;
const language = app ? this.language(app) : null;
const project = this.project(windowInfo);
if (!entity) {
return null;
Expand Down Expand Up @@ -79,11 +88,11 @@ export class MonitoredApp {
return null;
}
const patterns = [
/github\.com\/([^\/]+\/[^\/]+)\/?.*$/,
/bitbucket\.org\/([^\/]+\/[^\/]+)\/?.*$/,
/app\.circleci\.com\/.*\/?(github|bitbucket|gitlab)\/([^\/]+\/[^\/]+)\/?.*$/,
/app\.travis-ci\.com\/(github|bitbucket|gitlab)\/([^\/]+\/[^\/]+)\/?.*$/,
/app\.travis-ci\.org\/(github|bitbucket|gitlab)\/([^\/]+\/[^\/]+)\/?.*$/,
/github\.com\/([^/]+\/[^/]+)\/?.*$/,
/bitbucket\.org\/([^/]+\/[^/]+)\/?.*$/,
/app\.circleci\.com\/.*\/?(github|bitbucket|gitlab)\/([^/]+\/[^/]+)\/?.*$/,
/app\.travis-ci\.com\/(github|bitbucket|gitlab)\/([^/]+\/[^/]+)\/?.*$/,
/app\.travis-ci\.org\/(github|bitbucket|gitlab)\/([^/]+\/[^/]+)\/?.*$/,
];

for (const pattern of patterns) {
Expand All @@ -102,20 +111,18 @@ export class MonitoredApp {
return null;
}

static entity(windowInfo: WindowInfo, app: AppData) {
if (app.isBrowser) {
static entity(windowInfo: WindowInfo, app?: AppData) {
if (app?.isBrowser) {
if (windowInfo.url && FilterManager.filterBrowsedSites(windowInfo.url)) {
if (
PropertiesManager.domainPreference === DomainPreferenceType.domain
) {
if (PropertiesManager.domainPreference === "domain") {
return this.domainFromUrl(windowInfo.url);
}
return windowInfo.url;
}
return null;
}

switch (app.id) {
switch (app?.id) {
// TODO: Unimplemented feature
case "canva":
return null;
Expand All @@ -127,8 +134,8 @@ export class MonitoredApp {
}
}

static title(windowInfo: WindowInfo, app: AppData) {
switch (app.id) {
static title(windowInfo: WindowInfo, app?: AppData) {
switch (app?.id) {
case "arcbrowser":
case "brave":
case "canva":
Expand All @@ -139,7 +146,7 @@ export class MonitoredApp {
case "safaripreview":
case "xcode":
case "microsoft_edge":
return `${app.id} should never use window title as entity`;
return `${app?.id} should never use window title as entity`;
case "figma": {
const title = windowInfo.title.split(" - ")[0];
if (!title || title === "Figma" || title === "Drafts") {
Expand All @@ -162,7 +169,10 @@ export class MonitoredApp {
return title;
}
default:
return windowInfo.title.split(" - ")[0];
if (windowInfo.title) {
return windowInfo.title.split(" - ")[0];
}
return windowInfo.info.name;
}
}

Expand Down
6 changes: 0 additions & 6 deletions electron/helpers/monitoring-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ export abstract class MonitoringManager {
}

static isMonitored(path: string) {
if (!AppsManager.instance().getApp(path)) {
return;
}
const monitoringKey = this.monitoredKey(path);
const file = getDesktopWakaTimeConfigFilePath();
const monitoring = ConfigFileReader.getBool(
Expand All @@ -29,9 +26,6 @@ export abstract class MonitoringManager {
}

static set(path: string, monitor: boolean) {
if (!AppsManager.instance().getApp(path)) {
return;
}
const file = getDesktopWakaTimeConfigFilePath();
const monitoringKey = this.monitoredKey(path);
ConfigFileReader.setBool(file, "monitoring", monitoringKey, monitor);
Expand Down
Loading
Loading