~c-franke/do/XesamSearch

« back to all changes in this revision

Viewing changes to DoXesamDriver.cs

  • Committer: Christof Franke
  • Date: 2008-02-26 10:55:05 UTC
  • Revision ID: kraut@workstation-20080226105505-8am13vug28ok1wsf
version 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// DoXesamDriver.cs 
 
2
// User: kraut (c.franke@muenchen-mail.de) at 22:23¬†21.02.2008
 
3
 
 
4
using System;
 
5
using System.Diagnostics;
 
6
using System.Collections.Generic;
 
7
 
 
8
using NDesk.DBus;
 
9
using org.freedesktop.DBus;
 
10
 
 
11
namespace Do.Addins.DoXesam
 
12
{               
 
13
        public class DoXesamDriver
 
14
        {
 
15
                public static bool BInterface=false; //if there are beagle field its true       
 
16
           
 
17
             // Xesam Search Object Interface
 
18
                [Interface ("org.freedesktop.xesam.Search")]
 
19
                public interface ISearcher
 
20
                {
 
21
                        string NewSession ();
 
22
                        void CloseSession (string session);
 
23
                        string NewSearch (string ses, string query);
 
24
                        void StartSearch (string search);
 
25
                        void CloseSearch (string s);
 
26
                        uint GetHitCount (string s);
 
27
                        object[][] GetHitData (string s, uint[] ids, string[] fields);
 
28
                        object[][] GetHits (string search, uint count);
 
29
                        object GetProperty (string s, string prop);
 
30
                        object SetProperty (string s, string prop, object val);
 
31
                        string[] GetState ();
 
32
                        
 
33
                }
 
34
        
 
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
                {
 
43
                        //### Field Properties
 
44
                        // Ive added all Porperties that could be important for Beagle
 
45
                        // Some of them might be senseless
 
46
                        // I think i config file would be a good idea :)
 
47
                        
 
48
                        List<string> properties = new List<string>();
 
49
                        //##Beagle Only
 
50
                        //IMLog
 
51
                        properties.Add("beagle:speakingto");//aim nr or irc channel
 
52
                        properties.Add("beagle:speakingto_alias");//chat partner alis   
 
53
                        properties.Add("beagle:protocol");  //chat protcol: icq, aim,.....
 
54
                        properties.Add("beagle:client");
 
55
                        properties.Add("beagle:speakingto_icon");
 
56
                        //Email
 
57
                        properties.Add("beagle:stock_mail-replied");
 
58
                        properties.Add("beagle:stock_mail-open");
 
59
                        properties.Add("beagle:stock_mail");
 
60
                        properties.Add("beagle:from_name");
 
61
                        properties.Add("beagle:to_name");
 
62
                        properties.Add("beagle:isSent");
 
63
                        properties.Add("beagle:isAnswered");
 
64
                        //properties.Add("beagle:folder");  
 
65
                        properties.Add("beagle:hasAttachments");
 
66
                        properties.Add("beagle:attachment_title");
 
67
                        properties.Add("beagle:parent:title");
 
68
                        //properties.Add("emblem"); 
 
69
                        
 
70
                        //##Xesam no IMLog support
 
71
                        properties.Add("xesam:title");
 
72
                        properties.Add("xesam:author");
 
73
                        properties.Add("xesam:creator");
 
74
                        properties.Add("xesam:width");
 
75
                        properties.Add("xesam:hight");
 
76
                        properties.Add("xesam:pageCount");
 
77
                        properties.Add("xesam:mimeType");
 
78
                        properties.Add("xesam:url");
 
79
                        properties.Add("xesam:fileExtension");
 
80
                        properties.Add("xesam:from");
 
81
                        properties.Add("xesam:to");
 
82
                        properties.Add("xesam:subject");
 
83
                        properties.Add("xesam:isRead");
 
84
                        properties.Add("xesam:artist");
 
85
                        properties.Add("xesam:ExactFilename");
 
86
                        properties.Add("xesam:videoCodec");
 
87
                        properties.Add("xesam:sourceModified");
 
88
                        
 
89
                        try{
 
90
                                
 
91
                                ISearcher beagle = GetSearchObject(); //establish Connection to Beagle/Xesan
 
92
                                string session= beagle.NewSession();  //new search session
 
93
                                beagle.SetProperty(session, "hit.fields",  properties.ToArray() );
 
94
                                //Send Query
 
95
                                string search =beagle.NewSearch(session,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
 
96
                                "<request xmlns=\"http://freedesktop.org/standards/xesam/1.0/query\"><userQuery>"+searchstr+"</userQuery></request>");
 
97
                                
 
98
                                beagle.StartSearch(search); //Start Search
 
99
                                object[][] result=beagle.GetHits(search,count); //get results
 
100
                                List<Dictionary<string,string>> LResults=new List<Dictionary<string,string>>();
 
101
                                int BFieldCount=0;
 
102
                                foreach(object[] entry in result)
 
103
                                {
 
104
                                        Dictionary<string,string> DHitFields = new Dictionary<string,string>();
 
105
                                        int i =0;
 
106
                                        foreach(object field in entry)
 
107
                                        {
 
108
                                                DHitFields.Add(properties[i], (string)field);
 
109
                                                
 
110
                                                if(properties[i].StartsWith("beagle")&(string)field !="")
 
111
                                                        BFieldCount++;
 
112
                                                i++;
 
113
                                        }
 
114
                                        LResults.Add(DHitFields);
 
115
                                }
 
116
                                beagle.CloseSession(search); //close search session
 
117
                                if(BFieldCount>0)
 
118
                                        DoXesamDriver.BInterface=true;
 
119
                                        
 
120
                                return LResults;
 
121
 
 
122
                        }catch (Exception e){
 
123
                                Console.WriteLine("ctor->Exception {0}", e);
 
124
                                return null;
 
125
                        }
 
126
                }
 
127
                
 
128
                /**
 
129
                /* New Xesam/Beagle search object.
 
130
                /* returns: searchobj ISearcher - Xesam Search Object.
 
131
                 **/
 
132
                public static ISearcher GetSearchObject()
 
133
                {
 
134
                        const string ObjectPath = "/org/freedesktop/xesam/searcher/main";
 
135
                        const string ServiceBusName = "org.freedesktop.xesam.searcher";
 
136
                        try {
 
137
                                return Bus.Session.GetObject<ISearcher>(ServiceBusName, new ObjectPath (ObjectPath));
 
138
                        } catch {
 
139
                                return null;
 
140
                        }
 
141
                }
 
142
                
 
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
                
 
185
                public static void Main(String[] args){
 
186
                }
 
187
                
 
188
        }
 
189
}