Sorry for the combined patch. The changes became too inter-dependent.
[beagle.git] / tools / Config.cs
blob08f7b97c166036d24e71dccdd8ed83938c1ac7e9
1 //
2 // Config.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Diagnostics;
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Reflection;
34 using Beagle;
35 using Beagle.Util;
37 //using Gtk;
38 using GLib;
40 public static class ConfigTool {
42 private static void PrintUsageAndExit ()
44 string usage =
45 "beagle-config: Command-line interface to the Beagle config file.\n" +
46 "Web page: http://www.gnome.org/projects/beagle\n" +
47 "Copyright (C) 2005 Novell, Inc.\n\n";
48 usage +=
49 "Usage: beagle-config [OPTIONS]\n" +
50 " or: beagle-config <SECTION>\n" +
51 " or: beagle-config <SECTION> <SECTIONOPTION> [PARAMS]\n\n" +
52 "Options:\n" +
53 " --beagled-reload-config\tAsk the beagle daemon to reload\n" +
54 " \tthe configuration file.\n" +
55 " --list-sections\t\tList all available configuration sections.\n" +
56 " --help\t\t\tPrint this usage message.\n\n" +
57 "If a section is specified with no options, then a list of the available commands for that section is shown.\n";
59 Console.WriteLine (usage);
61 System.Environment.Exit (0);
64 private static void ListSectionsAndExit ()
66 Console.WriteLine ("Available configuration sections: ");
67 foreach (string key in Conf.Sections.Keys)
68 Console.WriteLine (" - {0}", key);
70 System.Environment.Exit (0);
73 private static void ListSectionOptionsAndExit (string sectionname, Hashtable options)
75 Console.WriteLine ("Available options for section '{0}':", sectionname);
76 foreach (string key in options.Keys) {
77 Console.Write (" - {0}", key);
78 if (options [key] != null)
79 Console.Write (" ({0})", options [key]);
81 Console.WriteLine ();
83 if (sectionname == "daemon")
84 Console.WriteLine (" - ListBackends (List enabled and disabled backends)");
86 System.Environment.Exit (0);
89 private static void ReloadConfigAndExit ()
91 try {
92 ReloadConfigRequest request = new ReloadConfigRequest ();
93 request.Send ();
94 Console.WriteLine ("ReloadConfig request was sent successfully.");
95 System.Environment.Exit (0);
96 } catch (Exception e) {
97 Console.Error.WriteLine ("ERROR: Could not send ReloadConfig request: {0}", e.Message);
98 System.Environment.Exit (-1);
102 public static void Main (string [] args)
104 if (args.Length == 0)
105 PrintUsageAndExit ();
107 int i = 0;
108 while (i < args.Length) {
109 switch (args [i]) {
110 case "--list-sections":
111 Conf.Load ();
112 ListSectionsAndExit ();
113 return;
115 case "--reload":
116 case "--beagled-reload-config":
117 ReloadConfigAndExit ();
118 return;
120 case "--help":
121 case "--usage":
122 PrintUsageAndExit ();
123 return;
125 default:
126 break;
128 ++i;
131 Conf.Load ();
133 string sectionname = args [0];
135 if (! Conf.Sections.ContainsKey (sectionname)) {
136 Console.Error.WriteLine ("ERROR: Invalid section name '{0}'", sectionname);
137 Environment.Exit (-1);
140 Conf.Section section = (Conf.Section) Conf.Sections [sectionname];
141 Hashtable options = Conf.GetOptions (section);
143 // No option specified?
144 if (args.Length == 1)
145 ListSectionOptionsAndExit (sectionname, options);
147 string optionname = args [1];
148 if (! options.ContainsKey (optionname)) {
149 if (sectionname == "daemon" && optionname == "ListBackends") {
150 ListBackends ();
151 Environment.Exit (0);
152 } else {
153 Console.Error.WriteLine ("ERROR: Invalid option name '{0}'", optionname);
154 Environment.Exit (-2);
159 // Invoke the method the user has chosen
162 // Pack the remaining command line params into an array used for
163 // params of the method.
164 string [] optionparams = new string [args.Length - 2];
165 int j, k;
166 for (j = 0, k = 2; k < args.Length; j++, k++)
167 optionparams [j] = args [k];
169 // Invoke the method
170 string output = null;
171 bool result = false;
173 try {
174 result = Conf.InvokeOption (section, optionname, optionparams, out output);
175 } catch (Exception e) {
176 Console.Error.WriteLine("ERROR: Command execution failed - caught exception.");
177 Console.Error.WriteLine(e.Message);
178 Environment.Exit (-3);
181 // Read the result and show the output
182 if (result == true)
183 Console.WriteLine (output);
184 else {
185 Console.Error.WriteLine ("ERROR: Command execution failed.");
186 Console.Error.WriteLine (output);
187 Environment.Exit (-4);
190 Conf.Save ();
191 Environment.Exit (0);
195 private static void ListBackends ()
197 ArrayList backends = new ArrayList ();
199 ArrayList assemblies = ReflectionFu.ScanEnvironmentForAssemblies ("BEAGLE_BACKEND_PATH", PathFinder.BackendDir);
201 // Add BeagleDaemonLib if it hasn't already been added.
202 bool found_daemon_lib = false;
203 foreach (Assembly assembly in assemblies) {
204 if (assembly.GetName ().Name == "BeagleDaemonLib") {
205 found_daemon_lib = true;
206 break;
210 if (!found_daemon_lib) {
211 try {
212 assemblies.Add (Assembly.LoadFrom (Path.Combine (PathFinder.PkgLibDir, "BeagleDaemonLib.dll")));
213 } catch (FileNotFoundException) {
214 Console.WriteLine ("WARNING: Could not find backend list.");
215 Environment.Exit (1);
219 foreach (Assembly assembly in assemblies) {
220 foreach (Type type in ReflectionFu.ScanAssemblyForInterface (assembly, typeof (Beagle.Daemon.IQueryable))) {
221 foreach (Beagle.Daemon.QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute (type, typeof (Beagle.Daemon.QueryableFlavor)))
222 backends.Add (flavor.Name);
226 if ( Directory.Exists (PathFinder.SystemIndexesDir)) {
227 foreach (DirectoryInfo index_dir in new DirectoryInfo (PathFinder.SystemIndexesDir).GetDirectories ())
228 backends.Add (index_dir.Name);
231 bool found_any = false;
233 Console.WriteLine ("Allowed backends:");
234 foreach (string name in backends) {
235 if (Conf.Daemon.DeniedBackends.Contains (name))
236 continue;
237 Console.WriteLine (" - {0}", name);
238 found_any = true;
241 if (! found_any)
242 Console.WriteLine (" (none)");
244 Console.WriteLine ();
246 found_any = false;
248 Console.WriteLine ("Denied backends:");
249 foreach (string name in Conf.Daemon.DeniedBackends) {
250 Console.WriteLine (" - {0}", name);
251 found_any = true;
254 if (! found_any)
255 Console.WriteLine (" (none)");