PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/wrt-plugins-tizen/src/platform/Tizen/Messaging/EmailUtils.cpp

https://review.tizen.org/git/
C++ | 96 lines | 67 code | 11 blank | 18 comment | 6 complexity | a982b039b1f09aeb6aa54702baaf373b MD5 | raw file
Possible License(s): GPL-3.0, AGPL-3.0, GPL-2.0, MPL-2.0, JSON, WTFPL, CC-BY-SA-4.0, CC-BY-3.0, BSD-3-Clause, LGPL-2.0, MPL-2.0-no-copyleft-exception, AGPL-1.0, 0BSD, Zlib, Unlicense, BSD-2-Clause, Apache-2.0, LGPL-3.0, ISC, MIT, CC-BY-SA-3.0, CC0-1.0, LGPL-2.1
  1. /*
  2. * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
  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. /**
  17. * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
  18. */
  19. #include <sstream>
  20. #include <pcrecpp.h>
  21. #include <Commons/Exception.h>
  22. #include <Commons/StringBuilder.h>
  23. #include <Commons/StringUtils.h>
  24. #include "EmailUtils.h"
  25. using namespace WrtDeviceApis::Commons;
  26. namespace {
  27. const char* EMAIL_ADDRESS_REGEX =
  28. "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}";
  29. const std::string EMAIL_ADDRESS_REGEX_GROUP =
  30. StringBuilder()
  31. .append("(")
  32. .append(EMAIL_ADDRESS_REGEX)
  33. .append(")")
  34. .toString();
  35. }
  36. namespace TizenApis {
  37. namespace Platform {
  38. namespace Messaging {
  39. namespace EmailUtils {
  40. std::string formatAddress(const std::string& address,
  41. const std::string& name)
  42. {
  43. if (address.empty()) {
  44. ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Address is empty.");
  45. }
  46. std::stringstream ss;
  47. if (!name.empty()) {
  48. ss << "\"" << name << "\" ";
  49. }
  50. ss << "<" << address << ">";
  51. return ss.str();
  52. }
  53. std::string formatAddressLine(const Api::Messaging::Recipients& recipients)
  54. {
  55. std::string result;
  56. for (size_t i = 0; i < recipients.getRecipientSize(); ++i) {
  57. result += formatAddress(recipients.getRecipient(i)) + ";";
  58. }
  59. return result;
  60. }
  61. std::string stripAddress(const std::string& address)
  62. {
  63. std::string result;
  64. pcrecpp::RE re(EMAIL_ADDRESS_REGEX_GROUP);
  65. if (!re.PartialMatch(address, &result)) {
  66. ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Couldn't discover email address");
  67. }
  68. return result;
  69. }
  70. Api::Messaging::Recipients stripAddressLine(const std::string& addressLine)
  71. {
  72. typedef std::vector<std::string> Addresses;
  73. Api::Messaging::Recipients result;
  74. Addresses addresses = String::split(addressLine, ';');
  75. for (Addresses::iterator it = addresses.begin();
  76. it != addresses.end();
  77. ++it) {
  78. result.appendRecipient(stripAddress(*it));
  79. }
  80. return result;
  81. }
  82. }
  83. }
  84. }
  85. }