CitySelectDlg::CitySelectDlg(QString str1,QString str2,QString str3):ZPopup()
{
    setTitle(str1);
    listBox = new ZListBox("%I%M",this);
    
    // insert menu items with images from SysRegistry
    ZConfig config ( getAppDir() + "cities.ini", false );
    QStringList grouplist;
    config.getGroupsKeyList ( grouplist );
    QString cityCode = "";
    
    for ( QStringList::Iterator it = grouplist.begin(); it != grouplist.end(); ++it ) 
    {
        cityCode = config.readEntry(*it, "Code", "");
        City *city = new City(listBox);
        qDebug(*it);
        qDebug(cityCode);
        city->setName(*it);
        city->setCode(cityCode);
        
        listBox->insertItem(city);
        city->appendSubItem(1,ctr(city->getName()));
        
    }
    
    insertChild(listBox);
    setSoftkeyText(str2,str3);
    connect(this,SIGNAL(leftSoftkeyClicked()),this,SLOT(slotSelected()));
    connect(listBox,SIGNAL(returnPressed(ZSettingItem *)),this,SLOT(slotItemClicked(ZSettingItem *)));
    
}
void ZAddAppDlg::procReg(QString FileRegistry)
{
	QString id;
	QString name;
	QString ico;
	static int i=0;
	
	ZConfig confReg ( FileRegistry, false );
	QStringList grouplist;
	confReg.getGroupsKeyList ( grouplist );
	for ( QStringList::Iterator it = grouplist.begin(); it != grouplist.end(); ++it ) 
	{	 
		name = confReg.readEntry ( *it, "Name", "" );
		id = confReg.readEntry ( *it, "AppID", "" );
		ico = confReg.readEntry ( *it, ICON_NAME, "" );
		#ifdef NEW_PLATFORM
		if (ico=="")
		{
			ico = confReg.readEntry ( *it, "Icon", "" );
		}		
		#endif		
		if (ico=="")
		{
			ico = confReg.readEntry ( *it, "BigIcon", "" );
		}		
		if ( (id!="")&&(name!=""))
		{
			AppRegistry app;
			app.setId(id);
			app.setName(name);
			#ifndef NEW_PLATFORM
			if ( ico.find(".g") == -1 && !QFile::exists(ico) )
			#else
			if ( ico.find(".g") == -1 && ico.find(".k") == -1 &&!QFile::exists(ico) )
			#endif
			{
					ico = confReg.readEntry ( *it, "Directory", "" ) + "/" + ico;
			}	
			app.setIcon(ico);
			mapApp[ i ] = app;
			addApp(name, ico, i);
			++i;
		}
	}	
}
bool xInstaller::run( QString sPakgPath )
{
	QCopChannel::send( "zPkgInstaller", "proc_show()" );
	qApp->processEvents();
	qApp->processEvents();
	//unpack pack
	{
		createTmpDir(sTemp);
		system( QString(PATH_7Z).arg( sPakgPath ).arg( sTemp ).utf8() );
		
		QString tarName = sPakgPath.right( sPakgPath.length() - sPakgPath.findRev('/') );
		tarName = tarName.left( tarName.findRev('.') );
		toLog( "Check tar: " + sTemp+tarName );
		if ( QFile::exists( sTemp+tarName ) )
		{
			system( QString(PATH_7Z).arg( sTemp+tarName ).arg( sTemp ).utf8() );	
			system( QString("rm %1").arg( sTemp+tarName ).utf8() );	
		}
		tarName = tarName +".tar";
		toLog( "Check tar: " + sTemp+tarName );
		if ( QFile::exists( sTemp+tarName ) )
		{
			system( QString(PATH_7Z).arg( sTemp+tarName ).arg( sTemp ).utf8() );	
			system( QString("rm %1").arg( sTemp+tarName ).utf8() );	
		}		
	}
	QCopChannel::send( "zPkgInstaller", "proc_hide()" );	
	
	//detect pak type
	PAK_TYPE type = PAK_UNKNOWN;
	QString sFolder;
	if ( QFile::exists( sTemp + "/description.ini" ) )
		type = PAK_PEP;
	else
	{
		QDir dir ( sTemp, "*" );
		dir.setFilter ( QDir::Dirs | QDir::Hidden );
		QStringList entries = dir.entryList();
		entries.sort();	

		QStringList::ConstIterator it = entries.begin();
		while ( it != entries.end() )
		{
			if ( *it != "." && *it != ".." )
			{
				sFolder = *it;
				toLog( QString( "Maybe pak folder:" ) + sFolder );
				if ( QFile::exists( sTemp + "/"+sFolder+"/"+sFolder+".cfg" ) )
					type = PAK_MGX;
			}
			it++;
		}
	}
	if ( type == PAK_UNKNOWN || bCancel )
	{
		toLog("Error: not detected pak type!");
		uiDlg->corruptFileDlg();
		cancelInstall();
		return 0;
	}
	
	toLog("Create discription app");
	AppEntry installedApp;
	installedApp.uid = AM_Global::createUuid();
	installedApp.appId = QString(installedApp.uid).right(37).left(36);
    installedApp.isLockEnabled = false;
    installedApp.isShared = false;
    installedApp.isErasable = true;
    installedApp.parentFolderUid = "abc9ddaa-6643-429b-9844-8dc429829417";
    installedApp.position = -1;
    installedApp.m_type = AM_AppLnk::NativeApp;
    
    #define toUTF8(a) QString::fromUtf8((a).utf8())
    switch ( type )
    {
		case PAK_PEP:
		{
			ZConfig cfg( sTemp + "/description.ini" );
			
			if ( cfg.readBoolEntry ( "Actions", "InstallApp", false ) )
			{
				installedApp.directory = fixDirectoryName( toUTF8(cfg.readEntry("InstallApp", "InstallPath", "")) );
				installedApp.bigIcon = fixIconName( toUTF8(cfg.readEntry("InstallApp", "BigIcon", "")), sTemp+"/app");
				installedApp.smallIcon = fixIconToSmall(fixIconName( toUTF8(cfg.readEntry("InstallApp", "Icon", "")), sTemp+"/app"), sTemp+"/app");
				installedApp.aniIcon = fixIconName( toUTF8(cfg.readEntry("InstallApp", "AniIcon", "" )), sTemp+"/app");
				installedApp.simpleIcon = toUTF8(installedApp.smallIcon);
				installedApp.name = toUTF8(cfg.readEntry("InstallApp", "Name", ""));
				installedApp.vendor = toUTF8(cfg.readEntry("InstallApp", "Author", ""));
				installedApp.nameResourceId = "";
				installedApp.version = toUTF8(cfg.readEntry ("InstallApp", "Version", ""));
				installedApp.exec = toUTF8(cfg.readEntry("InstallApp", "Exec", ""));
				installedApp.device = (installedApp.directory.find("/mmc/")==-1)?AM_Global::Flash2:AM_Global::Card1;
			} else
			if ( cfg.readBoolEntry ( "Actions", "UpdatePak", false ) )
			{
				installedApp.directory = fixDirectoryName( toUTF8(cfg.readEntry("UpdatePak", "InstallPath", "")) );
				installedApp.name = toUTF8(cfg.readEntry("UpdatePak", "Name", ""));
				installedApp.vendor = toUTF8(cfg.readEntry("UpdatePak", "Author", ""));
				installedApp.version = cfg.readEntry ( "UpdatePak", "Version", "" );
				installedApp.device = (installedApp.directory.find("/mmc/")==-1)?AM_Global::Flash2:AM_Global::Card1;
				type = PAK_PEP_UPDATE;
			}			
			break;
		}
		case PAK_MGX:
		{
			QString cfg = sTemp + "/"+sFolder+"/"+sFolder+".cfg";
			installedApp.directory = "";
			installedApp.name = toUTF8(getMgxReadConfig(cfg, "Name", ""));
			installedApp.vendor = toUTF8(getMgxReadConfig(cfg, "Author", ""));
			installedApp.nameResourceId = "";
			installedApp.version = toUTF8(getMgxReadConfig(cfg, "Version", ""));
			installedApp.exec = toUTF8(getMgxReadConfig(cfg, "Exec", ""));
			installedApp.bigIcon = fixIconName( toUTF8(getMgxReadConfig(cfg, "Icon", "")), sTemp + "/"+sFolder);
			installedApp.aniIcon = installedApp.bigIcon;
			installedApp.smallIcon = fixIconToSmall(installedApp.bigIcon, sTemp + "/"+sFolder);
			installedApp.simpleIcon = installedApp.smallIcon;
			break;
		}
		default:
			break;		
	}
    installedApp.appSize = STUFF_Global::getSize(sTemp);
    
    toLog("Check installed");
    bool bUpdateApp=false;
    QString sOldVersion="";
    
    if ( type == PAK_MGX || type == PAK_PEP )
    {
		ZConfig PhoneRegistry ( "/ezxlocal/download/appwrite/am/InstalledDB" );
		QStringList grouplist;
		PhoneRegistry.getGroupsKeyList( grouplist );
        for ( QStringList::Iterator it = grouplist.begin(); it != grouplist.end(); ++it ) 
        {
			if ( PhoneRegistry.readEntry( *it, "Name", "" ) == installedApp.name )
			{
				sOldVersion = PhoneRegistry.readEntry( *it, "Version", "" );
				installedApp.uid = QUuid(PhoneRegistry.readEntry( *it, "AppID", "" ));
				installedApp.appId = *it;
				installedApp.directory = PhoneRegistry.readEntry( *it, "Directory", "" );
				installedApp.device = AM_Global::Flash2;
				bUpdateApp=true;
				break;
			}
        }	
        if ( !bUpdateApp && uiDlg->isCardMounted() )
        {
			ZConfig CardRegistry ( "/mmc/mmca1/.system/java/CardRegistry" );
			CardRegistry.getGroupsKeyList( grouplist );
			for ( QStringList::Iterator it = grouplist.begin(); it != grouplist.end(); ++it ) 
			{
				if ( CardRegistry.readEntry( *it, "Name", "" ) == installedApp.name )
				{
					sOldVersion = CardRegistry.readEntry( *it, "Version", "" );
					installedApp.uid = QUuid(CardRegistry.readEntry( *it, "AppID", "" ));
					installedApp.appId = *it;
					installedApp.directory = CardRegistry.readEntry( *it, "Directory", "" );
					installedApp.device = AM_Global::Card1;
					bUpdateApp=true;
					break;
				}
			}				
		}
	} else
		sOldVersion="-";
	
    toLog("Show discription app");
    if ( bUpdateApp || type == PAK_PEP_UPDATE )
		bCancel = uiDlg->updateDlg(installedApp.name, sOldVersion, installedApp.version );
    else
		bCancel = uiDlg->appDetailsCardDlg("", installedApp.name, installedApp.vendor, installedApp.version, QString::number(installedApp.appSize) );
	
	if ( bCancel )
	{
		cancelInstall();
		return 0;
	}   
	
	if ( type == PAK_MGX && !bUpdateApp )
	{
		toLog("Get location app");
		uiDlg->installLocationDlg( installedApp.name, 0, installedApp.device );		
		toLog("Device id: "+QString::number( installedApp.device ) );
				
		toLog("Set directory for MGX");
		installedApp.directory = getPathByDevice(installedApp.device)+"/"+sFolder;
	}

	toLog("Check free space");	
	if ( getFileSystemFreeSize(installedApp.device) < installedApp.appSize && !bUpdateApp )
	{
		uiDlg->memFullDlg(installedApp.device, installedApp.appSize);
		cancelInstall();
		return 0;		
	}
	
	toLog("Show process dlg");
	uiDlg->initProgressBar( "", installedApp.name, "Install" );
	uiDlg->disableProgressCancel();
	
	if ( installedApp.directory.isEmpty() ||  bCancel )
	{
		uiDlg->invalidPkgDlg();
		cancelInstall();
		return 0;			
	}
	
	uiDlg->setProgressBar( 5 );
	qApp->processEvents();	
	
	toLog("Move folder");
    switch ( type )
    {
		case PAK_PEP:
		case PAK_PEP_UPDATE:
		{
			ZConfig cfg( sTemp + "/description.ini" );
			//if no need add app to menu => this pakege = update pakege
			if ( !cfg.readBoolEntry( "Actions", "AddAppToMenu", true ) ) 
				type = PAK_PEP_UPDATE;
			if ( cfg.readBoolEntry( "Actions", "PreInstall", false ) )
				system( (sTemp+"/scripts/"+cfg.readEntry( "PreInstall", "Name", "" )).utf8() );		
			
			QDir dir;
			if ( dir.exists ( installedApp.directory ) )
			{			
				system( QString ( "cp -rf %1/app/* %2" ).arg( sTemp ).arg( installedApp.directory ).utf8() );	
				system( QString ( "cp -rf %1/app/.[!.]* %2" ).arg( sTemp ).arg( installedApp.directory ).utf8() );
			} else
			{
				system( QString( "mkdir -p %1" ).arg( installedApp.directory ).utf8() );
				system( QString ( "mv -f %1/app/* %2" ).arg( sTemp ).arg( installedApp.directory ).utf8() );	
				system( QString ( "mv -f %1/app/.[!.]* %2" ).arg( sTemp ).arg( installedApp.directory ).utf8() );				
			}			
			//system( QString ( "mv -f %1/app/* %2" ).arg( sTemp ).arg( installedApp.directory ).utf8() );	
			//system( QString ( "mv -f %1/app/.[!.]* %2" ).arg( sTemp ).arg( installedApp.directory ).utf8() );
			//system( QString ( "cd %1; mkdir -p `find -type d | sed -e 's|\\./|%2/|'`; busybox_full find . -type f -exec mv -f '{}' '%3/{}' \\;" ).arg( sTemp+"/app" ).arg( installedApp.directory ).arg( installedApp.directory ).utf8() );
			//toLog( QString ( "cd %1; mkdir -p `find -type d | sed -e 's|\\./|%2/|'`; busybox_full find . -type f -exec mv -f '{}' '%3/{}' \\;" ).arg( sTemp+"/app" ).arg( installedApp.directory ).arg( installedApp.directory ).utf8() );
			if ( type != PAK_PEP_UPDATE )
			{
				if ( !installedApp.bigIcon.isEmpty() ) system( QString ( "mv -f %1/%2 %3/%4" ).arg( sTemp ).arg( installedApp.bigIcon ).arg( installedApp.directory ).arg( installedApp.bigIcon ).utf8() );
				if ( !installedApp.smallIcon.isEmpty() ) system( QString ( "mv -f %1/%2 %3/%4" ).arg( sTemp ).arg( installedApp.smallIcon ).arg( installedApp.directory ).arg( installedApp.smallIcon ).utf8() );
				if ( !installedApp.aniIcon.isEmpty() ) system( QString ( "mv -f %1/%2 %3/%4" ).arg( sTemp ).arg( installedApp.aniIcon ).arg( installedApp.directory ).arg( installedApp.aniIcon ).utf8() );
			}
			
			if ( cfg.readBoolEntry( "Actions", "AssignExt", false ) )
				extAssign( &installedApp, QStringList::split(";", cfg.readEntry("AssignExt", "Ext", "")) );
			break;
		}
		
		case PAK_MGX:
		{
			QDir dir;
			if ( dir.exists ( installedApp.directory ) )
			{
				system( QString ( "cp -rf %1/%2/* %3/" ).arg( sTemp ).arg( sFolder ).arg( installedApp.directory ).utf8() );
				system( QString ( "cp -rf %1/%2/.[!.]* %3/" ).arg( sTemp ).arg( sFolder ).arg( installedApp.directory ).utf8() );	
			} else
			{
				system( QString( "mkdir -p %1" ).arg( installedApp.directory ).utf8() );
				system( QString ( "mv -f %1/%2/* %3/" ).arg( sTemp ).arg( sFolder ).arg( installedApp.directory ).utf8() );
				system( QString ( "mv -f %1/%2/.[!.]* %3/" ).arg( sTemp ).arg( sFolder ).arg( installedApp.directory ).utf8() );					
			}
			
			//system( QString ( "cd %1; mkdir -p `find -type d | sed -e 's|\\./|%2/|'`; busybox_full find . -type f -exec mv -f '{}' '%3/{}' \\;" ).arg( sFolder ).arg( installedApp.directory ).arg( installedApp.directory ).utf8() );
			//toLog( QString ( "cd %1; mkdir -p `find -type d | sed -e 's|\\./|%2/|'`; busybox_full find . -type f -exec mv -f '{}' '%3/{}' \\;" ).arg( sFolder ).arg( installedApp.directory ).arg( installedApp.directory ).utf8() );
			break;
		}
		default:
			break;	
	}
	
	uiDlg->setProgressBar( 50 );
	qApp->processEvents();

	toLog(QString("Set chmod: ")+installedApp.directory+"/"+installedApp.exec );
	system ( QString( "chmod a+x %1/%2" ).arg( installedApp.directory ).arg( installedApp.exec ).utf8() );
	
	//Fix bad chinese script
	if ( installedApp.exec.endsWith(".sh") || installedApp.exec.endsWith(".lin") )
	{
		FILE * f = fopen( installedApp.directory+"/"+installedApp.exec, "rt" );
		if ( f )
		{
			fseek(f, 0, SEEK_END);
			int iFileSize = ftell(f);
			char * text = (char *)malloc(sizeof(char)*iFileSize);
			fseek(f, 0, SEEK_SET);		
			fread(text, sizeof(char), iFileSize, f);
			fclose(f);		
			int startId=0;
			while ( startId<iFileSize && *(text+startId) != '#' ) 
			{
				if ( *(text+startId) == 0x0A )
				{
					startId=-1;
					break;
				}
				startId++;
			}
			if ( startId > 0 )
			{
				f = fopen( installedApp.directory+"/"+installedApp.exec, "wt" );
				fwrite(text+3, sizeof(char), iFileSize-3, f); // 3 ??? maybe need change 3 to startId ???
				fclose(f);
			}
			free(text);
		}
	}
	
	uiDlg->setProgressBar( 55 );
	qApp->processEvents();
	
	if ( type != PAK_PEP_UPDATE )
	{
		toLog("Write info about program to registry");
		writeToRegistry( &installedApp );
	}
	
	toLog("Run post install script");
    switch ( type )
    {
		case PAK_PEP:
		case PAK_PEP_UPDATE:
		{
			ZConfig cfg( sTemp + "/description.ini" );
			if ( cfg.readBoolEntry( "Actions", "PostInstall", false ) )
				system( sTemp+"/scripts/"+cfg.readEntry( "PostInstall", "Name", "" )+"&" );			
			break;
		}
		default:
			break;	
	}
	
	uiDlg->setProgressBar( 90 );
	qApp->processEvents();	
	
	clearTmpDir(sTemp, false);
	
	uiDlg->setProgressBar( 100 );
	qApp->processEvents();	
	
	toLog("Close process dlg");
	uiDlg->close();
	
	toLog("Show dlg installed sucessful");
	uiDlg->appInstalledNoLaunchDlg( installedApp.name );
	
	return 1;
}