/[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.12 by sysadm, Sat May 31 08:54:03 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 42  static int section_list_draw_items(SECTI Line 47  static int section_list_draw_items(SECTI
47          int eol;          int eol;
48          int len;          int len;
49          int i;          int i;
50            char article_flag;
51            time_t tm_now;
52    
53            time(&tm_now);
54    
55          clrline(4, 23);          clrline(4, 23);
56    
57          for (i = 0; i < article_count; i++)          for (i = 0; i < article_count; i++)
58          {          {
59                    article_flag = ' ';
60    
61                    if (p_articles[i]->excerption)
62                    {
63                            article_flag = 'm';
64                    }
65                    else if (p_articles[i]->lock)
66                    {
67                            article_flag = 'x';
68                    }
69    
70                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);                  localtime_r(&p_articles[i]->sub_dt, &tm_sub);
71                  strftime(str_time, sizeof(str_time), "%b %e", &tm_sub);                  if (tm_now - p_articles[i]->sub_dt < 3600 * 24 * 365)
72                  strncpy(title_f, p_articles[i]->title, sizeof(title_f) - 1);                  {
73                            strftime(str_time, sizeof(str_time), "%b %e ", &tm_sub);
74                    }
75                    else
76                    {
77                            strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);
78                    }
79    
80                    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                  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)));
83                    strncat(title_f, p_articles[i]->title, sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
84                    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 %s%*s %s  %s%s",                  prints("  %7d %c %s%*s %s %s",
92                             p_articles[i]->aid,                             p_articles[i]->aid,
93                             p_articles[i]->username,                             article_flag,
94                             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),
95                               (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 124  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 205  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            case KEY_UP:
273                    if (p_ctx->reach_begin)
274                    {
275                            if (section_topic_view_mode)
276                            {
277                                    *p_key = KEY_PGUP;
278                            }
279                            return 1;
280                    }
281                    break;
282            case KEY_DOWN:
283                    if (p_ctx->reach_end)
284                    {
285                            if (section_topic_view_mode)
286                            {
287                                    *p_key = KEY_PGDN;
288                            }
289                            return 1;
290                    }
291                    break;
292            }
293    
294            return 0;
295    }
296    
297  int section_list_display(const char *sname)  int section_list_display(const char *sname)
298  {  {
299            static int display_nickname = 0;
300    
301          SECTION_LIST *p_section;          SECTION_LIST *p_section;
302            char stitle[BBS_section_title_max_len + 1];
303            char master_list[(BBS_username_max_len + 1) * 3 + 1];
304            char page_info_str[LINE_BUFFER_LEN];
305          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
306          int article_count;          int article_count;
307            int page_count;
308          int page_id = 0;          int page_id = 0;
309          int selected_index = 0;          int selected_index = 0;
310            ARTICLE_CACHE cache;
311          int ret;          int ret;
312            int loop;
313            int direction;
314    
315          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
316          if (p_section == NULL)          if (p_section == NULL)
# Line 221  int section_list_display(const char *sna Line 319  int section_list_display(const char *sna
319                  return -1;                  return -1;
320          }          }
321    
322          if (section_list_draw_screen(p_section) < 0)          if ((ret = section_list_rd_lock(p_section)) < 0)
323            {
324                    log_error("section_list_rd_lock(sid = 0) error\n");
325                    return -2;
326            }
327    
328            strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);
329            stitle[sizeof(stitle) - 1] = '\0';
330            strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);
331            master_list[sizeof(master_list) - 1] = '\0';
332    
333            if ((ret = section_list_rd_unlock(p_section)) < 0)
334            {
335                    log_error("section_list_rd_unlock(sid = 0) error\n");
336                    return -2;
337            }
338    
339            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
340          {          {
341                  log_error("section_list_draw_screen() error\n");                  log_error("section_list_draw_screen() error\n");
342                  return -2;                  return -2;
343          }          }
344    
345          if (p_section->visible_article_count > 0)          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
346            if (ret < 0)
347          {          {
348                  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);
349                  if (ret < 0)                  return -3;
                 {  
                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                         return -3;  
                 }  
350          }          }
351          else // empty section  
352            if (article_count == 0) // empty section
353          {          {
                 article_count = 0;  
354                  selected_index = 0;                  selected_index = 0;
355          }          }
356    
357          while (!SYS_server_exit)          while (!SYS_server_exit)
358          {          {
359                  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);
360                  if (ret < 0)                  if (ret < 0)
361                  {                  {
362                          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);
363                          return -4;                          return -4;
364                  }                  }
365    
366                    snprintf(page_info_str, sizeof(page_info_str),
367                                     "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
368                                     page_id + 1, MAX(page_count, 1));
369    
370                    show_bottom(page_info_str);
371                  iflush();                  iflush();
372    
373                  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);
374                  switch (ret)                  switch (ret)
375                  {                  {
376                  case EXIT_SECTION:                  case EXIT_SECTION:
377                          return 0;                          return 0;
378                  case CHANGE_PAGE:                  case CHANGE_PAGE:
379                          if (p_section->visible_article_count > 0)                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
380                            if (ret < 0)
381                          {                          {
382                                  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);
383                                  if (ret < 0)                                  return -3;
                                 {  
                                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                                         return -3;  
                                 }  
384                          }                          }
385                          else // empty section                          if (article_count == 0) // empty section
386                          {                          {
                                 article_count = 0;  
387                                  selected_index = 0;                                  selected_index = 0;
388                          }                          }
389                          if (selected_index >= article_count)                          else if (selected_index >= article_count)
390                          {                          {
391                                  selected_index = article_count - 1;                                  selected_index = article_count - 1;
392                          }                          }
393                          break;                          break;
394                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
395                          log_std("Debug: article %d selected\n", p_articles[selected_index]->aid);                          do
396                            {
397                                    loop = 0;
398    
399                                    if (article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]) < 0)
400                                    {
401                                            log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
402                                            break;
403                                    }
404    
405                                    ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 1, 0,
406                                                                       display_article_key_handler, DATA_READ_HELP);
407    
408                                    if (article_cache_unload(&cache) < 0)
409                                    {
410                                            log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
411                                            break;
412                                    }
413    
414                                    switch (ret)
415                                    {
416                                    case KEY_UP:
417                                            if (selected_index <= 0)
418                                            {
419                                                    if (page_id > 0)
420                                                    {
421                                                            page_id--;
422                                                            selected_index = BBS_article_limit_per_page - 1;
423    
424                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
425                                                            if (ret < 0)
426                                                            {
427                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
428                                                                    return -3;
429                                                            }
430    
431                                                            if (article_count != BBS_article_limit_per_page) // page is not full
432                                                            {
433                                                                    selected_index = MAX(0, article_count - 1);
434                                                            }
435                                                            else
436                                                            {
437                                                                    loop = 1;
438                                                            }
439                                                    }
440                                            }
441                                            else
442                                            {
443                                                    selected_index--;
444                                                    loop = 1;
445                                            }
446                                            break;
447                                    case KEY_DOWN:
448                                            if (selected_index + 1 >= article_count) // next page
449                                            {
450                                                    if (page_id + 1 < page_count)
451                                                    {
452                                                            page_id++;
453                                                            selected_index = 0;
454    
455                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
456                                                            if (ret < 0)
457                                                            {
458                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
459                                                                    return -3;
460                                                            }
461    
462                                                            if (article_count == 0) // empty page
463                                                            {
464                                                                    selected_index = 0;
465                                                            }
466                                                            else
467                                                            {
468                                                                    loop = 1;
469                                                            }
470                                                    }
471                                            }
472                                            else
473                                            {
474                                                    selected_index++;
475                                                    loop = 1;
476                                            }
477                                            break;
478                                    case KEY_PGUP:
479                                    case KEY_PGDN:
480                                            direction = (ret == KEY_PGUP ? -1 : 1);
481                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, &page_id, &selected_index, &page_count);
482                                            if (ret < 0)
483                                            {
484                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d) error\n",
485                                                                      p_section->sid, p_articles[selected_index]->aid, direction);
486                                                    return -3;
487                                            }
488                                            else if (ret > 0)
489                                            {
490                                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
491                                                    if (ret < 0)
492                                                    {
493                                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
494                                                            return -3;
495                                                    }
496                                                    loop = 1;
497                                            }
498                                            break;
499                                    }
500                            } while (loop);
501                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
502                          if (section_list_draw_screen(p_section) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
503                            {
504                                    log_error("section_list_draw_screen() error\n");
505                                    return -2;
506                            }
507                            break;
508                    case CHANGE_NAME_DISPLAY:
509                            display_nickname = !display_nickname;
510                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
511                          {                          {
512                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
513                                  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