autofaces.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
  3. * Copyright (C) 2008 Ricardo Mones and the Claws Mail team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include "defs.h"
  23. #include "utils.h"
  24. #include "autofaces.h"
  25. static gint get_content_for_any_face(gchar *buf, gint len, gchar *anyname, gint maxlen)
  26. {
  27. FILE *xfp;
  28. gchar *xfile;
  29. gint lastc;
  30. xfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, AUTOFACES_DIR,
  31. G_DIR_SEPARATOR_S, anyname, NULL);
  32. buf[0] = '\0';
  33. if ((xfp = g_fopen(xfile, "rb")) == NULL) {
  34. g_free(xfile);
  35. debug_print("header content file '%s' not found\n", anyname);
  36. return -1;
  37. }
  38. if (fgets(buf, (len < maxlen)? len: maxlen, xfp) == NULL) {
  39. fclose(xfp);
  40. g_free(xfile);
  41. g_warning("header content file '%s' read failure\n", anyname);
  42. return -2;
  43. }
  44. lastc = strlen(buf) - 1; /* remove trailing \n */
  45. buf[lastc] = (buf[lastc] == '\n')? '\0': buf[lastc];
  46. fclose(xfp);
  47. g_free(xfile);
  48. return 0;
  49. }
  50. static gchar * get_any_face_filename_for_account(gchar *facetype, gchar *accountname)
  51. {
  52. gchar *name = NULL;
  53. gchar *what = NULL;
  54. if (facetype == NULL || accountname == NULL)
  55. return NULL;
  56. if (*facetype == '\0' || *accountname == '\0')
  57. return NULL;
  58. what = name = g_strdup_printf("%s.%s", facetype, accountname);
  59. while (*what) {
  60. switch (*what) {
  61. case '/':
  62. case '\\':
  63. case '<':
  64. case '>':
  65. case ':':
  66. case '?':
  67. case '*':
  68. *what = '_';
  69. break;
  70. default:
  71. if (*what <= ' ') {
  72. *what = '_';
  73. }
  74. break;
  75. }
  76. ++what;
  77. }
  78. return name;
  79. }
  80. gint get_default_xface(gchar *buf, gint len) {
  81. return get_content_for_any_face(buf, len, "xface", MAX_XFACE_LEN);
  82. }
  83. gint get_account_xface(gchar *buf, gint len, gchar *name) {
  84. gchar *filename = get_any_face_filename_for_account("xface", name);
  85. if (filename) {
  86. gint result = get_content_for_any_face(buf, len, filename, MAX_XFACE_LEN);
  87. g_free(filename);
  88. return result;
  89. }
  90. g_warning("header xface filename invalid\n");
  91. return -1;
  92. }
  93. gint get_default_face(gchar *buf, gint len) {
  94. return get_content_for_any_face(buf, len, "face", MAX_FACE_LEN);
  95. }
  96. gint get_account_face(gchar *buf, gint len, gchar *name) {
  97. gchar *filename = get_any_face_filename_for_account("face", name);
  98. if (filename) {
  99. gint result = get_content_for_any_face(buf, len, filename, MAX_FACE_LEN);
  100. g_free(filename);
  101. return result;
  102. }
  103. g_warning("header face filename invalid\n");
  104. return -1;
  105. }