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 / fil73DFD180666871EC6F7D182ADABA99C2 < prev    next >
Text File  |  2010-07-12  |  9KB  |  257 lines

  1. var gYaPartner = {
  2.   _init: function() {
  3.     OBSERVER_SERVICE.addObserver(this, "quit-application-granted", false);
  4.     OBSERVER_SERVICE.addObserver(this, "profile-after-change", false);
  5.   },
  6.   
  7.   _uninit: function() {
  8.     OBSERVER_SERVICE.removeObserver(this, "profile-after-change");
  9.     OBSERVER_SERVICE.removeObserver(this, "quit-application-granted");
  10.     
  11.     if (gYaInstaller && gYaInstaller.isBarUninstalled)
  12.       this.removePartnerData(true);
  13.   },
  14.   
  15.   observe: function(aSubject, aTopic, aData) {
  16.     switch (aTopic) {
  17.       case "quit-application-granted":
  18.         this._uninit();
  19.         break;
  20.       
  21.       case "profile-after-change":
  22.         this._profileInit();
  23.         break;
  24.     }
  25.   },
  26.   
  27.   _profileInit: function() {
  28.     var partnerDir = this._getPartnerDir("profile") || this._getPartnerDir("xpi");
  29.     
  30.     if (partnerDir) {
  31.       var _partnetXmlProfileFile = this._getPartnerDir("yandex");
  32.       if (_partnetXmlProfileFile)
  33.         _partnetXmlProfileFile.append("partner.xml");
  34.       
  35.       if (!_partnetXmlProfileFile || !_partnetXmlProfileFile.exists() || !_partnetXmlProfileFile.isFile()) {
  36.         var partnerFile = partnerDir.clone();
  37.         partnerFile.append("partner.xml");
  38.         
  39.         if (partnerFile.exists() && partnerFile.isFile() && partnerFile.isReadable()) {
  40.           var partnerXml = this.convertPartnerFile(partnerFile);
  41.           if (partnerXml) {
  42.             var partnerXmlProfileFile = this._getPartnerDir("yandex", true);
  43.             if (partnerXmlProfileFile) {
  44.               partnerXmlProfileFile.append("partner.xml");
  45.               try {
  46.                 partnerXmlProfileFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0755);
  47.                 
  48.                 if (partnerXmlProfileFile.exists() && partnerXmlProfileFile.isFile() && partnerXmlProfileFile.isWritable()) {
  49.                   gYaSearchService.writeFile(partnerXmlProfileFile,
  50.                                              "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
  51.                                              partnerXml.toString());
  52.                   this._checkPartnerInUI();
  53.                 }
  54.               } catch(e) {
  55.                 gYaSearchService.log("gYaPartner._profileInit error: " + e);
  56.               }
  57.             }
  58.           }
  59.         }
  60.       }
  61.       
  62.       this.removePartnerData(false);
  63.     }
  64.   },
  65.   
  66.   removePartnerData: function(aIncludeInstalled) {
  67.     if (aIncludeInstalled == true) {
  68.       try { this._getPartnerDir("yandex").remove(true); } catch(e) {}
  69.     }
  70.     
  71.     try { this._getPartnerDir("profile").remove(true); } catch(e) {}
  72.     try { this._getPartnerDir("xpi").remove(true); } catch(e) {}
  73.   },
  74.   
  75.   _getPartnerDir: function(aType, aCreate) {
  76.     var partnerDir;
  77.     
  78.     switch (aType) {
  79.       case "profile":
  80.         partnerDir = Cc["@mozilla.org/file/directory_service;1"]
  81.                          .getService(Ci.nsIProperties)
  82.                          .get("ProfD", Ci.nsIFile);
  83.         break;
  84.       
  85.       case "xpi":
  86.         partnerDir = gYaSearchService.getInstallDir();
  87.         break;
  88.       
  89.       case "yandex":
  90.         partnerDir = gYaSearchService.getYandexDir();
  91.         break;
  92.       
  93.       default:
  94.         throw "Wrong directory type";
  95.         break;
  96.     }
  97.     
  98.     partnerDir.append("yapartner");
  99.     
  100.     if (aCreate == true && !(partnerDir.exists() && partnerDir.isDirectory())) {
  101.       try {
  102.         partnerDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
  103.       } catch(e) {}
  104.     }
  105.     
  106.     return (partnerDir.exists() && partnerDir.isDirectory()) ? partnerDir : null;
  107.   },
  108.   
  109.   convertPartnerFile: function(aPartnerFile) {
  110.     var partnerXml = gYaSearchService.safeE4Xml(gYaSearchService.readFile(aPartnerFile), null, "partner");
  111.     
  112.     if (!partnerXml)
  113.       return null;
  114.     
  115.     var partnerDir = aPartnerFile.parent;
  116.     
  117.     for each (var button in partnerXml.button) {
  118.       var iconFile = partnerDir.clone();
  119.       iconFile.append(button.@icon);
  120.       
  121.       if (iconFile.exists() && iconFile.isFile() && iconFile.isReadable()) {
  122.         var iconURI = this.generateImageDataURI(iconFile);
  123.         if (iconURI)
  124.           button.@image = iconURI;
  125.       }
  126.     }
  127.     
  128.     partnerXml.@version = "1";
  129.     
  130.     return partnerXml;
  131.   },
  132.   
  133.   generateImageDataURI: function(aFile) {
  134.     var contentType = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService).getTypeFromFile(aFile);
  135.     var inputStream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
  136.     inputStream.init(aFile, 0x01, 0600, 0);
  137.     
  138.     var stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
  139.     stream.setInputStream(inputStream);
  140.     
  141.     var imageBytes = stream.readByteArray(stream.available());
  142.     var encoded = btoa(String.fromCharCode.apply(null, imageBytes));
  143.     
  144.     stream.close();
  145.     inputStream.close();
  146.     
  147.     return "data:" + contentType + ";base64," + encoded;
  148.   },
  149.   
  150.   _hasToolbaritem: null,
  151.   
  152.   toolbaritemContent: function(aDocument) {
  153.     if (this._hasToolbaritem === false)
  154.       return false;
  155.     
  156.     var toolbarItem = false;
  157.     
  158.     var partnerXmlFile = this._getPartnerDir("yandex");
  159.     if (partnerXmlFile) {
  160.       partnerXmlFile.append("partner.xml");
  161.       
  162.       if (partnerXmlFile.exists() && partnerXmlFile.isFile()) {
  163.         var toolbarItemXml = gYaSearchService.safeE4Xml(gYaSearchService.readFile(partnerXmlFile), null, "partner");
  164.         if (toolbarItemXml) {
  165.           toolbarItem =
  166.               gYaSearchService.DOMUtils.adoptNode(
  167.                   gYaSearchService.getDOMDocContent("partner-item",
  168.                       gYaSearchService.domParser.parseFromString(toolbarItemXml.toString(), "text/xml")
  169.                   ), aDocument);
  170.         }
  171.       }
  172.     }
  173.     
  174.     this._hasToolbaritem = !!toolbarItem;
  175.     
  176.     return toolbarItem;
  177.   },
  178.   
  179.   _checkPartnerInUI: function() {
  180.     this._rdf = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
  181.     this._dataSource = this._rdf.GetDataSource("rdf:local-store");
  182.     this._dirty = false;
  183.     
  184.     var currentsetResource = this._rdf.GetResource("currentset");
  185.     var toolbar = this._rdf.GetResource("chrome://browser/content/browser.xul#yasearch-bar");
  186.     var currentset = this._getPersist(toolbar, currentsetResource);
  187.     
  188.     var overlayFile = gYaSearchService.getContentDir();
  189.     overlayFile.append("yasearch.xul");
  190.     var overlayData = gYaSearchService.readFile(overlayFile);
  191.     var overlayDoc = gYaSearchService.domParser.parseFromString(overlayData, "text/xml");
  192.     var defaultset = overlayDoc.getElementById("yasearch-bar").getAttribute("defaultset");
  193.     
  194.     if (defaultset && currentset) {
  195.       var newId = "yasearch-partner",
  196.           afterId = "yasearch-preferences",
  197.           newset;
  198.       
  199.       function _makeRegexpForId(aId) {
  200.         return new RegExp("(^|,)(" + aId.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1') + ")($|,)");
  201.       }
  202.       
  203.       if (currentset == "__empty") {
  204.         newset = newId;
  205.       } else if (!_makeRegexpForId(newId).test(currentset)) {
  206.         var defaultsetCuteArray =
  207.               (defaultset.split("," + newId)[0]).split(",")
  208.                 .filter(function(aId) { return !!aId; })
  209.                 .map(function(aId) { return _makeRegexpForId(aId); });
  210.         
  211.         var j = defaultsetCuteArray.length,
  212.             foundRe = false;
  213.         
  214.         while (!foundRe && --j > -1)
  215.           foundRe = defaultsetCuteArray[j].test(currentset) ? defaultsetCuteArray[j] : false;
  216.         
  217.         newset = foundRe ? currentset.replace(foundRe, "$1$2," + newId + "$3") :
  218.                            newId + ", " + currentset;
  219.       }
  220.       
  221.       if (newset)
  222.         this._setPersist(toolbar, currentsetResource, newset);
  223.     }
  224.     
  225.     if (this._dirty)
  226.       this._dataSource.QueryInterface(Ci.nsIRDFRemoteDataSource).Flush();
  227.  
  228.     this._rdf = null;
  229.     this._dataSource = null;
  230.   },
  231.   
  232.   _getPersist: function bg__getPersist(aSource, aProperty) {
  233.     var target = this._dataSource.GetTarget(aSource, aProperty, true);
  234.     if (target instanceof Ci.nsIRDFLiteral)
  235.       return target.Value;
  236.     
  237.     return null;
  238.   },
  239.   
  240.   _setPersist: function bg__setPersist(aSource, aProperty, aTarget) {
  241.     this._dirty = true;
  242.     try {
  243.       var oldTarget = this._dataSource.GetTarget(aSource, aProperty, true);
  244.       if (oldTarget) {
  245.         if (aTarget)
  246.           this._dataSource.Change(aSource, aProperty, oldTarget, this._rdf.GetLiteral(aTarget));
  247.         else
  248.           this._dataSource.Unassert(aSource, aProperty, oldTarget);
  249.       } else {
  250.         this._dataSource.Assert(aSource, aProperty, this._rdf.GetLiteral(aTarget), true);
  251.       }
  252.     }
  253.     catch(ex) {}
  254.   }
  255. };
  256.  
  257. gYaPartner._init();