/[LeafOK_CVS]/lbbs/src/section_list_display.c
ViewVC logotype

Diff of /lbbs/src/section_list_display.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.12 by sysadm, Sat May 31 08:54:03 2025 UTC Revision 1.28 by sysadm, Mon Jun 16 03:08:29 2025 UTC
# Line 17  Line 17 
17  #include "section_list_display.h"  #include "section_list_display.h"
18  #include "section_list_loader.h"  #include "section_list_loader.h"
19  #include "article_cache.h"  #include "article_cache.h"
20    #include "article_post.h"
21    #include "article_del.h"
22  #include "common.h"  #include "common.h"
23  #include "io.h"  #include "io.h"
24  #include "screen.h"  #include "screen.h"
25  #include "log.h"  #include "log.h"
26    #include "user_priv.h"
27    #include "article_view_log.h"
28  #include "str_process.h"  #include "str_process.h"
29  #include <time.h>  #include <time.h>
30  #include <sys/param.h>  #include <sys/param.h>
# Line 28  Line 32 
32  #include <string.h>  #include <string.h>
33    
34  static int section_topic_view_mode = 0;  static int section_topic_view_mode = 0;
35    static int section_topic_view_tid = -1;
36    
37  enum select_cmd_t  enum select_cmd_t
38  {  {
# Line 36  enum select_cmd_t Line 41  enum select_cmd_t
41          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
42          REFRESH_SCREEN = 3,          REFRESH_SCREEN = 3,
43          CHANGE_NAME_DISPLAY = 4,          CHANGE_NAME_DISPLAY = 4,
44            POST_ARTICLE = 5,
45            EDIT_ARTICLE = 6,
46            DELETE_ARTICLE = 7,
47            SHOW_HELP = 8,
48  };  };
49    
50  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname)  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname)
# Line 48  static int section_list_draw_items(int p Line 57  static int section_list_draw_items(int p
57          int len;          int len;
58          int i;          int i;
59          char article_flag;          char article_flag;
60            int is_viewed;
61          time_t tm_now;          time_t tm_now;
62    
63          time(&tm_now);          time(&tm_now);
# Line 56  static int section_list_draw_items(int p Line 66  static int section_list_draw_items(int p
66    
67          for (i = 0; i < article_count; i++)          for (i = 0; i < article_count; i++)
68          {          {
69                  article_flag = ' ';                  if (p_articles[i]->uid == BBS_priv.uid)
70                    {
71                            is_viewed = 1;
72                    }
73                    else
74                    {
75                            is_viewed = article_view_log_is_viewed(p_articles[i]->aid, &BBS_article_view_log);
76                            if (is_viewed < 0)
77                            {
78                                    log_error("article_view_log_is_viewed(aid=%d) error\n", p_articles[i]->aid);
79                                    is_viewed = 0;
80                            }
81                    }
82    
83                    article_flag = (is_viewed ? ' ' : 'N');
84    
85                  if (p_articles[i]->excerption)                  if (p_articles[i]->excerption)
86                  {                  {
87                          article_flag = 'm';                          article_flag = (is_viewed ? 'm' : 'M');
88                  }                  }
89                  else if (p_articles[i]->lock)                  else if (p_articles[i]->lock && is_viewed)
90                  {                  {
91                          article_flag = 'x';                          article_flag = 'x';
92                  }                  }
# Line 81  static int section_list_draw_items(int p Line 105  static int section_list_draw_items(int p
105                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
106                  strncat(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));                  strncat(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
107                  strncat(title_f, p_articles[i]->title, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));                  strncat(title_f, p_articles[i]->title, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
108                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len);                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len, 1);
109                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
110                  {                  {
111                          title_f[len] = '\0';                          title_f[len] = '\0';
112                  }                  }
113    
114                  moveto(4 + i, 1);                  moveto(4 + i, 1);
115                  prints("  %7d %c %s%*s %s %s",                  prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",
116                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
117                             p_articles[i]->aid,                             p_articles[i]->aid,
118                             article_flag,                             article_flag,
119                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
# Line 96  static int section_list_draw_items(int p Line 121  static int section_list_draw_items(int p
121                                                                   : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),                                                                   : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),
122                             "",                             "",
123                             str_time,                             str_time,
124                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
125                             title_f);                             title_f);
126          }          }
127    
# Line 117  static int section_list_draw_screen(cons Line 143  static int section_list_draw_screen(cons
143          show_top(str_section_master, stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
144          moveto(2, 0);          moveto(2, 0);
145          prints("返回[\033[1;32m←\033[0;37m,\033[1;32mESC\033[0;37m] 选择[\033[1;32m↑\033[0;37m,\033[1;32m↓\033[0;37m] "          prints("返回[\033[1;32m←\033[0;37m,\033[1;32mESC\033[0;37m] 选择[\033[1;32m↑\033[0;37m,\033[1;32m↓\033[0;37m] "
146                     "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m %s[\033[1;32mn\033[0;37m]\033[m",                     "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m] 发表[\033[1;32mCtrl-P\033[0;37m] "
147                       "%s[\033[1;32mn\033[0;37m] 帮助[\033[1;32mh\033[0;37m]\033[m",
148                     (display_nickname ? "显示用户名" : "显示昵称"));                     (display_nickname ? "显示用户名" : "显示昵称"));
149          moveto(3, 0);          moveto(3, 0);
150          if (display_nickname)          if (display_nickname)
# Line 167  static enum select_cmd_t section_list_se Line 194  static enum select_cmd_t section_list_se
194                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
195                  case CR:                  case CR:
196                          igetch_reset();                          igetch_reset();
197                    case 'r':
198                  case KEY_RIGHT:                  case KEY_RIGHT:
199                          if (item_count > 0)                          if (item_count > 0)
200                          {                          {
201                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
202                          }                          }
203                          break;                          break;
204                    case Ctrl('P'):
205                            return POST_ARTICLE;
206                    case 'E':
207                            return EDIT_ARTICLE;
208                    case 'd':
209                            return DELETE_ARTICLE;
210                  case KEY_HOME:                  case KEY_HOME:
211                          *p_page_id = 0;                          *p_page_id = 0;
212                    case 'P':
213                  case KEY_PGUP:                  case KEY_PGUP:
214                          *p_selected_index = 0;                          *p_selected_index = 0;
215                    case 'k':
216                  case KEY_UP:                  case KEY_UP:
217                          if (*p_selected_index <= 0)                          if (*p_selected_index <= 0)
218                          {                          {
# Line 191  static enum select_cmd_t section_list_se Line 227  static enum select_cmd_t section_list_se
227                                  (*p_selected_index)--;                                  (*p_selected_index)--;
228                          }                          }
229                          break;                          break;
230                    case '$':
231                  case KEY_END:                  case KEY_END:
232                          if (total_page > 0)                          if (total_page > 0)
233                          {                          {
234                                  *p_page_id = total_page - 1;                                  *p_page_id = total_page - 1;
235                          }                          }
236                    case 'N':
237                  case KEY_PGDN:                  case KEY_PGDN:
238                          if (item_count > 0)                          if (item_count > 0)
239                          {                          {
240                                  *p_selected_index = item_count - 1;                                  *p_selected_index = item_count - 1;
241                          }                          }
242                    case 'j':
243                  case KEY_DOWN:                  case KEY_DOWN:
244                          if (*p_selected_index + 1 >= item_count) // next page                          if (*p_selected_index + 1 >= item_count) // next page
245                          {                          {
# Line 209  static enum select_cmd_t section_list_se Line 248  static enum select_cmd_t section_list_se
248                                          (*p_page_id)++;                                          (*p_page_id)++;
249                                          *p_selected_index = 0;                                          *p_selected_index = 0;
250                                  }                                  }
251                                    else // end of last page
252                                    {
253                                            return CHANGE_PAGE; // force refresh pages
254                                    }
255                          }                          }
256                          else                          else
257                          {                          {
258                                  (*p_selected_index)++;                                  (*p_selected_index)++;
259                          }                          }
260                          break;                          break;
261                    case 'h':
262                            return SHOW_HELP;
263                  default:                  default:
264                  }                  }
265    
# Line 251  static int display_article_key_handler(i Line 296  static int display_article_key_handler(i
296          switch (*p_key)          switch (*p_key)
297          {          {
298          case 'p':          case 'p':
299            case Ctrl('X'):
300                  section_topic_view_mode = !section_topic_view_mode;                  section_topic_view_mode = !section_topic_view_mode;
301          case 0: // Set msg          case 0: // Set msg
302                  if (section_topic_view_mode)                  if (section_topic_view_mode)
303                  {                  {
304                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
305                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
306                                           "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] │ "                                           "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] "
307                                           "帮助[\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
308                  }                  }
309                  else                  else
310                  {                  {
311                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
312                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
313                                           "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ "                                           "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] "
314                                           "帮助[\033[32mh\033[33m] |");                                           "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
315                  }                  }
316                  *p_key = 0;                  *p_key = 0;
317                  break;                  break;
318            case 'r': // Reply article
319                    return 1;
320          case KEY_UP:          case KEY_UP:
321            case KEY_PGUP:
322            case KEY_HOME:
323                  if (p_ctx->reach_begin)                  if (p_ctx->reach_begin)
324                  {                  {
325                          if (section_topic_view_mode)                          if (section_topic_view_mode)
326                          {                          {
327                                  *p_key = KEY_PGUP;                                  *p_key = KEY_PGUP;
328                          }                          }
329                            else
330                            {
331                                    *p_key = KEY_UP;
332                            }
333                          return 1;                          return 1;
334                  }                  }
335                  break;                  break;
336            case 'k':
337                    if (section_topic_view_mode)
338                    {
339                            *p_key = KEY_PGUP;
340                    }
341                    else
342                    {
343                            *p_key = KEY_UP;
344                    }
345                    return 1;
346          case KEY_DOWN:          case KEY_DOWN:
347            case KEY_PGDN:
348            case KEY_END:
349                  if (p_ctx->reach_end)                  if (p_ctx->reach_end)
350                  {                  {
351                          if (section_topic_view_mode)                          if (section_topic_view_mode)
352                          {                          {
353                                  *p_key = KEY_PGDN;                                  *p_key = KEY_PGDN;
354                          }                          }
355                            else
356                            {
357                                    *p_key = KEY_DOWN;
358                            }
359                          return 1;                          return 1;
360                  }                  }
361                  break;                  break;
362            case 'j':
363                    if (section_topic_view_mode)
364                    {
365                            *p_key = KEY_PGDN;
366                    }
367                    else
368                    {
369                            *p_key = KEY_DOWN;
370                    }
371                    return 1;
372          }          }
373    
374          return 0;          return 0;
# Line 311  int section_list_display(const char *sna Line 391  int section_list_display(const char *sna
391          int ret;          int ret;
392          int loop;          int loop;
393          int direction;          int direction;
394            ARTICLE article_new;
395    
396          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
397          if (p_section == NULL)          if (p_section == NULL)
# Line 402  int section_list_display(const char *sna Line 483  int section_list_display(const char *sna
483                                          break;                                          break;
484                                  }                                  }
485    
486                                  ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 1, 0,                                  ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,
487                                                                     display_article_key_handler, DATA_READ_HELP);                                                                     display_article_key_handler, DATA_READ_HELP);
488    
489                                  if (article_cache_unload(&cache) < 0)                                  if (article_cache_unload(&cache) < 0)
# Line 411  int section_list_display(const char *sna Line 492  int section_list_display(const char *sna
492                                          break;                                          break;
493                                  }                                  }
494    
495                                    // Update article_view_log
496                                    if (article_view_log_set_viewed(p_articles[selected_index]->aid, &BBS_article_view_log) < 0)
497                                    {
498                                            log_error("article_view_log_set_viewed(aid=%d) error\n", p_articles[selected_index]->aid);
499                                    }
500    
501                                  switch (ret)                                  switch (ret)
502                                  {                                  {
503                                  case KEY_UP:                                  case KEY_UP:
# Line 496  int section_list_display(const char *sna Line 583  int section_list_display(const char *sna
583                                                  loop = 1;                                                  loop = 1;
584                                          }                                          }
585                                          break;                                          break;
586                                    case 'r': // Reply article
587                                            if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)
588                                            {
589                                                    log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);
590                                            }
591                                            loop = 1;
592                                            break;
593                                  }                                  }
594                          } while (loop);                          } while (loop);
595    
596                            // Update current topic
597                            section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
598                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
599                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
600                          {                          {
# Line 510  int section_list_display(const char *sna Line 607  int section_list_display(const char *sna
607                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
608                          {                          {
609                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
610                                    return -2;
611                            }
612                            break;
613                    case POST_ARTICLE:
614                            ret = article_post(p_section, &article_new);
615                            if (ret < 0)
616                            {
617                                    log_error("article_post(sid=%d) error\n", p_section->sid);
618                            }
619                            else if (ret > 0) // New article posted
620                            {
621                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
622                                    if (ret < 0)
623                                    {
624                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
625                                            return -3;
626                                    }
627                            }
628                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
629                            {
630                                    log_error("section_list_draw_screen() error\n");
631                                    return -2;
632                            }
633                            break;
634                    case EDIT_ARTICLE:
635                            if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
636                                    p_articles[selected_index]->uid != BBS_priv.uid)
637                            {
638                                    break; // No permission
639                            }
640                            if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)
641                            {
642                                    log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);
643                            }
644                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
645                            {
646                                    log_error("section_list_draw_screen() error\n");
647                                    return -2;
648                            }
649                            break;
650                    case DELETE_ARTICLE:
651                            if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
652                                    (!checkpriv(&BBS_priv, p_section->sid, S_MAN_S) && p_articles[selected_index]->uid != BBS_priv.uid))
653                            {
654                                    break; // No permission
655                            }
656                            if (article_del(p_section, p_articles[selected_index]) < 0)
657                            {
658                                    log_error("article_del(aid=%d) error\n", p_articles[selected_index]->aid);
659                            }
660                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
661                            {
662                                    log_error("section_list_draw_screen() error\n");
663                                    return -2;
664                            }
665                            break;
666                    case SHOW_HELP:
667                            // Display help information
668                            display_file(DATA_READ_HELP, 1);
669                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
670                            {
671                                    log_error("section_list_draw_screen() error\n");
672                                  return -2;                                  return -2;
673                          }                          }
674                          break;                          break;


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1