/[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.15 by sysadm, Sat May 31 14:59:18 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    static int section_topic_view_tid = -1;
32    
33  enum select_cmd_t  enum select_cmd_t
34  {  {
35          EXIT_SECTION = 0,          EXIT_SECTION = 0,
36          VIEW_ARTICLE = 1,          VIEW_ARTICLE = 1,
37          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
38          REFRESH_SCREEN = 3,          REFRESH_SCREEN = 3,
39            CHANGE_NAME_DISPLAY = 4,
40  };  };
41    
42  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)
43  {  {
44          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
45          struct tm tm_sub;          struct tm tm_sub;
# Line 72  static int section_list_draw_items(SECTI Line 78  static int section_list_draw_items(SECTI
78                          strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);                          strftime(str_time, sizeof(str_time), "%m/%Y", &tm_sub);
79                  }                  }
80    
81                  strncpy(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1);                  strncpy(title_f, (p_articles[i]->tid == 0 ? "● " : ""), sizeof(title_f) - 1);
82                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
83                    strncat(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
84                  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)));
85                  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);
86                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
87                  {                  {
88                          title_f[len] = '\0';                          title_f[len] = '\0';
89                  }                  }
90    
91                  moveto(4 + i, 1);                  moveto(4 + i, 1);
92                  prints("  %7d %c %s%*s %s %s%s",                  prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",
93                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
94                             p_articles[i]->aid,                             p_articles[i]->aid,
95                             article_flag,                             article_flag,
96                             p_articles[i]->username,                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
97                             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))
98                                                                     : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),
99                             "",                             "",
100                             str_time,                             str_time,
101                             (p_articles[i]->tid == 0 ? "● " : ""),                             (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
102                             title_f);                             title_f);
103          }          }
104    
105          return 0;          return 0;
106  }  }
107    
108  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)
109  {  {
110          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";          char str_section_master[LINE_BUFFER_LEN] = "诚征版主中";
111          char str_section_name[LINE_BUFFER_LEN];          char str_section_name[LINE_BUFFER_LEN];
112    
113          if (p_section->master_list[0] != '\0')          if (master_list[0] != '\0')
114          {          {
115                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", p_section->master_list);                  snprintf(str_section_master, sizeof(str_section_master), "版主:%s", master_list);
116          }          }
117          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", p_section->sname);          snprintf(str_section_name, sizeof(str_section_name), "讨论区 [%s]", sname);
118    
119          clearscr();          clearscr();
120          show_top(str_section_master, p_section->stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
121          moveto(2, 0);          moveto(2, 0);
122          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] "
123                       "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m %s[\033[1;32mn\033[0;37m]\033[m",
124                       (display_nickname ? "显示用户名" : "显示昵称"));
125          moveto(3, 0);          moveto(3, 0);
126          prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文 章 标 题                                    \033[m");          if (display_nickname)
127          show_bottom("");          {
128                    prints("\033[44;37m  \033[1;37m 编  号   发布者昵称           日  期  文章标题                               \033[m");
129            }
130            else
131            {
132                    prints("\033[44;37m  \033[1;37m 编  号   发 布 者     日  期  文章标题                                       \033[m");
133            }
134    
135          return 0;          return 0;
136  }  }
# Line 149  static enum select_cmd_t section_list_se Line 166  static enum select_cmd_t section_list_se
166                                  return EXIT_SECTION; // exit section                                  return EXIT_SECTION; // exit section
167                          }                          }
168                          continue;                          continue;
169                    case 'n':
170                            return CHANGE_NAME_DISPLAY;
171                  case CR:                  case CR:
172                          igetch_reset();                          igetch_reset();
173                  case KEY_RIGHT:                  case KEY_RIGHT:
# Line 230  static enum select_cmd_t section_list_se Line 249  static enum select_cmd_t section_list_se
249          return EXIT_SECTION;          return EXIT_SECTION;
250  }  }
251    
252    static int display_article_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
253    {
254            switch (*p_key)
255            {
256            case 'p':
257                    section_topic_view_mode = !section_topic_view_mode;
258            case 0: // Set msg
259                    if (section_topic_view_mode)
260                    {
261                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
262                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
263                                             "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] | "
264                                             "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");
265                    }
266                    else
267                    {
268                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
269                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
270                                             "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
271                                             "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");
272                    }
273                    *p_key = 0;
274                    break;
275            case KEY_UP:
276                    if (p_ctx->reach_begin)
277                    {
278                            if (section_topic_view_mode)
279                            {
280                                    *p_key = KEY_PGUP;
281                            }
282                            return 1;
283                    }
284                    break;
285            case KEY_DOWN:
286                    if (p_ctx->reach_end)
287                    {
288                            if (section_topic_view_mode)
289                            {
290                                    *p_key = KEY_PGDN;
291                            }
292                            return 1;
293                    }
294                    break;
295            }
296    
297            return 0;
298    }
299    
300  int section_list_display(const char *sname)  int section_list_display(const char *sname)
301  {  {
302            static int display_nickname = 0;
303    
304          SECTION_LIST *p_section;          SECTION_LIST *p_section;
305            char stitle[BBS_section_title_max_len + 1];
306            char master_list[(BBS_username_max_len + 1) * 3 + 1];
307            char page_info_str[LINE_BUFFER_LEN];
308          ARTICLE *p_articles[BBS_article_limit_per_page];          ARTICLE *p_articles[BBS_article_limit_per_page];
309          int article_count;          int article_count;
310            int page_count;
311          int page_id = 0;          int page_id = 0;
312          int selected_index = 0;          int selected_index = 0;
313            ARTICLE_CACHE cache;
314          int ret;          int ret;
315            int loop;
316            int direction;
317    
318          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
319          if (p_section == NULL)          if (p_section == NULL)
# Line 246  int section_list_display(const char *sna Line 322  int section_list_display(const char *sna
322                  return -1;                  return -1;
323          }          }
324    
325          if (section_list_draw_screen(p_section) < 0)          if ((ret = section_list_rd_lock(p_section)) < 0)
326            {
327                    log_error("section_list_rd_lock(sid = 0) error\n");
328                    return -2;
329            }
330    
331            strncpy(stitle, p_section->stitle, sizeof(stitle) - 1);
332            stitle[sizeof(stitle) - 1] = '\0';
333            strncpy(master_list, p_section->master_list, sizeof(master_list) - 1);
334            master_list[sizeof(master_list) - 1] = '\0';
335    
336            if ((ret = section_list_rd_unlock(p_section)) < 0)
337            {
338                    log_error("section_list_rd_unlock(sid = 0) error\n");
339                    return -2;
340            }
341    
342            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
343          {          {
344                  log_error("section_list_draw_screen() error\n");                  log_error("section_list_draw_screen() error\n");
345                  return -2;                  return -2;
346          }          }
347    
348          if (p_section->visible_article_count > 0)          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
349            if (ret < 0)
350          {          {
351                  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);
352                  if (ret < 0)                  return -3;
                 {  
                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                         return -3;  
                 }  
353          }          }
354          else // empty section  
355            if (article_count == 0) // empty section
356          {          {
                 article_count = 0;  
357                  selected_index = 0;                  selected_index = 0;
358          }          }
359    
360          while (!SYS_server_exit)          while (!SYS_server_exit)
361          {          {
362                  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);
363                  if (ret < 0)                  if (ret < 0)
364                  {                  {
365                          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);
366                          return -4;                          return -4;
367                  }                  }
368    
369                    snprintf(page_info_str, sizeof(page_info_str),
370                                     "\033[33m[第\033[36m%d\033[33m/\033[36m%d\033[33m页]",
371                                     page_id + 1, MAX(page_count, 1));
372    
373                    show_bottom(page_info_str);
374                  iflush();                  iflush();
375    
376                  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);
377                  switch (ret)                  switch (ret)
378                  {                  {
379                  case EXIT_SECTION:                  case EXIT_SECTION:
380                          return 0;                          return 0;
381                  case CHANGE_PAGE:                  case CHANGE_PAGE:
382                          if (p_section->visible_article_count > 0)                          ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
383                            if (ret < 0)
384                          {                          {
385                                  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);
386                                  if (ret < 0)                                  return -3;
                                 {  
                                         log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);  
                                         return -3;  
                                 }  
387                          }                          }
388                          else // empty section                          if (article_count == 0) // empty section
389                          {                          {
                                 article_count = 0;  
390                                  selected_index = 0;                                  selected_index = 0;
391                          }                          }
392                          if (selected_index >= article_count)                          else if (selected_index >= article_count)
393                          {                          {
394                                  selected_index = article_count - 1;                                  selected_index = article_count - 1;
395                          }                          }
396                          break;                          break;
397                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
398                          log_std("Debug: article %d selected\n", p_articles[selected_index]->aid);                          do
399                            {
400                                    loop = 0;
401    
402                                    if (article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]) < 0)
403                                    {
404                                            log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
405                                            break;
406                                    }
407    
408                                    ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 1, 0,
409                                                                       display_article_key_handler, DATA_READ_HELP);
410    
411                                    if (article_cache_unload(&cache) < 0)
412                                    {
413                                            log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
414                                            break;
415                                    }
416    
417                                    switch (ret)
418                                    {
419                                    case KEY_UP:
420                                            if (selected_index <= 0)
421                                            {
422                                                    if (page_id > 0)
423                                                    {
424                                                            page_id--;
425                                                            selected_index = BBS_article_limit_per_page - 1;
426    
427                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
428                                                            if (ret < 0)
429                                                            {
430                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
431                                                                    return -3;
432                                                            }
433    
434                                                            if (article_count != BBS_article_limit_per_page) // page is not full
435                                                            {
436                                                                    selected_index = MAX(0, article_count - 1);
437                                                            }
438                                                            else
439                                                            {
440                                                                    loop = 1;
441                                                            }
442                                                    }
443                                            }
444                                            else
445                                            {
446                                                    selected_index--;
447                                                    loop = 1;
448                                            }
449                                            break;
450                                    case KEY_DOWN:
451                                            if (selected_index + 1 >= article_count) // next page
452                                            {
453                                                    if (page_id + 1 < page_count)
454                                                    {
455                                                            page_id++;
456                                                            selected_index = 0;
457    
458                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
459                                                            if (ret < 0)
460                                                            {
461                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
462                                                                    return -3;
463                                                            }
464    
465                                                            if (article_count == 0) // empty page
466                                                            {
467                                                                    selected_index = 0;
468                                                            }
469                                                            else
470                                                            {
471                                                                    loop = 1;
472                                                            }
473                                                    }
474                                            }
475                                            else
476                                            {
477                                                    selected_index++;
478                                                    loop = 1;
479                                            }
480                                            break;
481                                    case KEY_PGUP:
482                                    case KEY_PGDN:
483                                            direction = (ret == KEY_PGUP ? -1 : 1);
484                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, &page_id, &selected_index, &page_count);
485                                            if (ret < 0)
486                                            {
487                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d) error\n",
488                                                                      p_section->sid, p_articles[selected_index]->aid, direction);
489                                                    return -3;
490                                            }
491                                            else if (ret > 0)
492                                            {
493                                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
494                                                    if (ret < 0)
495                                                    {
496                                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
497                                                            return -3;
498                                                    }
499                                                    loop = 1;
500                                            }
501                                            break;
502                                    }
503                            } while (loop);
504    
505                            // Update current topic
506                            section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
507                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
508                          if (section_list_draw_screen(p_section) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
509                            {
510                                    log_error("section_list_draw_screen() error\n");
511                                    return -2;
512                            }
513                            break;
514                    case CHANGE_NAME_DISPLAY:
515                            display_nickname = !display_nickname;
516                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
517                          {                          {
518                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
519                                  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