From b6ac567711bb052d2dd4c55e41d171ba5b36405a Mon Sep 17 00:00:00 2001
From: James Tandy <admin@tandyukservers.co.uk>
Date: Sun, 3 Jan 2016 02:18:15 +0000
Subject: [PATCH 1/8] Add Auto Open EDSM Option

---
 prefs.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/prefs.py b/prefs.py
index 1400bf31..5b564399 100644
--- a/prefs.py
+++ b/prefs.py
@@ -177,6 +177,11 @@ class PreferencesDialog(tk.Toplevel):
         self.edsm_apikey.insert(0, config.get('edsm_apikey') or '')
         self.edsm_apikey.grid(row=11, column=1, padx=PADX, pady=PADY, sticky=tk.EW)
 
+       self.edsm_autoopen= tk.IntVar(value = (output & config.EDSM_autoopen) and 1)
+        nb.Checkbutton(edsmframe, text=_('Automatically open EDSM System view for uncharted systems'), variable=self.EDSM_autoopen).grid(columnspan=2, padx=BUTTONX, sticky=tk.W)
+
+
+
         notebook.add(edsmframe, text='EDSM')		# Not translated
 
 
@@ -361,7 +366,8 @@ class PreferencesDialog(tk.Toplevel):
                    (self.out_log_file.get() and config.OUT_LOG_FILE) +
                    (self.out_ship_coriolis.get() and config.OUT_SHIP_CORIOLIS) +
                    (self.out_log_edsm.get() and config.OUT_LOG_EDSM) +
-                   (self.out_log_auto.get() and config.OUT_LOG_AUTO))
+                   (self.out_log_auto.get() and config.OUT_LOG_AUTO) +
+                   (self.EDSM_autoopen.get() and config.EDSM_autoopen))
         config.set('outdir', self.outdir.get().startswith('~') and join(config.home, self.outdir.get()[1:]) or self.outdir.get())
 
         config.set('edsm_cmdrname', self.edsm_cmdr.get().strip())

From f5913eefba586de4aff57f8871e71190072ff9e8 Mon Sep 17 00:00:00 2001
From: James Tandy <admin@tandyukservers.co.uk>
Date: Sun, 3 Jan 2016 02:24:48 +0000
Subject: [PATCH 2/8] Allow auto open of EDSM when finding an uncharted system

---
 edsm.py | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/edsm.py b/edsm.py
index 922ed2a4..f57f1acc 100644
--- a/edsm.py
+++ b/edsm.py
@@ -44,11 +44,15 @@ class EDSM:
             if data == -1:
                 # System not present - but don't create it on the assumption that the caller will
                 self.result['img'] = EDSM._IMG_NEW
+                if (config.getint('EDSM_autoopen')):
+                    webbrowser.open(self.result['url'])
             elif data.get('coords'):
                 self.result['img'] = EDSM._IMG_KNOWN
                 self.syscache.add(system_name)
             else:
                 self.result['img'] = EDSM._IMG_UNKNOWN
+                if (config.getint('EDSM_autoopen')):
+                    webbrowser.open(self.result['url'])
 
     # Asynchronous version of the above
     def start_lookup(self, system_name, known=0):

From 9d8f97bd6f2ee726ec63f35a669fb02365c86085 Mon Sep 17 00:00:00 2001
From: James Tandy <admin@tandyukservers.co.uk>
Date: Sun, 3 Jan 2016 02:47:13 +0000
Subject: [PATCH 3/8] Update prefs.py

---
 prefs.py | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/prefs.py b/prefs.py
index 5b564399..63f76753 100644
--- a/prefs.py
+++ b/prefs.py
@@ -177,11 +177,9 @@ class PreferencesDialog(tk.Toplevel):
         self.edsm_apikey.insert(0, config.get('edsm_apikey') or '')
         self.edsm_apikey.grid(row=11, column=1, padx=PADX, pady=PADY, sticky=tk.EW)
 
-       self.edsm_autoopen= tk.IntVar(value = (output & config.EDSM_autoopen) and 1)
+        self.edsm_autoopen= tk.IntVar(value = (output & config.EDSM_autoopen) and 1)
         nb.Checkbutton(edsmframe, text=_('Automatically open EDSM System view for uncharted systems'), variable=self.EDSM_autoopen).grid(columnspan=2, padx=BUTTONX, sticky=tk.W)
 
-
-
         notebook.add(edsmframe, text='EDSM')		# Not translated
 
 

From 19aa5dc9f8807354b3ddb66285cde91e36b9d04f Mon Sep 17 00:00:00 2001
From: James Tandy <admin@tandyukservers.co.uk>
Date: Sun, 3 Jan 2016 03:29:42 +0000
Subject: [PATCH 4/8] Update edsm.py

---
 edsm.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/edsm.py b/edsm.py
index f57f1acc..fee7b307 100644
--- a/edsm.py
+++ b/edsm.py
@@ -3,7 +3,7 @@ import threading
 from sys import platform
 import time
 import urllib
-
+import webbrowser
 import Tkinter as tk
 
 from config import applongname, appversion, config
@@ -44,14 +44,14 @@ class EDSM:
             if data == -1:
                 # System not present - but don't create it on the assumption that the caller will
                 self.result['img'] = EDSM._IMG_NEW
-                if (config.getint('EDSM_autoopen')):
+                if (config.EDSM_AUTOOPEN):
                     webbrowser.open(self.result['url'])
             elif data.get('coords'):
                 self.result['img'] = EDSM._IMG_KNOWN
                 self.syscache.add(system_name)
             else:
                 self.result['img'] = EDSM._IMG_UNKNOWN
-                if (config.getint('EDSM_autoopen')):
+                if (config.EDSM_AUTOOPEN):
                     webbrowser.open(self.result['url'])
 
     # Asynchronous version of the above
@@ -81,15 +81,20 @@ class EDSM:
                 result['img'] = EDSM._IMG_NEW
                 result['done'] = True	# give feedback immediately
                 requests.get('http://www.edsm.net/api-v1/url?sysname=%s&fromSoftware=%s&fromSoftwareVersion=%s' % (urllib.quote(system_name), urllib.quote(applongname), urllib.quote(appversion)), timeout=EDSM._TIMEOUT)	# creates system
+                if (config.EDSM_AUTOOPEN):
+                    webbrowser.open('http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name))                
             elif data.get('coords'):
                 result['img'] = EDSM._IMG_KNOWN
                 result['done'] = True
                 self.syscache.add(system_name)
             else:
                 result['img'] = EDSM._IMG_UNKNOWN
+                if (config.EDSM_AUTOOPEN):
+                    webbrowser.open('http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name))                
         except:
             if __debug__: print_exc()
             result['img'] = EDSM._IMG_ERROR
+
         result['done'] = True
 
 

From 33fa28c7ae21d2b2f5b6ca518cbc11e693fd98a3 Mon Sep 17 00:00:00 2001
From: James Tandy <admin@tandyukservers.co.uk>
Date: Sun, 3 Jan 2016 03:30:15 +0000
Subject: [PATCH 5/8] Update config.py

---
 config.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/config.py b/config.py
index dfb40d6d..03c95963 100644
--- a/config.py
+++ b/config.py
@@ -80,6 +80,7 @@ class Config:
     OUT_SHIP_CORIOLIS = 128
     OUT_LOG_EDSM = 256
     OUT_LOG_AUTO = 512
+    EDSM_AUTOOPEN = 1024
 
     if platform=='darwin':
 

From 8da38ad5f23b3f11fd39b614e2d3ce5137f03abd Mon Sep 17 00:00:00 2001
From: James Tandy <admin@tandyukservers.co.uk>
Date: Sun, 3 Jan 2016 03:31:36 +0000
Subject: [PATCH 6/8] Update prefs.py

---
 prefs.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/prefs.py b/prefs.py
index 63f76753..0fad188b 100644
--- a/prefs.py
+++ b/prefs.py
@@ -177,8 +177,8 @@ class PreferencesDialog(tk.Toplevel):
         self.edsm_apikey.insert(0, config.get('edsm_apikey') or '')
         self.edsm_apikey.grid(row=11, column=1, padx=PADX, pady=PADY, sticky=tk.EW)
 
-        self.edsm_autoopen= tk.IntVar(value = (output & config.EDSM_autoopen) and 1)
-        nb.Checkbutton(edsmframe, text=_('Automatically open EDSM System view for uncharted systems'), variable=self.EDSM_autoopen).grid(columnspan=2, padx=BUTTONX, sticky=tk.W)
+        self.edsm_autoopen= tk.IntVar(value = (output & config.EDSM_AUTOOPEN) and 1)
+        nb.Checkbutton(edsmframe, text=_('Automatically open EDSM System view for uncharted systems'), variable=self.edsm_autoopen).grid(columnspan=2, padx=BUTTONX, sticky=tk.W)
 
         notebook.add(edsmframe, text='EDSM')		# Not translated
 
@@ -365,7 +365,7 @@ class PreferencesDialog(tk.Toplevel):
                    (self.out_ship_coriolis.get() and config.OUT_SHIP_CORIOLIS) +
                    (self.out_log_edsm.get() and config.OUT_LOG_EDSM) +
                    (self.out_log_auto.get() and config.OUT_LOG_AUTO) +
-                   (self.EDSM_autoopen.get() and config.EDSM_autoopen))
+                   (self.edsm_autoopen.get() and config.EDSM_AUTOOPEN))
         config.set('outdir', self.outdir.get().startswith('~') and join(config.home, self.outdir.get()[1:]) or self.outdir.get())
 
         config.set('edsm_cmdrname', self.edsm_cmdr.get().strip())

From 6cda9a067e502bf76e4204c0c0a467afe7f7c442 Mon Sep 17 00:00:00 2001
From: Jonathan Harris <jonathan@marginal.org.uk>
Date: Tue, 5 Jan 2016 02:20:15 +0000
Subject: [PATCH 7/8] Enable autoopen only when flight log is selected.

---
 L10n/en.template | 3 +++
 prefs.py         | 7 ++++---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/L10n/en.template b/L10n/en.template
index bfc8c3c5..cea936a0 100644
--- a/L10n/en.template
+++ b/L10n/en.template
@@ -19,6 +19,9 @@
 /* Output setting. [prefs.py] */
 "Automatically make a log entry on entering a system" = "Automatically make a log entry on entering a system";
 
+/* [prefs.py] */
+"Automatically open uncharted systems' EDSM page" = "Automatically open uncharted systems' EDSM page";
+
 /* Cmdr stats. [stats.py] */
 "Balance" = "Balance";
 
diff --git a/prefs.py b/prefs.py
index eec7ecfb..82c4d019 100644
--- a/prefs.py
+++ b/prefs.py
@@ -156,6 +156,9 @@ class PreferencesDialog(tk.Toplevel):
         ttk.Separator(edsmframe, orient=tk.HORIZONTAL).grid(columnspan=2, padx=PADX, pady=PADY, sticky=tk.EW)
         self.out_log_edsm = tk.IntVar(value = (output & config.OUT_LOG_EDSM) and 1)
         nb.Checkbutton(edsmframe, text=_('Send flight log to Elite Dangerous Star Map'), variable=self.out_log_edsm, command=self.outvarchanged).grid(columnspan=2, padx=BUTTONX, sticky=tk.W)
+        self.edsm_autoopen = tk.IntVar(value = (output & config.EDSM_AUTOOPEN) and 1)
+        self.edsm_autoopen_button = nb.Checkbutton(edsmframe, text=_("Automatically open uncharted systems' EDSM page"), variable=self.edsm_autoopen)
+        self.edsm_autoopen_button.grid(columnspan=2, padx=BUTTONX, sticky=tk.W)
         if monitor.logdir:
             self.edsm_log_auto_button = nb.Checkbutton(edsmframe, text=_('Automatically make a log entry on entering a system'), variable=self.out_log_auto, command=self.outvarchanged)	# Output setting
             self.edsm_log_auto_button.grid(columnspan=2, padx=BUTTONX, sticky=tk.W)
@@ -177,9 +180,6 @@ class PreferencesDialog(tk.Toplevel):
         self.edsm_apikey.insert(0, config.get('edsm_apikey') or '')
         self.edsm_apikey.grid(row=11, column=1, padx=PADX, pady=PADY, sticky=tk.EW)
 
-        self.edsm_autoopen= tk.IntVar(value = (output & config.EDSM_AUTOOPEN) and 1)
-        nb.Checkbutton(edsmframe, text=_('Automatically open EDSM System view for uncharted systems'), variable=self.edsm_autoopen).grid(columnspan=2, padx=BUTTONX, sticky=tk.W)
-
         notebook.add(edsmframe, text='EDSM')		# Not translated
 
 
@@ -248,6 +248,7 @@ class PreferencesDialog(tk.Toplevel):
         self.outdir['state']       = local and 'readonly' or tk.DISABLED
 
         edsm_state = self.out_log_edsm.get() and tk.NORMAL or tk.DISABLED
+        self.edsm_autoopen_button['state'] = edsm_state
         self.edsm_label['state']        = edsm_state
         self.edsm_cmdr_label['state']   = edsm_state
         self.edsm_apikey_label['state'] = edsm_state

From a3d2ddf6b0815ad7906a366b34fe1e7b19b37b0e Mon Sep 17 00:00:00 2001
From: Jonathan Harris <jonathan@marginal.org.uk>
Date: Tue, 5 Jan 2016 02:26:15 +0000
Subject: [PATCH 8/8] Open system page after flight log has been sent.

Avoids potential race condition of opening the system page for a new
system before EDSM has created it.
---
 EDMarketConnector.py |  2 ++
 edsm.py              | 25 ++++++++++---------------
 2 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/EDMarketConnector.py b/EDMarketConnector.py
index 4e81b269..52e0499f 100755
--- a/EDMarketConnector.py
+++ b/EDMarketConnector.py
@@ -449,6 +449,8 @@ class AppWindow:
         result = self.edsm.result
         if result['done']:
             self.system['image'] = result['img']
+            if result['uncharted'] and (config.getint('output') & config.EDSM_AUTOOPEN):
+                webbrowser.open(result['url'])
         else:
             self.w.after(int(EDSM_POLL * 1000), self.edsmpoll)
 
diff --git a/edsm.py b/edsm.py
index fee7b307..ca356f9a 100644
--- a/edsm.py
+++ b/edsm.py
@@ -3,7 +3,7 @@ import threading
 from sys import platform
 import time
 import urllib
-import webbrowser
+
 import Tkinter as tk
 
 from config import applongname, appversion, config
@@ -28,15 +28,15 @@ class EDSM:
     # Just set link without doing a lookup
     def link(self, system_name):
         self.cancel_lookup()
-        self.result = { 'img': '', 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': True }
+        self.result = { 'img': '', 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': True, 'uncharted': False }
 
     def lookup(self, system_name, known=0):
         self.cancel_lookup()
 
         if known or system_name in self.syscache:
-            self.result = { 'img': EDSM._IMG_KNOWN, 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': True }
+            self.result = { 'img': EDSM._IMG_KNOWN, 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': True, 'uncharted': False }
         else:
-            self.result = { 'img': EDSM._IMG_ERROR, 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': True }
+            self.result = { 'img': EDSM._IMG_ERROR, 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': True, 'uncharted': False }
             r = requests.get('http://www.edsm.net/api-v1/system?sysname=%s&coords=1&fromSoftware=%s&fromSoftwareVersion=%s' % (urllib.quote(system_name), urllib.quote(applongname), urllib.quote(appversion)), timeout=EDSM._TIMEOUT)
             r.raise_for_status()
             data = r.json()
@@ -44,24 +44,22 @@ class EDSM:
             if data == -1:
                 # System not present - but don't create it on the assumption that the caller will
                 self.result['img'] = EDSM._IMG_NEW
-                if (config.EDSM_AUTOOPEN):
-                    webbrowser.open(self.result['url'])
+                self.result['uncharted'] = True
             elif data.get('coords'):
                 self.result['img'] = EDSM._IMG_KNOWN
                 self.syscache.add(system_name)
             else:
                 self.result['img'] = EDSM._IMG_UNKNOWN
-                if (config.EDSM_AUTOOPEN):
-                    webbrowser.open(self.result['url'])
+                self.result['uncharted'] = True
 
     # Asynchronous version of the above
     def start_lookup(self, system_name, known=0):
         self.cancel_lookup()
 
         if known or system_name in self.syscache:	# Cache URLs of systems with known coordinates
-            self.result = { 'img': EDSM._IMG_KNOWN, 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': True }
+            self.result = { 'img': EDSM._IMG_KNOWN, 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': True, 'uncharted': False }
         else:
-            self.result = { 'img': '', 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': False }
+            self.result = { 'img': '', 'url': 'http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name), 'done': False, 'uncharted': False }
             self.thread = threading.Thread(target = self.worker, name = 'EDSM worker', args = (system_name, self.result))
             self.thread.daemon = True
             self.thread.start()
@@ -79,22 +77,19 @@ class EDSM:
             if data == -1:
                 # System not present - create it
                 result['img'] = EDSM._IMG_NEW
+                result['uncharted'] = True
                 result['done'] = True	# give feedback immediately
                 requests.get('http://www.edsm.net/api-v1/url?sysname=%s&fromSoftware=%s&fromSoftwareVersion=%s' % (urllib.quote(system_name), urllib.quote(applongname), urllib.quote(appversion)), timeout=EDSM._TIMEOUT)	# creates system
-                if (config.EDSM_AUTOOPEN):
-                    webbrowser.open('http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name))                
             elif data.get('coords'):
                 result['img'] = EDSM._IMG_KNOWN
                 result['done'] = True
                 self.syscache.add(system_name)
             else:
                 result['img'] = EDSM._IMG_UNKNOWN
-                if (config.EDSM_AUTOOPEN):
-                    webbrowser.open('http://www.edsm.net/show-system?systemName=%s' % urllib.quote(system_name))                
+                result['uncharted'] = True
         except:
             if __debug__: print_exc()
             result['img'] = EDSM._IMG_ERROR
-
         result['done'] = True