~c-franke/do/XesamSearch

« back to all changes in this revision

Viewing changes to DoXesamDriver.cs

  • Committer: Christof Franke
  • Date: 2008-03-03 16:36:15 UTC
  • Revision ID: kraut@workstation-20080303163615-eyutlmi093o42xwn
* GNU General Public License added.
* Error Massage if Desktop Searchengine is not running.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// DoXesamDriver.cs 
2
2
// User: kraut (c.franke@muenchen-mail.de) at 22:23 21.02.2008
 
3
//
 
4
//  GNOME Do is the legal property of its developers, whose names are too numerous
 
5
//  to list here.  Please refer to the COPYRIGHT file distributed with this
 
6
//  source distribution.
 
7
//
 
8
//  This program is free software: you can redistribute it and/or modify
 
9
//  it under the terms of the GNU General Public License as published by
 
10
//  the Free Software Foundation, either version 3 of the License, or
 
11
//  (at your option) any later version.
 
12
//
 
13
//  This program is distributed in the hope that it will be useful,
 
14
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
//  GNU General Public License for more details.
 
17
//
 
18
//  You should have received a copy of the GNU General Public License
 
19
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
3
20
 
4
21
using System;
5
22
using System.Diagnostics;
6
23
using System.Collections.Generic;
7
 
 
 
24
using System.Text.RegularExpressions;
8
25
using NDesk.DBus;
9
26
using org.freedesktop.DBus;
10
 
 
 
27
//using GLib;
11
28
namespace Do.Addins.DoXesam
12
29
{               
 
30
//public delegate void DaemonStarted ();
13
31
        public class DoXesamDriver
14
32
        {
15
33
                public static bool BInterface=false; //if there are beagle field its true       
16
 
           
 
34
                //public static event DaemonStarted DaemonStarted;
17
35
             // Xesam Search Object Interface
18
36
                [Interface ("org.freedesktop.xesam.Search")]
19
37
                public interface ISearcher
32
50
                        
33
51
                }
34
52
        
35
 
                
36
 
                /**
37
 
                /* Makes a simple Search (userQuery).
38
 
                /* param: searchstr - string searchstring / searchkey.
39
 
                /* returns: result Dictionary<string,string> - Search results / Hits. Key is the field.
40
 
                 **/
41
 
                public static List<Dictionary<string,string>> GetSearchResults(string searchstr, uint count)
42
 
                {
 
53
                private static List<string> getProperties(){
43
54
                        //### Field Properties
44
 
                        // Ive added all Porperties that could be important for Beagle
 
55
                        // Ive added many Porperties that could be important for Beagle.
45
56
                        // Some of them might be senseless
 
57
                        // BUt some properties are missing like Calendar fields.
46
58
                        // I think i config file would be a good idea :)
 
59
                        //bool test = InstanceIsRunning;
47
60
                        
48
61
                        List<string> properties = new List<string>();
49
62
                        //##Beagle Only
65
78
                        properties.Add("beagle:hasAttachments");
66
79
                        properties.Add("beagle:attachment_title");
67
80
                        properties.Add("beagle:parent:title");
68
 
                        //properties.Add("emblem"); 
 
81
                         
69
82
                        
70
83
                        //##Xesam no IMLog support
71
84
                        properties.Add("xesam:title");
85
98
                        properties.Add("xesam:ExactFilename");
86
99
                        properties.Add("xesam:videoCodec");
87
100
                        properties.Add("xesam:sourceModified");
 
101
                        return properties;
 
102
                }
 
103
                /**
 
104
                /* Makes a simple Search (userQuery).
 
105
                /* param: searchstr - string searchstring / searchkey.
 
106
                /* returns: result Dictionary<string,string> - Search results / Hits. Key is the field.
 
107
                 **/
 
108
                public static List<Dictionary<string,string>> GetSearchResults(string searchstr, uint count)
 
109
                {
88
110
                        
 
111
                        List<Dictionary<string,string>> LResults=new List<Dictionary<string,string>>();
89
112
                        try{
90
 
                                
 
113
                                List<string> properties = DoXesamDriver.getProperties();
91
114
                                ISearcher beagle = GetSearchObject(); //establish Connection to Beagle/Xesan
 
115
                                if(beagle!=null){                               
92
116
                                string session= beagle.NewSession();  //new search session
93
117
                                beagle.SetProperty(session, "hit.fields",  properties.ToArray() );
94
118
                                //Send Query
97
121
                                
98
122
                                beagle.StartSearch(search); //Start Search
99
123
                                object[][] result=beagle.GetHits(search,count); //get results
100
 
                                List<Dictionary<string,string>> LResults=new List<Dictionary<string,string>>();
 
124
                                
101
125
                                int BFieldCount=0;
102
126
                                foreach(object[] entry in result)
103
127
                                {
116
140
                                beagle.CloseSession(search); //close search session
117
141
                                if(BFieldCount>0)
118
142
                                        DoXesamDriver.BInterface=true;
119
 
                                        
 
143
                                
 
144
                                }else{
 
145
                                        Dictionary<string,string> DHitFields = new Dictionary<string,string>();
 
146
                                        DHitFields.Add("error", "Please start Desktop Search Engine first!");
 
147
                                        LResults.Add(DHitFields);
 
148
                                }       
120
149
                                return LResults;
121
150
 
122
151
                        }catch (Exception e){
 
152
                                Dictionary<string,string> DHitFields = new Dictionary<string,string>();
 
153
                                        DHitFields.Add("error", "Please start Desktop Search Engine first!");
 
154
                                        LResults.Add(DHitFields);
123
155
                                Console.WriteLine("ctor->Exception {0}", e);
124
 
                                return null;
 
156
                                return LResults;
125
157
                        }
126
158
                }
127
159
                
129
161
                /* New Xesam/Beagle search object.
130
162
                /* returns: searchobj ISearcher - Xesam Search Object.
131
163
                 **/
132
 
                public static ISearcher GetSearchObject()
 
164
                private static ISearcher GetSearchObject()
133
165
                {
134
166
                        const string ObjectPath = "/org/freedesktop/xesam/searcher/main";
135
167
                        const string ServiceBusName = "org.freedesktop.xesam.searcher";
140
172
                        }
141
173
                }
142
174
                
143
 
                /**
144
 
                /* Gnome-Do BeagleSearch Plugin requires that XesamAdaptor is running.
145
 
                /* If Not following methods should start it.
146
 
                 * FIX ME: Doesent work, its hard to identifiy 
147
 
                 *          mono --dedub=BaegleXesam.exe
148
 
                 * Solution: using cosole tools like ps ux:
149
 
                 *            1. Process.Start(ps ux)
150
 
                 *            2.   Regex() XesamBeagle.exe
151
 
                 *            3. Split(" ")  second element has pid 
152
 
                 **/
153
 
                public static bool InstanceIsRunning
154
 
                {
155
 
                        get {
156
 
                                Process pidof;
157
 
                                
158
 
                                try {
159
 
                                        //FIX ME: IT doesnt work, no own prozess name
160
 
                                        // for beagle-xesam-adaptor. pid name = mono --debug /usr/local/lib/beagle-xesam/XesamAdaptor.exe
161
 
                                        
162
 
                                        //**strange
163
 
                                        // Use pidof command to look for pidgin process. Exit
164
 
                                        // status is 0 if at least one matching process is found.
165
 
                                        // If there's any error, just assume some Purple client
166
 
                                        // is running.
167
 
                                        pidof = Process.Start ("pidof", "XesamAdaptor.exe"); //beagle-xesam-adaptor
168
 
                                        pidof.WaitForExit ();   
169
 
                                        return pidof.ExitCode == 0;
170
 
                                } catch {
171
 
                                        return true;
172
 
                                }
173
 
                        }
174
 
                }
175
 
 
176
 
                public static void StartIfNeccessary ()
177
 
                {
178
 
                        //FIX ME: bragle-xesa-adaptor is not starting.                  
179
 
                        if (!InstanceIsRunning) {
180
 
                                Process.Start ("beagle-xesam-adaptor"); //Funktioniert nicht
181
 
                                System.Threading.Thread.Sleep (4 * 1000);
182
 
                        }
183
 
                }
184
175
                
185
176
                public static void Main(String[] args){
186
177
                }