home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / components / sbLibrarySearch.js < prev    next >
Text File  |  2012-06-08  |  4KB  |  141 lines

  1. /*
  2.  *=BEGIN SONGBIRD GPL
  3.  *
  4.  * This file is part of the Songbird web player.
  5.  *
  6.  * Copyright(c) 2005-2010 POTI, Inc.
  7.  * http://www.songbirdnest.com
  8.  *
  9.  * This file may be licensed under the terms of of the
  10.  * GNU General Public License Version 2 (the ``GPL'').
  11.  *
  12.  * Software distributed under the License is distributed
  13.  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
  14.  * express or implied. See the GPL for the specific language
  15.  * governing rights and limitations.
  16.  *
  17.  * You should have received a copy of the GPL along with this
  18.  * program. If not, go to http://www.gnu.org/licenses/gpl.html
  19.  * or write to the Free Software Foundation, Inc.,
  20.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21.  *
  22.  *=END SONGBIRD GPL
  23.  */
  24.  
  25. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  26. Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
  27. Components.utils.import("resource://app/jsmodules/DebugUtils.jsm");
  28.  
  29. const Cc = Components.classes;
  30. const Ci = Components.interfaces;
  31.  
  32. const LOG = DebugUtils.generateLogFunction("sbLibrarySearch", 2);
  33.  
  34. function sbLibrarySearch()
  35. {
  36. }
  37.  
  38. sbLibrarySearch.prototype = {
  39.   classDescription: "Songbird Internal Library Search Service",
  40.   classID:          Components.ID("{13d82f60-a625-11df-981c-0800200c9a66}"),
  41.   contractID:       "@songbirdnest.com/Songbird/songbird-internal-search;1",
  42.   QueryInterface:   XPCOMUtils.generateQI([Ci.sbISearchEngine]),
  43.  
  44.   /**
  45.    * Return true if the active tab is displaying a songbird media page.
  46.    * Note: The media page may not be initialized.
  47.    */
  48.   _isMediaPageShowing: function sbLibrarySearch__isMediaPageShowing(aBrowser) {
  49.     return aBrowser.currentMediaPage != null;
  50.   },
  51.  
  52.   /**
  53.    * Get the media list view that is currently showing in the media page
  54.    * in the browser
  55.    */
  56.   _getCurrentMediaListView:
  57.     function sbLibrarySearch__getCurrentMediaListView(aBrowser) {
  58.     if (aBrowser.currentMediaPage && aBrowser.currentMediaPage.mediaListView) {
  59.       return aBrowser.currentMediaPage.mediaListView;
  60.     } else {
  61.       return null;
  62.     }
  63.   },
  64.  
  65.   /**
  66.    * Attempt to set a search filter on the media page in the current tab.
  67.    * Returns true on success.
  68.    */
  69.   _setMediaPageSearch:
  70.     function sbLibrarySearch__setMediaPageSearch(aBrowser, aQuery) {
  71.     // Get the currently displayed sbIMediaListView
  72.     var mediaListView = this._getCurrentMediaListView(aBrowser);
  73.  
  74.     // we need an sbIMediaListView with a cascadeFilterSet
  75.     if (!mediaListView || !mediaListView.cascadeFilterSet) {
  76.       LOG("no cascade filter set!");
  77.       return;
  78.     }
  79.  
  80.     // Attempt to set the search filter on the media list view
  81.     var filters = mediaListView.cascadeFilterSet;
  82.  
  83.     var searchIndex = -1;
  84.     for (let i = 0; i < filters.length; ++i) {
  85.       if (filters.isSearch(i)) {
  86.         searchIndex = i;
  87.         break;
  88.       }
  89.     }
  90.     if (searchIndex < 0) {
  91.       searchIndex = filters.appendSearch(["*"], 1);
  92.     }
  93.  
  94.     if (aQuery == "" || aQuery == null) {
  95.       filters.set(searchIndex, [], 0);
  96.     } else {
  97.       var stringTransform =
  98.         Cc["@songbirdnest.com/Songbird/Intl/StringTransform;1"]
  99.           .createInstance(Ci.sbIStringTransform);
  100.  
  101.       aQuery = stringTransform.normalizeString("",
  102.                       Ci.sbIStringTransform.TRANSFORM_IGNORE_NONSPACE,
  103.                       aQuery);
  104.  
  105.       var valArray = aQuery.split(" ");
  106.  
  107.       filters.set(searchIndex, valArray, valArray.length);
  108.     }
  109.   },
  110.  
  111.   doSearch: function sbLibrarySearch_doSearch(aWindow, aQuery) {
  112.     var browser = aWindow.gBrowser;
  113.  
  114.     // If there is no gBrowser, then there is nothing for us to do.
  115.     if (!browser)
  116.       return;
  117.  
  118.     // trim the leading and trailing whitespace from the search string
  119.     var query = aQuery.trim();
  120.  
  121.     if (!this._isMediaPageShowing(browser)) {
  122.       // create a view into the main library with the requested search
  123.       var library = LibraryUtils.mainLibrary;
  124.       var view = LibraryUtils.createStandardMediaListView(library, query);
  125.  
  126.       // load that view
  127.       browser.loadMediaList(library, null, null, view);
  128.     } else {
  129.     // If we are showing a media page, then just set the query directly
  130.       this._setMediaPageSearch(browser, query);
  131.     }
  132.   }
  133. };
  134.  
  135. //-----------------------------------------------------------------------------
  136. // Module
  137. //-----------------------------------------------------------------------------
  138. function NSGetModule(compMgr, fileSpec) {
  139.   return XPCOMUtils.generateModule([sbLibrarySearch]);
  140. }
  141.