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.3KB

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