/[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.9 by sysadm, Thu May 29 09:44:01 2025 UTC Revision 1.25 by sysadm, Sat Jun 14 13:33:48 2025 UTC
# Line 17  Line 17 
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"  #include "article_cache.h"
20    #include "article_post.h"
21  #include "common.h"  #include "common.h"
22  #include "io.h"  #include "io.h"
23  #include "screen.h"  #include "screen.h"
24  #include "log.h"  #include "log.h"
25    #include "user_priv.h"
26    #include "article_view_log.h"
27  #include "str_process.h"  #include "str_process.h"
28  #include <time.h>  #include <time.h>
29  #include <sys/param.h>  #include <sys/param.h>
30  #define _POSIX_C_SOURCE 200809L  #define _POSIX_C_SOURCE 200809L
31  #include <string.h>  #include <string.h>
32    
33    static int section_topic_view_mode = 0;
34    static int section_topic_view_tid = -1;
35    
36  enum select_cmd_t  enum select_cmd_t
37  {  {
38          EXIT_SECTION = 0,          EXIT_SECTION = 0,
# Line 34  enum select_cmd_t Line 40  enum select_cmd_t
40          CHANGE_PAGE = 2,          CHANGE_PAGE = 2,
41          REFRESH_SCREEN = 3,          REFRESH_SCREEN = 3,
42          CHANGE_NAME_DISPLAY = 4,          CHANGE_NAME_DISPLAY = 4,
43            POST_ARTICLE = 5,
44            EDIT_ARTICLE = 6,
45  };  };
46    
47  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 54  static int section_list_draw_items(int p
54          int len;          int len;
55          int i;          int i;
56          char article_flag;          char article_flag;
57            int is_viewed;
58          time_t tm_now;          time_t tm_now;
59    
60          time(&tm_now);          time(&tm_now);
# Line 54  static int section_list_draw_items(int p Line 63  static int section_list_draw_items(int p
63    
64          for (i = 0; i < article_count; i++)          for (i = 0; i < article_count; i++)
65          {          {
66                  article_flag = ' ';                  if (p_articles[i]->uid == BBS_priv.uid)
67                    {
68                            is_viewed = 1;
69                    }
70                    else
71                    {
72                            is_viewed = article_view_log_is_viewed(p_articles[i]->aid, &BBS_article_view_log);
73                            if (is_viewed < 0)
74                            {
75                                    log_error("article_view_log_is_viewed(aid=%d) error\n", p_articles[i]->aid);
76                                    is_viewed = 0;
77                            }
78                    }
79    
80                    article_flag = (is_viewed ? ' ' : 'N');
81    
82                  if (p_articles[i]->excerption)                  if (p_articles[i]->excerption)
83                  {                  {
84                          article_flag = 'm';                          article_flag = (is_viewed ? 'm' : 'M');
85                  }                  }
86                  else if (p_articles[i]->lock)                  else if (p_articles[i]->lock && is_viewed)
87                  {                  {
88                          article_flag = 'x';                          article_flag = 'x';
89                  }                  }
# Line 86  static int section_list_draw_items(int p Line 109  static int section_list_draw_items(int p
109                  }                  }
110    
111                  moveto(4 + i, 1);                  moveto(4 + i, 1);
112                  prints("  %7d %c %s%*s %s %s",                  prints("  %s%7d\033[m %c %s%*s %s %s%s\033[m",
113                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
114                             p_articles[i]->aid,                             p_articles[i]->aid,
115                             article_flag,                             article_flag,
116                             (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 118  static int section_list_draw_items(int p
118                                                                   : 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))),
119                             "",                             "",
120                             str_time,                             str_time,
121                               (p_articles[i]->aid == section_topic_view_tid ? "\033[1;33m" : (p_articles[i]->tid == section_topic_view_tid ? "\033[1;36m" : "")),
122                             title_f);                             title_f);
123          }          }
124    
# Line 115  static int section_list_draw_screen(cons Line 140  static int section_list_draw_screen(cons
140          show_top(str_section_master, stitle, str_section_name);          show_top(str_section_master, stitle, str_section_name);
141          moveto(2, 0);          moveto(2, 0);
142          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] "
143                     "阅读[\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] "
144                       "修改[\033[1;32mE\033[0;37m] %s[\033[1;32mn\033[0;37m]\033[m",
145                     (display_nickname ? "显示用户名" : "显示昵称"));                     (display_nickname ? "显示用户名" : "显示昵称"));
146          moveto(3, 0);          moveto(3, 0);
147          if (display_nickname)          if (display_nickname)
# Line 171  static enum select_cmd_t section_list_se Line 197  static enum select_cmd_t section_list_se
197                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
198                          }                          }
199                          break;                          break;
200                    case Ctrl('P'):
201                            return POST_ARTICLE;
202                    case 'E':
203                            return EDIT_ARTICLE;
204                  case KEY_HOME:                  case KEY_HOME:
205                          *p_page_id = 0;                          *p_page_id = 0;
206                  case KEY_PGUP:                  case KEY_PGUP:
# Line 207  static enum select_cmd_t section_list_se Line 237  static enum select_cmd_t section_list_se
237                                          (*p_page_id)++;                                          (*p_page_id)++;
238                                          *p_selected_index = 0;                                          *p_selected_index = 0;
239                                  }                                  }
240                                    else // end of last page
241                                    {
242                                            return CHANGE_PAGE; // force refresh pages
243                                    }
244                          }                          }
245                          else                          else
246                          {                          {
# Line 244  static enum select_cmd_t section_list_se Line 278  static enum select_cmd_t section_list_se
278          return EXIT_SECTION;          return EXIT_SECTION;
279  }  }
280    
281    static int display_article_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
282    {
283            switch (*p_key)
284            {
285            case 'p':
286                    section_topic_view_mode = !section_topic_view_mode;
287            case 0: // Set msg
288                    if (section_topic_view_mode)
289                    {
290                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
291                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
292                                             "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] "
293                                             "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
294                    }
295                    else
296                    {
297                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
298                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] "
299                                             "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] "
300                                             "切换[\033[32mp\033[33m] 回复[\033[32mr\033[33m] 帮助[\033[32mh\033[33m] |");
301                    }
302                    *p_key = 0;
303                    break;
304            case 'r': // Reply article
305                    return 1;
306            case KEY_UP:
307            case KEY_PGUP:
308            case KEY_HOME:
309                    if (p_ctx->reach_begin)
310                    {
311                            if (section_topic_view_mode)
312                            {
313                                    *p_key = KEY_PGUP;
314                            }
315                            else
316                            {
317                                    *p_key = KEY_UP;
318                            }
319                            return 1;
320                    }
321                    break;
322            case KEY_DOWN:
323            case KEY_PGDN:
324            case KEY_END:
325                    if (p_ctx->reach_end)
326                    {
327                            if (section_topic_view_mode)
328                            {
329                                    *p_key = KEY_PGDN;
330                            }
331                            else
332                            {
333                                    *p_key = KEY_DOWN;
334                            }
335                            return 1;
336                    }
337                    break;
338            }
339    
340            return 0;
341    }
342    
343  int section_list_display(const char *sname)  int section_list_display(const char *sname)
344  {  {
345          static int display_nickname = 0;          static int display_nickname = 0;
# Line 257  int section_list_display(const char *sna Line 353  int section_list_display(const char *sna
353          int page_count;          int page_count;
354          int page_id = 0;          int page_id = 0;
355          int selected_index = 0;          int selected_index = 0;
356          ARTICLE_CACHE article_cache;          ARTICLE_CACHE cache;
357          int ret;          int ret;
358            int loop;
359            int direction;
360            ARTICLE article_new;
361    
362          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
363          if (p_section == NULL)          if (p_section == NULL)
# Line 340  int section_list_display(const char *sna Line 439  int section_list_display(const char *sna
439                          }                          }
440                          break;                          break;
441                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
442                          ret = article_cache_load(&article_cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]);                          do
                         if (ret < 0)  
443                          {                          {
444                                  log_error("article_cache_load(aid=%d, cid=%d)\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);                                  loop = 0;
                                 break;  
                         }  
445    
446                          log_std("Debug: view article aid = %d, cid = %d\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);                                  if (article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]) < 0)
447                                    {
448                                            log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
449                                            break;
450                                    }
451    
452                          ret = article_cache_unload(&article_cache);                                  ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,
453                          if (ret < 0)                                                                     display_article_key_handler, DATA_READ_HELP);
454                          {  
455                                  log_error("article_cache_unload(aid=%d, cid=%d)\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);                                  if (article_cache_unload(&cache) < 0)
456                                  break;                                  {
457                          }                                          log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
458                                            break;
459                                    }
460    
461                                    // Update article_view_log
462                                    if (article_view_log_set_viewed(p_articles[selected_index]->aid, &BBS_article_view_log) < 0)
463                                    {
464                                            log_error("article_view_log_set_viewed(aid=%d) error\n", p_articles[selected_index]->aid);
465                                    }
466    
467                                    switch (ret)
468                                    {
469                                    case KEY_UP:
470                                            if (selected_index <= 0)
471                                            {
472                                                    if (page_id > 0)
473                                                    {
474                                                            page_id--;
475                                                            selected_index = BBS_article_limit_per_page - 1;
476    
477                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
478                                                            if (ret < 0)
479                                                            {
480                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
481                                                                    return -3;
482                                                            }
483    
484                                                            if (article_count != BBS_article_limit_per_page) // page is not full
485                                                            {
486                                                                    selected_index = MAX(0, article_count - 1);
487                                                            }
488                                                            else
489                                                            {
490                                                                    loop = 1;
491                                                            }
492                                                    }
493                                            }
494                                            else
495                                            {
496                                                    selected_index--;
497                                                    loop = 1;
498                                            }
499                                            break;
500                                    case KEY_DOWN:
501                                            if (selected_index + 1 >= article_count) // next page
502                                            {
503                                                    if (page_id + 1 < page_count)
504                                                    {
505                                                            page_id++;
506                                                            selected_index = 0;
507    
508                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
509                                                            if (ret < 0)
510                                                            {
511                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
512                                                                    return -3;
513                                                            }
514    
515                                                            if (article_count == 0) // empty page
516                                                            {
517                                                                    selected_index = 0;
518                                                            }
519                                                            else
520                                                            {
521                                                                    loop = 1;
522                                                            }
523                                                    }
524                                            }
525                                            else
526                                            {
527                                                    selected_index++;
528                                                    loop = 1;
529                                            }
530                                            break;
531                                    case KEY_PGUP:
532                                    case KEY_PGDN:
533                                            direction = (ret == KEY_PGUP ? -1 : 1);
534                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, &page_id, &selected_index, &page_count);
535                                            if (ret < 0)
536                                            {
537                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d) error\n",
538                                                                      p_section->sid, p_articles[selected_index]->aid, direction);
539                                                    return -3;
540                                            }
541                                            else if (ret > 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                                                    loop = 1;
550                                            }
551                                            break;
552                                    case 'r': // Reply article
553                                            if (article_reply(p_section, p_articles[selected_index], &article_new) < 0)
554                                            {
555                                                    log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);
556                                            }
557                                            loop = 1;
558                                            break;
559                                    }
560                            } while (loop);
561    
562                          // TODO: locate last viewed article                          // Update current topic
563                            section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
564                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
565                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
566                          {                          {
# Line 369  int section_list_display(const char *sna Line 573  int section_list_display(const char *sna
573                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
574                          {                          {
575                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
576                                    return -2;
577                            }
578                            break;
579                    case POST_ARTICLE:
580                            ret = article_post(p_section, &article_new);
581                            if (ret < 0)
582                            {
583                                    log_error("article_post(sid=%d, NEW) error\n", p_section->sid);
584                            }
585                            else if (ret > 0) // New article posted
586                            {
587                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
588                                    if (ret < 0)
589                                    {
590                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
591                                            return -3;
592                                    }
593                            }
594                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
595                            {
596                                    log_error("section_list_draw_screen() error\n");
597                                    return -2;
598                            }
599                            break;
600                    case EDIT_ARTICLE:
601                            if (p_articles[selected_index]->uid != BBS_priv.uid)
602                            {
603                                    break;
604                            }
605                            if (article_modify(p_section, p_articles[selected_index], &article_new) < 0)
606                            {
607                                    log_error("article_post(aid=%d, EDIT) error\n", p_articles[selected_index]->aid);
608                            }
609                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
610                            {
611                                    log_error("section_list_draw_screen() error\n");
612                                  return -2;                                  return -2;
613                          }                          }
614                          break;                          break;


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

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