2009-06-16 12:20:12 -04:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
launch : function() {
|
2009-11-29 17:43:25 -05:00
|
|
|
Shell.DocSystem.get_default().open(this.recentInfo);
|
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) {
|
|
|
|
if (mtype != Search.MatchType.NONE)
|
|
|
|
return Search.MatchType.MULTIPLE;
|
|
|
|
mtype = Search.MatchType.PREFIX;
|
|
|
|
} else if (idx > 0) {
|
|
|
|
if (mtype != Search.MatchType.NONE)
|
|
|
|
return Search.MatchType.MULTIPLE;
|
|
|
|
mtype = Search.MatchType.SUBSTRING;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
/**
|
|
|
|
* DocManager wraps the DocSystem, primarily to expose DocInfo objects
|
|
|
|
* which conform to the GenericDisplay item API.
|
|
|
|
*/
|
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
|
|
|
},
|
|
|
|
|
|
|
|
initialSearch: function(terms) {
|
|
|
|
let multipleMatches = [];
|
|
|
|
let prefixMatches = [];
|
|
|
|
let substringMatches = [];
|
|
|
|
for (let i = 0; i < this._infosByTimestamp.length; i++) {
|
|
|
|
let item = this._infosByTimestamp[i];
|
|
|
|
let mtype = item.matchTerms(terms);
|
|
|
|
if (mtype == Search.MatchType.MULTIPLE)
|
|
|
|
multipleMatches.push(item.uri);
|
|
|
|
else if (mtype == Search.MatchType.PREFIX)
|
|
|
|
prefixMatches.push(item.uri);
|
|
|
|
else if (mtype == Search.MatchType.SUBSTRING)
|
|
|
|
substringMatches.push(item.uri);
|
|
|
|
}
|
|
|
|
return multipleMatches.concat(prefixMatches.concat(substringMatches));
|
|
|
|
},
|
|
|
|
|
|
|
|
subsearch: function(previousResults, terms) {
|
|
|
|
let multipleMatches = [];
|
|
|
|
let prefixMatches = [];
|
|
|
|
let substringMatches = [];
|
|
|
|
for (let i = 0; i < previousResults.length; i++) {
|
|
|
|
let uri = previousResults[i];
|
|
|
|
let item = this._infosByUri[uri];
|
|
|
|
let mtype = item.matchTerms(terms);
|
|
|
|
if (mtype == Search.MatchType.MULTIPLE)
|
|
|
|
multipleMatches.push(uri);
|
|
|
|
else if (mtype == Search.MatchType.PREFIX)
|
|
|
|
prefixMatches.push(uri);
|
|
|
|
else if (mtype == Search.MatchType.SUBSTRING)
|
|
|
|
substringMatches.push(uri);
|
|
|
|
}
|
|
|
|
return multipleMatches.concat(prefixMatches.concat(substringMatches));
|
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);
|