You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GravatarImage.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.panels;
  17. import java.text.MessageFormat;
  18. import org.apache.wicket.behavior.SimpleAttributeModifier;
  19. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  20. import org.apache.wicket.markup.html.link.Link;
  21. import org.apache.wicket.markup.html.panel.Panel;
  22. import org.eclipse.jgit.lib.PersonIdent;
  23. import com.gitblit.GitBlit;
  24. import com.gitblit.Keys;
  25. import com.gitblit.utils.ActivityUtils;
  26. import com.gitblit.utils.StringUtils;
  27. import com.gitblit.wicket.ExternalImage;
  28. import com.gitblit.wicket.WicketUtils;
  29. import com.gitblit.wicket.pages.GravatarProfilePage;
  30. /**
  31. * Represents a Gravatar image and links to the Gravatar profile page.
  32. *
  33. * @author James Moger
  34. *
  35. */
  36. public class GravatarImage extends Panel {
  37. private static final long serialVersionUID = 1L;
  38. public GravatarImage(String id, PersonIdent person) {
  39. this(id, person, 0);
  40. }
  41. public GravatarImage(String id, PersonIdent person, int width) {
  42. this(id, person, width, true);
  43. }
  44. public GravatarImage(String id, PersonIdent person, int width, boolean linked) {
  45. super(id);
  46. String email = person.getEmailAddress() == null ? person.getName().toLowerCase() : person.getEmailAddress().toLowerCase();
  47. String hash = StringUtils.getMD5(email);
  48. Link<Void> link = new BookmarkablePageLink<Void>("link", GravatarProfilePage.class,
  49. WicketUtils.newObjectParameter(hash));
  50. link.add(new SimpleAttributeModifier("target", "_blank"));
  51. String url = ActivityUtils.getGravatarThumbnailUrl(email, width);
  52. ExternalImage image = new ExternalImage("image", url);
  53. WicketUtils.setCssClass(image, "gravatar");
  54. link.add(image);
  55. if (linked) {
  56. WicketUtils.setHtmlTooltip(link,
  57. MessageFormat.format("View Gravatar profile for {0}", person.getName()));
  58. } else {
  59. WicketUtils.setHtmlTooltip(link, person.getName());
  60. }
  61. add(link.setEnabled(linked));
  62. setVisible(GitBlit.getBoolean(Keys.web.allowGravatar, true));
  63. }
  64. }