/[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.2 by sysadm, Wed May 28 07:41:46 2025 UTC Revision 1.9 by sysadm, Thu May 29 09:44:01 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    
# Line 31  enum select_cmd_t Line 33  enum select_cmd_t
33          VIEW_ARTICLE = 1,          VIEW_ARTICLE = 1,
34          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
35          REFRESH_SCREEN = 3,          REFRESH_SCREEN = 3,
36            CHANGE_NAME_DISPLAY = 4,
37  };  };
38    
39  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)
40  {  {
41          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
42          struct tm tm_sub;          struct tm tm_sub;
# Line 42  static int section_list_draw_items(SECTI Line 45  static int section_list_draw_items(SECTI
45          int eol;          int eol;
46          int len;          int len;
47          int i;          int i;
48            char article_flag;
49            time_t tm_now;
50    
51            time(&tm_now);
52    
53          clrline(4, 23);          clrline(4, 23);
54    
55          for (i = 0; i < article_count; i++)          for (i = 0; i < article_count; i++)
56          {          {
57                    article_flag = ' ';
58    
59                    if (p_articles[i]->excerption)
60                    {
61                            article_flag = 'm';
62                    }
63                    else if (p_articles[i]->lock)
64                    {
65                            article_flag = 'x';
66                    }
67    
68                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);
69                  strftime(str_time, sizeof(str_time), "%b %e", &tm_sub);                  if (tm_now - p_articles[i]->sub_dt < 3600 * 24 * 365)
70                  strncpy(title_f, p_articles[i]->title, sizeof(title_f) - 1);                  {
71                            strftime(str_time, sizeof(str_time), "%b %e ", &tm_sub);
72                    }
73                    else
74                    {
75                            strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);
76                    }
77    
78                    strncpy(title_f, (p_articles[i]->tid == 0 ? "● " : ""), sizeof(title_f) - 1);
79                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
80                  len = split_line(title_f, (p_articles[i]->tid == 0 ? 46 : 49), &eol, &title_f_len);                  strncat(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
81                    strncat(title_f, p_articles[i]->title, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
82                    len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len);
83                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
84                  {                  {
85                          title_f[len] = '\0';                          title_f[len] = '\0';
86                  }                  }
87    
88                  moveto(4 + i, 1);                  moveto(4 + i, 1);
89                  prints("  %7d %s%*s %s  %s%s",                  prints("  %7d %c %s%*s %s %s",
90                             p_articles[i]->aid,                             p_articles[i]->aid,
91                             p_articles[i]->username,                             article_flag,
92                             BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username)),                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
93                               (display_nickname ? BBS_nickname_max_len - (int)strnlen(p_articles[i]->nickname, sizeof(p_articles[i]->nickname))
94                                                                     : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),
95                             "",                             "",
96                             str_time,                             str_time,
                            (p_articles[i]->tid == 0 ? "● " : ""),  
97                             title_f);                             title_f);
98          }          }
99    
100          return 0;          return 0;
101  }  }
102    
103  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)
104  {  {
105          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";
106          char str_section_name[LINE_BUFFER_LEN];          char str_section_name[LINE_BUFFER_LEN];
107    
108          if (p_section->master_list[0] != '\0')          if (master_list[0] != '\0')
109          {          {
110                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", p_section->master_list);                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", master_list);
111          }          }
112          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", p_section->sname);          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", sname);
113    
114          clearscr();          clearscr();
115          show_top(str_section_master, p_section->stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
116          moveto(2, 0);          moveto(2, 0);
117          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] "
118                       "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m %s[\033[1;32mn\033[0;37m]\033[m",
119                       (display_nickname ? "显示用户名" : "显示昵称"));
120          moveto(3, 0);          moveto(3, 0);
121          prints("\033[44;37m  \033[1;37m 编 号  发 布 者     日  期  文 章 标 题                                      \033[m");          if (display_nickname)
122          show_bottom("");          {
123                    prints("\033[44;37m  \033[1;37m 编  号   发布者昵称           日  期  文章标题                               \033[m");
124            }
125            else
126            {
127                    prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文章标题                                       \033[m");
128            }
129    
130          return 0;          return 0;
131  }  }
# Line 124  static enum select_cmd_t section_list_se Line 161  static enum select_cmd_t section_list_se
161                                  return EXIT_SECTION; // exit section                                  return EXIT_SECTION; // exit section
162                          }                          }
163                          continue;                          continue;
164                    case 'n':
165                            return CHANGE_NAME_DISPLAY;
166                  case CR:                  case CR:
167                          igetch_reset();                          igetch_reset();
168                  case KEY_RIGHT:                  case KEY_RIGHT:
# Line 207  static enum select_cmd_t section_list_se Line 246  static enum select_cmd_t section_list_se
246    
247  int section_list_display(const char *sname)  int section_list_display(const char *sname)
248  {  {
249            static int display_nickname = 0;
250    
251          SECTION_LIST *p_section;          SECTION_LIST *p_section;
252            char stitle[BBS_section_title_max_len + 1];
253            char master_list[(BBS_username_max_len + 1) * 3 + 1];
254            char page_info_str[LINE_BUFFER_LEN];
255          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
256          int article_count;          int article_count;
257            int page_count;
258          int page_id = 0;          int page_id = 0;
259          int selected_index = 0;          int selected_index = 0;
260            ARTICLE_CACHE article_cache;
261          int ret;          int ret;
262    
263          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
# Line 221  int section_list_display(const char *sna Line 267  int section_list_display(const char *sna
267                  return -1;                  return -1;
268          }          }
269    
270          if (section_list_draw_screen(p_section) < 0)          if ((ret = section_list_rd_lock(p_section)) < 0)
271            {
272                    log_error("section_list_rd_lock(sid = 0) error\n");
273                    return -2;
274            }
275    
276            strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);
277            stitle[sizeof(stitle) - 1] = '\0';
278            strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);
279            master_list[sizeof(master_list) - 1] = '\0';
280    
281            if ((ret = section_list_rd_unlock(p_section)) < 0)
282            {
283                    log_error("section_list_rd_unlock(sid = 0) error\n");
284                    return -2;
285            }
286    
287            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
288          {          {
289                  log_error("section_list_draw_screen() error\n");                  log_error("section_list_draw_screen() error\n");
290                  return -2;                  return -2;
291          }          }
292    
293          if (p_section->visible_article_count > 0)          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
294            if (ret < 0)
295          {          {
296                  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);
297                  if (ret < 0)                  return -3;
                 {  
                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                         return -3;  
                 }  
298          }          }
299          else // empty section  
300            if (article_count == 0) // empty section
301          {          {
                 article_count = 0;  
302                  selected_index = 0;                  selected_index = 0;
303          }          }
304    
305          while (!SYS_server_exit)          while (!SYS_server_exit)
306          {          {
307                  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);
308                  if (ret < 0)                  if (ret < 0)
309                  {                  {
310                          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);
311                          return -4;                          return -4;
312                  }                  }
313    
314                    snprintf(page_info_str, sizeof(page_info_str),
315                                     "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
316                                     page_id + 1, MAX(page_count, 1));
317    
318                    show_bottom(page_info_str);
319                  iflush();                  iflush();
320    
321                  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);
322                  switch (ret)                  switch (ret)
323                  {                  {
324                  case EXIT_SECTION:                  case EXIT_SECTION:
325                          return 0;                          return 0;
326                  case CHANGE_PAGE:                  case CHANGE_PAGE:
327                          if (p_section->visible_article_count > 0)                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
328                            if (ret < 0)
329                          {                          {
330                                  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);
331                                  if (ret < 0)                                  return -3;
                                 {  
                                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                                         return -3;  
                                 }  
332                          }                          }
333                          else // empty section                          if (article_count == 0) // empty section
334                          {                          {
                                 article_count = 0;  
335                                  selected_index = 0;                                  selected_index = 0;
336                          }                          }
337                          if (selected_index >= article_count)                          else if (selected_index >= article_count)
338                          {                          {
339                                  selected_index = article_count - 1;                                  selected_index = article_count - 1;
340                          }                          }
341                          break;                          break;
342                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
343                          log_std("Debug: article %d selected\n", p_articles[selected_index]->aid);                          ret = article_cache_load(&article_cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]);
344                            if (ret < 0)
345                            {
346                                    log_error("article_cache_load(aid=%d, cid=%d)\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
347                                    break;
348                            }
349    
350                            log_std("Debug: view article aid = %d, cid = %d\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
351    
352                            ret = article_cache_unload(&article_cache);
353                            if (ret < 0)
354                            {
355                                    log_error("article_cache_unload(aid=%d, cid=%d)\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
356                                    break;
357                            }
358    
359                            // TODO: locate last viewed article
360                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
361                          if (section_list_draw_screen(p_section) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
362                            {
363                                    log_error("section_list_draw_screen() error\n");
364                                    return -2;
365                            }
366                            break;
367                    case CHANGE_NAME_DISPLAY:
368                            display_nickname = !display_nickname;
369                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
370                          {                          {
371                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
372                                  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