2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2009-06-16 12:20:12 -04:00
|
|
|
|
2010-02-09 12:42:07 -05:00
|
|
|
const St = imports.gi.St;
|
|
|
|
const Shell = imports.gi.Shell;
|
2009-07-08 21:06:06 -04:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const Signals = imports.signals;
|
2009-11-29 17:45:30 -05:00
|
|
|
const Search = imports.ui.search;
|
2009-06-16 12:20:12 -04:00
|
|
|
|
|
|
|
const THUMBNAIL_ICON_MARGIN = 2;
|
|
|
|
|
|
|
|
function DocInfo(recentInfo) {
|
|
|
|
this._init(recentInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
DocInfo.prototype = {
|
|
|
|
_init : function(recentInfo) {
|
2009-10-21 16:56:06 -04:00
|
|
|
this.recentInfo = recentInfo;
|
2009-07-25 11:00:37 -04:00
|
|
|
// We actually used get_modified() instead of get_visited()
|
|
|
|
// here, as GtkRecentInfo doesn't updated get_visited()
|
|
|
|
// correctly. See http://bugzilla.gnome.org/show_bug.cgi?id=567094
|
2010-07-09 18:17:20 -04:00
|
|
|
this.timestamp = recentInfo.get_modified();
|
2009-06-16 12:20:12 -04:00
|
|
|
this.name = recentInfo.get_display_name();
|
2009-11-29 17:45:30 -05:00
|
|
|
this._lowerName = this.name.toLowerCase();
|
2009-06-16 12:20:12 -04:00
|
|
|
this.uri = recentInfo.get_uri();
|
|
|
|
this.mimeType = recentInfo.get_mime_type();
|
|
|
|
},
|
|
|
|
|
2009-06-29 15:08:48 -04:00
|
|
|
createIcon : function(size) {
|
2010-02-09 12:42:07 -05:00
|
|
|
return St.TextureCache.get_default().load_recent_thumbnail(size, this.recentInfo);
|
2009-06-16 12:20:12 -04:00
|
|
|
},
|
|
|
|
|
2011-01-30 16:09:58 -05:00
|
|
|
launch : function(workspaceIndex) {
|
|
|
|
Shell.DocSystem.get_default().open(this.recentInfo, workspaceIndex);
|
2009-06-16 12:20:12 -04:00
|
|
|
},
|
|
|
|
|
2009-11-29 17:45:30 -05:00
|
|
|
matchTerms: function(terms) {
|
|
|
|
let mtype = Search.MatchType.NONE;
|
|
|
|
for (let i = 0; i < terms.length; i++) {
|
|
|
|
let term = terms[i];
|
|
|
|
let idx = this._lowerName.indexOf(term);
|
|
|
|
if (idx == 0) {
|
|
|
|
mtype = Search.MatchType.PREFIX;
|
|
|
|
} else if (idx > 0) {
|
2010-06-06 19:09:15 -04:00
|
|
|
if (mtype == Search.MatchType.NONE)
|
|
|
|
mtype = Search.MatchType.SUBSTRING;
|
2009-11-29 17:45:30 -05:00
|
|
|
} else {
|
2010-06-06 19:09:15 -04:00
|
|
|
return Search.MatchType.NONE;
|
2009-11-29 17:45:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mtype;
|
2009-06-16 12:20:12 -04:00
|
|
|
}
|
|
|
|
};
|
2009-07-08 21:06:06 -04:00
|
|
|
|
|
|
|
var docManagerInstance = null;
|
|
|
|
|
2009-08-16 22:23:44 -04:00
|
|
|
function getDocManager() {
|
2009-07-08 21:06:06 -04:00
|
|
|
if (docManagerInstance == null)
|
2009-08-16 22:23:44 -04:00
|
|
|
docManagerInstance = new DocManager();
|
2009-07-08 21:06:06 -04:00
|
|
|
return docManagerInstance;
|
|
|
|
}
|
|
|
|
|
2009-11-29 17:43:25 -05:00
|
|
|
/**
|
2011-01-14 16:15:53 -05:00
|
|
|
* DocManager wraps the DocSystem, primarily to expose DocInfo objects.
|
2009-11-29 17:43:25 -05:00
|
|
|
*/
|
2009-08-16 22:23:44 -04:00
|
|
|
function DocManager() {
|
|
|
|
this._init();
|
2009-07-08 21:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DocManager.prototype = {
|
2009-08-16 22:23:44 -04:00
|
|
|
_init: function() {
|
2009-11-29 17:43:25 -05:00
|
|
|
this._docSystem = Shell.DocSystem.get_default();
|
|
|
|
this._infosByTimestamp = [];
|
|
|
|
this._infosByUri = {};
|
|
|
|
this._docSystem.connect('changed', Lang.bind(this, this._reload));
|
2009-07-08 21:06:06 -04:00
|
|
|
this._reload();
|
|
|
|
},
|
|
|
|
|
|
|
|
_reload: function() {
|
2009-11-29 17:43:25 -05:00
|
|
|
let docs = this._docSystem.get_all();
|
|
|
|
this._infosByTimestamp = [];
|
|
|
|
this._infosByUri = {};
|
2009-07-08 21:06:06 -04:00
|
|
|
for (let i = 0; i < docs.length; i++) {
|
|
|
|
let recentInfo = docs[i];
|
2009-08-16 22:23:44 -04:00
|
|
|
|
2009-07-08 21:06:06 -04:00
|
|
|
let docInfo = new DocInfo(recentInfo);
|
2009-11-29 17:43:25 -05:00
|
|
|
this._infosByTimestamp.push(docInfo);
|
|
|
|
this._infosByUri[docInfo.uri] = docInfo;
|
2009-07-08 21:06:06 -04:00
|
|
|
}
|
2009-11-29 17:43:25 -05:00
|
|
|
this.emit('changed');
|
|
|
|
},
|
|
|
|
|
|
|
|
getTimestampOrderedInfos: function() {
|
|
|
|
return this._infosByTimestamp;
|
|
|
|
},
|
|
|
|
|
|
|
|
getInfosByUri: function() {
|
|
|
|
return this._infosByUri;
|
|
|
|
},
|
|
|
|
|
|
|
|
lookupByUri: function(uri) {
|
|
|
|
return this._infosByUri[uri];
|
2009-07-08 21:06:06 -04:00
|
|
|
},
|
|
|
|
|
2009-11-29 17:43:25 -05:00
|
|
|
queueExistenceCheck: function(count) {
|
|
|
|
return this._docSystem.queue_existence_check(count);
|
2009-11-29 17:45:30 -05:00
|
|
|
},
|
|
|
|
|
2010-06-06 19:09:15 -04:00
|
|
|
_searchDocs: function(items, terms) {
|
|
|
|
let multiplePrefixMatches = [];
|
2009-11-29 17:45:30 -05:00
|
|
|
let prefixMatches = [];
|
2010-06-06 19:09:15 -04:00
|
|
|
let multipleSubtringMatches = [];
|
2009-11-29 17:45:30 -05:00
|
|
|
let substringMatches = [];
|
2010-06-06 19:09:15 -04:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
let item = items[i];
|
2009-11-29 17:45:30 -05:00
|
|
|
let mtype = item.matchTerms(terms);
|
2010-06-06 19:09:15 -04:00
|
|
|
if (mtype == Search.MatchType.MULTIPLE_PREFIX)
|
|
|
|
multiplePrefixMatches.push(item.uri);
|
2009-11-29 17:45:30 -05:00
|
|
|
else if (mtype == Search.MatchType.PREFIX)
|
|
|
|
prefixMatches.push(item.uri);
|
2010-06-06 19:09:15 -04:00
|
|
|
else if (mtype == Search.MatchType.MULTIPLE_SUBSTRING)
|
|
|
|
multipleSubtringMatches.push(item.uri);
|
2009-11-29 17:45:30 -05:00
|
|
|
else if (mtype == Search.MatchType.SUBSTRING)
|
|
|
|
substringMatches.push(item.uri);
|
|
|
|
}
|
2010-06-06 19:09:15 -04:00
|
|
|
return multiplePrefixMatches.concat(prefixMatches.concat(multipleSubtringMatches.concat(substringMatches)));
|
|
|
|
},
|
|
|
|
|
|
|
|
initialSearch: function(terms) {
|
|
|
|
return this._searchDocs(this._infosByTimestamp, terms);
|
2009-11-29 17:45:30 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
subsearch: function(previousResults, terms) {
|
2010-06-06 19:09:15 -04:00
|
|
|
return this._searchDocs(previousResults.map(Lang.bind(this,
|
|
|
|
function(url) {
|
|
|
|
return this._infosByUri[url];
|
|
|
|
})), terms);
|
2009-07-08 21:06:06 -04:00
|
|
|
}
|
2010-05-19 13:26:41 -04:00
|
|
|
};
|
2009-07-08 21:06:06 -04:00
|
|
|
|
|
|
|
Signals.addSignalMethods(DocManager.prototype);
|