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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. public String locale;
  35. private final Map<String, UserRepositoryPreferences> repositoryPreferences = new TreeMap<String, UserRepositoryPreferences>();
  36. public UserPreferences(String username) {
  37. this.username = username;
  38. }
  39. public Locale getLocale() {
  40. if (StringUtils.isEmpty(locale)) {
  41. return null;
  42. }
  43. int underscore = locale.indexOf('_');
  44. if (underscore > 0) {
  45. String lang = locale.substring(0, underscore);
  46. String cc = locale.substring(underscore + 1);
  47. return new Locale(lang, cc);
  48. }
  49. return new Locale(locale);
  50. }
  51. public UserRepositoryPreferences getRepositoryPreferences(String repositoryName) {
  52. String key = repositoryName.toLowerCase();
  53. if (!repositoryPreferences.containsKey(key)) {
  54. // default preferences
  55. UserRepositoryPreferences prefs = new UserRepositoryPreferences();
  56. prefs.username = username;
  57. prefs.repositoryName = repositoryName;
  58. repositoryPreferences.put(key, prefs);
  59. }
  60. return repositoryPreferences.get(key);
  61. }
  62. public void setRepositoryPreferences(UserRepositoryPreferences pref) {
  63. repositoryPreferences.put(pref.repositoryName.toLowerCase(), pref);
  64. }
  65. public boolean isStarredRepository(String repository) {
  66. if (repositoryPreferences == null) {
  67. return false;
  68. }
  69. String key = repository.toLowerCase();
  70. if (repositoryPreferences.containsKey(key)) {
  71. UserRepositoryPreferences pref = repositoryPreferences.get(key);
  72. return pref.starred;
  73. }
  74. return false;
  75. }
  76. public List<String> getStarredRepositories() {
  77. List<String> list = new ArrayList<String>();
  78. for (UserRepositoryPreferences prefs : repositoryPreferences.values()) {
  79. if (prefs.starred) {
  80. list.add(prefs.repositoryName);
  81. }
  82. }
  83. Collections.sort(list);
  84. return list;
  85. }
  86. }