home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / AIMP2 / aimp_2.61.583.exe / $TEMP / YandexPackSetup.msi / fil3C94B6E2F359B9E12F12DBE383F345F7 < prev    next >
Text File  |  2010-07-12  |  12KB  |  288 lines

  1. function OverlayProvider(barApplication) {
  2.     this._application = barApplication;
  3.     this._logger = barApplication.core.Log4Moz.repository.getLogger(barApplication.name + ".XULOverlay");
  4.     this._commonItemPattern = new RegExp("^" + this._application.name + "\\.cb\\-(\\S+)\\-inst\\-(.+)$");
  5.     this._defaultItemPattern = new RegExp("^" + this._application.name + "\\.cb\\-default\\-(\\d+)$");
  6.     this._defaultSetIDs = {};
  7.     this._unicodeConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
  8.     this._unicodeConverter.charset = "UTF-8";
  9.     this._DOMSerializer = Cc["@mozilla.org/xmlextras/xmlserializer;1"].getService(Ci.nsIDOMSerializer);
  10.     
  11.     this._overlayDocScel = ' \
  12.         <!DOCTYPE overlay [ \
  13.             <!ENTITY app.name "' + this._application.name + '"> \
  14.             <!ENTITY % barDTD SYSTEM "chrome://' + this._application.name + '/locale/custombar.dtd"> \
  15.             %barDTD; \
  16.         ]> \
  17.         <overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> \
  18.           <script type="application/x-javascript" src="chrome://&app.name;/content/custombar/overlay_ctrl.js"/> \
  19.           <toolbarpalette id="BrowserToolbarPalette"/> \
  20.         </overlay>';
  21. };
  22.  
  23. OverlayProvider.prototype = {
  24.     constructor: OverlayProvider,
  25.     
  26.     clear: function Overlay_clear() {
  27.         this._overlayDoc = null;
  28.     },
  29.     
  30.     newURI: function Overlay_newURI(aSpec, aOriginalCharset, aBaseURI) {
  31.         if (!aSpec)
  32.             return null;
  33.         
  34.         let content = "";
  35.         try {
  36.             switch (aSpec.toLowerCase()) {
  37.                 case "browser-overlay":
  38.                     content = btoa(this._getBrowserOverlay());
  39.                     break;
  40.                 default:
  41.                     return null;
  42.             }
  43.         }
  44.         catch (e) {
  45.             this._logger.error(this._application.core.Lib.misc.formatError(e));
  46.             this._logger.debug(e.stack);
  47.         }
  48.         
  49.         let uri =  Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
  50.         uri.spec = "data:application/vnd.mozilla.xul+xml;base64," + content;
  51.         return uri;
  52.     },
  53.     
  54.     get currentSetIds() {
  55.         if (!this._currentSetIds)
  56.             this._currentSetIds = this._readCurrentSetIds();
  57.         return this._currentSetIds;
  58.     },
  59.     
  60.     parseWidgetItemId: function Overlay_parseWidgetItemId(itemID, fullMode) {
  61.         var match = null;
  62.         
  63.         if (match = itemID.match(this._commonItemPattern))
  64.             return {
  65.                 prototypeID: match[1],
  66.                 instanceID: match[2]
  67.             };
  68.         
  69.         if (match = itemID.match(this._defaultItemPattern)) {
  70.             let presetItemIndex = parseInt(match[1], 10);
  71.             let presetWidgetInfo = this._application.defaultWidgetsInfo[presetItemIndex];
  72.             if (presetWidgetInfo) {
  73.                 let itemInfo = {
  74.                     prototypeID: presetWidgetInfo.protoID,
  75.                     instanceID: 0,
  76.                     isFromDefaultSet: true
  77.                 };
  78.                 
  79.                 if (fullMode) {
  80.                     let instID;
  81.                     if (presetItemIndex in this._defaultSetIDs)
  82.                         itemInfo.instanceID = this._defaultSetIDs[presetItemIndex];
  83.                     else
  84.                         itemInfo.instanceID = this._defaultSetIDs[presetItemIndex] = this._application.getNewWidgetInstanceID();
  85.                     itemInfo.settings = this._application.core.Lib.sysutils.copyObj(presetWidgetInfo.settings);
  86.                 }
  87.                 return itemInfo;
  88.             }
  89.         }
  90.         
  91.         return null;
  92.     },
  93.     
  94.     compileWidgetItemId: function Overlay_compileWidgetItemId(protoID, instanceID) {
  95.         return this._application.name + ".cb-" + protoID + "-inst-" + instanceID;
  96.     },
  97.     
  98.     widgetItemRemoved: function Overlay_widgetItemRemoved(instID) {
  99.         for (let key in this._defaultSetIDs) {
  100.             if (this._defaultSetIDs[key] == instID) {
  101.                 delete this._defaultSetIDs[key];
  102.             }
  103.         }
  104.     },
  105.     
  106.     addProtoIDsToPalette: function Overlay_addProtoIDsToPalette(aNewProtoIDs) {
  107.         for each (let browserWindow in this._application.core.Lib.misc.getNavigatorWindows()) {
  108.             let doc = browserWindow.document;
  109.             let toolbox = doc.getElementById("navigator-toolbox");
  110.             let palette = toolbox.palette;
  111.             
  112.             var [paletteItems, defaultSetIDs] = this._makePaletteItems(doc, aNewProtoIDs);
  113.             for each (let paletteItem in paletteItems) {
  114.                 if (!palette.getElementsByAttribute("id", paletteItem.id).length)
  115.                     palette.appendChild(paletteItem);
  116.             }
  117.         }
  118.     },
  119.     
  120.     makePaletteItem: function Overlay_makePaletteItem(doc, widgetProto, instanceID) {
  121.         let toolbarItem;
  122.         
  123.         if (!Ci.nsIWorker) {
  124.             let toolbox = doc.getElementById("navigator-toolbox");
  125.             palette = toolbox ? toolbox.palette : null;
  126.             
  127.             if (palette) {
  128.                 let existItem = palette.getElementsByTagName("toolbaritem")[0];
  129.                 if (existItem) {
  130.                     toolbarItem = existItem.cloneNode(false);
  131.                     Array.slice(toolbarItem.attributes).forEach(function(attr) toolbarItem.removeAttribute(attr.name));
  132.                 }
  133.             }
  134.         }
  135.         
  136.         if (!toolbarItem)
  137.             toolbarItem = doc.createElementNS(this._consts.STR_XUL_NS, "toolbaritem");
  138.         
  139.         toolbarItem.setAttribute("id", this.compileWidgetItemId(widgetProto.id, instanceID));
  140.         toolbarItem.setAttribute("xb-proto-id", widgetProto.id);
  141.         toolbarItem.setAttribute("xb-app", this._application.name);
  142.         toolbarItem.setAttribute("xb-widget", "true");
  143.         toolbarItem.setAttribute("label", widgetProto.name);
  144.         toolbarItem.setAttribute("image", widgetProto.unit.unitPackage.resolvePath(widgetProto.iconPath));
  145.         
  146.         return toolbarItem;
  147.     },
  148.     
  149.     _commonItemPattern: null,
  150.     _defaultItemPattern: null,
  151.     _defaultSetIDs: null,
  152.     _overlayDocScel: undefined,
  153.     _unicodeConverter: null,
  154.     _DOMSerializer: null,
  155.     _overlayDoc: null,
  156.     _logger: null,
  157.     _consts: {
  158.         STR_XUL_NS: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
  159.         
  160.         WARN_NO_SUCH_WIDGET: "No such widget ",
  161.         
  162.         ERR_CREATE_ITEM: "Could not add widget palette item",
  163.         ERR_PACKAGE_REQUIRED: "Widget package required"
  164.     },
  165.     
  166.     _getBrowserOverlay: function Overlay__getBrowserOverlay() {
  167.         this._overlayDoc = this._createBrowserOverlay();
  168.         return this._unicodeConverter.ConvertFromUnicode(this._DOMSerializer.serializeToString(this._overlayDoc));
  169.     },
  170.     
  171.     _createBrowserOverlay: function Overlay__createBrowserOverlay() {
  172.         const DOMParser = Cc["@mozilla.org/xmlextras/domparser;1"].getService(Ci.nsIDOMParser);
  173.         var overlayDoc = DOMParser.parseFromString(this._overlayDocScel, "text/xml");
  174.         
  175.         var toolbarPalette = overlayDoc.getElementById("BrowserToolbarPalette");
  176.         var [paletteItems, defaultSetIDs] = this._makePaletteItems(overlayDoc);
  177.         for each (let paletteItem in paletteItems)
  178.             toolbarPalette.appendChild(paletteItem);
  179.         
  180.         return overlayDoc;
  181.     },
  182.     
  183.     _makePaletteItems: function Overlay_makePaletteItems(overlayDoc, aProtoIDs) {
  184.         var paletteItems = [];
  185.         var defaultSetIDs = [];
  186.         
  187.         var currentSetIDsData = this._readCurrentSetIds();
  188.         var widgetLibrary = this._application.widgetLibrary;
  189.         
  190.         let protoIDs = arguments.length == 1 ? widgetLibrary.getWidgetProtoIDs() : (aProtoIDs || []);
  191.         
  192.         for each (let protoID in protoIDs) {
  193.             let widgetProto = null;
  194.             try {
  195.                 widgetProto = widgetLibrary.getWidgetProto(protoID);
  196.             }
  197.             catch (e) {
  198.                 this._logger.error(this._consts.ERR_CREATE_ITEM + ". " +
  199.                                    this._application.core.Lib.misc.formatError(e));
  200.                 continue;
  201.             }
  202.             
  203.             let isUsed = false;
  204.             
  205.             let protoInstHash = currentSetIDsData[widgetProto.id] || null;
  206.             if (protoInstHash) {
  207.                 isUsed = true;
  208.                 for each (let instID in protoInstHash) {
  209.                     paletteItems.push(this.makePaletteItem(overlayDoc, widgetProto, instID));
  210.                     
  211.                     this._application.getNewWidgetInstanceID();
  212.                     
  213.                     if (widgetProto.isUnique)
  214.                         break;
  215.                 }
  216.             }
  217.             
  218.             if (!widgetProto.isUnique || !isUsed) {
  219.                 paletteItems.push(this.makePaletteItem(overlayDoc, widgetProto, 0));
  220.             }
  221.         }
  222.         
  223.         if (arguments.length == 1) {
  224.             var presetItemsInfo = this._application.defaultWidgetsInfo;
  225.             var itemIndex = 0;
  226.             for each (let presetItemInfo in presetItemsInfo) {
  227.                 let widgetProto = null;
  228.                 try {
  229.                     widgetProto = widgetLibrary.getWidgetProto(presetItemInfo.protoID);
  230.                 }
  231.                 catch (e) {
  232.                     this._logger.error(this._consts.ERR_CREATE_ITEM + ". " +
  233.                                        this._application.core.Lib.misc.formatError(e));
  234.                     continue;
  235.                 }
  236.                 
  237.                 let itemID = this._application.name + ".cb-default-" + itemIndex;
  238.                 let paletteItem = this.makePaletteItem(overlayDoc, widgetProto, 0);
  239.                 paletteItem.setAttribute("id", itemID);
  240.                 paletteItems.push(paletteItem);
  241.                 defaultSetIDs.push(itemID);
  242.                 itemIndex++;
  243.             }
  244.         }
  245.         
  246.         return [paletteItems, defaultSetIDs];
  247.     },
  248.     
  249.     _readCurrentSetIds: function Overlay_readCurrentSetIds() {
  250.         var result = {};
  251.         
  252.         var rdfService = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
  253.         var localStoreDataSource = rdfService.GetDataSource("rdf:local-store");
  254.         
  255.         var allResources = localStoreDataSource.GetAllResources();
  256.         var currentSetResource = rdfService.GetResource("currentset");
  257.         while (allResources.hasMoreElements()) {
  258.             let res = allResources.getNext().QueryInterface(Ci.nsIRDFResource);
  259.             let tool = res.Value;
  260.             
  261.             if (tool) {
  262.                 var toolbarResource = rdfService.GetResource(tool);
  263.                 var currentSetTarget = localStoreDataSource.GetTarget(toolbarResource, currentSetResource, true);
  264.                 var currentSetStr = "";
  265.                 if (currentSetTarget instanceof Ci.nsIRDFLiteral)
  266.                     currentSetStr = currentSetTarget.Value;
  267.                 
  268.                 if (currentSetStr) {
  269.                     var currentSetIds = currentSetStr.split(",");
  270.                     for (let i = 0, len = currentSetIds.length; i < len ; i++) {
  271.                         var barWidgetIDMatch = currentSetIds[i].match(this._commonItemPattern);
  272.                         if (barWidgetIDMatch) {
  273.                             var widgetProtoID = barWidgetIDMatch[1];
  274.                             var widgetInstance = barWidgetIDMatch[2];
  275.                             var instArray = result[widgetProtoID];
  276.                             if (!instArray) {
  277.                                 instArray = [];
  278.                                 result[widgetProtoID] = instArray;
  279.                             }
  280.                             instArray.push(widgetInstance);
  281.                         }
  282.                     }
  283.                 }
  284.             }
  285.         }
  286.         return result;
  287.     }
  288. };