static char rcsid[] = "@(#)$Id: quit.c,v 1.53.10.1 2007/08/24 20:01:56 hurtta Exp $";

/******************************************************************************
 *  The Elm (ME+) Mail System  -  $Revision: 1.53.10.1 $   $State: Exp $
 *
 *  Modified by: Kari Hurtta <hurtta+elm@posti.FMI.FI> 
 *                           (was hurtta+elm@ozone.FMI.FI)
 ******************************************************************************
 *  The Elm Mail System 
 *
 *			Copyright (c) 1988-1992 USENET Community Trust
 *			Copyright (c) 1986,1987 Dave Taylor
 *****************************************************************************/

/** quit: leave the current folder and quit the program.
  
**/

#include "def_elm.h"
#include "s_elm.h"


DEBUG_VAR(Debug,__FILE__,"ELM");

#include <errno.h>
#ifndef ANSI_C
extern int errno;		/* system error number on failure */
#endif       


/* returns:

   0 .. do not do exit
   1 .. do normal exit
   EOF
*/

int quit(prompt, mailbox, page)
     int prompt;
     struct MailboxView *mailbox;
     struct menu_context  *page;
{

 requit:
    if (mailbox) {
	int status;
	int LINES, COLUMNS;

	status = sync_mbox(FALSE, TRUE, prompt, NULL, mailbox,
			    page);
	if ( status == -1)
	    /* new mail - leave not done - can't change to another file yet
	     * check for change in mailfile_size in main() will do the work
	     * of calling newmbox to add in the new messages to the current
	     * file and fix the sorting sequence that leave_mbox may have
	     * changed for its own purposes */
	    return 0;
	
	if (status < 0) {
	    int ans;
	    lib_error(CATGETS(elm_msg_cat, ElmSet, ElmFailedLeaveFolder,
			      "Failed to leave folder!"));
	    sleep_message();
	    
	    if (!prompt) 
		return 0;
	    
	again1:
	    menu_get_sizes(page, &LINES, &COLUMNS);   
	    
	    /* NOTICE: prompt_letter may return EOF */
	    ans = prompt_letter(LINES-1,"P",*def_ans_no,
				PROMPT_center|PROMPT_yesno|
				PROMPT_redraw_mark|PROMPT_ctrlL,
				page,
				CATGETS(elm_msg_cat, ElmSet, ElmFailedLeaveFolderQuit,
					"Failed to leave folder: Try to quit again ?  ([P]anic/%c/%c) "),
				*def_ans_yes, *def_ans_no);
	    
	    if (ans == ('L'&31) || ans == REDRAW_MARK) {
		menu_ClearScreen(page);      
		goto again1;
	    }

	    if (ans == *def_ans_no) 
		return 0;

	    if (ans == *def_ans_yes) {
		clear_error();				    
		goto requit;
	    }
	  
	    if (ans == 'P')
		emergency_exit(0);

	    if (ans == EOF) 
		return EOF;

	    return 0;
	}
    }
  

    /* May remove mailbox */
    close_cleanup_mbox(mailbox);
  
    return 1;
}

void resync(mailbox, page, LOC)
     struct MailboxView **mailbox;
     struct menu_context  *page;
     struct screen_parts *LOC;
{
    /** Resync on the current folder. Leave current and read it back in.

        If redraw is required, uses 
           menu_trigger_redraw(page)
	  
    **/
    int  err;

    if (*mailbox) {
	int result;
	int sc,i;

	result = sync_mbox(TRUE, FALSE, TRUE, NULL, *mailbox, 
			   page);
	if( result == -1)
	    /* new mail - leave not done - can't change to another file yet
	     * check for change in mailfile_size in main() will do the work
	     * of calling newmbox to add in the new messages to the current
	     * file and fix the sorting sequence that leave_mbox may have
	     * changed for its own purposes */

	    return;

	if (result < 0) {
	    lib_error(CATGETS(elm_msg_cat, ElmSet, ElmFailedResyncFolder,
			      "Failed to resync folder!"));
	    sleep_message();

	    return;
	}
  
	sc = get_storage_count(*mailbox);
	for (i = 0; i < sc; i++) {
	    struct current_storage *storage = get_storage(*mailbox,i);
	    
	    if (!storage ||
		! storage->current_folder || !storage->headers) 
		continue;

	    if (!sessionlock_folder(storage->current_folder,
				    SESSIONLOCK_CHECK)) {
		err = errno;
;
		DPRINT(Debug,1,(&Debug, 
				"Error: given file %s as folder - unreadable (%s)!\n", 
				storage->current_folder->cur_folder_sys, 
				error_description(err)));

		lib_error(CATGETS(elm_msg_cat, ElmSet, ElmCantOpenFolderRead,
				  "Can't open folder '%S' for reading: %s"), 
			  storage->current_folder->cur_folder_disp,
			  error_description(err));

		Raw_OFF(1);  /* Will reprint error */

		leave(0);
	    }
	    
	    newmbox_1(storage->current_folder, mailbox,1, 
		      page);
	}
    }

    {
	struct menu_common MENU;
	set_mcommon_from_mbxview(&MENU,*mailbox);

	get_page(&MENU, LOC->header_page);  /* resort_mailbox no longer call get_page() */
    }

    menu_trigger_redraw(LOC->header_page);
    menu_trigger_redraw(LOC->title_page);

    return;
}

struct MailboxView * change_file(mailbox, aview, page)
     struct MailboxView *mailbox;
     struct AliasView *aview;
     struct menu_context  *page;
{
    /* Prompt user for name of folder to change to.
     * If all okay with that folder, leave the current folder.
     * If leave goes okay (i.e. no new messages in current folder),
     * change to the folder that the user specified.
     *
     * If redraw is needed use menu_trigger_redraw(page)
     */

    int redraw = FALSE;    
    struct folder_info *new_folder = NULL;
    struct folder_browser * XXX = new_browser(selection_folder);
    struct string         * buffer = NULL;
    struct MailboxView    * new_mailbox = NULL;

    int file_changed = 0;

    
    /* For some reason 0 is OK on input routine */

 ask_again:
    while(1) {
	int mbc;
	int i;

	new_folder = folder_browser(page,
				    XXX,&buffer,
				    aview,
				    CATGETS(elm_msg_cat, ElmSet, 
					    ElmChangeToWhichFolder,
					    "Change to which folder: "));
	if (!new_folder)
	    break;
	
	
	DPRINT(Debug,3,(&Debug,  
			"change_file(%p): new folder = %p\n",
			mailbox,new_folder));

	mbc = get_storage_count(mailbox);
	
	for (i = 0; i < mbc; i++) {
	    struct current_storage *storage = get_storage(mailbox,i);

	    if (!storage)
		continue;

	    /* don not accept the same file as the current */
	    if (storage->current_folder && 
		selection_is_folder(XXX,storage->current_folder)) {
		lib_error(CATGETS(elm_msg_cat, ElmSet, 
				  ElmAlreadyReadingThatFolder,
				  "Already reading that folder!"));
	    
		/* Closing of IMAP folder may clear that error message
		 * se sleep on here ...
		 */

		FlushBuffer();
		sleep_message();
		
		leave_old_folder(&new_folder,CLOSE_NORMAL);
		
		goto ask_again;	/* prompt again */
	    }	
	}
	
	/* Make sure this is a file the user can open, unless it is 
	 * the default mailfile, which is openable even if empty 
	 */
			       
	if (!sessionlock_folder(new_folder,SESSIONLOCK_CHECK)) {
	    leave_old_folder(&new_folder,CLOSE_NORMAL);
		
	    continue; 	/* prompt again */
	}
	
	file_changed = 1;
	break;	/* exit loop - we got the name of a good file */
	
    }
	    
    if (file_changed) {
	int LINES, COLUMNS;

    rechange:

	menu_get_sizes(page, &LINES, &COLUMNS);   

	/* All's clear with the new file to go ahead and leave the current. */
	MoveCursor(LINES-4, 30);
	CleartoEOS();
	
	
	if (mailbox) {
	    int result;

	    if (redraw) {
		/* Show page what we are leaving... */
		menu_ClearScreen(page);      

		/* Call refresh routines of children */
		menu_redraw_children(page);
	    }

	    result = sync_mbox(FALSE, FALSE, TRUE, &new_folder, mailbox,
			       page);
	    if(result ==-1) {
		/* new mail - leave not done - ca not change to another file 
		 * yet check for change in mailfile_size in main() will do the
		 * work of calling newmbox to add in the new messages to the 
		 * current file and fix the sorting sequence that leave_mbox 
		 * may have changed for its own purposes */
		
		goto fail;
	    }
	    
	    

	    if (result < 0) {
		int ans;
	    
		lib_error(CATGETS(elm_msg_cat, ElmSet, ElmFailedLeaveFolder,
				  "Failed to leave folder!"));
		sleep_message();
		
	    again1:
		menu_get_sizes(page, &LINES, &COLUMNS);   

		/* NOTICE: prompt_letter may return EOF */
		ans = prompt_letter(LINES-1,"P",*def_ans_no,
				    PROMPT_center|PROMPT_yesno|
				    PROMPT_redraw_mark|PROMPT_ctrlL,
				    page,
				    CATGETS(elm_msg_cat, ElmSet, ElmFailedLeaveChange,
					    "Failed to leave folder: Try to change again ? ([P]anic/%c/%c) "),
				    *def_ans_yes, *def_ans_no);

		
		if (ans == ('L'&31) || ans == REDRAW_MARK) {
		    redraw = 1;
		    menu_ClearScreen(page);      
		    goto again1;
		}
		if (ans == *def_ans_no) {
		    goto fail;
		}
		if (ans == *def_ans_yes) {
		    clear_error();
		    goto rechange;
		}
		if (ans == 'P' || ans == EOF)
		    emergency_exit(0);
		
		goto fail;
	    }
	
	    /* sync_mbox may have closed new_folder ... */
	    
	    if (!new_folder) {
		DPRINT(Debug,3,(&Debug,  
				"--> Need reopen new folder...\n"));

		new_folder = folder_from_dir_item(XXX);		

		if (!new_folder ||
		    !sessionlock_folder(new_folder,SESSIONLOCK_NORMAL)) {

		    /* FIXME: Is this required? */
		    emergency_exit(0);
		    
		    goto fail;   /* not used */
		}
		    
	    }	    
	}    
      
	if (set_window_title || with_title)
	    set_folder_window_title(new_folder,
				    wanted_title_string,
				    wanted_icon_string);

	/* folder is set to NULL, if it is not needed
	   to free
	   
	   read in the folder! 
	*/
	new_mailbox = enter_mailbox(&new_folder,page);

	/* No redraws before data is updated ... */
    }   

fail:

    if (new_folder) {
	leave_old_folder(&new_folder,CLOSE_NORMAL);
    }


    if (buffer)
	free_string(&buffer);

    free_dir(&XXX);

    if (redraw)
	menu_trigger_redraw(page);

    if (!new_mailbox) {
	DPRINT(Debug,3,(&Debug,  
			"change_file(%p) = NULL\n",mailbox));	
    } else {
	DPRINT(Debug,3,(&Debug,  
			"change_file(%p) = %p\n",mailbox,new_mailbox));
    }
    
    return new_mailbox;
}

/*
 * Local Variables:
 *  mode:c
 *  c-basic-offset:4
 *  buffer-file-coding-system: iso-8859-1
 * End:
 */


syntax highlighted by Code2HTML, v. 0.9.1