Repository: qutebrowser/qutebrowser

Issue:
Don't remember temporary download directory
I have the following settings:

storage -> download-directory = ""
storage -> prompt-download-directory = true
storage -> remember-download-directory = true

I noticed something unusual about the suggested download directory:

1. run `:download` to open the download prompt
2. note the suggested download location is my home directory or something
   'normal'
3. type `<c-x>` to open the file instead of save
4. later run `:download` again on some page
5. note the suggested download location is `/tmp/qutebrowser-download-xxxxxx/`

I believe this to be a result of the `remember-download-directory` option remembering where the previously downloaded (opened) file was downloaded to (the temp dir) and suggesting that. 

The behaviour should be that it only remembers the download location of the previously _downloaded_ file (not downloaded to a temporary location to be opened).



Discussion:
What version and backend are you seeing this with? I can't reproduce this, neither with QtWebKit nor QtWebEngine.
Version is latest git master, and backend QtWebEngine. I can't reproduce either with the qtwebkit backend.

Did some more testing - I can reproduce it reliably with urls for pdf files, but not with html pages. I have pdf.js enabled in settings, but iirc it's not implemented for the webengine backend yet, so opening a pdf opens a blank page and the download prompt - then the page doesn't auto-close once it's downloaded.
So when I use `C-x` on the automatic download prompt, I can't do `:download` on the same page (as it's an invalid URL). When I then do `:download` on a different page, I still can't reproduce this.

Can you please reproduce this with `--temp-basedir` and then do `:report` and mention this issue?
Reproduced and reported. I also did the same thing and screen-recorded it - see https://nextcloud.walladge.net/s/gMabrbax8qSnd2X
Oh, my bad! I just got confused between `/tmp/qutebrowser-basedir-*` and `qutebrowser-downloads-*` :blush: 

I can indeed reproduce it now, relevant part of the logs:

```
12:22:37 DEBUG    prompt     prompt:ask_question:151 Asking question <qutebrowser.utils.usertypes.Question default='/tmp/qutebrowser-basedir-im0_cjpa/download/' mode=<PromptMode.download: 5> text='Please enter a location for <b>http://www.buildeazy.com/plans/test.pdf</b>' title='Save file to:'>, blocking True, loops [], queue deque([])
[...]
12:22:38 DEBUG    commands   command:run:524 Calling qutebrowser.mainwindow.prompt.PromptContainer.prompt_open_download(<qutebrowser.mainwindow.prompt.PromptContainer win_id=0>, None)
12:22:38 DEBUG    downloads  downloads:_set_filename:605 Setting filename to /tmp/qutebrowser-downloads-8lghmrl0/tmpqd1ly8vstest.pdf
[...]
12:22:38 DEBUG    prompt     prompt:ask_question:188 Ending loop.exec_() for <qutebrowser.utils.usertypes.Question default='/tmp/qutebrowser-basedir-im0_cjpa/download/' mode=<PromptMode.download: 5> text='Please enter a location for <b>http://www.buildeazy.com/plans/test.pdf</b>' title='Save file to:'>
[...]
12:22:38 DEBUG    downloads  webenginedownloads:_on_state_changed:54 State for <qutebrowser.browser.webengine.webenginedownloads.DownloadItem basename='tmpqd1ly8vstest.pdf'> changed to DownloadCompleted
12:22:38 DEBUG    downloads  webenginedownloads:_on_state_changed:61 Download tmpqd1ly8vstest.pdf finished
12:22:38 DEBUG    downloads  downloads:open_file:526 Opening /tmp/qutebrowser-downloads-8lghmrl0/tmpqd1ly8vstest.pdf with the system application
[...]
12:22:41 DEBUG    prompt     prompt:ask_question:151 Asking question <qutebrowser.utils.usertypes.Question default='/tmp/qutebrowser-downloads-8lghmrl0/' mode=<PromptMode.download: 5> text='Please enter a location for <b>http://www.buildeazy.com/plans/test.pdf</b>' title='Save file to:'>, blocking True, loops [], queue deque([])
```

cc @Kingdread 
I guess the problem is that the webengine downloads just use [`self._set_filename`](https://github.com/The-Compiler/qutebrowser/blob/master/qutebrowser/browser/webengine/webenginedownloads.py#L98-L99) to handle temporary downloads. `_set_filename` in turn [always remembers the last used directory](https://github.com/The-Compiler/qutebrowser/blob/master/qutebrowser/browser/downloads.py#L603). webkit [doesn't do this](https://github.com/The-Compiler/qutebrowser/blob/master/qutebrowser/browser/qtnetworkdownloads.py#L227-L228). Though I'm not sure yet why this only happens with PDF files and not with `:download`.

I guess I'll just add a flag to `_set_filename`.

Edit: `:download` always uses the QNetworkManager downloads, that's why...

Resolving patch:
diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py
--- a/qutebrowser/browser/downloads.py
+++ b/qutebrowser/browser/downloads.py
@@ -564,13 +564,16 @@ def _set_tempfile(self, fileobj):
         """Set a temporary file when opening the download."""
         raise NotImplementedError
 
-    def _set_filename(self, filename, *, force_overwrite=False):
+    def _set_filename(self, filename, *, force_overwrite=False,
+                      remember_directory=True):
         """Set the filename to save the download to.
 
         Args:
             filename: The full filename to save the download to.
                       None: special value to stop the download.
             force_overwrite: Force overwriting existing files.
+            remember_directory: If True, remember the directory for future
+                                downloads.
         """
         global last_used_directory
         filename = os.path.expanduser(filename)
@@ -600,7 +603,8 @@ def _set_filename(self, filename, *, force_overwrite=False):
                                                   os.path.expanduser('~'))
 
         self.basename = os.path.basename(self._filename)
-        last_used_directory = os.path.dirname(self._filename)
+        if remember_directory:
+            last_used_directory = os.path.dirname(self._filename)
 
         log.downloads.debug("Setting filename to {}".format(filename))
         if force_overwrite:
diff --git a/qutebrowser/browser/webengine/webenginedownloads.py b/qutebrowser/browser/webengine/webenginedownloads.py
--- a/qutebrowser/browser/webengine/webenginedownloads.py
+++ b/qutebrowser/browser/webengine/webenginedownloads.py
@@ -96,7 +96,8 @@ def _set_fileobj(self, fileobj):
         raise downloads.UnsupportedOperationError
 
     def _set_tempfile(self, fileobj):
-        self._set_filename(fileobj.name, force_overwrite=True)
+        self._set_filename(fileobj.name, force_overwrite=True,
+                           remember_directory=False)
 
     def _ensure_can_set_filename(self, filename):
         state = self._qt_item.state()


Test patch:
diff --git a/tests/end2end/features/downloads.feature b/tests/end2end/features/downloads.feature
--- a/tests/end2end/features/downloads.feature
+++ b/tests/end2end/features/downloads.feature
@@ -424,6 +424,21 @@ Feature: Downloading things from a website.
         And I open data/downloads/download2.bin without waiting
         Then the download prompt should be shown with "(tmpdir)/download2.bin"
 
+    # https://github.com/The-Compiler/qutebrowser/issues/2173
+
+    Scenario: Remembering the temporary download directory (issue 2173)
+        When I set storage -> prompt-download-directory to true
+        And I set completion -> download-path-suggestion to both
+        And I set storage -> remember-download-directory to true
+        And I open data/downloads/download.bin without waiting
+        And I wait for the download prompt for "*"
+        And I run :prompt-accept (tmpdir)
+        And I open data/downloads/download.bin without waiting
+        And I wait for the download prompt for "*"
+        And I directly open the download
+        And I open data/downloads/download.bin without waiting
+        Then the download prompt should be shown with "(tmpdir)/download.bin"
+
     # Overwriting files
 
     Scenario: Not overwriting an existing file