~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to search/Beagle.Search/TypeFilter.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections;
 
3
using System.Text.RegularExpressions;
 
4
 
 
5
using Mono.Unix;
 
6
 
 
7
using Beagle.Search.Tiles;
 
8
 
 
9
// FIXME: it would be nicer to create QueryParts to do the filtering beagled-side
 
10
 
 
11
// FIXME: Whoa! This fucking thing is a complete ghetto. Needs a rewrite.
 
12
 
 
13
namespace Beagle.Search {
 
14
 
 
15
        public class TypeFilter {
 
16
 
 
17
                static Hashtable aliases;
 
18
                static Regex parser;
 
19
 
 
20
                delegate bool CustomFilter (Tile tile, string query);
 
21
 
 
22
                static void AddAliases (string aliasesString, object value)
 
23
                {
 
24
                        foreach (string alias in aliasesString.Split ('\n'))
 
25
                                aliases[alias.ToLower ()] = value;
 
26
                }
 
27
 
 
28
                static TypeFilter () {
 
29
                        parser = new Regex ("^(.*\\s)?(?:type:|kind:)(\\w+|\"[^\"]+\"|'[^\']+')(\\s.*)?$");
 
30
 
 
31
                        aliases = new Hashtable ();
 
32
 
 
33
                        /* Translators: the strings in TypeFilter.cs are for when the user
 
34
                         * wants to limit search results to a specific type. You don't need
 
35
                         * to give an exact translation of the exact same set of English
 
36
                         * words. Just provide a list of likely words the user might use
 
37
                         * after the "type:" keyword to refer to each type of match.
 
38
                         */
 
39
                        AddAliases (Catalog.GetString ("file\nfiles"), typeof (Tiles.TileFile));
 
40
                        AddAliases (Catalog.GetString ("mail\nemail\ne-mail"), typeof (Tiles.MailMessage));
 
41
                        AddAliases (Catalog.GetString ("im\nims\ninstant message\ninstant messages\ninstant-message\ninstant-messages\nchat\nchats"), typeof (Tiles.IMLog));
 
42
                        AddAliases (Catalog.GetString ("presentation\npresentations\nslideshow\nslideshows\nslide\nslides"), typeof (Tiles.Presentation));
 
43
 
 
44
                        AddAliases (Catalog.GetString ("application\napplications\napp\napps"), TileGroup.Application);
 
45
                        AddAliases (Catalog.GetString ("contact\ncontacts\nvcard\nvcards"), TileGroup.Contact);
 
46
                        AddAliases (Catalog.GetString ("folder\nfolders"), TileGroup.Folder);
 
47
                        AddAliases (Catalog.GetString ("image\nimages\nimg"), TileGroup.Image);
 
48
                        AddAliases (Catalog.GetString ("audio"), TileGroup.Audio);
 
49
                        AddAliases (Catalog.GetString ("video"), TileGroup.Video);
 
50
                        AddAliases (Catalog.GetString ("media"), new CustomFilter (MediaFilter));
 
51
                        AddAliases (Catalog.GetString ("document\ndocuments\noffice document\noffice documents"), TileGroup.Documents);
 
52
                        AddAliases (Catalog.GetString ("conversation\nconversations"), TileGroup.Conversations);
 
53
                        AddAliases (Catalog.GetString ("web\nwww\nwebsite\nwebsites"), TileGroup.Website);
 
54
                        AddAliases (Catalog.GetString ("feed\nnews\nblog\nrss"), TileGroup.Feed);
 
55
                        AddAliases (Catalog.GetString ("archive\narchives"), TileGroup.Archive);
 
56
                        AddAliases (Catalog.GetString ("person\npeople"), new CustomFilter (PersonFilter));
 
57
 
 
58
 
 
59
                        AddAliases ("avi", "video/x-msvideo");
 
60
                        AddAliases ("bz", "application/x-bzip");
 
61
                        AddAliases ("bz2", "application/x-bzip");
 
62
                        AddAliases ("bzip", "application/x-bzip");
 
63
                        AddAliases ("bzip2", "application/x-bzip");
 
64
                        AddAliases ("csv", "text/x-comma-separated-values");
 
65
                        AddAliases ("deb", "application/x-deb");
 
66
                        AddAliases ("doc", "application/msword");
 
67
                        AddAliases ("eps", "image/x-eps");
 
68
                        AddAliases ("excel", "application/vnd.ms-excel");
 
69
                        AddAliases ("gif", "image/gif");
 
70
                        AddAliases ("gimp", "image/x-xcf");
 
71
                        AddAliases ("gz", "application/x-gzip");
 
72
                        AddAliases ("gzip", "application/x-gzip");
 
73
                        AddAliases ("html", "text/html");
 
74
                        AddAliases ("impress", "application/vnd.sun.xml.impress");
 
75
                        AddAliases ("jpeg", "image/jpeg");
 
76
                        AddAliases ("jpg", "image/jpeg");
 
77
                        AddAliases ("mp3", "audio/mpeg");
 
78
                        AddAliases ("mpeg", "video/mpeg");
 
79
                        AddAliases ("msword", "application/msword");
 
80
                        AddAliases ("ods", "application/vnd.oasis.opendocument.spreadsheet");
 
81
                        AddAliases ("odt", "application/vnd.oasis.opendocument.text");
 
82
                        AddAliases ("ogg", "application/ogg"); // FIXME when this changes
 
83
                        AddAliases ("ots", "application/vnd.oasis.opendocument.spreadsheet-template");
 
84
                        AddAliases ("ott", "application/vnd.oasis.opendocument.text-template");
 
85
                        AddAliases ("pdf", "application/pdf");
 
86
                        AddAliases ("photoshop", "image/x-psd");
 
87
                        AddAliases ("png", "image/png");
 
88
                        AddAliases ("postscript", "application/postscript");
 
89
                        AddAliases ("powerpoint", "application/vnd.ms-powerpoint");
 
90
                        AddAliases ("ppt", "application/vnd.ms-powerpoint");
 
91
                        AddAliases ("ps", "application/postscript");
 
92
                        AddAliases ("psd", "image/x-psd");
 
93
                        AddAliases ("real", "audio/x-pn-realaudio");
 
94
                        AddAliases ("realaudio", "audio/x-pn-realaudio");
 
95
                        AddAliases ("rpm", "application/x-rpm");
 
96
                        AddAliases ("rtf", "application/rtf");
 
97
                        AddAliases ("sgi", "image/x-sgi");
 
98
                        AddAliases ("stc", "application/vnd.sun.xml.calc.template");
 
99
                        AddAliases ("stw", "application/vnd.sun.xml.writer.template");
 
100
                        AddAliases ("svg", "image/svg+xml");
 
101
                        AddAliases ("sxc", "application/vnd.sun.xml.calc");
 
102
                        AddAliases ("sxi", "application/vnd.sun.xml.impress");
 
103
                        AddAliases ("sxw", "application/vnd.sun.xml.writer");
 
104
                        AddAliases ("tar", "application/x-tar");
 
105
                        AddAliases ("tar.gz", "application/x-compressed-tar");
 
106
                        AddAliases ("text", "text/plain");
 
107
                        AddAliases ("tgz", "application/x-compressed-tar");
 
108
                        AddAliases ("theora", "application/ogg"); // FIXME when this changes
 
109
                        AddAliases ("tif", "image/tiff");
 
110
                        AddAliases ("tiff", "image/tiff");
 
111
                        AddAliases ("txt", "text/plain");
 
112
                        AddAliases ("vorbis", "application/ogg"); // FIXME when this changes
 
113
                        AddAliases ("wav", "audio/x-wav");
 
114
                        AddAliases ("word", "application/msword");
 
115
                        AddAliases ("xcf", "image/x-xcf");
 
116
                        AddAliases ("xls", "application/vnd.ms-excel");
 
117
                        AddAliases ("xlt", "application/vnd.ms-excel");
 
118
                        AddAliases ("xml", "text/xml");
 
119
                }
 
120
 
 
121
                static bool MediaFilter (Tile tile, string query)
 
122
                {
 
123
                        return tile.Group == TileGroup.Audio || tile.Group == TileGroup.Video;
 
124
                }
 
125
 
 
126
                // Check that each word in @query is a substring of at least one
 
127
                // of the values of @property in @hit
 
128
                static bool MatchWords (string query, Beagle.Hit hit, string property)
 
129
                {
 
130
                        string[] vals = hit.GetProperties (property);
 
131
                        if (vals == null) {
 
132
                                vals = hit.GetProperties ("parent:" + property);
 
133
                                if (vals == null)
 
134
                                        return false;
 
135
                        }
 
136
 
 
137
                        foreach (string word in query.ToLower().Split (' ')) {
 
138
                                bool matched = false;
 
139
 
 
140
                                foreach (string match in vals) {
 
141
                                        if (match.ToLower().IndexOf (word) != -1) {
 
142
                                                matched = true;
 
143
                                                break;
 
144
                                        }
 
145
                                }
 
146
                                if (!matched)
 
147
                                        return false;
 
148
                        }
 
149
                        return true;
 
150
                }
 
151
 
 
152
                static bool PersonFilter (Tile tile, string query)
 
153
                {
 
154
                        if (tile is Tiles.Contact)
 
155
                                return true;
 
156
                        else if (tile is Tiles.MailMessage) {
 
157
                                if (MatchWords (query, tile.Hit, "fixme:from"))
 
158
                                        return true;
 
159
                                if (MatchWords (query, tile.Hit, "fixme:to"))
 
160
                                        return true;
 
161
                                if (MatchWords (query, tile.Hit, "fixme:cc"))
 
162
                                        return true;
 
163
                        } else if (tile is Tiles.IMLog) {
 
164
                                if (MatchWords (query, tile.Hit, "fixme:speakingto"))
 
165
                                        return true;
 
166
                                if (MatchWords (query, tile.Hit, "fixme:speakingto_alias"))
 
167
                                        return true;
 
168
                        }
 
169
                        return false;
 
170
                }
 
171
 
 
172
                public static TypeFilter MakeFilter (ref string query)
 
173
                {
 
174
                        Match match = parser.Match (query);
 
175
                        if (!match.Success)
 
176
                                return null;
 
177
 
 
178
                        query = match.Groups[1].Value + match.Groups[3].Value;
 
179
                        try {
 
180
                                return new TypeFilter (match.Groups[2].Value, query);
 
181
                        } catch {
 
182
                                // FIXME
 
183
                                Console.Error.WriteLine ("Ignoring unrecognized type alias {0}", match.Groups[2].Value);
 
184
                                return null;
 
185
                        }
 
186
                }
 
187
 
 
188
                object filter;
 
189
                string query;
 
190
 
 
191
                public TypeFilter (string type, string query)
 
192
                {
 
193
                        filter = aliases[type.ToLower ()];
 
194
                        if (filter == null)
 
195
                                throw new ArgumentException ("Unrecognized type alias");
 
196
                        this.query = query;
 
197
                }
 
198
 
 
199
                public bool Filter (Tile tile)
 
200
                {
 
201
                        if (filter is Type) {
 
202
                                return (tile.GetType () == (Type)filter ||
 
203
                                        tile.GetType ().IsSubclassOf ((Type)filter));
 
204
                        } else if (filter is TileGroup) {
 
205
                                return tile.Group == (TileGroup)filter;
 
206
                        } else if (filter is string) {
 
207
                                return tile.Hit.MimeType == (string)filter;
 
208
                        } else if (filter is CustomFilter) {
 
209
                                return ((CustomFilter)filter) (tile, query);
 
210
                        } else
 
211
                                return true;
 
212
                }
 
213
        }
 
214
}