|
1 package org.hedgewars.hedgeroid.Datastructures; |
|
2 |
|
3 import java.io.File; |
|
4 import java.io.IOException; |
|
5 import java.util.ArrayList; |
|
6 import java.util.Collections; |
|
7 import java.util.List; |
|
8 import java.util.Map; |
|
9 import java.util.TreeMap; |
|
10 |
|
11 import org.hedgewars.hedgeroid.Utils; |
|
12 import org.hedgewars.hedgeroid.frontlib.Flib; |
|
13 import org.hedgewars.hedgeroid.frontlib.Frontlib.MetaschemePtr; |
|
14 import org.hedgewars.hedgeroid.frontlib.Frontlib.SchemelistPtr; |
|
15 |
|
16 import android.content.Context; |
|
17 |
|
18 /** |
|
19 * Functions for handling the persistent list of schemes. |
|
20 * Schemes in that list are identified by name (case sensitive). |
|
21 */ |
|
22 public final class Schemes { |
|
23 private Schemes() { |
|
24 throw new AssertionError("This class is not meant to be instantiated"); |
|
25 } |
|
26 |
|
27 public static File getUserSchemesFile(Context c) { |
|
28 return new File(c.getFilesDir(), "schemes_user.ini"); |
|
29 } |
|
30 |
|
31 public static File getBuiltinSchemesFile(Context c) { |
|
32 return new File(c.getFilesDir(), "schemes_builtin.ini"); |
|
33 } |
|
34 |
|
35 public static Map<String, Scheme> loadUserSchemes(Context c) throws IOException { |
|
36 return loadSchemes(c, getUserSchemesFile(c)); |
|
37 } |
|
38 |
|
39 public static Map<String, Scheme> loadBuiltinSchemes(Context c) throws IOException { |
|
40 return loadSchemes(c, getBuiltinSchemesFile(c)); |
|
41 } |
|
42 |
|
43 public static Map<String, Scheme> loadSchemes(Context c, File schemeFile) throws IOException { |
|
44 Map<String, Scheme> result = new TreeMap<String, Scheme>(); |
|
45 String metaschemePath = new File(Utils.getDataPathFile(c), "metasettings.ini").getAbsolutePath(); |
|
46 if(!schemeFile.isFile()) { |
|
47 // No schemes file == no schemes, no error |
|
48 return new TreeMap<String, Scheme>(); |
|
49 } |
|
50 MetaschemePtr meta = null; |
|
51 SchemelistPtr schemeListPtr = null; |
|
52 try { |
|
53 meta = Flib.INSTANCE.flib_metascheme_from_ini(metaschemePath); |
|
54 if(meta==null) { |
|
55 throw new IOException("Unable to read metascheme"); |
|
56 } |
|
57 schemeListPtr = Flib.INSTANCE.flib_schemelist_from_ini(meta, schemeFile.getAbsolutePath()); |
|
58 if(schemeListPtr == null) { |
|
59 throw new IOException("Unable to read schemelist"); |
|
60 } |
|
61 List<Scheme> schemeList = schemeListPtr.deref(); |
|
62 for(Scheme scheme : schemeList) { |
|
63 result.put(scheme.name, scheme); |
|
64 } |
|
65 return result; |
|
66 } finally { |
|
67 if(schemeListPtr != null) { |
|
68 Flib.INSTANCE.flib_schemelist_destroy(schemeListPtr); |
|
69 } |
|
70 if(meta != null) { |
|
71 Flib.INSTANCE.flib_metascheme_release(meta); |
|
72 } |
|
73 } |
|
74 } |
|
75 |
|
76 public static void saveUserSchemes(Context c, Map<String, Scheme> schemes) throws IOException { |
|
77 List<Scheme> schemeList = new ArrayList<Scheme>(schemes.values()); |
|
78 Collections.sort(schemeList, Scheme.caseInsensitiveNameComparator); |
|
79 SchemelistPtr ptr = SchemelistPtr.createJavaOwned(schemeList); |
|
80 Flib.INSTANCE.flib_schemelist_to_ini(getUserSchemesFile(c).getAbsolutePath(), ptr); |
|
81 } |
|
82 } |