Skip to content

Commit 0d7d1a5

Browse files
authored
Merge pull request #211 from chennes/addCacheControl
Add ability to disable QNAM cache for requests
2 parents 4c5e365 + aaac4b2 commit 0d7d1a5

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

NetworkManager.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def query_download_size(self, url: str, timeout_ms: int = default_timeout):
338338
current_index = next(self.counting_iterator) # A thread-safe counter
339339
item = QueueItem(
340340
current_index,
341-
self.__create_get_request(url, timeout_ms),
341+
self.__create_get_request(url, timeout_ms, disable_cache=true),
342342
track_progress=False,
343343
operation=QtNetwork.QNetworkAccessManager.HeadOperation,
344344
)
@@ -347,9 +347,7 @@ def query_download_size(self, url: str, timeout_ms: int = default_timeout):
347347
return current_index
348348

349349
def submit_unmonitored_get(
350-
self,
351-
url: str,
352-
timeout_ms: int = default_timeout,
350+
self, url: str, timeout_ms: int = default_timeout, disable_cache: bool = False
353351
) -> int:
354352
"""Adds this request to the queue, and returns an index that can be used by calling code
355353
in conjunction with the completed() signal to handle the results of the call. All data is
@@ -360,16 +358,16 @@ def submit_unmonitored_get(
360358
# Use a queue because we can only put things on the QNAM from the main event loop thread
361359
self.queue.put(
362360
QueueItem(
363-
current_index, self.__create_get_request(url, timeout_ms), track_progress=False
361+
current_index,
362+
self.__create_get_request(url, timeout_ms, disable_cache),
363+
track_progress=False,
364364
)
365365
)
366366
self.__request_queued.emit()
367367
return current_index
368368

369369
def submit_monitored_get(
370-
self,
371-
url: str,
372-
timeout_ms: int = default_timeout,
370+
self, url: str, timeout_ms: int = default_timeout, disable_cache: bool = False
373371
) -> int:
374372
"""Adds this request to the queue, and returns an index that can be used by calling code
375373
in conjunction with the progress_made() and progress_completed() signals to handle the
@@ -382,7 +380,9 @@ def submit_monitored_get(
382380
# Use a queue because we can only put things on the QNAM from the main event loop thread
383381
self.queue.put(
384382
QueueItem(
385-
current_index, self.__create_get_request(url, timeout_ms), track_progress=True
383+
current_index,
384+
self.__create_get_request(url, timeout_ms, disable_cache),
385+
track_progress=True,
386386
)
387387
)
388388
self.__request_queued.emit()
@@ -394,6 +394,7 @@ def blocking_get_with_retries(
394394
timeout_ms: int = default_timeout,
395395
max_attempts: int = 3,
396396
delay_ms: int = 1000,
397+
disable_cache: bool = False,
397398
):
398399
"""Submits a GET request to the QNetworkAccessManager and blocks until it is complete. Do
399400
not use on the main GUI thread, it will prevent any event processing while it blocks.
@@ -421,9 +422,7 @@ def blocking_get_with_retries(
421422
time.sleep(delay_ms / 1000)
422423

423424
def blocking_get(
424-
self,
425-
url: str,
426-
timeout_ms: int = default_timeout,
425+
self, url: str, timeout_ms: int = default_timeout, disable_cache: bool = False
427426
) -> Optional[QtCore.QByteArray]:
428427
"""Submits a GET request to the QNetworkAccessManager and blocks until it is complete. Do
429428
not use on the main GUI thread, it will prevent any event processing while it blocks.
@@ -438,7 +437,9 @@ def blocking_get(
438437

439438
self.queue.put(
440439
QueueItem(
441-
current_index, self.__create_get_request(url, timeout_ms), track_progress=False
440+
current_index,
441+
self.__create_get_request(url, timeout_ms, disable_cache),
442+
track_progress=False,
442443
)
443444
)
444445
self.__request_queued.emit()
@@ -476,18 +477,27 @@ def __synchronous_process_completion(
476477
self.synchronous_complete[index] = True
477478

478479
@staticmethod
479-
def __create_get_request(url: str, timeout_ms: int) -> QtNetwork.QNetworkRequest:
480+
def __create_get_request(
481+
url: str, timeout_ms: int, disable_cache: bool
482+
) -> QtNetwork.QNetworkRequest:
480483
"""Construct a network request to a given URL"""
481484
request = QtNetwork.QNetworkRequest(QtCore.QUrl(url))
482485
request.setAttribute(
483486
QtNetwork.QNetworkRequest.RedirectPolicyAttribute,
484487
QtNetwork.QNetworkRequest.ManualRedirectPolicy,
485488
)
486-
request.setAttribute(QtNetwork.QNetworkRequest.CacheSaveControlAttribute, True)
487-
request.setAttribute(
488-
QtNetwork.QNetworkRequest.CacheLoadControlAttribute,
489-
QtNetwork.QNetworkRequest.PreferNetwork,
490-
)
489+
if disable_cache:
490+
request.setAttribute(QtNetwork.QNetworkRequest.CacheSaveControlAttribute, False)
491+
request.setAttribute(
492+
QtNetwork.QNetworkRequest.CacheLoadControlAttribute,
493+
QtNetwork.QNetworkRequest.AlwaysNetwork,
494+
)
495+
else:
496+
request.setAttribute(QtNetwork.QNetworkRequest.CacheSaveControlAttribute, True)
497+
request.setAttribute(
498+
QtNetwork.QNetworkRequest.CacheLoadControlAttribute,
499+
QtNetwork.QNetworkRequest.PreferNetwork,
500+
)
491501
if hasattr(request, "setTransferTimeout"):
492502
# Added in Qt 5.15
493503
# In Qt 5, the function setTransferTimeout seems to accept
@@ -630,8 +640,13 @@ def __reply_finished(self) -> None:
630640
if hasattr(request, "transferTimeout"):
631641
timeout_ms = request.transferTimeout()
632642
new_url = reply.attribute(QtNetwork.QNetworkRequest.RedirectionTargetAttribute)
643+
disable_cache = not reply.request().attribute(
644+
QtNetwork.QNetworkRequest.CacheSaveControlAttribute
645+
)
633646
self.__launch_request(
634-
index, self.__create_get_request(new_url, timeout_ms), reply.operation()
647+
index,
648+
self.__create_get_request(new_url, timeout_ms, disable_cache=disable_cache),
649+
reply.operation(),
635650
)
636651
return # The task is not done, so get out of this method now
637652
if reply.error() != QtNetwork.QNetworkReply.NetworkError.OperationCanceledError:

addonmanager_connection_checker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class ConnectionCheckerGUI(QtCore.QObject):
3737
starts to take too long, and an error message if the network cannot be accessed."""
3838

3939
connection_available = QtCore.Signal()
40+
no_connection = QtCore.Signal()
4041
check_complete = QtCore.Signal()
4142

4243
def __init__(self):
@@ -87,12 +88,13 @@ def _network_connection_failed(self, message: str) -> None:
8788
# This must run on the main GUI thread
8889
if hasattr(self, "connection_check_message") and self.connection_check_message:
8990
self.connection_check_message.close()
91+
self.no_connection.emit()
9092
MessageDialog.show_modal(
9193
MessageDialog.DialogType.ERROR,
9294
"AddonManager_ConnectionFailedDialog",
9395
translate("AddonsInstaller", "Connection failed"),
9496
message,
95-
QtWidgets.QMessageBox.OK,
97+
QtWidgets.QMessageBox.Ok,
9698
)
9799
self._disconnect_signals()
98100
self.check_complete.emit()

addonmanager_workers_utility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def run(self):
6363
self.done = False
6464
NetworkManager.AM_NETWORK_MANAGER.completed.connect(self.connection_data_received)
6565
self.request_id = NetworkManager.AM_NETWORK_MANAGER.submit_unmonitored_get(
66-
url, timeout_ms=30000
66+
url, timeout_ms=30000, disable_cache=True
6767
)
6868
while not self.done:
6969
if QtCore.QThread.currentThread().isInterruptionRequested():

0 commit comments

Comments
 (0)