2008-12-19 23:27:57 -05:00
|
|
|
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Gtk = imports.gi.Gtk;
|
2009-02-10 14:12:13 -05:00
|
|
|
const Lang = imports.lang;
|
2008-12-19 23:27:57 -05:00
|
|
|
const Shell = imports.gi.Shell;
|
2009-02-02 18:02:16 -05:00
|
|
|
const Signals = imports.signals;
|
2008-12-19 23:27:57 -05:00
|
|
|
|
2009-06-16 12:20:12 -04:00
|
|
|
const DocInfo = imports.misc.docInfo;
|
2008-12-19 23:27:57 -05:00
|
|
|
const GenericDisplay = imports.ui.genericDisplay;
|
2009-05-01 14:13:51 -04:00
|
|
|
const Main = imports.ui.main;
|
2008-12-19 23:27:57 -05:00
|
|
|
|
|
|
|
/* This class represents a single display item containing information about a document.
|
|
|
|
*
|
2009-06-16 12:20:12 -04:00
|
|
|
* docInfo - DocInfo object containing information about the document
|
2008-12-19 23:27:57 -05:00
|
|
|
* availableWidth - total width available for the item
|
|
|
|
*/
|
|
|
|
function DocDisplayItem(docInfo, availableWidth) {
|
|
|
|
this._init(docInfo, availableWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
DocDisplayItem.prototype = {
|
|
|
|
__proto__: GenericDisplay.GenericDisplayItem.prototype,
|
|
|
|
|
|
|
|
_init : function(docInfo, availableWidth) {
|
|
|
|
GenericDisplay.GenericDisplayItem.prototype._init.call(this, availableWidth);
|
|
|
|
this._docInfo = docInfo;
|
|
|
|
|
2009-06-29 15:08:48 -04:00
|
|
|
this._setItemInfo(docInfo.name, "");
|
2008-12-19 23:27:57 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
//// Public methods ////
|
|
|
|
|
|
|
|
//// Public method overrides ////
|
|
|
|
|
|
|
|
// Opens a document represented by this display item.
|
|
|
|
launch : function() {
|
2009-06-16 12:20:12 -04:00
|
|
|
this._docInfo.launch();
|
2009-03-20 12:06:34 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
//// Protected method overrides ////
|
2008-12-19 23:27:57 -05:00
|
|
|
|
2009-06-29 15:08:48 -04:00
|
|
|
// Returns an icon for the item.
|
|
|
|
_createIcon : function() {
|
|
|
|
return this._docInfo.createIcon(GenericDisplay.ITEM_DISPLAY_ICON_SIZE);
|
|
|
|
},
|
|
|
|
|
2009-03-20 12:06:34 -04:00
|
|
|
// Ensures the preview icon is created.
|
|
|
|
_ensurePreviewIconCreated : function() {
|
2009-06-16 12:20:12 -04:00
|
|
|
if (!this._previewIcon)
|
2009-06-29 15:08:48 -04:00
|
|
|
this._previewIcon = this._docInfo.createIcon(GenericDisplay.PREVIEW_ICON_SIZE);
|
2009-04-28 15:35:36 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Creates and returns a large preview icon, but only if this._docInfo is an image file
|
|
|
|
// and we were able to generate a pixbuf from it successfully.
|
|
|
|
_createLargePreviewIcon : function(availableWidth, availableHeight) {
|
2009-06-16 12:20:12 -04:00
|
|
|
if (this._docInfo.mimeType == null || this._docInfo.mimeType.indexOf("image/") != 0)
|
2009-04-28 15:35:36 -04:00
|
|
|
return null;
|
|
|
|
|
2009-06-16 12:20:12 -04:00
|
|
|
return Shell.TextureCache.get_default().load_uri_sync(this._docInfo.uri, availableWidth, availableHeight);
|
2009-03-20 12:06:34 -04:00
|
|
|
}
|
2008-12-19 23:27:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This class represents a display containing a collection of document items.
|
|
|
|
* The documents are sorted by how recently they were last visited.
|
|
|
|
*
|
|
|
|
* width - width available for the display
|
|
|
|
*/
|
2009-07-04 15:30:12 -04:00
|
|
|
function DocDisplay(width) {
|
|
|
|
this._init(width);
|
2009-07-03 22:32:23 -04:00
|
|
|
}
|
2008-12-19 23:27:57 -05:00
|
|
|
|
|
|
|
DocDisplay.prototype = {
|
|
|
|
__proto__: GenericDisplay.GenericDisplay.prototype,
|
|
|
|
|
2009-07-04 15:30:12 -04:00
|
|
|
_init : function(width) {
|
|
|
|
GenericDisplay.GenericDisplay.prototype._init.call(this, width);
|
2008-12-19 23:27:57 -05:00
|
|
|
let me = this;
|
2009-07-08 21:06:06 -04:00
|
|
|
|
|
|
|
this._docManager = DocInfo.getDocManager(GenericDisplay.ITEM_DISPLAY_ICON_SIZE);
|
2008-12-19 23:27:57 -05:00
|
|
|
this._docsStale = true;
|
2009-07-08 21:06:06 -04:00
|
|
|
this._docManager.connect('changed', function(mgr, userData) {
|
2008-12-19 23:27:57 -05:00
|
|
|
me._docsStale = true;
|
|
|
|
// Changes in local recent files should not happen when we are in the overlay mode,
|
|
|
|
// but redisplaying right away is cool when we use Zephyr.
|
|
|
|
// Also, we might be displaying remote documents, like Google Docs, in the future
|
|
|
|
// which might be edited by someone else.
|
2009-03-11 17:56:22 -04:00
|
|
|
me._redisplay(false);
|
2008-12-19 23:27:57 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
//// Protected method overrides ////
|
|
|
|
|
|
|
|
// Gets the list of recent items from the recent items manager.
|
|
|
|
_refreshCache : function() {
|
|
|
|
let me = this;
|
|
|
|
if (!this._docsStale)
|
|
|
|
return;
|
2009-07-08 21:06:06 -04:00
|
|
|
this._allItems = this._docManager.getItems();
|
2008-12-19 23:27:57 -05:00
|
|
|
this._docsStale = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Sets the list of the displayed items based on how recently they were last visited.
|
|
|
|
_setDefaultList : function() {
|
|
|
|
// It seems to be an implementation detail of the Mozilla JavaScript that object
|
|
|
|
// properties are returned during the iteration in the same order in which they were
|
|
|
|
// defined, but it is not a guarantee according to this
|
|
|
|
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in
|
2009-01-08 18:31:23 -05:00
|
|
|
// While this._allItems associative array seems to always be ordered by last added,
|
2008-12-19 23:27:57 -05:00
|
|
|
// as the results of this._recentManager.get_items() based on which it is constructed are,
|
2009-01-08 18:31:23 -05:00
|
|
|
// we should do the sorting manually because we want the order to be based on last visited.
|
2009-01-08 20:09:35 -05:00
|
|
|
//
|
|
|
|
// This function is called each time the search string is set back to '' or we display
|
|
|
|
// the overlay, so we are doing the sorting over the same items multiple times if the list
|
|
|
|
// of recent items didn't change. We could store an additional array of doc ids and sort
|
|
|
|
// them once when they are returned by this._recentManager.get_items() to avoid having to do
|
|
|
|
// this sorting each time, but the sorting seems to be very fast anyway, so there is no need
|
|
|
|
// to introduce an additional class variable.
|
2009-03-09 16:52:11 -04:00
|
|
|
this._matchedItems = [];
|
|
|
|
let docIdsToRemove = [];
|
2008-12-19 23:27:57 -05:00
|
|
|
for (docId in this._allItems) {
|
2009-03-09 16:52:11 -04:00
|
|
|
// this._allItems[docId].exists() checks if the resource still exists
|
|
|
|
if (this._allItems[docId].exists())
|
|
|
|
this._matchedItems.push(docId);
|
|
|
|
else
|
|
|
|
docIdsToRemove.push(docId);
|
2008-12-19 23:27:57 -05:00
|
|
|
}
|
2009-03-09 16:52:11 -04:00
|
|
|
|
|
|
|
for (docId in docIdsToRemove) {
|
|
|
|
delete this._allItems[docId];
|
2008-12-19 23:27:57 -05:00
|
|
|
}
|
2009-03-09 16:52:11 -04:00
|
|
|
|
|
|
|
this._matchedItems.sort(Lang.bind(this, function (a,b) { return this._compareItems(a,b); }));
|
2008-12-19 23:27:57 -05:00
|
|
|
},
|
|
|
|
|
2009-02-10 14:12:13 -05:00
|
|
|
// Compares items associated with the item ids based on how recently the items
|
|
|
|
// were last visited.
|
|
|
|
// Returns an integer value indicating the result of the comparison.
|
|
|
|
_compareItems : function(itemIdA, itemIdB) {
|
|
|
|
let docA = this._allItems[itemIdA];
|
|
|
|
let docB = this._allItems[itemIdB];
|
2009-06-16 12:20:12 -04:00
|
|
|
|
|
|
|
return docB.lastVisited() - docA.lastVisited();
|
2008-12-19 23:27:57 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Checks if the item info can be a match for the search string by checking
|
|
|
|
// the name of the document. Item info is expected to be GtkRecentInfo.
|
|
|
|
// Returns a boolean flag indicating if itemInfo is a match.
|
|
|
|
_isInfoMatching : function(itemInfo, search) {
|
2009-01-08 18:31:23 -05:00
|
|
|
if (!itemInfo.exists())
|
|
|
|
return false;
|
|
|
|
|
2008-12-19 23:27:57 -05:00
|
|
|
if (search == null || search == '')
|
|
|
|
return true;
|
2009-01-08 18:31:23 -05:00
|
|
|
|
2009-06-16 12:20:12 -04:00
|
|
|
let name = itemInfo.name.toLowerCase();
|
2008-12-19 23:27:57 -05:00
|
|
|
if (name.indexOf(search) >= 0)
|
|
|
|
return true;
|
|
|
|
// TODO: we can also check doc URIs, so that
|
|
|
|
// if you search for a directory name, we display recent files from it
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2009-06-16 12:20:12 -04:00
|
|
|
// Creates a DocDisplayItem based on itemInfo, which is expected to be a DocInfo object.
|
2009-07-04 15:30:12 -04:00
|
|
|
_createDisplayItem: function(itemInfo, width) {
|
|
|
|
return new DocDisplayItem(itemInfo, width);
|
|
|
|
}
|
2008-12-19 23:27:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Signals.addSignalMethods(DocDisplay.prototype);
|