Repository navigation
Expand file tree
/
Copy pathService.qml
More file actions
175 lines (159 loc) · 5.23 KB
/
Copy pathService.qml
File metadata and controls
175 lines (159 loc) · 5.23 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
import QtQuick
import Quickshell.Io
import qs.Commons
import "Model.js" as Model
QtObject {
id: root
property bool isLoggedIn: false
property bool loading: false
property string errorMessage: ""
property string user: ""
property string query: ""
property var recentBookmarks: []
property var searchResults: []
property var currentResults: []
// Credential manager — declared as a property, not inline child
property CredentialManager credentialManager: CredentialManager {
id: credManager
onTokenLoaded: function(token) {
if (token.length > 0) {
root.verifyAndConnect(token);
}
}
onTokenCleared: {
root.isLoggedIn = false;
root.user = "";
root.recentBookmarks = [];
root.searchResults = [];
root.currentResults = [];
}
}
// API bridge — runs each request in a short-lived helper process
// (bin/keeply-api) instead of QML's own XMLHttpRequest, since Qt
// materializes the entire response into responseText inside this
// long-lived shell process as it streams in, before any JS callback
// can inspect or bound it. See ApiBridge.qml.
property ApiBridge apiBridge: ApiBridge {
id: api
}
// Auth bridge for OAuth flow
property AuthBridge authBridge: AuthBridge {
id: bridge
onSuccess: function(token) {
credManager._pendingToken = token;
credManager.store(token);
root.verifyAndConnect(token);
}
onFailed: function(message) {
root.loading = false;
root.errorMessage = message;
}
}
function login() {
root.loading = true;
root.errorMessage = "";
bridge.start();
}
function logout() {
credManager.clear();
}
function verifyAndConnect(token) {
root.loading = true;
root.errorMessage = "";
api.verifyToken(token, function(result, err) {
root.loading = false;
if (result) {
root.isLoggedIn = true;
root.user = result.email || result.name || "";
root.fetchRecent();
} else {
root.isLoggedIn = false;
root.user = "";
if (err === "Request failed (401)" || err === "Request failed (403)") {
// The server has definitively rejected this token (revoked,
// expired, or otherwise no longer valid) — holding onto it
// serves no purpose.
credManager.clear();
} else {
// Anything else (network unreachable, a 5xx, a timeout) says
// nothing about whether the token itself is still good. Clearing
// it here would force a full new OAuth flow over what might be
// a one-off blip; keep it and let retryIfNeeded() try again the
// next time the panel opens.
root.errorMessage = err || "Could not verify your Keeply account.";
}
}
});
}
function fetchRecent() {
if (!root.isLoggedIn) return;
var token = credManager.token;
root.loading = true;
api.fetchBookmarks(token, 1, 20, "date-desc", function(result, err) {
root.loading = false;
if (result && result.data) {
root.errorMessage = "";
var rows = Model.bookmarksToRows(result.data);
root.recentBookmarks = rows;
if (root.query.length === 0) {
root.currentResults = rows;
}
} else {
root.errorMessage = err || "Could not load your bookmarks.";
}
});
}
// Called whenever the panel opens. fetchRecent() and verifyAndConnect()
// otherwise only ever run once, right at startup — if that one attempt
// hits a transient failure, nothing would retry it short of restarting
// the whole shell, and reopening the panel would silently show a
// half-connected or empty state indefinitely.
function retryIfNeeded() {
if (root.loading) return;
if (root.isLoggedIn) {
if (root.recentBookmarks.length === 0) {
root.fetchRecent();
}
} else if (credManager.token.length > 0) {
root.verifyAndConnect(credManager.token);
}
}
function search(q) {
if (!root.isLoggedIn) return;
root.query = q;
if (q.length === 0) {
root.searchResults = [];
root.currentResults = root.recentBookmarks;
return;
}
var token = credManager.token;
root.loading = true;
api.searchBookmarks(token, q, 1, 30, function(result, err) {
// A faster keystroke may have already superseded this request.
if (root.query !== q) return;
root.loading = false;
if (result && result.hits) {
var rows = Model.bookmarksToRows(result.hits);
root.searchResults = rows;
root.currentResults = rows;
} else {
root.searchResults = [];
root.currentResults = [];
}
});
}
function openBookmark(url) {
// Bookmark URLs come from the server, not user input typed here, but
// never hand an arbitrary scheme to the platform's URL handler — only
// plain web links should ever come out of a bookmark manager.
var scheme = String(url || "").match(/^([a-zA-Z][a-zA-Z0-9+.-]*):/);
if (!scheme || (scheme[1].toLowerCase() !== "http" && scheme[1].toLowerCase() !== "https")) {
root.errorMessage = "Refused to open a non-web link.";
return;
}
Qt.openUrlExternally(url);
}
Component.onCompleted: {
credManager.lookup();
}
}