<?vsp 
--  
--  $Id: folder.vsp,v 1.2 2006/08/15 19:30:34 source Exp $
--  
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--  
--  Copyright (C) 1998-2006 OpenLink Software
--  
--  This project is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; only version 2 of the License, dated June 1991.
--  
--  This program is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details.
--  
--  You should have received a copy of the GNU General Public License along
--  with this program; if not, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--  
--  
 http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
 declare _mm_bcc, _mm_body, _mm_cc, _mm_fld, _mm_from, _mm_own, _mm_rec_date, _mm_snd_time, _mm_subj, _mm_to varchar;
 declare _mm_id varchar;
   declare _user_name varchar;

 declare _login_ok, _to_do varchar;
  declare _counter1, _counter2 integer;
  declare _sid, _folder1, _new_folder_name, _new_folder_in_db varchar;
 declare _old_folder_name varchar;
  declare _random1 integer;

  _random1 := rnd(9999999);
  _sid := '';
  _sid := get_keyword('SESS', params);
 _folder1 := get_keyword('FOLDER', params);
 _new_folder_name := get_keyword('NEW_FOLDER_NAME', params);
 _old_folder_name := get_keyword('OLD_FOLDER_NAME', params);
 _to_do := get_keyword('TO_DO', params);
   _login_ok := 'NO';
   _counter2 := 1;
   _new_folder_in_db := 'YES';

 if (_sid   <> '')
 {
  for select _username, _userpass from wapsession where sid = _sid  do
  {
   _login_ok := 'YES';
   _user_name := _username;
   update wapsession set _time_last_active = datestring ( now() ) where  sid = _sid;
  }
  }else{
    _login_ok := 'NO';
  }

if (_login_ok = 'YES')
{
  if(_to_do = 'OPTIONS')
  {
  --displaying what options can be performed on the folder
  http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
 <card id="page1" title="<?=_folder1?>" >
 <do type="option" label="Mail Menu">
 <?vsp
  http(sprintf('<go href="checkmail.vsp?SESS=%s&amp;LIST=1&amp;FOLDER=%s"/>\n',_sid, 'Inbox'));
 ?>
   </do>
    <p>Folder: <?=_folder1?></p>
   <?vsp
           http(sprintf('<p><a href="checkmail.vsp?SESS=%s&amp;LIST=1&amp;FOLDER=%s">View Messages</a></p>\n',_sid, _folder1));
           http(sprintf('<p><a href="#rename">Rename Folder</a></p>\n'));
           http(sprintf('<p><a href="#delete">Delete Folder</a></p>\n'));
   ?>
  </card>

  <card id="rename" title="Rename <?=_folder1?>">
     <do type="accept" label="OK">
     <go href="#rename1"></go>
   </do>
   <do type="help" label="Back">
     <prev/>
   </do>
   <p>
  Enter New Folder Name:
  <input name="new_folder" title="Login Name" value="" tabindex="2" maxlength="30" />
 </p>
  </card>

  <card id="rename1" title="Are you sure?">
   <do type="options" label="Back">
     <prev/>
   </do>
   <p>Are you sure to rename <?=_folder1?> to $(new_folder)?</p>

  <p><a href="folder.vsp?SESS=<?=_sid?>&amp;FOLDER=<?=_folder1?>&amp;TO_DO=RENAME&amp;NEW_FOLDER_NAME=$(new_folder:e)" title="YES">YES</a></p>
  <p><a href="#page1" title="NO">NO</a></p>
  </card>

  <card id="delete" title="Are you sure?">
   <do type="options" label="Back">
     <prev/>
   </do>
   <p>
  All message in <?=_folder1?> will be deleted.
  Are you sure?
 </p>

  <p><a href="folder.vsp?SESS=<?=_sid?>&amp;FOLDER=<?=_folder1?>&amp;TO_DO=DELETE" title="Delete">Delete</a></p>
  <p><a href="#page1" title="Cancel">Cancel</a></p>
  </card>
</wml>
<?vsp
 }
 if (_to_do = 'RENAME')
 {
  if (not exists (select 1 from wapfolders where wap_username = _user_name and wap_folder = _new_folder_name))
  {
   --when it is ok to rename the folder
   update wapfolders set wap_folder = _new_folder_name where wap_username = _user_name and wap_folder = _old_folder_name;
   update MAIL_MESSAGE set MM_FLD = _new_folder_name where MM_OWN = _user_name and MM_FLD = _old_folder_name;
   http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
 <card id="main1" title="<?=_new_folder_name?>" >
   <onevent type="onenterforward">
          <refresh>
   <setvar name="sid" value="<?=_sid?>"/>
   <setvar name="new_folder" value=""/>
   <setvar name="new_folder1" value=""/>
          </refresh>
   </onevent>
 <do type="accept" label="Continue">
 <?vsp
  http(sprintf('<go href="checkmail.vsp?SESS=%s&amp;OPEN=Inbox"/>\n',_sid));
 ?>
   </do>
   <do type="options" label="Options">
    <go href="#opt"/>
   </do>
    <p> Folder renamed to <?=_new_folder_name?></p>
 <p><a href="checkmail.vsp?SESS=$(sid:e)&amp;REF=<?vsp http(sprintf('%d',_random1));?>">Mail Menu</a></p>

  </card>
 <card id="opt" title="Options">
   <do type="options" label ="Exit">
    <go href="#main1"/>
 </do>
     <p><a href="checkmail.vsp?SESS=$(sid:e)&amp;REFRESH=<?=_random1?>">Mail Menu</a></p>
   <p><a href="logout.vsp?SESS=$(sid:e)">Logout</a></p>
   <p><a href="#main1">Exit</a></p>
   </card>

</wml>
<?vsp
  }
  else
  {
  --when it is NOT ok to rename the folder cos' the new name is already in the database
  http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
 <card id="page1" title="<?=_folder1?>" >
 <do type="accept" label="Mail Menu">
 <?vsp
  http(sprintf('<go href="checkmail.vsp?SESS=%s&amp;OPEN=Inbox"/>\n',_sid, 'Inbox'));
 ?>
   </do>
   <do type="options" label="Options">
    <go href="#opt"/>
   </do>
    <p>
      <?=_new_folder_name?> Already Exists
    </p>
  </card>
 <card id="opt" title="Options">
   <do type="options" label ="Exit">
    <go href="#main1"/>
 </do>
     <p><a href="checkmail.vsp?SESS=$(sid:e)&amp;REFRESH=<?=_random1?>">Mail Menu</a></p>
   <p><a href="logout.vsp?SESS=$(sid:e)">Logout</a></p>
   <p><a href="#main1">Exit</a></p>
   </card>
</wml>
<?vsp
  }
 }
 if (_to_do = 'DELETE')
 {
  -- when the user wants to delete a folder
  delete from MAIL_MESSAGE where MM_OWN = _user_name and MM_FLD = _old_folder_name;
  update wapfolders set wap_active = 0 where wap_username = _user_name and wap_folder = _old_folder_name;
  http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
 <card id="main1" title="<?=_folder1?>" >
   <onevent type="onenterforward">
          <refresh>
   <setvar name="sid" value="<?=_sid?>"/>
   <setvar name="new_folder" value=""/>
   <setvar name="new_folder1" value=""/>
          </refresh>
   </onevent>
 <do type="accept" label="Continue">
 <?vsp
  http(sprintf('<go href="checkmail.vsp?SESS=%s"/>\n',_sid, 'Inbox'));
 ?>
   </do>
   <do type="options" label="Options">
    <go href="#opt"/>
   </do>
     <p> Folder Deleted    </p>
 <p><a href="checkmail.vsp?SESS=$(sid:e)&amp;REFRESH=<?=_random1?>">Mail Menu</a></p>
  </card>

 <card id="opt" title="Options">
   <do type="options" label ="Exit">
    <go href="#main1"/>
 </do>
     <p><a href="checkmail.vsp?SESS=$(sid:e)&amp;REFRESH=<?=_random1?>">Mail Menu</a></p>
   <p><a href="logout.vsp?SESS=$(sid:e)">Logout</a></p>
   <p><a href="#main1">Exit</a></p>
   </card>
</wml>
<?vsp
 }
}

if(_login_ok = 'NO'){ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
  <card id="P1" title="Wrong" >
    <p mode="nowrap">
   Wrong Login
    </p>
  </card>
</wml>
<?vsp
}
?>
