Repository navigation
Expand file tree
/
Copy pathContactReview.qml
More file actions
322 lines (316 loc) · 13.2 KB
/
Copy pathContactReview.qml
File metadata and controls
322 lines (316 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import QtQuick
import QtQuick.Controls as QQC
import QtQuick.Layouts
import Quickshell
import Quickshell.Io
import qs.Commons
import qs.Ui
// A conversation tool. The helper supplies validated display rows; this view
// holds only session state and never changes preferences or contact cards.
FocusScope {
id: root
property bool opened: false
property var threads: []
property color foreground: Color.foreground
property color accent: "#0a84ff"
property string fontFamily: Style.font.family
property int fontSize: Style.font.bodySmall
property var model: null
property var overview: null
property string error: ""
property string notice: ""
property bool busy: false
property var scanConversations: []
property var selectedCard: null
property string savingHandle: ""
visible: opened
readonly property string helper: decodeURIComponent(Qt.resolvedUrl("contact-review.ts").toString().replace(/^file:\/\//, ""))
signal closed()
signal copyRequested(string text)
signal contactSaved()
function close() {
if (savingHandle !== "" && saveLoader.item && saveLoader.item.busy) return
selectedCard = null; savingHandle = ""; opened = false; closed()
}
function textField(value, maximum) { return typeof value === "string" ? value.slice(0, maximum) : "" }
function metadata(thread) {
return { chat: textField(thread.chat, 320), name: textField(thread.name, 160) }
}
function review(thread) {
if (busy) return
var data = metadata(thread)
var people = Array.isArray(thread.participants) ? thread.participants.slice(0, 64) : []
data.participants = people.map(function(person) {
return typeof person === "string" ? textField(person, 320)
: { handle: textField(person.handle, 320), name: textField(person.name, 160) }
})
model = null; overview = null; opened = true
request("review", { conversation: data })
forceActiveFocus()
}
function scan(page) {
if (busy) return
if (threads.length > 1000) { error = "Too many conversations to scan at once"; return }
overview = null
if (page === undefined) {
page = 0
scanConversations = threads.map(function(thread) {
return {chat: textField(thread.chat, 320), participants: (thread.participants || []).slice(0, 64).map(function(p) {
return {handle: textField(typeof p === "string" ? p : p.handle, 320)}
})}
})
}
request("audit", {conversations: scanConversations, page: page})
}
function closeSave() {
var handle = savingHandle
savingHandle = ""
request("candidates", {handle: handle})
}
function back() {
if (savingHandle !== "") {
if (!saveLoader.item || !saveLoader.item.busy) closeSave()
return
}
if (selectedCard) { selectedCard = null; return }
if (busy) return
if (overview) { model = overview; overview = null; error = ""; notice = "" }
else close()
}
function request(operation, payload) {
if (busy) return
var encoded = JSON.stringify(payload)
error = ""; notice = ""
if (encoded.length > (operation === "audit" ? 4194304 : 49152)) { error = "Too many contacts in this request"; return }
worker.received = false
worker.command = operation === "vcard"
? ["bun", helper.replace(/contact-review\.ts$/, "contact-vcard.ts")]
: ["bun", helper, operation]
busy = true
worker.stdinEnabled = true
worker.running = true
worker.write(encoded)
worker.stdinEnabled = false
}
function validText(value, maximum) {
return typeof value === "string" && value.length <= maximum
&& !/[\x00-\x1f\x7f-\x9f\u202a-\u202e\u2066-\u2069]/.test(value)
}
function consume(text) {
worker.received = true
if (!opened) return
if (text.length > 49152) { error = "Contact response is too large"; return }
try {
var result = JSON.parse(text)
if (!result || result.ok !== true) {
error = result && validText(result.error, 180) ? result.error : "Contact review failed"
return
}
if (["people", "cards", "scan", "opened", "copied", "saved", "cancelled"].indexOf(result.view) < 0
|| !validText(result.title, 160) || !validText(result.detail, 480)
|| !Array.isArray(result.rows) || result.rows.length > 200) throw "schema"
if (result.view === "scan" && (!Number.isInteger(result.page) || !Number.isInteger(result.pageCount)
|| result.page < 0 || result.pageCount < 1 || result.pageCount > 250 || result.page >= result.pageCount)) throw "page"
for (var i = 0; i < result.rows.length; i++) {
var row = result.rows[i]
if (!row || !validText(row.name, 320) || !validText(row.detail, 480)
|| !validText(row.handle, 320) || ["candidates", "open"].indexOf(row.action) < 0
|| typeof row.token !== "string" || (row.action === "open"
? !/^sha256:[0-9a-f]{64}$/.test(row.token) : row.token !== "")) throw "row"
}
if (["opened", "copied", "saved", "cancelled"].indexOf(result.view) >= 0) notice = result.detail
else { model = result; reviewFlick.contentY = 0 }
} catch (e) { error = "Contact review returned an invalid response" }
}
Keys.onEscapePressed: back()
Process {
id: worker
property bool received: false
stdout: StdioCollector { onStreamFinished: root.consume(text.trim()) }
onRunningChanged: if (!running) Qt.callLater(function() {
root.busy = false
if (!worker.received) root.error = "Could not start contact review"
})
}
Loader {
id: saveLoader
anchors.fill: parent
active: root.savingHandle !== ""
sourceComponent: ContactSave {
handle: root.savingHandle
foreground: root.foreground; accent: root.accent; fontFamily: root.fontFamily; fontSize: root.fontSize
onClosed: root.closeSave()
onSaved: root.contactSaved()
}
}
Loader {
anchors.fill: parent
active: root.selectedCard !== null
sourceComponent: ContactDetails {
card: root.selectedCard
foreground: root.foreground; accent: root.accent; fontFamily: root.fontFamily; fontSize: root.fontSize
onClosed: root.selectedCard = null
onCopyRequested: function(text) { root.copyRequested(text) }
onOpenOnMac: {
var card = root.selectedCard
root.selectedCard = null
root.request("open", {handle: card.handle, token: card.token})
}
}
}
ColumnLayout {
visible: root.selectedCard === null && root.savingHandle === ""
anchors.fill: parent
spacing: Style.space(10)
RowLayout {
Layout.fillWidth: true
PanelActionButton {
iconText: "←"; tooltipText: "Back"; enabled: !root.busy; focusable: true
foreground: root.foreground; hoverColor: root.accent
onClicked: root.back()
}
Text {
Layout.fillWidth: true
text: root.model ? root.model.title : "Contact review"
textFormat: Text.PlainText; color: root.foreground
font.family: root.fontFamily; font.pixelSize: root.fontSize + 2; font.bold: true
elide: Text.ElideRight
}
}
Text {
Layout.fillWidth: true
text: "Matching cards from Mac Contacts."
textFormat: Text.PlainText; wrapMode: Text.WordWrap
color: Qt.darker(root.foreground, 1.4)
font.family: root.fontFamily; font.pixelSize: root.fontSize
}
Text {
Layout.fillWidth: true
text: root.busy ? "Checking Mac Contacts…" : root.error || root.notice || (root.model ? root.model.detail : "")
textFormat: Text.PlainText; wrapMode: Text.WordWrap
color: root.error !== "" ? Color.urgent : root.foreground
font.family: root.fontFamily; font.pixelSize: root.fontSize
}
PanelSeparator { Layout.fillWidth: true; foreground: root.foreground }
Flickable {
id: reviewFlick
Layout.fillWidth: true; Layout.fillHeight: true
contentWidth: width; contentHeight: rows.implicitHeight
clip: true; boundsBehavior: Flickable.StopAtBounds
QQC.ScrollBar.vertical: QQC.ScrollBar { }
ColumnLayout {
id: rows
width: parent.width
spacing: Style.space(12)
Text {
Layout.fillWidth: true
visible: !root.busy && root.error === "" && root.model !== null && root.model.rows.length === 0
text: root.model && root.model.view === "cards" ? "No matching contact cards found."
: root.model && root.model.view === "scan" ? "No duplicate candidates or name conflicts found." : ""
textFormat: Text.PlainText; wrapMode: Text.WordWrap
color: root.foreground; font.family: root.fontFamily; font.pixelSize: root.fontSize
}
Repeater {
model: root.model ? root.model.rows : []
delegate: Rectangle {
required property var modelData
Layout.fillWidth: true
implicitHeight: card.implicitHeight + Style.space(20)
radius: Style.cornerRadius
color: Qt.rgba(root.foreground.r, root.foreground.g, root.foreground.b, 0.06)
ColumnLayout {
id: card
anchors.left: parent.left; anchors.right: parent.right; anchors.top: parent.top
anchors.margins: Style.space(10)
spacing: Style.space(4)
Text {
Layout.fillWidth: true; text: modelData.name; textFormat: Text.PlainText
wrapMode: Text.WordWrap; color: root.foreground
font.family: root.fontFamily; font.pixelSize: root.fontSize; font.bold: true
}
Text {
Layout.fillWidth: true; text: modelData.detail; textFormat: Text.PlainText
wrapMode: Text.WordWrap; color: Qt.darker(root.foreground, 1.3)
font.family: root.fontFamily; font.pixelSize: root.fontSize
}
RowLayout {
Layout.fillWidth: true
visible: modelData.action === "open"
spacing: Style.space(6)
ContactButton {
Layout.fillWidth: true
text: "Copy vCard"
enabled: !root.busy
foreground: root.foreground; accent: root.accent; fontFamily: root.fontFamily; fontSize: root.fontSize
onClicked: root.request("vcard", {handle: modelData.handle, token: modelData.token})
}
ContactButton {
Layout.fillWidth: true
text: "Save vCard…"
enabled: !root.busy
foreground: root.foreground; accent: root.accent; fontFamily: root.fontFamily; fontSize: root.fontSize
onClicked: root.request("vcard", {handle: modelData.handle, token: modelData.token, action: "save"})
}
}
ContactButton {
Layout.fillWidth: true
visible: modelData.action === "open"
text: "View details"
enabled: !root.busy
foreground: root.foreground; accent: root.accent; fontFamily: root.fontFamily; fontSize: root.fontSize
onClicked: root.selectedCard = modelData
}
ContactButton {
Layout.fillWidth: true
foreground: root.foreground; accent: root.accent
fontFamily: root.fontFamily; fontSize: root.fontSize
enabled: !root.busy
text: modelData.action === "open" ? "Open on Mac ↗" : "Review"
onClicked: {
if (modelData.action === "candidates" && root.model.view !== "cards") root.overview = root.model
root.request(modelData.action, { handle: modelData.handle, token: modelData.token })
}
}
}
}
}
}
}
RowLayout {
Layout.fillWidth: true
visible: root.model !== null && root.model.view === "scan" && root.model.pageCount > 1
ContactButton {
text: "Previous"; enabled: !root.busy && root.model && root.model.page > 0
foreground: root.foreground; accent: root.accent; fontFamily: root.fontFamily; fontSize: root.fontSize
onClicked: root.scan(root.model.page - 1)
}
Text {
Layout.fillWidth: true; horizontalAlignment: Text.AlignHCenter
text: root.model ? (root.model.page + 1) + " / " + root.model.pageCount : ""
color: root.foreground; font.family: root.fontFamily; font.pixelSize: root.fontSize; textFormat: Text.PlainText
}
ContactButton {
text: "Next"; enabled: !root.busy && root.model && root.model.page + 1 < root.model.pageCount
foreground: root.foreground; accent: root.accent; fontFamily: root.fontFamily; fontSize: root.fontSize
onClicked: root.scan(root.model.page + 1)
}
}
ContactButton {
Layout.fillWidth: true
text: "Save new contact"
visible: root.model !== null && root.model.view === "cards" && root.model.rows.length === 0
enabled: !root.busy && root.error === ""
foreground: root.foreground; accent: root.accent
fontFamily: root.fontFamily; fontSize: root.fontSize
onClicked: root.savingHandle = root.model.detail
}
ContactButton {
Layout.fillWidth: true
text: "Scan contacts"
enabled: !root.busy
foreground: root.foreground; accent: root.accent
fontFamily: root.fontFamily; fontSize: root.fontSize
onClicked: root.scan()
}
}
}