2929
3030package org .scijava .desktop .platform .linux ;
3131
32+ import org .scijava .Context ;
3233import org .scijava .app .AppService ;
3334import org .scijava .desktop .DesktopIntegrationProvider ;
34- import org .scijava .desktop .links .LinkHandler ;
3535import org .scijava .desktop .links .LinkService ;
3636import org .scijava .desktop .links .SchemeInstaller ;
3737import org .scijava .log .LogService ;
4747import java .nio .file .Path ;
4848import java .nio .file .Paths ;
4949import java .nio .file .StandardOpenOption ;
50- import java .util .HashSet ;
50+ import java .util .Collections ;
5151import java .util .LinkedHashMap ;
52- import java .util .List ;
5352import java .util .Map ;
5453import java .util .Set ;
5554
@@ -74,7 +73,7 @@ public class LinuxPlatform extends AbstractPlatform
7473{
7574
7675 @ Parameter
77- private LinkService linkService ;
76+ private Context context ;
7877
7978 @ Parameter
8079 private AppService appService ;
@@ -120,20 +119,16 @@ public void open(final URL url) throws IOException {
120119 public boolean isWebLinksEnabled () {
121120 try {
122121 final DesktopFile df = getOrCreateDesktopFile ();
123- final Set <String > schemes = collectSchemes ();
124- if (schemes .isEmpty ()) return false ;
125122
126123 // Check if any scheme is registered
127- for (final String scheme : schemes ) {
124+ for (final String scheme : schemes () ) {
128125 if (df .hasMimeType ("x-scheme-handler/" + scheme )) return true ;
129126 }
130- return false ;
131- } catch (final IOException e ) {
132- if (log != null ) {
133- log .debug ("Failed to check web links status" , e );
134- }
135- return false ;
136127 }
128+ catch (final IOException e ) {
129+ if (log != null ) log .debug ("Failed to check web links status" , e );
130+ }
131+ return false ;
137132 }
138133
139134 @ Override
@@ -143,14 +138,10 @@ public boolean isWebLinksEnabled() {
143138 public void setWebLinksEnabled (final boolean enable ) throws IOException {
144139 final DesktopFile df = getOrCreateDesktopFile ();
145140
146- final Set <String > schemes = collectSchemes ();
147- for (final String scheme : schemes ) {
141+ for (final String scheme : schemes ()) {
148142 final String mimeType = "x-scheme-handler/" + scheme ;
149- if (enable ) {
150- df .addMimeType (mimeType );
151- } else {
152- df .removeMimeType (mimeType );
153- }
143+ if (enable ) df .addMimeType (mimeType );
144+ else df .removeMimeType (mimeType );
154145 }
155146
156147 df .save ();
@@ -269,14 +260,13 @@ public void setFileExtensionsEnabled(final boolean enable) throws IOException {
269260 // Remove file extension MIME types from .desktop file
270261 // Keep URI scheme handlers (x-scheme-handler/...)
271262 final DesktopFile df = getOrCreateDesktopFile ();
272- final Set <String > uriSchemes = collectSchemes ();
273263
274264 for (final String mimeType : mimeMapping .values ()) {
275265 df .removeMimeType (mimeType );
276266 }
277267
278268 // Re-add URI scheme handlers
279- for (final String scheme : uriSchemes ) {
269+ for (final String scheme : schemes () ) {
280270 df .addMimeType ("x-scheme-handler/" + scheme );
281271 }
282272
@@ -397,20 +387,23 @@ private String sanitizeFileName(final String name) {
397387
398388 // -- Helper methods --
399389
400- /**
401- * Collects all URI schemes from registered LinkHandler plugins.
402- */
403- private Set <String > collectSchemes () {
404- final Set <String > schemes = new HashSet <>();
405- if (linkService == null ) return schemes ;
406-
407- for (final LinkHandler handler : linkService .getInstances ()) {
408- final List <String > handlerSchemes = handler .getSchemes ();
409- if (handlerSchemes != null ) {
410- schemes .addAll (handlerSchemes );
411- }
412- }
413- return schemes ;
390+ private LinkService linkService () {
391+ // NB: We cannot declare LinkService as an @Parameter because
392+ // the PlatformService creates its plugin singletons before the
393+ // LinkService has been instantiated and added to the context.
394+ return context .getService (LinkService .class );
395+ }
396+
397+ private Set <String > schemes () {
398+ final LinkService linkService = linkService ();
399+ return linkService == null ?
400+ Collections .emptySet () : linkService .getSchemes ();
401+ }
402+
403+ private Set <String > extensions () {
404+ final LinkService linkService = linkService ();
405+ return linkService == null ?
406+ Collections .emptySet () : linkService .getFileExtensions ();
414407 }
415408
416409 /**
@@ -421,8 +414,8 @@ private synchronized Map<String, String> loadMimeTypeMapping() throws IOExceptio
421414
422415 extensionToMime = new LinkedHashMap <>();
423416
424- // TODO: We need public API in LinkService for registering the extensions we want.
425- // TODO: Then, in fiji-links, we can use that API to plug in all the SCIFIO-supported file formats.
417+ final Set < String > extensions = extensions ();
418+ // TODO: How do we know the MIME type of each extension?
426419
427420 return extensionToMime ;
428421 }
0 commit comments