OSDN Git Service

Release 1.0.1.8
[ntch/develop.git] / src / main.c
index fd52cec..06fcd7f 100644 (file)
@@ -36,6 +36,8 @@
 #include "net/nt_cookie.h"
 #include "utils/file.h"
 #include "utils/nt_usr_db.h"
+#include "utils/nt_mutex.h"
+#include "usr/favorite_t.h"
 #include "_2ch/_2ch.h"
 #include "_2ch/maru_2ch.h"
 #include "_2ch/search_2ch.h"
@@ -46,7 +48,9 @@
 #define S_SIZE         (1024)
 
 static int draw_title(WINDOW *wp, wchar_t *title, attr_t attr);
-static BOOL DoLoop(WINDOW *scrp, void *db_handle);
+static BOOL DoLoop(WINDOW *scrp, void *db_handle,
+                       nt_favorite_handle h_favorite, 
+                       nt_favorite_grp_handle h_favorite_grp);
 static void print_error(WINDOW *wp, const wchar_t *msg);
 static BOOL set_sel_item(nt_2ch_model_tp modelp,
                        nt_2ch_selected_item_tp sel_itemp, 
@@ -58,6 +62,9 @@ int main(int argc, char *argv[])
        int result = 1;
        WINDOW *scrp;
        void *usr_db_handle;
+       nt_favorite_handle h_favorite;
+       nt_favorite_grp_handle h_favorite_grp;
+       nt_link_tp text_linkp, text2_linkp;
 
        if(0 != set_option(argc, argv)){
                return 1;
@@ -83,23 +90,70 @@ int main(int argc, char *argv[])
        }
 
        if(!nt_2ch_model_init()){
-               fputs("Failed to read the board menu data.\n", stderr);
+               fputs("Failed to read board menu data.\n", stderr);
                goto ERROR_TRAP;
        }
 
        if(!nt_init_board_menu()){
-               fputs("Failed to initialize the board menu data.\n", stderr);
+               fputs("Failed to initialize board menu data.\n", stderr);
                goto ERROR_TRAP;
        }
+       
+       nt_mutex_lib_init();
 
+       h_favorite = nt_favorite_alloc(L"favorite");
+       if(!h_favorite){
+               return FALSE;
+       }
+       h_favorite_grp = nt_favorite_grp_alloc(h_favorite, 
+                               L"お気に入りのスレッド",
+                               NT_FAVORITE_GRP_FLAG_FOLDER_OPEN);
+       if(!h_favorite_grp){
+               nt_favorite_release_ref(h_favorite);
+               return FALSE;
+       }
+       
+       text_linkp = nt_read_text_file(USR_FAVORITE_BOARD_FILE_PATH);
+       if(text_linkp){
+               nt_favorite_load_boards(h_favorite, text_linkp);
+               nt_all_link_free(text_linkp, free);
+       }
+       text_linkp = nt_read_text_file(USR_FAVORITE_THREAD_FILE_PATH);
+       if(text_linkp){
+               nt_favorite_load_threads(h_favorite, h_favorite_grp, text_linkp);
+               nt_all_link_free(text_linkp, free);
+       }
+       
        cbreak();
        noecho();
 
-       if(DoLoop(scrp, usr_db_handle))
+       if(DoLoop(scrp, usr_db_handle, h_favorite, h_favorite_grp))
                result = 0;
 
        echo();
        nocbreak();
+       
+       text_linkp = nt_favorite_retrieve_boards(h_favorite);
+       if(text_linkp){
+               nt_write_text_file(USR_FAVORITE_BOARD_FILE_PATH, text_linkp);
+               nt_all_link_free(text_linkp, free);
+       }else{
+               unlink(USR_FAVORITE_BOARD_FILE_PATH);
+       }
+       if(nt_favorite_retrieve_threads(h_favorite, 
+                       &text_linkp, &text2_linkp)){
+               if(text_linkp)
+                       nt_all_link_free(text_linkp, free);
+               if(text2_linkp){
+                       nt_write_text_file(USR_FAVORITE_THREAD_FILE_PATH, text2_linkp);
+                       nt_all_link_free(text2_linkp, free);
+               }else{
+                       unlink(USR_FAVORITE_THREAD_FILE_PATH);
+               }
+       }
+
+       nt_favorite_grp_release_ref(h_favorite_grp);
+       nt_favorite_release_ref(h_favorite);
 
 ERROR_TRAP:
        clear();
@@ -110,12 +164,16 @@ ERROR_TRAP:
        endwin();
 
        nt_usr_db_finish_lib(usr_db_handle);
+       
+       nt_mutex_lib_finish();
 
        return (result);
 }
 
 
-static BOOL DoLoop(WINDOW *scrp, void *db_handle)
+static BOOL DoLoop(WINDOW *scrp, void *db_handle,
+               nt_favorite_handle h_favorite, 
+               nt_favorite_grp_handle h_favorite_grp)
 {
        int ch, state, num;
        int disp_state, nresult;
@@ -123,6 +181,7 @@ static BOOL DoLoop(WINDOW *scrp, void *db_handle)
        nt_window_tp twinp = NULL;
        nt_window_tp rwinp = NULL;
        nt_window_tp search_winp = NULL;
+       nt_window_tp favorite_winp = NULL;
        char buf[256];
        wchar_t wch, *title;
        const wchar_t *status_msg = NULL;
@@ -135,7 +194,9 @@ static BOOL DoLoop(WINDOW *scrp, void *db_handle)
        nt_searched_thread_tp sel_threadp;
        nt_2ch_selected_item_t sel_items;
        nt_2ch_selected_item_t sel_items_tmp;
-       
+       nt_favorite_board_handle h_favorite_board;
+       nt_favorite_thread_handle h_favorite_thread;
+       void *handle;
        
        disp_state = DISP_STATE_BOARDMENU; 
        cookiep = nt_load_cookies(USR_COOKIE_PATH);
@@ -153,7 +214,7 @@ static BOOL DoLoop(WINDOW *scrp, void *db_handle)
        rwinp = nt_disp_win_alloc(scrp, LINES-1, COLS, 1, 0, buf);
        if(!rwinp)
                return FALSE;
-
+       
        keypad(stdscr, true);
        while(1){
                if(FORCE_REFRESH)
@@ -168,6 +229,19 @@ static BOOL DoLoop(WINDOW *scrp, void *db_handle)
                        if(!nt_disp_win_move(scrp, bwinp, LINES-1, COLS, 1, 0))
                                goto END_WHILE;
                        state = disp_board_menu(bwinp, app_2ch_modelp, &sel_items);
+                       if(DISP_CMD(state)){
+                               if(DISP_CMD(state) == DISP_CMD_ADD_FAVORITE){
+                                       h_favorite_board = nt_favorite_board_alloc(
+                                               h_favorite,
+                                               sel_items.selected_boardp->name);
+                                       if(h_favorite_board){
+                                               nt_favorite_board_release_ref(h_favorite_board);
+                                               //favorite_dump(h_favorite);
+                                       }
+                               }
+                               state = DISP_STATE_BOARDMENU;
+                               ch = NT_KEY_NONE;
+                       }
                        if(DISP_STATE_ERROR == state){
                                goto END_WHILE;
                        }else if(DISP_STATE_THREADTITLE == state){
@@ -187,6 +261,11 @@ static BOOL DoLoop(WINDOW *scrp, void *db_handle)
                                disp_state = DISP_STATE_SEARCH_THREAD;
                                ch = NT_KEY_NONE;
                                continue;
+                       }else if(DISP_STATE_FAVORITE == state){
+                               state = DISP_STATE_BOARDMENU;
+                               disp_state = DISP_STATE_FAVORITE;
+                               ch = NT_KEY_NONE;
+                               continue;
                        }
                        break;
                case DISP_STATE_THREADTITLE:
@@ -198,7 +277,24 @@ static BOOL DoLoop(WINDOW *scrp, void *db_handle)
                        if(!nt_disp_win_move(scrp, twinp, LINES-1, COLS, 1, 0))
                                goto END_WHILE;
                        state = disp_threadlist(twinp, &sel_items, db_handle);
-                       if(DISP_STATE_ERROR == state){
+                       if(DISP_CMD(state)){
+                               if(DISP_CMD(state) == DISP_CMD_ADD_FAVORITE){
+                                       h_favorite_thread = nt_favorite_thread_alloc(
+                                               h_favorite_grp,
+                                               sel_items.selected_threadp->file_name,
+                                               sel_items.selected_boardp->name,
+                                               sel_items.selected_threadp->name);
+                                       if(h_favorite_thread){
+                                               nt_favorite_thread_release_ref(h_favorite_thread);
+                                               //favorite_dump(h_favorite);
+                                               status_msg = NT_INFO_ADD_FAVORITE_SUCCEEDED;
+                                       }else{
+                                               status_msg = NT_ERR_MSG_ADD_FAVORITE_FAILED;
+                                       }
+                               }
+                               state = DISP_STATE_THREADTITLE;
+                               ch = NT_KEY_NONE;
+                       }else if(DISP_STATE_ERROR == state){
                                goto END_WHILE;
                        }else if(DISP_STATE_BOARDMENU == state){
                                        disp_state = state;
@@ -237,8 +333,13 @@ static BOOL DoLoop(WINDOW *scrp, void *db_handle)
                                disp_state = DISP_STATE_SEARCH_THREAD;
                                ch = NT_KEY_NONE;
                                continue;
+                       }else if(DISP_STATE_FAVORITE == state){
+                               state = DISP_STATE_THREADTITLE;
+                               disp_state = DISP_STATE_FAVORITE;
+                               ch = NT_KEY_NONE;
+                               continue;
                        }else{
-                                       status_msg = twinp->status_msg;
+                               status_msg = twinp->status_msg;
                        }
                        break;
                case DISP_STATE_RESLIST:
@@ -271,6 +372,11 @@ static BOOL DoLoop(WINDOW *scrp, void *db_handle)
                                disp_state = DISP_STATE_SEARCH_THREAD;
                                ch = NT_KEY_NONE;
                                continue;
+                       }else if(DISP_STATE_FAVORITE == state){
+                               state = DISP_STATE_RESLIST;
+                               disp_state = DISP_STATE_FAVORITE;
+                               ch = NT_KEY_NONE;
+                               continue;
                        }else if(state != DISP_STATE_RESLIST){
                                disp_state = state;
                                ch = NT_KEY_NONE;
@@ -364,6 +470,67 @@ static BOOL DoLoop(WINDOW *scrp, void *db_handle)
                                continue;
                        }
                        break;
+               case DISP_STATE_FAVORITE:
+                       draw_title(scrp, L"お気に入り", WA_REVERSE);
+                       if(!favorite_winp)
+                               favorite_winp = nt_disp_win_alloc(
+                                               scrp, LINES-1, COLS, 1, 0, buf);
+                       if(!favorite_winp)
+                               goto END_WHILE;
+                       favorite_winp->key = ch;
+                       state = disp_favorite(favorite_winp, state, h_favorite, 
+                                                       &handle,  &sel_threadp);
+                       if(DISP_CMD(state)){
+                               ch = NT_KEY_NONE;
+                               if(DISP_CMD(state) == DISP_CMD_DEL_FAVORITE_BOARD){
+                                       if(nt_favorite_board_remove(handle)){
+                                               h_favorite_board = (nt_favorite_board_handle)handle;
+                                               nt_favorite_board_release_ref(h_favorite_board);
+                                               status_msg = NT_INFO_DEL_FAVORITE_SUCCEEDED;
+                                       }else{
+                                               status_msg = NT_ERR_MSG_DEL_FAVORITE_FAILED;
+                                       }
+                               }else if(DISP_CMD(state) == DISP_CMD_DEL_FAVORITE_THREAD){
+                                       if(nt_favorite_thread_remove(handle)){
+                                               h_favorite_thread = (nt_favorite_thread_handle)handle;
+                                               nt_favorite_thread_release_ref(h_favorite_thread);
+                                               status_msg = NT_INFO_DEL_FAVORITE_SUCCEEDED;
+                                       }else{
+                                               status_msg = NT_ERR_MSG_DEL_FAVORITE_FAILED;
+                                       }
+                               }else if(DISP_CMD(state) == DISP_CMD_SEL_FAVORITE_BOARD ||
+                                               DISP_CMD(state) == DISP_CMD_SEL_FAVORITE_THREAD){
+                                       if(set_sel_item(app_2ch_modelp,
+                                                       &sel_items_tmp, sel_threadp, &status_msg)){
+                                               disp_state = (DISP_CMD(state) == DISP_CMD_SEL_FAVORITE_BOARD)
+                                                       ? DISP_STATE_THREADTITLE : DISP_STATE_RESLIST;
+                                               sel_items = sel_items_tmp;
+                                               if(rwinp->data){
+                                                       free_reslist_ctx(rwinp->data);
+                                                       rwinp->data = NULL;
+                                               }
+                                               if(twinp->data){
+                                                       free_threadlist_ctx(twinp->data);
+                                                       twinp->data = NULL;
+                                               }
+                                       }
+                                       if(sel_threadp->board_name)
+                                               free(sel_threadp->board_name);
+                                       if(sel_threadp->dat_name)
+                                               free(sel_threadp->dat_name);
+                                       free(sel_threadp);
+                               }else{
+                                       disp_state = state = DISP_STATE_FAVORITE;
+                               }
+                               continue;
+                       }else if(state != DISP_STATE_FAVORITE){
+                               ch = NT_KEY_NONE;
+                               disp_state = state;
+                               continue;
+                       }else{
+                               disp_state = DISP_STATE_FAVORITE;
+                       }
+                       break;
                default:
                        goto END_WHILE;
                }/* end switch*/
@@ -435,9 +602,15 @@ END_WHILE:
                        free_search_thread_ctx(search_winp->data);
                nt_disp_win_free(search_winp);
        }
+       if(favorite_winp){
+               if(favorite_winp->data)
+                       free_favorite_ctx(favorite_winp->data);
+               nt_disp_win_free(favorite_winp);
+       }
        nt_disp_win_free(bwinp);
        nt_disp_win_free(twinp);
        nt_disp_win_free(rwinp);
+       
        return result;
 }
 
@@ -451,7 +624,7 @@ static BOOL set_sel_item(nt_2ch_model_tp modelp,
        
        assert(sel_threadp);
        assert(sel_threadp->board_name);
-       assert(sel_threadp->dat_name);
+       //assert(sel_threadp->dat_name);
        
        boardp = nt_get_board_by_name(
                        modelp, sel_threadp->board_name, &categoryp);
@@ -471,6 +644,9 @@ static BOOL set_sel_item(nt_2ch_model_tp modelp,
                        return FALSE;
                }
        }
+       if(!sel_threadp->dat_name)
+               return TRUE;
+       
        threadp = nt_get_thread_by_dat_name(
                        boardp->threadlistp, sel_threadp->dat_name);
        if(!threadp){