Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8ac61c5258d789e00622399ccfdbf1b521bf3e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.cdt.arduino.core.internal;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.service.prefs.BackingStoreException;

public class ArduinoPreferences {

	private static final String ARDUINO_HOME = "arduinoHome"; //$NON-NLS-1$
	private static final String BOARD_URLS = "boardUrls"; //$NON-NLS-1$

	private static final String defaultHome = Paths.get(System.getProperty("user.home"), ".arduinocdt").toString(); //$NON-NLS-1$ //$NON-NLS-2$
	private static final String defaultBoardUrls = "http://downloads.arduino.cc/packages/package_index.json" //$NON-NLS-1$
			+ "\nhttp://arduino.esp8266.com/stable/package_esp8266com_index.json" //$NON-NLS-1$
			+ "\nhttps://adafruit.github.io/arduino-board-index/package_adafruit_index.json"; //$NON-NLS-1$

	private static IEclipsePreferences getPrefs() {
		return InstanceScope.INSTANCE.getNode(Activator.getId());
	}

	public static Path getArduinoHome() {
		return Paths.get(getPrefs().get(ARDUINO_HOME, defaultHome));
	}

	public static String getBoardUrls() {
		return getPrefs().get(BOARD_URLS, defaultBoardUrls);
	}

	public static Collection<URL> getBoardUrlList() throws CoreException {
		List<URL> urlList = new ArrayList<>();
		for (String url : getBoardUrls().split("\n")) { //$NON-NLS-1$
			try {
				urlList.add(new URL(url.trim()));
			} catch (MalformedURLException e) {
				throw Activator.coreException(e);
			}
		}
		return urlList;
	}
	
	public static void setBoardUrls(String boardUrls) {
		IEclipsePreferences prefs = getPrefs();
		prefs.put(BOARD_URLS, boardUrls);
		try {
			prefs.flush();
		} catch (BackingStoreException e) {
			Activator.log(e);
		}
	}

	public static String getDefaultBoardUrls() {
		return defaultBoardUrls;
	}
}

Back to the top