pom.xml : comment for the file permissions problem
[taxeditor.git] / eu.etaxonomy.taxeditor.application / src / main / java / eu / etaxonomy / taxeditor / update / P2Util.java
index e666929acc0b96365cf295874c7fdef93a2c2738..9e1731c4cf7cdcc7c73745e5bcf39b1d8ef705b1 100644 (file)
@@ -2,19 +2,29 @@ package eu.etaxonomy.taxeditor.update;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
+import org.eclipse.equinox.internal.p2.ui.ProvUI;
 import org.eclipse.equinox.internal.p2.ui.model.ElementUtils;
 import org.eclipse.equinox.internal.p2.ui.model.MetadataRepositoryElement;
+import org.eclipse.equinox.p2.repository.IRepository;
+import org.eclipse.equinox.p2.repository.IRepositoryManager;
+import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
+import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
 import org.eclipse.equinox.p2.core.IProvisioningAgent;
+import org.eclipse.equinox.p2.core.ProvisionException;
 import org.eclipse.equinox.p2.operations.ProvisioningJob;
 import org.eclipse.equinox.p2.operations.ProvisioningSession;
+import org.eclipse.equinox.p2.operations.RepositoryTracker;
 import org.eclipse.equinox.p2.operations.Update;
 import org.eclipse.equinox.p2.operations.UpdateOperation;
 import org.eclipse.equinox.p2.ui.ProvisioningUI;
@@ -26,6 +36,7 @@ import org.osgi.framework.ServiceReference;
 import eu.etaxonomy.taxeditor.ApplicationUtil;
 import eu.etaxonomy.taxeditor.TaxonomicEditorPlugin;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 
 /**
  * This class is a utility class for updating the editor from a p2 update site,
@@ -36,10 +47,7 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
  * - Allow configurable update sites
  *
  * Notes :
- * - tried to get rid of the popup dialog which warns about unsigned jars using
- *   -Declipse.p2.unsignedPolicy=allow but this does not work due to
- *   https://bugs.eclipse.org/bugs/show_bug.cgi?id=391399, so the only solution
- *   is to sign the jars with an official certificate.
+ *
  *
  * @see http://wiki.eclipse.org/Equinox/p2/Adding_Self-Update_to_an_RCP_Application
  * @see http://bugs.eclipse.org/281226
@@ -48,6 +56,7 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
 public class P2Util {
 
     //private static String LOCAL_UPDATE_SITE = "file:///path/.../to/Development/EDIT/taxeditor/eu.etaxonomy.taxeditor/target/repository/";
+
     private static String EDIT_NIGHTLY_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/nightly/";
     private static String EDIT_NIGHTLY_UPDATE_SITE_NAME = "Taxonomic Editor Nightly";
 
@@ -57,8 +66,12 @@ public class P2Util {
     private static String EDIT_STABLE_UPDATE_SITE = "http://cybertaxonomy.eu/download/taxeditor/update/stable/";
     private static String EDIT_STABLE_UPDATE_SITE_NAME = "Taxonomic Editor Stable";
 
+    /**
+     * Retrieve and load the saved list of repositories from the preference store,
+     * making sure that at least the default repository is always loaded.
+     */
     @SuppressWarnings("restriction")
-    public static void setDefaultUpdateRepositories() {
+    public static void setP2UpdateRepositories() {
         String updateSite = EDIT_NIGHTLY_UPDATE_SITE;
         String updateSiteName = EDIT_NIGHTLY_UPDATE_SITE_NAME;
 
@@ -66,19 +79,67 @@ public class P2Util {
             updateSite = EDIT_STABLE_UPDATE_SITE;
             updateSiteName = EDIT_STABLE_UPDATE_SITE_NAME;
         }
-        try {
-          final MetadataRepositoryElement element = new MetadataRepositoryElement(null, new URI(updateSite), true);
-          element.setNickname(updateSiteName);
-          ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),new MetadataRepositoryElement[] {element}, null);
-        } catch (URISyntaxException e) {
-            MessagingUtils.errorDialog("Invalid update site URI",
-                    P2Util.class,
-                    "The update site URI has an invalid syntax",
-                    TaxonomicEditorPlugin.PLUGIN_ID,
-                    e,
-                    false);
+        List<MetadataRepositoryElement> repoElements = new ArrayList<MetadataRepositoryElement>();
+        List<MetadataRepositoryElement> savedRepoElements = PreferencesUtil.getP2Repositories();
+        if(savedRepoElements.isEmpty()) {
+            // we always need an update site, so we add the default one
+            try {
+                MetadataRepositoryElement element = new MetadataRepositoryElement(null, new URI(updateSite), true);
+                element.setNickname(updateSiteName);
+                repoElements.add(element);
+            } catch (URISyntaxException e) {
+                MessagingUtils.errorDialog("Invalid update site URI",
+                        P2Util.class,
+                        "The update site URI has an invalid syntax",
+                        TaxonomicEditorPlugin.PLUGIN_ID,
+                        e,
+                        false);
+            }
+        }
+        repoElements.addAll(savedRepoElements);
+
+        ElementUtils.updateRepositoryUsingElements(ProvisioningUI.getDefaultUI(),repoElements
+                .toArray(new MetadataRepositoryElement[]{} ), null);
+
+    }
+
+    /**
+     * {@link org.eclipse.equinox.p2.ui.RepositoryManipulationPage} which handles the repsitory site list
+     * in preferences does not create a preference store and hence the changes are not saved. This means
+     * that we need to save it ourselves.
+     *
+     * This method saves the list of current repositories in the preference store as a string with
+     * specific delimiters.
+     */
+
+    public static void saveP2RepositoryPreferences() {
+
+        IMetadataRepositoryManager metaManager = ProvUI.getMetadataRepositoryManager(ProvisioningUI.getDefaultUI().getSession());
+
+        URI[] currentlyEnabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_ALL);
+        URI[] currentlyDisabled = metaManager.getKnownRepositories(IRepositoryManager.REPOSITORIES_DISABLED);
+
+        List<MetadataRepositoryElement> repoElements = new ArrayList<MetadataRepositoryElement>();
+
+        for(URI repo : currentlyEnabled) {
+            boolean enabled = true;
+            String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
+            MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
+            element.setNickname(nickname);
+            element.setEnabled(enabled);
+            repoElements.add(element);
         }
-      }
+
+        for(URI repo : currentlyDisabled) {
+            boolean enabled = false;
+            String nickname = metaManager.getRepositoryProperty(repo, IRepository.PROP_NICKNAME);
+            MetadataRepositoryElement element = new MetadataRepositoryElement(null, repo, true);
+            element.setNickname(nickname);
+            element.setEnabled(enabled);
+            repoElements.add(element);
+        }
+        PreferencesUtil.setP2Repositories(repoElements);
+    }
 
     /**
      *