Index: applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java
===================================================================
--- applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java	(revision 19149)
+++ applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java	(revision 19267)
@@ -12,4 +12,5 @@
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Date;
 import java.util.Vector;
 
@@ -43,4 +44,5 @@
     }
     private Vector<PlanImage> listOfFeuilles = new Vector<PlanImage>();
+    private long cookieTimestamp;
 
     final String baseURL = "http://www.cadastre.gouv.fr";
@@ -51,5 +53,5 @@
     final String cOptionListEnd = "</option>";
     final String cBBoxCommunStart = "new GeoBox(";
-    final String cBBoxCommunEnd = ")";
+    final String cBBoxCommunEnd = ")";    
 
     final String cInterfaceVector = "afficherCarteCommune.do";
@@ -58,30 +60,32 @@
     final String cImageLinkStart = "title=\"image\"><a href=\"#\" onClick=\"popup('afficherCarteFeuille.do?f=";
     final String cImageNameStart = ">Feuille ";
+    
+    final static long cCookieExpiration = 30 * 60 * 1000; // 30 minutes expressed in milliseconds
+
+    final  int cRetriesGetCookie = 10; // 10 times every 3 seconds means 30 seconds trying to get a cookie
 
     public boolean retrieveInterface(WMSLayer wmsLayer) throws DuplicateLayerException {
         if (wmsLayer.getName().equals(""))
             return false;
+        if (wmsLayer.getName().equals(lastWMSLayerName))
+            return true;
         // open the session with the French Cadastre web front end
         downloadCancelled = false;
         try {
-            if (cookie == null || !wmsLayer.getName().equals(lastWMSLayerName)) {
+            if (cookie == null || isCookieExpired())
                 getCookie();
-                getInterface(wmsLayer);
-                this.lastWMSLayerName = wmsLayer.getName();
+            if (cookie != null && interfaceRef == null) {
+                    getInterface(wmsLayer);
+                    this.lastWMSLayerName = wmsLayer.getName();
+            } else {
+                JOptionPane.showMessageDialog(Main.parent,
+                        tr("Cannot open a new client session.\nServer in maintenance or temporary overloaded."));
+                return false;
             }
             openInterface();
         } catch (IOException e) {
-            /*JOptionPane.showMessageDialog(Main.parent,
+            JOptionPane.showMessageDialog(Main.parent,
                     tr("Town/city {0} not found or not available\n" +
-                            "or action canceled", wmsLayer.getLocation()));*/
-            JOptionPane pane = new JOptionPane(
-                    tr("Town/city {0} not found or not available\n" +
-                            "or action canceled", wmsLayer.getLocation()),
-                            JOptionPane.INFORMATION_MESSAGE);
-            // this below is a temporary workaround to fix the "always on top" issue
-            JDialog dialog = pane.createDialog(Main.parent, tr("Select commune"));
-            CadastrePlugin.prepareDialog(dialog);
-            dialog.setVisible(true);
-            // till here
+                            "or action canceled", wmsLayer.getLocation()));
             return false;
         }
@@ -89,23 +93,37 @@
     }
 
+    /**
+     * 
+     * @return true if a cookie is delivered by WMS and false is WMS is not opening a client session
+     *         (too many clients or in maintenance)
+     * @throws IOException
+     */
     private void getCookie() throws IOException {
+        boolean cookied = false;
+        int retries = cRetriesGetCookie;
         try {
-            // first, get the cookie from Cadastre to allow next downloads
             searchFormURL = new URL(baseURL + "/scpc/accueil.do");
-            urlConn = (HttpURLConnection)searchFormURL.openConnection();
-            urlConn.setRequestMethod("GET");
-            urlConn.connect();
-            if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
-                throw new IOException("Cannot get Cadastre cookie.");
-            }
-            System.out.println("GET "+searchFormURL);
-            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
-            while(in.readLine() != null) {}  // read the buffer otherwise we sent POST too early
-            String headerName=null;
-            for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
-                if (headerName.equals("Set-Cookie")) {
-                    cookie = urlConn.getHeaderField(i);
-                    cookie = cookie.substring(0, cookie.indexOf(";"));
-                    System.out.println("Cookie="+cookie);
+            while (cookied == false && retries > 0) {
+                urlConn = (HttpURLConnection)searchFormURL.openConnection();
+                urlConn.setRequestMethod("GET");
+                urlConn.connect();
+                if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
+                    System.out.println("GET "+searchFormURL);
+                    BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
+                    while(in.readLine() != null) {}  // read the buffer otherwise we sent POST too early
+                    String headerName=null;
+                    for (int i=1; (headerName = urlConn.getHeaderFieldKey(i))!=null; i++) {
+                        if (headerName.equals("Set-Cookie")) {
+                            cookie = urlConn.getHeaderField(i);
+                            cookie = cookie.substring(0, cookie.indexOf(";"));
+                            cookieTimestamp = new Date().getTime();
+                            System.out.println("received cookie=" + cookie + " at " + new Date(cookieTimestamp));
+                            cookied = true;
+                        }
+                    }
+                } else {
+                    System.out.println("Request to home page failed. Http error:"+urlConn.getResponseCode()+". Try again "+retries+" times");
+                    CadastrePlugin.safeSleep(3000);
+                    retries --;
                 }
             }
@@ -118,9 +136,19 @@
     public void resetCookie() {
         lastWMSLayerName = null;
-    }
-
-    public void resetCookieIfNewLayer(String newWMSLayerName) {
+        cookie = null;
+    }
+    
+    public boolean isCookieExpired() {
+        long now = new Date().getTime();
+        if ((now - cookieTimestamp) > cCookieExpiration) {
+            System.out.println("cookie received at "+new Date(cookieTimestamp)+" expired (now is "+new Date(now)+")");
+            return true;
+        }
+        return false;
+    }
+
+    public void resetInterfaceRefIfNewLayer(String newWMSLayerName) {
         if (!newWMSLayerName.equals(lastWMSLayerName)) {
-            resetCookie();
+            interfaceRef = null;
         }
     }
@@ -154,5 +182,4 @@
                     int res = selectFeuilleDialog();
                     if (res != -1) {
-                        // TODO
                         wmsLayer.setCodeCommune(listOfFeuilles.elementAt(res).name);
                         checkLayerDuplicates(wmsLayer);
Index: applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastrePlugin.java
===================================================================
--- applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastrePlugin.java	(revision 19149)
+++ applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastrePlugin.java	(revision 19267)
@@ -87,8 +87,13 @@
  *                 - removed autosourcing of empty new nodes
  * 1.6 28-Nov-2009 - Fix minor issues if Grab is called without layer (possible since projection rework)
- * 1.7 12-Dec-2009 - Change URL's changes for cookie and downgrade imgs resolution due to WMS changes 
+ * 1.7 12-Dec-2009 - Change URL's changes for cookie and downgrade imgs resolution due to WMS changes
+ * 1.8 xxx         - filter the mouse button 1 during georeferencing
+ *                 - possibility to modify the auto-sourcing text just before upload 
+ *                 - retry if getting a new cookie failed (10 times during 30 seconds)
+ *                 - cookie expiration automatically detected and renewed (after 30 minutes)
+ *                 - proper WMS layer cleanup at destruction (workaround for memory leak)
  */
 public class CadastrePlugin extends Plugin {
-    static String VERSION = "1.7";
+    static String VERSION = "1.8";
 
     static JMenu cadastreJMenu;
@@ -122,5 +127,5 @@
      */
     public CadastrePlugin() throws Exception {
-        System.out.println("Pluging \"cadastre-fr\" started...");
+        System.out.println("Pluging cadastre-fr v"+VERSION+" started...");
         if (Main.pref.get("cadastrewms.cacheDir").equals(""))
             cacheDir = Main.pref.getPreferencesDir()+"plugins/cadastrewms/";
Index: applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionGrabPlanImage.java
===================================================================
--- applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionGrabPlanImage.java	(revision 19149)
+++ applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionGrabPlanImage.java	(revision 19267)
@@ -124,4 +124,6 @@
         else
             mouseClickedTime = System.currentTimeMillis();
+        if (e.getButton() != MouseEvent.BUTTON1)
+            return;
         countMouseClicked++;
         EastNorth ea = Main.proj.latlon2eastNorth(Main.map.mapView.getLatLon(e.getX(), e.getY()));
Index: applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionNewLocation.java
===================================================================
--- applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionNewLocation.java	(revision 19149)
+++ applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionNewLocation.java	(revision 19267)
@@ -39,5 +39,5 @@
         String codeDepartement = "";
         String codeCommune = "";
-        boolean resetCookie = false;
+        boolean changeInterface = false;
         JLabel labelSectionNewLocation = new JLabel(tr("Add a new layer"));
         JPanel p = new JPanel(new GridBagLayout());
@@ -66,5 +66,5 @@
         if (!inputTown.getText().equals("")) {
             location = inputTown.getText().toUpperCase();
-            resetCookie = true;
+            changeInterface = true;
             Main.pref.put("cadastrewms.location", location);
             Main.pref.put("cadastrewms.codeCommune", codeCommune);
@@ -89,9 +89,9 @@
         } else if (existingLayers != null && existingLayers.size() > 0 && Main.map.mapView.getActiveLayer() instanceof WMSLayer) {
             wmsLayer = (WMSLayer)Main.map.mapView.getActiveLayer();
-            resetCookie = true;
+            changeInterface = true;
         }
 
-        if (resetCookie)
-            CadastrePlugin.cadastreGrabber.getWmsInterface().resetCookieIfNewLayer(wmsLayer.getName());
+        if (changeInterface)
+            CadastrePlugin.cadastreGrabber.getWmsInterface().resetInterfaceRefIfNewLayer(wmsLayer.getName());
         return wmsLayer;
     }
Index: applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSLayer.java
===================================================================
--- applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSLayer.java	(revision 19149)
+++ applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSLayer.java	(revision 19267)
@@ -88,4 +88,11 @@
     }
 
+    public void destroy() {
+        super.destroy();
+        images = null;
+        dividedBbox = null;
+        System.out.println("Layer "+location+" destroyed");
+    }
+    
     private static String buildName(String location, String codeCommune) {
         String ret = new String(location.toUpperCase());
