/[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.1 by sysadm, Wed May 28 07:30:23 2025 UTC Revision 1.10 by sysadm, Fri May 30 12:51:00 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 ? 40 : 37), &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 205  static enum select_cmd_t section_list_se Line 244  static enum select_cmd_t section_list_se
244          return EXIT_SECTION;          return EXIT_SECTION;
245  }  }
246    
247    static int display_article_key_handler(int *key, char *msg, size_t msg_len)
248    {
249            static int topic_view = 0;
250    
251            switch (*key)
252            {
253            case 0: // Set msg
254                    snprintf(msg, msg_len,
255                                     "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "
256                                     "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ "
257                                     "帮助[\033[32mh\033[33m] |");
258                    break;
259            case 'p':
260                    break;
261                    topic_view = !topic_view;
262                    if (topic_view)
263                    {
264                            snprintf(msg, msg_len,
265                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "
266                                             "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] │ "
267                                             "帮助[\033[32mh\033[33m] |");
268                    }
269                    else
270                    {
271                            snprintf(msg, msg_len,
272                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "
273                                             "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ "
274                                             "帮助[\033[32mh\033[33m] |");
275                    }
276                    *key = 0;
277                    break;
278            case 'H':
279                    *key = 'h';
280                    return 0;
281            }
282    
283            return 0;
284    }
285    
286  int section_list_display(const char *sname)  int section_list_display(const char *sname)
287  {  {
288            static int display_nickname = 0;
289    
290          SECTION_LIST *p_section;          SECTION_LIST *p_section;
291            char stitle[BBS_section_title_max_len + 1];
292            char master_list[(BBS_username_max_len + 1) * 3 + 1];
293            char page_info_str[LINE_BUFFER_LEN];
294          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
295          int article_count;          int article_count;
296            int page_count;
297          int page_id = 0;          int page_id = 0;
298          int selected_index = 0;          int selected_index = 0;
299            ARTICLE_CACHE cache;
300          int ret;          int ret;
301    
302          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 306  int section_list_display(const char *sna
306                  return -1;                  return -1;
307          }          }
308    
309          if (section_list_draw_screen(p_section) < 0)          if ((ret = section_list_rd_lock(p_section)) < 0)
310            {
311                    log_error("section_list_rd_lock(sid = 0) error\n");
312                    return -2;
313            }
314    
315            strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);
316            stitle[sizeof(stitle) - 1] = '\0';
317            strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);
318            master_list[sizeof(master_list) - 1] = '\0';
319    
320            if ((ret = section_list_rd_unlock(p_section)) < 0)
321            {
322                    log_error("section_list_rd_unlock(sid = 0) error\n");
323                    return -2;
324            }
325    
326            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
327          {          {
328                  log_error("section_list_draw_screen() error\n");                  log_error("section_list_draw_screen() error\n");
329                  return -2;                  return -2;
330          }          }
331    
332          if (p_section->visible_article_count > 0)          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
333            if (ret < 0)
334          {          {
335                  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);
336                  if (ret < 0)                  return -3;
                 {  
                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                         return -3;  
                 }  
337          }          }
338          else // empty section  
339            if (article_count == 0) // empty section
340          {          {
                 article_count = 0;  
341                  selected_index = 0;                  selected_index = 0;
342          }          }
343    
344          while (!SYS_server_exit)          while (!SYS_server_exit)
345          {          {
346                  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);
347                  if (ret < 0)                  if (ret < 0)
348                  {                  {
349                          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);
350                          return -4;                          return -4;
351                  }                  }
352    
353                    snprintf(page_info_str, sizeof(page_info_str),
354                                     "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
355                                     page_id + 1, MAX(page_count, 1));
356    
357                    show_bottom(page_info_str);
358                  iflush();                  iflush();
359    
360                  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);
361                  switch (ret)                  switch (ret)
362                  {                  {
363                  case EXIT_SECTION:                  case EXIT_SECTION:
364                          return 0;                          return 0;
365                  case CHANGE_PAGE:                  case CHANGE_PAGE:
366                          if (p_section->visible_article_count > 0)                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
367                            if (ret < 0)
368                          {                          {
369                                  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);
370                                  if (ret < 0)                                  return -3;
                                 {  
                                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                                         return -3;  
                                 }  
371                          }                          }
372                          else // empty section                          if (article_count == 0) // empty section
373                          {                          {
                                 article_count = 0;  
374                                  selected_index = 0;                                  selected_index = 0;
375                          }                          }
376                          if (selected_index >= article_count)                          else if (selected_index >= article_count)
377                          {                          {
378                                  selected_index = article_count - 1;                                  selected_index = article_count - 1;
379                          }                          }
380                          break;                          break;
381                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
382                          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]);
383                            if (ret < 0)
384                            {
385                                    log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
386                                    break;
387                            }
388    
389                            ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 1, 0,
390                                                               display_article_key_handler, DATA_READ_HELP);
391                            if (ret < 0)
392                            {
393                                    log_error("display_data(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
394                                    break;
395                            }
396    
397                            ret = article_cache_unload(&cache);
398                            if (ret < 0)
399                            {
400                                    log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
401                                    break;
402                            }
403    
404                            // TODO: locate last viewed article
405                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
406                          if (section_list_draw_screen(p_section) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
407                            {
408                                    log_error("section_list_draw_screen() error\n");
409                                    return -2;
410                            }
411                            break;
412                    case CHANGE_NAME_DISPLAY:
413                            display_nickname = !display_nickname;
414                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
415                          {                          {
416                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
417                                  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