Source

miuler-mocksmtp / src / main / scala / com / miuler / mocksmtp / gui / MainGUI.scala

package com.miuler.mocksmtp.gui

import com.trolltech.qt.gui._
import com.miuler.mocksmtp.core.MockSMTP
import org.slf4j.LoggerFactory
import com.miuler.mocksmtp.utils.Utils.showMessageE
import com.trolltech.qt.QThread
import com.trolltech.qt.core.Qt.ConnectionType
import collection.mutable.ListBuffer
import org.apache.james.mime4j.message.DefaultMessageBuilder
import java.io.ByteArrayInputStream
import org.apache.james.mime4j.dom.Message
import com.trolltech.qt.gui.QPalette.ColorRole


/**
 * Created with IntelliJ IDEA.
 * User: miuler
 * Date: 13/09/12
 * Time: 08:16 PM
 * To change this template use File | Settings | File Templates.
 */
class MainGUI(id: String = null, serverId: String = null) {
  QApplication.initialize(Array())
  new MainWindowSMTP()
  QApplication.exec()
}

/**
 * Created with IntelliJ IDEA.
 * User: miuler
 * Date: 13/09/12
 * Time: 08:16 PM
 * To change this template use File | Settings | File Templates.
 */
class MainWindowSMTP(id: String = null, serverId: String = null) extends QWidget {

  private val log = LoggerFactory.getLogger(classOf[MainGUI])
  private val lcd = new QLCDNumber()
  private var btnInitSMTP: QPushButton = _
  private var btnStopSMTP: QPushButton = _
  private var emails = ListBuffer[Message]()

  private var mockSMTP: MockSMTP = _
  private val connectionTheadNetty = new QConnectionThreadNetty
  private var threadConnectionThreadNetty: QThread = _

  initGUI
  initConnectionThread

  private def initGUI {
    btnInitSMTP = new QPushButton()
    btnInitSMTP.setCheckable(true)
    btnInitSMTP.setText("Iniciar servidor SMTP")
    btnInitSMTP.toggled.connect(this, "startSMTP()")

    btnStopSMTP = new QPushButton()
    btnStopSMTP.setCheckable(true)
    btnStopSMTP.setText("Parar servidor SMTP")
    btnStopSMTP.toggle()
    btnStopSMTP.setDisabled(true)
    btnStopSMTP.toggled.connect(this, "stopSMTP()")

    lcd.setDigitCount(3)
    lcd.setBackgroundRole(ColorRole.Dark)

    val hlayout = new QHBoxLayout()
    hlayout.addWidget(btnInitSMTP)
    hlayout.addWidget(btnStopSMTP)
    hlayout.addWidget(lcd)

    val vlayout = new QVBoxLayout()
    vlayout.addItem(hlayout)
    setLayout(hlayout)
    setWindowTitle("MOCK SMTP")
    show()
  }

  def initConnectionThread {
    threadConnectionThreadNetty = new QThread (connectionTheadNetty)
    connectionTheadNetty.moveToThread(threadConnectionThreadNetty)
    connectionTheadNetty.messageSignal.connect(this, "addEmail(String)", ConnectionType.QueuedConnection)
    threadConnectionThreadNetty.start
    mockSMTP = new MockSMTP(connectionTheadNetty)
  }

  def startSMTP() {
    try {
      if (btnInitSMTP.isChecked()) {
        mockSMTP.start
        btnInitSMTP.setDisabled(true)
        btnStopSMTP.toggle()
        btnStopSMTP.setDisabled(false)
        lcd.setBackgroundRole(ColorRole.Light)
      }
    } catch {
      case e: Exception => {
        log.error("Error al iniciar servidor SMTP", e)
        showMessageE(e)
      }
    }
  }

  def stopSMTP() {
    if (btnStopSMTP.isChecked()) {
      mockSMTP.stop
      btnInitSMTP.setDisabled(false)
      btnInitSMTP.toggle()
      btnStopSMTP.setDisabled(true)
      lcd.setBackgroundRole(ColorRole.Dark)
    }
  }

  override def closeEvent(evn: QCloseEvent) {
    log.debug("salir")
    if (btnInitSMTP.isChecked()) {
      log.debug("Apagando el servidor")
      mockSMTP.stop
    }
    if (threadConnectionThreadNetty.isAlive) {
      log.debug("threadConnectionThreadNetty isAlive: {}", threadConnectionThreadNetty.isAlive)
      connectionTheadNetty.loopConnectionNetty = false
      connectionTheadNetty.mutex.unlock
      log.debug("threadConnectionThreadNetty isAlive: {}", threadConnectionThreadNetty.isAlive)
    }
  }

  def addEmail(msg: String) {
    var email = new DefaultMessageBuilder().parseMessage(new ByteArrayInputStream(msg.getBytes()))
    emails.append(email)
    lcd.display(emails.size)
  }

}