Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
feat: expose sync operations for cli usage
Browse files Browse the repository at this point in the history
  • Loading branch information
oae committed Oct 26, 2021
1 parent 9d263b6 commit 752c544
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
6 changes: 6 additions & 0 deletions resources/dbus/io.elhan.ExtensionsSync.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<node>
<interface name="io.elhan.ExtensionsSync">
<method name="save" />
<method name="read" />
</interface>
</node>
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default [
{ src: './resources/icons', dest: `${buildPath}` },
{ src: './resources/metadata.json', dest: `${buildPath}` },
{ src: './resources/schemas', dest: `${buildPath}` },
{ src: './resources/dbus', dest: `${buildPath}` },
],
}),
cleanup({
Expand Down
18 changes: 17 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { StatusMenu } from '@esync/panel/statusMenu';
import { Sync } from '@esync/sync';
import { logger } from '@esync/utils';
import { EventEmitter } from 'events';
import { loadInterfaceXML } from '@esync/shell';
import './styles/stylesheet.css';
import { DBus, DBusExportedObject } from '@imports/gio2';
import { SyncEvent } from './api/types';

const debug = logger('extension');

Expand All @@ -14,25 +17,38 @@ class SyncExtension {
private api: Api;
private eventEmitter: EventEmitter;
private data: Data;

private dbus: DBusExportedObject;
constructor() {
this.eventEmitter = new EventEmitter();
this.data = new Data();
this.api = new Api(this.eventEmitter, this.data);
this.sync = new Sync(this.eventEmitter, this.data);
this.statusMenu = new StatusMenu(this.eventEmitter);
const iface = loadInterfaceXML('io.elhan.ExtensionsSync');
this.dbus = DBusExportedObject.wrapJSObject(iface, this);

debug('extension is initialized');
}

save(): void {
this.eventEmitter.emit(SyncEvent.SAVE);
}

read(): void {
this.eventEmitter.emit(SyncEvent.READ);
}

enable(): void {
this.sync.start();
this.statusMenu.show();
this.dbus.export(DBus.session, '/io/elhan/ExtensionsSync');
debug('extension is enabled');
}

disable(): void {
this.sync.stop();
this.statusMenu.hide();
this.dbus.unexport();
debug('extension is disabled');
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ declare const imports: {
};
declare const log: (arg: any) => void;
declare const _: (arg: string) => string;
declare class TextDecoder {
decode: (content: any) => any;
}
6 changes: 2 additions & 4 deletions src/panel/statusMenu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SyncEvent } from '@esync/api/types';
import { getCurrentExtension, getCurrentExtensionSettings, ShellExtension } from '@esync/shell';
import { getCurrentExtension, ShellExtension } from '@esync/shell';
import { execute, logger } from '@esync/utils';
import { icon_new_for_string, Settings } from '@imports/gio2';
import { icon_new_for_string } from '@imports/gio2';
import { Icon } from '@imports/st1';
import { EventEmitter } from 'events';

Expand All @@ -15,12 +15,10 @@ export class StatusMenu {
private eventEmitter: EventEmitter;
private button: any;
private extension: ShellExtension;
private settings: Settings;

constructor(eventEmitter: EventEmitter) {
this.eventEmitter = eventEmitter;
this.extension = getCurrentExtension();
this.settings = getCurrentExtensionSettings();
}

show(): void {
Expand Down
14 changes: 14 additions & 0 deletions src/shell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ export const writeDconfData = async (schemaPath: string, data: string): Promise<
export const readDconfData = async (schemaPath: string): Promise<string> => {
return execute(`dconf dump ${schemaPath}`);
};

export const loadInterfaceXML = (iface: string): any => {
const uri = `file:https:///${getCurrentExtension().path}/dbus/${iface}.xml`;
const file = File.new_for_uri(uri);

try {
const [, bytes] = file.load_contents(null);
return new TextDecoder().decode(bytes);
} catch (e) {
log(`Failed to load D-Bus interface ${iface}`);
}

return null;
};

0 comments on commit 752c544

Please sign in to comment.