From 89271681cf77d6bab22c607e3b790a14477dcd92 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 27 Nov 2013 08:44:27 +0530 Subject: [PATCH] File open dialogs; If the previously used initial directory, no longer exists, use a directory one level up. Fixes #1255216 [Adding book or new format opening Windows\System32 folder as default](https://bugs.launchpad.net/calibre/+bug/1255216) --- src/calibre/gui2/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index ea3eef8c6f..f2c1fd6065 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -585,6 +585,16 @@ def file_icon_provider(): initialize_file_icon_provider() return _file_icon_provider +def select_initial_dir(q): + while q: + c = os.path.dirname(q) + if c == q: + break + if os.path.exists(c): + return c + q = c + return os.path.expanduser('~') + class FileDialog(QObject): def __init__(self, title=_('Choose Files'), filters=[], @@ -620,6 +630,8 @@ class FileDialog(QObject): os.path.expanduser(default_dir)) if not isinstance(initial_dir, basestring): initial_dir = os.path.expanduser(default_dir) + if not initial_dir or not os.path.exists(initial_dir): + initial_dir = select_initial_dir(initial_dir) self.selected_files = [] use_native_dialog = 'CALIBRE_NO_NATIVE_FILEDIALOGS' not in os.environ with SanitizeLibraryPath():