package DADA::MailingList::Settings::Db; 

	 use base "DADA::App::GenericDBFile";
 
=pod

=head1 NAME


DADA::MailingList::Settings::Db

=head1 SYNOPSIS 

 use DADA::MailingList::Settings; 
 my $ls = DADA::MailingList::Settings->new(-List => 'listshortname'); 
  
 my $list_info = $ls->get(); 
 
 $ls->save({key => 'value'});

=head1 DESCRIPTION

This module holds the DB File backend for list settings. 

=head1 Public Methods

=cut


use strict; 

use lib qw(./ ../ ../../ ../../../ ./../../DADA ../../perllib); 

use AnyDBM_File; 
use Fcntl qw(
O_WRONLY 
O_TRUNC 
O_CREAT 
O_CREAT 
O_RDWR
O_RDONLY
LOCK_EX
LOCK_SH 
LOCK_NB); 
use Carp qw(croak carp); 


use DADA::Config qw(!:DEFAULT);  
use DADA::App::Guts;  # For now, my dear. 

=pod

=head2 new

returns the DADA::Mailinglist::Settings object.

 my $ls = DADA::MailingList::Settings->new(-List => 'listshortname');

you can also optionally pass the B<-new_list => 1> parameter to circumvent list checks.

=cut


sub new {
	my $class = shift;
	
	my %args = (-List => undef,
				-new_list => 0,  
					@_); 
					     
    my $self = SUPER::new $class (
    							  function => 'settings',
    							 );  
         
       $self->{new_list} = $args{-new_list};
	   $self->_init(\%args); 
	   
	   return $self;
}



=pod

=head2 get

 my $list_info = $ls->get();

returns a hasref of the list settings. Keys are the name of the setting, value is the 
value of the setting.

You can optionally pass the B<replaced => 1> parameter to change pseudo tags in 
html and email messages to their value.

=cut




=pod 

=head2 save

 $ls->save({settingkey => 'settingvalue'});

saves a setting. Notice that this method takes a hashref, and nothing else.

=cut

sub save { 
 	my ($self, $new_settings) = @_;
	if($new_settings){  		
 		
 		$self->_existance_check($new_settings);
 		
 		$self->_open_db; 
			my %RAW_DB_HASH     = %{$self->{DB_HASH}};
			my %merge_info      = (%RAW_DB_HASH, %$new_settings); 	
			%{$self->{DB_HASH}} = %merge_info; 	
		
		
		if($self->{DB_HASH}->{list}){ 		
			#special cases:
			#unless(defined($self->{DB_HASH}->{admin_menu})){ 
			
			if(! defined($self->{DB_HASH}->{admin_menu}) || $self->{DB_HASH}->{admin_menu} eq ""){ 

			    require DADA::Template::Widgets::Admin_Menu; 
				$self->{DB_HASH}->{admin_menu} = DADA::Template::Widgets::Admin_Menu::create_save_set();
			}
			
			
			if(! defined($self->{DB_HASH}->{cipher_key}) || $self->{DB_HASH}->{cipher_key} eq ""){ 
    
			#unless(defined($self->{DB_HASH}->{cipher_key})){ 
			
			#die "yes!"; 
			
				require DADA::Security::Password; 
				$self->{DB_HASH}->{cipher_key} = DADA::Security::Password::make_cipher_key();
			}
			
			
			#/special cases:
		}else{ 
			carp "$DADA::Config::PROGRAM_NAME $DADA::Config::VER warning! listshortname isn't defined! list " . $self->{function} . " db possibly corrupted!"
				unless $self->{new_list}; 
		}
		
		$self->_close_db; 
		$self->backupToDir;
		
		require DADA::App::ScreenCache; 
		my $c = DADA::App::ScreenCache->new; 
	   	   $c->flush;
	   
		return 1; 
     }
     return 1;
}




sub perhapsCorrupted { 
	my $self = shift; 
	$self->_open_db;
	my %RAW_DB_HASH     = %{$self->{DB_HASH}};
	$self->_close_db; 
	$RAW_DB_HASH{list} ? return 1 : return 0; 
}







sub _raw_db_hash { 
	my $self = shift; 
	$self->_lock_db;	
	$self->_open_db; 
	my %RAW_DB_HASH = %{$self->{DB_HASH}};
	$self->{RAW_DB_HASH} = {%RAW_DB_HASH};
	$self->_close_db;
	$self->_unlock_db; 	
}




sub _list_name_check { 

	my ($self, $n) = @_; 
		$n = $self->_trim($n);
	return 0 if !$n; 
	return 0 if $self->_list_exists($n) == 0;  
	$self->{name} = $n;
	return 1; 
}




sub _list_exists { 
	my ($self, $n)  = @_; 
	return DADA::App::Guts::check_if_list_exists(-List => $n);
}






1;



=pod

=head1 See Also

DADA::MailingList::Settings

=head1 COPYRIGHT

Copyright (c) 1999-2007 Justin Simoni 
http://justinsimoni.com 
All rights reserved. 

This program 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; either version 2
of the License, or (at your option) any later version.

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

=cut

