/**
   * Handles the selection rows in the library window, enabling or disabling buttons and chat menu
   * items depending on the values in the selected rows.
   *
   * @param row the index of the first row that is selected
   */
  public void handleSelection(int row) {
    int[] sel = TABLE.getSelectedRows();
    if (sel.length == 0) {
      handleNoSelection();
      return;
    }

    File selectedFile = getFile(sel[0]);

    //  always turn on Launch, Delete, Magnet Lookup, Bitzi Lookup
    LAUNCH_ACTION.setEnabled(true);
    LAUNCH_OS_ACTION.setEnabled(true);
    DELETE_ACTION.setEnabled(true);

    if (selectedFile != null && !selectedFile.getName().endsWith(".torrent")) {
      CREATE_TORRENT_ACTION.setEnabled(sel.length == 1);
    }

    if (selectedFile != null) {
      SEND_TO_FRIEND_ACTION.setEnabled(sel.length == 1);

      if (getMediaType().equals(MediaType.getAnyTypeMediaType())) {
        boolean atLeastOneIsPlayable = false;

        for (int i : sel) {
          File f = getFile(i);
          if (MediaPlayer.isPlayableFile(f) || hasExtension(f.getAbsolutePath(), "mp4")) {
            atLeastOneIsPlayable = true;
            break;
          }
        }

        SEND_TO_ITUNES_ACTION.setEnabled(atLeastOneIsPlayable);
      } else {
        SEND_TO_ITUNES_ACTION.setEnabled(
            getMediaType().equals(MediaType.getAudioMediaType())
                || hasExtension(selectedFile.getAbsolutePath(), "mp4"));
      }
    }

    if (sel.length == 1 && selectedFile.isFile() && selectedFile.getParentFile() != null) {
      OPEN_IN_FOLDER_ACTION.setEnabled(true);
    } else {
      OPEN_IN_FOLDER_ACTION.setEnabled(false);
    }

    if (sel.length == 1) {
      LibraryMediator.instance().getLibraryCoverArt().setFile(selectedFile);
    }

    //        boolean anyBeingShared = isAnyBeingShared();
    //        WIFI_SHARE_ACTION.setEnabled(!anyBeingShared);
    //        WIFI_UNSHARE_ACTION.setEnabled(!anyBeingShared);
  }
 private void uxLogMediaPreview() {
   MediaType mediaType = MediaType.getMediaTypeForExtension(searchResult.getExtension());
   if (mediaType != null) {
     boolean isVideo = mediaType.equals(MediaType.getVideoMediaType());
     UXStats.instance()
         .log(
             isVideo
                 ? UXAction.SEARCH_RESULT_VIDEO_PREVIEW
                 : UXAction.SEARCH_RESULT_AUDIO_PREVIEW);
   }
 }
  /**
   * Updates the Table based on the selection of the given table. Perform lookups to remove any
   * store files from the shared folder view and to only display store files in the store view
   */
  void updateTableFiles(DirectoryHolder dirHolder) {
    if (dirHolder == null) {
      return;
    }

    if (dirHolder instanceof MediaTypeSavedFilesDirectoryHolder) {
      MediaType mediaType = ((MediaTypeSavedFilesDirectoryHolder) dirHolder).getMediaType();
      setMediaType(mediaType);

      if (mediaType.equals(MediaType.getAudioMediaType())) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_AUDIO);
      } else if (mediaType == MediaType.getImageMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_PICTURES);
      } else if (mediaType == MediaType.getDocumentMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_DOCUMENTS);
      } else if (mediaType == MediaType.getVideoMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_VIDEOS);
      } else if (mediaType == MediaType.getTorrentMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_TORRENTS);
      } else if (mediaType == MediaType.getProgramMediaType()) {
        UXStats.instance().log(UXAction.LIBRARY_BROWSE_FILE_TYPE_APPLICATIONS);
      }
    } else {
      setMediaType(MediaType.getAnyTypeMediaType());
    }
    clearTable();

    List<List<File>> partitionedFiles = split(100, Arrays.asList(dirHolder.getFiles()));

    for (List<File> partition : partitionedFiles) {
      final List<File> fPartition = partition;

      BackgroundExecutorService.schedule(
          new Runnable() {

            @Override
            public void run() {
              for (final File file : fPartition) {
                GUIMediator.safeInvokeLater(
                    new Runnable() {
                      public void run() {
                        addUnsorted(file);
                      }
                    });
              }

              GUIMediator.safeInvokeLater(
                  new Runnable() {
                    public void run() {
                      LibraryMediator.instance().getLibrarySearch().addResults(fPartition.size());
                    }
                  });
            }
          });
    }

    forceResort();
  }
  /** Launches the associated applications for each selected file in the library if it can. */
  void launch(boolean playAudio) {
    int[] rows = TABLE.getSelectedRows();
    if (rows.length == 0) {
      return;
    }

    File selectedFile = DATA_MODEL.getFile(rows[0]);

    if (OSUtils.isWindows()) {
      if (selectedFile.isDirectory()) {
        GUIMediator.launchExplorer(selectedFile);
        return;
      } else if (!MediaPlayer.isPlayableFile(selectedFile)) {
        String extension = FilenameUtils.getExtension(selectedFile.getName());
        if (extension != null && extension.equals("torrent")) {
          GUIMediator.instance().openTorrentFile(selectedFile, true);
        } else {
          GUIMediator.launchFile(selectedFile);
        }
        return;
      }
    }

    LaunchableProvider[] providers = new LaunchableProvider[rows.length];
    boolean stopAudio = false;
    for (int i = 0; i < rows.length; i++) {
      try {
        MediaType mt =
            MediaType.getMediaTypeForExtension(
                FilenameUtils.getExtension(DATA_MODEL.getFile(rows[i]).getName()));
        if (mt.equals(MediaType.getVideoMediaType())) {
          stopAudio = true;
        }
      } catch (Throwable e) {
        // ignore
      }
      providers[i] = new FileProvider(DATA_MODEL.getFile(rows[i]));
    }
    if (stopAudio || !playAudio) {
      MediaPlayer.instance().stop();
    }

    if (playAudio) {
      GUILauncher.launch(providers);
      UXStats.instance()
          .log(stopAudio ? UXAction.LIBRARY_VIDEO_PLAY : UXAction.LIBRARY_PLAY_AUDIO_FROM_FILE);
    } else {
      GUIMediator.launchFile(selectedFile);
    }
  }
  public void handleActionKey() {
    LibraryFilesTableDataLine line = DATA_MODEL.get(TABLE.getSelectedRow());
    if (line == null || line.getFile() == null) {
      return;
    }
    if (getMediaType().equals(MediaType.getAudioMediaType())
        && MediaPlayer.isPlayableFile(line.getFile())) {
      MediaPlayer.instance()
          .asyncLoadMedia(new MediaSource(line.getFile()), true, false, true, null, getFilesView());
      UXStats.instance().log(UXAction.LIBRARY_PLAY_AUDIO_FROM_FILE);
      return;
    }

    int[] rows = TABLE.getSelectedRows();
    // LibraryTableModel ltm = DATA_MODEL;
    // File file;
    for (int i = 0; i < rows.length; i++) {
      // file = ltm.getFile(rows[i]);
      // if it's a directory try to select it in the library tree
      // if it could be selected return
      //			if (file.isDirectory()
      //				&& LibraryMediator.setSelectedDirectory(file))
      //				return;
    }

    launch(true);
  }
  // inherit doc comment
  protected JPopupMenu createPopupMenu() {
    if (TABLE.getSelectionModel().isSelectionEmpty()) {
      return null;
    }

    JPopupMenu menu = new SkinPopupMenu();

    menu.add(new SkinMenuItem(LAUNCH_ACTION));
    if (getMediaType().equals(MediaType.getAudioMediaType())) {
      menu.add(new SkinMenuItem(LAUNCH_OS_ACTION));
    }
    if (hasExploreAction()) {
      menu.add(new SkinMenuItem(OPEN_IN_FOLDER_ACTION));
    }

    if (areAllSelectedFilesMP4s()) {
      menu.add(DEMUX_MP4_AUDIO_ACTION);
      DEMUX_MP4_AUDIO_ACTION.setEnabled(
          !((DemuxMP4AudioAction) DEMUX_MP4_AUDIO_ACTION).isDemuxing());
    }

    menu.add(new SkinMenuItem(CREATE_TORRENT_ACTION));

    if (areAllSelectedFilesPlayable()) {
      menu.add(createAddToPlaylistSubMenu());
    }

    menu.add(new SkinMenuItem(SEND_TO_FRIEND_ACTION));
    menu.add(new SkinMenuItem(SEND_TO_ITUNES_ACTION));

    menu.addSeparator();
    menu.add(new SkinMenuItem(DELETE_ACTION));
    menu.addSeparator();

    int[] rows = TABLE.getSelectedRows();
    boolean dirSelected = false;
    boolean fileSelected = false;

    for (int i = 0; i < rows.length; i++) {
      File f = DATA_MODEL.get(rows[i]).getFile();
      if (f.isDirectory()) {
        dirSelected = true;
        //				if (IncompleteFileManager.isTorrentFolder(f))
        //					torrentSelected = true;
      } else fileSelected = true;

      if (dirSelected && fileSelected) break;
    }

    DELETE_ACTION.setEnabled(true);
    LibraryFilesTableDataLine line = DATA_MODEL.get(rows[0]);
    menu.add(createSearchSubMenu(line));
    return menu;
  }
    /**
     * Returns an List of URNs, from 'youngest' to 'oldest'.
     * @param max the maximum number of URNs you want returned.  if you
     * want all, give Integer.MAX_VALUE.
     * @param request in case the query has meta-flags, you can give it to
     * me. null is fine though.
     * @return a List ordered by younger URNs.
     */
    public List getFiles(final QueryRequest request, final int max)
        throws IllegalArgumentException {
        // if i'm using FM, always grab that lock first and then me.  be quick
        // about it though :)
        synchronized (RouterService.getFileManager()) {
        synchronized (this) {
        if (max < 1) throw new IllegalArgumentException("bad max = " + max);
        List urnList = new ArrayList();
        Iterator iter = TIME_TO_URNSET_MAP.entrySet().iterator();
        final MediaType.Aggregator filter = 
            (request == null ? null : MediaType.getAggregator(request));

        // may be non-null at loop end
        List toRemove = null;

        // we bank on the fact that the TIME_TO_URNSET_MAP iterator returns the 
        // entries in descending order....
        while (iter.hasNext() && (urnList.size() < max)) {
            Map.Entry currEntry = (Map.Entry) iter.next();
            Set urns = (Set) currEntry.getValue();

            // only put as many as desired, and possibly filter results based
            // on what the query desires
            Iterator innerIter = urns.iterator();
            while ((urnList.size() < max) && innerIter.hasNext()) {
                URN currURN = (URN) innerIter.next();
                FileDesc fd =
                    RouterService.getFileManager().getFileDescForUrn(currURN);
                // unfortunately fds can turn into ifds so ignore
                if ((fd == null) || (fd instanceof IncompleteFileDesc)) {
                    if (toRemove == null) toRemove = new ArrayList();
                    toRemove.add(currURN);
                    continue;
                }

                if (filter == null) urnList.add(currURN);
                else if (filter.allow(fd.getFileName())) urnList.add(currURN);
            }
        }

        // clear any ifd's or unshared files that may have snuck into structures
        if (toRemove != null) {
            Iterator removees = toRemove.iterator();
            while (removees.hasNext()) {
                URN currURN = (URN) removees.next();
                removeTime(currURN);
            }
        }

        return urnList;
        }
        }
    }
  /**
   * Specialized constructor for creating a "dummy" result panel. This should only be called once at
   * search window creation-time.
   */
  SearchResultMediator(JPanel overlay) {
    super(SEARCH_TABLE);
    setupFakeTable(overlay);

    SEARCH_INFO = SearchInformation.createKeywordSearch("", null, MediaType.getAnyTypeMediaType());
    FILTER = null;
    this.token = 0;
    this.searchTokens = null;
    setButtonEnabled(SearchButtons.TORRENT_DETAILS_BUTTON_INDEX, false);
    // disable dnd for overlay panel
    TABLE.setDragEnabled(false);
    TABLE.setTransferHandler(null);

    SOUTH_PANEL.setVisible(false);
  }
 private boolean isSearchResultPlayable() {
   boolean playable = false;
   if (searchResult.getSearchResult() instanceof StreamableSearchResult) {
     playable = ((StreamableSearchResult) searchResult.getSearchResult()).getStreamUrl() != null;
     if (searchResult.getExtension() != null) {
       MediaType mediaType = MediaType.getMediaTypeForExtension(searchResult.getExtension());
       playable =
           mediaType != null && (mediaType.equals(MediaType.getAudioMediaType()))
               || mediaType.equals(MediaType.getVideoMediaType());
     }
   }
   return playable;
 }
Ejemplo n.º 10
0
  /** Returns a menu with a 'repeat search' and 'repeat search no clear' action. */
  protected final JMenu createSearchAgainMenu(SearchResultDataLine line) {
    JMenu menu = new SkinMenu(I18n.tr("Search More"));
    menu.add(new SkinMenuItem(new RepeatSearchAction()));

    if (line == null) {
      menu.setEnabled(isRepeatSearchEnabled());
      return menu;
    }

    menu.addSeparator();
    String keywords = QueryUtils.createQueryString(line.getFilename());
    SearchInformation info =
        SearchInformation.createKeywordSearch(keywords, null, MediaType.getAnyTypeMediaType());
    if (SearchMediator.validateInfo(info) == SearchMediator.QUERY_VALID) {
      menu.add(
          new SkinMenuItem(new SearchAction(info, I18nMarker.marktr("Search for Keywords: {0}"))));
    }

    return menu;
  }
  public void scan(String filePath) {
    try {
      MediaType mt = MediaType.getMediaTypeForExtension(FilenameUtils.getExtension(filePath));

      if (mt == null) {
        scanDocument(filePath, true);
      } else if (mt.equals(MediaType.getAudioMediaType())) {
        scanAudio(filePath, true);
      } else if (mt.equals(MediaType.getImageMediaType())) {
        scanPictures(filePath, true);
      } else if (mt.equals(MediaType.getVideoMediaType())) {
        scanVideo(
            filePath,
            true); // until we integrate mplayer for video and research metadata extraction
      } else {
        scanDocument(filePath, true);
      }

    } catch (Throwable e) {
      scanDocument(filePath, true);
      LOG.log(Level.WARNING, "Error scanning file, scanned as document: " + filePath, e);
    }
  }
 /**
  * Note: This is set up for this to work. Polling is not needed though, because updates already
  * generate update events.
  */
 private LibraryPlaylistsTableMediator() {
   super("LIBRARY_PLAYLISTS_TABLE");
   setMediaType(MediaType.getAudioMediaType());
   ThemeMediator.addThemeObserver(this);
 }
Ejemplo n.º 13
0
 /** @return true if the Response falls within one of the MediaTypes this aggregates. */
 public boolean allow(final String fName) {
   for (MediaType mt : _filters) {
     if (mt.matches(fName)) return true;
   }
   return false;
 }