|
| 1 | +import GLib from 'gi://GLib'; |
| 2 | +import GObject from 'gi://GObject'; |
| 3 | + |
| 4 | +import { ClipboardHistory, ItemType, Tag } from '../common/constants.js'; |
| 5 | +import { int32ParamSpec, registerClass } from '../common/gjs.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Metadata. |
| 9 | + */ |
| 10 | +export type Metadata = CodeMetadata | FileMetadata | LinkMetadata; |
| 11 | + |
| 12 | +/** |
| 13 | + * Programming language. |
| 14 | + */ |
| 15 | +export interface Language { |
| 16 | + id: string; |
| 17 | + name: string; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Code metadata. |
| 22 | + */ |
| 23 | +export interface CodeMetadata { |
| 24 | + language: Language | null; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * File operation. |
| 29 | + */ |
| 30 | +export const FileOperation = { |
| 31 | + Copy: 'copy', |
| 32 | + Cut: 'cut', |
| 33 | +} as const; |
| 34 | + |
| 35 | +export type FileOperation = (typeof FileOperation)[keyof typeof FileOperation]; |
| 36 | + |
| 37 | +/** |
| 38 | + * File metadata. |
| 39 | + */ |
| 40 | +export interface FileMetadata { |
| 41 | + operation: FileOperation; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Link metadata. |
| 46 | + */ |
| 47 | +export interface LinkMetadata { |
| 48 | + title: string | null; |
| 49 | + description: string | null; |
| 50 | + image: string | null; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Clipboard entry. |
| 55 | + */ |
| 56 | +@registerClass({ |
| 57 | + Properties: { |
| 58 | + id: int32ParamSpec('id', GObject.ParamFlags.READABLE, 0), |
| 59 | + type: GObject.ParamSpec.string('type', null, null, GObject.ParamFlags.READWRITE, ItemType.Text), |
| 60 | + content: GObject.ParamSpec.string('content', null, null, GObject.ParamFlags.READWRITE, ''), |
| 61 | + pinned: GObject.ParamSpec.boolean('pinned', null, null, GObject.ParamFlags.READWRITE, false), |
| 62 | + tag: GObject.ParamSpec.string('tag', null, null, GObject.ParamFlags.READWRITE, ''), |
| 63 | + datetime: GObject.ParamSpec.boxed('datetime', null, null, GObject.ParamFlags.READWRITE, GLib.DateTime), |
| 64 | + metadata: GObject.ParamSpec.jsobject('metadata', null, null, GObject.ParamFlags.READWRITE), |
| 65 | + title: GObject.ParamSpec.string('title', null, null, GObject.ParamFlags.READWRITE, ''), |
| 66 | + }, |
| 67 | + Signals: { |
| 68 | + delete: {}, |
| 69 | + }, |
| 70 | +}) |
| 71 | +export class ClipboardEntry extends GObject.Object { |
| 72 | + private readonly _id: number; |
| 73 | + declare type: ItemType; |
| 74 | + declare content: string; |
| 75 | + declare pinned: boolean; |
| 76 | + declare tag: Tag | null; |
| 77 | + declare datetime: GLib.DateTime; |
| 78 | + declare metadata: Metadata | null; |
| 79 | + declare title: string; |
| 80 | + |
| 81 | + constructor( |
| 82 | + id: number, |
| 83 | + type: ItemType, |
| 84 | + content: string, |
| 85 | + pinned: boolean, |
| 86 | + tag: Tag | null, |
| 87 | + datetime: GLib.DateTime, |
| 88 | + metadata: Metadata | null = null, |
| 89 | + title: string = '', |
| 90 | + ) { |
| 91 | + super(); |
| 92 | + |
| 93 | + this._id = id; |
| 94 | + this.type = type; |
| 95 | + this.content = content; |
| 96 | + this.pinned = pinned; |
| 97 | + this.tag = tag; |
| 98 | + this.datetime = datetime; |
| 99 | + this.metadata = metadata; |
| 100 | + this.title = title; |
| 101 | + } |
| 102 | + |
| 103 | + get id() { |
| 104 | + return this._id; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Clipboard database |
| 110 | + */ |
| 111 | +export interface Database { |
| 112 | + /** |
| 113 | + * Initializes the database |
| 114 | + */ |
| 115 | + init(): Promise<void>; |
| 116 | + |
| 117 | + /** |
| 118 | + * Clears the database. |
| 119 | + * @param history Which items to keep. |
| 120 | + */ |
| 121 | + clear(history: ClipboardHistory): Promise<number[]>; |
| 122 | + |
| 123 | + /** |
| 124 | + * Close the connection to the database. |
| 125 | + */ |
| 126 | + close(): Promise<void>; |
| 127 | + |
| 128 | + /** |
| 129 | + * Gets the entries of the database. |
| 130 | + */ |
| 131 | + entries(): Promise<ClipboardEntry[]>; |
| 132 | + |
| 133 | + /** |
| 134 | + * Select a conflicting entry by its type and content. |
| 135 | + * @param entry The entry to search its conflict for. |
| 136 | + * @returns The id of the conflicting entry or null if no conflict was found. |
| 137 | + */ |
| 138 | + selectConflict(entry: ClipboardEntry | { type: ItemType; content: string }): Promise<number | null>; |
| 139 | + |
| 140 | + /** |
| 141 | + * Inserts an entry into the database. |
| 142 | + * @param type The type of the entry. |
| 143 | + * @param content The content of the entry. |
| 144 | + * @param metadata Metadata of the entry. |
| 145 | + * @returns The inserted entry or null if the insertion failed. |
| 146 | + */ |
| 147 | + insert(type: ItemType, content: string, metadata: Metadata | null): Promise<ClipboardEntry | null>; |
| 148 | + |
| 149 | + /** |
| 150 | + * Updates a property of an inserted database entry. |
| 151 | + * @param entry The entry to update the property of. |
| 152 | + * @param property The property of the entry to update. |
| 153 | + * @returns -1 if the property was updated, the id of a conflicting entry otherwise. |
| 154 | + */ |
| 155 | + updateProperty<K extends Exclude<keyof ClipboardEntry, keyof GObject.Object | 'id'>>( |
| 156 | + entry: ClipboardEntry, |
| 157 | + property: K, |
| 158 | + ): Promise<number>; |
| 159 | + |
| 160 | + /** |
| 161 | + * Delete an entry from the database. |
| 162 | + * @param entry The entry to delete. |
| 163 | + * @returns true if the entry was deleted, false otherwise. |
| 164 | + */ |
| 165 | + delete(entry: ClipboardEntry): Promise<boolean>; |
| 166 | + |
| 167 | + /** |
| 168 | + * Delete the oldest entries of the database. |
| 169 | + * @param offset The number of entries to keep. |
| 170 | + * @param olderThanMinutes Items older than this value will be deleted. |
| 171 | + * @returns The ids of entries that were deleted. |
| 172 | + */ |
| 173 | + deleteOldest(offset: number, olderThanMinutes: number): Promise<number[]>; |
| 174 | +} |
0 commit comments