/[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.3 by sysadm, Wed May 28 10:27:12 2025 UTC Revision 1.11 by sysadm, Sat May 31 03:37:47 2025 UTC
# Line 16  Line 16 
16    
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"
20  #include "common.h"  #include "common.h"
21  #include "io.h"  #include "io.h"
22  #include "screen.h"  #include "screen.h"
23  #include "log.h"  #include "log.h"
24  #include "str_process.h"  #include "str_process.h"
25  #include <time.h>  #include <time.h>
26    #include <sys/param.h>
27  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
28  #include <string.h>  #include <string.h>
29    
30    static int section_topic_view_mode = 0;
31    
32  enum select_cmd_t  enum select_cmd_t
33  {  {
34          EXIT_SECTION = 0,          EXIT_SECTION = 0,
35          VIEW_ARTICLE = 1,          VIEW_ARTICLE = 1,
36          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
37          REFRESH_SCREEN = 3,          REFRESH_SCREEN = 3,
38            CHANGE_NAME_DISPLAY = 4,
39  };  };
40    
41  static int section_list_draw_items(SECTION_LIST *p_section, int page_id, ARTICLE *p_articles[], int article_count)  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname)
42  {  {
43          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
44          struct tm tm_sub;          struct tm tm_sub;
# Line 72  static int section_list_draw_items(SECTI Line 77  static int section_list_draw_items(SECTI
77                          strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);                          strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);
78                  }                  }
79    
80                  strncpy(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1);                  strncpy(title_f, (p_articles[i]->tid == 0 ? "● " : ""), sizeof(title_f) - 1);
81                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
82                    strncat(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
83                  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)));
84                  len = split_line(title_f, (p_articles[i]->tid == 0 ? 46 : 49), &eol, &title_f_len);                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len);
85                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
86                  {                  {
87                          title_f[len] = '\0';                          title_f[len] = '\0';
88                  }                  }
89    
90                  moveto(4 + i, 1);                  moveto(4 + i, 1);
91                  prints("  %7d %c %s%*s %s %s%s",                  prints("  %7d %c %s%*s %s %s",
92                             p_articles[i]->aid,                             p_articles[i]->aid,
93                             article_flag,                             article_flag,
94                             p_articles[i]->username,                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
95                             BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username)),                             (display_nickname ? BBS_nickname_max_len - (int)strnlen(p_articles[i]->nickname, sizeof(p_articles[i]->nickname))
96                                                                     : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),
97                             "",                             "",
98                             str_time,                             str_time,
                            (p_articles[i]->tid == 0 ? "● " : ""),  
99                             title_f);                             title_f);
100          }          }
101    
102          return 0;          return 0;
103  }  }
104    
105  static int section_list_draw_screen(SECTION_LIST *p_section)  static int section_list_draw_screen(const char *sname, const char *stitle, const char *master_list, int display_nickname)
106  {  {
107          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";
108          char str_section_name[LINE_BUFFER_LEN];          char str_section_name[LINE_BUFFER_LEN];
109    
110          if (p_section->master_list[0] != '\0')          if (master_list[0] != '\0')
111          {          {
112                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", p_section->master_list);                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", master_list);
113          }          }
114          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", p_section->sname);          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", sname);
115    
116          clearscr();          clearscr();
117          show_top(str_section_master, p_section->stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
118          moveto(2, 0);          moveto(2, 0);
119          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] 阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m");          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] "
120                       "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m %s[\033[1;32mn\033[0;37m]\033[m",
121                       (display_nickname ? "显示用户名" : "显示昵称"));
122          moveto(3, 0);          moveto(3, 0);
123          prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文 章 标 题                                    \033[m");          if (display_nickname)
124          show_bottom("");          {
125                    prints("\033[44;37m  \033[1;37m 编  号   发布者昵称           日  期  文章标题                               \033[m");
126            }
127            else
128            {
129                    prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文章标题                                       \033[m");
130            }
131    
132          return 0;          return 0;
133  }  }
# Line 149  static enum select_cmd_t section_list_se Line 163  static enum select_cmd_t section_list_se
163                                  return EXIT_SECTION; // exit section                                  return EXIT_SECTION; // exit section
164                          }                          }
165                          continue;                          continue;
166                    case 'n':
167                            return CHANGE_NAME_DISPLAY;
168                  case CR:                  case CR:
169                          igetch_reset();                          igetch_reset();
170                  case KEY_RIGHT:                  case KEY_RIGHT:
# Line 230  static enum select_cmd_t section_list_se Line 246  static enum select_cmd_t section_list_se
246          return EXIT_SECTION;          return EXIT_SECTION;
247  }  }
248    
249    static int display_article_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
250    {
251            switch (*p_key)
252            {
253            case 'p':
254                    section_topic_view_mode = !section_topic_view_mode;
255            case 0: // Set msg
256                    if (section_topic_view_mode)
257                    {
258                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
259                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "
260                                             "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] │ "
261                                             "帮助[\033[32mh\033[33m] |");
262                    }
263                    else
264                    {
265                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
266                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "
267                                             "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ "
268                                             "帮助[\033[32mh\033[33m] |");
269                    }
270                    *p_key = 0;
271                    break;
272            }
273    
274            return 0;
275    }
276    
277  int section_list_display(const char *sname)  int section_list_display(const char *sname)
278  {  {
279            static int display_nickname = 0;
280    
281          SECTION_LIST *p_section;          SECTION_LIST *p_section;
282            char stitle[BBS_section_title_max_len + 1];
283            char master_list[(BBS_username_max_len + 1) * 3 + 1];
284            char page_info_str[LINE_BUFFER_LEN];
285          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
286          int article_count;          int article_count;
287            int page_count;
288          int page_id = 0;          int page_id = 0;
289          int selected_index = 0;          int selected_index = 0;
290            ARTICLE_CACHE cache;
291          int ret;          int ret;
292    
293          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
# Line 246  int section_list_display(const char *sna Line 297  int section_list_display(const char *sna
297                  return -1;                  return -1;
298          }          }
299    
300          if (section_list_draw_screen(p_section) < 0)          if ((ret = section_list_rd_lock(p_section)) < 0)
301            {
302                    log_error("section_list_rd_lock(sid = 0) error\n");
303                    return -2;
304            }
305    
306            strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);
307            stitle[sizeof(stitle) - 1] = '\0';
308            strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);
309            master_list[sizeof(master_list) - 1] = '\0';
310    
311            if ((ret = section_list_rd_unlock(p_section)) < 0)
312            {
313                    log_error("section_list_rd_unlock(sid = 0) error\n");
314                    return -2;
315            }
316    
317            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
318          {          {
319                  log_error("section_list_draw_screen() error\n");                  log_error("section_list_draw_screen() error\n");
320                  return -2;                  return -2;
321          }          }
322    
323          if (p_section->visible_article_count > 0)          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
324            if (ret < 0)
325          {          {
326                  ret = query_section_articles(p_section, page_id, p_articles, &article_count);                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
327                  if (ret < 0)                  return -3;
                 {  
                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                         return -3;  
                 }  
328          }          }
329          else // empty section  
330            if (article_count == 0) // empty section
331          {          {
                 article_count = 0;  
332                  selected_index = 0;                  selected_index = 0;
333          }          }
334    
335          while (!SYS_server_exit)          while (!SYS_server_exit)
336          {          {
337                  ret = section_list_draw_items(p_section, page_id, p_articles, article_count);                  ret = section_list_draw_items(page_id, p_articles, article_count, display_nickname);
338                  if (ret < 0)                  if (ret < 0)
339                  {                  {
340                          log_error("section_list_draw_items(sid=%d, page_id=%d) error\n", p_section->sid, page_id);                          log_error("section_list_draw_items(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
341                          return -4;                          return -4;
342                  }                  }
343    
344                    snprintf(page_info_str, sizeof(page_info_str),
345                                     "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
346                                     page_id + 1, MAX(page_count, 1));
347    
348                    show_bottom(page_info_str);
349                  iflush();                  iflush();
350    
351                  ret = section_list_select(p_section->page_count, article_count, &page_id, &selected_index);                  ret = section_list_select(page_count, article_count, &page_id, &selected_index);
352                  switch (ret)                  switch (ret)
353                  {                  {
354                  case EXIT_SECTION:                  case EXIT_SECTION:
355                          return 0;                          return 0;
356                  case CHANGE_PAGE:                  case CHANGE_PAGE:
357                          if (p_section->visible_article_count > 0)                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
358                            if (ret < 0)
359                          {                          {
360                                  ret = query_section_articles(p_section, page_id, p_articles, &article_count);                                  log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
361                                  if (ret < 0)                                  return -3;
                                 {  
                                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                                         return -3;  
                                 }  
362                          }                          }
363                          else // empty section                          if (article_count == 0) // empty section
364                          {                          {
                                 article_count = 0;  
365                                  selected_index = 0;                                  selected_index = 0;
366                          }                          }
367                          if (selected_index >= article_count)                          else if (selected_index >= article_count)
368                          {                          {
369                                  selected_index = article_count - 1;                                  selected_index = article_count - 1;
370                          }                          }
371                          break;                          break;
372                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
373                          log_std("Debug: article %d selected\n", p_articles[selected_index]->aid);                          ret = article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]);
374                            if (ret < 0)
375                            {
376                                    log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
377                                    break;
378                            }
379    
380                            ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 1, 0,
381                                                               display_article_key_handler, DATA_READ_HELP);
382                            if (ret < 0)
383                            {
384                                    log_error("display_data(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
385                                    break;
386                            }
387    
388                            ret = article_cache_unload(&cache);
389                            if (ret < 0)
390                            {
391                                    log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
392                                    break;
393                            }
394    
395                            // TODO: locate last viewed article
396                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
397                          if (section_list_draw_screen(p_section) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
398                            {
399                                    log_error("section_list_draw_screen() error\n");
400                                    return -2;
401                            }
402                            break;
403                    case CHANGE_NAME_DISPLAY:
404                            display_nickname = !display_nickname;
405                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
406                          {                          {
407                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
408                                  return -2;                                  return -2;


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

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