summaryrefslogtreecommitdiff
path: root/bindings/csharp/Manager.cs
blob: 85d9a83881d3acab1d1921d30071bbbff539135a (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2014  CrossWire Bible Society (http://www.crosswire.org)
//  	CrossWire Bible Society
//  	P. O. Box 2528
//  	Tempe, AZ  85280-2528
//  
//  This program is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License as published by the
//  Free Software Foundation version 2.
//  
//  This program is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  General Public License for more details.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Sword
{
	public class Manager : IDisposable
	{
		IntPtr _handle;
		
		public Manager ()
		{
			_handle = NativeMethods.org_crosswire_sword_SWMgr_new();
		}
		
		public Manager (string path)
		{
			_handle = NativeMethods.org_crosswire_sword_SWMgr_newWithPath(path);
		}
		
		
		internal IntPtr Handle
		{
			get
			{
				return _handle;	
			}
		}
		
		protected void Dispose(bool disposing)
		{
			if(disposing)
			{
				if(_handle != IntPtr.Zero)
				{
					NativeMethods.org_crosswire_sword_SWMgr_delete(_handle);
					_handle = IntPtr.Zero;
				}
			}
		}
		
		public Module GetModuleByName(string name)
		{
			IntPtr modulePtr = NativeMethods.org_crosswire_sword_SWMgr_getModuleByName(_handle, name);
			if(modulePtr == IntPtr.Zero)
			{
				return null;	
			}
			return new Module(modulePtr);
		}
		
		/// <summary>
		/// Gets a list of the installed modules
		/// </summary>
		public IEnumerable<ModInfo> GetModInfoList()
		{
			IntPtr modulesPointer = NativeMethods.org_crosswire_sword_SWMgr_getModInfoList(_handle);
			ModInfo modInfo = (ModInfo)Marshal.PtrToStructure(modulesPointer, typeof(ModInfo));
			
			while (modInfo.Name != null) 
			{
				yield return modInfo;
				modulesPointer = new IntPtr(modulesPointer.ToInt64() + Marshal.SizeOf(typeof(ModInfo)));
				modInfo = (ModInfo)Marshal.PtrToStructure(modulesPointer, typeof(ModInfo));
			}
		}
		
		public string Version
		{
			get
			{
				IntPtr versionPtr = NativeMethods.org_crosswire_sword_SWMgr_version(_handle);	
				return Marshal.PtrToStringAnsi(versionPtr);
			}
		}
		
		public string PrefixPath
		{
			get
			{
				IntPtr prefixPathPtr = NativeMethods.org_crosswire_sword_SWMgr_getPrefixPath(_handle);	
				return Marshal.PtrToStringAnsi(prefixPathPtr);
			}
		}
		
		public string ConfigPath
		{
			get
			{
				IntPtr configPathPtr = NativeMethods.org_crosswire_sword_SWMgr_getConfigPath(_handle);	
				return Marshal.PtrToStringAnsi(configPathPtr);
			}
		}
		
		public void SetGlobalOption(string option, string @value)
		{
			NativeMethods.org_crosswire_sword_SWMgr_setGlobalOption(_handle, option, @value);
		}
		
		public IEnumerable<string> GetGlobalOptionValues(string option)
		{
			IntPtr optionsPtr = NativeMethods.org_crosswire_sword_SWMgr_getGlobalOptionValues(_handle, option);
			return NativeMethods.MarshalStringArray(optionsPtr);
		}
		
		public void SetCipherKey(string modName, byte[] key)
		{
			NativeMethods.org_crosswire_sword_SWMgr_setCipherKey(_handle, modName, key);
		}
		
		public bool Javascript
		{
			set
			{
				NativeMethods.org_crosswire_sword_SWMgr_setJavascript(_handle, value);
			}
		}
		
		public IEnumerable<string> AvailableLocales
		{
			get
			{
				IntPtr localesPtr = NativeMethods.org_crosswire_sword_SWMgr_getAvailableLocales(_handle);
				return NativeMethods.MarshalStringArray(localesPtr);
			}
		}
		
		public string DefaultLocale
		{
			set
			{
				NativeMethods.org_crosswire_sword_SWMgr_setDefaultLocale(_handle, value);
			}
		}
		
		public string Translate(string text, string localeName)
		{
			IntPtr translatedPtr = NativeMethods.org_crosswire_sword_SWMgr_translate(_handle, text, localeName);
			return Marshal.PtrToStringAnsi(translatedPtr);
		}
		
		public void Dispose ()
		{
			Dispose (true);
			GC.Collect();
		}
	}
}