public void setDownloadPathPopup(NSPopUpButton downloadPathPopup) {
    this.downloadPathPopup = downloadPathPopup;
    this.downloadPathPopup.setTarget(this.id());
    final Selector action = Foundation.selector("downloadPathPopupClicked:");
    this.downloadPathPopup.setAction(action);
    this.downloadPathPopup.removeAllItems();

    // Default download folder
    this.addDownloadPath(action, host.getDownloadFolder());
    this.downloadPathPopup.menu().addItem(NSMenuItem.separatorItem());
    this.addDownloadPath(
        action,
        LocalFactory.createLocal(Preferences.instance().getProperty("queue.download.folder")));
    // Shortcut to the Desktop
    this.addDownloadPath(action, LocalFactory.createLocal("~/Desktop"));
    // Shortcut to user home
    this.addDownloadPath(action, LocalFactory.createLocal("~"));
    // Shortcut to user downloads for 10.5
    this.addDownloadPath(action, LocalFactory.createLocal("~/Downloads"));
    // Choose another folder

    // Choose another folder
    this.downloadPathPopup.menu().addItem(NSMenuItem.separatorItem());
    this.downloadPathPopup
        .menu()
        .addItemWithTitle_action_keyEquivalent(CHOOSE, action, StringUtils.EMPTY);
    this.downloadPathPopup.lastItem().setTarget(this.id());
  }
 @Action
 public void anonymousCheckboxClicked(final NSButton sender) {
   if (sender.state() == NSCell.NSOnState) {
     host.getCredentials()
         .setUsername(Preferences.instance().getProperty("connection.login.anon.name"));
   }
   if (sender.state() == NSCell.NSOffState) {
     if (Preferences.instance()
         .getProperty("connection.login.name")
         .equals(Preferences.instance().getProperty("connection.login.anon.name"))) {
       host.getCredentials().setUsername(StringUtils.EMPTY);
     } else {
       host.getCredentials()
           .setUsername(Preferences.instance().getProperty("connection.login.name"));
     }
   }
   this.itemChanged();
   this.init();
 }
 public static ApplicationBadgeLabeler get() {
   if (!Preferences.instance().getBoolean("queue.dock.badge")) {
     return new DisabledApplicationBadgeLabeler();
   }
   if (!factories.containsKey(NATIVE_PLATFORM)) {
     log.warn(String.format("No implementation for %s", NATIVE_PLATFORM));
     return new DisabledApplicationBadgeLabeler();
   }
   return factories.get(NATIVE_PLATFORM).create();
 }
  @Override
  public void awakeFromNib() {
    this.cascade();
    this.init();
    this.setState(
        this.toggleOptionsButton, Preferences.instance().getBoolean("bookmark.toggle.options"));
    this.reachable();
    this.updateFavicon();

    super.awakeFromNib();
  }
  private void updateFavicon() {
    if (Preferences.instance().getBoolean("bookmark.favicon.download")) {
      this.background(
          new AbstractBackgroundAction<Void>() {

            @Override
            public void run() {
              NSImage img;
              final String f = host.getProtocol().favicon();
              if (StringUtils.isNotBlank(f)) {
                img = IconCache.iconNamed(f, 16);
              } else {
                String url = host.getWebURL() + "/favicon.ico";
                // Default favicon location
                final NSData data = NSData.dataWithContentsOfURL(NSURL.URLWithString(url));
                if (null == data) {
                  return;
                }
                img = NSImage.imageWithData(data);
              }
              if (null == img) {
                return;
              }
              favicon = IconCache.instance().convert(img, 16);
            }

            @Override
            public void cleanup() {
              if (null == favicon) {
                return;
              }
              webUrlImage.setImage(favicon);
            }

            @Override
            public Object lock() {
              return BookmarkController.this;
            }
          });
    }
  }
 private void init() {
   window.setTitle(host.getNickname());
   this.updateField(hostField, host.getHostname());
   hostField.setEnabled(host.getProtocol().isHostnameConfigurable());
   hostField.cell().setPlaceholderString(host.getProtocol().getDefaultHostname());
   this.updateField(nicknameField, host.getNickname());
   final String url;
   if (StringUtils.isNotBlank(host.getDefaultPath())) {
     url = host.toURL() + Path.normalize(host.getDefaultPath());
   } else {
     url = host.toURL();
   }
   urlField.setAttributedStringValue(HyperlinkAttributedStringFactory.create(url));
   this.updateField(portField, String.valueOf(host.getPort()));
   portField.setEnabled(host.getProtocol().isPortConfigurable());
   this.updateField(pathField, host.getDefaultPath());
   this.updateField(usernameField, host.getCredentials().getUsername());
   usernameField.cell().setPlaceholderString(host.getProtocol().getUsernamePlaceholder());
   usernameField.setEnabled(!host.getCredentials().isAnonymousLogin());
   anonymousCheckbox.setEnabled(host.getProtocol().isAnonymousConfigurable());
   anonymousCheckbox.setState(
       host.getCredentials().isAnonymousLogin() ? NSCell.NSOnState : NSCell.NSOffState);
   protocolPopup.selectItemAtIndex(
       protocolPopup.indexOfItemWithRepresentedObject(
           String.valueOf(host.getProtocol().hashCode())));
   if (null == host.getMaxConnections()) {
     transferPopup.selectItemWithTitle(DEFAULT);
   } else {
     transferPopup.selectItemWithTitle(
         host.getMaxConnections() == 1 ? TRANSFER_BROWSERCONNECTION : TRANSFER_NEWCONNECTION);
   }
   encodingPopup.setEnabled(host.getProtocol().isEncodingConfigurable());
   connectmodePopup.setEnabled(host.getProtocol().isConnectModeConfigurable());
   if (host.getProtocol().isConnectModeConfigurable()) {
     if (null == host.getFTPConnectMode()) {
       connectmodePopup.selectItemWithTitle(DEFAULT);
     } else if (host.getFTPConnectMode().equals(FTPConnectMode.PASV)) {
       connectmodePopup.selectItemWithTitle(CONNECTMODE_PASSIVE);
     } else if (host.getFTPConnectMode().equals(FTPConnectMode.PORT)) {
       connectmodePopup.selectItemWithTitle(CONNECTMODE_ACTIVE);
     }
   }
   pkCheckbox.setEnabled(host.getProtocol().equals(Protocol.SFTP));
   if (host.getCredentials().isPublicKeyAuthentication()) {
     pkCheckbox.setState(NSCell.NSOnState);
     this.updateField(pkLabel, host.getCredentials().getIdentity().getAbbreviatedPath());
     pkLabel.setTextColor(NSColor.textColor());
   } else {
     pkCheckbox.setState(NSCell.NSOffState);
     pkLabel.setStringValue(Locale.localizedString("No private key selected"));
     pkLabel.setTextColor(NSColor.disabledControlTextColor());
   }
   webURLField.setEnabled(host.getProtocol().isWebUrlConfigurable());
   final String webURL = host.getWebURL();
   webUrlImage.setToolTip(webURL);
   this.updateField(webURLField, host.getDefaultWebURL().equals(webURL) ? null : webURL);
   this.updateField(commentField, host.getComment());
   this.timezonePopup.setEnabled(!host.getProtocol().isUTCTimezone());
   if (null == host.getTimezone()) {
     if (host.getProtocol().isUTCTimezone()) {
       this.timezonePopup.setTitle(UTC.getID());
     } else {
       if (Preferences.instance().getBoolean("ftp.timezone.auto")) {
         this.timezonePopup.setTitle(AUTO);
       } else {
         this.timezonePopup.setTitle(
             TimeZone.getTimeZone(Preferences.instance().getProperty("ftp.timezone.default"))
                 .getID());
       }
     }
   } else {
     this.timezonePopup.setTitle(host.getTimezone().getID());
   }
 }
 @Override
 protected void invalidate() {
   Preferences.instance().setProperty("bookmark.toggle.options", this.toggleOptionsButton.state());
   BookmarkCollection.defaultCollection().removeListener(bookmarkCollectionListener);
   super.invalidate();
 }
  @Override
  public void write(
      boolean enabled,
      String origin,
      Distribution.Method method,
      String[] cnames,
      boolean logging,
      String loggingBucket,
      String defaultRootObject) {
    try {
      this.check();

      // Configure CDN
      LoggingStatus loggingStatus = null;
      if (logging) {
        if (this.isLoggingSupported(method)) {
          final String loggingDestination =
              StringUtils.isNotBlank(loggingBucket)
                  ? ServiceUtils.generateS3HostnameForBucket(
                      loggingBucket, false, Protocol.S3_SSL.getDefaultHostname())
                  : origin;
          loggingStatus =
              new LoggingStatus(
                  loggingDestination,
                  Preferences.instance().getProperty("cloudfront.logging.prefix"));
        }
      }
      StringBuilder name =
          new StringBuilder(Locale.localizedString("Amazon CloudFront", "S3"))
              .append(" ")
              .append(method.toString());
      if (enabled) {
        this.message(
            MessageFormat.format(
                Locale.localizedString("Enable {0} Distribution", "Status"), name));
      } else {
        this.message(
            MessageFormat.format(
                Locale.localizedString("Disable {0} Distribution", "Status"), name));
      }
      Distribution d = distributionStatus.get(method).get(origin);
      if (null == d) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("No existing distribution found for method %s", method));
        }
        this.createDistribution(enabled, method, origin, cnames, loggingStatus, defaultRootObject);
      } else {
        boolean modified = false;
        if (d.isEnabled() != enabled) {
          modified = true;
        }
        if (!Arrays.equals(d.getCNAMEs(), cnames)) {
          modified = true;
        }
        if (d.isLogging() != logging) {
          modified = true;
        }
        // Compare default root object for possible change
        if (!StringUtils.equals(d.getDefaultRootObject(), defaultRootObject)) {
          modified = true;
        }
        // Compare logging target for possible change
        if (!StringUtils.equals(d.getLoggingTarget(), loggingBucket)) {
          modified = true;
        }
        if (modified) {
          this.updateDistribution(
              enabled,
              method,
              origin,
              d.getId(),
              d.getEtag(),
              d.getReference(),
              cnames,
              loggingStatus,
              defaultRootObject);
        } else {
          log.info("Skip updating distribution not modified.");
        }
      }
    } catch (CloudFrontServiceException e) {
      this.error("Cannot write CDN configuration", e);
    } catch (IOException e) {
      this.error("Cannot write CDN configuration", e);
    } finally {
      distributionStatus.get(method).clear();
    }
  }
Example #9
0
/** @version $Id: IconCache.java 10832 2013-04-10 14:57:36Z dkocher $ */
public final class IconCache {
  private static Logger log = Logger.getLogger(IconCache.class);

  /**
   * No resizing of the cached image.
   *
   * @param name Icon filename with extension
   * @return Cached icon
   */
  public static NSImage iconNamed(final String name) {
    return IconCache.instance().iconForName(name);
  }

  /**
   * @param name Icon filename with extension
   * @param size Requested size
   * @return Cached icon
   */
  public static NSImage iconNamed(final String name, final Integer size) {
    return IconCache.instance().iconForName(name, size);
  }

  /**
   * @param name Name to lookup
   * @param width Requested size
   * @param height Requested size
   * @return Cached icon
   */
  public static NSImage iconNamed(final String name, final Integer width, final Integer height) {
    return IconCache.instance().iconForName(name, width, height);
  }

  /**
   * @param size Requested size
   * @return Standard folder icon for this platform
   */
  public static NSImage folderIcon(final Integer size) {
    return IconCache.instance().iconForFolder(size);
  }

  public static NSImage documentIcon(final String extension) {
    return IconCache.instance().iconForExtension(extension, null);
  }

  public static NSImage documentIcon(final String extension, final Integer size) {
    return IconCache.instance().iconForExtension(extension, size);
  }

  public static NSImage aliasIcon(final String extension, final Integer size) {
    return IconCache.instance()
        .badge(
            IconCache.instance().iconForName("aliasbadge.tiff", size),
            IconCache.instance().iconForExtension(extension, size));
  }

  private static IconCache instance = null;

  private static final Object lock = new Object();

  public static IconCache instance() {
    synchronized (lock) {
      if (null == instance) {
        instance = new IconCache();
      }
    }
    return instance;
  }

  private IconCache() {
    //
  }

  /** Cache limited to n entries */
  private Map<String, Map<Integer, NSImage>> cache =
      new LRUMap(Preferences.instance().getInteger("icon.cache.size")) {
        @Override
        protected boolean removeLRU(LinkEntry entry) {
          if (log.isDebugEnabled()) {
            log.debug("Removing from cache:" + entry);
          }
          return true;
        }
      };

  private void put(final String key, final NSImage image, final Integer size) {
    Map<Integer, NSImage> versions;
    if (cache.containsKey(key)) {
      versions = cache.get(key);
    } else {
      versions = new HashMap<Integer, NSImage>();
    }
    versions.put(size, image);
    cache.put(key, versions);
  }

  private NSImage load(final String key, final Integer size) {
    if (!cache.containsKey(key)) {
      log.debug(String.format("No cached image for %s", key));
      return null;
    }
    final Map<Integer, NSImage> versions = cache.get(key);
    return versions.get(size);
  }

  /**
   * @param extension File type
   * @param size Requested size
   * @return Cached icon
   */
  public NSImage iconForExtension(final String extension, final Integer size) {
    NSImage image = this.load(extension, size);
    if (null == image) {
      image = NSWorkspace.sharedWorkspace().iconForFileType(extension);
      this.put(extension, this.convert(image, size), size);
    }
    return image;
  }

  public NSImage iconForExtension(final NSImage badge, final String extension, final Integer size) {
    final String name = extension + badge.name();
    NSImage icon = this.iconForName(name, size);
    if (null == icon) {
      icon = this.badge(badge, this.iconForExtension(extension, size));
      this.put(name, icon, size);
    }
    return icon;
  }

  public NSImage iconForFolder(final Integer size) {
    NSImage folder = this.iconForName("NSFolder", size);
    if (null == folder) {
      return this.iconForPath(FOLDER_PATH, size);
    }
    return folder;
  }

  private NSImage iconForFolder(final NSImage badge, final Integer size) {
    final String name = String.format("NSFolder-%s", badge.name());
    NSImage folder = this.iconForName(name, size);
    if (null == folder) {
      folder = this.badge(badge, this.convert(FOLDER_ICON, size));
      this.put(name, folder, size);
    }
    return folder;
  }

  /**
   * Overlay badge image.
   *
   * @param badge Overlay
   * @param icon Icon
   * @return Cached icon
   */
  private NSImage badge(final NSImage badge, final NSImage icon) {
    NSImage f = NSImage.imageWithSize(icon.size());
    f.lockFocus();
    icon.drawInRect(
        new NSRect(new NSPoint(0, 0), icon.size()),
        NSZeroRect,
        NSGraphics.NSCompositeSourceOver,
        1.0f);
    badge.drawInRect(
        new NSRect(new NSPoint(0, 0), icon.size()),
        NSZeroRect,
        NSGraphics.NSCompositeSourceOver,
        1.0f);
    f.unlockFocus();
    return f;
  }

  /**
   * @param name Icon name
   * @return Cached image
   */
  protected NSImage iconForName(final String name) {
    return this.iconForName(name, null);
  }

  /**
   * @param name Icon filename with extension
   * @param size Requested size
   * @return Cached image
   * @see #convert(ch.cyberduck.ui.cocoa.application.NSImage, Integer)
   */
  protected NSImage iconForName(final String name, final Integer size) {
    return this.iconForName(name, size, size);
  }

  /**
   * @param name When looking for files in the application bundle, it is better (but not required)
   *     to include the filename extension in the name parameter
   * @param width Requested size
   * @param height Requested size
   * @return Cached icon
   * @see NSImage#imageNamed(String)
   * @see #convert(ch.cyberduck.ui.cocoa.application.NSImage, Integer, Integer)
   */
  protected NSImage iconForName(final String name, final Integer width, final Integer height) {
    NSImage image = this.load(name, width);
    if (null == image) {
      if (name.startsWith("/")) {
        image = NSImage.imageWithContentsOfFile(name);
      } else {
        image = NSImage.imageNamed(name);
      }
      if (null == image) {
        log.warn(String.format("No icon named %s", name));
        this.put(name, null, width);
      } else {
        // You can clear an image object from the cache explicitly by passing nil for the image
        // name.
        image.setName(null);
        this.put(name, this.convert(image, width, height), width);
      }
    }
    return image;
  }

  public NSImage iconForPath(final Local item) {
    return this.iconForPath(item, null);
  }

  /**
   * @param item File
   * @param size Requested size
   * @return Cached icon
   */
  public NSImage iconForPath(final Local item, final Integer size) {
    NSImage icon = null;
    if (item.exists()) {
      icon = this.load(item.getAbsolute(), size);
      if (null == icon) {
        icon = NSWorkspace.sharedWorkspace().iconForFile(item.getAbsolute());
        this.put(item.getAbsolute(), this.convert(icon, size), size);
      }
    }
    if (null == icon) {
      return this.iconForName("notfound.tiff", size);
    }
    return icon;
  }

  public NSImage iconForApplication(final String bundleIdentifier) {
    return this.iconForApplication(bundleIdentifier, null);
  }

  /**
   * @param bundleIdentifier Application
   * @param size Requested size
   * @return Cached icon
   */
  public NSImage iconForApplication(final String bundleIdentifier, final Integer size) {
    NSImage icon = this.load(bundleIdentifier, size);
    if (null == icon) {
      icon =
          NSWorkspace.sharedWorkspace()
              .iconForFile(
                  NSWorkspace.sharedWorkspace()
                      .absolutePathForAppBundleWithIdentifier(bundleIdentifier));
      this.put(bundleIdentifier, this.convert(icon, size), size);
    }
    if (null == icon) {
      return this.iconForName("notfound.tiff", size);
    }
    return icon;
  }

  private final NSRect NSZeroRect = new NSRect(0, 0);

  private static final Local FOLDER_PATH =
      LocalFactory.createLocal(Preferences.instance().getProperty("application.support.path"));

  private final NSImage FOLDER_ICON = this.iconForPath(FOLDER_PATH);

  /**
   * @param item File
   * @param size Requested size
   * @return Cached icon
   */
  public NSImage iconForPath(final Path item, final Integer size) {
    return this.iconForPath(
        item, size, Preferences.instance().getBoolean("browser.markInaccessibleFolders"));
  }

  public NSImage iconForPath(final Path item, final Integer size, final boolean overlay) {
    if (item.attributes().isSymbolicLink()) {
      final NSImage badge = this.iconForName("aliasbadge.tiff", size);
      badge.setName("aliasbadge");
      if (item.attributes().isDirectory()) {
        return this.iconForFolder(badge, size);
      }
      return this.iconForExtension(badge, item.getExtension(), size);
    }
    if (item.attributes().isFile()) {
      if (StringUtils.isEmpty(item.getExtension())) {
        if (item.attributes().getPermission().isExecutable()) {
          return this.iconForName("executable.tiff", size);
        }
      }
      return this.iconForExtension(item.getExtension(), size);
    }
    if (item.attributes().isVolume()) {
      return this.iconForName(item.getHost().getProtocol().disk(), size);
    }
    if (item.attributes().isDirectory()) {
      if (overlay) {
        if (!item.attributes().getPermission().isExecutable()) {
          final NSImage badge = this.iconForName("privatefolderbadge.tiff", size);
          badge.setName("privatefolderbadge");
          return this.iconForFolder(badge, size);
        }
        if (!item.attributes().getPermission().isReadable()) {
          if (item.attributes().getPermission().isWritable()) {
            final NSImage badge = this.iconForName("dropfolderbadge.tiff", size);
            badge.setName("dropfolderbadge");
            return this.iconForFolder(badge, size);
          }
        }
        if (!item.attributes().getPermission().isWritable()) {
          final NSImage badge = this.iconForName("readonlyfolderbadge.tiff", size);
          badge.setName("readonlyfolderbadge");
          return this.iconForFolder(badge, size);
        }
      }
      return this.iconForFolder(size);
    }
    return this.iconForName("notfound.tiff", size);
  }

  public NSImage convert(final NSImage icon, final Integer size) {
    return this.convert(icon, size, size);
  }

  public NSImage convert(final NSImage icon, final Integer width, final Integer height) {
    if (null == width || null == height) {
      log.debug(String.format("Return default size for %s", icon.name()));
      return icon;
    }
    icon.setSize(new NSSize(width, height));
    return icon;
  }
}
Example #10
0
 /**
  * @param item File
  * @param size Requested size
  * @return Cached icon
  */
 public NSImage iconForPath(final Path item, final Integer size) {
   return this.iconForPath(
       item, size, Preferences.instance().getBoolean("browser.markInaccessibleFolders"));
 }