27
27
import java .nio .file .Paths ;
28
28
import java .nio .file .StandardCopyOption ;
29
29
import java .util .ArrayList ;
30
+ import java .util .Arrays ;
30
31
import java .util .HashMap ;
32
+ import java .util .HashSet ;
31
33
import java .util .List ;
32
34
import java .util .Map ;
35
+ import java .util .Set ;
33
36
34
37
import org .apache .commons .compress .archivers .ArchiveEntry ;
35
38
import org .apache .commons .compress .archivers .ArchiveInputStream ;
@@ -61,17 +64,13 @@ public class Manager {
61
64
private Manager () {
62
65
}
63
66
64
- public static void addPackageURLs (String packageUrlsToAdd []) {
65
- String [] originalBoardUrls = ConfigurationPreferences .getPackageURLList ();
67
+ public static void addPackageURLs (HashSet <String > packageUrlsToAdd , boolean forceDownload ) {
68
+ HashSet <String > originalBoardUrls = new HashSet <>(
69
+ Arrays .asList (ConfigurationPreferences .getBoardsPackageURLList ()));
70
+ packageUrlsToAdd .addAll (originalBoardUrls );
66
71
67
- String [] newBoardsUrls = new String [packageUrlsToAdd .length + originalBoardUrls .length ];
68
- System .arraycopy (packageUrlsToAdd , 0 , newBoardsUrls , 0 , packageUrlsToAdd .length );
69
- System .arraycopy (originalBoardUrls , 0 , newBoardsUrls , packageUrlsToAdd .length , originalBoardUrls .length );
70
- ConfigurationPreferences .setPackageURLs (newBoardsUrls );
71
-
72
- if (packageIndices != null ) {
73
- loadIndices ();
74
- }
72
+ ConfigurationPreferences .setBoardsPackageURLs (packageUrlsToAdd );
73
+ loadIndices (forceDownload );
75
74
76
75
}
77
76
@@ -82,7 +81,7 @@ public static void addPackageURLs(String packageUrlsToAdd[]) {
82
81
* @param monitor
83
82
*/
84
83
public static void startup_Pluging (IProgressMonitor monitor ) {
85
- loadIndices ();
84
+ loadIndices (ConfigurationPreferences . getUpdateJasonFilesValue () );
86
85
try {
87
86
List <Board > allBoards = getInstalledBoards ();
88
87
if (allBoards .isEmpty ()) { // we test for boards
@@ -172,11 +171,11 @@ static public IStatus downloadAndInstall(ArduinoPlatform platform, boolean force
172
171
173
172
}
174
173
175
- static public void loadIndices () {
176
- String [] boardUrls = ConfigurationPreferences .getPackageURLList ();
174
+ static private void loadIndices (boolean forceDownload ) {
175
+ String [] boardUrls = ConfigurationPreferences .getBoardsPackageURLList ();
177
176
packageIndices = new ArrayList <>(boardUrls .length );
178
177
for (String boardUrl : boardUrls ) {
179
- loadPackageIndex (boardUrl , false );
178
+ loadPackageIndex (boardUrl , forceDownload );
180
179
}
181
180
182
181
loadLibraryIndex (false );
@@ -245,7 +244,7 @@ static private void loadPackageIndex(String url, boolean forceDownload) {
245
244
246
245
static public List <PackageIndex > getPackageIndices () {
247
246
if (packageIndices == null ) {
248
- String [] boardUrls = ConfigurationPreferences .getPackageURLList ();
247
+ String [] boardUrls = ConfigurationPreferences .getBoardsPackageURLList ();
249
248
packageIndices = new ArrayList <>(boardUrls .length );
250
249
for (String boardUrl : boardUrls ) {
251
250
loadPackageIndex (boardUrl , false );
@@ -772,4 +771,71 @@ public static int compareVersions(String version1, String version2) {
772
771
return 0 ;
773
772
}
774
773
774
+ /**
775
+ * This method removes the json files from disk and removes memory
776
+ * references to these files or their content
777
+ *
778
+ * @param packageUrlsToRemove
779
+ */
780
+ public static void removeBoardsPackageURLs (Set <String > packageUrlsToRemove ) {
781
+ // remove the files from memory
782
+ Set <String > activeBoardUrls = new HashSet <>(Arrays .asList (ConfigurationPreferences .getBoardsPackageURLList ()));
783
+
784
+ activeBoardUrls .removeAll (packageUrlsToRemove );
785
+
786
+ ConfigurationPreferences .setBoardsPackageURLs (activeBoardUrls .toArray (null ));
787
+
788
+ // remove the files from disk
789
+ for (String curJson : packageUrlsToRemove ) {
790
+ File localFile = getLocalFileName (curJson );
791
+ if (localFile .exists ()) {
792
+ localFile .delete ();
793
+ }
794
+ }
795
+
796
+ // reload the indices (this will remove all potential remaining
797
+ // references
798
+ // existing files do not need to be refreshed as they have been
799
+ // refreshed at startup
800
+ loadIndices (false );
801
+
802
+ }
803
+
804
+ public static String [] getBoardsPackageURLList () {
805
+ return ConfigurationPreferences .getBoardsPackageURLList ();
806
+ }
807
+
808
+ /**
809
+ * Completely replace the list with jsons with a new list
810
+ *
811
+ * @param newBoardJsonUrls
812
+ */
813
+ public static void setBoardsPackageURL (String [] newBoardJsonUrls ) {
814
+
815
+ String curJsons [] = getBoardsPackageURLList ();
816
+ HashSet <String > origJsons = new HashSet <>(Arrays .asList (curJsons ));
817
+ HashSet <String > currentSelectedJsons = new HashSet <>(Arrays .asList (newBoardJsonUrls ));
818
+ currentSelectedJsons .removeAll (origJsons );
819
+ // remove the files from disk which were in the old lst but not in the
820
+ // new one
821
+ for (String curJson : currentSelectedJsons ) {
822
+ File localFile = getLocalFileName (curJson );
823
+ if (localFile .exists ()) {
824
+ localFile .delete ();
825
+ }
826
+ }
827
+ // save to configurationsettings before calling LoadIndices
828
+ ConfigurationPreferences .setBoardsPackageURLs (newBoardJsonUrls );
829
+ // reload the indices (this will remove all potential remaining
830
+ // references
831
+ // existing files do not need to be refreshed as they have been
832
+ // refreshed at startup
833
+ // new files will be added
834
+ loadIndices (false );
835
+ }
836
+
837
+ public static String getBoardsPackageURLs () {
838
+ return ConfigurationPreferences .getDefaultBoardsPackageURLs ();
839
+ }
840
+
775
841
}
0 commit comments