settings.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. DisplayMode string
  27. DefaultReadingSpeed int
  28. CJKReadingSpeed int
  29. DefaultHomePage string
  30. }
  31. // Merge updates the fields of the given user.
  32. func (s *SettingsForm) Merge(user *model.User) *model.User {
  33. user.Username = s.Username
  34. user.Theme = s.Theme
  35. user.Language = s.Language
  36. user.Timezone = s.Timezone
  37. user.EntryDirection = s.EntryDirection
  38. user.EntryOrder = s.EntryOrder
  39. user.EntriesPerPage = s.EntriesPerPage
  40. user.KeyboardShortcuts = s.KeyboardShortcuts
  41. user.ShowReadingTime = s.ShowReadingTime
  42. user.Stylesheet = s.CustomCSS
  43. user.EntrySwipe = s.EntrySwipe
  44. user.DisplayMode = s.DisplayMode
  45. user.CJKReadingSpeed = s.CJKReadingSpeed
  46. user.DefaultReadingSpeed = s.DefaultReadingSpeed
  47. user.DefaultHomePage = s.DefaultHomePage
  48. if s.Password != "" {
  49. user.Password = s.Password
  50. }
  51. return user
  52. }
  53. // Validate makes sure the form values are valid.
  54. func (s *SettingsForm) Validate() error {
  55. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" || s.EntryDirection == "" || s.DisplayMode == "" || s.DefaultHomePage == "" {
  56. return errors.NewLocalizedError("error.settings_mandatory_fields")
  57. }
  58. if s.CJKReadingSpeed <= 0 || s.DefaultReadingSpeed <= 0 {
  59. return errors.NewLocalizedError("error.settings_reading_speed_is_positive")
  60. }
  61. if s.Confirmation == "" {
  62. // Firefox insists on auto-completing the password field.
  63. // If the confirmation field is blank, the user probably
  64. // didn't intend to change their password.
  65. s.Password = ""
  66. } else if s.Password != "" {
  67. if s.Password != s.Confirmation {
  68. return errors.NewLocalizedError("error.different_passwords")
  69. }
  70. }
  71. return nil
  72. }
  73. // NewSettingsForm returns a new SettingsForm.
  74. func NewSettingsForm(r *http.Request) *SettingsForm {
  75. entriesPerPage, err := strconv.ParseInt(r.FormValue("entries_per_page"), 10, 0)
  76. if err != nil {
  77. entriesPerPage = 0
  78. }
  79. defaultReadingSpeed, err := strconv.ParseInt(r.FormValue("default_reading_speed"), 10, 0)
  80. if err != nil {
  81. defaultReadingSpeed = 0
  82. }
  83. cjkReadingSpeed, err := strconv.ParseInt(r.FormValue("cjk_reading_speed"), 10, 0)
  84. if err != nil {
  85. cjkReadingSpeed = 0
  86. }
  87. return &SettingsForm{
  88. Username: r.FormValue("username"),
  89. Password: r.FormValue("password"),
  90. Confirmation: r.FormValue("confirmation"),
  91. Theme: r.FormValue("theme"),
  92. Language: r.FormValue("language"),
  93. Timezone: r.FormValue("timezone"),
  94. EntryDirection: r.FormValue("entry_direction"),
  95. EntryOrder: r.FormValue("entry_order"),
  96. EntriesPerPage: int(entriesPerPage),
  97. KeyboardShortcuts: r.FormValue("keyboard_shortcuts") == "1",
  98. ShowReadingTime: r.FormValue("show_reading_time") == "1",
  99. CustomCSS: r.FormValue("custom_css"),
  100. EntrySwipe: r.FormValue("entry_swipe") == "1",
  101. DisplayMode: r.FormValue("display_mode"),
  102. DefaultReadingSpeed: int(defaultReadingSpeed),
  103. CJKReadingSpeed: int(cjkReadingSpeed),
  104. DefaultHomePage: r.FormValue("default_home_page"),
  105. }
  106. }