/[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.21 by sysadm, Fri Jun 13 12:01:27 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 171  static enum select_cmd_t section_list_se Line 196  static enum select_cmd_t section_list_se
196                                  return VIEW_ARTICLE;                                  return VIEW_ARTICLE;
197                          }                          }
198                          break;                          break;
199                    case Ctrl('P'):
200                            return POST_ARTICLE;
201                    case 'E':
202                            return EDIT_ARTICLE;
203                  case KEY_HOME:                  case KEY_HOME:
204                          *p_page_id = 0;                          *p_page_id = 0;
205                  case KEY_PGUP:                  case KEY_PGUP:
# Line 244  static enum select_cmd_t section_list_se Line 273  static enum select_cmd_t section_list_se
273          return EXIT_SECTION;          return EXIT_SECTION;
274  }  }
275    
276    static int display_article_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
277    {
278            switch (*p_key)
279            {
280            case 'p':
281                    section_topic_view_mode = !section_topic_view_mode;
282            case 0: // Set msg
283                    if (section_topic_view_mode)
284                    {
285                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
286                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
287                                             "同主题阅读[\033[32m↑\033[33m/\033[32m↓\033[33m] | "
288                                             "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");
289                    }
290                    else
291                    {
292                            snprintf(p_ctx->msg, sizeof(p_ctx->msg),
293                                             "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
294                                             "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
295                                             "模式[\033[32mp\033[33m] | 帮助[\033[32mh\033[33m] |");
296                    }
297                    *p_key = 0;
298                    break;
299            case 'r': // Reply article
300                    return 1;
301            case KEY_UP:
302            case KEY_PGUP:
303            case KEY_HOME:
304                    if (p_ctx->reach_begin)
305                    {
306                            if (section_topic_view_mode)
307                            {
308                                    *p_key = KEY_PGUP;
309                            }
310                            else
311                            {
312                                    *p_key = KEY_UP;
313                            }
314                            return 1;
315                    }
316                    break;
317            case KEY_DOWN:
318            case KEY_PGDN:
319            case KEY_END:
320                    if (p_ctx->reach_end)
321                    {
322                            if (section_topic_view_mode)
323                            {
324                                    *p_key = KEY_PGDN;
325                            }
326                            else
327                            {
328                                    *p_key = KEY_DOWN;
329                            }
330                            return 1;
331                    }
332                    break;
333            }
334    
335            return 0;
336    }
337    
338  int section_list_display(const char *sname)  int section_list_display(const char *sname)
339  {  {
340          static int display_nickname = 0;          static int display_nickname = 0;
# Line 257  int section_list_display(const char *sna Line 348  int section_list_display(const char *sna
348          int page_count;          int page_count;
349          int page_id = 0;          int page_id = 0;
350          int selected_index = 0;          int selected_index = 0;
351          ARTICLE_CACHE article_cache;          ARTICLE_CACHE cache;
352          int ret;          int ret;
353            int loop;
354            int direction;
355    
356          p_section = section_list_find_by_name(sname);          p_section = section_list_find_by_name(sname);
357          if (p_section == NULL)          if (p_section == NULL)
# Line 340  int section_list_display(const char *sna Line 433  int section_list_display(const char *sna
433                          }                          }
434                          break;                          break;
435                  case VIEW_ARTICLE:                  case VIEW_ARTICLE:
436                          ret = article_cache_load(&article_cache, VAR_ARTICLE_CACHE_DIR, p_articles[selected_index]);                          do
                         if (ret < 0)  
437                          {                          {
438                                  log_error("article_cache_load(aid=%d, cid=%d)\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);                                  loop = 0;
                                 break;  
                         }  
439    
440                          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)
441                                    {
442                                            log_error("article_cache_load(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
443                                            break;
444                                    }
445    
446                          ret = article_cache_unload(&article_cache);                                  ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,
447                          if (ret < 0)                                                                     display_article_key_handler, DATA_READ_HELP);
448                          {  
449                                  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)
450                                  break;                                  {
451                          }                                          log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_articles[selected_index]->aid, p_articles[selected_index]->cid);
452                                            break;
453                                    }
454    
455                                    // Update article_view_log
456                                    if (article_view_log_set_viewed(p_articles[selected_index]->aid, &BBS_article_view_log) < 0)
457                                    {
458                                            log_error("article_view_log_set_viewed(aid=%d) error\n", p_articles[selected_index]->aid);
459                                    }
460    
461                          // TODO: locate last viewed article                                  switch (ret)
462                                    {
463                                    case KEY_UP:
464                                            if (selected_index <= 0)
465                                            {
466                                                    if (page_id > 0)
467                                                    {
468                                                            page_id--;
469                                                            selected_index = BBS_article_limit_per_page - 1;
470    
471                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
472                                                            if (ret < 0)
473                                                            {
474                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
475                                                                    return -3;
476                                                            }
477    
478                                                            if (article_count != BBS_article_limit_per_page) // page is not full
479                                                            {
480                                                                    selected_index = MAX(0, article_count - 1);
481                                                            }
482                                                            else
483                                                            {
484                                                                    loop = 1;
485                                                            }
486                                                    }
487                                            }
488                                            else
489                                            {
490                                                    selected_index--;
491                                                    loop = 1;
492                                            }
493                                            break;
494                                    case KEY_DOWN:
495                                            if (selected_index + 1 >= article_count) // next page
496                                            {
497                                                    if (page_id + 1 < page_count)
498                                                    {
499                                                            page_id++;
500                                                            selected_index = 0;
501    
502                                                            ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
503                                                            if (ret < 0)
504                                                            {
505                                                                    log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
506                                                                    return -3;
507                                                            }
508    
509                                                            if (article_count == 0) // empty page
510                                                            {
511                                                                    selected_index = 0;
512                                                            }
513                                                            else
514                                                            {
515                                                                    loop = 1;
516                                                            }
517                                                    }
518                                            }
519                                            else
520                                            {
521                                                    selected_index++;
522                                                    loop = 1;
523                                            }
524                                            break;
525                                    case KEY_PGUP:
526                                    case KEY_PGDN:
527                                            direction = (ret == KEY_PGUP ? -1 : 1);
528                                            ret = locate_article_in_section(p_section, p_articles[selected_index], direction, &page_id, &selected_index, &page_count);
529                                            if (ret < 0)
530                                            {
531                                                    log_error("locate_article_in_section(sid=%d, aid=%d, direction=%d) error\n",
532                                                                      p_section->sid, p_articles[selected_index]->aid, direction);
533                                                    return -3;
534                                            }
535                                            else if (ret > 0)
536                                            {
537                                                    ret = query_section_articles(p_section, page_id, p_articles, &article_count, &page_count);
538                                                    if (ret < 0)
539                                                    {
540                                                            log_error("query_section_articles(sid=%d, page_id=%d) error\n", p_section->sid, page_id);
541                                                            return -3;
542                                                    }
543                                                    loop = 1;
544                                            }
545                                            break;
546                                    case 'r': // Reply article
547                                            if (article_reply(p_section, p_articles[selected_index]) < 0)
548                                            {
549                                                    log_error("article_post(aid=%d, REPLY) error\n", p_articles[selected_index]->aid);
550                                            }
551                                            loop = 1;
552                                            break;
553                                    }
554                            } while (loop);
555    
556                            // Update current topic
557                            section_topic_view_tid = (p_articles[selected_index]->tid == 0 ? p_articles[selected_index]->aid : p_articles[selected_index]->tid);
558                  case REFRESH_SCREEN:                  case REFRESH_SCREEN:
559                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
560                          {                          {
# Line 369  int section_list_display(const char *sna Line 567  int section_list_display(const char *sna
567                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)                          if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
568                          {                          {
569                                  log_error("section_list_draw_screen() error\n");                                  log_error("section_list_draw_screen() error\n");
570                                    return -2;
571                            }
572                            break;
573                    case POST_ARTICLE:
574                            if (article_post(p_section) < 0)
575                            {
576                                    log_error("article_post(sid=%d, NEW) error\n", p_section->sid);
577                            }
578                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
579                            {
580                                    log_error("section_list_draw_screen() error\n");
581                                    return -2;
582                            }
583                            break;
584                    case EDIT_ARTICLE:
585                            if (p_articles[selected_index]->uid != BBS_priv.uid)
586                            {
587                                    break;
588                            }
589                            if (article_modify(p_section, p_articles[selected_index]) < 0)
590                            {
591                                    log_error("article_post(aid=%d, EDIT) error\n", p_articles[selected_index]->aid);
592                            }
593                            if (section_list_draw_screen(sname, stitle, master_list, display_nickname) < 0)
594                            {
595                                    log_error("section_list_draw_screen() error\n");
596                                  return -2;                                  return -2;
597                          }                          }
598                          break;                          break;


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

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