/* $Cambridge: hermes/src/prayer/cmd/cmd_rename.c,v 1.2 2008/05/19 15:55:54 dpc22 Exp $ */
/************************************************
 *    Prayer - a Webmail Interface              *
 ************************************************/

/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */

#include "prayer_session.h"

void cmd_rename(struct session *session)
{
    struct template_vals *tvals = session->template_vals;
    struct folderlist *fl = folderlist_fetch(session);
    struct prefs *prefs = session->options->prefs;
    BOOL suppress_dotfiles = prefs->suppress_dotfiles;
    struct request *request = session->request;
    struct buffer *b = request->write_buffer;
    struct folderitem *fi;
    char *name;

    if ((request->argc == 3) && !strcmp(request->argv[1], "toggle")) {
        name = string_canon_decode(request->argv[2]);
        fi   = folderlist_lookup(folderlist_fetch(session), name);
        if (fi) {
            if (fi->expanded) {
                session_message(session, "Collapsed \"%s\"", name);
                fi->expanded = NIL;
            } else {
                folderlist_expand(session, fi);
                session_message(session, "Expanded \"%s\"", name);
                fi->expanded = T;
            }
        }
    } else if (request->argc == 2) {
        string_strdup(&session->rename_foldername, request->argv[1]);
        string_canon_decode(session->rename_foldername);
    }

    folderlist_template_vals_tree(fl, suppress_dotfiles, tvals, "@folder");
    folderlist_template_vals_list(fl, suppress_dotfiles, tvals,
                                  T, "@dirlist");
    template_vals_string(tvals, "rename_foldername",
                         session->rename_foldername);
    session_seed_template(session, tvals);
    template_expand("rename", tvals, b);

    response_html(request, 200);
}
