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 java.io.ByteArrayInputStream
import com.trolltech.qt.gui.QPalette.ColorRole
import com.trolltech.qt.core.Qt
import java.util.Date
import javax.mail.Session
import javax.mail.internet.MimeMessage
import java.util.Properties

/**
 * 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 session = Session.getInstance(new Properties)
  private var emails = ListBuffer[MimeMessage]()
  private val lcd = new QLCDNumber()
  private var btnInitSMTP: QPushButton = _
  private var btnStopSMTP: QPushButton = _
  private var treeView = new QTreeView
  private var model: QStandardItemModel = _

  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)
    // =========================================================================

    // =========================================================================
    model = new QStandardItemModel(0, 3, this)
    model.setHeaderData(0, Qt.Orientation.Horizontal, tr("id"))
    model.setHeaderData(1, Qt.Orientation.Horizontal, tr("Fecha de envio"))
    model.setHeaderData(2, Qt.Orientation.Horizontal, tr("Asunto"))
    treeView.setRootIsDecorated(false)
    treeView.setAlternatingRowColors(true)
    treeView.setModel(model)
    treeView.setSortingEnabled(true)
    treeView.sortByColumn(1, Qt.SortOrder.AscendingOrder)
    treeView.setMinimumSize(300, 200) //TODO: @Miuler: Esto es para evitar un error en algunas maquinas
    // =========================================================================

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

    // =========================================================================
    val vlayout = new QVBoxLayout()
    vlayout.addItem(hlayout)
    vlayout.addWidget(treeView)
    setLayout(vlayout)
    setWindowTitle("MOCK SMTP")
    resize(320, 240)
    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) {
    log.debug("email_string: {}", msg)
    val email = new MimeMessage(session, new ByteArrayInputStream(msg.getBytes()))

    log.debug("email: {}", email)
    log.debug("email.getContent: {}", email.getContent())
    log.debug("email.getSubject: {}", email.getSubject())
    emails.append(email)
    model.insertRow(0)
    model.setData(model.index(0, 0), model.rowCount())
    model.setData(model.index(0, 1), new Date toString)
    model.setData(model.index(0, 2), email.getSubject())
    model.setData(model.index(0, 3), email)
    lcd.display(emails.size)
  }

}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.