Bug 516013 - Store the header and footer images of the selected lightweight theme in the profile folder. r=dtownsend

This commit is contained in:
Dão Gottwald 2009-10-09 20:25:58 +02:00
parent 85f34ced28
commit 75963b39e0

View File

@ -40,6 +40,31 @@ const Cc = Components.classes;
const Ci = Components.interfaces;
const MAX_USED_THEMES_COUNT = 8;
const MAX_PREVIEW_SECONDS = 30;
const PERSIST_ENABLED = true;
const PERSIST_BYPASS_CACHE = false;
const PERSIST_FILES = {
headerURL: "lightweighttheme-header",
footerURL: "lightweighttheme-footer"
};
__defineGetter__("_prefs", function () {
delete this._prefs;
return this._prefs =
Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefService).getBranch("lightweightThemes.");
});
__defineGetter__("_observerService", function () {
delete this._observerService;
return this._observerService =
Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
});
__defineGetter__("_ioService", function () {
delete this._ioService;
return this._ioService =
Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
});
var LightweightThemeManager = {
get usedThemes () {
@ -53,9 +78,22 @@ var LightweightThemeManager = {
get currentTheme () {
try {
if (_prefs.getBoolPref("isThemeSelected"))
return this.usedThemes[0];
var data = this.usedThemes[0];
} catch (e) {}
return null;
if (!data)
return null;
if (PERSIST_ENABLED) {
for (let key in PERSIST_FILES) {
try {
if (data[key] && _prefs.getBoolPref("persisted." + key))
data[key] = _getLocalImageURI(PERSIST_FILES[key]).spec;
} catch (e) {}
}
}
return data;
},
set currentTheme (aData) {
@ -84,6 +122,9 @@ var LightweightThemeManager = {
_prefs.setBoolPref("isThemeSelected", aData != null);
_notifyWindows(aData);
if (PERSIST_ENABLED && aData)
_persistImages(aData);
return aData;
},
@ -157,17 +198,66 @@ var _previewTimerCallback = {
notify: function () {
LightweightThemeManager.resetPreview();
}
};
function _persistImages(aData) {
function onSuccess(key) function () {
let current = LightweightThemeManager.currentTheme;
if (current && current.id == aData.id)
_prefs.setBoolPref("persisted." + key, true);
};
for (let key in PERSIST_FILES) {
_prefs.setBoolPref("persisted." + key, false);
if (aData[key])
_persistImage(aData[key], PERSIST_FILES[key], onSuccess(key));
}
}
__defineGetter__("_prefs", function () {
delete this._prefs;
return this._prefs =
Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefService).getBranch("lightweightThemes.");
});
function _getLocalImageURI(localFileName) {
var localFile = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get("ProfD", Ci.nsILocalFile);
localFile.append(localFileName);
return _ioService.newFileURI(localFile);
}
__defineGetter__("_observerService", function () {
delete this._observerService;
return this._observerService =
Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
});
function _persistImage(sourceURL, localFileName, callback) {
var targetURI = _getLocalImageURI(localFileName);
var sourceURI = _ioService.newURI(sourceURL, null, null);
var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
.createInstance(Ci.nsIWebBrowserPersist);
persist.persistFlags =
Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION |
(PERSIST_BYPASS_CACHE ?
Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE :
Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE);
persist.progressListener = new _persistProgressListener(callback);
persist.saveURI(sourceURI, null, null, null, null, targetURI);
}
function _persistProgressListener(callback) {
this.onLocationChange = function () {};
this.onProgressChange = function () {};
this.onStatusChange = function () {};
this.onSecurityChange = function () {};
this.onStateChange = function (aWebProgress, aRequest, aStateFlags, aStatus) {
if (aRequest &&
aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK &&
aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
try {
if (aRequest.QueryInterface(Ci.nsIHttpChannel).requestSucceeded) {
// success
callback();
return;
}
} catch (e) { }
// failure
}
};
}