fit-webview-bridge 0.2.1a3__tar.gz → 0.2.1a4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of fit-webview-bridge might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: fit-webview-bridge
3
- Version: 0.2.1a3
3
+ Version: 0.2.1a4
4
4
  Summary: Qt native WebView bridge with PySide6 bindings
5
5
  Author: FIT Project
6
6
  License: LGPL-3.0-or-later
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "fit-webview-bridge"
3
- version = "0.2.1a3"
3
+ version = "0.2.1a4"
4
4
  description = "Qt native WebView bridge with PySide6 bindings"
5
5
  requires-python = ">=3.11,<3.14"
6
6
  dependencies = ["PySide6==6.9.0", "shiboken6==6.9.0", "shiboken6-generator==6.9.0"]
@@ -45,13 +45,13 @@ static NSURL* toNSURL(QUrl u);
45
45
  }
46
46
  @end
47
47
 
48
- // =======================
49
- // Navigation + Download delegate
50
- // =======================
48
+ // ===== WKNavDelegate =====
51
49
  @interface WKNavDelegate : NSObject <WKNavigationDelegate, WKDownloadDelegate>
52
50
  @property(nonatomic, assign) WKWebViewWidget* owner;
53
- // stato download (nel delegate, non toccare i privati del widget)
54
- @property(nonatomic, strong) NSMapTable<WKDownload*, NSString*>* downloadPaths;
51
+ // mappe per download
52
+ @property(nonatomic, strong) NSMapTable<WKDownload*, NSString*>* downloadPaths; // weak key -> strong value
53
+ @property(nonatomic, strong) NSMapTable<NSProgress*, WKDownload*>* progressToDownload; // weak->weak
54
+ @property(nonatomic, strong) NSHashTable<NSProgress*>* completedProgresses; // weak set
55
55
  @end
56
56
 
57
57
  @implementation WKNavDelegate
@@ -59,6 +59,8 @@ static NSURL* toNSURL(QUrl u);
59
59
  - (instancetype)init {
60
60
  if ((self = [super init])) {
61
61
  _downloadPaths = [NSMapTable weakToStrongObjectsMapTable];
62
+ _progressToDownload = [NSMapTable weakToWeakObjectsMapTable];
63
+ _completedProgresses = [NSHashTable weakObjectsHashTable];
62
64
  }
63
65
  return self;
64
66
  }
@@ -118,8 +120,7 @@ decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
118
120
  if (navigationResponse.canShowMIMEType) {
119
121
  decisionHandler(WKNavigationResponsePolicyAllow);
120
122
  } else {
121
- // API moderna: "Download" (non BecomeDownload)
122
- decisionHandler(WKNavigationResponsePolicyDownload);
123
+ decisionHandler(WKNavigationResponsePolicyDownload); // API moderna
123
124
  }
124
125
  }
125
126
 
@@ -130,10 +131,9 @@ navigationAction:(WKNavigationAction *)navigationAction
130
131
  didBecomeDownload:(WKDownload *)download
131
132
  {
132
133
  download.delegate = self;
133
- if (self.owner) {
134
- emit self.owner->downloadStarted(QString(), QString());
135
- }
136
-
134
+ if (self.owner) emit self.owner->downloadStarted(QString(), QString());
135
+
136
+ // KVO su NSProgress (3 keyPath, con INITIAL)
137
137
  [download.progress addObserver:self forKeyPath:@"fractionCompleted"
138
138
  options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial)
139
139
  context:NULL];
@@ -143,6 +143,8 @@ didBecomeDownload:(WKDownload *)download
143
143
  [download.progress addObserver:self forKeyPath:@"totalUnitCount"
144
144
  options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial)
145
145
  context:NULL];
146
+
147
+ [self.progressToDownload setObject:download forKey:download.progress];
146
148
  }
147
149
 
148
150
  - (void)webView:(WKWebView *)webView
@@ -153,10 +155,9 @@ didBecomeDownload:(WKDownload *)download
153
155
 
154
156
  NSString* suggested = navigationResponse.response.suggestedFilename ?: @"download";
155
157
  if (self.owner) {
156
- QString destDir = self.owner->downloadDirectory();
157
- QString destPath = destDir + "/" + QString::fromUtf8(suggested.UTF8String);
158
- emit self.owner->downloadStarted(QString::fromUtf8(suggested.UTF8String),
159
- destPath);
158
+ QString dir = self.owner->downloadDirectory();
159
+ QString path = dir + "/" + QString::fromUtf8(suggested.UTF8String);
160
+ emit self.owner->downloadStarted(QString::fromUtf8(suggested.UTF8String), path);
160
161
  }
161
162
 
162
163
  [download.progress addObserver:self forKeyPath:@"fractionCompleted"
@@ -168,6 +169,8 @@ didBecomeDownload:(WKDownload *)download
168
169
  [download.progress addObserver:self forKeyPath:@"totalUnitCount"
169
170
  options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial)
170
171
  context:NULL];
172
+
173
+ [self.progressToDownload setObject:download forKey:download.progress];
171
174
  }
172
175
 
173
176
  #pragma mark - Scegli destinazione
@@ -199,9 +202,7 @@ completionHandler:(void (^)(NSURL * _Nullable destination))completionHandler
199
202
 
200
203
  QString qdir = self.owner->downloadDirectory();
201
204
  NSString* dir = [NSString stringWithUTF8String:qdir.toUtf8().constData()];
202
- if (!dir.length) {
203
- dir = [NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"];
204
- }
205
+ if (!dir.length) dir = [NSHomeDirectory() stringByAppendingPathComponent:@"Downloads"];
205
206
 
206
207
  [[NSFileManager defaultManager] createDirectoryAtPath:dir
207
208
  withIntermediateDirectories:YES
@@ -227,18 +228,19 @@ completionHandler:(void (^)(NSURL * _Nullable destination))completionHandler
227
228
  [super observeValueForKeyPath:keyPath ofObject:obj change:change context:ctx];
228
229
  return;
229
230
  }
230
-
231
231
  NSProgress* prog = (NSProgress*)obj;
232
- int64_t total = prog.totalUnitCount; // può essere -1 (sconosciuto)
232
+
233
+ // ignora aggiornamenti dopo il completamento
234
+ if ([self.completedProgresses containsObject:prog]) return;
235
+
236
+ int64_t total = prog.totalUnitCount; // -1 se sconosciuto
233
237
  int64_t done = prog.completedUnitCount;
234
238
 
235
- // Emetti SEMPRE su main (thread-safety Qt/UI)
236
239
  dispatch_async(dispatch_get_main_queue(), ^{
237
240
  emit self.owner->downloadProgress(done, (total >= 0 ? total : -1));
238
241
  });
239
242
  }
240
243
 
241
-
242
244
  - (void)downloadDidFinish:(WKDownload *)download {
243
245
  if (!self.owner) return;
244
246
 
@@ -248,13 +250,12 @@ completionHandler:(void (^)(NSURL * _Nullable destination))completionHandler
248
250
  [download.progress removeObserver:self forKeyPath:@"totalUnitCount"];
249
251
  } @catch (...) {}
250
252
 
253
+ [self.completedProgresses addObject:download.progress];
254
+
251
255
  NSString* finalPath = [self.downloadPaths objectForKey:download];
252
- if (finalPath) {
253
- emit self.owner->downloadFinished(QString::fromUtf8(finalPath.UTF8String));
254
- [self.downloadPaths removeObjectForKey:download];
255
- } else {
256
- emit self.owner->downloadFinished(QString());
257
- }
256
+ emit self.owner->downloadFinished(finalPath ? QString::fromUtf8(finalPath.UTF8String) : QString());
257
+ if (finalPath) [self.downloadPaths removeObjectForKey:download];
258
+ [self.progressToDownload removeObjectForKey:download.progress];
258
259
  }
259
260
 
260
261
  - (void)download:(WKDownload *)download didFailWithError:(NSError *)error resumeData:(NSData *)resumeData {
@@ -266,10 +267,15 @@ completionHandler:(void (^)(NSURL * _Nullable destination))completionHandler
266
267
  [download.progress removeObserver:self forKeyPath:@"totalUnitCount"];
267
268
  } @catch (...) {}
268
269
 
269
- NSString* finalPath = [self.downloadPaths objectForKey:download];
270
- QString qpath = finalPath ? QString::fromUtf8(finalPath.UTF8String) : QString();
271
- emit self.owner->downloadFailed(qpath, QString::fromUtf8(error.localizedDescription.UTF8String));
270
+ [self.completedProgresses addObject:download.progress];
271
+
272
+ NSString* finalPath = [self.downloadPaths objectForKey:download];
273
+ emit self.owner->downloadFailed(
274
+ finalPath ? QString::fromUtf8(finalPath.UTF8String) : QString(),
275
+ QString::fromUtf8(error.localizedDescription.UTF8String)
276
+ );
272
277
  if (finalPath) [self.downloadPaths removeObjectForKey:download];
278
+ [self.progressToDownload removeObjectForKey:download.progress];
273
279
  }
274
280
 
275
281
  @end