#include <QtGui>


#include "utility.h"


void utility::LogMessage(char priority ,char* message){

syslog(LOG_MAKEPRI(LOG_USER, priority),"%s",message);	

}

bool utility::SetKeyboardMode(KeybMode mode){

fprintf(stdout,"Mode changed %u \n", mode);	

QFile filek("/var/pipes/prstd_in");	

		if (!filek.open(QIODevice::WriteOnly))
 		return false;


		if(mode==KBMode_Numeric){
		filek.write("KN",2);
		//fprintf(stdout,"KN");
		}
		else if(mode==KBMode_Leaving){
		filek.write("KL",2);
		//fprintf(stdout,"KN");
		}
		else if(mode==KBMode_Alpha){	
		filek.write("KA",2);
		//fprintf(stdout,"KN");
		}
		else{ 
		return false;
		}
		
		filek.close();
		return true;

		
}

int utility::PixmapMessageBox(QString title, QString text, QPixmap picture, QMessageBox::StandardButtons buttons){
	QMessageBox message;
	message.setWindowTitle(title);
	message.setText(text);
    message.setStandardButtons(buttons);
    message.setIconPixmap(picture);   // here is the error
    return(message.exec()); 
}


QChar utility::WinToUtf(char key){

switch (key){
	case 0x9D : //ť
	return(0x165);
	break;
	case 0x9E : //ž
	return(0x17E);
	break;
	case 0xE1 : //á
	return(0xE1);
	break;
	case 0xE9 : //é
	return(0xE9);
	break;
	case 0xE8 : //č
	return(0x10D);
	break;
	case 0xEC : //ě
	return(0x11B);
	break;
	case 0xED : //í
	return(0xED);
	break;
	case 0xEF : //ď
	return(0x10F);
	break;
	case 0xF2 : //ň
	return(0x148);
	break; 
	case 0xF3 : //ó
	return(0xF3);
	break;
	case 0xF8 : //ř
	return(0x159);
	break; 
	case 0xF9 : //ů
	return(0x16F);
	break;
	case 0xFD : //ý
	return(0xFD);
	break;
	case 0x9A : //š
	return(0x161);
	break;
	default :
	return(key);
  }

}


QHBoxLayout* utility::CreateHeader(QString title,QString picture){


	QHBoxLayout* headerLayout = new QHBoxLayout;
	QLabel* Title = new QLabel(title);
	Title->setStyleSheet(QString("* { font-weight: bold; margin: 10px; background-image: url(/sdcard/"+picture+".png); background-repeat: none; background-position: left; background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop:0 rgb(158,169,244), stop:1 rgba(234,234,234)); background-clip: margin; background-origin: margin; }"));
	
	headerLayout->addWidget(Title,Qt::AlignLeft | Qt::AlignTop);
	headerLayout->setContentsMargins(0,0,0,0);
	return(headerLayout);

}

void PLineEdit::Keypressed(QByteArray &keyt){
QChar key= utility::WinToUtf(keyt[1]);;
if((key=='*')||this->text().length()==0){
		this->setText(this->text().insert(this->text().length() ,' '));
		}	
		else if((key=='#')){
		this->backspace();		
		}		
		else if(this->text().length()==1)
		this->setText(this->text().replace(this->text().length()-1,1,key.toUpper()));
		else
		this->setText(this->text().replace(this->text().length()-1,1,key));



}

void PRights::LoadRights(QByteArray rights){

Rights=rights;
if((rights.at(2)<'0') || (rights.at(2)>'9')) rights[2]=NULL;
this->Group = rights.mid(1).toInt();

}

char PRights::GetRights(){

return Rights[0];

}


int PRights::GetGroup(){

return Group;

}
PRights::PRights(){}

bool PRights::isAdmin(){

return((Rights[0]==SuperAdmin) || (Rights[0]==GroupAdmin));

}

