remotepermissions.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) by Olivier Goffart <ogoffart@woboq.com>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "remotepermissions.h"
  19. #include <cstring>
  20. namespace OCC {
  21. static const char letters[] = " WDNVCKRSMm";
  22. template <typename Char>
  23. void RemotePermissions::fromArray(const Char *p)
  24. {
  25. _value = notNullMask;
  26. if (!p)
  27. return;
  28. while (*p) {
  29. if (auto res = std::strchr(letters, static_cast<char>(*p)))
  30. _value |= (1 << (res - letters));
  31. ++p;
  32. }
  33. }
  34. QByteArray RemotePermissions::toDbValue() const
  35. {
  36. QByteArray result;
  37. if (isNull())
  38. return result;
  39. result.reserve(PermissionsCount);
  40. for (uint i = 1; i <= PermissionsCount; ++i) {
  41. if (_value & (1 << i))
  42. result.append(letters[i]);
  43. }
  44. if (result.isEmpty()) {
  45. // Make sure it is not empty so we can differentiate null and empty permissions
  46. result.append(' ');
  47. }
  48. return result;
  49. }
  50. QString RemotePermissions::toString() const
  51. {
  52. return QString::fromUtf8(toDbValue());
  53. }
  54. RemotePermissions RemotePermissions::fromDbValue(const QByteArray &value)
  55. {
  56. if (value.isEmpty())
  57. return {};
  58. RemotePermissions perm;
  59. perm.fromArray(value.constData());
  60. return perm;
  61. }
  62. RemotePermissions RemotePermissions::fromServerString(const QString &value)
  63. {
  64. RemotePermissions perm;
  65. perm.fromArray(value.utf16());
  66. return perm;
  67. }
  68. } // namespace OCC