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.

UserPreferences.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2013 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.models;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.Locale;
  22. import java.util.Map;
  23. import java.util.TreeMap;
  24. import com.gitblit.utils.StringUtils;
  25. /**
  26. * User preferences.
  27. *
  28. * @author James Moger
  29. *
  30. */
  31. public class UserPreferences implements Serializable {
  32. private static final long serialVersionUID = 1L;
  33. public final String username;
  34. private String locale;
  35. private Boolean emailMeOnMyTicketChanges;
  36. private final Map<String, UserRepositoryPreferences> repositoryPreferences = new TreeMap<String, UserRepositoryPreferences>();
  37. public UserPreferences(String username) {
  38. this.username = username;
  39. }
  40. public Locale getLocale() {
  41. if (StringUtils.isEmpty(locale)) {
  42. return null;
  43. }
  44. int underscore = locale.indexOf('_');
  45. if (underscore > 0) {
  46. String lang = locale.substring(0, underscore);
  47. String cc = locale.substring(underscore + 1);
  48. return new Locale(lang, cc);
  49. }
  50. return new Locale(locale);
  51. }
  52. public void setLocale(String locale) {
  53. this.locale = locale;
  54. }
  55. public UserRepositoryPreferences getRepositoryPreferences(String repositoryName) {
  56. String key = repositoryName.toLowerCase();
  57. if (!repositoryPreferences.containsKey(key)) {
  58. // default preferences
  59. UserRepositoryPreferences prefs = new UserRepositoryPreferences();
  60. prefs.username = username;
  61. prefs.repositoryName = repositoryName;
  62. repositoryPreferences.put(key, prefs);
  63. }
  64. return repositoryPreferences.get(key);
  65. }
  66. public void setRepositoryPreferences(UserRepositoryPreferences pref) {
  67. repositoryPreferences.put(pref.repositoryName.toLowerCase(), pref);
  68. }
  69. public boolean isStarredRepository(String repository) {
  70. if (repositoryPreferences == null) {
  71. return false;
  72. }
  73. String key = repository.toLowerCase();
  74. if (repositoryPreferences.containsKey(key)) {
  75. UserRepositoryPreferences pref = repositoryPreferences.get(key);
  76. return pref.starred;
  77. }
  78. return false;
  79. }
  80. public List<String> getStarredRepositories() {
  81. List<String> list = new ArrayList<String>();
  82. for (UserRepositoryPreferences prefs : repositoryPreferences.values()) {
  83. if (prefs.starred) {
  84. list.add(prefs.repositoryName);
  85. }
  86. }
  87. Collections.sort(list);
  88. return list;
  89. }
  90. public boolean isEmailMeOnMyTicketChanges() {
  91. if (emailMeOnMyTicketChanges == null) {
  92. return true;
  93. }
  94. return emailMeOnMyTicketChanges;
  95. }
  96. public void setEmailMeOnMyTicketChanges(boolean value) {
  97. this.emailMeOnMyTicketChanges = value;
  98. }
  99. }