# $Id: FolderMenus.pm,v 1.2 2001/02/03 16:22:54 muhri Exp $
# -*- perl -*-
package Pronto::FolderMenus;
use strict;

use vars qw( %pmv_items %pmv_menus %pcp_items %pcp_menus
%cp_menu_items  %cp_menu_menus %mv_menu_items %mv_menu_menus );

sub init_folder_menu {

       	($mv_menu_menus{0},$pmv_menus{0},$cp_menu_menus{0},$pcp_menus{0}) = mvcp_menu(0);
	$mv_menu_items{0} = new Gtk::MenuItem(_("Move"));
      	$pmv_items{0} = new Gtk::MenuItem(_("Move"));
	$cp_menu_items{0} = new Gtk::MenuItem(_("Copy"));
      	$pcp_items{0} = new Gtk::MenuItem(_("Copy"));
	$mv_menu_items{0}->set_submenu($mv_menu_menus{0});
 	$pmv_items{0}->set_submenu($pmv_menus{0});
 	$cp_menu_items{0}->set_submenu($cp_menu_menus{0});
 	$pcp_items{0}->set_submenu($pcp_menus{0});	
 
 return 1;
}

sub mvcp_menu {
	my($id) = @_;
	my($sql,$query,@ret,$menuitem,$menuitem2,$menuitem3,$menuitem4,@row);
	$ret[0] = new Gtk::Menu();
	$ret[1] = new Gtk::Menu();
	$ret[2] = new Gtk::Menu();
        $ret[3] = new Gtk::Menu();
	if($id!=0 && $id<1000) {
		$menuitem = new Gtk::MenuItem($main::folderidtoname{$id});
                $menuitem->signal_connect("activate" => \&Pronto::MainWindow::handle_move_msgs, $id);
                $menuitem->show;
                $ret[0]->append($menuitem);
		$menuitem = new Gtk::MenuItem;
                $menuitem->show;
		$ret[0]->append($menuitem);
		$menuitem2 = new Gtk::MenuItem($main::folderidtoname{$id});
                $menuitem2->signal_connect("activate" => \&Pronto::MainWindow::handle_move_msgs, $id);
                $menuitem2->show;
                $ret[1]->append($menuitem2);
                $menuitem2 = new Gtk::MenuItem;
                $menuitem2->show;
		$ret[1]->append($menuitem2);
		$menuitem = new Gtk::MenuItem($main::folderidtoname{$id});
                $menuitem->signal_connect("activate" => \&Pronto::MainWindow::handle_copy_msgs, $id);
                $menuitem->show;
                $ret[2]->append($menuitem);
                $menuitem = new Gtk::MenuItem;
                $menuitem->show;
                $ret[2]->append($menuitem);
                $menuitem2 = new Gtk::MenuItem($main::folderidtoname{$id});
                $menuitem2->signal_connect("activate" => \&Pronto::MainWindow::handle_copy_msgs, $id);
                $menuitem2->show;
                $ret[3]->append($menuitem2);
                $menuitem2 = new Gtk::MenuItem;
                $menuitem2->show;
                $ret[3]->append($menuitem2);
	}
	$sql = "select name,id from boxlist where parent=$id order by sibling";
 	$query = $main::conn->prepare($sql);
 	$query->execute();
 	while (@row=$query->fetchrow_array()) {
 	       if ($row[1] >= 1000) { next }
 	       my ($menuitem,$menuitem2,$menuitem3,$menuitem4) = &mvcp_menuitem($row[0],$row[1],$row[2],$row[3]);
 	       $ret[0]->append($menuitem);
 	       $ret[1]->append($menuitem2);
	       $ret[2]->append($menuitem3);
               $ret[3]->append($menuitem4);
 	}
	return @ret;
}
	

sub mvcp_menuitem {
	my ($name,$id) = @_;
	my($sql,$query,@row,@ret,@menu,$menuitem);
	$ret[0] = new Gtk::MenuItem($name);
	$ret[1] = new Gtk::MenuItem($name);
	$ret[2] = new Gtk::MenuItem($name);
        $ret[3] = new Gtk::MenuItem($name);
	$ret[0]->show;
	$ret[1]->show;
	$ret[2]->show;
        $ret[3]->show;
	$sql = "select name,id from boxlist where parent=$id order by sibling";
	$query = $main::conn->prepare($sql);
	$query->execute();
	if(@row=$query->fetchrow_array()) {
		@menu = mvcp_menu($id);
	}
	if(defined $menu[0]) {
		$ret[0]->set_submenu($menu[0]);
		$ret[1]->set_submenu($menu[1]);
		$ret[2]->set_submenu($menu[2]);
                $ret[3]->set_submenu($menu[3]);
		$ret[0]->signal_connect("destroy" =>
			sub {
				$menu[0]->destroy;
			});
		$ret[1]->signal_connect("destroy" =>
                        sub {   
                                $menu[1]->destroy;
                        });
		$ret[2]->signal_connect("destroy" =>
                        sub {
                                $menu[2]->destroy;
                        });
                $ret[3]->signal_connect("destroy" =>
                        sub {
                                $menu[3]->destroy;
                        });
                $mv_menu_menus{$id} = $menu[0];
                $pmv_menus{$id} = $menu[1];
		$cp_menu_menus{$id} = $menu[2];
                $pcp_menus{$id} = $menu[3];
	}
	else {
		$ret[0]->signal_connect("activate" => \&Pronto::MainWindow::handle_move_msgs, $id);
		$ret[1]->signal_connect("activate" => \&Pronto::MainWindow::handle_move_msgs, $id);
		$ret[2]->signal_connect("activate" => \&Pronto::MainWindow::handle_copy_msgs, $id);
		$ret[3]->signal_connect("activate" => \&Pronto::MainWindow::handle_copy_msgs, $id);
	}
       	$mv_menu_items{$id} = $ret[0];
        $pmv_items{$id} = $ret[1];
	$cp_menu_items{$id} = $ret[2];
        $pcp_items{$id} = $ret[3];
	return @ret;
}


1;
