# HG changeset patch
# User Rudolph Gutzerhagen <rudolph.gutzerhagen@gmail.com>
# Date 1610683906 18000
#      Thu Jan 14 23:11:46 2021 -0500
# Node ID 331b3ae7103677deaf7eac5ed7b613a5e3573b8f
# Parent  786c6252a1edeea27e66a22cc028b8f4b42bb964
Tune up start-directory and last-directory behaviour, particularly for
Chirp on Windows.  This change is relatively benign for the user's
perspective.

start_dir
    o fix win32 file dialog start-directory issue in platform.py
      to support the open of correct or selected directory
      when requested.
    o add start_dir optional parameter to do_open() and do_import() methods.
      Include logic to pass start_dir requests to gui_open_file() in platform .

last_dir
    o capture last_dir information when files opened in do_open() and do_import methods.
    o pass last_dir as start_dir parameter in call to do_open()
      from ChirpMain's "menu helper" mh() method.

diff --git a/chirp/platform.py b/chirp/platform.py
--- a/chirp/platform.py
+++ b/chirp/platform.py
@@ -390,7 +390,17 @@
             typestrs = None
 
         try:
-            fname, _, _ = win32gui.GetOpenFileNameW(Filter=typestrs)
+            if not start_dir:
+                fname, _, _ = win32gui.GetOpenFileNameW(Filter=typestrs)
+            else:
+                try:
+                    # set environment variable with start-directory
+                    os.environ['opentodir'] = start_dir
+                except Exception, e:
+                    LOG.error("Failed to set environment opentodir: %s" % e)
+                    return None
+                fname, _, _ = win32gui.GetOpenFileNameW(
+                    Filter=typestrs, InitialDir=os.environ['opentodir'])
         except Exception, e:
             LOG.error("Failed to get filename: %s" % e)
             return None
diff --git a/chirp/ui/mainapp.py b/chirp/ui/mainapp.py
--- a/chirp/ui/mainapp.py
+++ b/chirp/ui/mainapp.py
@@ -314,7 +314,7 @@
         except:
             return
 
-    def do_open(self, fname=None, tempname=None):
+    def do_open(self, fname=None, tempname=None, start_dir=None):
         if not fname:
             types = [(_("All files") + " (*.*)", "*"),
                      (_("CHIRP Radio Images") + " (*.img)", "*.img"),
@@ -327,7 +327,11 @@
                      (_("VX6 Commander Files") + " (*.vx6)", "*.vx6"),
                      (_("VX7 Commander Files") + " (*.vx7)", "*.vx7"),
                      ]
-            fname = platform.get_platform().gui_open_file(types=types)
+            if not start_dir:
+                fname = platform.get_platform().gui_open_file(types=types)
+            else:
+                fname = platform.get_platform().gui_open_file(
+                        start_dir=start_dir, types=types)
             if not fname:
                 return
 
@@ -379,6 +383,8 @@
                 common.show_error(os.path.basename(fname) + ": " + str(e))
                 return
 
+        platform.get_platform().set_last_dir(os.path.dirname(fname))
+        LOG.debug('set last_dir at open: %s' % os.path.dirname(fname))
         first_tab = False
         try:
             eset = editorset.EditorSet(radio, self,
@@ -844,7 +850,7 @@
 
         return True
 
-    def do_import(self):
+    def do_import(self, start_dir=None):
         types = [(_("All files") + " (*.*)", "*"),
                  (_("CHIRP Files") + " (*.chirp)", "*.chirp"),
                  (_("CHIRP Radio Images") + " (*.img)", "*.img"),
@@ -858,10 +864,16 @@
                  (_("VX5 Commander Files") + " (*.vx5)", "*.vx5"),
                  (_("VX6 Commander Files") + " (*.vx6)", "*.vx6"),
                  (_("VX7 Commander Files") + " (*.vx7)", "*.vx7")]
-        filen = platform.get_platform().gui_open_file(types=types)
+        if not start_dir:
+            filen = platform.get_platform().gui_open_file(types=types)
+        else:
+            filen = platform.get_platform().gui_open_file(
+                    start_dir=start_dir, types=types)
         if not filen:
             return
 
+        platform.get_platform().set_last_dir(os.path.dirname(filen))
+        LOG.debug('set last_dir at import: %s' % os.path.dirname(filen))
         eset = self.get_current_editorset()
         count = eset.do_import(filen)
         reporting.report_model_usage(eset.rthread.radio, "import", count > 0)
@@ -1773,7 +1785,7 @@
         elif action == "new":
             self.do_new()
         elif action == "open":
-            self.do_open()
+            self.do_open(start_dir=platform.get_platform().get_last_dir())
         elif action == "save":
             self.do_save()
         elif action == "saveas":
@@ -1785,7 +1797,7 @@
         elif action == "close":
             self.do_close()
         elif action == "import":
-            self.do_import()
+            self.do_import(start_dir=platform.get_platform().get_last_dir())
         elif action in ["qdmrmarc", "idmrmarc"]:
             self.do_dmrmarc(action[0] == "i")
         elif action in ["qrfinder", "irfinder"]:
