settings.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package form // import "miniflux.app/ui/form"
  5. import (
  6. "net/http"
  7. "strconv"
  8. "miniflux.app/errors"
  9. "miniflux.app/model"
  10. )
  11. // SettingsForm represents the settings form.
  12. type SettingsForm struct {
  13. Username string
  14. Password string
  15. Confirmation string
  16. Theme string
  17. Language string
  18. Timezone string
  19. EntryDirection string
  20. EntryOrder string
  21. EntriesPerPage int
  22. KeyboardShortcuts bool
  23. ShowReadingTime bool
  24. CustomCSS string
  25. EntrySwipe bool
  26. DoubleTap bool
  27. DisplayMode string
  28. DefaultReadingSpeed int
  29. CJKReadingSpeed int
  30. DefaultHomePage string
  31. CategoriesSortingOrder string
  32. }
  33. // Merge updates the fields of the given user.
  34. func (s *SettingsForm) Merge(user *model.User) *model.User {
  35. user.Username = s.Username
  36. user.Theme = s.Theme
  37. user.Language = s.Language
  38. user.Timezone = s.Timezone
  39. user.EntryDirection = s.EntryDirection
  40. user.EntryOrder = s.EntryOrder
  41. user.EntriesPerPage = s.EntriesPerPage
  42. user.KeyboardShortcuts = s.KeyboardShortcuts
  43. user.ShowReadingTime = s.ShowReadingTime
  44. user.Stylesheet = s.CustomCSS
  45. user.EntrySwipe = s.EntrySwipe
  46. user.DoubleTap = s.DoubleTap
  47. user.DisplayMode = s.DisplayMode
  48. user.CJKReadingSpeed = s.CJKReadingSpeed
  49. user.DefaultReadingSpeed = s.DefaultReadingSpeed
  50. user.DefaultHomePage = s.DefaultHomePage
  51. user.CategoriesSortingOrder = s.CategoriesSortingOrder
  52. if s.Password != "" {
  53. user.Password = s.Password
  54. }
  55. return user
  56. }
  57. // Validate makes sure the form values are valid.
  58. func (s *SettingsForm) Validate() error {
  59. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" || s.EntryDirection == "" || s.DisplayMode == "" || s.DefaultHomePage == "" {
  60. return errors.NewLocalizedError("error.settings_mandatory_fields")
  61. }
  62. if s.CJKReadingSpeed <= 0 || s.DefaultReadingSpeed <= 0 {
  63. return errors.NewLocalizedError("error.settings_reading_speed_is_positive")
  64. }
  65. if s.Confirmation == "" {
  66. // Firefox insists on auto-completing the password field.
  67. // If the confirmation field is blank, the user probably
  68. // didn't intend to change their password.
  69. s.Password = ""
  70. } else if s.Password != "" {
  71. if s.Password != s.Confirmation {
  72. return errors.NewLocalizedError("error.different_passwords")
  73. }
  74. }
  75. return nil
  76. }
  77. // NewSettingsForm returns a new SettingsForm.
  78. func NewSettingsForm(r *http.Request) *SettingsForm {
  79. entriesPerPage, err := strconv.ParseInt(r.FormValue("entries_per_page"), 10, 0)
  80. if err != nil {
  81. entriesPerPage = 0
  82. }
  83. defaultReadingSpeed, err := strconv.ParseInt(r.FormValue("default_reading_speed"), 10, 0)
  84. if err != nil {
  85. defaultReadingSpeed = 0
  86. }
  87. cjkReadingSpeed, err := strconv.ParseInt(r.FormValue("cjk_reading_speed"), 10, 0)
  88. if err != nil {
  89. cjkReadingSpeed = 0
  90. }
  91. return &SettingsForm{
  92. Username: r.FormValue("username"),
  93. Password: r.FormValue("password"),
  94. Confirmation: r.FormValue("confirmation"),
  95. Theme: r.FormValue("theme"),
  96. Language: r.FormValue("language"),
  97. Timezone: r.FormValue("timezone"),
  98. EntryDirection: r.FormValue("entry_direction"),
  99. EntryOrder: r.FormValue("entry_order"),
  100. EntriesPerPage: int(entriesPerPage),
  101. KeyboardShortcuts: r.FormValue("keyboard_shortcuts") == "1",
  102. ShowReadingTime: r.FormValue("show_reading_time") == "1",
  103. CustomCSS: r.FormValue("custom_css"),
  104. EntrySwipe: r.FormValue("entry_swipe") == "1",
  105. DoubleTap: r.FormValue("double_tap") == "1",
  106. DisplayMode: r.FormValue("display_mode"),
  107. DefaultReadingSpeed: int(defaultReadingSpeed),
  108. CJKReadingSpeed: int(cjkReadingSpeed),
  109. DefaultHomePage: r.FormValue("default_home_page"),
  110. CategoriesSortingOrder: r.FormValue("categories_sorting_order"),
  111. }
  112. }