/* 
  * |P|I|X|O| Image cataloging using html and thumb nails
  * Released under GPL http://www.gnu.org/copyleft/gpl.html
  * 
  * kishan@hackorama.com www.hackorama.com     2001 MAY
  *
  * Class "Page"
  * Generates the html pages, 
  * at the folder set with a call to setBase() 
  * defaults to "." if no folder is specified.
  *
  */
 
#include "page.h"


Page::Page()
{

}

Page::Page( char** list, int count )
{
	img_list = list;
	img_totalcount = count;
        index_size = 10 ;
	init();

}

Page::Page( char** list, int count , int indexsize )
{
	img_list = list;
	img_totalcount = count;
        index_size = indexsize ;
	init();
}


Page::~Page()
{
	
}

void
Page::init()
{
      if( img_totalcount > 0 ){

	IMG_NAVIG = false;
	TEXT_NAVIG = false;
	INDEX_NAVIG = true;
#ifndef _WINDOWS

	strcpy( separator, "/" );
#else

	strcpy( separator, "\\" );
#endif

	image_count = 0;
	index_count = 1 ;
	strcpy (next_img , "next.jpg" );
	strcpy (last_img, "last.jpg" );
	strcpy( base, "." );
	strcpy (target , "main" );
	strcpy(index_page_prefix, "pixo-navig-" );
	strcpy(main_page_prefix, "pixo-" );
	strcpy(thumb_image_prefix, "pixo-t-");
	strcpy(main_image_prefix, "pixo-m-");
	strcpy( startpage, "pixo-index" );
	copyright = NULL;
	imagetitle = NULL;

     }

}

void
Page::setBase( char *_base )
{
	strcpy( base , _base );
}

void
Page::setCopyright( char *cright )
{
	copyright = cright;
}

void
Page::setImagetitle( char *ititle )
{
	imagetitle = ititle;
}

void
Page::setIndexSize( int size )
{
	index_size = size;
}

void
Page::setNavigOption( bool img_navig, bool text_navig, bool index_navig )
{
	IMG_NAVIG = img_navig;
	TEXT_NAVIG = text_navig;
	INDEX_NAVIG = index_navig;
}

void
Page::process()
{
	if( !IMG_NAVIG && !INDEX_NAVIG )
		TEXT_NAVIG = true;
	startIndexPage();
        for( int i = 0 ; i < img_totalcount ; i++) {
               processOne( (char*) img_list[i] );
        }
}

void 
Page::processOne(char* img)
{
	image_name =  img ;
	updateIndices();
	printDisplayPage();
}

void Page::updateIndices()
{
		
	if( image_count > 0 ){
		if( (image_count%index_size) == 0 ){
			endIndexPage();
			index_count++;
			startIndexPage();
		}
		image_count++;
		appendIndexPage();
	}else if (image_count == 0 ) {
		image_count++;
		appendIndexPage();
	}
}


void Page::printDisplayPage()
{
#ifdef DEBUG

	cout << "	" << image_name << " to page " << main_page_prefix << image_count << endl ;
#endif /* DEBUG */


	char i_file_name[ PATH_MAX + 1];
	sprintf( i_file_name, "%s%s" , main_image_prefix, image_name );
	char file_name[PATH_MAX+1];
	resolvePath( file_name, main_page_prefix, image_count );
	main_writer.open ( file_name , ofstream::out );
	prePrintDisplayPage();
	main_writer << "<br><br>" <<  endl ;
	if( imagetitle != NULL ){
		main_writer << imagetitle  << endl ;
		main_writer << "<br><br>" << endl ;
	}
	main_writer << "<a  href=\"" << image_name  << "\">";
	main_writer << "<img src=\"" << i_file_name << "\" border=\"0\">";
	main_writer << "</a>" << endl ;
	if( copyright != NULL ){
		main_writer << "<br><br><br>" << endl ;
		main_writer << copyright << endl ;
	}
	main_writer << "<br>" << endl ;
	postPrintDisplayPage();
	main_writer.close();
		
#ifndef SILENT

	cout <<  "	printing (" << file_name <<") \t";
	cout << image_count << "/" << img_totalcount << endl ;
#endif /* SILENT */


}
void Page::startIndexPage()
{
	char file_name[PATH_MAX+1];
	resolvePath( file_name, index_page_prefix , index_count );
	index_writer.open ( file_name , ofstream::out );
	index_writer << "<html><body bgcolor=\"WHITE\" ><center><table border=0 ><tr><td>" << endl ;
	if( INDEX_NAVIG ){
		char fname[PATH_MAX+1];
		
		int pages = (img_totalcount/index_size);
		if( img_totalcount%index_size != 0 ) //kludge for bounday condition

			pages++;

		index_writer << "<table width=100% border=0><tr>" << endl ;
		index_writer << "<td align=left>" << endl ;
		index_writer << "<font size=-2 face=helvetica>" << endl ;
		if ( index_count > 1 ){
			sprintf( fname, "%s%d.html" , index_page_prefix, index_count-1 );
			index_writer << "<a style=\"text-decoration:none\" href=\"" << fname  << "\">";
			index_writer << "PREV | " ;
			index_writer << "</a> " << endl ;
		}
		if( pages > 1 ){
		  	for( int i = 1 ; i <= pages; i++ ){
				sprintf( fname, "%s%d.html" , index_page_prefix, i );
				if( i == index_count ){
					index_writer << "[<b> " << i << " </b>]" << endl ;
				}else{
					index_writer << "<a style=\"text-decoration:none\" href=\"" << fname  << "\">";
					index_writer << i ;
					index_writer << "</a> " << endl ;
				}
	        	}
		}
		if ( index_count < pages ){
			sprintf( fname, "%s%d.html" , index_page_prefix, index_count+1 );
			index_writer << "<a style=\"text-decoration:none\" href=\"" << fname  << "\">";
			index_writer << " | NEXT" ;
			index_writer << "</a> " << endl ;
		}
		index_writer << "</font>" << endl ;
		index_writer << "</td>" << endl ;
		index_writer << "<td align=right>" << endl ;
		index_writer << "<font size=-2 face=helvetica>" << endl ;
		if ( index_count < pages ){
			sprintf( fname, "%s%d.html" , index_page_prefix, index_count+1 );
			index_writer << "<a style=\"text-decoration:none\" href=\"" << fname  << "\">";
			index_writer << ">" ;
			index_writer << "</a> " << endl ;
		}
		index_writer << "</font>" << endl ;
		index_writer << "</td></tr></table>" << endl ;
		
		index_writer << "</td>" << endl ;
		index_writer << "</tr>" << endl ;
		index_writer << "<tr>" << endl ;
		index_writer << "<td>" << endl ;
		index_writer << "<table border=0 ><tr>" << endl ;
	}
	if ( (index_count > 1) && ( IMG_NAVIG || TEXT_NAVIG)){
		char last_page[PATH_MAX+1];
		sprintf( last_page, "%s%d.html" , index_page_prefix, index_count-1 );

		index_writer << "<td  align=center>" << endl ;
		index_writer << "<font size=-4 face=helvetica>" << endl ;
		index_writer << "<a style=\"text-decoration:none\" href=\"" << last_page  << "\">";
		if( IMG_NAVIG )
			index_writer << "<img src=\"" << last_img << "\" border=\"0\" >" ;
		else if (TEXT_NAVIG )
			index_writer << "<&nbsp;PREV&nbsp;" ;
		index_writer << "</a>" << endl ;
		index_writer << "</font>" << endl ;
		index_writer << "</td>" << endl ;
	}

#ifndef SILENT

	cout <<  "	printing (" << file_name <<")" << endl ;
#endif /* SILENT */


}

void Page::appendIndexPage()
{
#ifdef DEBUG

	cout << "	added  " << main_page_prefix << image_count << " to " << index_page_prefix << index_count << endl;
#endif /* DEBUG */


	char i_file_name[PATH_MAX+1];
	sprintf( i_file_name, "%s%s" , thumb_image_prefix , image_name );
	
	char main_page[PATH_MAX+1];
	sprintf( main_page, "%s%d.html" , main_page_prefix, image_count );

	index_writer << "<td><center>" << endl ;
	index_writer << "<a href=\"" << main_page  << "\" target=\"" << target << "\">" << endl ;
	index_writer << "<img src=\"" << i_file_name << "\" border=\"0\" >";
	index_writer << "</a>" << endl ;
	index_writer << "</center></td>" << endl ;

}

void Page::endIndexPage()
{
	if( ((image_count%index_size) == 0)  && ( IMG_NAVIG || TEXT_NAVIG) ){
		char next_page[PATH_MAX+1];
		sprintf( next_page, "%s%d.html" , index_page_prefix, index_count );

		index_writer << "<td  align=center>" << endl ;
		index_writer << "<font size=-4 face=helvetica>" << endl ;
		index_writer << "<a style=\"text-decoration:none\"href=\"" << next_page  << "\">" ;
		if( IMG_NAVIG )
			index_writer << "<img src=\"" << next_img << "\" border=\"0\" >" ;
		else if( TEXT_NAVIG )
			index_writer << "&nbsp;NEXT&nbsp;>" ;
		index_writer << "</a>" << endl ;
		index_writer << "</font>" << endl ;
		index_writer << "</td>" << endl ;
	}

	index_writer << "</tr></table></td></tr></table></center></body></html>" << endl ;
	index_writer.close();
}

void 
Page::cleanUp()
{
	endIndexPage();
#ifndef SILENT

	cout << endl << "Indexed  " << image_count ;
	cout << " images across " << index_count << " index pages" << endl ;
#endif /* SILENT */

	printStartPage();
}


void 
Page::prePrintDisplayPage()
{
	main_writer << "<html><body bgcolor=\"WHITE\"><center>" << endl ;
}

void 
Page::postPrintDisplayPage()
{
	main_writer << "</center></body></html>" << endl ;
}

void
Page::resolvePath( char* path , char* prefix, int count)
{
	if( strcmp( base, "." ) == 0  ){
		sprintf( path, "%s%d.html" , prefix, count );
	}else{
		char tmp[2];
		tmp[0] =  base[strlen(base)-1];
		tmp[1] =  '\0';
		if( strcmp( tmp, separator) == 0  )
			sprintf( path, "%s%s%d.html" , base, prefix, count );
		else
			sprintf( path, "%s%s%s%d.html" , base, separator, prefix, count );
	}

}

void
Page::resolvePath( char* path , char* name)
{
	if( strcmp( base, "." ) == 0  ){
		sprintf( path, "%s" , name );
	}else{
		char tmp[2];
		tmp[0] =  base[strlen(base)-1];
		tmp[1] =  '\0';
		if( strcmp( tmp, separator) == 0  )
			sprintf( path, "%s%s" , base, name );
		else
			sprintf( path, "%s%s%s" , base, separator, name );
	}

}

void
Page::printStartPage( )
{
	char name[PATH_MAX+1];
	char file_name[PATH_MAX+1];
	resolvePath( file_name , startpage );

	sprintf( name, "%s.html", file_name );

	/*if( fileExists( name ) ){
		sprintf( name, "%s-pixo.html", file_name );
	}*/

	ofstream tmp_writer;
	tmp_writer.open ( name , ofstream::out );
	tmp_writer << "<html>" <<endl;	
	tmp_writer << "<frameset rows=\"125,*\" frameborder=0 framespacing=0 border=0 resize=yes>" <<endl;	
	tmp_writer << "<frame src=" << index_page_prefix << "1.html marginwidth=0 marginheight=0 name=index>" <<endl;	
	tmp_writer << "<frame src=" << main_page_prefix << "1.html marginwidth=0 marginheight=0  name=main>" <<endl;	
	tmp_writer << "</frameset>" <<endl;	
	tmp_writer << "</html>" <<endl;	
	tmp_writer.close();

#ifndef SILENT	

	cout <<  "Please open \"" << name << "\" in a web-browser" << endl ;
#endif /* ! SILENT */


}

bool
Page::fileExists( char* filename )
{
	bool result = false;
	ifstream tmp;
	tmp.open ( filename , ifstream::in );
	if( tmp.good())
		result =  true;
	tmp.close();	
	return result;
}


syntax highlighted by Code2HTML, v. 0.9