1
/* LocateFilesCommand.cs
3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this
7
* This program is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
using System.Collections.Generic;
27
public class LocateFilesCommand : AbstractCommand
30
bool allowHidden = false;
31
uint maxResults = 1000;
33
public override string Name
35
get { return "Locate Files"; }
38
public override string Description
40
get { return "Search your filesystem using locate."; }
43
public override string Icon
45
get { return "search"; }
48
public override Type[] SupportedItemTypes
57
public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
60
System.Diagnostics.Process locate;
63
files = new List<IItem> ();
64
query = (items[0] as ITextItem).Text;
66
locate = new System.Diagnostics.Process ();
67
locate.StartInfo.FileName = "locate";
68
locate.StartInfo.Arguments = string.Format ("-i -n {0} {1}", maxResults, query);
69
locate.StartInfo.RedirectStandardOutput = true;
70
locate.StartInfo.UseShellExecute = false;
74
Console.Error.WriteLine ("LocateFilesCommand error: The program 'locate' could not be found.");
79
query = query.ToLower ();
80
while (null != (path = locate.StandardOutput.ReadLine ())) {
81
// Disallow hidden directories in the absolute path.
82
// This gets rid of messy .svn directories and their contents.
84
Path.GetDirectoryName (path).Contains ("/."))
87
// Only allow files that contain the query as a substring
88
// of the file name. It may be faster to use grep, but I've
89
// tested this and it seems prety snappy.
90
if (Path.GetFileName (path).ToLower().Contains (query))
91
files.Add (FileItem.Create (path));
93
files.Sort (new FileItemNameComparer (query));
94
return files.ToArray ();
97
// Order files by (A) position of query in the file name and
98
// (B) by name length.
99
private class FileItemNameComparer : IComparer<IItem>
103
public FileItemNameComparer (string query)
105
this.query = query.ToLower ();
108
public int Compare (IItem a, IItem b)
110
string a_name_lower, b_name_lower;
111
int a_score, b_score;
113
a_name_lower = (a as FileItem).Path;
114
a_name_lower = Path.GetFileName (a_name_lower).ToLower ();
115
b_name_lower = (b as FileItem).Path;
116
b_name_lower = Path.GetFileName (b_name_lower).ToLower ();
118
a_score = a_name_lower.IndexOf (query);
119
b_score = b_name_lower.IndexOf (query);
121
if (a_score == b_score)
122
return a_name_lower.Length - b_name_lower.Length;
124
return a_score - b_score;