/[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.10 by sysadm, Fri May 30 12:51:00 2025 UTC Revision 1.29 by sysadm, Mon Jun 16 14:30:44 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #define _POSIX_C_SOURCE 200809L
18    
19  #include "section_list_display.h"  #include "section_list_display.h"
20  #include "section_list_loader.h"  #include "section_list_loader.h"
21  #include "article_cache.h"  #include "article_cache.h"
22    #include "article_post.h"
23    #include "article_del.h"
24  #include "common.h"  #include "common.h"
25  #include "io.h"  #include "io.h"
26  #include "screen.h"  #include "screen.h"
27  #include "log.h"  #include "log.h"
28    #include "user_priv.h"
29    #include "article_view_log.h"
30  #include "str_process.h"  #include "str_process.h"
31    #include <string.h>
32  #include <time.h>  #include <time.h>
33  #include <sys/param.h>  #include <sys/param.h>
34  #define _POSIX_C_SOURCE 200809L  
35  #include <string.h>  static int section_topic_view_mode = 0;
36    static int section_topic_view_tid = -1;
37    
38  enum select_cmd_t  enum select_cmd_t
39  {  {
# Line 34  enum select_cmd_t Line 42  enum select_cmd_t
42          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
43          REFRESH_SCREEN = 3,          REFRESH_SCREEN = 3,
44          CHANGE_NAME_DISPLAY = 4,          CHANGE_NAME_DISPLAY = 4,
45            POST_ARTICLE = 5,
46            EDIT_ARTICLE = 6,
47            DELETE_ARTICLE = 7,
48            SHOW_HELP = 8,
49  };  };
50    
51  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname)  static int section_list_draw_items(int page_id, ARTICLE *p_articles[], int article_count, int display_nickname)
# Line 46  static int section_list_draw_items(int p Line 58  static int section_list_draw_items(int p
58          int len;          int len;
59          int i;          int i;
60          char article_flag;          char article_flag;
61            int is_viewed;
62          time_t tm_now;          time_t tm_now;
63    
64          time(&tm_now);          time(&tm_now);
# Line 54  static int section_list_draw_items(int p Line 67  static int section_list_draw_items(int p
67    
68          for (i = 0; i < article_count; i++)          for (i = 0; i < article_count; i++)
69          {          {
70                  article_flag = ' ';                  if (p_articles[i]->uid == BBS_priv.uid)
71                    {
72                            is_viewed = 1;
73                    }
74                    else
75                    {
76                            is_viewed = article_view_log_is_viewed(p_articles[i]->aid, &BBS_article_view_log);
77                            if (is_viewed < 0)
78                            {
79                                    log_error("article_view_log_is_viewed(aid=%d) error\n", p_articles[i]->aid);
80                                    is_viewed = 0;
81                            }
82                    }
83    
84                    article_flag = (is_viewed ? ' ' : 'N');
85    
86                  if (p_articles[i]->excerption)                  if (p_articles[i]->excerption)
87                  {                  {
88                          article_flag = 'm';                          article_flag = (is_viewed ? 'm' : 'M');
89                  }                  }
90                  else if (p_articles[i]->lock)                  else if (p_articles[i]->lock && is_viewed)
91                  {                  {
92                          article_flag = 'x';                          article_flag = 'x';
93                  }                  }
# Line 79  static int section_list_draw_items(int p Line 106  static int section_list_draw_items(int p
106                  title_f[sizeof(title_f) - 1] = '\0';                  title_f[sizeof(title_f) - 1] = '\0';
107                  strncat(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));                  strncat(title_f, (p_articles[i]->transship ? "[转载]" : ""), sizeof(title_f) - 1 - strnlen(title_f, sizeof(title_f)));
108                  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)));
109                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len);                  len = split_line(title_f, 47 - (display_nickname ? 8 : 0), &eol, &title_f_len, 1);
110                  if (title_f[len] != '\0')                  if (title_f[len] != '\0')
111                  {                  {
112                          title_f[len] = '\0';                          title_f[len] = '\0';
113                  }                  }
114    
115                  moveto(4 + i, 1);                  moveto(4 + i, 1);
116                  prints("  %7d %c %s%*s %s %s",                  prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",
117                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
118                             p_articles[i]->aid,                             p_articles[i]->aid,
119                             article_flag,                             article_flag,
120                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),                             (display_nickname ? p_articles[i]->nickname : p_articles[i]->username),
# Line 94  static int section_list_draw_items(int p Line 122  static int section_list_draw_items(int p
122                                                                   : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),                                                                   : BBS_username_max_len - (int)strnlen(p_articles[i]->username, sizeof(p_articles[i]->username))),
123                             "",                             "",
124                             str_time,                             str_time,
125                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
126                             title_f);                             title_f);
127          }          }
128    
# Line 115  static int section_list_draw_screen(cons Line 144  static int section_list_draw_screen(cons
144          show_top(str_section_master, stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
145          moveto(2, 0);          moveto(2, 0);
146          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] "          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] "
147                     "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m]\033[m %s[\033[1;32mn\033[0;37m]\033[m",                     "阅读[\033[1;32m→\033[0;37m,\033[1;32mENTER\033[0;37m] 发表[\033[1;32mCtrl-P\033[0;37m] "
148                       "%s[\033[1;32mn\033[0;37m] 帮助[\033[1;32mh\033[0;37m]\033[m",
149                     (display_nickname ? "显示用户名" : "显示昵称"));                     (display_nickname ? "显示用户名" : "显示昵称"));
150          moveto(3, 0);          moveto(3, 0);
151          if (display_nickname)          if (display_nickname)
# Line 165  static enum select_cmd_t section_list_se Line 195  static enum select_cmd_t section_list_se
195                          return CHANGE_NAME_DISPLAY;                          return CHANGE_NAME_DISPLAY;
196                  case CR:                  case CR:
197                          igetch_reset();                          igetch_reset();
198                    case 'r':
199                  case KEY_RIGHT:                  case KEY_RIGHT:
200                          if (item_count > 0)                          if (item_count > 0)
201                          {                          {
202                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
203                          }                          }
204                          break;                          break;
205                    case Ctrl('P'):
206                            return POST_ARTICLE;
207                    case 'E':
208                            return EDIT_ARTICLE;
209                    case 'd':
210                            return DELETE_ARTICLE;
211                  case KEY_HOME:                  case KEY_HOME:
212                          *p_page_id = 0;                          *p_page_id = 0;
213                    case 'P':
214                  case KEY_PGUP:                  case KEY_PGUP:
215                          *p_selected_index = 0;                          *p_selected_index = 0;
216                    case 'k':
217                  case KEY_UP:                  case KEY_UP:
218                          if (*p_selected_index <= 0)                          if (*p_selected_index <= 0)
219                          {                          {
# Line 189  static enum select_cmd_t section_list_se Line 228  static enum select_cmd_t section_list_se
228                                  (*p_selected_index)--;                                  (*p_selected_index)--;
229                          }                          }
230                          break;                          break;
231                    case '$':
232                  case KEY_END:                  case KEY_END:
233                          if (total_page > 0)                          if (total_page > 0)
234                          {                          {
235                                  *p_page_id = total_page - 1;                                  *p_page_id = total_page - 1;
236                          }                          }
237                    case 'N':
238                  case KEY_PGDN:                  case KEY_PGDN:
239                          if (item_count > 0)                          if (item_count > 0)
240                          {                          {
241                                  *p_selected_index = item_count - 1;                                  *p_selected_index = item_count - 1;
242                          }                          }
243                    case 'j':
244                  case KEY_DOWN:                  case KEY_DOWN:
245                          if (*p_selected_index + 1 >= item_count) // next page                          if (*p_selected_index + 1 >= item_count) // next page
246                          {                          {
# Line 207  static enum select_cmd_t section_list_se Line 249  static enum select_cmd_t section_list_se
249                                          (*p_page_id)++;                                          (*p_page_id)++;
250                                          *p_selected_index = 0;                                          *p_selected_index = 0;
251                                  }                                  }
252                                    else // end of last page
253                                    {
254                                            return CHANGE_PAGE; // force refresh pages
255                                    }
256                          }                          }
257                          else                          else
258                          {                          {
259                                  (*p_selected_index)++;                                  (*p_selected_index)++;
260                          }                          }
261                          break;                          break;
262                    case 'h':
263                            return SHOW_HELP;
264                  default:                  default:
265                  }                  }
266    
# Line 244  static enum select_cmd_t section_list_se Line 292  static enum select_cmd_t section_list_se
292          return EXIT_SECTION;          return EXIT_SECTION;
293  }  }
294    
295  static int display_article_key_handler(int *key, char *msg, size_t msg_len)  static int display_article_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
296  {  {
297          static int topic_view = 0;          switch (*p_key)
   
         switch (*key)  
298          {          {
299            case 'p':
300            case Ctrl('X'):
301                    section_topic_view_mode = !section_topic_view_mode;
302          case 0: // Set msg          case 0: // Set msg
303                  snprintf(msg, msg_len,                  if (section_topic_view_mode)
304                                   "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "                  {
305                                   "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ "                          snprintf(p_ctx->msg, sizeof(p_ctx->msg),
306                                   "帮助[\033[32mh\033[33m] |");                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
307                                             "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] "
308                                             "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
309                    }
310                    else
311                    {
312                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
313                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
314                                             "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] "
315                                             "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
316                    }
317                    *p_key = 0;
318                  break;                  break;
319          case 'p':          case 'r': // Reply article
320                    return 1;
321            case KEY_UP:
322            case KEY_PGUP:
323            case KEY_HOME:
324                    if (p_ctx->reach_begin)
325                    {
326                            if (section_topic_view_mode)
327                            {
328                                    *p_key = KEY_PGUP;
329                            }
330                            else
331                            {
332                                    *p_key = KEY_UP;
333                            }
334                            return 1;
335                    }
336                  break;                  break;
337                  topic_view = !topic_view;          case 'k':
338                  if (topic_view)                  if (section_topic_view_mode)
339                  {                  {
340                          snprintf(msg, msg_len,                          *p_key = KEY_PGUP;
                                          "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "  
                                          "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] │ "  
                                          "帮助[\033[32mh\033[33m] |");  
341                  }                  }
342                  else                  else
343                  {                  {
344                          snprintf(msg, msg_len,                          *p_key = KEY_UP;
345                                           "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] │ "                  }
346                                           "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] │ "                  return 1;
347                                           "帮助[\033[32mh\033[33m] |");          case KEY_DOWN:
348            case KEY_PGDN:
349            case KEY_END:
350                    if (p_ctx->reach_end)
351                    {
352                            if (section_topic_view_mode)
353                            {
354                                    *p_key = KEY_PGDN;
355                            }
356                            else
357                            {
358                                    *p_key = KEY_DOWN;
359                            }
360                            return 1;
361                  }                  }
                 *key = 0;  
362                  break;                  break;
363          case 'H':          case 'j':
364                  *key = 'h';                  if (section_topic_view_mode)
365                  return 0;                  {
366                            *p_key = KEY_PGDN;
367                    }
368                    else
369                    {
370                            *p_key = KEY_DOWN;
371                    }
372                    return 1;
373          }          }
374    
375          return 0;          return 0;
# Line 298  int section_list_display(const char *sna Line 390  int section_list_display(const char *sna
390          int selected_index = 0;          int selected_index = 0;
391          ARTICLE_CACHE cache;          ARTICLE_CACHE cache;
392          int ret;          int ret;
393            int loop;
394            int direction;
395            ARTICLE article_new;
396    
397          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
398          if (p_section == NULL)          if (p_section == NULL)
# Line 379  int section_list_display(const char *sna Line 474  int section_list_display(const char *sna
474                          }                          }
475                          break;                          break;
476                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
477                          ret = article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]);                          do
                         if (ret < 0)  
478                          {                          {
479                                  log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);                                  loop = 0;
                                 break;  
                         }  
480    
481                          ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 1, 0,                                  if (article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]) < 0)
482                                                             display_article_key_handler, DATA_READ_HELP);                                  {
483                          if (ret < 0)                                          log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
484                          {                                          break;
485                                  log_error("display_data(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);                                  }
                                 break;  
                         }  
486    
487                          ret = article_cache_unload(&cache);                                  ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,
488                          if (ret < 0)                                                                     display_article_key_handler, DATA_READ_HELP);
                         {  
                                 log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);  
                                 break;  
                         }  
489    
490                          // TODO: locate last viewed article                                  if (article_cache_unload(&cache) < 0)
491                                    {
492                                            log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
493                                            break;
494                                    }
495    
496                                    // Update article_view_log
497                                    if (article_view_log_set_viewed(p_articles[selected_index]->aid, &BBS_article_view_log) < 0)
498                                    {
499                                            log_error("article_view_log_set_viewed(aid=%d) error\n", p_articles[selected_index]->aid);
500                                    }
501    
502                                    switch (ret)
503                                    {
504                                    case KEY_UP:
505                                            if (selected_index <= 0)
506                                            {
507                                                    if (page_id > 0)
508                                                    {
509                                                            page_id--;
510                                                            selected_index = BBS_article_limit_per_page - 1;
511    
512                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
513                                                            if (ret < 0)
514                                                            {
515                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
516                                                                    return -3;
517                                                            }
518    
519                                                            if (article_count != BBS_article_limit_per_page) // page is not full
520                                                            {
521                                                                    selected_index = MAX(0, article_count - 1);
522                                                            }
523                                                            else
524                                                            {
525                                                                    loop = 1;
526                                                            }
527                                                    }
528                                            }
529                                            else
530                                            {
531                                                    selected_index--;
532                                                    loop = 1;
533                                            }
534                                            break;
535                                    case KEY_DOWN:
536                                            if (selected_index + 1 >= article_count) // next page
537                                            {
538                                                    if (page_id + 1 < page_count)
539                                                    {
540                                                            page_id++;
541                                                            selected_index = 0;
542    
543                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
544                                                            if (ret < 0)
545                                                            {
546                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
547                                                                    return -3;
548                                                            }
549    
550                                                            if (article_count == 0) // empty page
551                                                            {
552                                                                    selected_index = 0;
553                                                            }
554                                                            else
555                                                            {
556                                                                    loop = 1;
557                                                            }
558                                                    }
559                                            }
560                                            else
561                                            {
562                                                    selected_index++;
563                                                    loop = 1;
564                                            }
565                                            break;
566                                    case KEY_PGUP:
567                                    case KEY_PGDN:
568                                            direction = (ret == KEY_PGUP ? -1 : 1);
569                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, &page_id, &selected_index, &page_count);
570                                            if (ret < 0)
571                                            {
572                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d) error\n",
573                                                                      p_section->sid, p_articles[selected_index]->aid, direction);
574                                                    return -3;
575                                            }
576                                            else if (ret > 0)
577                                            {
578                                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
579                                                    if (ret < 0)
580                                                    {
581                                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
582                                                            return -3;
583                                                    }
584                                                    loop = 1;
585                                            }
586                                            break;
587                                    case 'r': // Reply article
588                                            if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)
589                                            {
590                                                    log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);
591                                            }
592                                            loop = 1;
593                                            break;
594                                    }
595                            } while (loop);
596    
597                            // Update current topic
598                            section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
599                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
600                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
601                          {                          {
# Line 414  int section_list_display(const char *sna Line 608  int section_list_display(const char *sna
608                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
609                          {                          {
610                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
611                                    return -2;
612                            }
613                            break;
614                    case POST_ARTICLE:
615                            ret = article_post(p_section, &article_new);
616                            if (ret < 0)
617                            {
618                                    log_error("article_post(sid=%d) error\n", p_section->sid);
619                            }
620                            else if (ret > 0) // New article posted
621                            {
622                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
623                                    if (ret < 0)
624                                    {
625                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
626                                            return -3;
627                                    }
628                            }
629                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
630                            {
631                                    log_error("section_list_draw_screen() error\n");
632                                    return -2;
633                            }
634                            break;
635                    case EDIT_ARTICLE:
636                            if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
637                                    p_articles[selected_index]->uid != BBS_priv.uid)
638                            {
639                                    break; // No permission
640                            }
641                            if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)
642                            {
643                                    log_error("article_modify(aid=%d) error\n", p_articles[selected_index]->aid);
644                            }
645                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
646                            {
647                                    log_error("section_list_draw_screen() error\n");
648                                    return -2;
649                            }
650                            break;
651                    case DELETE_ARTICLE:
652                            if (!checkpriv(&BBS_priv, p_section->sid, S_POST) ||
653                                    (!checkpriv(&BBS_priv, p_section->sid, S_MAN_S) && p_articles[selected_index]->uid != BBS_priv.uid))
654                            {
655                                    break; // No permission
656                            }
657                            if (article_del(p_section, p_articles[selected_index]) < 0)
658                            {
659                                    log_error("article_del(aid=%d) error\n", p_articles[selected_index]->aid);
660                            }
661                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
662                            {
663                                    log_error("section_list_draw_screen() error\n");
664                                    return -2;
665                            }
666                            break;
667                    case SHOW_HELP:
668                            // Display help information
669                            display_file(DATA_READ_HELP, 1);
670                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
671                            {
672                                    log_error("section_list_draw_screen() error\n");
673                                  return -2;                                  return -2;
674                          }                          }
675                          break;                          break;


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

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