forked from cewee/tracker-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
170 lines (141 loc) · 5.57 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Tracker Search Provider for Gnome Shell
*
* Copyright (c) 2012 Christian Weber, Felix Schultze
*
* This programm is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Version 1.4
*
* https://github.com/cewee/tracker-search
*/
const Main = imports.ui.main;
const Search = imports.ui.search;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const Shell = imports.gi.Shell;
const Util = imports.misc.util;
const Tracker = imports.gi.Tracker;
const St = imports.gi.St;
/* let xdg-open pick the appropriate program to open/execute the file */
const DEFAULT_EXEC = 'xdg-open';
/* Limit search results, since number of displayed items is limited */
const MAX_RESULTS = 12;
var trackerSearchProvider = null;
function TrackerSearchProvider() {
this._init();
}
TrackerSearchProvider.prototype = {
__proto__ : Search.SearchProvider.prototype,
_init : function(name) {
Search.SearchProvider.prototype._init.call(this, "TRACKER SEARCH");
},
getResultMetas: function(resultIds) {
let metas = [];
for (let i = 0; i < resultIds.length; i++) {
metas.push(this.getResultMeta(resultIds[i]));
}
return metas;
},
getResultMeta : function(resultId) {
let type = resultId.contentType;
let name = resultId.filename;
return {
'id' : resultId,
'name' : name,
'createIcon' : function(size) {
let icon = Gio.app_info_get_default_for_type(type,null).get_icon();
return imports.gi.St.TextureCache.get_default().load_gicon(null, icon, size);
}
};
},
activateResult : function(id) {
// Action executed when clicked on result
var target = id.fileAndPath;
Util.spawn([ DEFAULT_EXEC, target ]);
},
getInitialResultSet : function(terms) { // terms holds array of search items
// check if 1st search term is >2 letters else drop the request
if(terms[0].length <= 2) {
return [];
}
let results = [];
/* Build sparql query */
var new_query = "SELECT nie:url(?f) WHERE {?f fts:match'";
for ( var i = 0; i < terms.length; i++) {
new_query = new_query + " " + terms[i] + "*";
}
new_query = new_query + "' } ORDER BY DESC (fts:rank(?f)) LIMIT " + String(MAX_RESULTS);
let conn = Tracker.SparqlConnection.get(null);
let cursor = conn.query(new_query, null);
try {
while (cursor != null && cursor.next(null)) {
var result = cursor.get_string (null);
// filter our, bogus tracker responses
if (String(result) == ",0") {
continue;
}
// cut of number of internal hits
var fileStr = String(result).split(',');
fileStr = decodeURI(fileStr[0]);
// Extract filename from line
var splitted = String(fileStr).split('/');
var filename = decodeURI(splitted[splitted.length - 1]);
let ft = String(filename).split('.');
// extract path and filename
splitted = String(fileStr).split('ile://');
var fileAndPath = "";
if (splitted.length == 2) {
fileAndPath = decodeURI(splitted[1]);
}
// if file does not exist, it won't be shown
if(!Gio.file_new_for_path(fileAndPath).query_exists(null))
{
continue;
}
// contentType is an array, the index "1" set true,
// if function is uncertain if type is the right one
let contentType = Gio.content_type_guess(fileAndPath, null);
var newContentType = contentType[0];
if(contentType[1]){
if(newContentType == "application/octet-stream") {
let fileInfo = Gio.file_new_for_path(fileAndPath).query_info('standard::type', 0, null);
// for some reason 'content_type_guess' returns a wrong mime type for folders
if(fileInfo.get_file_type() == Gio.FileType.DIRECTORY)
{
newContentType = "inode/directory";
} else {
// unrecognized mime-types are set to text, so that later an icon can be picked
newContentType = "text/x-log";
}
}
}
results.push({
'filename' : filename,
'fileAndPath' : fileAndPath,
'contentType' : newContentType
});
}
} catch (error) {
global.log("cursor " + error.message); //
return [];
}
return (results.length > 0) ? results : [];
},
getSubsearchResultSet : function(previousResults, terms) {
return this.getInitialResultSet(terms);
},
};
function init(meta) {
}
function enable() {
trackerSearchProvider = new TrackerSearchProvider();
Main.overview.addSearchProvider(trackerSearchProvider);
}
function disable() {
Main.overview.removeSearchProvider(trackerSearchProvider);
trackerSearchProvider = null;
}