Remove debug statements from KCal backends.
[beagle.git] / tools / Info.cs
blob190e39908b6b049373101c1051f854013335df74
1 //
2 // Info.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 // Copyright (C) 2006 Debajyoti Bera <dbera.web@gmail.com>
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
28 using System;
29 using System.IO;
30 using System.Text;
31 using System.Reflection;
32 using System.Collections;
34 using Beagle;
35 using Beagle.Daemon;
36 using Beagle.Util;
38 class InfoTool {
40 public static void PrintUsageAndExit ()
42 string usage =
43 "beagle-info: Statistics from the Beagle daemon.\n" +
44 "Web page: http://www.gnome.org/projects/beagle\n" +
45 "Copyright (C) 2004-2006 Novell, Inc.\n\n";
46 usage +=
47 "Usage: beagle-info <OPTIONS>\n\n" +
48 "Options:\n" +
49 " --daemon-version\t\tPrint the version of the running daemon.\n" +
50 " --status\t\t\tDisplay status of the running daemon.\n" +
51 " --index-info\t\t\tDisplay statistics of the Beagle indexes.\n" +
52 " --is-indexing\t\t\tDisplay whether the indexer is currently active.\n" +
53 " --all-info\t\t\tAll of the above information.\n" +
54 " --list-backends\t\tList the currently available backends.\n" +
55 " --list-filters\t\tList the currently available filters.\n" +
56 " --list-static-indexes\t\tList the available static indexes.\n" +
57 " --help\t\t\tPrint this usage message.\n";
59 Console.WriteLine (usage);
61 System.Environment.Exit (0);
64 static int Main (string[] args)
66 if (args.Length == 0 || Array.IndexOf (args, "--help") > -1)
67 PrintUsageAndExit ();
69 if (Array.IndexOf (args, "--list-filters") > -1)
70 PrintFilterInformation ();
71 else if (Array.IndexOf (args, "--list-backends") > -1)
72 PrintBackendInformation ();
73 else if (Array.IndexOf (args, "--list-static-indexes") > -1)
74 PrintStaticIndexInformation ();
75 else
76 return PrintDaemonInformation (args);
78 return 0;
81 private static int PrintDaemonInformation (string[] args)
83 DaemonInformationRequest request = new DaemonInformationRequest ();
84 DaemonInformationResponse response;
86 bool get_version = false;
87 bool get_sched_info = false;
88 bool get_index_status = false;
89 bool get_is_indexing = false;
91 get_version = (Array.IndexOf (args, "--daemon-version") > -1);
92 get_sched_info = (Array.IndexOf (args, "--status") > -1);
93 get_index_status = (Array.IndexOf (args, "--index-info") > -1);
94 get_is_indexing = (Array.IndexOf (args, "--is-indexing") > -1);
96 if (Array.IndexOf (args, "--all-info") > -1)
97 get_version = get_sched_info = get_index_status = get_is_indexing = true;
99 try {
100 response = (DaemonInformationResponse) request.Send ();
101 } catch (Beagle.ResponseMessageException) {
102 Console.WriteLine ("Could not connect to the daemon.");
103 return 1;
106 if (get_version)
107 Console.WriteLine ("Daemon version: {0}", response.Version);
109 if (get_sched_info)
110 Console.Write (response.HumanReadableStatus);
112 if (get_index_status) {
113 Console.WriteLine ("Index information:");
114 Console.WriteLine (response.IndexInformation);
117 if (get_is_indexing)
118 Console.WriteLine ("Daemon indexing: {0}", response.IsIndexing);
120 return 0;
123 private static void PrintFilterInformation ()
125 ReflectionFu.ScanEnvironmentForAssemblies ("BEAGLE_FILTER_PATH", PathFinder.FilterDir, PrintFilterDetails);
128 static void PrintFilterDetails (Assembly assembly)
130 StringBuilder sb = new StringBuilder ();
131 foreach (Type t in ReflectionFu.GetTypesFromAssemblyAttribute (assembly, typeof (FilterTypesAttribute))) {
132 Filter filter = null;
134 try {
135 filter = (Filter) Activator.CreateInstance (t);
136 } catch (Exception ex) {
137 Logger.Log.Error (ex, "Caught exception while instantiating {0}", t);
140 if (filter == null)
141 continue;
143 string name;
145 if (t.FullName.StartsWith (t.Namespace))
146 name = t.FullName.Substring (t.Namespace.Length + 1);
147 else
148 name = t.FullName;
150 sb.Length = 0;
151 sb.Append (name + " - Version " + filter.Version + " (" + assembly.Location + ")\n");
152 bool has_filter = false;
154 foreach (FilterFlavor flavor in filter.SupportedFlavors) {
155 if (flavor.MimeType != null && (! flavor.MimeType.StartsWith ("beagle"))) {
156 sb.Append (" - " + flavor.MimeType + "\n");
157 has_filter = true;
159 if (flavor.Extension != null) {
160 sb.Append (" - *" + flavor.Extension + "\n");
161 has_filter = true;
165 if (has_filter)
166 Console.WriteLine (sb.ToString ());
170 private static void PrintBackendInformation ()
172 ArrayList assemblies = ReflectionFu.ScanEnvironmentForAssemblies ("BEAGLE_BACKEND_PATH", PathFinder.BackendDir);
174 // Add BeagleDaemonLib if it hasn't already been added.
175 bool found_daemon_lib = false;
176 foreach (Assembly assembly in assemblies) {
177 if (assembly.GetName ().Name == "BeagleDaemonLib") {
178 found_daemon_lib = true;
179 break;
183 if (!found_daemon_lib) {
184 try {
185 assemblies.Add (Assembly.LoadFrom (Path.Combine (PathFinder.PkgLibDir, "BeagleDaemonLib.dll")));
186 } catch (FileNotFoundException) {
187 Console.WriteLine ("WARNING: Could not find backend list.");
188 Environment.Exit (1);
192 foreach (Assembly assembly in assemblies) {
193 foreach (Type type in ReflectionFu.GetTypesFromAssemblyAttribute (assembly, typeof (IQueryableTypesAttribute))) {
194 foreach (Beagle.Daemon.QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute (type, typeof (Beagle.Daemon.QueryableFlavor))) {
195 Console.WriteLine ("{0,-20} (" + assembly.Location + ")", flavor.Name);
201 private static void PrintStaticIndexInformation ()
203 try {
204 foreach (DirectoryInfo index_dir in new DirectoryInfo (PathFinder.SystemIndexesDir).GetDirectories ())
205 Console.WriteLine ("[System index] " + index_dir.Name + " (" + index_dir.FullName + ")");
206 } catch (DirectoryNotFoundException) { }
208 foreach (string index_path in Conf.Daemon.StaticQueryables)
209 Console.WriteLine ("[User index] " + index_path);