-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.js
229 lines (191 loc) · 6.79 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Copyright (C) 2012-2014 Marcus Habermehl <[email protected]>
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
// Gjs imports
const Gettext = imports.gettext;
const Lang = imports.lang;
// Internal imports
const Config = imports.misc.config;
const ExtensionUtils = imports.misc.extensionUtils;
const Main = imports.ui.main;
const _gettextDomain = Gettext.domain('searchbookmarks');
const _ = _gettextDomain.gettext;
const _thisExtension = ExtensionUtils.getCurrentExtension();
// Extension imports
const Bookmark = _thisExtension.imports.bookmark;
const Chrome = _thisExtension.imports.chrome;
const Epiphany = _thisExtension.imports.epiphany;
const Mozilla = _thisExtension.imports.mozilla;
const Midori = _thisExtension.imports.midori;
const Opera = _thisExtension.imports.opera;
// Variable to hold the extension instance
var _searchBookmarksInstance = null;
// Should we use the new search system in GS >= 3.11.2 or the old one
const versionArray = Config.PACKAGE_VERSION.split('.');
var useNewSearch;
if (versionArray[0] == 3 && versionArray[1] >= 11 && versionArray[2] >= 2) {
useNewSearch = true;
} else {
useNewSearch = false;
}
/**
* _resultSort:
* @a: Bookmark.Bookmark
* @b: Bookmark.Bookmark
*
* Sort the list of bookmarks in the following order.
*
* 1. descending by the score
* 2. ascending by the name
*/
function _resultSort(a, b) {
if (a.score < b.score) return 1;
if (a.score > b.score) return -1;
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
}
const SearchBookmarks = new Lang.Class({
Name: 'SearchBookmarks',
_init: function() {
this.id = '[email protected]';
// Required for GS < 3.11.2
this.searchSystem = null;
this.modules = [Chrome, Epiphany, Mozilla, Midori, Opera];
},
activateResult: function(id) {
id.activate();
},
createResultObject: function(metaInfo, terms) {
return new Bookmark.BookmarkIcon(metaInfo.id);
},
filterResults: function(results, maxResults) {
return results.slice(0, maxResults);
},
/**
* GS <= 3.11.1: getInitialResultSet(terms)
* GS >= 3.11.2: getInitialResultSet(terms, callback, cancellable)
*/
getInitialResultSet: function() {
let terms, callback, cancellable;
if (useNewSearch) {
[terms, callback, cancellable] = arguments;
} else {
[terms] = arguments;
}
let searchResults = [];
for (let i = 0; i < this.modules.length; i++) {
let bookmarks = this.modules[i].getBookmarks();
for (let j = 0; j < bookmarks.length; j++) {
bookmarks[j].rate(terms);
if (bookmarks[j].score > 0) {
searchResults.push(bookmarks[j]);
}
}
}
searchResults.sort(_resultSort);
if (useNewSearch) {
callback(searchResults);
} else {
this.searchSystem.setResults(this, searchResults);
}
},
/**
* GS <= 3.11.1: getSubsearchResultSet(previousResults, terms)
* GS >= 3.11.2: getSubsearchResultSet(previousResults, terms, callback,
* cancellable)
*/
getSubsearchResultSet: function() {
let previousResults, terms, callback, cancellable;
if (useNewSearch) {
[previousResults, terms, callback, cancellable] = arguments;
} else {
[previousResults, terms] = arguments;
}
let searchResults = [];
for (let i = 0; i < previousResults.length; i++) {
previousResults[i].rate(terms);
if (previousResults[i].score > 0) {
searchResults.push(previousResults[i]);
}
}
searchResults.sort(_resultSort);
if (useNewSearch) {
callback(searchResults);
} else {
this.searchSystem.setResults(this, searchResults);
}
},
getResultMetas: function(ids, callback) {
let results = [];
for (let i = 0; i < ids.length; i++) {
let id = ids[i];
results.push({id: id, name: id.title})
}
callback(results);
}
});
function init() {
let localeDir = _thisExtension.dir.get_child('locale');
if (localeDir.query_exists(null)) {
Gettext.bindtextdomain('searchbookmarks', localeDir.get_path());
} else {
Gettext.bindtextdomain('searchbookmarks', Config.LOCALEDIR);
}
}
function enable() {
try {
const Gda = imports.gi.Gda;
} catch(e) {
/**
* TODO: How to wrap translatable lines in GJS? ' \n' is not
* allowed by GJS. ',\n' and '\\n' is not handled by intltool/xgettext.
*/
Main.notifyError(
_("Search Midori and Mozilla bookmarks disbaled"),
_("The GObject Introspection library 'Gda' could not be imported. If you want to search in Midori or Mozilla bookmarks, you must install the package that contains the file 'Gda-5.0.typelib'."));
}
if (_searchBookmarksInstance == null) {
_searchBookmarksInstance = new SearchBookmarks();
/**
* Hack copied from
* https://github.com/hamiller/tracker-search-provider/blob/gnome_12_listview/extension.js
* to fix https://bugzilla.gnome.org/show_bug.cgi?id=727461
*/
if (useNewSearch) {
Main.overview.viewSelector._searchResults._searchSystem.addProvider(_searchBookmarksInstance);
} else {
Main.overview.addSearchProvider(_searchBookmarksInstance);
}
}
}
function disable() {
if (_searchBookmarksInstance != null) {
/**
* Hack copied from
* https://github.com/hamiller/tracker-search-provider/blob/gnome_12_listview/extension.js
* to fix https://bugzilla.gnome.org/show_bug.cgi?id=727461
*/
if (useNewSearch) {
Main.overview.viewSelector._searchResults._searchSystem._unregisterProvider(_searchBookmarksInstance);
} else {
Main.overview.removeSearchProvider(_searchBookmarksInstance);
}
_searchBookmarksInstance = null;
}
}