Skip to content

Commit cdb991e

Browse files
committed
store paste data text into rdb
1 parent 60ddffc commit cdb991e

File tree

7 files changed

+189
-8
lines changed

7 files changed

+189
-8
lines changed

entry/oh-package-lock.json5

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

entry/oh-package.json5

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"author": "",
77
"license": "",
88
"dependencies": {
9-
"libentry.so": "file:./src/main/cpp/types/libentry"
9+
"libentry.so": "file:./src/main/cpp/types/libentry",
10+
"@liushengyi/smartdb": "^4.0.1"
1011
}
1112
}

entry/src/main/ets/InputMethodExtensionAbility/model/pasteboard/PasteboardController.ets

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,89 @@
11
import { Context } from '@kit.AbilityKit'
22
import { BusinessError, pasteboard } from '@kit.BasicServicesKit'
3+
import sql from '@liushengyi/smartdb'
4+
import { PasteboardDao } from './PasteboardDao'
5+
import { PasteboardDatabase } from './PasteboardDatabase'
6+
import { PasteboardEntry } from './PasteboardEntry'
7+
import { HashSet } from '@kit.ArkTS'
8+
import { StrUtil } from '../../../utils/StrUtil'
39

410
const systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard()
511

12+
export interface OnPasteboardUpdateListener {
13+
onUpdate(entry: PasteboardEntry): void
14+
}
15+
616
export class PasteboardController {
17+
private psbDao: PasteboardDao | null = null
18+
719
constructor() {
820
}
9-
21+
22+
private onUpdateListeners = new HashSet<OnPasteboardUpdateListener>()
23+
24+
public addOnUpdateListener(listener: OnPasteboardUpdateListener): void {
25+
this.onUpdateListeners.add(listener)
26+
}
27+
28+
public removeOnUpdateListener(listener: OnPasteboardUpdateListener): void {
29+
this.onUpdateListeners.remove(listener)
30+
}
31+
32+
public lastEntry: PasteboardEntry | null = null
33+
34+
private updateLastEntry(entry: PasteboardEntry): void {
35+
this.lastEntry = entry
36+
this.onUpdateListeners.forEach((listener) => {
37+
listener?.onUpdate(entry)
38+
})
39+
}
40+
1041
public onCreate(context: Context): void {
1142
console.debug('onCreate')
43+
PasteboardDatabase.init(context)
44+
this.psbDao = new PasteboardDao()
1245
systemPasteboard.on('update', this.onSystemPasteboardUpdate)
1346
}
14-
47+
1548
public onDestroy(): void {
1649
console.debug('onDestroy')
1750
systemPasteboard.off('update', this.onSystemPasteboardUpdate)
1851
}
19-
52+
53+
@sql.Transactional()
54+
private async insertEntry(entry: PasteboardEntry): Promise<PasteboardEntry> {
55+
const rowId = await this.psbDao!.insert(entry)
56+
if (!rowId) {
57+
return entry
58+
}
59+
const inserted = await this.psbDao!.get(rowId)
60+
return inserted ? inserted : entry
61+
}
62+
2063
public onSystemPasteboardUpdate(): void {
21-
systemPasteboard.getData((err: BusinessError, data: pasteboard.PasteData) => {
64+
systemPasteboard.getData(async (err: BusinessError, data: pasteboard.PasteData) => {
2265
if (err) {
2366
console.error(`Failed to get paste data: ${err.message}`)
2467
return
2568
}
26-
const text = data.getPrimaryText()
27-
console.debug(`get primary text: ${text}`)
69+
const entry = PasteboardEntry.fromPasteData(data)
70+
if (!entry || StrUtil.isBlank(entry.text)) {
71+
return
72+
}
73+
try {
74+
const existed = await pasteboardController.psbDao!.find(entry.text, entry.sensitive)
75+
if (existed) {
76+
await pasteboardController.psbDao!.updateTime(existed.id, entry.timestamp)
77+
} else {
78+
const insertedEntry = await pasteboardController.insertEntry(entry)
79+
console.debug('get entry text: ' + insertedEntry.text)
80+
pasteboardController.updateLastEntry(insertedEntry)
81+
}
82+
} catch (e) {
83+
const error = e as Error
84+
console.warn(`Failed to update clipboard database: ${error.message}`)
85+
pasteboardController.updateLastEntry(entry)
86+
}
2887
})
2988
}
3089
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sql from '@liushengyi/smartdb';
2+
import { PasteboardEntry } from './PasteboardEntry';
3+
4+
export class PasteboardDao {
5+
constructor() {
6+
}
7+
8+
@sql.SqlInsert(`INSERT OR ABORT INTO ${PasteboardEntry.TABLE_NAME} (text, pinned, timestamp, type, deleted, sensitive)
9+
VALUES (#{entry.text}, #{entry.pinned}, #{entry.timestamp}, #{entry.type}, #{entry.deleted}, #{entry.sensitive})`,
10+
{ table: PasteboardEntry.TABLE_NAME, id: 'id' })
11+
insert(@sql.Param('entry') entry: PasteboardEntry): Promise<number> {
12+
return sql.PromiseNull()
13+
}
14+
15+
@sql.SqlUpdate(`UPDATE ${PasteboardEntry.TABLE_NAME} SET timestamp=#{timestamp} WHERE id=#{id}`)
16+
updateTime(@sql.Param('id') id: number, @sql.Param('timestamp') timestamp: number): Promise<void> {
17+
return sql.PromiseNull()
18+
}
19+
20+
@sql.SqlQuery(`SELECT * FROM ${PasteboardEntry.TABLE_NAME} WHERE rowId=#{rowId} AND deleted=0 LIMIT 1`)
21+
@sql.ReturnType(PasteboardEntry)
22+
get(@sql.Param('rowId') rowId: number): Promise<PasteboardEntry> {
23+
return sql.PromiseNull()
24+
}
25+
26+
@sql.SqlQuery(`SELECT * FROM ${PasteboardEntry.TABLE_NAME} WHERE text=#{text} AND sensitive=#{sensitive} AND deleted=0 LIMIT 1`)
27+
@sql.ReturnType(PasteboardEntry)
28+
find(@sql.Param('text') text: string,
29+
@sql.Param('sensitive') sensitive: boolean = false): Promise<PasteboardEntry> {
30+
return sql.PromiseNull()
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sql from '@liushengyi/smartdb';
2+
import relationalStore from '@ohos.data.relationalStore';
3+
4+
export class PasteboardDatabase {
5+
static VERSION = 1
6+
7+
static init(context: Context): void {
8+
sql.dbHelper.initDb(context, 'clbdb', PasteboardDatabase.VERSION, new PasteboardDbOpenHelper())
9+
}
10+
}
11+
12+
class PasteboardDbOpenHelper extends sql.DbOpenHelper {
13+
constructor() {
14+
super()
15+
}
16+
17+
override onCreate(db: relationalStore.RdbStore): void {
18+
db.executeSync("CREATE TABLE IF NOT EXISTS clipboard (" +
19+
"id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
20+
"text TEXT NOT NULL, " +
21+
"pinned INTEGER NOT NULL, timestamp INTEGER NOT NULL DEFAULT -1, " +
22+
"type TEXT NOT NULL DEFAULT 'text/plain', " +
23+
"deleted INTEGER NOT NULL DEFAULT 0, " +
24+
"sensitive INTEGER NOT NULL DEFAULT 0)")
25+
}
26+
27+
override onUpgrade(db: relationalStore.RdbStore, oldVersion: number, newVersion: number): void {
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { pasteboard } from '@kit.BasicServicesKit';
2+
import sql from '@liushengyi/smartdb';
3+
4+
export class PasteboardEntry {
5+
static TABLE_NAME = 'clipboard'
6+
@sql.SqlColumn(sql.ColumnType.INTEGER)
7+
id: number = 0
8+
@sql.SqlColumn(sql.ColumnType.TEXT)
9+
text: string
10+
@sql.SqlColumn(sql.ColumnType.INTEGER)
11+
pinned: boolean = false
12+
@sql.SqlColumn(sql.ColumnType.INTEGER)
13+
timestamp: number = Date.now()
14+
@sql.SqlColumn(sql.ColumnType.TEXT)
15+
type: string = pasteboard.MIMETYPE_TEXT_PLAIN
16+
@sql.SqlColumn(sql.ColumnType.INTEGER)
17+
deleted: boolean = false
18+
@sql.SqlColumn(sql.ColumnType.INTEGER)
19+
sensitive: boolean = false
20+
21+
constructor(text: string) {
22+
this.text = text
23+
}
24+
25+
static fromPasteData(pasteData: pasteboard.PasteData): PasteboardEntry | undefined {
26+
const prop = pasteData.getProperty()
27+
const record = pasteData.getRecord(0)
28+
const str = record.plainText
29+
if (!str) return undefined
30+
const newEntry = new PasteboardEntry(str)
31+
newEntry.timestamp = prop.timestamp
32+
newEntry.type = prop.mimeTypes[0]
33+
return newEntry
34+
}
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export namespace StrUtil {
2+
export function isBlank(str: string): boolean {
3+
return Array.from(str).every((ch) => ch.match(/\s/))
4+
}
5+
}

0 commit comments

Comments
 (0)