// main.h :

#include <stdio.h>
#include <qapplication.h>
#include <qpushbutton.h>
#include <qvbox.h>
#include <qfont.h>
#include <qstring.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qpixmap.h>
#include <qprocess.h>
#include <qmessagebox.h>
#include <qtimer.h>
#include <qobject.h>

class HauptFenster : public QWidget
{    
    //Q_OBJECT
public:
    HauptFenster( QWidget *parent=0, const char *name=0 );
     
     void init(void);
     void timer(void);
     
public slots:
    void work(); 
     
};


// main .cpp :


#include "main.h"

char* readFileName(QString);

HauptFenster::HauptFenster( QWidget *parent, const char *name )
        : QWidget( parent, name )
{    
    QPushButton *quit = new QPushButton( "Quit", this, "quit" );
    quit->setFont( QFont( "Times", 10, QFont::Bold ) );
    quit->resize( 75, 30 );
    connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
    this->timer();
}   

void HauptFenster::timer(void)
{
    QTimer *timer = new QTimer( this );
    connect( timer, SIGNAL(timeout()),  SLOT(work()) );
    timer->start( 5000, TRUE ); // 5 seconds single-shot timer
}

void HauptFenster::init(void)
{
    QString s1 = readFileName("/home/lmartens/DVD/player1.tox");
    QLabel *text1 = new QLabel(" ",this, "test1");
    text1->setBackgroundOrigin(AncestorOrigin);
    text1->setAlignment( AlignHCenter | AlignBottom);
    text1->setText( s1 );
    text1->setFont( QFont( "Times", 20, QFont::Bold,true ) );
    
    QString s2 = readFileName("/home/lmartens/DVD/player2.tox");
    QLabel *text2 = new QLabel(" ",this, "test2");
    text2->setBackgroundOrigin(AncestorOrigin);
    text2->setAlignment( AlignHCenter | AlignBottom);
    text2->setText( s2 );
    text2->setFont( QFont( "Times", 20, QFont::Bold,true ) );    
    
    QString s3 = readFileName("/home/lmartens/DVD/player3.tox");
    QLabel *text3 = new QLabel(" ",this, "test3");
    text3->setBackgroundOrigin(AncestorOrigin);
    text3->setAlignment( AlignCenter );
    text3->setText( s3 );
    text3->setFont( QFont( "Times", 20, QFont::Bold,true ) );
    
    QString s4 = readFileName("/home/lmartens/DVD/player4.tox");
    QLabel *text4 = new QLabel(" ",this, "test4");
    text4->setBackgroundOrigin(AncestorOrigin);
    text4->setAlignment( AlignCenter );
    text4->setText( s4 );
    text4->setFont( QFont( "Times", 20, QFont::Bold,true ) );
    
   
    QGridLayout *grid = new QGridLayout( this, 6, 6, 10 );
    grid->addWidget( text1, 2, 1 );
    grid->addWidget( text2, 2, 4 );
    grid->addWidget( text3, 5, 1 );
    grid->addWidget( text4, 5, 4 );
}


void HauptFenster::work()
{
    if(vfork()==0)
    {
	execl("/bin/bash","bash","-c","xine --auto-play=h -G 320x288+160+62 --no-logo -B -l=loop -p /home/lmartens/DVD/player1.tox &",NULL);
    }
     if(vfork()==0)
    {
	execl("/bin/bash","bash","-c","xine --auto-play=h -G 320x288+800+62 --no-logo -B -l=loop -p /home/lmartens/DVD/player2.tox &",NULL);
    }
     if(vfork()==0)
    {
	execl("/bin/bash","bash","-c","xine --auto-play=h -G 320x288+160+574 --no-logo -B -l=loop -p /home/lmartens/DVD/player3.tox &",NULL);
    }
     if(vfork()==0)
    {
	execl("/bin/bash","bash","-c","xine --auto-play=h -G 320x288+800+574 --no-logo -B -l=loop -p /home/lmartens/DVD/player4.tox &",NULL);
    }
   
}

char* readFileName(QString name) 
{
   char **var;
   FILE *file;
   int BUFSIZE = 1024;
   char buffer[BUFSIZE];
   char *p;
   char *delims = { "=  \n" };
   int len, i= 0, j = 0;
   char* s = {" "};
   char str[1024];
   
// Lies alle Zeilen der Datei
   file = fopen(name, "r");
   if(file==NULL)
   {
      perror("READ CONFIG :" );
      return NULL;
   }
   while (fgets(buffer, BUFSIZE, file) != NULL) 
   {
       
       // Teile die Zeile am "=" auf
       p = strtok(buffer, delims);
       
       if( p!=NULL )
       {
	   while((p[i] == ' ') || (p[i] == '\t'))
	       i++;
	   
	   while ((p[i] != ' ') && (p[i] != '\t') && (p[i]!='\0'))
	   {
	       str[j] = p[i];
	       i++;	 
	       j++;
	   }
	   str[j] = '\0';
	   j=0;
	   i=0;
       }
       var = 0;
        	
       // Speichere die variable, die belegt wird, in 'var'
       if (str!=NULL) 
       {
          if (strcmp(str, "name")==0) 
	     var = &s;
          
          p = strtok(NULL, delims);
       }
	
       // Lies den wert, der zugeordnet wird, und speichere ihn in 'var'
       if (p!=NULL && var!=0) 
       {
          len = strlen(p);
          *var = (char*)calloc(len+1, sizeof(char));
	  strcpy(*var, p);
       }
   }
   fclose(file);
   while(s[i] != ';')
       i++;
   s[i] = '\0';
   i = 0;
  
   return s;
}

int main( int argc, char **argv )
{
    QApplication a( argc, argv );
    
    QString bild = "hintergrund.png";
    QPixmap *background = new QPixmap(bild,0);
    
    HauptFenster w;
    w.setGeometry( 1280, 1024, 0, 0 );
    w.setPaletteBackgroundPixmap ( *background );
    w.init();
    w.showFullScreen();
    a.setMainWidget( &w );
   
    return a.exec();
}
 
