1 /*
2  * CRANMirror.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.studio.client.common.mirrors.model;
16 
17 import java.util.ArrayList;
18 
19 import org.rstudio.core.client.StringUtil;
20 import org.rstudio.studio.client.workbench.prefs.model.UserPrefs;
21 
22 public class CRANMirror extends UserPrefs.CranMirror
23 {
CRANMirror()24    protected CRANMirror()
25    {
26    }
27 
empty()28    public final static native CRANMirror empty() /*-{
29       var cranMirror = new Object();
30       cranMirror.name = "";
31       cranMirror.host = "";
32       cranMirror.url = "";
33       cranMirror.secondary = "";
34       cranMirror.country = "";
35       cranMirror.changed = false;
36 
37       return cranMirror;
38    }-*/;
39 
isEmpty()40    public final boolean isEmpty()
41    {
42       return getName() == null || getName().length() == 0;
43    }
44 
setName(String name)45    public final native void setName(String name) /*-{
46       this.name = name;
47    }-*/;
48 
setHost(String host)49    public final native void setHost(String host) /*-{
50       this.host = host;
51    }-*/;
52 
getURL()53    public final native String getURL() /*-{
54       return this.url;
55    }-*/;
56 
setURL(String url)57    public final native void setURL(String url) /*-{
58       this.url = url;
59    }-*/;
60 
setSecondary(String secondary)61    private final native void setSecondary(String secondary) /*-{
62       this.secondary = secondary;
63    }-*/;
64 
getError()65    private final native String getError() /*-{
66       return this.error;
67    }-*/;
68 
setSecondaryRepos(String cran, ArrayList<CRANMirror> repos)69    private final void setSecondaryRepos(String cran, ArrayList<CRANMirror> repos)
70    {
71       setURL(cran);
72 
73       ArrayList<String> entries = new ArrayList<>();
74       for (CRANMirror repo : repos)
75       {
76          if (!repo.getName().toLowerCase().equals("cran"))
77          {
78             entries.add(repo.getName() + "|" + repo.getURL());
79          }
80       }
81 
82       setSecondary(StringUtil.join(entries, "|"));
83    }
84 
setSecondaryRepos(ArrayList<CRANMirror> repos)85    public final void setSecondaryRepos(ArrayList<CRANMirror> repos)
86    {
87       setSecondaryRepos(getURL(), repos);
88    }
89 
getSecondaryRepos()90    public final ArrayList<CRANMirror> getSecondaryRepos()
91    {
92       ArrayList<CRANMirror> repos = new ArrayList<>();
93 
94       String secondary = getSecondary();
95       if (StringUtil.isNullOrEmpty(secondary))
96       {
97          // Return empty list of secondary repos if none were defined.
98          return repos;
99       }
100 
101       String[] entries = secondary.split("\\|");
102       for (int i = 0; i < entries.length / 2; i++)
103       {
104          CRANMirror repo = CRANMirror.empty();
105          repo.setName(entries[2 * i]);
106          repo.setURL(entries[2 * i + 1]);
107 
108          repos.add(repo);
109       }
110 
111       return repos;
112    }
113 
114    /**
115     * Sets Name and Host as a "custom" CRAN repo defined by URL alone
116     */
setAsCustom()117    public final void setAsCustom()
118    {
119       setName(getCustomEnumValue());
120       setHost(getCustomEnumValue());
121    }
122 
123    /**
124     * Returns whether this is a "custom" CRAN repo (eg: defined by URL alone)
125     */
isCustom()126    public final boolean isCustom()
127    {
128       return getHost().equals(getCustomEnumValue());
129    }
130 
131    /**
132     * Returns a formatted display name for this CRANMirror.
133     *
134     * Returned name includes Name and Host if a standard host, and simply the URL of a custom host from the user
135     */
getDisplay()136    public final String getDisplay()
137    {
138       if (isCustom())
139       {
140          // Host is Custom with no standard Name/Host info.  Identify by URL
141          return getURL();
142       } else {
143          return getName() + " - " + getHost();
144       }
145    }
146 
147    // Implement enumerators as functions because this extends a JavaScriptObject
148    // A cleaner way might be to wrap this in a wrapper class, but this gets the job done
getCustomEnumValue()149    public final static String getCustomEnumValue()
150    {
151       return "Custom"; //$NON-NLS-1$
152    }
153 
getSecondaryEnumValue()154    public final static String getSecondaryEnumValue()
155    {
156       return "Secondary"; //$NON-NLS-1$
157    }
158 
159 }
160