2009-09-02 16:23:40 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org Code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Dao Gottwald <dao@mozilla.com>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["LightweightThemeManager"];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const MAX_USED_THEMES_COUNT = 8;
|
|
|
|
const MAX_PREVIEW_SECONDS = 30;
|
2009-10-09 18:25:58 +00:00
|
|
|
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);
|
|
|
|
});
|
2009-09-02 16:23:40 +00:00
|
|
|
|
|
|
|
var LightweightThemeManager = {
|
|
|
|
get usedThemes () {
|
|
|
|
try {
|
|
|
|
return JSON.parse(_prefs.getCharPref("usedThemes"));
|
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get currentTheme () {
|
|
|
|
try {
|
|
|
|
if (_prefs.getBoolPref("isThemeSelected"))
|
2009-10-09 18:25:58 +00:00
|
|
|
var data = this.usedThemes[0];
|
2009-09-02 16:23:40 +00:00
|
|
|
} catch (e) {}
|
2009-10-09 18:25:58 +00:00
|
|
|
|
|
|
|
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;
|
2009-09-02 16:23:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set currentTheme (aData) {
|
2009-09-17 09:03:33 +00:00
|
|
|
let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
|
|
|
|
cancel.data = false;
|
|
|
|
_observerService.notifyObservers(cancel, "lightweight-theme-change-requested",
|
|
|
|
JSON.stringify(aData));
|
2009-09-02 16:23:40 +00:00
|
|
|
|
|
|
|
if (aData) {
|
|
|
|
let usedThemes = _usedThemesExceptId(aData.id);
|
2009-09-17 09:03:33 +00:00
|
|
|
if (cancel.data && _prefs.getBoolPref("isThemeSelected"))
|
|
|
|
usedThemes.splice(1, 0, aData);
|
|
|
|
else
|
|
|
|
usedThemes.unshift(aData);
|
2009-09-02 16:23:40 +00:00
|
|
|
_updateUsedThemes(usedThemes);
|
2009-09-17 09:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cancel.data)
|
|
|
|
return null;
|
2009-09-02 16:23:40 +00:00
|
|
|
|
2009-09-17 09:03:33 +00:00
|
|
|
if (_previewTimer) {
|
|
|
|
_previewTimer.cancel();
|
|
|
|
_previewTimer = null;
|
2009-09-02 16:23:40 +00:00
|
|
|
}
|
|
|
|
|
2009-09-17 09:03:33 +00:00
|
|
|
_prefs.setBoolPref("isThemeSelected", aData != null);
|
2009-09-02 16:23:40 +00:00
|
|
|
_notifyWindows(aData);
|
|
|
|
|
2009-10-09 18:25:58 +00:00
|
|
|
if (PERSIST_ENABLED && aData)
|
|
|
|
_persistImages(aData);
|
|
|
|
|
2009-09-02 16:23:40 +00:00
|
|
|
return aData;
|
|
|
|
},
|
|
|
|
|
|
|
|
getUsedTheme: function (aId) {
|
|
|
|
var usedThemes = this.usedThemes;
|
|
|
|
for (let i = 0; i < usedThemes.length; i++) {
|
|
|
|
if (usedThemes[i].id == aId)
|
|
|
|
return usedThemes[i];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
forgetUsedTheme: function (aId) {
|
|
|
|
var currentTheme = this.currentTheme;
|
|
|
|
if (currentTheme && currentTheme.id == aId)
|
|
|
|
this.currentTheme = null;
|
|
|
|
|
|
|
|
_updateUsedThemes(_usedThemesExceptId(aId));
|
|
|
|
},
|
|
|
|
|
|
|
|
previewTheme: function (aData) {
|
|
|
|
if (!aData)
|
|
|
|
return;
|
|
|
|
|
2009-09-17 09:03:33 +00:00
|
|
|
let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
|
|
|
|
cancel.data = false;
|
|
|
|
_observerService.notifyObservers(cancel, "lightweight-theme-preview-requested",
|
|
|
|
JSON.stringify(aData));
|
|
|
|
if (cancel.data)
|
|
|
|
return;
|
|
|
|
|
2009-09-02 16:23:40 +00:00
|
|
|
if (_previewTimer)
|
|
|
|
_previewTimer.cancel();
|
|
|
|
else
|
|
|
|
_previewTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
_previewTimer.initWithCallback(_previewTimerCallback,
|
|
|
|
MAX_PREVIEW_SECONDS * 1000,
|
|
|
|
_previewTimer.TYPE_ONE_SHOT);
|
|
|
|
|
|
|
|
_notifyWindows(aData);
|
|
|
|
},
|
|
|
|
|
|
|
|
resetPreview: function () {
|
|
|
|
if (_previewTimer) {
|
|
|
|
_previewTimer.cancel();
|
|
|
|
_previewTimer = null;
|
|
|
|
_notifyWindows(this.currentTheme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function _usedThemesExceptId(aId)
|
|
|
|
LightweightThemeManager.usedThemes.filter(function (t) t.id != aId);
|
|
|
|
|
|
|
|
function _updateUsedThemes(aList) {
|
|
|
|
if (aList.length > MAX_USED_THEMES_COUNT)
|
|
|
|
aList.length = MAX_USED_THEMES_COUNT;
|
|
|
|
|
|
|
|
_prefs.setCharPref("usedThemes", JSON.stringify(aList));
|
|
|
|
|
|
|
|
_observerService.notifyObservers(null, "lightweight-theme-list-changed", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _notifyWindows(aThemeData) {
|
|
|
|
_observerService.notifyObservers(null, "lightweight-theme-changed",
|
|
|
|
JSON.stringify(aThemeData));
|
|
|
|
}
|
|
|
|
|
|
|
|
var _previewTimer;
|
|
|
|
var _previewTimerCallback = {
|
|
|
|
notify: function () {
|
|
|
|
LightweightThemeManager.resetPreview();
|
|
|
|
}
|
2009-10-09 18:25:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2009-09-02 16:23:40 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 18:25:58 +00:00
|
|
|
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);
|
|
|
|
}
|
2009-10-09 18:25:58 +00:00
|
|
|
|
2009-10-09 18:25:58 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|