context: 
  using System;
  using Qyoto;
  using System.IO;
  using System.Reflection;
  using System.Collections.Generic;
  
  using comexbase;
  using monosimbase;
  
  using log4net;
  
  namespace monosimqt
  {
  	
  	
  	public partial class MainWindowClass: QMainWindow
  	{
  
  
  		// Attributes
  		private string retStr = "";
  		private string ATR = "";
  		private ThreadNotify notify = null;
  		private System.Threading.Thread simThread = null;
  		private List<string> allReaders = new List<string>();
  		private bool isEnd = false;
  		private bool isReading = false;
  		
  		
  		
  		#region Private Methods
  		
  		
  		
  		
  		
  		/// <summary>
  		/// Information about
  		/// </summary>
  		private void OpenInfo()
  		{
  			AboutDialogClass adc = new AboutDialogClass();
  			adc.Show();
  			
  		}
  		
  		
  		
  		
  		
  		/// <summary>
  		/// Show settings dialog
  		/// </summary>
  		private void OpenSettings()
  		{
  			SettingsDialogClass sdc = new SettingsDialogClass();
  			sdc.Show();
  		}
  		
  		
  		
  		
  		
  		
  		/// <summary>
  		/// Perform change of selected reader
  		/// </summary>
  		private void UpdateSelectedReader(string newSelReader)
  		{
  			GlobalObj.CloseConnection();
  			GlobalObj.SelectedReader = newSelReader;
  			mainwindow_Ui.StatusBar.ShowMessage(GlobalObj.LMan.GetString("selreader") + ": " + newSelReader);
  		}
  		
  		
  		
  		
  		
  		
  		/// <summary>
  		/// Create new contact file
  		/// </summary>
  		private void NewContactsFile()
  		{
  			mainwindow_Ui.LstFileContacts.Clear();
  			GlobalObjUI.FileContacts = new Contacts();
  			GlobalObjUI.ContactsFilePath = "";
  			UpdateFileControls(true);
  		}
  		
  		
  		
  		
  
  		/// <summary>
  		/// Update file widgets status
  		/// </summary>
  		private void UpdateFileControls(bool isSensitive)
  		{
  			mainwindow_Ui.MenuFileNew.Enabled = !isSensitive;
  			mainwindow_Ui.MenuFileOpen.Enabled = !isSensitive;
  			mainwindow_Ui.MenuFileSaveFile.Enabled = isSensitive;			
  			mainwindow_Ui.MenuFileClose.Enabled = isSensitive;
  			mainwindow_Ui.LstFileContacts.Enabled = isSensitive;
  			
  			
  			if (isSensitive)
  			{
  				// add filename to frame label
  				mainwindow_Ui.FrameFile.Title = GlobalObjUI.LMan.GetString("framefile");
  				if (GlobalObjUI.ContactsFilePath != "")
  				{
  					mainwindow_Ui.FrameFile.Title = GlobalObjUI.LMan.GetString("framefile")+
  						" [" + Path.GetFileNameWithoutExtension(GlobalObjUI.ContactsFilePath) + 
  					" - size: " + GlobalObjUI.FileContacts.SimContacts.Count.ToString() + "]"; 					
  				}
  				
  				// check for sim power on
  				if (mainwindow_Ui.LstSimContacts.Enabled)
  				{
  					mainwindow_Ui.MenuFileSaveSim.Enabled = isSensitive;
  				}
  				else
  				{
  					mainwindow_Ui.MenuFileSaveSim.Enabled = false;
  				}
  			}
  			else
  			{
  				// clear frame label
  				mainwindow_Ui.FrameFile.Title = GlobalObjUI.LMan.GetString("framefile");
  				mainwindow_Ui.MenuFileSaveSim.Enabled = isSensitive;
  			}
  		}
  		
  		
  		
  		
  		/// <summary>
  		/// Close contacts file
  		/// </summary>
  		private void CloseContactsFile()
  		{
  			mainwindow_Ui.LstFileContacts.Clear();
  			GlobalObjUI.ContactsFilePath = "";
  			UpdateFileControls(false);
  		}
  		
  		
  		
  		
  
  		/// <summary>
  		/// Update sim widgets status
  		/// </summary>
  		private void UpdateSimControls(bool isSensitive)
  		{
  			mainwindow_Ui.MenuSimConnect.Enabled = !isSensitive;
  			mainwindow_Ui.MenuSimSaveSim.Enabled = isSensitive;
  			mainwindow_Ui.MenuSimSaveFile.Enabled = isSensitive;
  			mainwindow_Ui.MenuSimDeleteAll.Enabled = isSensitive;
  			mainwindow_Ui.MenuSimPin.Enabled = isSensitive;
  			mainwindow_Ui.MenuSimDisconnect.Enabled = isSensitive;
  			mainwindow_Ui.LstSimContacts.Enabled = isSensitive;
  			
  			if (isSensitive)
  			{
  				// add iccid to frame label
  				mainwindow_Ui.FrameSim.Title = GlobalObjUI.LMan.GetString("framesim") + " [" +
  					GlobalObjUI.SimIccID + " - size: " + GlobalObjUI.SimADNRecordCount.ToString() + "]"; 
  				
  				mainwindow_Ui.StatusBar.ShowMessage(GlobalObjUI.LMan.GetString("recordnoempty") + 
  					GlobalObjUI.SimADNRecordNoEmpty.ToString());
  				
  				// check for File area enabled
  				if (mainwindow_Ui.LstFileContacts.Enabled)
  				{
  					mainwindow_Ui.MenuFileSaveSim.Enabled = isSensitive;
  				}
  				else
  				{
  					mainwindow_Ui.MenuFileSaveSim.Enabled = false;
  				}
  				
  			}
  			else
  			{
  				// clear frame label
  				mainwindow_Ui.FrameSim.Title = GlobalObjUI.LMan.GetString("framesim");
  				mainwindow_Ui.MenuFileSaveSim.Enabled = isSensitive;
  			}
  			
  		}
  		
  		
  		
  		/// <summary>
  		/// Enable only change pin1 and disconnect actions
  		/// </summary>
  		private void EnableSimPinControl()
  		{
  			mainwindow_Ui.MenuSimConnect.Enabled = false;
  			mainwindow_Ui.MenuSimPin.Enabled = true;
  			mainwindow_Ui.MenuSimDisconnect.Enabled = true;			
  		}
  		
  		
  		
  		/// <summary>
  		/// Open contacts file.
  		/// </summary>
  		private void OpenContactsFile()
  		{
  			GlobalObjUI.ContactsFilePath = "";
  			
  			// New dialog for select contacts file 
  			string selectedFile = QFileDialog.GetOpenFileName(this, 
                                                          GlobalObjUI.LMan.GetString("openfileact"),
                                                          null,
                                                          "monosim files *.monosim (*.monosim)");
              
  			if (string.IsNullOrEmpty(selectedFile))
              {
              	return;
              }
  			
  			// path of a right file returned
  			GlobalObjUI.ContactsFilePath = selectedFile;
  			
  			// Update gui
  			UpdateFileControls(false);	
  					
  			// Clear ListView
  			mainwindow_Ui.LstFileContacts.Clear();			
  			MainClass.QtWait();
  			
  			try
  			{
  				GlobalObjUI.FileContacts = new Contacts();
  				StreamReader sr = new StreamReader(GlobalObjUI.ContactsFilePath);
  				string descRow = sr.ReadLine();			
  				string phoneRow = "";
  				while (!sr.EndOfStream)
  				{
  					phoneRow = sr.ReadLine();
  					// check for right values
  					if (descRow.Trim() != "" && phoneRow.Trim() != "")
  					{
  						GlobalObjUI.FileContacts.SimContacts.Add(new Contact(descRow, phoneRow));
  					}
  					
  					// read new contact description
  					descRow = sr.ReadLine();
  				}
  				sr.Close();
  				sr.Dispose();
  				sr = null;			
  				
  			}
  			catch (Exception Ex)
  			{
  				log.Error("MainWindowClass::OpenContactsFile: " + Ex.Message + "\r\n" + Ex.StackTrace);
  				MainClass.ShowMessage(this, "ERROR", Ex.Message,MainClass.MessageType.Error);
  				return;
  			}
  			
  			// loop to append data readed from file			
  			List<string> rowContent = null;
  			foreach(Contact cnt in GlobalObjUI.FileContacts.SimContacts)
  			{
  				rowContent = new List<string>();
  				rowContent.Add(" ");
  				rowContent.Add(cnt.Description);
  				rowContent.Add(cnt.PhoneNumber);
  				new QTreeWidgetItem(mainwindow_Ui.LstFileContacts, rowContent);				
  			}
  			
  			UpdateFileControls(true);
  			
  		}
  		
  		
  		
  		
  
  		/// <summary>
  		/// Perform sim card connection and contacts read.
  		/// </summary>
  		private void SimConnect()
  		{
  			MainClass.QtWait();
  			
  			if (GlobalObj.IsPowered)
  			{
  				// Disconnect card if needed
  				GlobalObj.CloseConnection();
  			}
  			
  			// Connect to smartcard
  
gt: 			retStr = GlobalObj.AnswerToReset(ref ATR);