/* /////////////////////////////////////////////////////////////////////////////
 * Application main window constructor
 */
TTCutMainWindow::TTCutMainWindow() 
: QMainWindow()
{

  // setup Qt Designer UI
  setupUi( this );

  // images
  // --------------------------------------------------------------------------
  TTCut::imgDownArrow  = new QPixmap( downarrow_18_xpm );
  TTCut::imgUpArrow    = new QPixmap( uparrow_18_xpm );
  TTCut::imgDelete     = new QPixmap( cancel_18_xpm );
  TTCut::imgFileOpen24 = new QPixmap( fileopen_24_xpm );
  TTCut::imgFileNew    = new QPixmap( filenew_16_xpm );
  TTCut::imgFileOpen   = new QPixmap( fileopen_16_xpm );
  TTCut::imgFileSave   = new QPixmap( filesave_16_xpm );;
  TTCut::imgFileSaveAs = new QPixmap( filesaveas_16_xpm );
  TTCut::imgSaveImage  = new QPixmap( saveimage_16_xpm );
  TTCut::imgSettings   = new QPixmap( settings_16_xpm );
  TTCut::imgSettings18 = new QPixmap( settings_18_xpm );
  TTCut::imgExit       = new QPixmap( exit_16_xpm );
  TTCut::imgPlay       = new QPixmap( play_18_xpm );
  TTCut::imgStop       = new QPixmap( stop_18_xpm );
  TTCut::imgSearch     = new QPixmap( search_18_xpm );
  TTCut::imgChapter    = new QPixmap( chapter_18_xpm );
  TTCut::imgPreview    = new QPixmap( preview_18_xpm );
  TTCut::imgCutAV      = new QPixmap( cutav_18_xpm );
  TTCut::imgCutAudio   = new QPixmap( cutaudio_18_xpm );
  TTCut::imgGoTo       = new QPixmap( goto_18_xpm );
  TTCut::imgMarker     = new QPixmap( note_18_xpm );
  TTCut::imgClock      = new QPixmap( clock_16_xpm );
  TTCut::imgApply      = new QPixmap( apply_18_xpm );
  TTCut::imgAddToList  = new QPixmap( addtolist_18_xpm );
  TTCut::imgFileClose  = new QPixmap( fileclose_18_xpm );

  setFocusPolicy(Qt::StrongFocus);

  // Message logger instance
  log = TTMessageLogger::getInstance();
  
  // Get the current Qt version at runtime
  log->infoMsg(oName, "TTCut-Version: %s", qPrintable(TTCut::versionString));
  log->infoMsg(oName, "Qt-Version:    %s", qVersion());

#if QT_VERSION < 0x040100
  // TODO: Show message box and abort session
  log->errorMsg(oName, "Qt-Version >= 4.1.0 required");
#endif

  // Settings
  TTCut::recentFileList.clear();
  settings = new TTCutSettings();
  settings->readSettings();
  log->enableLogFile(TTCut::createLogFile);
  log->setLogModeConsole(TTCut::logModeConsole);
  log->setLogModeExtended(TTCut::logModeExtended);

 
  // Audio list
  audioList = new TTAudioListData();
  audioFileInfo->setListData(audioList);

  // Mux list and mplex provider (later by plugin)
  muxListData   = new TTMuxListData();
  mplexProvider = new TTMplexProvider();

  // no navigation
  navigationEnabled( false );
 
  // init
  cutListData            = NULL;
  mpegStream             = NULL;
  TTCut::isVideoOpen     = false;
  TTCut::projectFileName = "";
  
  // Signal and slot connections
  // 
  // Connect signals from main menu
  // --------------------------------------------------------------------------
  connect(actionOpenVideo,        SIGNAL(triggered()), videoFileInfo, SLOT(onFileOpen()));
  connect(actionOpenAudio,        SIGNAL(triggered()), audioFileInfo, SLOT(onFileOpen()));
  connect(actionFileNew,          SIGNAL(triggered()), SLOT(onFileNew()));
  connect(actionFileOpen,         SIGNAL(triggered()), SLOT(onFileOpen()));
  connect(actionFileSave,         SIGNAL(triggered()), SLOT(onFileSave()));
  connect(actionFileSaveAs,       SIGNAL(triggered()), SLOT(onFileSaveAs()));
  connect(actionExit,             SIGNAL(triggered()), SLOT(onFileExit()));
  connect(actionSaveCurrentFrame, SIGNAL(triggered()), SLOT(onActionSave()));
  connect(actionSettings,         SIGNAL(triggered()), SLOT(onActionSettings()));
  connect(actionAbout,            SIGNAL(triggered()), SLOT(onHelpAbout()));

  // recent files
  for (int i = 0; i < MaxRecentFiles; ++i) {
    recentFileAction[i] = new QAction(this);
    recentFileAction[i]->setVisible(false);
    menuRecentProjects->addAction(recentFileAction[i]);
    connect(recentFileAction[i], SIGNAL(triggered()), SLOT(onFileRecent()));
  }
 
  updateRecentFileActions();

  // Connect signals from video and audio info
  // --------------------------------------------------------------------------
  connect(videoFileInfo,          SIGNAL(fileOpened(QString)), SLOT(onReadVideoStream(QString)));
  connect(audioFileInfo,          SIGNAL(fileOpened(QString)), SLOT(onReadAudioStream(QString)));

  // Connect signals from navigation widget
  // --------------------------------------------------------------------------
  connect(navigation, SIGNAL(prevIFrame()),      currentFrame, SLOT(onPrevIFrame()));
  connect(navigation, SIGNAL(nextIFrame()),      currentFrame, SLOT(onNextIFrame()));
  connect(navigation, SIGNAL(prevPFrame()),      currentFrame, SLOT(onPrevPFrame()));
  connect(navigation, SIGNAL(nextPFrame()),      currentFrame, SLOT(onNextPFrame()));
  connect(navigation, SIGNAL(prevBFrame()),      currentFrame, SLOT(onPrevBFrame()));
  connect(navigation, SIGNAL(nextBFrame()),      currentFrame, SLOT(onNextBFrame()));
  connect(navigation, SIGNAL(setCutOut(int)),    currentFrame, SLOT(onSetCutOut(int)));
  connect(navigation, SIGNAL(setCutOut(int)),    cutOutFrame,  SLOT(onGotoCutOut(int)));
  connect(navigation, SIGNAL(setCutIn(int)),     currentFrame, SLOT(onSetCutIn(int)));
  connect(navigation, SIGNAL(gotoCutIn(int)),    currentFrame, SLOT(onGotoCutIn(int)));
  connect(navigation, SIGNAL(gotoCutOut(int)),   currentFrame, SLOT(onGotoCutOut(int)));
  connect(navigation, SIGNAL(addCutRange(int, int)), cutList,   SLOT(onAddEntry(int, int)));
  connect(navigation, SIGNAL(gotoMarker(int)),   currentFrame, SLOT(onGotoMarker(int)));
  connect(navigation, SIGNAL(moveNumSteps(int)), currentFrame, SLOT(onMoveNumSteps(int)));
  connect(navigation, SIGNAL(moveToHome()),      currentFrame, SLOT(onMoveToHome()));
  connect(navigation, SIGNAL(moveToEnd()),       currentFrame, SLOT(onMoveToEnd()));

  // Connect signal from video slider
  // --------------------------------------------------------------------------
  connect(streamNavigator, SIGNAL(sliderValueChanged(int)), SLOT(onVideoSliderChanged(int)));

  // Connect signals from cut-out frame widget
  // --------------------------------------------------------------------------
  connect(cutOutFrame, SIGNAL(equalFrameFound(int)), currentFrame, SLOT(onGotoFrame(int)));
  connect(cutOutFrame, SIGNAL(newCutOutFramePos(int)), cutList,    SLOT(onEditCutOut(int)));

  // Connect signals from current frame widget
  // --------------------------------------------------------------------------
  connect(currentFrame, SIGNAL(newFramePosition(int)), SLOT(onNewFramePos(int))); 

  // Connect signals from cut list widget
  // --------------------------------------------------------------------------
  connect(cutList, SIGNAL(entrySelected(int)), cutOutFrame,     SLOT(onGotoCutOut(int)));
  connect(cutList, SIGNAL(entryEdit(const TTCutListDataItem&)), 
                                              navigation,       SLOT(onEditCut(const TTCutListDataItem&)));
  connect(cutList, SIGNAL(gotoCutIn(int)),     currentFrame,    SLOT(onGotoFrame(int)));
  connect(cutList, SIGNAL(gotoCutOut(int)),    currentFrame,    SLOT(onGotoFrame(int)));
  connect(cutList, SIGNAL(refreshDisplay()),   streamNavigator, SLOT(onRefreshDisplay()));
  connect(cutList, SIGNAL(previewCut(int)),                     SLOT(onPreviewCut(int)));
  connect(cutList, SIGNAL(audioVideoCut(int)),                  SLOT(onAudioVideoCut(int)));
  connect(cutList, SIGNAL(audioCut(int)),                       SLOT(onAudioCut(int)));
}
QTPcapReplay::QTPcapReplay(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	/* Connects  */
	m_jobReplay = new JobReplay;
	m_jobReplay->SetPcapReplay(this);
	m_jobReplay->SetUI(&ui);

	//BUTTONACT(ui.removeFileButton, removeFile());
	connect(ui.removeFileButton, SIGNAL(pressed()), this, SLOT(removeFile()));
	connect(ui.exitAppButton, SIGNAL(pressed()), this, SLOT(onFileExit()));
	connect(ui.actionFiles, SIGNAL(triggered()), this, SLOT(onFileOpen()));
	connect(ui.actionFrom_Folder, SIGNAL(triggered()), this, SLOT(onFolderOpen()));
	connect(ui.actionClose, SIGNAL(triggered()), this, SLOT(onFileExit()));
	connect(ui.actionNew_Window, SIGNAL(triggered()), this, SLOT(onNewWindow()));
	connect(ui.actionNew_Window_Dynamic, SIGNAL(triggered()), this, SLOT(onNewWindowDynamic()));
	connect(ui.actionScheduler, SIGNAL(triggered()), this, SLOT(onSchedulerDynamic()));
	connect(ui.startReplayButton, SIGNAL(pressed()), m_jobReplay, SLOT(StartReplay()));
	connect(ui.pauseReplayButton, SIGNAL(pressed()), this, SLOT(onPause()));
	connect(ui.stopReplayButton, SIGNAL(pressed()), this, SLOT(StopPlaying()));
	connect(m_jobReplay, SIGNAL(updateProgress(const int&)), this, SLOT(getProgress(int)));
	connect(m_jobReplay, SIGNAL(updateNextPacketNumber(const QString&)), this, SLOT(getNextPacketNumber(QString)));
	connect(m_jobReplay, SIGNAL(updateCurrentPlayingFile(const QString&)), this, SLOT(getCurStatus(QString)));
	connect(m_jobReplay, SIGNAL(updatePlayingStatus(const bool&)), this, SLOT(updatePlayingStatus(bool)));
	connect(&(m_SchedulerTimer.m_timer), SIGNAL(timeout()), this, SLOT(handleTimeout()));
	connect(&(m_NetSpeedMeasure), SIGNAL(UpdateNetSpeed(double)), this, SLOT(updateNetSpeedMeasure(double)));
	connect(&(m_PacketTimeReportor), SIGNAL(UpdateTimeReport(QString)), this, SLOT(PrintTimeReport(QString)));
	connect(m_jobReplay, SIGNAL(sendMessageBox(const QString&)), this, SLOT(showMessageBox(const QString&)));
	connect(ui.regularReplayButton, SIGNAL(pressed()), m_jobReplay, SLOT(startRegularReplay()));
	connect(m_jobReplay, SIGNAL(updateRegularReplayingStatus(const bool&)), this, SLOT(updateRegularReplayingStatus(bool)));
	connect(m_jobReplay, SIGNAL(sendUpdateRegularButtonText(const QString&)), this, SLOT(updateRegularButtonText(const QString&)));
	connect(ui.actionSave_Config, SIGNAL(triggered()), this, SLOT(save_config_setting()));
	connect(ui.actionLoad_Config, SIGNAL(triggered()), this, SLOT(load_config_setting()));

	ui.progressBar->setValue(0);

	if (InitAdapterName() == 1)
		exit(0);

	m_playerTimer = new PlayerTimer;
	m_threadPool = new threadPool(2);
	m_threadPool->submit(m_playerTimer);

	// Modify display
	ui.curPcapPackage->setText(QString(""));
	ui.StatusReportLabel->setText(QString(""));
	ui.schedulerStatus->setText(QString("Scheduler Stop"));
	ui.schedulerStatus->setStyleSheet("QLabel { background-color : red; color : black; }");
	ui.pauseReplayButton->setDisabled(true);
	ui.stopReplayButton->setDisabled(true);
	ui.offSet_lineEdit->setText("0");
	ui.speed_LineEdit->setText("1");
	ui.netSpeedMon->setText("");
	ui.packetTimeLabel->setText("");

	// Status
	m_bPlaying = false;
	m_bBeforePlay = true;

	
	m_openFilePath = QDir::currentPath();

	// net speed measurement

	// icon
	//m_icon.addFile("C:\NGN2\NGN2011\App\QTPcapReplay\QTPcapReplay\test.bmp");
	//this->setWindowIcon(m_icon);
}